OSDN Git Service

* dwarf2out.c (add_name_and_src_coords_attributes): Push on the
[pf3gnuchains/gcc-fork.git] / gcc / dwarf2out.c
1 /* Output Dwarf2 format symbol table information from GCC.
2    Copyright (C) 1992, 1993, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
3    2003, 2004, 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
4    Contributed by Gary Funck (gary@intrepid.com).
5    Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
6    Extensively modified by Jason Merrill (jason@cygnus.com).
7
8 This file is part of GCC.
9
10 GCC is free software; you can redistribute it and/or modify it under
11 the terms of the GNU General Public License as published by the Free
12 Software Foundation; either version 3, or (at your option) any later
13 version.
14
15 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
16 WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
18 for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING3.  If not see
22 <http://www.gnu.org/licenses/>.  */
23
24 /* TODO: Emit .debug_line header even when there are no functions, since
25            the file numbers are used by .debug_info.  Alternately, leave
26            out locations for types and decls.
27          Avoid talking about ctors and op= for PODs.
28          Factor out common prologue sequences into multiple CIEs.  */
29
30 /* The first part of this file deals with the DWARF 2 frame unwind
31    information, which is also used by the GCC efficient exception handling
32    mechanism.  The second part, controlled only by an #ifdef
33    DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
34    information.  */
35
36 /* DWARF2 Abbreviation Glossary:
37
38    CFA = Canonical Frame Address
39            a fixed address on the stack which identifies a call frame.
40            We define it to be the value of SP just before the call insn.
41            The CFA register and offset, which may change during the course
42            of the function, are used to calculate its value at runtime.
43
44    CFI = Call Frame Instruction
45            an instruction for the DWARF2 abstract machine
46
47    CIE = Common Information Entry
48            information describing information common to one or more FDEs
49
50    DIE = Debugging Information Entry
51
52    FDE = Frame Description Entry
53            information describing the stack call frame, in particular,
54            how to restore registers
55
56    DW_CFA_... = DWARF2 CFA call frame instruction
57    DW_TAG_... = DWARF2 DIE tag */
58
59 #include "config.h"
60 #include "system.h"
61 #include "coretypes.h"
62 #include "tm.h"
63 #include "tree.h"
64 #include "version.h"
65 #include "flags.h"
66 #include "real.h"
67 #include "rtl.h"
68 #include "hard-reg-set.h"
69 #include "regs.h"
70 #include "insn-config.h"
71 #include "reload.h"
72 #include "function.h"
73 #include "output.h"
74 #include "expr.h"
75 #include "libfuncs.h"
76 #include "except.h"
77 #include "dwarf2.h"
78 #include "dwarf2out.h"
79 #include "dwarf2asm.h"
80 #include "toplev.h"
81 #include "varray.h"
82 #include "ggc.h"
83 #include "md5.h"
84 #include "tm_p.h"
85 #include "diagnostic.h"
86 #include "debug.h"
87 #include "target.h"
88 #include "langhooks.h"
89 #include "hashtab.h"
90 #include "cgraph.h"
91 #include "input.h"
92
93 #ifdef DWARF2_DEBUGGING_INFO
94 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
95
96 static rtx last_var_location_insn;
97 #endif
98
99 #ifdef VMS_DEBUGGING_INFO
100 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
101
102 /* Define this macro to be a nonzero value if the directory specifications
103     which are output in the debug info should end with a separator.  */
104 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
105 /* Define this macro to evaluate to a nonzero value if GCC should refrain
106    from generating indirect strings in DWARF2 debug information, for instance
107    if your target is stuck with an old version of GDB that is unable to
108    process them properly or uses VMS Debug.  */
109 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
110 #else
111 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
112 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
113 #endif
114
115 #ifndef DWARF2_FRAME_INFO
116 # ifdef DWARF2_DEBUGGING_INFO
117 #  define DWARF2_FRAME_INFO \
118   (write_symbols == DWARF2_DEBUG || write_symbols == VMS_AND_DWARF2_DEBUG)
119 # else
120 #  define DWARF2_FRAME_INFO 0
121 # endif
122 #endif
123
124 /* Map register numbers held in the call frame info that gcc has
125    collected using DWARF_FRAME_REGNUM to those that should be output in
126    .debug_frame and .eh_frame.  */
127 #ifndef DWARF2_FRAME_REG_OUT
128 #define DWARF2_FRAME_REG_OUT(REGNO, FOR_EH) (REGNO)
129 #endif
130
131 /* Save the result of dwarf2out_do_frame across PCH.  */
132 static GTY(()) bool saved_do_cfi_asm = 0;
133
134 /* Decide whether we want to emit frame unwind information for the current
135    translation unit.  */
136
137 int
138 dwarf2out_do_frame (void)
139 {
140   /* We want to emit correct CFA location expressions or lists, so we
141      have to return true if we're going to output debug info, even if
142      we're not going to output frame or unwind info.  */
143   return (write_symbols == DWARF2_DEBUG
144           || write_symbols == VMS_AND_DWARF2_DEBUG
145           || DWARF2_FRAME_INFO || saved_do_cfi_asm
146 #ifdef DWARF2_UNWIND_INFO
147           || (DWARF2_UNWIND_INFO
148               && (flag_unwind_tables
149                   || (flag_exceptions && ! USING_SJLJ_EXCEPTIONS)))
150 #endif
151           );
152 }
153
154 /* Decide whether to emit frame unwind via assembler directives.  */
155
156 int
157 dwarf2out_do_cfi_asm (void)
158 {
159   int enc;
160
161 #ifdef MIPS_DEBUGGING_INFO
162   return false;
163 #endif
164   if (!flag_dwarf2_cfi_asm || !dwarf2out_do_frame ())
165     return false;
166   if (saved_do_cfi_asm || !eh_personality_libfunc)
167     return true;
168   if (!HAVE_GAS_CFI_PERSONALITY_DIRECTIVE)
169     return false;
170
171   /* Make sure the personality encoding is one the assembler can support.
172      In particular, aligned addresses can't be handled.  */
173   enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,/*global=*/1);
174   if ((enc & 0x70) != 0 && (enc & 0x70) != DW_EH_PE_pcrel)
175     return false;
176   enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,/*global=*/0);
177   if ((enc & 0x70) != 0 && (enc & 0x70) != DW_EH_PE_pcrel)
178     return false;
179
180   saved_do_cfi_asm = true;
181   return true;
182 }
183
184 /* The size of the target's pointer type.  */
185 #ifndef PTR_SIZE
186 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
187 #endif
188
189 /* Array of RTXes referenced by the debugging information, which therefore
190    must be kept around forever.  */
191 static GTY(()) VEC(rtx,gc) *used_rtx_array;
192
193 /* A pointer to the base of a list of incomplete types which might be
194    completed at some later time.  incomplete_types_list needs to be a
195    VEC(tree,gc) because we want to tell the garbage collector about
196    it.  */
197 static GTY(()) VEC(tree,gc) *incomplete_types;
198
199 /* A pointer to the base of a table of references to declaration
200    scopes.  This table is a display which tracks the nesting
201    of declaration scopes at the current scope and containing
202    scopes.  This table is used to find the proper place to
203    define type declaration DIE's.  */
204 static GTY(()) VEC(tree,gc) *decl_scope_table;
205
206 /* Pointers to various DWARF2 sections.  */
207 static GTY(()) section *debug_info_section;
208 static GTY(()) section *debug_abbrev_section;
209 static GTY(()) section *debug_aranges_section;
210 static GTY(()) section *debug_macinfo_section;
211 static GTY(()) section *debug_line_section;
212 static GTY(()) section *debug_loc_section;
213 static GTY(()) section *debug_pubnames_section;
214 static GTY(()) section *debug_pubtypes_section;
215 static GTY(()) section *debug_str_section;
216 static GTY(()) section *debug_ranges_section;
217 static GTY(()) section *debug_frame_section;
218
219 /* How to start an assembler comment.  */
220 #ifndef ASM_COMMENT_START
221 #define ASM_COMMENT_START ";#"
222 #endif
223
224 typedef struct dw_cfi_struct *dw_cfi_ref;
225 typedef struct dw_fde_struct *dw_fde_ref;
226 typedef union  dw_cfi_oprnd_struct *dw_cfi_oprnd_ref;
227
228 /* Call frames are described using a sequence of Call Frame
229    Information instructions.  The register number, offset
230    and address fields are provided as possible operands;
231    their use is selected by the opcode field.  */
232
233 enum dw_cfi_oprnd_type {
234   dw_cfi_oprnd_unused,
235   dw_cfi_oprnd_reg_num,
236   dw_cfi_oprnd_offset,
237   dw_cfi_oprnd_addr,
238   dw_cfi_oprnd_loc
239 };
240
241 typedef union GTY(()) dw_cfi_oprnd_struct {
242   unsigned int GTY ((tag ("dw_cfi_oprnd_reg_num"))) dw_cfi_reg_num;
243   HOST_WIDE_INT GTY ((tag ("dw_cfi_oprnd_offset"))) dw_cfi_offset;
244   const char * GTY ((tag ("dw_cfi_oprnd_addr"))) dw_cfi_addr;
245   struct dw_loc_descr_struct * GTY ((tag ("dw_cfi_oprnd_loc"))) dw_cfi_loc;
246 }
247 dw_cfi_oprnd;
248
249 typedef struct GTY(()) dw_cfi_struct {
250   dw_cfi_ref dw_cfi_next;
251   enum dwarf_call_frame_info dw_cfi_opc;
252   dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd1_desc (%1.dw_cfi_opc)")))
253     dw_cfi_oprnd1;
254   dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd2_desc (%1.dw_cfi_opc)")))
255     dw_cfi_oprnd2;
256 }
257 dw_cfi_node;
258
259 /* This is how we define the location of the CFA. We use to handle it
260    as REG + OFFSET all the time,  but now it can be more complex.
261    It can now be either REG + CFA_OFFSET or *(REG + BASE_OFFSET) + CFA_OFFSET.
262    Instead of passing around REG and OFFSET, we pass a copy
263    of this structure.  */
264 typedef struct GTY(()) cfa_loc {
265   HOST_WIDE_INT offset;
266   HOST_WIDE_INT base_offset;
267   unsigned int reg;
268   BOOL_BITFIELD indirect : 1;  /* 1 if CFA is accessed via a dereference.  */
269   BOOL_BITFIELD in_use : 1;    /* 1 if a saved cfa is stored here.  */
270 } dw_cfa_location;
271
272 /* All call frame descriptions (FDE's) in the GCC generated DWARF
273    refer to a single Common Information Entry (CIE), defined at
274    the beginning of the .debug_frame section.  This use of a single
275    CIE obviates the need to keep track of multiple CIE's
276    in the DWARF generation routines below.  */
277
278 typedef struct GTY(()) dw_fde_struct {
279   tree decl;
280   const char *dw_fde_begin;
281   const char *dw_fde_current_label;
282   const char *dw_fde_end;
283   const char *dw_fde_hot_section_label;
284   const char *dw_fde_hot_section_end_label;
285   const char *dw_fde_unlikely_section_label;
286   const char *dw_fde_unlikely_section_end_label;
287   dw_cfi_ref dw_fde_cfi;
288   dw_cfi_ref dw_fde_switch_cfi; /* Last CFI before switching sections.  */
289   unsigned funcdef_number;
290   HOST_WIDE_INT stack_realignment;
291   /* Dynamic realign argument pointer register.  */
292   unsigned int drap_reg;
293   /* Virtual dynamic realign argument pointer register.  */
294   unsigned int vdrap_reg;
295   unsigned all_throwers_are_sibcalls : 1;
296   unsigned nothrow : 1;
297   unsigned uses_eh_lsda : 1;
298   /* Whether we did stack realign in this call frame.  */
299   unsigned stack_realign : 1;
300   /* Whether dynamic realign argument pointer register has been saved.  */
301   unsigned drap_reg_saved: 1;
302   /* True iff dw_fde_begin label is in text_section or cold_text_section.  */
303   unsigned in_std_section : 1;
304   /* True iff dw_fde_unlikely_section_label is in text_section or
305      cold_text_section.  */
306   unsigned cold_in_std_section : 1;
307   /* True iff switched sections.  */
308   unsigned dw_fde_switched_sections : 1;
309   /* True iff switching from cold to hot section.  */
310   unsigned dw_fde_switched_cold_to_hot : 1;
311 }
312 dw_fde_node;
313
314 /* Maximum size (in bytes) of an artificially generated label.  */
315 #define MAX_ARTIFICIAL_LABEL_BYTES      30
316
317 /* The size of addresses as they appear in the Dwarf 2 data.
318    Some architectures use word addresses to refer to code locations,
319    but Dwarf 2 info always uses byte addresses.  On such machines,
320    Dwarf 2 addresses need to be larger than the architecture's
321    pointers.  */
322 #ifndef DWARF2_ADDR_SIZE
323 #define DWARF2_ADDR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
324 #endif
325
326 /* The size in bytes of a DWARF field indicating an offset or length
327    relative to a debug info section, specified to be 4 bytes in the
328    DWARF-2 specification.  The SGI/MIPS ABI defines it to be the same
329    as PTR_SIZE.  */
330
331 #ifndef DWARF_OFFSET_SIZE
332 #define DWARF_OFFSET_SIZE 4
333 #endif
334
335 /* According to the (draft) DWARF 3 specification, the initial length
336    should either be 4 or 12 bytes.  When it's 12 bytes, the first 4
337    bytes are 0xffffffff, followed by the length stored in the next 8
338    bytes.
339
340    However, the SGI/MIPS ABI uses an initial length which is equal to
341    DWARF_OFFSET_SIZE.  It is defined (elsewhere) accordingly.  */
342
343 #ifndef DWARF_INITIAL_LENGTH_SIZE
344 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
345 #endif
346
347 /* Round SIZE up to the nearest BOUNDARY.  */
348 #define DWARF_ROUND(SIZE,BOUNDARY) \
349   ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
350
351 /* Offsets recorded in opcodes are a multiple of this alignment factor.  */
352 #ifndef DWARF_CIE_DATA_ALIGNMENT
353 #ifdef STACK_GROWS_DOWNWARD
354 #define DWARF_CIE_DATA_ALIGNMENT (-((int) UNITS_PER_WORD))
355 #else
356 #define DWARF_CIE_DATA_ALIGNMENT ((int) UNITS_PER_WORD)
357 #endif
358 #endif
359
360 /* CIE identifier.  */
361 #if HOST_BITS_PER_WIDE_INT >= 64
362 #define DWARF_CIE_ID \
363   (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
364 #else
365 #define DWARF_CIE_ID DW_CIE_ID
366 #endif
367
368 /* A pointer to the base of a table that contains frame description
369    information for each routine.  */
370 static GTY((length ("fde_table_allocated"))) dw_fde_ref fde_table;
371
372 /* Number of elements currently allocated for fde_table.  */
373 static GTY(()) unsigned fde_table_allocated;
374
375 /* Number of elements in fde_table currently in use.  */
376 static GTY(()) unsigned fde_table_in_use;
377
378 /* Size (in elements) of increments by which we may expand the
379    fde_table.  */
380 #define FDE_TABLE_INCREMENT 256
381
382 /* Get the current fde_table entry we should use.  */
383
384 static inline dw_fde_ref
385 current_fde (void)
386 {
387   return fde_table_in_use ? &fde_table[fde_table_in_use - 1] : NULL;
388 }
389
390 /* A list of call frame insns for the CIE.  */
391 static GTY(()) dw_cfi_ref cie_cfi_head;
392
393 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
394 /* Some DWARF extensions (e.g., MIPS/SGI) implement a subprogram
395    attribute that accelerates the lookup of the FDE associated
396    with the subprogram.  This variable holds the table index of the FDE
397    associated with the current function (body) definition.  */
398 static unsigned current_funcdef_fde;
399 #endif
400
401 struct GTY(()) indirect_string_node {
402   const char *str;
403   unsigned int refcount;
404   enum dwarf_form form;
405   char *label;
406 };
407
408 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
409
410 static GTY(()) int dw2_string_counter;
411 static GTY(()) unsigned long dwarf2out_cfi_label_num;
412
413 /* True if the compilation unit places functions in more than one section.  */
414 static GTY(()) bool have_multiple_function_sections = false;
415
416 /* Whether the default text and cold text sections have been used at all.  */
417
418 static GTY(()) bool text_section_used = false;
419 static GTY(()) bool cold_text_section_used = false;
420
421 /* The default cold text section.  */
422 static GTY(()) section *cold_text_section;
423
424 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
425
426 /* Forward declarations for functions defined in this file.  */
427
428 static char *stripattributes (const char *);
429 static const char *dwarf_cfi_name (unsigned);
430 static dw_cfi_ref new_cfi (void);
431 static void add_cfi (dw_cfi_ref *, dw_cfi_ref);
432 static void add_fde_cfi (const char *, dw_cfi_ref);
433 static void lookup_cfa_1 (dw_cfi_ref, dw_cfa_location *, dw_cfa_location *);
434 static void lookup_cfa (dw_cfa_location *);
435 static void reg_save (const char *, unsigned, unsigned, HOST_WIDE_INT);
436 #ifdef DWARF2_UNWIND_INFO
437 static void initial_return_save (rtx);
438 #endif
439 static HOST_WIDE_INT stack_adjust_offset (const_rtx, HOST_WIDE_INT,
440                                           HOST_WIDE_INT);
441 static void output_cfi (dw_cfi_ref, dw_fde_ref, int);
442 static void output_cfi_directive (dw_cfi_ref);
443 static void output_call_frame_info (int);
444 static void dwarf2out_note_section_used (void);
445 static void dwarf2out_stack_adjust (rtx, bool);
446 static void dwarf2out_args_size_adjust (HOST_WIDE_INT, const char *);
447 static void flush_queued_reg_saves (void);
448 static bool clobbers_queued_reg_save (const_rtx);
449 static void dwarf2out_frame_debug_expr (rtx, const char *);
450
451 /* Support for complex CFA locations.  */
452 static void output_cfa_loc (dw_cfi_ref);
453 static void output_cfa_loc_raw (dw_cfi_ref);
454 static void get_cfa_from_loc_descr (dw_cfa_location *,
455                                     struct dw_loc_descr_struct *);
456 static struct dw_loc_descr_struct *build_cfa_loc
457   (dw_cfa_location *, HOST_WIDE_INT);
458 static struct dw_loc_descr_struct *build_cfa_aligned_loc
459   (HOST_WIDE_INT, HOST_WIDE_INT);
460 static void def_cfa_1 (const char *, dw_cfa_location *);
461
462 /* How to start an assembler comment.  */
463 #ifndef ASM_COMMENT_START
464 #define ASM_COMMENT_START ";#"
465 #endif
466
467 /* Data and reference forms for relocatable data.  */
468 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
469 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
470
471 #ifndef DEBUG_FRAME_SECTION
472 #define DEBUG_FRAME_SECTION     ".debug_frame"
473 #endif
474
475 #ifndef FUNC_BEGIN_LABEL
476 #define FUNC_BEGIN_LABEL        "LFB"
477 #endif
478
479 #ifndef FUNC_END_LABEL
480 #define FUNC_END_LABEL          "LFE"
481 #endif
482
483 #ifndef FRAME_BEGIN_LABEL
484 #define FRAME_BEGIN_LABEL       "Lframe"
485 #endif
486 #define CIE_AFTER_SIZE_LABEL    "LSCIE"
487 #define CIE_END_LABEL           "LECIE"
488 #define FDE_LABEL               "LSFDE"
489 #define FDE_AFTER_SIZE_LABEL    "LASFDE"
490 #define FDE_END_LABEL           "LEFDE"
491 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
492 #define LINE_NUMBER_END_LABEL   "LELT"
493 #define LN_PROLOG_AS_LABEL      "LASLTP"
494 #define LN_PROLOG_END_LABEL     "LELTP"
495 #define DIE_LABEL_PREFIX        "DW"
496
497 /* The DWARF 2 CFA column which tracks the return address.  Normally this
498    is the column for PC, or the first column after all of the hard
499    registers.  */
500 #ifndef DWARF_FRAME_RETURN_COLUMN
501 #ifdef PC_REGNUM
502 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGNUM (PC_REGNUM)
503 #else
504 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGISTERS
505 #endif
506 #endif
507
508 /* The mapping from gcc register number to DWARF 2 CFA column number.  By
509    default, we just provide columns for all registers.  */
510 #ifndef DWARF_FRAME_REGNUM
511 #define DWARF_FRAME_REGNUM(REG) DBX_REGISTER_NUMBER (REG)
512 #endif
513 \f
514 /* Hook used by __throw.  */
515
516 rtx
517 expand_builtin_dwarf_sp_column (void)
518 {
519   unsigned int dwarf_regnum = DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM);
520   return GEN_INT (DWARF2_FRAME_REG_OUT (dwarf_regnum, 1));
521 }
522
523 /* Return a pointer to a copy of the section string name S with all
524    attributes stripped off, and an asterisk prepended (for assemble_name).  */
525
526 static inline char *
527 stripattributes (const char *s)
528 {
529   char *stripped = XNEWVEC (char, strlen (s) + 2);
530   char *p = stripped;
531
532   *p++ = '*';
533
534   while (*s && *s != ',')
535     *p++ = *s++;
536
537   *p = '\0';
538   return stripped;
539 }
540
541 /* MEM is a memory reference for the register size table, each element of
542    which has mode MODE.  Initialize column C as a return address column.  */
543
544 static void
545 init_return_column_size (enum machine_mode mode, rtx mem, unsigned int c)
546 {
547   HOST_WIDE_INT offset = c * GET_MODE_SIZE (mode);
548   HOST_WIDE_INT size = GET_MODE_SIZE (Pmode);
549   emit_move_insn (adjust_address (mem, mode, offset), GEN_INT (size));
550 }
551
552 /* Divide OFF by DWARF_CIE_DATA_ALIGNMENT, asserting no remainder.  */
553
554 static inline HOST_WIDE_INT
555 div_data_align (HOST_WIDE_INT off)
556 {
557   HOST_WIDE_INT r = off / DWARF_CIE_DATA_ALIGNMENT;
558   gcc_assert (r * DWARF_CIE_DATA_ALIGNMENT == off);
559   return r;
560 }
561
562 /* Return true if we need a signed version of a given opcode
563    (e.g. DW_CFA_offset_extended_sf vs DW_CFA_offset_extended).  */
564
565 static inline bool
566 need_data_align_sf_opcode (HOST_WIDE_INT off)
567 {
568   return DWARF_CIE_DATA_ALIGNMENT < 0 ? off > 0 : off < 0;
569 }
570
571 /* Generate code to initialize the register size table.  */
572
573 void
574 expand_builtin_init_dwarf_reg_sizes (tree address)
575 {
576   unsigned int i;
577   enum machine_mode mode = TYPE_MODE (char_type_node);
578   rtx addr = expand_normal (address);
579   rtx mem = gen_rtx_MEM (BLKmode, addr);
580   bool wrote_return_column = false;
581
582   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
583     {
584       int rnum = DWARF2_FRAME_REG_OUT (DWARF_FRAME_REGNUM (i), 1);
585
586       if (rnum < DWARF_FRAME_REGISTERS)
587         {
588           HOST_WIDE_INT offset = rnum * GET_MODE_SIZE (mode);
589           enum machine_mode save_mode = reg_raw_mode[i];
590           HOST_WIDE_INT size;
591
592           if (HARD_REGNO_CALL_PART_CLOBBERED (i, save_mode))
593             save_mode = choose_hard_reg_mode (i, 1, true);
594           if (DWARF_FRAME_REGNUM (i) == DWARF_FRAME_RETURN_COLUMN)
595             {
596               if (save_mode == VOIDmode)
597                 continue;
598               wrote_return_column = true;
599             }
600           size = GET_MODE_SIZE (save_mode);
601           if (offset < 0)
602             continue;
603
604           emit_move_insn (adjust_address (mem, mode, offset),
605                           gen_int_mode (size, mode));
606         }
607     }
608
609   if (!wrote_return_column)
610     init_return_column_size (mode, mem, DWARF_FRAME_RETURN_COLUMN);
611
612 #ifdef DWARF_ALT_FRAME_RETURN_COLUMN
613   init_return_column_size (mode, mem, DWARF_ALT_FRAME_RETURN_COLUMN);
614 #endif
615
616   targetm.init_dwarf_reg_sizes_extra (address);
617 }
618
619 /* Convert a DWARF call frame info. operation to its string name */
620
621 static const char *
622 dwarf_cfi_name (unsigned int cfi_opc)
623 {
624   switch (cfi_opc)
625     {
626     case DW_CFA_advance_loc:
627       return "DW_CFA_advance_loc";
628     case DW_CFA_offset:
629       return "DW_CFA_offset";
630     case DW_CFA_restore:
631       return "DW_CFA_restore";
632     case DW_CFA_nop:
633       return "DW_CFA_nop";
634     case DW_CFA_set_loc:
635       return "DW_CFA_set_loc";
636     case DW_CFA_advance_loc1:
637       return "DW_CFA_advance_loc1";
638     case DW_CFA_advance_loc2:
639       return "DW_CFA_advance_loc2";
640     case DW_CFA_advance_loc4:
641       return "DW_CFA_advance_loc4";
642     case DW_CFA_offset_extended:
643       return "DW_CFA_offset_extended";
644     case DW_CFA_restore_extended:
645       return "DW_CFA_restore_extended";
646     case DW_CFA_undefined:
647       return "DW_CFA_undefined";
648     case DW_CFA_same_value:
649       return "DW_CFA_same_value";
650     case DW_CFA_register:
651       return "DW_CFA_register";
652     case DW_CFA_remember_state:
653       return "DW_CFA_remember_state";
654     case DW_CFA_restore_state:
655       return "DW_CFA_restore_state";
656     case DW_CFA_def_cfa:
657       return "DW_CFA_def_cfa";
658     case DW_CFA_def_cfa_register:
659       return "DW_CFA_def_cfa_register";
660     case DW_CFA_def_cfa_offset:
661       return "DW_CFA_def_cfa_offset";
662
663     /* DWARF 3 */
664     case DW_CFA_def_cfa_expression:
665       return "DW_CFA_def_cfa_expression";
666     case DW_CFA_expression:
667       return "DW_CFA_expression";
668     case DW_CFA_offset_extended_sf:
669       return "DW_CFA_offset_extended_sf";
670     case DW_CFA_def_cfa_sf:
671       return "DW_CFA_def_cfa_sf";
672     case DW_CFA_def_cfa_offset_sf:
673       return "DW_CFA_def_cfa_offset_sf";
674
675     /* SGI/MIPS specific */
676     case DW_CFA_MIPS_advance_loc8:
677       return "DW_CFA_MIPS_advance_loc8";
678
679     /* GNU extensions */
680     case DW_CFA_GNU_window_save:
681       return "DW_CFA_GNU_window_save";
682     case DW_CFA_GNU_args_size:
683       return "DW_CFA_GNU_args_size";
684     case DW_CFA_GNU_negative_offset_extended:
685       return "DW_CFA_GNU_negative_offset_extended";
686
687     default:
688       return "DW_CFA_<unknown>";
689     }
690 }
691
692 /* Return a pointer to a newly allocated Call Frame Instruction.  */
693
694 static inline dw_cfi_ref
695 new_cfi (void)
696 {
697   dw_cfi_ref cfi = GGC_NEW (dw_cfi_node);
698
699   cfi->dw_cfi_next = NULL;
700   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = 0;
701   cfi->dw_cfi_oprnd2.dw_cfi_reg_num = 0;
702
703   return cfi;
704 }
705
706 /* Add a Call Frame Instruction to list of instructions.  */
707
708 static inline void
709 add_cfi (dw_cfi_ref *list_head, dw_cfi_ref cfi)
710 {
711   dw_cfi_ref *p;
712   dw_fde_ref fde = current_fde ();
713
714   /* When DRAP is used, CFA is defined with an expression.  Redefine
715      CFA may lead to a different CFA value.   */
716   /* ??? Of course, this heuristic fails when we're annotating epilogues,
717      because of course we'll always want to redefine the CFA back to the
718      stack pointer on the way out.  Where should we move this check?  */
719   if (0 && fde && fde->drap_reg != INVALID_REGNUM)
720     switch (cfi->dw_cfi_opc)
721       {
722         case DW_CFA_def_cfa_register:
723         case DW_CFA_def_cfa_offset:
724         case DW_CFA_def_cfa_offset_sf:
725         case DW_CFA_def_cfa:
726         case DW_CFA_def_cfa_sf:
727           gcc_unreachable ();
728
729         default:
730           break;
731       }
732
733   /* Find the end of the chain.  */
734   for (p = list_head; (*p) != NULL; p = &(*p)->dw_cfi_next)
735     ;
736
737   *p = cfi;
738 }
739
740 /* Generate a new label for the CFI info to refer to.  FORCE is true
741    if a label needs to be output even when using .cfi_* directives.  */
742
743 char *
744 dwarf2out_cfi_label (bool force)
745 {
746   static char label[20];
747
748   if (!force && dwarf2out_do_cfi_asm ())
749     {
750       /* In this case, we will be emitting the asm directive instead of
751          the label, so just return a placeholder to keep the rest of the
752          interfaces happy.  */
753       strcpy (label, "<do not output>");
754     }
755   else
756     {
757       ASM_GENERATE_INTERNAL_LABEL (label, "LCFI", dwarf2out_cfi_label_num++);
758       ASM_OUTPUT_LABEL (asm_out_file, label);
759     }
760
761   return label;
762 }
763
764 /* True if remember_state should be emitted before following CFI directive.  */
765 static bool emit_cfa_remember;
766
767 /* Add CFI to the current fde at the PC value indicated by LABEL if specified,
768    or to the CIE if LABEL is NULL.  */
769
770 static void
771 add_fde_cfi (const char *label, dw_cfi_ref cfi)
772 {
773   dw_cfi_ref *list_head;
774
775   if (emit_cfa_remember)
776     {
777       dw_cfi_ref cfi_remember;
778
779       /* Emit the state save.  */
780       emit_cfa_remember = false;
781       cfi_remember = new_cfi (); 
782       cfi_remember->dw_cfi_opc = DW_CFA_remember_state;
783       add_fde_cfi (label, cfi_remember);
784     }
785
786   list_head = &cie_cfi_head;
787
788   if (dwarf2out_do_cfi_asm ())
789     {
790       if (label)
791         {
792           dw_fde_ref fde = current_fde ();
793
794           gcc_assert (fde != NULL);
795
796           /* We still have to add the cfi to the list so that lookup_cfa
797              works later on.  When -g2 and above we even need to force
798              emitting of CFI labels and add to list a DW_CFA_set_loc for
799              convert_cfa_to_fb_loc_list purposes.  If we're generating
800              DWARF3 output we use DW_OP_call_frame_cfa and so don't use
801              convert_cfa_to_fb_loc_list.  */
802           if (dwarf_version == 2
803               && debug_info_level > DINFO_LEVEL_TERSE
804               && (write_symbols == DWARF2_DEBUG
805                   || write_symbols == VMS_AND_DWARF2_DEBUG))
806             {
807               switch (cfi->dw_cfi_opc)
808                 {
809                 case DW_CFA_def_cfa_offset:
810                 case DW_CFA_def_cfa_offset_sf:
811                 case DW_CFA_def_cfa_register:
812                 case DW_CFA_def_cfa:
813                 case DW_CFA_def_cfa_sf:
814                 case DW_CFA_def_cfa_expression:
815                 case DW_CFA_restore_state:
816                   if (*label == 0 || strcmp (label, "<do not output>") == 0)
817                     label = dwarf2out_cfi_label (true);
818
819                   if (fde->dw_fde_current_label == NULL
820                       || strcmp (label, fde->dw_fde_current_label) != 0)
821                     {
822                       dw_cfi_ref xcfi;
823
824                       label = xstrdup (label);
825
826                       /* Set the location counter to the new label.  */
827                       xcfi = new_cfi ();
828                       /* It doesn't metter whether DW_CFA_set_loc
829                          or DW_CFA_advance_loc4 is added here, those aren't
830                          emitted into assembly, only looked up by
831                          convert_cfa_to_fb_loc_list.  */
832                       xcfi->dw_cfi_opc = DW_CFA_set_loc;
833                       xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
834                       add_cfi (&fde->dw_fde_cfi, xcfi);
835                       fde->dw_fde_current_label = label;
836                     }
837                   break;
838                 default:
839                   break;
840                 }
841             }
842
843           output_cfi_directive (cfi);
844
845           list_head = &fde->dw_fde_cfi;
846         }
847       /* ??? If this is a CFI for the CIE, we don't emit.  This
848          assumes that the standard CIE contents that the assembler
849          uses matches the standard CIE contents that the compiler
850          uses.  This is probably a bad assumption.  I'm not quite
851          sure how to address this for now.  */
852     }
853   else if (label)
854     {
855       dw_fde_ref fde = current_fde ();
856
857       gcc_assert (fde != NULL);
858
859       if (*label == 0)
860         label = dwarf2out_cfi_label (false);
861
862       if (fde->dw_fde_current_label == NULL
863           || strcmp (label, fde->dw_fde_current_label) != 0)
864         {
865           dw_cfi_ref xcfi;
866
867           label = xstrdup (label);
868
869           /* Set the location counter to the new label.  */
870           xcfi = new_cfi ();
871           /* If we have a current label, advance from there, otherwise
872              set the location directly using set_loc.  */
873           xcfi->dw_cfi_opc = fde->dw_fde_current_label
874                              ? DW_CFA_advance_loc4
875                              : DW_CFA_set_loc;
876           xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
877           add_cfi (&fde->dw_fde_cfi, xcfi);
878
879           fde->dw_fde_current_label = label;
880         }
881
882       list_head = &fde->dw_fde_cfi;
883     }
884
885   add_cfi (list_head, cfi);
886 }
887
888 /* Subroutine of lookup_cfa.  */
889
890 static void
891 lookup_cfa_1 (dw_cfi_ref cfi, dw_cfa_location *loc, dw_cfa_location *remember)
892 {
893   switch (cfi->dw_cfi_opc)
894     {
895     case DW_CFA_def_cfa_offset:
896     case DW_CFA_def_cfa_offset_sf:
897       loc->offset = cfi->dw_cfi_oprnd1.dw_cfi_offset;
898       break;
899     case DW_CFA_def_cfa_register:
900       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
901       break;
902     case DW_CFA_def_cfa:
903     case DW_CFA_def_cfa_sf:
904       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
905       loc->offset = cfi->dw_cfi_oprnd2.dw_cfi_offset;
906       break;
907     case DW_CFA_def_cfa_expression:
908       get_cfa_from_loc_descr (loc, cfi->dw_cfi_oprnd1.dw_cfi_loc);
909       break;
910
911     case DW_CFA_remember_state:
912       gcc_assert (!remember->in_use);
913       *remember = *loc;
914       remember->in_use = 1;
915       break;
916     case DW_CFA_restore_state:
917       gcc_assert (remember->in_use);
918       *loc = *remember;
919       remember->in_use = 0;
920       break;
921
922     default:
923       break;
924     }
925 }
926
927 /* Find the previous value for the CFA.  */
928
929 static void
930 lookup_cfa (dw_cfa_location *loc)
931 {
932   dw_cfi_ref cfi;
933   dw_fde_ref fde;
934   dw_cfa_location remember;
935
936   memset (loc, 0, sizeof (*loc));
937   loc->reg = INVALID_REGNUM;
938   remember = *loc;
939
940   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
941     lookup_cfa_1 (cfi, loc, &remember);
942
943   fde = current_fde ();
944   if (fde)
945     for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
946       lookup_cfa_1 (cfi, loc, &remember);
947 }
948
949 /* The current rule for calculating the DWARF2 canonical frame address.  */
950 static dw_cfa_location cfa;
951
952 /* The register used for saving registers to the stack, and its offset
953    from the CFA.  */
954 static dw_cfa_location cfa_store;
955
956 /* The current save location around an epilogue.  */
957 static dw_cfa_location cfa_remember;
958
959 /* The running total of the size of arguments pushed onto the stack.  */
960 static HOST_WIDE_INT args_size;
961
962 /* The last args_size we actually output.  */
963 static HOST_WIDE_INT old_args_size;
964
965 /* Entry point to update the canonical frame address (CFA).
966    LABEL is passed to add_fde_cfi.  The value of CFA is now to be
967    calculated from REG+OFFSET.  */
968
969 void
970 dwarf2out_def_cfa (const char *label, unsigned int reg, HOST_WIDE_INT offset)
971 {
972   dw_cfa_location loc;
973   loc.indirect = 0;
974   loc.base_offset = 0;
975   loc.reg = reg;
976   loc.offset = offset;
977   def_cfa_1 (label, &loc);
978 }
979
980 /* Determine if two dw_cfa_location structures define the same data.  */
981
982 static bool
983 cfa_equal_p (const dw_cfa_location *loc1, const dw_cfa_location *loc2)
984 {
985   return (loc1->reg == loc2->reg
986           && loc1->offset == loc2->offset
987           && loc1->indirect == loc2->indirect
988           && (loc1->indirect == 0
989               || loc1->base_offset == loc2->base_offset));
990 }
991
992 /* This routine does the actual work.  The CFA is now calculated from
993    the dw_cfa_location structure.  */
994
995 static void
996 def_cfa_1 (const char *label, dw_cfa_location *loc_p)
997 {
998   dw_cfi_ref cfi;
999   dw_cfa_location old_cfa, loc;
1000
1001   cfa = *loc_p;
1002   loc = *loc_p;
1003
1004   if (cfa_store.reg == loc.reg && loc.indirect == 0)
1005     cfa_store.offset = loc.offset;
1006
1007   loc.reg = DWARF_FRAME_REGNUM (loc.reg);
1008   lookup_cfa (&old_cfa);
1009
1010   /* If nothing changed, no need to issue any call frame instructions.  */
1011   if (cfa_equal_p (&loc, &old_cfa))
1012     return;
1013
1014   cfi = new_cfi ();
1015
1016   if (loc.reg == old_cfa.reg && !loc.indirect)
1017     {
1018       /* Construct a "DW_CFA_def_cfa_offset <offset>" instruction, indicating
1019          the CFA register did not change but the offset did.  The data 
1020          factoring for DW_CFA_def_cfa_offset_sf happens in output_cfi, or
1021          in the assembler via the .cfi_def_cfa_offset directive.  */
1022       if (loc.offset < 0)
1023         cfi->dw_cfi_opc = DW_CFA_def_cfa_offset_sf;
1024       else
1025         cfi->dw_cfi_opc = DW_CFA_def_cfa_offset;
1026       cfi->dw_cfi_oprnd1.dw_cfi_offset = loc.offset;
1027     }
1028
1029 #ifndef MIPS_DEBUGGING_INFO  /* SGI dbx thinks this means no offset.  */
1030   else if (loc.offset == old_cfa.offset
1031            && old_cfa.reg != INVALID_REGNUM
1032            && !loc.indirect)
1033     {
1034       /* Construct a "DW_CFA_def_cfa_register <register>" instruction,
1035          indicating the CFA register has changed to <register> but the
1036          offset has not changed.  */
1037       cfi->dw_cfi_opc = DW_CFA_def_cfa_register;
1038       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
1039     }
1040 #endif
1041
1042   else if (loc.indirect == 0)
1043     {
1044       /* Construct a "DW_CFA_def_cfa <register> <offset>" instruction,
1045          indicating the CFA register has changed to <register> with
1046          the specified offset.  The data factoring for DW_CFA_def_cfa_sf
1047          happens in output_cfi, or in the assembler via the .cfi_def_cfa
1048          directive.  */
1049       if (loc.offset < 0)
1050         cfi->dw_cfi_opc = DW_CFA_def_cfa_sf;
1051       else
1052         cfi->dw_cfi_opc = DW_CFA_def_cfa;
1053       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
1054       cfi->dw_cfi_oprnd2.dw_cfi_offset = loc.offset;
1055     }
1056   else
1057     {
1058       /* Construct a DW_CFA_def_cfa_expression instruction to
1059          calculate the CFA using a full location expression since no
1060          register-offset pair is available.  */
1061       struct dw_loc_descr_struct *loc_list;
1062
1063       cfi->dw_cfi_opc = DW_CFA_def_cfa_expression;
1064       loc_list = build_cfa_loc (&loc, 0);
1065       cfi->dw_cfi_oprnd1.dw_cfi_loc = loc_list;
1066     }
1067
1068   add_fde_cfi (label, cfi);
1069 }
1070
1071 /* Add the CFI for saving a register.  REG is the CFA column number.
1072    LABEL is passed to add_fde_cfi.
1073    If SREG is -1, the register is saved at OFFSET from the CFA;
1074    otherwise it is saved in SREG.  */
1075
1076 static void
1077 reg_save (const char *label, unsigned int reg, unsigned int sreg, HOST_WIDE_INT offset)
1078 {
1079   dw_cfi_ref cfi = new_cfi ();
1080   dw_fde_ref fde = current_fde ();
1081
1082   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
1083
1084   /* When stack is aligned, store REG using DW_CFA_expression with
1085      FP.  */
1086   if (fde
1087       && fde->stack_realign
1088       && sreg == INVALID_REGNUM)
1089     {
1090       cfi->dw_cfi_opc = DW_CFA_expression;
1091       cfi->dw_cfi_oprnd2.dw_cfi_reg_num = reg;
1092       cfi->dw_cfi_oprnd1.dw_cfi_loc
1093         = build_cfa_aligned_loc (offset, fde->stack_realignment);
1094     }
1095   else if (sreg == INVALID_REGNUM)
1096     {
1097       if (need_data_align_sf_opcode (offset))
1098         cfi->dw_cfi_opc = DW_CFA_offset_extended_sf;
1099       else if (reg & ~0x3f)
1100         cfi->dw_cfi_opc = DW_CFA_offset_extended;
1101       else
1102         cfi->dw_cfi_opc = DW_CFA_offset;
1103       cfi->dw_cfi_oprnd2.dw_cfi_offset = offset;
1104     }
1105   else if (sreg == reg)
1106     cfi->dw_cfi_opc = DW_CFA_same_value;
1107   else
1108     {
1109       cfi->dw_cfi_opc = DW_CFA_register;
1110       cfi->dw_cfi_oprnd2.dw_cfi_reg_num = sreg;
1111     }
1112
1113   add_fde_cfi (label, cfi);
1114 }
1115
1116 /* Add the CFI for saving a register window.  LABEL is passed to reg_save.
1117    This CFI tells the unwinder that it needs to restore the window registers
1118    from the previous frame's window save area.
1119
1120    ??? Perhaps we should note in the CIE where windows are saved (instead of
1121    assuming 0(cfa)) and what registers are in the window.  */
1122
1123 void
1124 dwarf2out_window_save (const char *label)
1125 {
1126   dw_cfi_ref cfi = new_cfi ();
1127
1128   cfi->dw_cfi_opc = DW_CFA_GNU_window_save;
1129   add_fde_cfi (label, cfi);
1130 }
1131
1132 /* Add a CFI to update the running total of the size of arguments
1133    pushed onto the stack.  */
1134
1135 void
1136 dwarf2out_args_size (const char *label, HOST_WIDE_INT size)
1137 {
1138   dw_cfi_ref cfi;
1139
1140   if (size == old_args_size)
1141     return;
1142
1143   old_args_size = size;
1144
1145   cfi = new_cfi ();
1146   cfi->dw_cfi_opc = DW_CFA_GNU_args_size;
1147   cfi->dw_cfi_oprnd1.dw_cfi_offset = size;
1148   add_fde_cfi (label, cfi);
1149 }
1150
1151 /* Entry point for saving a register to the stack.  REG is the GCC register
1152    number.  LABEL and OFFSET are passed to reg_save.  */
1153
1154 void
1155 dwarf2out_reg_save (const char *label, unsigned int reg, HOST_WIDE_INT offset)
1156 {
1157   reg_save (label, DWARF_FRAME_REGNUM (reg), INVALID_REGNUM, offset);
1158 }
1159
1160 /* Entry point for saving the return address in the stack.
1161    LABEL and OFFSET are passed to reg_save.  */
1162
1163 void
1164 dwarf2out_return_save (const char *label, HOST_WIDE_INT offset)
1165 {
1166   reg_save (label, DWARF_FRAME_RETURN_COLUMN, INVALID_REGNUM, offset);
1167 }
1168
1169 /* Entry point for saving the return address in a register.
1170    LABEL and SREG are passed to reg_save.  */
1171
1172 void
1173 dwarf2out_return_reg (const char *label, unsigned int sreg)
1174 {
1175   reg_save (label, DWARF_FRAME_RETURN_COLUMN, DWARF_FRAME_REGNUM (sreg), 0);
1176 }
1177
1178 #ifdef DWARF2_UNWIND_INFO
1179 /* Record the initial position of the return address.  RTL is
1180    INCOMING_RETURN_ADDR_RTX.  */
1181
1182 static void
1183 initial_return_save (rtx rtl)
1184 {
1185   unsigned int reg = INVALID_REGNUM;
1186   HOST_WIDE_INT offset = 0;
1187
1188   switch (GET_CODE (rtl))
1189     {
1190     case REG:
1191       /* RA is in a register.  */
1192       reg = DWARF_FRAME_REGNUM (REGNO (rtl));
1193       break;
1194
1195     case MEM:
1196       /* RA is on the stack.  */
1197       rtl = XEXP (rtl, 0);
1198       switch (GET_CODE (rtl))
1199         {
1200         case REG:
1201           gcc_assert (REGNO (rtl) == STACK_POINTER_REGNUM);
1202           offset = 0;
1203           break;
1204
1205         case PLUS:
1206           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1207           offset = INTVAL (XEXP (rtl, 1));
1208           break;
1209
1210         case MINUS:
1211           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1212           offset = -INTVAL (XEXP (rtl, 1));
1213           break;
1214
1215         default:
1216           gcc_unreachable ();
1217         }
1218
1219       break;
1220
1221     case PLUS:
1222       /* The return address is at some offset from any value we can
1223          actually load.  For instance, on the SPARC it is in %i7+8. Just
1224          ignore the offset for now; it doesn't matter for unwinding frames.  */
1225       gcc_assert (CONST_INT_P (XEXP (rtl, 1)));
1226       initial_return_save (XEXP (rtl, 0));
1227       return;
1228
1229     default:
1230       gcc_unreachable ();
1231     }
1232
1233   if (reg != DWARF_FRAME_RETURN_COLUMN)
1234     reg_save (NULL, DWARF_FRAME_RETURN_COLUMN, reg, offset - cfa.offset);
1235 }
1236 #endif
1237
1238 /* Given a SET, calculate the amount of stack adjustment it
1239    contains.  */
1240
1241 static HOST_WIDE_INT
1242 stack_adjust_offset (const_rtx pattern, HOST_WIDE_INT cur_args_size,
1243                      HOST_WIDE_INT cur_offset)
1244 {
1245   const_rtx src = SET_SRC (pattern);
1246   const_rtx dest = SET_DEST (pattern);
1247   HOST_WIDE_INT offset = 0;
1248   enum rtx_code code;
1249
1250   if (dest == stack_pointer_rtx)
1251     {
1252       code = GET_CODE (src);
1253
1254       /* Assume (set (reg sp) (reg whatever)) sets args_size
1255          level to 0.  */
1256       if (code == REG && src != stack_pointer_rtx)
1257         {
1258           offset = -cur_args_size;
1259 #ifndef STACK_GROWS_DOWNWARD
1260           offset = -offset;
1261 #endif
1262           return offset - cur_offset;
1263         }
1264
1265       if (! (code == PLUS || code == MINUS)
1266           || XEXP (src, 0) != stack_pointer_rtx
1267           || !CONST_INT_P (XEXP (src, 1)))
1268         return 0;
1269
1270       /* (set (reg sp) (plus (reg sp) (const_int))) */
1271       offset = INTVAL (XEXP (src, 1));
1272       if (code == PLUS)
1273         offset = -offset;
1274       return offset;
1275     }
1276
1277   if (MEM_P (src) && !MEM_P (dest))
1278     dest = src;
1279   if (MEM_P (dest))
1280     {
1281       /* (set (mem (pre_dec (reg sp))) (foo)) */
1282       src = XEXP (dest, 0);
1283       code = GET_CODE (src);
1284
1285       switch (code)
1286         {
1287         case PRE_MODIFY:
1288         case POST_MODIFY:
1289           if (XEXP (src, 0) == stack_pointer_rtx)
1290             {
1291               rtx val = XEXP (XEXP (src, 1), 1);
1292               /* We handle only adjustments by constant amount.  */
1293               gcc_assert (GET_CODE (XEXP (src, 1)) == PLUS
1294                           && CONST_INT_P (val));
1295               offset = -INTVAL (val);
1296               break;
1297             }
1298           return 0;
1299
1300         case PRE_DEC:
1301         case POST_DEC:
1302           if (XEXP (src, 0) == stack_pointer_rtx)
1303             {
1304               offset = GET_MODE_SIZE (GET_MODE (dest));
1305               break;
1306             }
1307           return 0;
1308
1309         case PRE_INC:
1310         case POST_INC:
1311           if (XEXP (src, 0) == stack_pointer_rtx)
1312             {
1313               offset = -GET_MODE_SIZE (GET_MODE (dest));
1314               break;
1315             }
1316           return 0;
1317
1318         default:
1319           return 0;
1320         }
1321     }
1322   else
1323     return 0;
1324
1325   return offset;
1326 }
1327
1328 /* Precomputed args_size for CODE_LABELs and BARRIERs preceeding them,
1329    indexed by INSN_UID.  */
1330
1331 static HOST_WIDE_INT *barrier_args_size;
1332
1333 /* Helper function for compute_barrier_args_size.  Handle one insn.  */
1334
1335 static HOST_WIDE_INT
1336 compute_barrier_args_size_1 (rtx insn, HOST_WIDE_INT cur_args_size,
1337                              VEC (rtx, heap) **next)
1338 {
1339   HOST_WIDE_INT offset = 0;
1340   int i;
1341
1342   if (! RTX_FRAME_RELATED_P (insn))
1343     {
1344       if (prologue_epilogue_contains (insn))
1345         /* Nothing */;
1346       else if (GET_CODE (PATTERN (insn)) == SET)
1347         offset = stack_adjust_offset (PATTERN (insn), cur_args_size, 0);
1348       else if (GET_CODE (PATTERN (insn)) == PARALLEL
1349                || GET_CODE (PATTERN (insn)) == SEQUENCE)
1350         {
1351           /* There may be stack adjustments inside compound insns.  Search
1352              for them.  */
1353           for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1354             if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1355               offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i),
1356                                              cur_args_size, offset);
1357         }
1358     }
1359   else
1360     {
1361       rtx expr = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
1362
1363       if (expr)
1364         {
1365           expr = XEXP (expr, 0);
1366           if (GET_CODE (expr) == PARALLEL
1367               || GET_CODE (expr) == SEQUENCE)
1368             for (i = 1; i < XVECLEN (expr, 0); i++)
1369               {
1370                 rtx elem = XVECEXP (expr, 0, i);
1371
1372                 if (GET_CODE (elem) == SET && !RTX_FRAME_RELATED_P (elem))
1373                   offset += stack_adjust_offset (elem, cur_args_size, offset);
1374               }
1375         }
1376     }
1377
1378 #ifndef STACK_GROWS_DOWNWARD
1379   offset = -offset;
1380 #endif
1381
1382   cur_args_size += offset;
1383   if (cur_args_size < 0)
1384     cur_args_size = 0;
1385
1386   if (JUMP_P (insn))
1387     {
1388       rtx dest = JUMP_LABEL (insn);
1389
1390       if (dest)
1391         {
1392           if (barrier_args_size [INSN_UID (dest)] < 0)
1393             {
1394               barrier_args_size [INSN_UID (dest)] = cur_args_size;
1395               VEC_safe_push (rtx, heap, *next, dest);
1396             }
1397         }
1398     }
1399
1400   return cur_args_size;
1401 }
1402
1403 /* Walk the whole function and compute args_size on BARRIERs.  */
1404
1405 static void
1406 compute_barrier_args_size (void)
1407 {
1408   int max_uid = get_max_uid (), i;
1409   rtx insn;
1410   VEC (rtx, heap) *worklist, *next, *tmp;
1411
1412   barrier_args_size = XNEWVEC (HOST_WIDE_INT, max_uid);
1413   for (i = 0; i < max_uid; i++)
1414     barrier_args_size[i] = -1;
1415
1416   worklist = VEC_alloc (rtx, heap, 20);
1417   next = VEC_alloc (rtx, heap, 20);
1418   insn = get_insns ();
1419   barrier_args_size[INSN_UID (insn)] = 0;
1420   VEC_quick_push (rtx, worklist, insn);
1421   for (;;)
1422     {
1423       while (!VEC_empty (rtx, worklist))
1424         {
1425           rtx prev, body, first_insn;
1426           HOST_WIDE_INT cur_args_size;
1427
1428           first_insn = insn = VEC_pop (rtx, worklist);
1429           cur_args_size = barrier_args_size[INSN_UID (insn)];
1430           prev = prev_nonnote_insn (insn);
1431           if (prev && BARRIER_P (prev))
1432             barrier_args_size[INSN_UID (prev)] = cur_args_size;
1433
1434           for (; insn; insn = NEXT_INSN (insn))
1435             {
1436               if (INSN_DELETED_P (insn) || NOTE_P (insn))
1437                 continue;
1438               if (BARRIER_P (insn))
1439                 break;
1440
1441               if (LABEL_P (insn))
1442                 {
1443                   if (insn == first_insn)
1444                     continue;
1445                   else if (barrier_args_size[INSN_UID (insn)] < 0)
1446                     {
1447                       barrier_args_size[INSN_UID (insn)] = cur_args_size;
1448                       continue;
1449                     }
1450                   else
1451                     {
1452                       /* The insns starting with this label have been
1453                          already scanned or are in the worklist.  */
1454                       break;
1455                     }
1456                 }
1457
1458               body = PATTERN (insn);
1459               if (GET_CODE (body) == SEQUENCE)
1460                 {
1461                   HOST_WIDE_INT dest_args_size = cur_args_size;
1462                   for (i = 1; i < XVECLEN (body, 0); i++)
1463                     if (INSN_ANNULLED_BRANCH_P (XVECEXP (body, 0, 0))
1464                         && INSN_FROM_TARGET_P (XVECEXP (body, 0, i)))
1465                       dest_args_size
1466                         = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1467                                                        dest_args_size, &next);
1468                     else
1469                       cur_args_size
1470                         = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1471                                                        cur_args_size, &next);
1472
1473                   if (INSN_ANNULLED_BRANCH_P (XVECEXP (body, 0, 0)))
1474                     compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1475                                                  dest_args_size, &next);
1476                   else
1477                     cur_args_size
1478                       = compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1479                                                      cur_args_size, &next);
1480                 }
1481               else
1482                 cur_args_size
1483                   = compute_barrier_args_size_1 (insn, cur_args_size, &next);
1484             }
1485         }
1486
1487       if (VEC_empty (rtx, next))
1488         break;
1489
1490       /* Swap WORKLIST with NEXT and truncate NEXT for next iteration.  */
1491       tmp = next;
1492       next = worklist;
1493       worklist = tmp;
1494       VEC_truncate (rtx, next, 0);
1495     }
1496
1497   VEC_free (rtx, heap, worklist);
1498   VEC_free (rtx, heap, next);
1499 }
1500
1501
1502 /* Check INSN to see if it looks like a push or a stack adjustment, and
1503    make a note of it if it does.  EH uses this information to find out how
1504    much extra space it needs to pop off the stack.  */
1505
1506 static void
1507 dwarf2out_stack_adjust (rtx insn, bool after_p)
1508 {
1509   HOST_WIDE_INT offset;
1510   const char *label;
1511   int i;
1512
1513   /* Don't handle epilogues at all.  Certainly it would be wrong to do so
1514      with this function.  Proper support would require all frame-related
1515      insns to be marked, and to be able to handle saving state around
1516      epilogues textually in the middle of the function.  */
1517   if (prologue_epilogue_contains (insn))
1518     return;
1519
1520   /* If INSN is an instruction from target of an annulled branch, the
1521      effects are for the target only and so current argument size
1522      shouldn't change at all.  */
1523   if (final_sequence
1524       && INSN_ANNULLED_BRANCH_P (XVECEXP (final_sequence, 0, 0))
1525       && INSN_FROM_TARGET_P (insn))
1526     return;
1527
1528   /* If only calls can throw, and we have a frame pointer,
1529      save up adjustments until we see the CALL_INSN.  */
1530   if (!flag_asynchronous_unwind_tables && cfa.reg != STACK_POINTER_REGNUM)
1531     {
1532       if (CALL_P (insn) && !after_p)
1533         {
1534           /* Extract the size of the args from the CALL rtx itself.  */
1535           insn = PATTERN (insn);
1536           if (GET_CODE (insn) == PARALLEL)
1537             insn = XVECEXP (insn, 0, 0);
1538           if (GET_CODE (insn) == SET)
1539             insn = SET_SRC (insn);
1540           gcc_assert (GET_CODE (insn) == CALL);
1541           dwarf2out_args_size ("", INTVAL (XEXP (insn, 1)));
1542         }
1543       return;
1544     }
1545
1546   if (CALL_P (insn) && !after_p)
1547     {
1548       if (!flag_asynchronous_unwind_tables)
1549         dwarf2out_args_size ("", args_size);
1550       return;
1551     }
1552   else if (BARRIER_P (insn))
1553     {
1554       /* Don't call compute_barrier_args_size () if the only
1555          BARRIER is at the end of function.  */
1556       if (barrier_args_size == NULL && next_nonnote_insn (insn))
1557         compute_barrier_args_size ();
1558       if (barrier_args_size == NULL)
1559         offset = 0;
1560       else
1561         {
1562           offset = barrier_args_size[INSN_UID (insn)];
1563           if (offset < 0)
1564             offset = 0;
1565         }
1566
1567       offset -= args_size;
1568 #ifndef STACK_GROWS_DOWNWARD
1569       offset = -offset;
1570 #endif
1571     }
1572   else if (GET_CODE (PATTERN (insn)) == SET)
1573     offset = stack_adjust_offset (PATTERN (insn), args_size, 0);
1574   else if (GET_CODE (PATTERN (insn)) == PARALLEL
1575            || GET_CODE (PATTERN (insn)) == SEQUENCE)
1576     {
1577       /* There may be stack adjustments inside compound insns.  Search
1578          for them.  */
1579       for (offset = 0, i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1580         if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1581           offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i),
1582                                          args_size, offset);
1583     }
1584   else
1585     return;
1586
1587   if (offset == 0)
1588     return;
1589
1590   label = dwarf2out_cfi_label (false);
1591   dwarf2out_args_size_adjust (offset, label);
1592 }
1593
1594 /* Adjust args_size based on stack adjustment OFFSET.  */
1595
1596 static void
1597 dwarf2out_args_size_adjust (HOST_WIDE_INT offset, const char *label)
1598 {
1599   if (cfa.reg == STACK_POINTER_REGNUM)
1600     cfa.offset += offset;
1601
1602   if (cfa_store.reg == STACK_POINTER_REGNUM)
1603     cfa_store.offset += offset;
1604
1605 #ifndef STACK_GROWS_DOWNWARD
1606   offset = -offset;
1607 #endif
1608
1609   args_size += offset;
1610   if (args_size < 0)
1611     args_size = 0;
1612
1613   def_cfa_1 (label, &cfa);
1614   if (flag_asynchronous_unwind_tables)
1615     dwarf2out_args_size (label, args_size);
1616 }
1617
1618 #endif
1619
1620 /* We delay emitting a register save until either (a) we reach the end
1621    of the prologue or (b) the register is clobbered.  This clusters
1622    register saves so that there are fewer pc advances.  */
1623
1624 struct GTY(()) queued_reg_save {
1625   struct queued_reg_save *next;
1626   rtx reg;
1627   HOST_WIDE_INT cfa_offset;
1628   rtx saved_reg;
1629 };
1630
1631 static GTY(()) struct queued_reg_save *queued_reg_saves;
1632
1633 /* The caller's ORIG_REG is saved in SAVED_IN_REG.  */
1634 struct GTY(()) reg_saved_in_data {
1635   rtx orig_reg;
1636   rtx saved_in_reg;
1637 };
1638
1639 /* A list of registers saved in other registers.
1640    The list intentionally has a small maximum capacity of 4; if your
1641    port needs more than that, you might consider implementing a
1642    more efficient data structure.  */
1643 static GTY(()) struct reg_saved_in_data regs_saved_in_regs[4];
1644 static GTY(()) size_t num_regs_saved_in_regs;
1645
1646 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
1647 static const char *last_reg_save_label;
1648
1649 /* Add an entry to QUEUED_REG_SAVES saying that REG is now saved at
1650    SREG, or if SREG is NULL then it is saved at OFFSET to the CFA.  */
1651
1652 static void
1653 queue_reg_save (const char *label, rtx reg, rtx sreg, HOST_WIDE_INT offset)
1654 {
1655   struct queued_reg_save *q;
1656
1657   /* Duplicates waste space, but it's also necessary to remove them
1658      for correctness, since the queue gets output in reverse
1659      order.  */
1660   for (q = queued_reg_saves; q != NULL; q = q->next)
1661     if (REGNO (q->reg) == REGNO (reg))
1662       break;
1663
1664   if (q == NULL)
1665     {
1666       q = GGC_NEW (struct queued_reg_save);
1667       q->next = queued_reg_saves;
1668       queued_reg_saves = q;
1669     }
1670
1671   q->reg = reg;
1672   q->cfa_offset = offset;
1673   q->saved_reg = sreg;
1674
1675   last_reg_save_label = label;
1676 }
1677
1678 /* Output all the entries in QUEUED_REG_SAVES.  */
1679
1680 static void
1681 flush_queued_reg_saves (void)
1682 {
1683   struct queued_reg_save *q;
1684
1685   for (q = queued_reg_saves; q; q = q->next)
1686     {
1687       size_t i;
1688       unsigned int reg, sreg;
1689
1690       for (i = 0; i < num_regs_saved_in_regs; i++)
1691         if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (q->reg))
1692           break;
1693       if (q->saved_reg && i == num_regs_saved_in_regs)
1694         {
1695           gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1696           num_regs_saved_in_regs++;
1697         }
1698       if (i != num_regs_saved_in_regs)
1699         {
1700           regs_saved_in_regs[i].orig_reg = q->reg;
1701           regs_saved_in_regs[i].saved_in_reg = q->saved_reg;
1702         }
1703
1704       reg = DWARF_FRAME_REGNUM (REGNO (q->reg));
1705       if (q->saved_reg)
1706         sreg = DWARF_FRAME_REGNUM (REGNO (q->saved_reg));
1707       else
1708         sreg = INVALID_REGNUM;
1709       reg_save (last_reg_save_label, reg, sreg, q->cfa_offset);
1710     }
1711
1712   queued_reg_saves = NULL;
1713   last_reg_save_label = NULL;
1714 }
1715
1716 /* Does INSN clobber any register which QUEUED_REG_SAVES lists a saved
1717    location for?  Or, does it clobber a register which we've previously
1718    said that some other register is saved in, and for which we now
1719    have a new location for?  */
1720
1721 static bool
1722 clobbers_queued_reg_save (const_rtx insn)
1723 {
1724   struct queued_reg_save *q;
1725
1726   for (q = queued_reg_saves; q; q = q->next)
1727     {
1728       size_t i;
1729       if (modified_in_p (q->reg, insn))
1730         return true;
1731       for (i = 0; i < num_regs_saved_in_regs; i++)
1732         if (REGNO (q->reg) == REGNO (regs_saved_in_regs[i].orig_reg)
1733             && modified_in_p (regs_saved_in_regs[i].saved_in_reg, insn))
1734           return true;
1735     }
1736
1737   return false;
1738 }
1739
1740 /* Entry point for saving the first register into the second.  */
1741
1742 void
1743 dwarf2out_reg_save_reg (const char *label, rtx reg, rtx sreg)
1744 {
1745   size_t i;
1746   unsigned int regno, sregno;
1747
1748   for (i = 0; i < num_regs_saved_in_regs; i++)
1749     if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (reg))
1750       break;
1751   if (i == num_regs_saved_in_regs)
1752     {
1753       gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1754       num_regs_saved_in_regs++;
1755     }
1756   regs_saved_in_regs[i].orig_reg = reg;
1757   regs_saved_in_regs[i].saved_in_reg = sreg;
1758
1759   regno = DWARF_FRAME_REGNUM (REGNO (reg));
1760   sregno = DWARF_FRAME_REGNUM (REGNO (sreg));
1761   reg_save (label, regno, sregno, 0);
1762 }
1763
1764 /* What register, if any, is currently saved in REG?  */
1765
1766 static rtx
1767 reg_saved_in (rtx reg)
1768 {
1769   unsigned int regn = REGNO (reg);
1770   size_t i;
1771   struct queued_reg_save *q;
1772
1773   for (q = queued_reg_saves; q; q = q->next)
1774     if (q->saved_reg && regn == REGNO (q->saved_reg))
1775       return q->reg;
1776
1777   for (i = 0; i < num_regs_saved_in_regs; i++)
1778     if (regs_saved_in_regs[i].saved_in_reg
1779         && regn == REGNO (regs_saved_in_regs[i].saved_in_reg))
1780       return regs_saved_in_regs[i].orig_reg;
1781
1782   return NULL_RTX;
1783 }
1784
1785
1786 /* A temporary register holding an integral value used in adjusting SP
1787    or setting up the store_reg.  The "offset" field holds the integer
1788    value, not an offset.  */
1789 static dw_cfa_location cfa_temp;
1790
1791 /* A subroutine of dwarf2out_frame_debug, process a REG_DEF_CFA note.  */
1792
1793 static void
1794 dwarf2out_frame_debug_def_cfa (rtx pat, const char *label)
1795 {
1796   memset (&cfa, 0, sizeof (cfa));
1797
1798   switch (GET_CODE (pat))
1799     {
1800     case PLUS:
1801       cfa.reg = REGNO (XEXP (pat, 0));
1802       cfa.offset = INTVAL (XEXP (pat, 1));
1803       break;
1804
1805     case REG:
1806       cfa.reg = REGNO (pat);
1807       break;
1808
1809     default:
1810       /* Recurse and define an expression.  */
1811       gcc_unreachable ();
1812     }
1813
1814   def_cfa_1 (label, &cfa);
1815 }
1816
1817 /* A subroutine of dwarf2out_frame_debug, process a REG_ADJUST_CFA note.  */
1818
1819 static void
1820 dwarf2out_frame_debug_adjust_cfa (rtx pat, const char *label)
1821 {
1822   rtx src, dest;
1823
1824   gcc_assert (GET_CODE (pat) == SET);
1825   dest = XEXP (pat, 0);
1826   src = XEXP (pat, 1);
1827
1828   switch (GET_CODE (src))
1829     {
1830     case PLUS:
1831       gcc_assert (REGNO (XEXP (src, 0)) == cfa.reg);
1832       cfa.offset -= INTVAL (XEXP (src, 1));
1833       break;
1834
1835     case REG:
1836         break;
1837
1838     default:
1839         gcc_unreachable ();
1840     }
1841
1842   cfa.reg = REGNO (dest);
1843   gcc_assert (cfa.indirect == 0);
1844
1845   def_cfa_1 (label, &cfa);
1846 }
1847
1848 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_OFFSET note.  */
1849
1850 static void
1851 dwarf2out_frame_debug_cfa_offset (rtx set, const char *label)
1852 {
1853   HOST_WIDE_INT offset;
1854   rtx src, addr, span;
1855
1856   src = XEXP (set, 1);
1857   addr = XEXP (set, 0);
1858   gcc_assert (MEM_P (addr));
1859   addr = XEXP (addr, 0);
1860   
1861   /* As documented, only consider extremely simple addresses.  */
1862   switch (GET_CODE (addr))
1863     {
1864     case REG:
1865       gcc_assert (REGNO (addr) == cfa.reg);
1866       offset = -cfa.offset;
1867       break;
1868     case PLUS:
1869       gcc_assert (REGNO (XEXP (addr, 0)) == cfa.reg);
1870       offset = INTVAL (XEXP (addr, 1)) - cfa.offset;
1871       break;
1872     default:
1873       gcc_unreachable ();
1874     }
1875
1876   span = targetm.dwarf_register_span (src);
1877
1878   /* ??? We'd like to use queue_reg_save, but we need to come up with
1879      a different flushing heuristic for epilogues.  */
1880   if (!span)
1881     reg_save (label, DWARF_FRAME_REGNUM (REGNO (src)), INVALID_REGNUM, offset);
1882   else
1883     {
1884       /* We have a PARALLEL describing where the contents of SRC live.
1885          Queue register saves for each piece of the PARALLEL.  */
1886       int par_index;
1887       int limit;
1888       HOST_WIDE_INT span_offset = offset;
1889
1890       gcc_assert (GET_CODE (span) == PARALLEL);
1891
1892       limit = XVECLEN (span, 0);
1893       for (par_index = 0; par_index < limit; par_index++)
1894         {
1895           rtx elem = XVECEXP (span, 0, par_index);
1896
1897           reg_save (label, DWARF_FRAME_REGNUM (REGNO (elem)),
1898                     INVALID_REGNUM, span_offset);
1899           span_offset += GET_MODE_SIZE (GET_MODE (elem));
1900         }
1901     }
1902 }
1903
1904 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_REGISTER note.  */
1905
1906 static void
1907 dwarf2out_frame_debug_cfa_register (rtx set, const char *label)
1908 {
1909   rtx src, dest;
1910   unsigned sregno, dregno;
1911
1912   src = XEXP (set, 1);
1913   dest = XEXP (set, 0);
1914
1915   if (src == pc_rtx)
1916     sregno = DWARF_FRAME_RETURN_COLUMN;
1917   else
1918     sregno = DWARF_FRAME_REGNUM (REGNO (src));
1919
1920   dregno = DWARF_FRAME_REGNUM (REGNO (dest));
1921
1922   /* ??? We'd like to use queue_reg_save, but we need to come up with
1923      a different flushing heuristic for epilogues.  */
1924   reg_save (label, sregno, dregno, 0);
1925 }
1926
1927 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_RESTORE note.  */
1928
1929 static void
1930 dwarf2out_frame_debug_cfa_restore (rtx reg, const char *label)
1931 {
1932   dw_cfi_ref cfi = new_cfi ();
1933   unsigned int regno = DWARF_FRAME_REGNUM (REGNO (reg));
1934
1935   cfi->dw_cfi_opc = (regno & ~0x3f ? DW_CFA_restore_extended : DW_CFA_restore);
1936   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = regno;
1937
1938   add_fde_cfi (label, cfi);
1939 }
1940
1941 /* Record call frame debugging information for an expression EXPR,
1942    which either sets SP or FP (adjusting how we calculate the frame
1943    address) or saves a register to the stack or another register.
1944    LABEL indicates the address of EXPR.
1945
1946    This function encodes a state machine mapping rtxes to actions on
1947    cfa, cfa_store, and cfa_temp.reg.  We describe these rules so
1948    users need not read the source code.
1949
1950   The High-Level Picture
1951
1952   Changes in the register we use to calculate the CFA: Currently we
1953   assume that if you copy the CFA register into another register, we
1954   should take the other one as the new CFA register; this seems to
1955   work pretty well.  If it's wrong for some target, it's simple
1956   enough not to set RTX_FRAME_RELATED_P on the insn in question.
1957
1958   Changes in the register we use for saving registers to the stack:
1959   This is usually SP, but not always.  Again, we deduce that if you
1960   copy SP into another register (and SP is not the CFA register),
1961   then the new register is the one we will be using for register
1962   saves.  This also seems to work.
1963
1964   Register saves: There's not much guesswork about this one; if
1965   RTX_FRAME_RELATED_P is set on an insn which modifies memory, it's a
1966   register save, and the register used to calculate the destination
1967   had better be the one we think we're using for this purpose.
1968   It's also assumed that a copy from a call-saved register to another
1969   register is saving that register if RTX_FRAME_RELATED_P is set on
1970   that instruction.  If the copy is from a call-saved register to
1971   the *same* register, that means that the register is now the same
1972   value as in the caller.
1973
1974   Except: If the register being saved is the CFA register, and the
1975   offset is nonzero, we are saving the CFA, so we assume we have to
1976   use DW_CFA_def_cfa_expression.  If the offset is 0, we assume that
1977   the intent is to save the value of SP from the previous frame.
1978
1979   In addition, if a register has previously been saved to a different
1980   register,
1981
1982   Invariants / Summaries of Rules
1983
1984   cfa          current rule for calculating the CFA.  It usually
1985                consists of a register and an offset.
1986   cfa_store    register used by prologue code to save things to the stack
1987                cfa_store.offset is the offset from the value of
1988                cfa_store.reg to the actual CFA
1989   cfa_temp     register holding an integral value.  cfa_temp.offset
1990                stores the value, which will be used to adjust the
1991                stack pointer.  cfa_temp is also used like cfa_store,
1992                to track stores to the stack via fp or a temp reg.
1993
1994   Rules  1- 4: Setting a register's value to cfa.reg or an expression
1995                with cfa.reg as the first operand changes the cfa.reg and its
1996                cfa.offset.  Rule 1 and 4 also set cfa_temp.reg and
1997                cfa_temp.offset.
1998
1999   Rules  6- 9: Set a non-cfa.reg register value to a constant or an
2000                expression yielding a constant.  This sets cfa_temp.reg
2001                and cfa_temp.offset.
2002
2003   Rule 5:      Create a new register cfa_store used to save items to the
2004                stack.
2005
2006   Rules 10-14: Save a register to the stack.  Define offset as the
2007                difference of the original location and cfa_store's
2008                location (or cfa_temp's location if cfa_temp is used).
2009
2010   Rules 16-20: If AND operation happens on sp in prologue, we assume
2011                stack is realigned.  We will use a group of DW_OP_XXX
2012                expressions to represent the location of the stored
2013                register instead of CFA+offset.
2014
2015   The Rules
2016
2017   "{a,b}" indicates a choice of a xor b.
2018   "<reg>:cfa.reg" indicates that <reg> must equal cfa.reg.
2019
2020   Rule 1:
2021   (set <reg1> <reg2>:cfa.reg)
2022   effects: cfa.reg = <reg1>
2023            cfa.offset unchanged
2024            cfa_temp.reg = <reg1>
2025            cfa_temp.offset = cfa.offset
2026
2027   Rule 2:
2028   (set sp ({minus,plus,losum} {sp,fp}:cfa.reg
2029                               {<const_int>,<reg>:cfa_temp.reg}))
2030   effects: cfa.reg = sp if fp used
2031            cfa.offset += {+/- <const_int>, cfa_temp.offset} if cfa.reg==sp
2032            cfa_store.offset += {+/- <const_int>, cfa_temp.offset}
2033              if cfa_store.reg==sp
2034
2035   Rule 3:
2036   (set fp ({minus,plus,losum} <reg>:cfa.reg <const_int>))
2037   effects: cfa.reg = fp
2038            cfa_offset += +/- <const_int>
2039
2040   Rule 4:
2041   (set <reg1> ({plus,losum} <reg2>:cfa.reg <const_int>))
2042   constraints: <reg1> != fp
2043                <reg1> != sp
2044   effects: cfa.reg = <reg1>
2045            cfa_temp.reg = <reg1>
2046            cfa_temp.offset = cfa.offset
2047
2048   Rule 5:
2049   (set <reg1> (plus <reg2>:cfa_temp.reg sp:cfa.reg))
2050   constraints: <reg1> != fp
2051                <reg1> != sp
2052   effects: cfa_store.reg = <reg1>
2053            cfa_store.offset = cfa.offset - cfa_temp.offset
2054
2055   Rule 6:
2056   (set <reg> <const_int>)
2057   effects: cfa_temp.reg = <reg>
2058            cfa_temp.offset = <const_int>
2059
2060   Rule 7:
2061   (set <reg1>:cfa_temp.reg (ior <reg2>:cfa_temp.reg <const_int>))
2062   effects: cfa_temp.reg = <reg1>
2063            cfa_temp.offset |= <const_int>
2064
2065   Rule 8:
2066   (set <reg> (high <exp>))
2067   effects: none
2068
2069   Rule 9:
2070   (set <reg> (lo_sum <exp> <const_int>))
2071   effects: cfa_temp.reg = <reg>
2072            cfa_temp.offset = <const_int>
2073
2074   Rule 10:
2075   (set (mem (pre_modify sp:cfa_store (???? <reg1> <const_int>))) <reg2>)
2076   effects: cfa_store.offset -= <const_int>
2077            cfa.offset = cfa_store.offset if cfa.reg == sp
2078            cfa.reg = sp
2079            cfa.base_offset = -cfa_store.offset
2080
2081   Rule 11:
2082   (set (mem ({pre_inc,pre_dec} sp:cfa_store.reg)) <reg>)
2083   effects: cfa_store.offset += -/+ mode_size(mem)
2084            cfa.offset = cfa_store.offset if cfa.reg == sp
2085            cfa.reg = sp
2086            cfa.base_offset = -cfa_store.offset
2087
2088   Rule 12:
2089   (set (mem ({minus,plus,losum} <reg1>:{cfa_store,cfa_temp} <const_int>))
2090
2091        <reg2>)
2092   effects: cfa.reg = <reg1>
2093            cfa.base_offset = -/+ <const_int> - {cfa_store,cfa_temp}.offset
2094
2095   Rule 13:
2096   (set (mem <reg1>:{cfa_store,cfa_temp}) <reg2>)
2097   effects: cfa.reg = <reg1>
2098            cfa.base_offset = -{cfa_store,cfa_temp}.offset
2099
2100   Rule 14:
2101   (set (mem (postinc <reg1>:cfa_temp <const_int>)) <reg2>)
2102   effects: cfa.reg = <reg1>
2103            cfa.base_offset = -cfa_temp.offset
2104            cfa_temp.offset -= mode_size(mem)
2105
2106   Rule 15:
2107   (set <reg> {unspec, unspec_volatile})
2108   effects: target-dependent
2109
2110   Rule 16:
2111   (set sp (and: sp <const_int>))
2112   constraints: cfa_store.reg == sp
2113   effects: current_fde.stack_realign = 1
2114            cfa_store.offset = 0
2115            fde->drap_reg = cfa.reg if cfa.reg != sp and cfa.reg != fp
2116
2117   Rule 17:
2118   (set (mem ({pre_inc, pre_dec} sp)) (mem (plus (cfa.reg) (const_int))))
2119   effects: cfa_store.offset += -/+ mode_size(mem)
2120
2121   Rule 18:
2122   (set (mem ({pre_inc, pre_dec} sp)) fp)
2123   constraints: fde->stack_realign == 1
2124   effects: cfa_store.offset = 0
2125            cfa.reg != HARD_FRAME_POINTER_REGNUM
2126
2127   Rule 19:
2128   (set (mem ({pre_inc, pre_dec} sp)) cfa.reg)
2129   constraints: fde->stack_realign == 1
2130                && cfa.offset == 0
2131                && cfa.indirect == 0
2132                && cfa.reg != HARD_FRAME_POINTER_REGNUM
2133   effects: Use DW_CFA_def_cfa_expression to define cfa
2134            cfa.reg == fde->drap_reg
2135
2136   Rule 20:
2137   (set reg fde->drap_reg)
2138   constraints: fde->vdrap_reg == INVALID_REGNUM
2139   effects: fde->vdrap_reg = reg.
2140   (set mem fde->drap_reg)
2141   constraints: fde->drap_reg_saved == 1
2142   effects: none.  */
2143
2144 static void
2145 dwarf2out_frame_debug_expr (rtx expr, const char *label)
2146 {
2147   rtx src, dest, span;
2148   HOST_WIDE_INT offset;
2149   dw_fde_ref fde;
2150
2151   /* If RTX_FRAME_RELATED_P is set on a PARALLEL, process each member of
2152      the PARALLEL independently. The first element is always processed if
2153      it is a SET. This is for backward compatibility.   Other elements
2154      are processed only if they are SETs and the RTX_FRAME_RELATED_P
2155      flag is set in them.  */
2156   if (GET_CODE (expr) == PARALLEL || GET_CODE (expr) == SEQUENCE)
2157     {
2158       int par_index;
2159       int limit = XVECLEN (expr, 0);
2160       rtx elem;
2161
2162       /* PARALLELs have strict read-modify-write semantics, so we
2163          ought to evaluate every rvalue before changing any lvalue.
2164          It's cumbersome to do that in general, but there's an
2165          easy approximation that is enough for all current users:
2166          handle register saves before register assignments.  */
2167       if (GET_CODE (expr) == PARALLEL)
2168         for (par_index = 0; par_index < limit; par_index++)
2169           {
2170             elem = XVECEXP (expr, 0, par_index);
2171             if (GET_CODE (elem) == SET
2172                 && MEM_P (SET_DEST (elem))
2173                 && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
2174               dwarf2out_frame_debug_expr (elem, label);
2175           }
2176
2177       for (par_index = 0; par_index < limit; par_index++)
2178         {
2179           elem = XVECEXP (expr, 0, par_index);
2180           if (GET_CODE (elem) == SET
2181               && (!MEM_P (SET_DEST (elem)) || GET_CODE (expr) == SEQUENCE)
2182               && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
2183             dwarf2out_frame_debug_expr (elem, label);
2184           else if (GET_CODE (elem) == SET
2185                    && par_index != 0
2186                    && !RTX_FRAME_RELATED_P (elem))
2187             {
2188               /* Stack adjustment combining might combine some post-prologue
2189                  stack adjustment into a prologue stack adjustment.  */
2190               HOST_WIDE_INT offset = stack_adjust_offset (elem, args_size, 0);
2191
2192               if (offset != 0)
2193                 dwarf2out_args_size_adjust (offset, label);
2194             }
2195         }
2196       return;
2197     }
2198
2199   gcc_assert (GET_CODE (expr) == SET);
2200
2201   src = SET_SRC (expr);
2202   dest = SET_DEST (expr);
2203
2204   if (REG_P (src))
2205     {
2206       rtx rsi = reg_saved_in (src);
2207       if (rsi)
2208         src = rsi;
2209     }
2210
2211   fde = current_fde ();
2212
2213   if (REG_P (src)
2214       && fde
2215       && fde->drap_reg == REGNO (src)
2216       && (fde->drap_reg_saved
2217           || REG_P (dest)))
2218     {
2219       /* Rule 20 */
2220       /* If we are saving dynamic realign argument pointer to a
2221          register, the destination is virtual dynamic realign
2222          argument pointer.  It may be used to access argument.  */
2223       if (REG_P (dest))
2224         {
2225           gcc_assert (fde->vdrap_reg == INVALID_REGNUM);
2226           fde->vdrap_reg = REGNO (dest);
2227         }
2228       return;
2229     }
2230
2231   switch (GET_CODE (dest))
2232     {
2233     case REG:
2234       switch (GET_CODE (src))
2235         {
2236           /* Setting FP from SP.  */
2237         case REG:
2238           if (cfa.reg == (unsigned) REGNO (src))
2239             {
2240               /* Rule 1 */
2241               /* Update the CFA rule wrt SP or FP.  Make sure src is
2242                  relative to the current CFA register.
2243
2244                  We used to require that dest be either SP or FP, but the
2245                  ARM copies SP to a temporary register, and from there to
2246                  FP.  So we just rely on the backends to only set
2247                  RTX_FRAME_RELATED_P on appropriate insns.  */
2248               cfa.reg = REGNO (dest);
2249               cfa_temp.reg = cfa.reg;
2250               cfa_temp.offset = cfa.offset;
2251             }
2252           else
2253             {
2254               /* Saving a register in a register.  */
2255               gcc_assert (!fixed_regs [REGNO (dest)]
2256                           /* For the SPARC and its register window.  */
2257                           || (DWARF_FRAME_REGNUM (REGNO (src))
2258                               == DWARF_FRAME_RETURN_COLUMN));
2259
2260               /* After stack is aligned, we can only save SP in FP
2261                  if drap register is used.  In this case, we have
2262                  to restore stack pointer with the CFA value and we
2263                  don't generate this DWARF information.  */
2264               if (fde
2265                   && fde->stack_realign
2266                   && REGNO (src) == STACK_POINTER_REGNUM)
2267                 gcc_assert (REGNO (dest) == HARD_FRAME_POINTER_REGNUM
2268                             && fde->drap_reg != INVALID_REGNUM
2269                             && cfa.reg != REGNO (src));
2270               else
2271                 queue_reg_save (label, src, dest, 0);
2272             }
2273           break;
2274
2275         case PLUS:
2276         case MINUS:
2277         case LO_SUM:
2278           if (dest == stack_pointer_rtx)
2279             {
2280               /* Rule 2 */
2281               /* Adjusting SP.  */
2282               switch (GET_CODE (XEXP (src, 1)))
2283                 {
2284                 case CONST_INT:
2285                   offset = INTVAL (XEXP (src, 1));
2286                   break;
2287                 case REG:
2288                   gcc_assert ((unsigned) REGNO (XEXP (src, 1))
2289                               == cfa_temp.reg);
2290                   offset = cfa_temp.offset;
2291                   break;
2292                 default:
2293                   gcc_unreachable ();
2294                 }
2295
2296               if (XEXP (src, 0) == hard_frame_pointer_rtx)
2297                 {
2298                   /* Restoring SP from FP in the epilogue.  */
2299                   gcc_assert (cfa.reg == (unsigned) HARD_FRAME_POINTER_REGNUM);
2300                   cfa.reg = STACK_POINTER_REGNUM;
2301                 }
2302               else if (GET_CODE (src) == LO_SUM)
2303                 /* Assume we've set the source reg of the LO_SUM from sp.  */
2304                 ;
2305               else
2306                 gcc_assert (XEXP (src, 0) == stack_pointer_rtx);
2307
2308               if (GET_CODE (src) != MINUS)
2309                 offset = -offset;
2310               if (cfa.reg == STACK_POINTER_REGNUM)
2311                 cfa.offset += offset;
2312               if (cfa_store.reg == STACK_POINTER_REGNUM)
2313                 cfa_store.offset += offset;
2314             }
2315           else if (dest == hard_frame_pointer_rtx)
2316             {
2317               /* Rule 3 */
2318               /* Either setting the FP from an offset of the SP,
2319                  or adjusting the FP */
2320               gcc_assert (frame_pointer_needed);
2321
2322               gcc_assert (REG_P (XEXP (src, 0))
2323                           && (unsigned) REGNO (XEXP (src, 0)) == cfa.reg
2324                           && CONST_INT_P (XEXP (src, 1)));
2325               offset = INTVAL (XEXP (src, 1));
2326               if (GET_CODE (src) != MINUS)
2327                 offset = -offset;
2328               cfa.offset += offset;
2329               cfa.reg = HARD_FRAME_POINTER_REGNUM;
2330             }
2331           else
2332             {
2333               gcc_assert (GET_CODE (src) != MINUS);
2334
2335               /* Rule 4 */
2336               if (REG_P (XEXP (src, 0))
2337                   && REGNO (XEXP (src, 0)) == cfa.reg
2338                   && CONST_INT_P (XEXP (src, 1)))
2339                 {
2340                   /* Setting a temporary CFA register that will be copied
2341                      into the FP later on.  */
2342                   offset = - INTVAL (XEXP (src, 1));
2343                   cfa.offset += offset;
2344                   cfa.reg = REGNO (dest);
2345                   /* Or used to save regs to the stack.  */
2346                   cfa_temp.reg = cfa.reg;
2347                   cfa_temp.offset = cfa.offset;
2348                 }
2349
2350               /* Rule 5 */
2351               else if (REG_P (XEXP (src, 0))
2352                        && REGNO (XEXP (src, 0)) == cfa_temp.reg
2353                        && XEXP (src, 1) == stack_pointer_rtx)
2354                 {
2355                   /* Setting a scratch register that we will use instead
2356                      of SP for saving registers to the stack.  */
2357                   gcc_assert (cfa.reg == STACK_POINTER_REGNUM);
2358                   cfa_store.reg = REGNO (dest);
2359                   cfa_store.offset = cfa.offset - cfa_temp.offset;
2360                 }
2361
2362               /* Rule 9 */
2363               else if (GET_CODE (src) == LO_SUM
2364                        && CONST_INT_P (XEXP (src, 1)))
2365                 {
2366                   cfa_temp.reg = REGNO (dest);
2367                   cfa_temp.offset = INTVAL (XEXP (src, 1));
2368                 }
2369               else
2370                 gcc_unreachable ();
2371             }
2372           break;
2373
2374           /* Rule 6 */
2375         case CONST_INT:
2376           cfa_temp.reg = REGNO (dest);
2377           cfa_temp.offset = INTVAL (src);
2378           break;
2379
2380           /* Rule 7 */
2381         case IOR:
2382           gcc_assert (REG_P (XEXP (src, 0))
2383                       && (unsigned) REGNO (XEXP (src, 0)) == cfa_temp.reg
2384                       && CONST_INT_P (XEXP (src, 1)));
2385
2386           if ((unsigned) REGNO (dest) != cfa_temp.reg)
2387             cfa_temp.reg = REGNO (dest);
2388           cfa_temp.offset |= INTVAL (XEXP (src, 1));
2389           break;
2390
2391           /* Skip over HIGH, assuming it will be followed by a LO_SUM,
2392              which will fill in all of the bits.  */
2393           /* Rule 8 */
2394         case HIGH:
2395           break;
2396
2397           /* Rule 15 */
2398         case UNSPEC:
2399         case UNSPEC_VOLATILE:
2400           gcc_assert (targetm.dwarf_handle_frame_unspec);
2401           targetm.dwarf_handle_frame_unspec (label, expr, XINT (src, 1));
2402           return;
2403
2404           /* Rule 16 */
2405         case AND:
2406           /* If this AND operation happens on stack pointer in prologue,
2407              we assume the stack is realigned and we extract the
2408              alignment.  */
2409           if (fde && XEXP (src, 0) == stack_pointer_rtx)
2410             {
2411               gcc_assert (cfa_store.reg == REGNO (XEXP (src, 0)));
2412               fde->stack_realign = 1;
2413               fde->stack_realignment = INTVAL (XEXP (src, 1));
2414               cfa_store.offset = 0;
2415
2416               if (cfa.reg != STACK_POINTER_REGNUM
2417                   && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2418                 fde->drap_reg = cfa.reg;
2419             }
2420           return;
2421
2422         default:
2423           gcc_unreachable ();
2424         }
2425
2426       def_cfa_1 (label, &cfa);
2427       break;
2428
2429     case MEM:
2430
2431       /* Saving a register to the stack.  Make sure dest is relative to the
2432          CFA register.  */
2433       switch (GET_CODE (XEXP (dest, 0)))
2434         {
2435           /* Rule 10 */
2436           /* With a push.  */
2437         case PRE_MODIFY:
2438           /* We can't handle variable size modifications.  */
2439           gcc_assert (GET_CODE (XEXP (XEXP (XEXP (dest, 0), 1), 1))
2440                       == CONST_INT);
2441           offset = -INTVAL (XEXP (XEXP (XEXP (dest, 0), 1), 1));
2442
2443           gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
2444                       && cfa_store.reg == STACK_POINTER_REGNUM);
2445
2446           cfa_store.offset += offset;
2447           if (cfa.reg == STACK_POINTER_REGNUM)
2448             cfa.offset = cfa_store.offset;
2449
2450           offset = -cfa_store.offset;
2451           break;
2452
2453           /* Rule 11 */
2454         case PRE_INC:
2455         case PRE_DEC:
2456           offset = GET_MODE_SIZE (GET_MODE (dest));
2457           if (GET_CODE (XEXP (dest, 0)) == PRE_INC)
2458             offset = -offset;
2459
2460           gcc_assert ((REGNO (XEXP (XEXP (dest, 0), 0))
2461                        == STACK_POINTER_REGNUM)
2462                       && cfa_store.reg == STACK_POINTER_REGNUM);
2463
2464           cfa_store.offset += offset;
2465
2466           /* Rule 18: If stack is aligned, we will use FP as a
2467              reference to represent the address of the stored
2468              regiser.  */
2469           if (fde
2470               && fde->stack_realign
2471               && src == hard_frame_pointer_rtx)
2472             {
2473               gcc_assert (cfa.reg != HARD_FRAME_POINTER_REGNUM);
2474               cfa_store.offset = 0;
2475             }
2476
2477           if (cfa.reg == STACK_POINTER_REGNUM)
2478             cfa.offset = cfa_store.offset;
2479
2480           offset = -cfa_store.offset;
2481           break;
2482
2483           /* Rule 12 */
2484           /* With an offset.  */
2485         case PLUS:
2486         case MINUS:
2487         case LO_SUM:
2488           {
2489             int regno;
2490
2491             gcc_assert (CONST_INT_P (XEXP (XEXP (dest, 0), 1))
2492                         && REG_P (XEXP (XEXP (dest, 0), 0)));
2493             offset = INTVAL (XEXP (XEXP (dest, 0), 1));
2494             if (GET_CODE (XEXP (dest, 0)) == MINUS)
2495               offset = -offset;
2496
2497             regno = REGNO (XEXP (XEXP (dest, 0), 0));
2498
2499             if (cfa_store.reg == (unsigned) regno)
2500               offset -= cfa_store.offset;
2501             else
2502               {
2503                 gcc_assert (cfa_temp.reg == (unsigned) regno);
2504                 offset -= cfa_temp.offset;
2505               }
2506           }
2507           break;
2508
2509           /* Rule 13 */
2510           /* Without an offset.  */
2511         case REG:
2512           {
2513             int regno = REGNO (XEXP (dest, 0));
2514
2515             if (cfa_store.reg == (unsigned) regno)
2516               offset = -cfa_store.offset;
2517             else
2518               {
2519                 gcc_assert (cfa_temp.reg == (unsigned) regno);
2520                 offset = -cfa_temp.offset;
2521               }
2522           }
2523           break;
2524
2525           /* Rule 14 */
2526         case POST_INC:
2527           gcc_assert (cfa_temp.reg
2528                       == (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)));
2529           offset = -cfa_temp.offset;
2530           cfa_temp.offset -= GET_MODE_SIZE (GET_MODE (dest));
2531           break;
2532
2533         default:
2534           gcc_unreachable ();
2535         }
2536
2537         /* Rule 17 */
2538         /* If the source operand of this MEM operation is not a
2539            register, basically the source is return address.  Here
2540            we only care how much stack grew and we don't save it.  */
2541       if (!REG_P (src))
2542         break;
2543
2544       if (REGNO (src) != STACK_POINTER_REGNUM
2545           && REGNO (src) != HARD_FRAME_POINTER_REGNUM
2546           && (unsigned) REGNO (src) == cfa.reg)
2547         {
2548           /* We're storing the current CFA reg into the stack.  */
2549
2550           if (cfa.offset == 0)
2551             {
2552               /* Rule 19 */
2553               /* If stack is aligned, putting CFA reg into stack means
2554                  we can no longer use reg + offset to represent CFA.
2555                  Here we use DW_CFA_def_cfa_expression instead.  The
2556                  result of this expression equals to the original CFA
2557                  value.  */
2558               if (fde
2559                   && fde->stack_realign
2560                   && cfa.indirect == 0
2561                   && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2562                 {
2563                   dw_cfa_location cfa_exp;
2564
2565                   gcc_assert (fde->drap_reg == cfa.reg);
2566
2567                   cfa_exp.indirect = 1;
2568                   cfa_exp.reg = HARD_FRAME_POINTER_REGNUM;
2569                   cfa_exp.base_offset = offset;
2570                   cfa_exp.offset = 0;
2571
2572                   fde->drap_reg_saved = 1;
2573
2574                   def_cfa_1 (label, &cfa_exp);
2575                   break;
2576                 }
2577
2578               /* If the source register is exactly the CFA, assume
2579                  we're saving SP like any other register; this happens
2580                  on the ARM.  */
2581               def_cfa_1 (label, &cfa);
2582               queue_reg_save (label, stack_pointer_rtx, NULL_RTX, offset);
2583               break;
2584             }
2585           else
2586             {
2587               /* Otherwise, we'll need to look in the stack to
2588                  calculate the CFA.  */
2589               rtx x = XEXP (dest, 0);
2590
2591               if (!REG_P (x))
2592                 x = XEXP (x, 0);
2593               gcc_assert (REG_P (x));
2594
2595               cfa.reg = REGNO (x);
2596               cfa.base_offset = offset;
2597               cfa.indirect = 1;
2598               def_cfa_1 (label, &cfa);
2599               break;
2600             }
2601         }
2602
2603       def_cfa_1 (label, &cfa);
2604       {
2605         span = targetm.dwarf_register_span (src);
2606
2607         if (!span)
2608           queue_reg_save (label, src, NULL_RTX, offset);
2609         else
2610           {
2611             /* We have a PARALLEL describing where the contents of SRC
2612                live.  Queue register saves for each piece of the
2613                PARALLEL.  */
2614             int par_index;
2615             int limit;
2616             HOST_WIDE_INT span_offset = offset;
2617
2618             gcc_assert (GET_CODE (span) == PARALLEL);
2619
2620             limit = XVECLEN (span, 0);
2621             for (par_index = 0; par_index < limit; par_index++)
2622               {
2623                 rtx elem = XVECEXP (span, 0, par_index);
2624
2625                 queue_reg_save (label, elem, NULL_RTX, span_offset);
2626                 span_offset += GET_MODE_SIZE (GET_MODE (elem));
2627               }
2628           }
2629       }
2630       break;
2631
2632     default:
2633       gcc_unreachable ();
2634     }
2635 }
2636
2637 /* Record call frame debugging information for INSN, which either
2638    sets SP or FP (adjusting how we calculate the frame address) or saves a
2639    register to the stack.  If INSN is NULL_RTX, initialize our state.
2640
2641    If AFTER_P is false, we're being called before the insn is emitted,
2642    otherwise after.  Call instructions get invoked twice.  */
2643
2644 void
2645 dwarf2out_frame_debug (rtx insn, bool after_p)
2646 {
2647   const char *label;
2648   rtx note, n;
2649   bool handled_one = false;
2650
2651   if (insn == NULL_RTX)
2652     {
2653       size_t i;
2654
2655       /* Flush any queued register saves.  */
2656       flush_queued_reg_saves ();
2657
2658       /* Set up state for generating call frame debug info.  */
2659       lookup_cfa (&cfa);
2660       gcc_assert (cfa.reg
2661                   == (unsigned long)DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM));
2662
2663       cfa.reg = STACK_POINTER_REGNUM;
2664       cfa_store = cfa;
2665       cfa_temp.reg = -1;
2666       cfa_temp.offset = 0;
2667
2668       for (i = 0; i < num_regs_saved_in_regs; i++)
2669         {
2670           regs_saved_in_regs[i].orig_reg = NULL_RTX;
2671           regs_saved_in_regs[i].saved_in_reg = NULL_RTX;
2672         }
2673       num_regs_saved_in_regs = 0;
2674
2675       if (barrier_args_size)
2676         {
2677           XDELETEVEC (barrier_args_size);
2678           barrier_args_size = NULL;
2679         }
2680       return;
2681     }
2682
2683   if (!NONJUMP_INSN_P (insn) || clobbers_queued_reg_save (insn))
2684     flush_queued_reg_saves ();
2685
2686   if (! RTX_FRAME_RELATED_P (insn))
2687     {
2688       if (!ACCUMULATE_OUTGOING_ARGS)
2689         dwarf2out_stack_adjust (insn, after_p);
2690       return;
2691     }
2692
2693   label = dwarf2out_cfi_label (false);
2694
2695   for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
2696     switch (REG_NOTE_KIND (note))
2697       {
2698       case REG_FRAME_RELATED_EXPR:
2699         insn = XEXP (note, 0);
2700         goto found;
2701
2702       case REG_CFA_DEF_CFA:
2703         dwarf2out_frame_debug_def_cfa (XEXP (note, 0), label);
2704         handled_one = true;
2705         break;
2706
2707       case REG_CFA_ADJUST_CFA:
2708         n = XEXP (note, 0);
2709         if (n == NULL)
2710           {
2711             n = PATTERN (insn);
2712             if (GET_CODE (n) == PARALLEL)
2713               n = XVECEXP (n, 0, 0);
2714           }
2715         dwarf2out_frame_debug_adjust_cfa (n, label);
2716         handled_one = true;
2717         break;
2718
2719       case REG_CFA_OFFSET:
2720         n = XEXP (note, 0);
2721         if (n == NULL)
2722           n = single_set (insn);
2723         dwarf2out_frame_debug_cfa_offset (n, label);
2724         handled_one = true;
2725         break;
2726
2727       case REG_CFA_REGISTER:
2728         n = XEXP (note, 0);
2729         if (n == NULL)
2730           {
2731             n = PATTERN (insn);
2732             if (GET_CODE (n) == PARALLEL)
2733               n = XVECEXP (n, 0, 0);
2734           }
2735         dwarf2out_frame_debug_cfa_register (n, label);
2736         handled_one = true;
2737         break;
2738
2739       case REG_CFA_RESTORE:
2740         n = XEXP (note, 0);
2741         if (n == NULL)
2742           {
2743             n = PATTERN (insn);
2744             if (GET_CODE (n) == PARALLEL)
2745               n = XVECEXP (n, 0, 0);
2746             n = XEXP (n, 0);
2747           }
2748         dwarf2out_frame_debug_cfa_restore (n, label);
2749         handled_one = true;
2750         break;
2751
2752       default:
2753         break;
2754       }
2755   if (handled_one)
2756     return;
2757
2758   insn = PATTERN (insn);
2759  found:
2760   dwarf2out_frame_debug_expr (insn, label);
2761 }
2762
2763 /* Determine if we need to save and restore CFI information around this
2764    epilogue.  If SIBCALL is true, then this is a sibcall epilogue.  If
2765    we do need to save/restore, then emit the save now, and insert a
2766    NOTE_INSN_CFA_RESTORE_STATE at the appropriate place in the stream.  */
2767
2768 void
2769 dwarf2out_begin_epilogue (rtx insn)
2770 {
2771   bool saw_frp = false;
2772   rtx i;
2773
2774   /* Scan forward to the return insn, noticing if there are possible
2775      frame related insns.  */
2776   for (i = NEXT_INSN (insn); i ; i = NEXT_INSN (i))
2777     {
2778       if (!INSN_P (i))
2779         continue;
2780
2781       /* Look for both regular and sibcalls to end the block.  */
2782       if (returnjump_p (i))
2783         break;
2784       if (CALL_P (i) && SIBLING_CALL_P (i))
2785         break;
2786
2787       if (GET_CODE (PATTERN (i)) == SEQUENCE)
2788         {
2789           int idx;
2790           rtx seq = PATTERN (i);
2791
2792           if (returnjump_p (XVECEXP (seq, 0, 0)))
2793             break;
2794           if (CALL_P (XVECEXP (seq, 0, 0))
2795               && SIBLING_CALL_P (XVECEXP (seq, 0, 0)))
2796             break;
2797
2798           for (idx = 0; idx < XVECLEN (seq, 0); idx++)
2799             if (RTX_FRAME_RELATED_P (XVECEXP (seq, 0, idx)))
2800               saw_frp = true;
2801         }
2802
2803       if (RTX_FRAME_RELATED_P (i))
2804         saw_frp = true;
2805     }
2806
2807   /* If the port doesn't emit epilogue unwind info, we don't need a
2808      save/restore pair.  */
2809   if (!saw_frp)
2810     return;
2811
2812   /* Otherwise, search forward to see if the return insn was the last
2813      basic block of the function.  If so, we don't need save/restore.  */
2814   gcc_assert (i != NULL);
2815   i = next_real_insn (i);
2816   if (i == NULL)
2817     return;
2818
2819   /* Insert the restore before that next real insn in the stream, and before
2820      a potential NOTE_INSN_EPILOGUE_BEG -- we do need these notes to be
2821      properly nested.  This should be after any label or alignment.  This
2822      will be pushed into the CFI stream by the function below.  */
2823   while (1)
2824     {
2825       rtx p = PREV_INSN (i);
2826       if (!NOTE_P (p))
2827         break;
2828       if (NOTE_KIND (p) == NOTE_INSN_BASIC_BLOCK)
2829         break;
2830       i = p;
2831     }
2832   emit_note_before (NOTE_INSN_CFA_RESTORE_STATE, i);
2833
2834   emit_cfa_remember = true;
2835
2836   /* And emulate the state save.  */
2837   gcc_assert (!cfa_remember.in_use);
2838   cfa_remember = cfa;
2839   cfa_remember.in_use = 1;
2840 }
2841
2842 /* A "subroutine" of dwarf2out_begin_epilogue.  Emit the restore required.  */
2843
2844 void
2845 dwarf2out_frame_debug_restore_state (void)
2846 {
2847   dw_cfi_ref cfi = new_cfi (); 
2848   const char *label = dwarf2out_cfi_label (false);
2849
2850   cfi->dw_cfi_opc = DW_CFA_restore_state;
2851   add_fde_cfi (label, cfi);
2852
2853   gcc_assert (cfa_remember.in_use);
2854   cfa = cfa_remember;
2855   cfa_remember.in_use = 0;
2856 }
2857
2858 #endif
2859
2860 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used.  */
2861 static enum dw_cfi_oprnd_type dw_cfi_oprnd1_desc
2862  (enum dwarf_call_frame_info cfi);
2863
2864 static enum dw_cfi_oprnd_type
2865 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
2866 {
2867   switch (cfi)
2868     {
2869     case DW_CFA_nop:
2870     case DW_CFA_GNU_window_save:
2871     case DW_CFA_remember_state:
2872     case DW_CFA_restore_state:
2873       return dw_cfi_oprnd_unused;
2874
2875     case DW_CFA_set_loc:
2876     case DW_CFA_advance_loc1:
2877     case DW_CFA_advance_loc2:
2878     case DW_CFA_advance_loc4:
2879     case DW_CFA_MIPS_advance_loc8:
2880       return dw_cfi_oprnd_addr;
2881
2882     case DW_CFA_offset:
2883     case DW_CFA_offset_extended:
2884     case DW_CFA_def_cfa:
2885     case DW_CFA_offset_extended_sf:
2886     case DW_CFA_def_cfa_sf:
2887     case DW_CFA_restore:
2888     case DW_CFA_restore_extended:
2889     case DW_CFA_undefined:
2890     case DW_CFA_same_value:
2891     case DW_CFA_def_cfa_register:
2892     case DW_CFA_register:
2893       return dw_cfi_oprnd_reg_num;
2894
2895     case DW_CFA_def_cfa_offset:
2896     case DW_CFA_GNU_args_size:
2897     case DW_CFA_def_cfa_offset_sf:
2898       return dw_cfi_oprnd_offset;
2899
2900     case DW_CFA_def_cfa_expression:
2901     case DW_CFA_expression:
2902       return dw_cfi_oprnd_loc;
2903
2904     default:
2905       gcc_unreachable ();
2906     }
2907 }
2908
2909 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used.  */
2910 static enum dw_cfi_oprnd_type dw_cfi_oprnd2_desc
2911  (enum dwarf_call_frame_info cfi);
2912
2913 static enum dw_cfi_oprnd_type
2914 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
2915 {
2916   switch (cfi)
2917     {
2918     case DW_CFA_def_cfa:
2919     case DW_CFA_def_cfa_sf:
2920     case DW_CFA_offset:
2921     case DW_CFA_offset_extended_sf:
2922     case DW_CFA_offset_extended:
2923       return dw_cfi_oprnd_offset;
2924
2925     case DW_CFA_register:
2926       return dw_cfi_oprnd_reg_num;
2927
2928     default:
2929       return dw_cfi_oprnd_unused;
2930     }
2931 }
2932
2933 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
2934
2935 /* Switch to eh_frame_section.  If we don't have an eh_frame_section,
2936    switch to the data section instead, and write out a synthetic label
2937    for collect2.  */
2938
2939 static void
2940 switch_to_eh_frame_section (void)
2941 {
2942   tree label;
2943
2944 #ifdef EH_FRAME_SECTION_NAME
2945   if (eh_frame_section == 0)
2946     {
2947       int flags;
2948
2949       if (EH_TABLES_CAN_BE_READ_ONLY)
2950         {
2951           int fde_encoding;
2952           int per_encoding;
2953           int lsda_encoding;
2954
2955           fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
2956                                                        /*global=*/0);
2957           per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
2958                                                        /*global=*/1);
2959           lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
2960                                                         /*global=*/0);
2961           flags = ((! flag_pic
2962                     || ((fde_encoding & 0x70) != DW_EH_PE_absptr
2963                         && (fde_encoding & 0x70) != DW_EH_PE_aligned
2964                         && (per_encoding & 0x70) != DW_EH_PE_absptr
2965                         && (per_encoding & 0x70) != DW_EH_PE_aligned
2966                         && (lsda_encoding & 0x70) != DW_EH_PE_absptr
2967                         && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
2968                    ? 0 : SECTION_WRITE);
2969         }
2970       else
2971         flags = SECTION_WRITE;
2972       eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
2973     }
2974 #endif
2975
2976   if (eh_frame_section)
2977     switch_to_section (eh_frame_section);
2978   else
2979     {
2980       /* We have no special eh_frame section.  Put the information in
2981          the data section and emit special labels to guide collect2.  */
2982       switch_to_section (data_section);
2983       label = get_file_function_name ("F");
2984       ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
2985       targetm.asm_out.globalize_label (asm_out_file,
2986                                        IDENTIFIER_POINTER (label));
2987       ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
2988     }
2989 }
2990
2991 /* Output a Call Frame Information opcode and its operand(s).  */
2992
2993 static void
2994 output_cfi (dw_cfi_ref cfi, dw_fde_ref fde, int for_eh)
2995 {
2996   unsigned long r;
2997   HOST_WIDE_INT off;
2998
2999   if (cfi->dw_cfi_opc == DW_CFA_advance_loc)
3000     dw2_asm_output_data (1, (cfi->dw_cfi_opc
3001                              | (cfi->dw_cfi_oprnd1.dw_cfi_offset & 0x3f)),
3002                          "DW_CFA_advance_loc " HOST_WIDE_INT_PRINT_HEX,
3003                          ((unsigned HOST_WIDE_INT)
3004                           cfi->dw_cfi_oprnd1.dw_cfi_offset));
3005   else if (cfi->dw_cfi_opc == DW_CFA_offset)
3006     {
3007       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3008       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
3009                            "DW_CFA_offset, column 0x%lx", r);
3010       off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3011       dw2_asm_output_data_uleb128 (off, NULL);
3012     }
3013   else if (cfi->dw_cfi_opc == DW_CFA_restore)
3014     {
3015       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3016       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
3017                            "DW_CFA_restore, column 0x%lx", r);
3018     }
3019   else
3020     {
3021       dw2_asm_output_data (1, cfi->dw_cfi_opc,
3022                            "%s", dwarf_cfi_name (cfi->dw_cfi_opc));
3023
3024       switch (cfi->dw_cfi_opc)
3025         {
3026         case DW_CFA_set_loc:
3027           if (for_eh)
3028             dw2_asm_output_encoded_addr_rtx (
3029                 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0),
3030                 gen_rtx_SYMBOL_REF (Pmode, cfi->dw_cfi_oprnd1.dw_cfi_addr),
3031                 false, NULL);
3032           else
3033             dw2_asm_output_addr (DWARF2_ADDR_SIZE,
3034                                  cfi->dw_cfi_oprnd1.dw_cfi_addr, NULL);
3035           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3036           break;
3037
3038         case DW_CFA_advance_loc1:
3039           dw2_asm_output_delta (1, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3040                                 fde->dw_fde_current_label, NULL);
3041           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3042           break;
3043
3044         case DW_CFA_advance_loc2:
3045           dw2_asm_output_delta (2, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3046                                 fde->dw_fde_current_label, NULL);
3047           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3048           break;
3049
3050         case DW_CFA_advance_loc4:
3051           dw2_asm_output_delta (4, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3052                                 fde->dw_fde_current_label, NULL);
3053           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3054           break;
3055
3056         case DW_CFA_MIPS_advance_loc8:
3057           dw2_asm_output_delta (8, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3058                                 fde->dw_fde_current_label, NULL);
3059           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3060           break;
3061
3062         case DW_CFA_offset_extended:
3063           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3064           dw2_asm_output_data_uleb128 (r, NULL);
3065           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3066           dw2_asm_output_data_uleb128 (off, NULL);
3067           break;
3068
3069         case DW_CFA_def_cfa:
3070           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3071           dw2_asm_output_data_uleb128 (r, NULL);
3072           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
3073           break;
3074
3075         case DW_CFA_offset_extended_sf:
3076           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3077           dw2_asm_output_data_uleb128 (r, NULL);
3078           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3079           dw2_asm_output_data_sleb128 (off, NULL);
3080           break;
3081
3082         case DW_CFA_def_cfa_sf:
3083           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3084           dw2_asm_output_data_uleb128 (r, NULL);
3085           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3086           dw2_asm_output_data_sleb128 (off, NULL);
3087           break;
3088
3089         case DW_CFA_restore_extended:
3090         case DW_CFA_undefined:
3091         case DW_CFA_same_value:
3092         case DW_CFA_def_cfa_register:
3093           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3094           dw2_asm_output_data_uleb128 (r, NULL);
3095           break;
3096
3097         case DW_CFA_register:
3098           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3099           dw2_asm_output_data_uleb128 (r, NULL);
3100           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, for_eh);
3101           dw2_asm_output_data_uleb128 (r, NULL);
3102           break;
3103
3104         case DW_CFA_def_cfa_offset:
3105         case DW_CFA_GNU_args_size:
3106           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
3107           break;
3108
3109         case DW_CFA_def_cfa_offset_sf:
3110           off = div_data_align (cfi->dw_cfi_oprnd1.dw_cfi_offset);
3111           dw2_asm_output_data_sleb128 (off, NULL);
3112           break;
3113
3114         case DW_CFA_GNU_window_save:
3115           break;
3116
3117         case DW_CFA_def_cfa_expression:
3118         case DW_CFA_expression:
3119           output_cfa_loc (cfi);
3120           break;
3121
3122         case DW_CFA_GNU_negative_offset_extended:
3123           /* Obsoleted by DW_CFA_offset_extended_sf.  */
3124           gcc_unreachable ();
3125
3126         default:
3127           break;
3128         }
3129     }
3130 }
3131
3132 /* Similar, but do it via assembler directives instead.  */
3133
3134 static void
3135 output_cfi_directive (dw_cfi_ref cfi)
3136 {
3137   unsigned long r, r2;
3138
3139   switch (cfi->dw_cfi_opc)
3140     {
3141     case DW_CFA_advance_loc:
3142     case DW_CFA_advance_loc1:
3143     case DW_CFA_advance_loc2:
3144     case DW_CFA_advance_loc4:
3145     case DW_CFA_MIPS_advance_loc8:
3146     case DW_CFA_set_loc:
3147       /* Should only be created by add_fde_cfi in a code path not
3148          followed when emitting via directives.  The assembler is
3149          going to take care of this for us.  */
3150       gcc_unreachable ();
3151
3152     case DW_CFA_offset:
3153     case DW_CFA_offset_extended:
3154     case DW_CFA_offset_extended_sf:
3155       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3156       fprintf (asm_out_file, "\t.cfi_offset %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
3157                r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
3158       break;
3159
3160     case DW_CFA_restore:
3161     case DW_CFA_restore_extended:
3162       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3163       fprintf (asm_out_file, "\t.cfi_restore %lu\n", r);
3164       break;
3165
3166     case DW_CFA_undefined:
3167       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3168       fprintf (asm_out_file, "\t.cfi_undefined %lu\n", r);
3169       break;
3170
3171     case DW_CFA_same_value:
3172       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3173       fprintf (asm_out_file, "\t.cfi_same_value %lu\n", r);
3174       break;
3175
3176     case DW_CFA_def_cfa:
3177     case DW_CFA_def_cfa_sf:
3178       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3179       fprintf (asm_out_file, "\t.cfi_def_cfa %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
3180                r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
3181       break;
3182
3183     case DW_CFA_def_cfa_register:
3184       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3185       fprintf (asm_out_file, "\t.cfi_def_cfa_register %lu\n", r);
3186       break;
3187
3188     case DW_CFA_register:
3189       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3190       r2 = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, 1);
3191       fprintf (asm_out_file, "\t.cfi_register %lu, %lu\n", r, r2);
3192       break;
3193
3194     case DW_CFA_def_cfa_offset:
3195     case DW_CFA_def_cfa_offset_sf:
3196       fprintf (asm_out_file, "\t.cfi_def_cfa_offset "
3197                HOST_WIDE_INT_PRINT_DEC"\n",
3198                cfi->dw_cfi_oprnd1.dw_cfi_offset);
3199       break;
3200
3201     case DW_CFA_remember_state:
3202       fprintf (asm_out_file, "\t.cfi_remember_state\n");
3203       break;
3204     case DW_CFA_restore_state:
3205       fprintf (asm_out_file, "\t.cfi_restore_state\n");
3206       break;
3207
3208     case DW_CFA_GNU_args_size:
3209       fprintf (asm_out_file, "\t.cfi_escape 0x%x,", DW_CFA_GNU_args_size);
3210       dw2_asm_output_data_uleb128_raw (cfi->dw_cfi_oprnd1.dw_cfi_offset);
3211       if (flag_debug_asm)
3212         fprintf (asm_out_file, "\t%s args_size "HOST_WIDE_INT_PRINT_DEC,
3213                  ASM_COMMENT_START, cfi->dw_cfi_oprnd1.dw_cfi_offset);
3214       fputc ('\n', asm_out_file);
3215       break;
3216
3217     case DW_CFA_GNU_window_save:
3218       fprintf (asm_out_file, "\t.cfi_window_save\n");
3219       break;
3220
3221     case DW_CFA_def_cfa_expression:
3222     case DW_CFA_expression:
3223       fprintf (asm_out_file, "\t.cfi_escape 0x%x,", cfi->dw_cfi_opc);
3224       output_cfa_loc_raw (cfi);
3225       fputc ('\n', asm_out_file);
3226       break;
3227
3228     default:
3229       gcc_unreachable ();
3230     }
3231 }
3232
3233 DEF_VEC_P (dw_cfi_ref);
3234 DEF_VEC_ALLOC_P (dw_cfi_ref, heap);
3235
3236 /* Output CFIs to bring current FDE to the same state as after executing
3237    CFIs in CFI chain.  DO_CFI_ASM is true if .cfi_* directives shall
3238    be emitted, false otherwise.  If it is false, FDE and FOR_EH are the
3239    other arguments to pass to output_cfi.  */
3240
3241 static void
3242 output_cfis (dw_cfi_ref cfi, bool do_cfi_asm, dw_fde_ref fde, bool for_eh)
3243 {
3244   struct dw_cfi_struct cfi_buf;
3245   dw_cfi_ref cfi2;
3246   dw_cfi_ref cfi_args_size = NULL, cfi_cfa = NULL, cfi_cfa_offset = NULL;
3247   VEC (dw_cfi_ref, heap) *regs = VEC_alloc (dw_cfi_ref, heap, 32);
3248   unsigned int len, idx;
3249
3250   for (;; cfi = cfi->dw_cfi_next)
3251     switch (cfi ? cfi->dw_cfi_opc : DW_CFA_nop)
3252       {
3253       case DW_CFA_advance_loc:
3254       case DW_CFA_advance_loc1:
3255       case DW_CFA_advance_loc2:
3256       case DW_CFA_advance_loc4:
3257       case DW_CFA_MIPS_advance_loc8:
3258       case DW_CFA_set_loc:
3259         /* All advances should be ignored.  */
3260         break;
3261       case DW_CFA_remember_state:
3262         {
3263           dw_cfi_ref args_size = cfi_args_size;
3264
3265           /* Skip everything between .cfi_remember_state and
3266              .cfi_restore_state.  */
3267           for (cfi2 = cfi->dw_cfi_next; cfi2; cfi2 = cfi2->dw_cfi_next)
3268             if (cfi2->dw_cfi_opc == DW_CFA_restore_state)
3269               break;
3270             else if (cfi2->dw_cfi_opc == DW_CFA_GNU_args_size)
3271               args_size = cfi2;
3272             else
3273               gcc_assert (cfi2->dw_cfi_opc != DW_CFA_remember_state);
3274
3275           if (cfi2 == NULL)
3276             goto flush_all;
3277           else
3278             {
3279               cfi = cfi2;
3280               cfi_args_size = args_size;
3281             }
3282           break;
3283         }
3284       case DW_CFA_GNU_args_size:
3285         cfi_args_size = cfi;
3286         break;
3287       case DW_CFA_GNU_window_save:
3288         goto flush_all;
3289       case DW_CFA_offset:
3290       case DW_CFA_offset_extended:
3291       case DW_CFA_offset_extended_sf:
3292       case DW_CFA_restore:
3293       case DW_CFA_restore_extended:
3294       case DW_CFA_undefined:
3295       case DW_CFA_same_value:
3296       case DW_CFA_register:
3297       case DW_CFA_val_offset:
3298       case DW_CFA_val_offset_sf:
3299       case DW_CFA_expression:
3300       case DW_CFA_val_expression:
3301       case DW_CFA_GNU_negative_offset_extended:
3302         if (VEC_length (dw_cfi_ref, regs) <= cfi->dw_cfi_oprnd1.dw_cfi_reg_num)
3303           VEC_safe_grow_cleared (dw_cfi_ref, heap, regs,
3304                                  cfi->dw_cfi_oprnd1.dw_cfi_reg_num + 1);
3305         VEC_replace (dw_cfi_ref, regs, cfi->dw_cfi_oprnd1.dw_cfi_reg_num, cfi);
3306         break;
3307       case DW_CFA_def_cfa:
3308       case DW_CFA_def_cfa_sf:
3309       case DW_CFA_def_cfa_expression:
3310         cfi_cfa = cfi;
3311         cfi_cfa_offset = cfi;
3312         break;
3313       case DW_CFA_def_cfa_register:
3314         cfi_cfa = cfi;
3315         break;
3316       case DW_CFA_def_cfa_offset:
3317       case DW_CFA_def_cfa_offset_sf:
3318         cfi_cfa_offset = cfi;
3319         break;
3320       case DW_CFA_nop:
3321         gcc_assert (cfi == NULL);
3322       flush_all:
3323         len = VEC_length (dw_cfi_ref, regs);
3324         for (idx = 0; idx < len; idx++)
3325           {
3326             cfi2 = VEC_replace (dw_cfi_ref, regs, idx, NULL);
3327             if (cfi2 != NULL
3328                 && cfi2->dw_cfi_opc != DW_CFA_restore
3329                 && cfi2->dw_cfi_opc != DW_CFA_restore_extended)
3330               {
3331                 if (do_cfi_asm)
3332                   output_cfi_directive (cfi2);
3333                 else
3334                   output_cfi (cfi2, fde, for_eh);
3335               }
3336           }
3337         if (cfi_cfa && cfi_cfa_offset && cfi_cfa_offset != cfi_cfa)
3338           {
3339             gcc_assert (cfi_cfa->dw_cfi_opc != DW_CFA_def_cfa_expression);
3340             cfi_buf = *cfi_cfa;
3341             switch (cfi_cfa_offset->dw_cfi_opc)
3342               {
3343               case DW_CFA_def_cfa_offset:
3344                 cfi_buf.dw_cfi_opc = DW_CFA_def_cfa;
3345                 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd1;
3346                 break;
3347               case DW_CFA_def_cfa_offset_sf:
3348                 cfi_buf.dw_cfi_opc = DW_CFA_def_cfa_sf;
3349                 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd1;
3350                 break;
3351               case DW_CFA_def_cfa:
3352               case DW_CFA_def_cfa_sf:
3353                 cfi_buf.dw_cfi_opc = cfi_cfa_offset->dw_cfi_opc;
3354                 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd2;
3355                 break;
3356               default:
3357                 gcc_unreachable ();
3358               }
3359             cfi_cfa = &cfi_buf;
3360           }
3361         else if (cfi_cfa_offset)
3362           cfi_cfa = cfi_cfa_offset;
3363         if (cfi_cfa)
3364           {
3365             if (do_cfi_asm)
3366               output_cfi_directive (cfi_cfa);
3367             else
3368               output_cfi (cfi_cfa, fde, for_eh);
3369           }
3370         cfi_cfa = NULL;
3371         cfi_cfa_offset = NULL;
3372         if (cfi_args_size
3373             && cfi_args_size->dw_cfi_oprnd1.dw_cfi_offset)
3374           {
3375             if (do_cfi_asm)
3376               output_cfi_directive (cfi_args_size);
3377             else
3378               output_cfi (cfi_args_size, fde, for_eh);
3379           }
3380         cfi_args_size = NULL;
3381         if (cfi == NULL)
3382           {
3383             VEC_free (dw_cfi_ref, heap, regs);
3384             return;
3385           }
3386         else if (do_cfi_asm)
3387           output_cfi_directive (cfi);
3388         else
3389           output_cfi (cfi, fde, for_eh);
3390         break;
3391       default:
3392         gcc_unreachable ();
3393     }
3394 }
3395
3396 /* Output one FDE.  */
3397
3398 static void
3399 output_fde (dw_fde_ref fde, bool for_eh, bool second,
3400             char *section_start_label, int fde_encoding, char *augmentation,
3401             bool any_lsda_needed, int lsda_encoding)
3402 {
3403   const char *begin, *end;
3404   static unsigned int j;
3405   char l1[20], l2[20];
3406   dw_cfi_ref cfi;
3407
3408   targetm.asm_out.unwind_label (asm_out_file, fde->decl, for_eh,
3409                                 /* empty */ 0);
3410   targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
3411                                   for_eh + j);
3412   ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
3413   ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
3414   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
3415     dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
3416                          " indicating 64-bit DWARF extension");
3417   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
3418                         "FDE Length");
3419   ASM_OUTPUT_LABEL (asm_out_file, l1);
3420
3421   if (for_eh)
3422     dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
3423   else
3424     dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
3425                            debug_frame_section, "FDE CIE offset");
3426
3427   if (!fde->dw_fde_switched_sections)
3428     {
3429       begin = fde->dw_fde_begin;
3430       end = fde->dw_fde_end;
3431     }
3432   else if (second ^ fde->dw_fde_switched_cold_to_hot)
3433     {
3434       begin = fde->dw_fde_unlikely_section_label;
3435       end = fde->dw_fde_unlikely_section_end_label;
3436     }
3437   else
3438     {
3439       begin = fde->dw_fde_hot_section_label;
3440       end = fde->dw_fde_hot_section_end_label;
3441     }
3442
3443   if (for_eh)
3444     {
3445       rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
3446       SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
3447       dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
3448                                        "FDE initial location");
3449       dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
3450                             end, begin, "FDE address range");
3451     }
3452   else
3453     {
3454       dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
3455       dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
3456     }
3457
3458   if (augmentation[0])
3459     {
3460       if (any_lsda_needed)
3461         {
3462           int size = size_of_encoded_value (lsda_encoding);
3463
3464           if (lsda_encoding == DW_EH_PE_aligned)
3465             {
3466               int offset = (  4         /* Length */
3467                             + 4         /* CIE offset */
3468                             + 2 * size_of_encoded_value (fde_encoding)
3469                             + 1         /* Augmentation size */ );
3470               int pad = -offset & (PTR_SIZE - 1);
3471
3472               size += pad;
3473               gcc_assert (size_of_uleb128 (size) == 1);
3474             }
3475
3476           dw2_asm_output_data_uleb128 (size, "Augmentation size");
3477
3478           if (fde->uses_eh_lsda)
3479             {
3480               ASM_GENERATE_INTERNAL_LABEL (l1, "LLSDA", fde->funcdef_number);
3481               dw2_asm_output_encoded_addr_rtx (lsda_encoding,
3482                                                gen_rtx_SYMBOL_REF (Pmode, l1),
3483                                                false,
3484                                                "Language Specific Data Area");
3485             }
3486           else
3487             {
3488               if (lsda_encoding == DW_EH_PE_aligned)
3489                 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
3490               dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
3491                                    "Language Specific Data Area (none)");
3492             }
3493         }
3494       else
3495         dw2_asm_output_data_uleb128 (0, "Augmentation size");
3496     }
3497
3498   /* Loop through the Call Frame Instructions associated with
3499      this FDE.  */
3500   fde->dw_fde_current_label = begin;
3501   if (!fde->dw_fde_switched_sections)
3502     for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
3503       output_cfi (cfi, fde, for_eh);
3504   else if (!second)
3505     {
3506       if (fde->dw_fde_switch_cfi)
3507         for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
3508           {
3509             output_cfi (cfi, fde, for_eh);
3510             if (cfi == fde->dw_fde_switch_cfi)
3511               break;
3512           }
3513     }
3514   else
3515     {
3516       dw_cfi_ref cfi_next = fde->dw_fde_cfi;
3517
3518       if (fde->dw_fde_switch_cfi)
3519         {
3520           cfi_next = fde->dw_fde_switch_cfi->dw_cfi_next;
3521           fde->dw_fde_switch_cfi->dw_cfi_next = NULL;
3522           output_cfis (fde->dw_fde_cfi, false, fde, for_eh);
3523           fde->dw_fde_switch_cfi->dw_cfi_next = cfi_next;
3524         }
3525       for (cfi = cfi_next; cfi != NULL; cfi = cfi->dw_cfi_next)
3526         output_cfi (cfi, fde, for_eh);
3527     }
3528
3529   /* Pad the FDE out to an address sized boundary.  */
3530   ASM_OUTPUT_ALIGN (asm_out_file,
3531                     floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
3532   ASM_OUTPUT_LABEL (asm_out_file, l2);
3533
3534   j += 2;
3535 }
3536
3537
3538 /* Output the call frame information used to record information
3539    that relates to calculating the frame pointer, and records the
3540    location of saved registers.  */
3541
3542 static void
3543 output_call_frame_info (int for_eh)
3544 {
3545   unsigned int i;
3546   dw_fde_ref fde;
3547   dw_cfi_ref cfi;
3548   char l1[20], l2[20], section_start_label[20];
3549   bool any_lsda_needed = false;
3550   char augmentation[6];
3551   int augmentation_size;
3552   int fde_encoding = DW_EH_PE_absptr;
3553   int per_encoding = DW_EH_PE_absptr;
3554   int lsda_encoding = DW_EH_PE_absptr;
3555   int return_reg;
3556   int dw_cie_version;
3557
3558   /* Don't emit a CIE if there won't be any FDEs.  */
3559   if (fde_table_in_use == 0)
3560     return;
3561
3562   /* Nothing to do if the assembler's doing it all.  */
3563   if (dwarf2out_do_cfi_asm ())
3564     return;
3565
3566   /* If we make FDEs linkonce, we may have to emit an empty label for
3567      an FDE that wouldn't otherwise be emitted.  We want to avoid
3568      having an FDE kept around when the function it refers to is
3569      discarded.  Example where this matters: a primary function
3570      template in C++ requires EH information, but an explicit
3571      specialization doesn't.  */
3572   if (TARGET_USES_WEAK_UNWIND_INFO
3573       && ! flag_asynchronous_unwind_tables
3574       && flag_exceptions
3575       && for_eh)
3576     for (i = 0; i < fde_table_in_use; i++)
3577       if ((fde_table[i].nothrow || fde_table[i].all_throwers_are_sibcalls)
3578           && !fde_table[i].uses_eh_lsda
3579           && ! DECL_WEAK (fde_table[i].decl))
3580         targetm.asm_out.unwind_label (asm_out_file, fde_table[i].decl,
3581                                       for_eh, /* empty */ 1);
3582
3583   /* If we don't have any functions we'll want to unwind out of, don't
3584      emit any EH unwind information.  Note that if exceptions aren't
3585      enabled, we won't have collected nothrow information, and if we
3586      asked for asynchronous tables, we always want this info.  */
3587   if (for_eh)
3588     {
3589       bool any_eh_needed = !flag_exceptions || flag_asynchronous_unwind_tables;
3590
3591       for (i = 0; i < fde_table_in_use; i++)
3592         if (fde_table[i].uses_eh_lsda)
3593           any_eh_needed = any_lsda_needed = true;
3594         else if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
3595           any_eh_needed = true;
3596         else if (! fde_table[i].nothrow
3597                  && ! fde_table[i].all_throwers_are_sibcalls)
3598           any_eh_needed = true;
3599
3600       if (! any_eh_needed)
3601         return;
3602     }
3603
3604   /* We're going to be generating comments, so turn on app.  */
3605   if (flag_debug_asm)
3606     app_enable ();
3607
3608   if (for_eh)
3609     switch_to_eh_frame_section ();
3610   else
3611     {
3612       if (!debug_frame_section)
3613         debug_frame_section = get_section (DEBUG_FRAME_SECTION,
3614                                            SECTION_DEBUG, NULL);
3615       switch_to_section (debug_frame_section);
3616     }
3617
3618   ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
3619   ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
3620
3621   /* Output the CIE.  */
3622   ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
3623   ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
3624   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
3625     dw2_asm_output_data (4, 0xffffffff,
3626       "Initial length escape value indicating 64-bit DWARF extension");
3627   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
3628                         "Length of Common Information Entry");
3629   ASM_OUTPUT_LABEL (asm_out_file, l1);
3630
3631   /* Now that the CIE pointer is PC-relative for EH,
3632      use 0 to identify the CIE.  */
3633   dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
3634                        (for_eh ? 0 : DWARF_CIE_ID),
3635                        "CIE Identifier Tag");
3636
3637   /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
3638      use CIE version 1, unless that would produce incorrect results
3639      due to overflowing the return register column.  */
3640   return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
3641   dw_cie_version = 1;
3642   if (return_reg >= 256 || dwarf_version > 2)
3643     dw_cie_version = 3;
3644   dw2_asm_output_data (1, dw_cie_version, "CIE Version");
3645
3646   augmentation[0] = 0;
3647   augmentation_size = 0;
3648   if (for_eh)
3649     {
3650       char *p;
3651
3652       /* Augmentation:
3653          z      Indicates that a uleb128 is present to size the
3654                 augmentation section.
3655          L      Indicates the encoding (and thus presence) of
3656                 an LSDA pointer in the FDE augmentation.
3657          R      Indicates a non-default pointer encoding for
3658                 FDE code pointers.
3659          P      Indicates the presence of an encoding + language
3660                 personality routine in the CIE augmentation.  */
3661
3662       fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
3663       per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
3664       lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
3665
3666       p = augmentation + 1;
3667       if (eh_personality_libfunc)
3668         {
3669           *p++ = 'P';
3670           augmentation_size += 1 + size_of_encoded_value (per_encoding);
3671           assemble_external_libcall (eh_personality_libfunc);
3672         }
3673       if (any_lsda_needed)
3674         {
3675           *p++ = 'L';
3676           augmentation_size += 1;
3677         }
3678       if (fde_encoding != DW_EH_PE_absptr)
3679         {
3680           *p++ = 'R';
3681           augmentation_size += 1;
3682         }
3683       if (p > augmentation + 1)
3684         {
3685           augmentation[0] = 'z';
3686           *p = '\0';
3687         }
3688
3689       /* Ug.  Some platforms can't do unaligned dynamic relocations at all.  */
3690       if (eh_personality_libfunc && per_encoding == DW_EH_PE_aligned)
3691         {
3692           int offset = (  4             /* Length */
3693                         + 4             /* CIE Id */
3694                         + 1             /* CIE version */
3695                         + strlen (augmentation) + 1     /* Augmentation */
3696                         + size_of_uleb128 (1)           /* Code alignment */
3697                         + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
3698                         + 1             /* RA column */
3699                         + 1             /* Augmentation size */
3700                         + 1             /* Personality encoding */ );
3701           int pad = -offset & (PTR_SIZE - 1);
3702
3703           augmentation_size += pad;
3704
3705           /* Augmentations should be small, so there's scarce need to
3706              iterate for a solution.  Die if we exceed one uleb128 byte.  */
3707           gcc_assert (size_of_uleb128 (augmentation_size) == 1);
3708         }
3709     }
3710
3711   dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
3712   dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
3713   dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
3714                                "CIE Data Alignment Factor");
3715
3716   if (dw_cie_version == 1)
3717     dw2_asm_output_data (1, return_reg, "CIE RA Column");
3718   else
3719     dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
3720
3721   if (augmentation[0])
3722     {
3723       dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
3724       if (eh_personality_libfunc)
3725         {
3726           dw2_asm_output_data (1, per_encoding, "Personality (%s)",
3727                                eh_data_format_name (per_encoding));
3728           dw2_asm_output_encoded_addr_rtx (per_encoding,
3729                                            eh_personality_libfunc,
3730                                            true, NULL);
3731         }
3732
3733       if (any_lsda_needed)
3734         dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
3735                              eh_data_format_name (lsda_encoding));
3736
3737       if (fde_encoding != DW_EH_PE_absptr)
3738         dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
3739                              eh_data_format_name (fde_encoding));
3740     }
3741
3742   for (cfi = cie_cfi_head; cfi != NULL; cfi = cfi->dw_cfi_next)
3743     output_cfi (cfi, NULL, for_eh);
3744
3745   /* Pad the CIE out to an address sized boundary.  */
3746   ASM_OUTPUT_ALIGN (asm_out_file,
3747                     floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
3748   ASM_OUTPUT_LABEL (asm_out_file, l2);
3749
3750   /* Loop through all of the FDE's.  */
3751   for (i = 0; i < fde_table_in_use; i++)
3752     {
3753       unsigned int k;
3754       fde = &fde_table[i];
3755
3756       /* Don't emit EH unwind info for leaf functions that don't need it.  */
3757       if (for_eh && !flag_asynchronous_unwind_tables && flag_exceptions
3758           && (fde->nothrow || fde->all_throwers_are_sibcalls)
3759           && ! (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
3760           && !fde->uses_eh_lsda)
3761         continue;
3762
3763       for (k = 0; k < (fde->dw_fde_switched_sections ? 2 : 1); k++)
3764         output_fde (fde, for_eh, k, section_start_label, fde_encoding,
3765                     augmentation, any_lsda_needed, lsda_encoding);
3766     }
3767
3768   if (for_eh && targetm.terminate_dw2_eh_frame_info)
3769     dw2_asm_output_data (4, 0, "End of Table");
3770 #ifdef MIPS_DEBUGGING_INFO
3771   /* Work around Irix 6 assembler bug whereby labels at the end of a section
3772      get a value of 0.  Putting .align 0 after the label fixes it.  */
3773   ASM_OUTPUT_ALIGN (asm_out_file, 0);
3774 #endif
3775
3776   /* Turn off app to make assembly quicker.  */
3777   if (flag_debug_asm)
3778     app_disable ();
3779 }
3780
3781 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed.  */
3782
3783 static void
3784 dwarf2out_do_cfi_startproc (void)
3785 {
3786   int enc;
3787   rtx ref;
3788
3789   fprintf (asm_out_file, "\t.cfi_startproc\n");
3790
3791   if (eh_personality_libfunc)
3792     {
3793       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
3794       ref = eh_personality_libfunc;
3795
3796       /* ??? The GAS support isn't entirely consistent.  We have to
3797          handle indirect support ourselves, but PC-relative is done
3798          in the assembler.  Further, the assembler can't handle any
3799          of the weirder relocation types.  */
3800       if (enc & DW_EH_PE_indirect)
3801         ref = dw2_force_const_mem (ref, true);
3802
3803       fprintf (asm_out_file, "\t.cfi_personality 0x%x,", enc);
3804       output_addr_const (asm_out_file, ref);
3805       fputc ('\n', asm_out_file);
3806     }
3807
3808   if (crtl->uses_eh_lsda)
3809     {
3810       char lab[20];
3811
3812       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
3813       ASM_GENERATE_INTERNAL_LABEL (lab, "LLSDA",
3814                                    current_function_funcdef_no);
3815       ref = gen_rtx_SYMBOL_REF (Pmode, lab);
3816       SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
3817
3818       if (enc & DW_EH_PE_indirect)
3819         ref = dw2_force_const_mem (ref, true);
3820
3821       fprintf (asm_out_file, "\t.cfi_lsda 0x%x,", enc);
3822       output_addr_const (asm_out_file, ref);
3823       fputc ('\n', asm_out_file);
3824     }
3825 }
3826
3827 /* Output a marker (i.e. a label) for the beginning of a function, before
3828    the prologue.  */
3829
3830 void
3831 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
3832                           const char *file ATTRIBUTE_UNUSED)
3833 {
3834   char label[MAX_ARTIFICIAL_LABEL_BYTES];
3835   char * dup_label;
3836   dw_fde_ref fde;
3837   section *fnsec;
3838
3839   current_function_func_begin_label = NULL;
3840
3841 #ifdef TARGET_UNWIND_INFO
3842   /* ??? current_function_func_begin_label is also used by except.c
3843      for call-site information.  We must emit this label if it might
3844      be used.  */
3845   if ((! flag_exceptions || USING_SJLJ_EXCEPTIONS)
3846       && ! dwarf2out_do_frame ())
3847     return;
3848 #else
3849   if (! dwarf2out_do_frame ())
3850     return;
3851 #endif
3852
3853   fnsec = function_section (current_function_decl);
3854   switch_to_section (fnsec);
3855   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
3856                                current_function_funcdef_no);
3857   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
3858                           current_function_funcdef_no);
3859   dup_label = xstrdup (label);
3860   current_function_func_begin_label = dup_label;
3861
3862 #ifdef TARGET_UNWIND_INFO
3863   /* We can elide the fde allocation if we're not emitting debug info.  */
3864   if (! dwarf2out_do_frame ())
3865     return;
3866 #endif
3867
3868   /* Expand the fde table if necessary.  */
3869   if (fde_table_in_use == fde_table_allocated)
3870     {
3871       fde_table_allocated += FDE_TABLE_INCREMENT;
3872       fde_table = GGC_RESIZEVEC (dw_fde_node, fde_table, fde_table_allocated);
3873       memset (fde_table + fde_table_in_use, 0,
3874               FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
3875     }
3876
3877   /* Record the FDE associated with this function.  */
3878   current_funcdef_fde = fde_table_in_use;
3879
3880   /* Add the new FDE at the end of the fde_table.  */
3881   fde = &fde_table[fde_table_in_use++];
3882   fde->decl = current_function_decl;
3883   fde->dw_fde_begin = dup_label;
3884   fde->dw_fde_current_label = dup_label;
3885   fde->dw_fde_hot_section_label = NULL;
3886   fde->dw_fde_hot_section_end_label = NULL;
3887   fde->dw_fde_unlikely_section_label = NULL;
3888   fde->dw_fde_unlikely_section_end_label = NULL;
3889   fde->dw_fde_switched_sections = 0;
3890   fde->dw_fde_switched_cold_to_hot = 0;
3891   fde->dw_fde_end = NULL;
3892   fde->dw_fde_cfi = NULL;
3893   fde->dw_fde_switch_cfi = NULL;
3894   fde->funcdef_number = current_function_funcdef_no;
3895   fde->nothrow = crtl->nothrow;
3896   fde->uses_eh_lsda = crtl->uses_eh_lsda;
3897   fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
3898   fde->drap_reg = INVALID_REGNUM;
3899   fde->vdrap_reg = INVALID_REGNUM;
3900   if (flag_reorder_blocks_and_partition)
3901     {
3902       section *unlikelysec;
3903       if (first_function_block_is_cold)
3904         fde->in_std_section = 1;
3905       else
3906         fde->in_std_section
3907           = (fnsec == text_section
3908              || (cold_text_section && fnsec == cold_text_section));
3909       unlikelysec = unlikely_text_section ();
3910       fde->cold_in_std_section
3911         = (unlikelysec == text_section
3912            || (cold_text_section && unlikelysec == cold_text_section));
3913     }
3914   else
3915     {
3916       fde->in_std_section
3917         = (fnsec == text_section
3918            || (cold_text_section && fnsec == cold_text_section));
3919       fde->cold_in_std_section = 0;
3920     }
3921
3922   args_size = old_args_size = 0;
3923
3924   /* We only want to output line number information for the genuine dwarf2
3925      prologue case, not the eh frame case.  */
3926 #ifdef DWARF2_DEBUGGING_INFO
3927   if (file)
3928     dwarf2out_source_line (line, file, 0, true);
3929 #endif
3930
3931   if (dwarf2out_do_cfi_asm ())
3932     dwarf2out_do_cfi_startproc ();
3933 }
3934
3935 /* Output a marker (i.e. a label) for the absolute end of the generated code
3936    for a function definition.  This gets called *after* the epilogue code has
3937    been generated.  */
3938
3939 void
3940 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
3941                         const char *file ATTRIBUTE_UNUSED)
3942 {
3943   dw_fde_ref fde;
3944   char label[MAX_ARTIFICIAL_LABEL_BYTES];
3945
3946 #ifdef DWARF2_DEBUGGING_INFO
3947   last_var_location_insn = NULL_RTX;
3948 #endif
3949
3950   if (dwarf2out_do_cfi_asm ())
3951     fprintf (asm_out_file, "\t.cfi_endproc\n");
3952
3953   /* Output a label to mark the endpoint of the code generated for this
3954      function.  */
3955   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
3956                                current_function_funcdef_no);
3957   ASM_OUTPUT_LABEL (asm_out_file, label);
3958   fde = current_fde ();
3959   gcc_assert (fde != NULL);
3960   fde->dw_fde_end = xstrdup (label);
3961 }
3962
3963 void
3964 dwarf2out_frame_init (void)
3965 {
3966   /* Allocate the initial hunk of the fde_table.  */
3967   fde_table = GGC_CNEWVEC (dw_fde_node, FDE_TABLE_INCREMENT);
3968   fde_table_allocated = FDE_TABLE_INCREMENT;
3969   fde_table_in_use = 0;
3970
3971   /* Generate the CFA instructions common to all FDE's.  Do it now for the
3972      sake of lookup_cfa.  */
3973
3974   /* On entry, the Canonical Frame Address is at SP.  */
3975   dwarf2out_def_cfa (NULL, STACK_POINTER_REGNUM, INCOMING_FRAME_SP_OFFSET);
3976
3977 #ifdef DWARF2_UNWIND_INFO
3978   if (DWARF2_UNWIND_INFO || DWARF2_FRAME_INFO)
3979     initial_return_save (INCOMING_RETURN_ADDR_RTX);
3980 #endif
3981 }
3982
3983 void
3984 dwarf2out_frame_finish (void)
3985 {
3986   /* Output call frame information.  */
3987   if (DWARF2_FRAME_INFO)
3988     output_call_frame_info (0);
3989
3990 #ifndef TARGET_UNWIND_INFO
3991   /* Output another copy for the unwinder.  */
3992   if (! USING_SJLJ_EXCEPTIONS && (flag_unwind_tables || flag_exceptions))
3993     output_call_frame_info (1);
3994 #endif
3995 }
3996
3997 /* Note that the current function section is being used for code.  */
3998
3999 static void
4000 dwarf2out_note_section_used (void)
4001 {
4002   section *sec = current_function_section ();
4003   if (sec == text_section)
4004     text_section_used = true;
4005   else if (sec == cold_text_section)
4006     cold_text_section_used = true;
4007 }
4008
4009 void
4010 dwarf2out_switch_text_section (void)
4011 {
4012   dw_fde_ref fde = current_fde ();
4013
4014   gcc_assert (cfun && fde && !fde->dw_fde_switched_sections);
4015
4016   fde->dw_fde_switched_sections = 1;
4017   fde->dw_fde_switched_cold_to_hot = !in_cold_section_p;
4018
4019   fde->dw_fde_hot_section_label = crtl->subsections.hot_section_label;
4020   fde->dw_fde_hot_section_end_label = crtl->subsections.hot_section_end_label;
4021   fde->dw_fde_unlikely_section_label = crtl->subsections.cold_section_label;
4022   fde->dw_fde_unlikely_section_end_label = crtl->subsections.cold_section_end_label;
4023   have_multiple_function_sections = true;
4024
4025   /* Reset the current label on switching text sections, so that we
4026      don't attempt to advance_loc4 between labels in different sections.  */
4027   fde->dw_fde_current_label = NULL;
4028
4029   /* There is no need to mark used sections when not debugging.  */
4030   if (cold_text_section != NULL)
4031     dwarf2out_note_section_used ();
4032
4033   if (dwarf2out_do_cfi_asm ())
4034     fprintf (asm_out_file, "\t.cfi_endproc\n");
4035
4036   /* Now do the real section switch.  */
4037   switch_to_section (current_function_section ());
4038
4039   if (dwarf2out_do_cfi_asm ())
4040     {
4041       dwarf2out_do_cfi_startproc ();
4042       /* As this is a different FDE, insert all current CFI instructions
4043          again.  */
4044       output_cfis (fde->dw_fde_cfi, true, fde, true);
4045     }
4046   else
4047     {
4048       dw_cfi_ref cfi = fde->dw_fde_cfi;
4049
4050       cfi = fde->dw_fde_cfi;
4051       if (cfi)
4052         while (cfi->dw_cfi_next != NULL)
4053           cfi = cfi->dw_cfi_next;
4054       fde->dw_fde_switch_cfi = cfi;
4055     }
4056 }
4057 #endif
4058 \f
4059 /* And now, the subset of the debugging information support code necessary
4060    for emitting location expressions.  */
4061
4062 /* Data about a single source file.  */
4063 struct GTY(()) dwarf_file_data {
4064   const char * filename;
4065   int emitted_number;
4066 };
4067
4068 typedef struct dw_val_struct *dw_val_ref;
4069 typedef struct die_struct *dw_die_ref;
4070 typedef const struct die_struct *const_dw_die_ref;
4071 typedef struct dw_loc_descr_struct *dw_loc_descr_ref;
4072 typedef struct dw_loc_list_struct *dw_loc_list_ref;
4073
4074 typedef struct GTY(()) deferred_locations_struct
4075 {
4076   tree variable;
4077   dw_die_ref die;
4078 } deferred_locations;
4079
4080 DEF_VEC_O(deferred_locations);
4081 DEF_VEC_ALLOC_O(deferred_locations,gc);
4082
4083 static GTY(()) VEC(deferred_locations, gc) *deferred_locations_list;
4084
4085 /* Each DIE may have a series of attribute/value pairs.  Values
4086    can take on several forms.  The forms that are used in this
4087    implementation are listed below.  */
4088
4089 enum dw_val_class
4090 {
4091   dw_val_class_addr,
4092   dw_val_class_offset,
4093   dw_val_class_loc,
4094   dw_val_class_loc_list,
4095   dw_val_class_range_list,
4096   dw_val_class_const,
4097   dw_val_class_unsigned_const,
4098   dw_val_class_long_long,
4099   dw_val_class_vec,
4100   dw_val_class_flag,
4101   dw_val_class_die_ref,
4102   dw_val_class_fde_ref,
4103   dw_val_class_lbl_id,
4104   dw_val_class_lineptr,
4105   dw_val_class_str,
4106   dw_val_class_macptr,
4107   dw_val_class_file
4108 };
4109
4110 /* Describe a double word constant value.  */
4111 /* ??? Every instance of long_long in the code really means CONST_DOUBLE.  */
4112
4113 typedef struct GTY(()) dw_long_long_struct {
4114   unsigned long hi;
4115   unsigned long low;
4116 }
4117 dw_long_long_const;
4118
4119 /* Describe a floating point constant value, or a vector constant value.  */
4120
4121 typedef struct GTY(()) dw_vec_struct {
4122   unsigned char * GTY((length ("%h.length"))) array;
4123   unsigned length;
4124   unsigned elt_size;
4125 }
4126 dw_vec_const;
4127
4128 /* The dw_val_node describes an attribute's value, as it is
4129    represented internally.  */
4130
4131 typedef struct GTY(()) dw_val_struct {
4132   enum dw_val_class val_class;
4133   union dw_val_struct_union
4134     {
4135       rtx GTY ((tag ("dw_val_class_addr"))) val_addr;
4136       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_offset"))) val_offset;
4137       dw_loc_list_ref GTY ((tag ("dw_val_class_loc_list"))) val_loc_list;
4138       dw_loc_descr_ref GTY ((tag ("dw_val_class_loc"))) val_loc;
4139       HOST_WIDE_INT GTY ((default)) val_int;
4140       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_unsigned_const"))) val_unsigned;
4141       dw_long_long_const GTY ((tag ("dw_val_class_long_long"))) val_long_long;
4142       dw_vec_const GTY ((tag ("dw_val_class_vec"))) val_vec;
4143       struct dw_val_die_union
4144         {
4145           dw_die_ref die;
4146           int external;
4147         } GTY ((tag ("dw_val_class_die_ref"))) val_die_ref;
4148       unsigned GTY ((tag ("dw_val_class_fde_ref"))) val_fde_index;
4149       struct indirect_string_node * GTY ((tag ("dw_val_class_str"))) val_str;
4150       char * GTY ((tag ("dw_val_class_lbl_id"))) val_lbl_id;
4151       unsigned char GTY ((tag ("dw_val_class_flag"))) val_flag;
4152       struct dwarf_file_data * GTY ((tag ("dw_val_class_file"))) val_file;
4153     }
4154   GTY ((desc ("%1.val_class"))) v;
4155 }
4156 dw_val_node;
4157
4158 /* Locations in memory are described using a sequence of stack machine
4159    operations.  */
4160
4161 typedef struct GTY(()) dw_loc_descr_struct {
4162   dw_loc_descr_ref dw_loc_next;
4163   ENUM_BITFIELD (dwarf_location_atom) dw_loc_opc : 8;
4164   /* Used to distinguish DW_OP_addr with a direct symbol relocation
4165      from DW_OP_addr with a dtp-relative symbol relocation.  */
4166   unsigned int dtprel : 1;
4167   int dw_loc_addr;
4168   dw_val_node dw_loc_oprnd1;
4169   dw_val_node dw_loc_oprnd2;
4170 }
4171 dw_loc_descr_node;
4172
4173 /* Location lists are ranges + location descriptions for that range,
4174    so you can track variables that are in different places over
4175    their entire life.  */
4176 typedef struct GTY(()) dw_loc_list_struct {
4177   dw_loc_list_ref dw_loc_next;
4178   const char *begin; /* Label for begin address of range */
4179   const char *end;  /* Label for end address of range */
4180   char *ll_symbol; /* Label for beginning of location list.
4181                       Only on head of list */
4182   const char *section; /* Section this loclist is relative to */
4183   dw_loc_descr_ref expr;
4184 } dw_loc_list_node;
4185
4186 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
4187
4188 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
4189
4190 /* Convert a DWARF stack opcode into its string name.  */
4191
4192 static const char *
4193 dwarf_stack_op_name (unsigned int op)
4194 {
4195   switch (op)
4196     {
4197     case DW_OP_addr:
4198       return "DW_OP_addr";
4199     case DW_OP_deref:
4200       return "DW_OP_deref";
4201     case DW_OP_const1u:
4202       return "DW_OP_const1u";
4203     case DW_OP_const1s:
4204       return "DW_OP_const1s";
4205     case DW_OP_const2u:
4206       return "DW_OP_const2u";
4207     case DW_OP_const2s:
4208       return "DW_OP_const2s";
4209     case DW_OP_const4u:
4210       return "DW_OP_const4u";
4211     case DW_OP_const4s:
4212       return "DW_OP_const4s";
4213     case DW_OP_const8u:
4214       return "DW_OP_const8u";
4215     case DW_OP_const8s:
4216       return "DW_OP_const8s";
4217     case DW_OP_constu:
4218       return "DW_OP_constu";
4219     case DW_OP_consts:
4220       return "DW_OP_consts";
4221     case DW_OP_dup:
4222       return "DW_OP_dup";
4223     case DW_OP_drop:
4224       return "DW_OP_drop";
4225     case DW_OP_over:
4226       return "DW_OP_over";
4227     case DW_OP_pick:
4228       return "DW_OP_pick";
4229     case DW_OP_swap:
4230       return "DW_OP_swap";
4231     case DW_OP_rot:
4232       return "DW_OP_rot";
4233     case DW_OP_xderef:
4234       return "DW_OP_xderef";
4235     case DW_OP_abs:
4236       return "DW_OP_abs";
4237     case DW_OP_and:
4238       return "DW_OP_and";
4239     case DW_OP_div:
4240       return "DW_OP_div";
4241     case DW_OP_minus:
4242       return "DW_OP_minus";
4243     case DW_OP_mod:
4244       return "DW_OP_mod";
4245     case DW_OP_mul:
4246       return "DW_OP_mul";
4247     case DW_OP_neg:
4248       return "DW_OP_neg";
4249     case DW_OP_not:
4250       return "DW_OP_not";
4251     case DW_OP_or:
4252       return "DW_OP_or";
4253     case DW_OP_plus:
4254       return "DW_OP_plus";
4255     case DW_OP_plus_uconst:
4256       return "DW_OP_plus_uconst";
4257     case DW_OP_shl:
4258       return "DW_OP_shl";
4259     case DW_OP_shr:
4260       return "DW_OP_shr";
4261     case DW_OP_shra:
4262       return "DW_OP_shra";
4263     case DW_OP_xor:
4264       return "DW_OP_xor";
4265     case DW_OP_bra:
4266       return "DW_OP_bra";
4267     case DW_OP_eq:
4268       return "DW_OP_eq";
4269     case DW_OP_ge:
4270       return "DW_OP_ge";
4271     case DW_OP_gt:
4272       return "DW_OP_gt";
4273     case DW_OP_le:
4274       return "DW_OP_le";
4275     case DW_OP_lt:
4276       return "DW_OP_lt";
4277     case DW_OP_ne:
4278       return "DW_OP_ne";
4279     case DW_OP_skip:
4280       return "DW_OP_skip";
4281     case DW_OP_lit0:
4282       return "DW_OP_lit0";
4283     case DW_OP_lit1:
4284       return "DW_OP_lit1";
4285     case DW_OP_lit2:
4286       return "DW_OP_lit2";
4287     case DW_OP_lit3:
4288       return "DW_OP_lit3";
4289     case DW_OP_lit4:
4290       return "DW_OP_lit4";
4291     case DW_OP_lit5:
4292       return "DW_OP_lit5";
4293     case DW_OP_lit6:
4294       return "DW_OP_lit6";
4295     case DW_OP_lit7:
4296       return "DW_OP_lit7";
4297     case DW_OP_lit8:
4298       return "DW_OP_lit8";
4299     case DW_OP_lit9:
4300       return "DW_OP_lit9";
4301     case DW_OP_lit10:
4302       return "DW_OP_lit10";
4303     case DW_OP_lit11:
4304       return "DW_OP_lit11";
4305     case DW_OP_lit12:
4306       return "DW_OP_lit12";
4307     case DW_OP_lit13:
4308       return "DW_OP_lit13";
4309     case DW_OP_lit14:
4310       return "DW_OP_lit14";
4311     case DW_OP_lit15:
4312       return "DW_OP_lit15";
4313     case DW_OP_lit16:
4314       return "DW_OP_lit16";
4315     case DW_OP_lit17:
4316       return "DW_OP_lit17";
4317     case DW_OP_lit18:
4318       return "DW_OP_lit18";
4319     case DW_OP_lit19:
4320       return "DW_OP_lit19";
4321     case DW_OP_lit20:
4322       return "DW_OP_lit20";
4323     case DW_OP_lit21:
4324       return "DW_OP_lit21";
4325     case DW_OP_lit22:
4326       return "DW_OP_lit22";
4327     case DW_OP_lit23:
4328       return "DW_OP_lit23";
4329     case DW_OP_lit24:
4330       return "DW_OP_lit24";
4331     case DW_OP_lit25:
4332       return "DW_OP_lit25";
4333     case DW_OP_lit26:
4334       return "DW_OP_lit26";
4335     case DW_OP_lit27:
4336       return "DW_OP_lit27";
4337     case DW_OP_lit28:
4338       return "DW_OP_lit28";
4339     case DW_OP_lit29:
4340       return "DW_OP_lit29";
4341     case DW_OP_lit30:
4342       return "DW_OP_lit30";
4343     case DW_OP_lit31:
4344       return "DW_OP_lit31";
4345     case DW_OP_reg0:
4346       return "DW_OP_reg0";
4347     case DW_OP_reg1:
4348       return "DW_OP_reg1";
4349     case DW_OP_reg2:
4350       return "DW_OP_reg2";
4351     case DW_OP_reg3:
4352       return "DW_OP_reg3";
4353     case DW_OP_reg4:
4354       return "DW_OP_reg4";
4355     case DW_OP_reg5:
4356       return "DW_OP_reg5";
4357     case DW_OP_reg6:
4358       return "DW_OP_reg6";
4359     case DW_OP_reg7:
4360       return "DW_OP_reg7";
4361     case DW_OP_reg8:
4362       return "DW_OP_reg8";
4363     case DW_OP_reg9:
4364       return "DW_OP_reg9";
4365     case DW_OP_reg10:
4366       return "DW_OP_reg10";
4367     case DW_OP_reg11:
4368       return "DW_OP_reg11";
4369     case DW_OP_reg12:
4370       return "DW_OP_reg12";
4371     case DW_OP_reg13:
4372       return "DW_OP_reg13";
4373     case DW_OP_reg14:
4374       return "DW_OP_reg14";
4375     case DW_OP_reg15:
4376       return "DW_OP_reg15";
4377     case DW_OP_reg16:
4378       return "DW_OP_reg16";
4379     case DW_OP_reg17:
4380       return "DW_OP_reg17";
4381     case DW_OP_reg18:
4382       return "DW_OP_reg18";
4383     case DW_OP_reg19:
4384       return "DW_OP_reg19";
4385     case DW_OP_reg20:
4386       return "DW_OP_reg20";
4387     case DW_OP_reg21:
4388       return "DW_OP_reg21";
4389     case DW_OP_reg22:
4390       return "DW_OP_reg22";
4391     case DW_OP_reg23:
4392       return "DW_OP_reg23";
4393     case DW_OP_reg24:
4394       return "DW_OP_reg24";
4395     case DW_OP_reg25:
4396       return "DW_OP_reg25";
4397     case DW_OP_reg26:
4398       return "DW_OP_reg26";
4399     case DW_OP_reg27:
4400       return "DW_OP_reg27";
4401     case DW_OP_reg28:
4402       return "DW_OP_reg28";
4403     case DW_OP_reg29:
4404       return "DW_OP_reg29";
4405     case DW_OP_reg30:
4406       return "DW_OP_reg30";
4407     case DW_OP_reg31:
4408       return "DW_OP_reg31";
4409     case DW_OP_breg0:
4410       return "DW_OP_breg0";
4411     case DW_OP_breg1:
4412       return "DW_OP_breg1";
4413     case DW_OP_breg2:
4414       return "DW_OP_breg2";
4415     case DW_OP_breg3:
4416       return "DW_OP_breg3";
4417     case DW_OP_breg4:
4418       return "DW_OP_breg4";
4419     case DW_OP_breg5:
4420       return "DW_OP_breg5";
4421     case DW_OP_breg6:
4422       return "DW_OP_breg6";
4423     case DW_OP_breg7:
4424       return "DW_OP_breg7";
4425     case DW_OP_breg8:
4426       return "DW_OP_breg8";
4427     case DW_OP_breg9:
4428       return "DW_OP_breg9";
4429     case DW_OP_breg10:
4430       return "DW_OP_breg10";
4431     case DW_OP_breg11:
4432       return "DW_OP_breg11";
4433     case DW_OP_breg12:
4434       return "DW_OP_breg12";
4435     case DW_OP_breg13:
4436       return "DW_OP_breg13";
4437     case DW_OP_breg14:
4438       return "DW_OP_breg14";
4439     case DW_OP_breg15:
4440       return "DW_OP_breg15";
4441     case DW_OP_breg16:
4442       return "DW_OP_breg16";
4443     case DW_OP_breg17:
4444       return "DW_OP_breg17";
4445     case DW_OP_breg18:
4446       return "DW_OP_breg18";
4447     case DW_OP_breg19:
4448       return "DW_OP_breg19";
4449     case DW_OP_breg20:
4450       return "DW_OP_breg20";
4451     case DW_OP_breg21:
4452       return "DW_OP_breg21";
4453     case DW_OP_breg22:
4454       return "DW_OP_breg22";
4455     case DW_OP_breg23:
4456       return "DW_OP_breg23";
4457     case DW_OP_breg24:
4458       return "DW_OP_breg24";
4459     case DW_OP_breg25:
4460       return "DW_OP_breg25";
4461     case DW_OP_breg26:
4462       return "DW_OP_breg26";
4463     case DW_OP_breg27:
4464       return "DW_OP_breg27";
4465     case DW_OP_breg28:
4466       return "DW_OP_breg28";
4467     case DW_OP_breg29:
4468       return "DW_OP_breg29";
4469     case DW_OP_breg30:
4470       return "DW_OP_breg30";
4471     case DW_OP_breg31:
4472       return "DW_OP_breg31";
4473     case DW_OP_regx:
4474       return "DW_OP_regx";
4475     case DW_OP_fbreg:
4476       return "DW_OP_fbreg";
4477     case DW_OP_bregx:
4478       return "DW_OP_bregx";
4479     case DW_OP_piece:
4480       return "DW_OP_piece";
4481     case DW_OP_deref_size:
4482       return "DW_OP_deref_size";
4483     case DW_OP_xderef_size:
4484       return "DW_OP_xderef_size";
4485     case DW_OP_nop:
4486       return "DW_OP_nop";
4487
4488     case DW_OP_push_object_address:
4489       return "DW_OP_push_object_address";
4490     case DW_OP_call2:
4491       return "DW_OP_call2";
4492     case DW_OP_call4:
4493       return "DW_OP_call4";
4494     case DW_OP_call_ref:
4495       return "DW_OP_call_ref";
4496     case DW_OP_form_tls_address:
4497       return "DW_OP_form_tls_address";
4498     case DW_OP_call_frame_cfa:
4499       return "DW_OP_call_frame_cfa";
4500     case DW_OP_bit_piece:
4501       return "DW_OP_bit_piece";
4502
4503     case DW_OP_GNU_push_tls_address:
4504       return "DW_OP_GNU_push_tls_address";
4505     case DW_OP_GNU_uninit:
4506       return "DW_OP_GNU_uninit";
4507     case DW_OP_GNU_encoded_addr:
4508       return "DW_OP_GNU_encoded_addr";
4509
4510     default:
4511       return "OP_<unknown>";
4512     }
4513 }
4514
4515 /* Return a pointer to a newly allocated location description.  Location
4516    descriptions are simple expression terms that can be strung
4517    together to form more complicated location (address) descriptions.  */
4518
4519 static inline dw_loc_descr_ref
4520 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
4521                unsigned HOST_WIDE_INT oprnd2)
4522 {
4523   dw_loc_descr_ref descr = GGC_CNEW (dw_loc_descr_node);
4524
4525   descr->dw_loc_opc = op;
4526   descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
4527   descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
4528   descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
4529   descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
4530
4531   return descr;
4532 }
4533
4534 /* Return a pointer to a newly allocated location description for
4535    REG and OFFSET.  */
4536
4537 static inline dw_loc_descr_ref
4538 new_reg_loc_descr (unsigned int reg,  unsigned HOST_WIDE_INT offset)
4539 {
4540   if (reg <= 31)
4541     return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
4542                           offset, 0);
4543   else
4544     return new_loc_descr (DW_OP_bregx, reg, offset);
4545 }
4546
4547 /* Add a location description term to a location description expression.  */
4548
4549 static inline void
4550 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
4551 {
4552   dw_loc_descr_ref *d;
4553
4554   /* Find the end of the chain.  */
4555   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
4556     ;
4557
4558   *d = descr;
4559 }
4560
4561 /* Add a constant OFFSET to a location expression.  */
4562
4563 static void
4564 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
4565 {
4566   dw_loc_descr_ref loc;
4567   HOST_WIDE_INT *p;
4568
4569   gcc_assert (*list_head != NULL);
4570
4571   if (!offset)
4572     return;
4573
4574   /* Find the end of the chain.  */
4575   for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
4576     ;
4577
4578   p = NULL;
4579   if (loc->dw_loc_opc == DW_OP_fbreg
4580       || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
4581     p = &loc->dw_loc_oprnd1.v.val_int;
4582   else if (loc->dw_loc_opc == DW_OP_bregx)
4583     p = &loc->dw_loc_oprnd2.v.val_int;
4584
4585   /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
4586      offset.  Don't optimize if an signed integer overflow would happen.  */
4587   if (p != NULL
4588       && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
4589           || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
4590     *p += offset;
4591
4592   else if (offset > 0)
4593     loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
4594
4595   else
4596     {
4597       loc->dw_loc_next = int_loc_descriptor (offset);
4598       add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_plus, 0, 0));
4599     }
4600 }
4601
4602 /* Return the size of a location descriptor.  */
4603
4604 static unsigned long
4605 size_of_loc_descr (dw_loc_descr_ref loc)
4606 {
4607   unsigned long size = 1;
4608
4609   switch (loc->dw_loc_opc)
4610     {
4611     case DW_OP_addr:
4612       size += DWARF2_ADDR_SIZE;
4613       break;
4614     case DW_OP_const1u:
4615     case DW_OP_const1s:
4616       size += 1;
4617       break;
4618     case DW_OP_const2u:
4619     case DW_OP_const2s:
4620       size += 2;
4621       break;
4622     case DW_OP_const4u:
4623     case DW_OP_const4s:
4624       size += 4;
4625       break;
4626     case DW_OP_const8u:
4627     case DW_OP_const8s:
4628       size += 8;
4629       break;
4630     case DW_OP_constu:
4631       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4632       break;
4633     case DW_OP_consts:
4634       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4635       break;
4636     case DW_OP_pick:
4637       size += 1;
4638       break;
4639     case DW_OP_plus_uconst:
4640       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4641       break;
4642     case DW_OP_skip:
4643     case DW_OP_bra:
4644       size += 2;
4645       break;
4646     case DW_OP_breg0:
4647     case DW_OP_breg1:
4648     case DW_OP_breg2:
4649     case DW_OP_breg3:
4650     case DW_OP_breg4:
4651     case DW_OP_breg5:
4652     case DW_OP_breg6:
4653     case DW_OP_breg7:
4654     case DW_OP_breg8:
4655     case DW_OP_breg9:
4656     case DW_OP_breg10:
4657     case DW_OP_breg11:
4658     case DW_OP_breg12:
4659     case DW_OP_breg13:
4660     case DW_OP_breg14:
4661     case DW_OP_breg15:
4662     case DW_OP_breg16:
4663     case DW_OP_breg17:
4664     case DW_OP_breg18:
4665     case DW_OP_breg19:
4666     case DW_OP_breg20:
4667     case DW_OP_breg21:
4668     case DW_OP_breg22:
4669     case DW_OP_breg23:
4670     case DW_OP_breg24:
4671     case DW_OP_breg25:
4672     case DW_OP_breg26:
4673     case DW_OP_breg27:
4674     case DW_OP_breg28:
4675     case DW_OP_breg29:
4676     case DW_OP_breg30:
4677     case DW_OP_breg31:
4678       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4679       break;
4680     case DW_OP_regx:
4681       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4682       break;
4683     case DW_OP_fbreg:
4684       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4685       break;
4686     case DW_OP_bregx:
4687       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4688       size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
4689       break;
4690     case DW_OP_piece:
4691       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4692       break;
4693     case DW_OP_deref_size:
4694     case DW_OP_xderef_size:
4695       size += 1;
4696       break;
4697     case DW_OP_call2:
4698       size += 2;
4699       break;
4700     case DW_OP_call4:
4701       size += 4;
4702       break;
4703     case DW_OP_call_ref:
4704       size += DWARF2_ADDR_SIZE;
4705       break;
4706     default:
4707       break;
4708     }
4709
4710   return size;
4711 }
4712
4713 /* Return the size of a series of location descriptors.  */
4714
4715 static unsigned long
4716 size_of_locs (dw_loc_descr_ref loc)
4717 {
4718   dw_loc_descr_ref l;
4719   unsigned long size;
4720
4721   /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
4722      field, to avoid writing to a PCH file.  */
4723   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
4724     {
4725       if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
4726         break;
4727       size += size_of_loc_descr (l);
4728     }
4729   if (! l)
4730     return size;
4731
4732   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
4733     {
4734       l->dw_loc_addr = size;
4735       size += size_of_loc_descr (l);
4736     }
4737
4738   return size;
4739 }
4740
4741 /* Output location description stack opcode's operands (if any).  */
4742
4743 static void
4744 output_loc_operands (dw_loc_descr_ref loc)
4745 {
4746   dw_val_ref val1 = &loc->dw_loc_oprnd1;
4747   dw_val_ref val2 = &loc->dw_loc_oprnd2;
4748
4749   switch (loc->dw_loc_opc)
4750     {
4751 #ifdef DWARF2_DEBUGGING_INFO
4752     case DW_OP_const2u:
4753     case DW_OP_const2s:
4754       dw2_asm_output_data (2, val1->v.val_int, NULL);
4755       break;
4756     case DW_OP_const4u:
4757     case DW_OP_const4s:
4758       dw2_asm_output_data (4, val1->v.val_int, NULL);
4759       break;
4760     case DW_OP_const8u:
4761     case DW_OP_const8s:
4762       gcc_assert (HOST_BITS_PER_LONG >= 64);
4763       dw2_asm_output_data (8, val1->v.val_int, NULL);
4764       break;
4765     case DW_OP_skip:
4766     case DW_OP_bra:
4767       {
4768         int offset;
4769
4770         gcc_assert (val1->val_class == dw_val_class_loc);
4771         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
4772
4773         dw2_asm_output_data (2, offset, NULL);
4774       }
4775       break;
4776 #else
4777     case DW_OP_const2u:
4778     case DW_OP_const2s:
4779     case DW_OP_const4u:
4780     case DW_OP_const4s:
4781     case DW_OP_const8u:
4782     case DW_OP_const8s:
4783     case DW_OP_skip:
4784     case DW_OP_bra:
4785       /* We currently don't make any attempt to make sure these are
4786          aligned properly like we do for the main unwind info, so
4787          don't support emitting things larger than a byte if we're
4788          only doing unwinding.  */
4789       gcc_unreachable ();
4790 #endif
4791     case DW_OP_const1u:
4792     case DW_OP_const1s:
4793       dw2_asm_output_data (1, val1->v.val_int, NULL);
4794       break;
4795     case DW_OP_constu:
4796       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4797       break;
4798     case DW_OP_consts:
4799       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
4800       break;
4801     case DW_OP_pick:
4802       dw2_asm_output_data (1, val1->v.val_int, NULL);
4803       break;
4804     case DW_OP_plus_uconst:
4805       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4806       break;
4807     case DW_OP_breg0:
4808     case DW_OP_breg1:
4809     case DW_OP_breg2:
4810     case DW_OP_breg3:
4811     case DW_OP_breg4:
4812     case DW_OP_breg5:
4813     case DW_OP_breg6:
4814     case DW_OP_breg7:
4815     case DW_OP_breg8:
4816     case DW_OP_breg9:
4817     case DW_OP_breg10:
4818     case DW_OP_breg11:
4819     case DW_OP_breg12:
4820     case DW_OP_breg13:
4821     case DW_OP_breg14:
4822     case DW_OP_breg15:
4823     case DW_OP_breg16:
4824     case DW_OP_breg17:
4825     case DW_OP_breg18:
4826     case DW_OP_breg19:
4827     case DW_OP_breg20:
4828     case DW_OP_breg21:
4829     case DW_OP_breg22:
4830     case DW_OP_breg23:
4831     case DW_OP_breg24:
4832     case DW_OP_breg25:
4833     case DW_OP_breg26:
4834     case DW_OP_breg27:
4835     case DW_OP_breg28:
4836     case DW_OP_breg29:
4837     case DW_OP_breg30:
4838     case DW_OP_breg31:
4839       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
4840       break;
4841     case DW_OP_regx:
4842       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4843       break;
4844     case DW_OP_fbreg:
4845       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
4846       break;
4847     case DW_OP_bregx:
4848       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4849       dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
4850       break;
4851     case DW_OP_piece:
4852       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4853       break;
4854     case DW_OP_deref_size:
4855     case DW_OP_xderef_size:
4856       dw2_asm_output_data (1, val1->v.val_int, NULL);
4857       break;
4858
4859     case DW_OP_addr:
4860       if (loc->dtprel)
4861         {
4862           if (targetm.asm_out.output_dwarf_dtprel)
4863             {
4864               targetm.asm_out.output_dwarf_dtprel (asm_out_file,
4865                                                    DWARF2_ADDR_SIZE,
4866                                                    val1->v.val_addr);
4867               fputc ('\n', asm_out_file);
4868             }
4869           else
4870             gcc_unreachable ();
4871         }
4872       else
4873         {
4874 #ifdef DWARF2_DEBUGGING_INFO
4875           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
4876 #else
4877           gcc_unreachable ();
4878 #endif
4879         }
4880       break;
4881
4882     default:
4883       /* Other codes have no operands.  */
4884       break;
4885     }
4886 }
4887
4888 /* Output a sequence of location operations.  */
4889
4890 static void
4891 output_loc_sequence (dw_loc_descr_ref loc)
4892 {
4893   for (; loc != NULL; loc = loc->dw_loc_next)
4894     {
4895       /* Output the opcode.  */
4896       dw2_asm_output_data (1, loc->dw_loc_opc,
4897                            "%s", dwarf_stack_op_name (loc->dw_loc_opc));
4898
4899       /* Output the operand(s) (if any).  */
4900       output_loc_operands (loc);
4901     }
4902 }
4903
4904 /* Output location description stack opcode's operands (if any).
4905    The output is single bytes on a line, suitable for .cfi_escape.  */
4906
4907 static void
4908 output_loc_operands_raw (dw_loc_descr_ref loc)
4909 {
4910   dw_val_ref val1 = &loc->dw_loc_oprnd1;
4911   dw_val_ref val2 = &loc->dw_loc_oprnd2;
4912
4913   switch (loc->dw_loc_opc)
4914     {
4915     case DW_OP_addr:
4916       /* We cannot output addresses in .cfi_escape, only bytes.  */
4917       gcc_unreachable ();
4918
4919     case DW_OP_const1u:
4920     case DW_OP_const1s:
4921     case DW_OP_pick:
4922     case DW_OP_deref_size:
4923     case DW_OP_xderef_size:
4924       fputc (',', asm_out_file);
4925       dw2_asm_output_data_raw (1, val1->v.val_int);
4926       break;
4927
4928     case DW_OP_const2u:
4929     case DW_OP_const2s:
4930       fputc (',', asm_out_file);
4931       dw2_asm_output_data_raw (2, val1->v.val_int);
4932       break;
4933
4934     case DW_OP_const4u:
4935     case DW_OP_const4s:
4936       fputc (',', asm_out_file);
4937       dw2_asm_output_data_raw (4, val1->v.val_int);
4938       break;
4939
4940     case DW_OP_const8u:
4941     case DW_OP_const8s:
4942       gcc_assert (HOST_BITS_PER_LONG >= 64);
4943       fputc (',', asm_out_file);
4944       dw2_asm_output_data_raw (8, val1->v.val_int);
4945       break;
4946
4947     case DW_OP_skip:
4948     case DW_OP_bra:
4949       {
4950         int offset;
4951
4952         gcc_assert (val1->val_class == dw_val_class_loc);
4953         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
4954
4955         fputc (',', asm_out_file);
4956         dw2_asm_output_data_raw (2, offset);
4957       }
4958       break;
4959
4960     case DW_OP_constu:
4961     case DW_OP_plus_uconst:
4962     case DW_OP_regx:
4963     case DW_OP_piece:
4964       fputc (',', asm_out_file);
4965       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
4966       break;
4967
4968     case DW_OP_consts:
4969     case DW_OP_breg0:
4970     case DW_OP_breg1:
4971     case DW_OP_breg2:
4972     case DW_OP_breg3:
4973     case DW_OP_breg4:
4974     case DW_OP_breg5:
4975     case DW_OP_breg6:
4976     case DW_OP_breg7:
4977     case DW_OP_breg8:
4978     case DW_OP_breg9:
4979     case DW_OP_breg10:
4980     case DW_OP_breg11:
4981     case DW_OP_breg12:
4982     case DW_OP_breg13:
4983     case DW_OP_breg14:
4984     case DW_OP_breg15:
4985     case DW_OP_breg16:
4986     case DW_OP_breg17:
4987     case DW_OP_breg18:
4988     case DW_OP_breg19:
4989     case DW_OP_breg20:
4990     case DW_OP_breg21:
4991     case DW_OP_breg22:
4992     case DW_OP_breg23:
4993     case DW_OP_breg24:
4994     case DW_OP_breg25:
4995     case DW_OP_breg26:
4996     case DW_OP_breg27:
4997     case DW_OP_breg28:
4998     case DW_OP_breg29:
4999     case DW_OP_breg30:
5000     case DW_OP_breg31:
5001     case DW_OP_fbreg:
5002       fputc (',', asm_out_file);
5003       dw2_asm_output_data_sleb128_raw (val1->v.val_int);
5004       break;
5005
5006     case DW_OP_bregx:
5007       fputc (',', asm_out_file);
5008       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
5009       fputc (',', asm_out_file);
5010       dw2_asm_output_data_sleb128_raw (val2->v.val_int);
5011       break;
5012
5013     default:
5014       /* Other codes have no operands.  */
5015       break;
5016     }
5017 }
5018
5019 static void
5020 output_loc_sequence_raw (dw_loc_descr_ref loc)
5021 {
5022   while (1)
5023     {
5024       /* Output the opcode.  */
5025       fprintf (asm_out_file, "0x%x", loc->dw_loc_opc);
5026       output_loc_operands_raw (loc);
5027
5028       if (!loc->dw_loc_next)
5029         break;
5030       loc = loc->dw_loc_next;
5031
5032       fputc (',', asm_out_file);
5033     }
5034 }
5035
5036 /* This routine will generate the correct assembly data for a location
5037    description based on a cfi entry with a complex address.  */
5038
5039 static void
5040 output_cfa_loc (dw_cfi_ref cfi)
5041 {
5042   dw_loc_descr_ref loc;
5043   unsigned long size;
5044
5045   if (cfi->dw_cfi_opc == DW_CFA_expression)
5046     dw2_asm_output_data (1, cfi->dw_cfi_oprnd2.dw_cfi_reg_num, NULL);
5047
5048   /* Output the size of the block.  */
5049   loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
5050   size = size_of_locs (loc);
5051   dw2_asm_output_data_uleb128 (size, NULL);
5052
5053   /* Now output the operations themselves.  */
5054   output_loc_sequence (loc);
5055 }
5056
5057 /* Similar, but used for .cfi_escape.  */
5058
5059 static void
5060 output_cfa_loc_raw (dw_cfi_ref cfi)
5061 {
5062   dw_loc_descr_ref loc;
5063   unsigned long size;
5064
5065   if (cfi->dw_cfi_opc == DW_CFA_expression)
5066     fprintf (asm_out_file, "0x%x,", cfi->dw_cfi_oprnd2.dw_cfi_reg_num);
5067
5068   /* Output the size of the block.  */
5069   loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
5070   size = size_of_locs (loc);
5071   dw2_asm_output_data_uleb128_raw (size);
5072   fputc (',', asm_out_file);
5073
5074   /* Now output the operations themselves.  */
5075   output_loc_sequence_raw (loc);
5076 }
5077
5078 /* This function builds a dwarf location descriptor sequence from a
5079    dw_cfa_location, adding the given OFFSET to the result of the
5080    expression.  */
5081
5082 static struct dw_loc_descr_struct *
5083 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
5084 {
5085   struct dw_loc_descr_struct *head, *tmp;
5086
5087   offset += cfa->offset;
5088
5089   if (cfa->indirect)
5090     {
5091       head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
5092       head->dw_loc_oprnd1.val_class = dw_val_class_const;
5093       tmp = new_loc_descr (DW_OP_deref, 0, 0);
5094       add_loc_descr (&head, tmp);
5095       if (offset != 0)
5096         {
5097           tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
5098           add_loc_descr (&head, tmp);
5099         }
5100     }
5101   else
5102     head = new_reg_loc_descr (cfa->reg, offset);
5103
5104   return head;
5105 }
5106
5107 /* This function builds a dwarf location descriptor sequence for
5108    the address at OFFSET from the CFA when stack is aligned to
5109    ALIGNMENT byte.  */
5110
5111 static struct dw_loc_descr_struct *
5112 build_cfa_aligned_loc (HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
5113 {
5114   struct dw_loc_descr_struct *head;
5115   unsigned int dwarf_fp
5116     = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
5117
5118  /* When CFA is defined as FP+OFFSET, emulate stack alignment.  */
5119   if (cfa.reg == HARD_FRAME_POINTER_REGNUM && cfa.indirect == 0)
5120     {
5121       head = new_reg_loc_descr (dwarf_fp, 0);
5122       add_loc_descr (&head, int_loc_descriptor (alignment));
5123       add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
5124       loc_descr_plus_const (&head, offset);
5125     }
5126   else
5127     head = new_reg_loc_descr (dwarf_fp, offset);
5128   return head;
5129 }
5130
5131 /* This function fills in aa dw_cfa_location structure from a dwarf location
5132    descriptor sequence.  */
5133
5134 static void
5135 get_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_struct *loc)
5136 {
5137   struct dw_loc_descr_struct *ptr;
5138   cfa->offset = 0;
5139   cfa->base_offset = 0;
5140   cfa->indirect = 0;
5141   cfa->reg = -1;
5142
5143   for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
5144     {
5145       enum dwarf_location_atom op = ptr->dw_loc_opc;
5146
5147       switch (op)
5148         {
5149         case DW_OP_reg0:
5150         case DW_OP_reg1:
5151         case DW_OP_reg2:
5152         case DW_OP_reg3:
5153         case DW_OP_reg4:
5154         case DW_OP_reg5:
5155         case DW_OP_reg6:
5156         case DW_OP_reg7:
5157         case DW_OP_reg8:
5158         case DW_OP_reg9:
5159         case DW_OP_reg10:
5160         case DW_OP_reg11:
5161         case DW_OP_reg12:
5162         case DW_OP_reg13:
5163         case DW_OP_reg14:
5164         case DW_OP_reg15:
5165         case DW_OP_reg16:
5166         case DW_OP_reg17:
5167         case DW_OP_reg18:
5168         case DW_OP_reg19:
5169         case DW_OP_reg20:
5170         case DW_OP_reg21:
5171         case DW_OP_reg22:
5172         case DW_OP_reg23:
5173         case DW_OP_reg24:
5174         case DW_OP_reg25:
5175         case DW_OP_reg26:
5176         case DW_OP_reg27:
5177         case DW_OP_reg28:
5178         case DW_OP_reg29:
5179         case DW_OP_reg30:
5180         case DW_OP_reg31:
5181           cfa->reg = op - DW_OP_reg0;
5182           break;
5183         case DW_OP_regx:
5184           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
5185           break;
5186         case DW_OP_breg0:
5187         case DW_OP_breg1:
5188         case DW_OP_breg2:
5189         case DW_OP_breg3:
5190         case DW_OP_breg4:
5191         case DW_OP_breg5:
5192         case DW_OP_breg6:
5193         case DW_OP_breg7:
5194         case DW_OP_breg8:
5195         case DW_OP_breg9:
5196         case DW_OP_breg10:
5197         case DW_OP_breg11:
5198         case DW_OP_breg12:
5199         case DW_OP_breg13:
5200         case DW_OP_breg14:
5201         case DW_OP_breg15:
5202         case DW_OP_breg16:
5203         case DW_OP_breg17:
5204         case DW_OP_breg18:
5205         case DW_OP_breg19:
5206         case DW_OP_breg20:
5207         case DW_OP_breg21:
5208         case DW_OP_breg22:
5209         case DW_OP_breg23:
5210         case DW_OP_breg24:
5211         case DW_OP_breg25:
5212         case DW_OP_breg26:
5213         case DW_OP_breg27:
5214         case DW_OP_breg28:
5215         case DW_OP_breg29:
5216         case DW_OP_breg30:
5217         case DW_OP_breg31:
5218           cfa->reg = op - DW_OP_breg0;
5219           cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
5220           break;
5221         case DW_OP_bregx:
5222           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
5223           cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
5224           break;
5225         case DW_OP_deref:
5226           cfa->indirect = 1;
5227           break;
5228         case DW_OP_plus_uconst:
5229           cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
5230           break;
5231         default:
5232           internal_error ("DW_LOC_OP %s not implemented",
5233                           dwarf_stack_op_name (ptr->dw_loc_opc));
5234         }
5235     }
5236 }
5237 #endif /* .debug_frame support */
5238 \f
5239 /* And now, the support for symbolic debugging information.  */
5240 #ifdef DWARF2_DEBUGGING_INFO
5241
5242 /* .debug_str support.  */
5243 static int output_indirect_string (void **, void *);
5244
5245 static void dwarf2out_init (const char *);
5246 static void dwarf2out_finish (const char *);
5247 static void dwarf2out_define (unsigned int, const char *);
5248 static void dwarf2out_undef (unsigned int, const char *);
5249 static void dwarf2out_start_source_file (unsigned, const char *);
5250 static void dwarf2out_end_source_file (unsigned);
5251 static void dwarf2out_begin_block (unsigned, unsigned);
5252 static void dwarf2out_end_block (unsigned, unsigned);
5253 static bool dwarf2out_ignore_block (const_tree);
5254 static void dwarf2out_global_decl (tree);
5255 static void dwarf2out_type_decl (tree, int);
5256 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
5257 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
5258                                                  dw_die_ref);
5259 static void dwarf2out_abstract_function (tree);
5260 static void dwarf2out_var_location (rtx);
5261 static void dwarf2out_begin_function (tree);
5262 static void dwarf2out_set_name (tree, tree);
5263
5264 /* The debug hooks structure.  */
5265
5266 const struct gcc_debug_hooks dwarf2_debug_hooks =
5267 {
5268   dwarf2out_init,
5269   dwarf2out_finish,
5270   dwarf2out_define,
5271   dwarf2out_undef,
5272   dwarf2out_start_source_file,
5273   dwarf2out_end_source_file,
5274   dwarf2out_begin_block,
5275   dwarf2out_end_block,
5276   dwarf2out_ignore_block,
5277   dwarf2out_source_line,
5278   dwarf2out_begin_prologue,
5279   debug_nothing_int_charstar,   /* end_prologue */
5280   dwarf2out_end_epilogue,
5281   dwarf2out_begin_function,
5282   debug_nothing_int,            /* end_function */
5283   dwarf2out_decl,               /* function_decl */
5284   dwarf2out_global_decl,
5285   dwarf2out_type_decl,          /* type_decl */
5286   dwarf2out_imported_module_or_decl,
5287   debug_nothing_tree,           /* deferred_inline_function */
5288   /* The DWARF 2 backend tries to reduce debugging bloat by not
5289      emitting the abstract description of inline functions until
5290      something tries to reference them.  */
5291   dwarf2out_abstract_function,  /* outlining_inline_function */
5292   debug_nothing_rtx,            /* label */
5293   debug_nothing_int,            /* handle_pch */
5294   dwarf2out_var_location,
5295   dwarf2out_switch_text_section,
5296   dwarf2out_set_name,
5297   1                             /* start_end_main_source_file */
5298 };
5299 #endif
5300 \f
5301 /* NOTE: In the comments in this file, many references are made to
5302    "Debugging Information Entries".  This term is abbreviated as `DIE'
5303    throughout the remainder of this file.  */
5304
5305 /* An internal representation of the DWARF output is built, and then
5306    walked to generate the DWARF debugging info.  The walk of the internal
5307    representation is done after the entire program has been compiled.
5308    The types below are used to describe the internal representation.  */
5309
5310 /* Various DIE's use offsets relative to the beginning of the
5311    .debug_info section to refer to each other.  */
5312
5313 typedef long int dw_offset;
5314
5315 /* Define typedefs here to avoid circular dependencies.  */
5316
5317 typedef struct dw_attr_struct *dw_attr_ref;
5318 typedef struct dw_line_info_struct *dw_line_info_ref;
5319 typedef struct dw_separate_line_info_struct *dw_separate_line_info_ref;
5320 typedef struct pubname_struct *pubname_ref;
5321 typedef struct dw_ranges_struct *dw_ranges_ref;
5322 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
5323
5324 /* Each entry in the line_info_table maintains the file and
5325    line number associated with the label generated for that
5326    entry.  The label gives the PC value associated with
5327    the line number entry.  */
5328
5329 typedef struct GTY(()) dw_line_info_struct {
5330   unsigned long dw_file_num;
5331   unsigned long dw_line_num;
5332 }
5333 dw_line_info_entry;
5334
5335 /* Line information for functions in separate sections; each one gets its
5336    own sequence.  */
5337 typedef struct GTY(()) dw_separate_line_info_struct {
5338   unsigned long dw_file_num;
5339   unsigned long dw_line_num;
5340   unsigned long function;
5341 }
5342 dw_separate_line_info_entry;
5343
5344 /* Each DIE attribute has a field specifying the attribute kind,
5345    a link to the next attribute in the chain, and an attribute value.
5346    Attributes are typically linked below the DIE they modify.  */
5347
5348 typedef struct GTY(()) dw_attr_struct {
5349   enum dwarf_attribute dw_attr;
5350   dw_val_node dw_attr_val;
5351 }
5352 dw_attr_node;
5353
5354 DEF_VEC_O(dw_attr_node);
5355 DEF_VEC_ALLOC_O(dw_attr_node,gc);
5356
5357 /* The Debugging Information Entry (DIE) structure.  DIEs form a tree.
5358    The children of each node form a circular list linked by
5359    die_sib.  die_child points to the node *before* the "first" child node.  */
5360
5361 typedef struct GTY((chain_circular ("%h.die_sib"))) die_struct {
5362   enum dwarf_tag die_tag;
5363   char *die_symbol;
5364   VEC(dw_attr_node,gc) * die_attr;
5365   dw_die_ref die_parent;
5366   dw_die_ref die_child;
5367   dw_die_ref die_sib;
5368   dw_die_ref die_definition; /* ref from a specification to its definition */
5369   dw_offset die_offset;
5370   unsigned long die_abbrev;
5371   int die_mark;
5372   /* Die is used and must not be pruned as unused.  */
5373   int die_perennial_p;
5374   unsigned int decl_id;
5375 }
5376 die_node;
5377
5378 /* Evaluate 'expr' while 'c' is set to each child of DIE in order.  */
5379 #define FOR_EACH_CHILD(die, c, expr) do {       \
5380   c = die->die_child;                           \
5381   if (c) do {                                   \
5382     c = c->die_sib;                             \
5383     expr;                                       \
5384   } while (c != die->die_child);                \
5385 } while (0)
5386
5387 /* The pubname structure */
5388
5389 typedef struct GTY(()) pubname_struct {
5390   dw_die_ref die;
5391   const char *name;
5392 }
5393 pubname_entry;
5394
5395 DEF_VEC_O(pubname_entry);
5396 DEF_VEC_ALLOC_O(pubname_entry, gc);
5397
5398 struct GTY(()) dw_ranges_struct {
5399   /* If this is positive, it's a block number, otherwise it's a
5400      bitwise-negated index into dw_ranges_by_label.  */
5401   int num;
5402 };
5403
5404 struct GTY(()) dw_ranges_by_label_struct {
5405   const char *begin;
5406   const char *end;
5407 };
5408
5409 /* The limbo die list structure.  */
5410 typedef struct GTY(()) limbo_die_struct {
5411   dw_die_ref die;
5412   tree created_for;
5413   struct limbo_die_struct *next;
5414 }
5415 limbo_die_node;
5416
5417 /* How to start an assembler comment.  */
5418 #ifndef ASM_COMMENT_START
5419 #define ASM_COMMENT_START ";#"
5420 #endif
5421
5422 /* Define a macro which returns nonzero for a TYPE_DECL which was
5423    implicitly generated for a tagged type.
5424
5425    Note that unlike the gcc front end (which generates a NULL named
5426    TYPE_DECL node for each complete tagged type, each array type, and
5427    each function type node created) the g++ front end generates a
5428    _named_ TYPE_DECL node for each tagged type node created.
5429    These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
5430    generate a DW_TAG_typedef DIE for them.  */
5431
5432 #define TYPE_DECL_IS_STUB(decl)                         \
5433   (DECL_NAME (decl) == NULL_TREE                        \
5434    || (DECL_ARTIFICIAL (decl)                           \
5435        && is_tagged_type (TREE_TYPE (decl))             \
5436        && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl)))  \
5437            /* This is necessary for stub decls that     \
5438               appear in nested inline functions.  */    \
5439            || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
5440                && (decl_ultimate_origin (decl)          \
5441                    == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
5442
5443 /* Information concerning the compilation unit's programming
5444    language, and compiler version.  */
5445
5446 /* Fixed size portion of the DWARF compilation unit header.  */
5447 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
5448   (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
5449
5450 /* Fixed size portion of public names info.  */
5451 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
5452
5453 /* Fixed size portion of the address range info.  */
5454 #define DWARF_ARANGES_HEADER_SIZE                                       \
5455   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4,      \
5456                 DWARF2_ADDR_SIZE * 2)                                   \
5457    - DWARF_INITIAL_LENGTH_SIZE)
5458
5459 /* Size of padding portion in the address range info.  It must be
5460    aligned to twice the pointer size.  */
5461 #define DWARF_ARANGES_PAD_SIZE \
5462   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
5463                 DWARF2_ADDR_SIZE * 2)                              \
5464    - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
5465
5466 /* Use assembler line directives if available.  */
5467 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
5468 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
5469 #define DWARF2_ASM_LINE_DEBUG_INFO 1
5470 #else
5471 #define DWARF2_ASM_LINE_DEBUG_INFO 0
5472 #endif
5473 #endif
5474
5475 /* Minimum line offset in a special line info. opcode.
5476    This value was chosen to give a reasonable range of values.  */
5477 #define DWARF_LINE_BASE  -10
5478
5479 /* First special line opcode - leave room for the standard opcodes.  */
5480 #define DWARF_LINE_OPCODE_BASE  10
5481
5482 /* Range of line offsets in a special line info. opcode.  */
5483 #define DWARF_LINE_RANGE  (254-DWARF_LINE_OPCODE_BASE+1)
5484
5485 /* Flag that indicates the initial value of the is_stmt_start flag.
5486    In the present implementation, we do not mark any lines as
5487    the beginning of a source statement, because that information
5488    is not made available by the GCC front-end.  */
5489 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
5490
5491 #ifdef DWARF2_DEBUGGING_INFO
5492 /* This location is used by calc_die_sizes() to keep track
5493    the offset of each DIE within the .debug_info section.  */
5494 static unsigned long next_die_offset;
5495 #endif
5496
5497 /* Record the root of the DIE's built for the current compilation unit.  */
5498 static GTY(()) dw_die_ref comp_unit_die;
5499
5500 /* A list of DIEs with a NULL parent waiting to be relocated.  */
5501 static GTY(()) limbo_die_node *limbo_die_list;
5502
5503 /* A list of DIEs for which we may have to generate
5504    DW_AT_MIPS_linkage_name once their DECL_ASSEMBLER_NAMEs are
5505    set.  */
5506 static GTY(()) limbo_die_node *deferred_asm_name;
5507
5508 /* Filenames referenced by this compilation unit.  */
5509 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
5510
5511 /* A hash table of references to DIE's that describe declarations.
5512    The key is a DECL_UID() which is a unique number identifying each decl.  */
5513 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
5514
5515 /* A hash table of references to DIE's that describe COMMON blocks.
5516    The key is DECL_UID() ^ die_parent.  */
5517 static GTY ((param_is (struct die_struct))) htab_t common_block_die_table;
5518
5519 /* Node of the variable location list.  */
5520 struct GTY ((chain_next ("%h.next"))) var_loc_node {
5521   rtx GTY (()) var_loc_note;
5522   const char * GTY (()) label;
5523   const char * GTY (()) section_label;
5524   struct var_loc_node * GTY (()) next;
5525 };
5526
5527 /* Variable location list.  */
5528 struct GTY (()) var_loc_list_def {
5529   struct var_loc_node * GTY (()) first;
5530
5531   /* Do not mark the last element of the chained list because
5532      it is marked through the chain.  */
5533   struct var_loc_node * GTY ((skip ("%h"))) last;
5534
5535   /* DECL_UID of the variable decl.  */
5536   unsigned int decl_id;
5537 };
5538 typedef struct var_loc_list_def var_loc_list;
5539
5540
5541 /* Table of decl location linked lists.  */
5542 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
5543
5544 /* A pointer to the base of a list of references to DIE's that
5545    are uniquely identified by their tag, presence/absence of
5546    children DIE's, and list of attribute/value pairs.  */
5547 static GTY((length ("abbrev_die_table_allocated")))
5548   dw_die_ref *abbrev_die_table;
5549
5550 /* Number of elements currently allocated for abbrev_die_table.  */
5551 static GTY(()) unsigned abbrev_die_table_allocated;
5552
5553 /* Number of elements in type_die_table currently in use.  */
5554 static GTY(()) unsigned abbrev_die_table_in_use;
5555
5556 /* Size (in elements) of increments by which we may expand the
5557    abbrev_die_table.  */
5558 #define ABBREV_DIE_TABLE_INCREMENT 256
5559
5560 /* A pointer to the base of a table that contains line information
5561    for each source code line in .text in the compilation unit.  */
5562 static GTY((length ("line_info_table_allocated")))
5563      dw_line_info_ref line_info_table;
5564
5565 /* Number of elements currently allocated for line_info_table.  */
5566 static GTY(()) unsigned line_info_table_allocated;
5567
5568 /* Number of elements in line_info_table currently in use.  */
5569 static GTY(()) unsigned line_info_table_in_use;
5570
5571 /* A pointer to the base of a table that contains line information
5572    for each source code line outside of .text in the compilation unit.  */
5573 static GTY ((length ("separate_line_info_table_allocated")))
5574      dw_separate_line_info_ref separate_line_info_table;
5575
5576 /* Number of elements currently allocated for separate_line_info_table.  */
5577 static GTY(()) unsigned separate_line_info_table_allocated;
5578
5579 /* Number of elements in separate_line_info_table currently in use.  */
5580 static GTY(()) unsigned separate_line_info_table_in_use;
5581
5582 /* Size (in elements) of increments by which we may expand the
5583    line_info_table.  */
5584 #define LINE_INFO_TABLE_INCREMENT 1024
5585
5586 /* A pointer to the base of a table that contains a list of publicly
5587    accessible names.  */
5588 static GTY (()) VEC (pubname_entry, gc) *  pubname_table;
5589
5590 /* A pointer to the base of a table that contains a list of publicly
5591    accessible types.  */
5592 static GTY (()) VEC (pubname_entry, gc) * pubtype_table;
5593
5594 /* Array of dies for which we should generate .debug_arange info.  */
5595 static GTY((length ("arange_table_allocated"))) dw_die_ref *arange_table;
5596
5597 /* Number of elements currently allocated for arange_table.  */
5598 static GTY(()) unsigned arange_table_allocated;
5599
5600 /* Number of elements in arange_table currently in use.  */
5601 static GTY(()) unsigned arange_table_in_use;
5602
5603 /* Size (in elements) of increments by which we may expand the
5604    arange_table.  */
5605 #define ARANGE_TABLE_INCREMENT 64
5606
5607 /* Array of dies for which we should generate .debug_ranges info.  */
5608 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
5609
5610 /* Number of elements currently allocated for ranges_table.  */
5611 static GTY(()) unsigned ranges_table_allocated;
5612
5613 /* Number of elements in ranges_table currently in use.  */
5614 static GTY(()) unsigned ranges_table_in_use;
5615
5616 /* Array of pairs of labels referenced in ranges_table.  */
5617 static GTY ((length ("ranges_by_label_allocated")))
5618      dw_ranges_by_label_ref ranges_by_label;
5619
5620 /* Number of elements currently allocated for ranges_by_label.  */
5621 static GTY(()) unsigned ranges_by_label_allocated;
5622
5623 /* Number of elements in ranges_by_label currently in use.  */
5624 static GTY(()) unsigned ranges_by_label_in_use;
5625
5626 /* Size (in elements) of increments by which we may expand the
5627    ranges_table.  */
5628 #define RANGES_TABLE_INCREMENT 64
5629
5630 /* Whether we have location lists that need outputting */
5631 static GTY(()) bool have_location_lists;
5632
5633 /* Unique label counter.  */
5634 static GTY(()) unsigned int loclabel_num;
5635
5636 #ifdef DWARF2_DEBUGGING_INFO
5637 /* Record whether the function being analyzed contains inlined functions.  */
5638 static int current_function_has_inlines;
5639 #endif
5640 #if 0 && defined (MIPS_DEBUGGING_INFO)
5641 static int comp_unit_has_inlines;
5642 #endif
5643
5644 /* The last file entry emitted by maybe_emit_file().  */
5645 static GTY(()) struct dwarf_file_data * last_emitted_file;
5646
5647 /* Number of internal labels generated by gen_internal_sym().  */
5648 static GTY(()) int label_num;
5649
5650 /* Cached result of previous call to lookup_filename.  */
5651 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
5652
5653 #ifdef DWARF2_DEBUGGING_INFO
5654
5655 /* Offset from the "steady-state frame pointer" to the frame base,
5656    within the current function.  */
5657 static HOST_WIDE_INT frame_pointer_fb_offset;
5658
5659 /* Forward declarations for functions defined in this file.  */
5660
5661 static int is_pseudo_reg (const_rtx);
5662 static tree type_main_variant (tree);
5663 static int is_tagged_type (const_tree);
5664 static const char *dwarf_tag_name (unsigned);
5665 static const char *dwarf_attr_name (unsigned);
5666 static const char *dwarf_form_name (unsigned);
5667 static tree decl_ultimate_origin (const_tree);
5668 static tree decl_class_context (tree);
5669 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
5670 static inline enum dw_val_class AT_class (dw_attr_ref);
5671 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
5672 static inline unsigned AT_flag (dw_attr_ref);
5673 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
5674 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
5675 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
5676 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
5677 static void add_AT_long_long (dw_die_ref, enum dwarf_attribute, unsigned long,
5678                               unsigned long);
5679 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
5680                                unsigned int, unsigned char *);
5681 static hashval_t debug_str_do_hash (const void *);
5682 static int debug_str_eq (const void *, const void *);
5683 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
5684 static inline const char *AT_string (dw_attr_ref);
5685 static enum dwarf_form AT_string_form (dw_attr_ref);
5686 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
5687 static void add_AT_specification (dw_die_ref, dw_die_ref);
5688 static inline dw_die_ref AT_ref (dw_attr_ref);
5689 static inline int AT_ref_external (dw_attr_ref);
5690 static inline void set_AT_ref_external (dw_attr_ref, int);
5691 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
5692 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
5693 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
5694 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
5695                              dw_loc_list_ref);
5696 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
5697 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
5698 static inline rtx AT_addr (dw_attr_ref);
5699 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
5700 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
5701 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
5702 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
5703                            unsigned HOST_WIDE_INT);
5704 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
5705                                unsigned long);
5706 static inline const char *AT_lbl (dw_attr_ref);
5707 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
5708 static const char *get_AT_low_pc (dw_die_ref);
5709 static const char *get_AT_hi_pc (dw_die_ref);
5710 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
5711 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
5712 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
5713 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
5714 static bool is_c_family (void);
5715 static bool is_cxx (void);
5716 static bool is_java (void);
5717 static bool is_fortran (void);
5718 static bool is_ada (void);
5719 static void remove_AT (dw_die_ref, enum dwarf_attribute);
5720 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
5721 static void add_child_die (dw_die_ref, dw_die_ref);
5722 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
5723 static dw_die_ref lookup_type_die (tree);
5724 static void equate_type_number_to_die (tree, dw_die_ref);
5725 static hashval_t decl_die_table_hash (const void *);
5726 static int decl_die_table_eq (const void *, const void *);
5727 static dw_die_ref lookup_decl_die (tree);
5728 static hashval_t common_block_die_table_hash (const void *);
5729 static int common_block_die_table_eq (const void *, const void *);
5730 static hashval_t decl_loc_table_hash (const void *);
5731 static int decl_loc_table_eq (const void *, const void *);
5732 static var_loc_list *lookup_decl_loc (const_tree);
5733 static void equate_decl_number_to_die (tree, dw_die_ref);
5734 static void add_var_loc_to_decl (tree, struct var_loc_node *);
5735 static void print_spaces (FILE *);
5736 static void print_die (dw_die_ref, FILE *);
5737 static void print_dwarf_line_table (FILE *);
5738 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
5739 static dw_die_ref pop_compile_unit (dw_die_ref);
5740 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
5741 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
5742 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
5743 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
5744 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
5745 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
5746 static int same_die_p (dw_die_ref, dw_die_ref, int *);
5747 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
5748 static void compute_section_prefix (dw_die_ref);
5749 static int is_type_die (dw_die_ref);
5750 static int is_comdat_die (dw_die_ref);
5751 static int is_symbol_die (dw_die_ref);
5752 static void assign_symbol_names (dw_die_ref);
5753 static void break_out_includes (dw_die_ref);
5754 static hashval_t htab_cu_hash (const void *);
5755 static int htab_cu_eq (const void *, const void *);
5756 static void htab_cu_del (void *);
5757 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
5758 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
5759 static void add_sibling_attributes (dw_die_ref);
5760 static void build_abbrev_table (dw_die_ref);
5761 static void output_location_lists (dw_die_ref);
5762 static int constant_size (unsigned HOST_WIDE_INT);
5763 static unsigned long size_of_die (dw_die_ref);
5764 static void calc_die_sizes (dw_die_ref);
5765 static void mark_dies (dw_die_ref);
5766 static void unmark_dies (dw_die_ref);
5767 static void unmark_all_dies (dw_die_ref);
5768 static unsigned long size_of_pubnames (VEC (pubname_entry,gc) *);
5769 static unsigned long size_of_aranges (void);
5770 static enum dwarf_form value_format (dw_attr_ref);
5771 static void output_value_format (dw_attr_ref);
5772 static void output_abbrev_section (void);
5773 static void output_die_symbol (dw_die_ref);
5774 static void output_die (dw_die_ref);
5775 static void output_compilation_unit_header (void);
5776 static void output_comp_unit (dw_die_ref, int);
5777 static const char *dwarf2_name (tree, int);
5778 static void add_pubname (tree, dw_die_ref);
5779 static void add_pubname_string (const char *, dw_die_ref);
5780 static void add_pubtype (tree, dw_die_ref);
5781 static void output_pubnames (VEC (pubname_entry,gc) *);
5782 static void add_arange (tree, dw_die_ref);
5783 static void output_aranges (void);
5784 static unsigned int add_ranges_num (int);
5785 static unsigned int add_ranges (const_tree);
5786 static unsigned int add_ranges_by_labels (const char *, const char *);
5787 static void output_ranges (void);
5788 static void output_line_info (void);
5789 static void output_file_names (void);
5790 static dw_die_ref base_type_die (tree);
5791 static int is_base_type (tree);
5792 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
5793 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
5794 static int type_is_enum (const_tree);
5795 static unsigned int dbx_reg_number (const_rtx);
5796 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
5797 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
5798 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
5799                                                 enum var_init_status);
5800 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
5801                                                      enum var_init_status);
5802 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
5803                                          enum var_init_status);
5804 static int is_based_loc (const_rtx);
5805 static dw_loc_descr_ref mem_loc_descriptor (rtx, enum machine_mode mode,
5806                                             enum var_init_status);
5807 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
5808                                                enum var_init_status);
5809 static dw_loc_descr_ref loc_descriptor (rtx, enum var_init_status);
5810 static dw_loc_descr_ref loc_descriptor_from_tree_1 (tree, int);
5811 static dw_loc_descr_ref loc_descriptor_from_tree (tree);
5812 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
5813 static tree field_type (const_tree);
5814 static unsigned int simple_type_align_in_bits (const_tree);
5815 static unsigned int simple_decl_align_in_bits (const_tree);
5816 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
5817 static HOST_WIDE_INT field_byte_offset (const_tree);
5818 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
5819                                          dw_loc_descr_ref);
5820 static void add_data_member_location_attribute (dw_die_ref, tree);
5821 static void add_const_value_attribute (dw_die_ref, rtx);
5822 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
5823 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
5824 static void insert_float (const_rtx, unsigned char *);
5825 static rtx rtl_for_decl_location (tree);
5826 static void add_location_or_const_value_attribute (dw_die_ref, tree,
5827                                                    enum dwarf_attribute);
5828 static void tree_add_const_value_attribute (dw_die_ref, tree);
5829 static void add_name_attribute (dw_die_ref, const char *);
5830 static void add_comp_dir_attribute (dw_die_ref);
5831 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
5832 static void add_subscript_info (dw_die_ref, tree, bool);
5833 static void add_byte_size_attribute (dw_die_ref, tree);
5834 static void add_bit_offset_attribute (dw_die_ref, tree);
5835 static void add_bit_size_attribute (dw_die_ref, tree);
5836 static void add_prototyped_attribute (dw_die_ref, tree);
5837 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
5838 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
5839 static void add_src_coords_attributes (dw_die_ref, tree);
5840 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
5841 static void push_decl_scope (tree);
5842 static void pop_decl_scope (void);
5843 static dw_die_ref scope_die_for (tree, dw_die_ref);
5844 static inline int local_scope_p (dw_die_ref);
5845 static inline int class_scope_p (dw_die_ref);
5846 static inline int class_or_namespace_scope_p (dw_die_ref);
5847 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
5848 static void add_calling_convention_attribute (dw_die_ref, tree);
5849 static const char *type_tag (const_tree);
5850 static tree member_declared_type (const_tree);
5851 #if 0
5852 static const char *decl_start_label (tree);
5853 #endif
5854 static void gen_array_type_die (tree, dw_die_ref);
5855 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
5856 #if 0
5857 static void gen_entry_point_die (tree, dw_die_ref);
5858 #endif
5859 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
5860 static dw_die_ref gen_formal_parameter_die (tree, tree, dw_die_ref);
5861 static void gen_unspecified_parameters_die (tree, dw_die_ref);
5862 static void gen_formal_types_die (tree, dw_die_ref);
5863 static void gen_subprogram_die (tree, dw_die_ref);
5864 static void gen_variable_die (tree, tree, dw_die_ref);
5865 static void gen_const_die (tree, dw_die_ref);
5866 static void gen_label_die (tree, dw_die_ref);
5867 static void gen_lexical_block_die (tree, dw_die_ref, int);
5868 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
5869 static void gen_field_die (tree, dw_die_ref);
5870 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
5871 static dw_die_ref gen_compile_unit_die (const char *);
5872 static void gen_inheritance_die (tree, tree, dw_die_ref);
5873 static void gen_member_die (tree, dw_die_ref);
5874 static void gen_struct_or_union_type_die (tree, dw_die_ref,
5875                                                 enum debug_info_usage);
5876 static void gen_subroutine_type_die (tree, dw_die_ref);
5877 static void gen_typedef_die (tree, dw_die_ref);
5878 static void gen_type_die (tree, dw_die_ref);
5879 static void gen_block_die (tree, dw_die_ref, int);
5880 static void decls_for_scope (tree, dw_die_ref, int);
5881 static int is_redundant_typedef (const_tree);
5882 static void gen_namespace_die (tree, dw_die_ref);
5883 static void gen_decl_die (tree, tree, dw_die_ref);
5884 static dw_die_ref force_decl_die (tree);
5885 static dw_die_ref force_type_die (tree);
5886 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
5887 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
5888 static struct dwarf_file_data * lookup_filename (const char *);
5889 static void retry_incomplete_types (void);
5890 static void gen_type_die_for_member (tree, tree, dw_die_ref);
5891 static void splice_child_die (dw_die_ref, dw_die_ref);
5892 static int file_info_cmp (const void *, const void *);
5893 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
5894                                      const char *, const char *, unsigned);
5895 static void add_loc_descr_to_loc_list (dw_loc_list_ref *, dw_loc_descr_ref,
5896                                        const char *, const char *,
5897                                        const char *);
5898 static void output_loc_list (dw_loc_list_ref);
5899 static char *gen_internal_sym (const char *);
5900
5901 static void prune_unmark_dies (dw_die_ref);
5902 static void prune_unused_types_mark (dw_die_ref, int);
5903 static void prune_unused_types_walk (dw_die_ref);
5904 static void prune_unused_types_walk_attribs (dw_die_ref);
5905 static void prune_unused_types_prune (dw_die_ref);
5906 static void prune_unused_types (void);
5907 static int maybe_emit_file (struct dwarf_file_data *fd);
5908
5909 /* Section names used to hold DWARF debugging information.  */
5910 #ifndef DEBUG_INFO_SECTION
5911 #define DEBUG_INFO_SECTION      ".debug_info"
5912 #endif
5913 #ifndef DEBUG_ABBREV_SECTION
5914 #define DEBUG_ABBREV_SECTION    ".debug_abbrev"
5915 #endif
5916 #ifndef DEBUG_ARANGES_SECTION
5917 #define DEBUG_ARANGES_SECTION   ".debug_aranges"
5918 #endif
5919 #ifndef DEBUG_MACINFO_SECTION
5920 #define DEBUG_MACINFO_SECTION   ".debug_macinfo"
5921 #endif
5922 #ifndef DEBUG_LINE_SECTION
5923 #define DEBUG_LINE_SECTION      ".debug_line"
5924 #endif
5925 #ifndef DEBUG_LOC_SECTION
5926 #define DEBUG_LOC_SECTION       ".debug_loc"
5927 #endif
5928 #ifndef DEBUG_PUBNAMES_SECTION
5929 #define DEBUG_PUBNAMES_SECTION  ".debug_pubnames"
5930 #endif
5931 #ifndef DEBUG_PUBTYPES_SECTION
5932 #define DEBUG_PUBTYPES_SECTION  ".debug_pubtypes"
5933 #endif
5934 #ifndef DEBUG_STR_SECTION
5935 #define DEBUG_STR_SECTION       ".debug_str"
5936 #endif
5937 #ifndef DEBUG_RANGES_SECTION
5938 #define DEBUG_RANGES_SECTION    ".debug_ranges"
5939 #endif
5940
5941 /* Standard ELF section names for compiled code and data.  */
5942 #ifndef TEXT_SECTION_NAME
5943 #define TEXT_SECTION_NAME       ".text"
5944 #endif
5945
5946 /* Section flags for .debug_str section.  */
5947 #define DEBUG_STR_SECTION_FLAGS \
5948   (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings               \
5949    ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1        \
5950    : SECTION_DEBUG)
5951
5952 /* Labels we insert at beginning sections we can reference instead of
5953    the section names themselves.  */
5954
5955 #ifndef TEXT_SECTION_LABEL
5956 #define TEXT_SECTION_LABEL              "Ltext"
5957 #endif
5958 #ifndef COLD_TEXT_SECTION_LABEL
5959 #define COLD_TEXT_SECTION_LABEL         "Ltext_cold"
5960 #endif
5961 #ifndef DEBUG_LINE_SECTION_LABEL
5962 #define DEBUG_LINE_SECTION_LABEL        "Ldebug_line"
5963 #endif
5964 #ifndef DEBUG_INFO_SECTION_LABEL
5965 #define DEBUG_INFO_SECTION_LABEL        "Ldebug_info"
5966 #endif
5967 #ifndef DEBUG_ABBREV_SECTION_LABEL
5968 #define DEBUG_ABBREV_SECTION_LABEL      "Ldebug_abbrev"
5969 #endif
5970 #ifndef DEBUG_LOC_SECTION_LABEL
5971 #define DEBUG_LOC_SECTION_LABEL         "Ldebug_loc"
5972 #endif
5973 #ifndef DEBUG_RANGES_SECTION_LABEL
5974 #define DEBUG_RANGES_SECTION_LABEL      "Ldebug_ranges"
5975 #endif
5976 #ifndef DEBUG_MACINFO_SECTION_LABEL
5977 #define DEBUG_MACINFO_SECTION_LABEL     "Ldebug_macinfo"
5978 #endif
5979
5980 /* Definitions of defaults for formats and names of various special
5981    (artificial) labels which may be generated within this file (when the -g
5982    options is used and DWARF2_DEBUGGING_INFO is in effect.
5983    If necessary, these may be overridden from within the tm.h file, but
5984    typically, overriding these defaults is unnecessary.  */
5985
5986 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
5987 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5988 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5989 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
5990 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5991 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5992 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5993 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5994 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5995 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
5996
5997 #ifndef TEXT_END_LABEL
5998 #define TEXT_END_LABEL          "Letext"
5999 #endif
6000 #ifndef COLD_END_LABEL
6001 #define COLD_END_LABEL          "Letext_cold"
6002 #endif
6003 #ifndef BLOCK_BEGIN_LABEL
6004 #define BLOCK_BEGIN_LABEL       "LBB"
6005 #endif
6006 #ifndef BLOCK_END_LABEL
6007 #define BLOCK_END_LABEL         "LBE"
6008 #endif
6009 #ifndef LINE_CODE_LABEL
6010 #define LINE_CODE_LABEL         "LM"
6011 #endif
6012 #ifndef SEPARATE_LINE_CODE_LABEL
6013 #define SEPARATE_LINE_CODE_LABEL        "LSM"
6014 #endif
6015
6016 \f
6017 /* We allow a language front-end to designate a function that is to be
6018    called to "demangle" any name before it is put into a DIE.  */
6019
6020 static const char *(*demangle_name_func) (const char *);
6021
6022 void
6023 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
6024 {
6025   demangle_name_func = func;
6026 }
6027
6028 /* Test if rtl node points to a pseudo register.  */
6029
6030 static inline int
6031 is_pseudo_reg (const_rtx rtl)
6032 {
6033   return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
6034           || (GET_CODE (rtl) == SUBREG
6035               && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
6036 }
6037
6038 /* Return a reference to a type, with its const and volatile qualifiers
6039    removed.  */
6040
6041 static inline tree
6042 type_main_variant (tree type)
6043 {
6044   type = TYPE_MAIN_VARIANT (type);
6045
6046   /* ??? There really should be only one main variant among any group of
6047      variants of a given type (and all of the MAIN_VARIANT values for all
6048      members of the group should point to that one type) but sometimes the C
6049      front-end messes this up for array types, so we work around that bug
6050      here.  */
6051   if (TREE_CODE (type) == ARRAY_TYPE)
6052     while (type != TYPE_MAIN_VARIANT (type))
6053       type = TYPE_MAIN_VARIANT (type);
6054
6055   return type;
6056 }
6057
6058 /* Return nonzero if the given type node represents a tagged type.  */
6059
6060 static inline int
6061 is_tagged_type (const_tree type)
6062 {
6063   enum tree_code code = TREE_CODE (type);
6064
6065   return (code == RECORD_TYPE || code == UNION_TYPE
6066           || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
6067 }
6068
6069 /* Convert a DIE tag into its string name.  */
6070
6071 static const char *
6072 dwarf_tag_name (unsigned int tag)
6073 {
6074   switch (tag)
6075     {
6076     case DW_TAG_padding:
6077       return "DW_TAG_padding";
6078     case DW_TAG_array_type:
6079       return "DW_TAG_array_type";
6080     case DW_TAG_class_type:
6081       return "DW_TAG_class_type";
6082     case DW_TAG_entry_point:
6083       return "DW_TAG_entry_point";
6084     case DW_TAG_enumeration_type:
6085       return "DW_TAG_enumeration_type";
6086     case DW_TAG_formal_parameter:
6087       return "DW_TAG_formal_parameter";
6088     case DW_TAG_imported_declaration:
6089       return "DW_TAG_imported_declaration";
6090     case DW_TAG_label:
6091       return "DW_TAG_label";
6092     case DW_TAG_lexical_block:
6093       return "DW_TAG_lexical_block";
6094     case DW_TAG_member:
6095       return "DW_TAG_member";
6096     case DW_TAG_pointer_type:
6097       return "DW_TAG_pointer_type";
6098     case DW_TAG_reference_type:
6099       return "DW_TAG_reference_type";
6100     case DW_TAG_compile_unit:
6101       return "DW_TAG_compile_unit";
6102     case DW_TAG_string_type:
6103       return "DW_TAG_string_type";
6104     case DW_TAG_structure_type:
6105       return "DW_TAG_structure_type";
6106     case DW_TAG_subroutine_type:
6107       return "DW_TAG_subroutine_type";
6108     case DW_TAG_typedef:
6109       return "DW_TAG_typedef";
6110     case DW_TAG_union_type:
6111       return "DW_TAG_union_type";
6112     case DW_TAG_unspecified_parameters:
6113       return "DW_TAG_unspecified_parameters";
6114     case DW_TAG_variant:
6115       return "DW_TAG_variant";
6116     case DW_TAG_common_block:
6117       return "DW_TAG_common_block";
6118     case DW_TAG_common_inclusion:
6119       return "DW_TAG_common_inclusion";
6120     case DW_TAG_inheritance:
6121       return "DW_TAG_inheritance";
6122     case DW_TAG_inlined_subroutine:
6123       return "DW_TAG_inlined_subroutine";
6124     case DW_TAG_module:
6125       return "DW_TAG_module";
6126     case DW_TAG_ptr_to_member_type:
6127       return "DW_TAG_ptr_to_member_type";
6128     case DW_TAG_set_type:
6129       return "DW_TAG_set_type";
6130     case DW_TAG_subrange_type:
6131       return "DW_TAG_subrange_type";
6132     case DW_TAG_with_stmt:
6133       return "DW_TAG_with_stmt";
6134     case DW_TAG_access_declaration:
6135       return "DW_TAG_access_declaration";
6136     case DW_TAG_base_type:
6137       return "DW_TAG_base_type";
6138     case DW_TAG_catch_block:
6139       return "DW_TAG_catch_block";
6140     case DW_TAG_const_type:
6141       return "DW_TAG_const_type";
6142     case DW_TAG_constant:
6143       return "DW_TAG_constant";
6144     case DW_TAG_enumerator:
6145       return "DW_TAG_enumerator";
6146     case DW_TAG_file_type:
6147       return "DW_TAG_file_type";
6148     case DW_TAG_friend:
6149       return "DW_TAG_friend";
6150     case DW_TAG_namelist:
6151       return "DW_TAG_namelist";
6152     case DW_TAG_namelist_item:
6153       return "DW_TAG_namelist_item";
6154     case DW_TAG_packed_type:
6155       return "DW_TAG_packed_type";
6156     case DW_TAG_subprogram:
6157       return "DW_TAG_subprogram";
6158     case DW_TAG_template_type_param:
6159       return "DW_TAG_template_type_param";
6160     case DW_TAG_template_value_param:
6161       return "DW_TAG_template_value_param";
6162     case DW_TAG_thrown_type:
6163       return "DW_TAG_thrown_type";
6164     case DW_TAG_try_block:
6165       return "DW_TAG_try_block";
6166     case DW_TAG_variant_part:
6167       return "DW_TAG_variant_part";
6168     case DW_TAG_variable:
6169       return "DW_TAG_variable";
6170     case DW_TAG_volatile_type:
6171       return "DW_TAG_volatile_type";
6172     case DW_TAG_dwarf_procedure:
6173       return "DW_TAG_dwarf_procedure";
6174     case DW_TAG_restrict_type:
6175       return "DW_TAG_restrict_type";
6176     case DW_TAG_interface_type:
6177       return "DW_TAG_interface_type";
6178     case DW_TAG_namespace:
6179       return "DW_TAG_namespace";
6180     case DW_TAG_imported_module:
6181       return "DW_TAG_imported_module";
6182     case DW_TAG_unspecified_type:
6183       return "DW_TAG_unspecified_type";
6184     case DW_TAG_partial_unit:
6185       return "DW_TAG_partial_unit";
6186     case DW_TAG_imported_unit:
6187       return "DW_TAG_imported_unit";
6188     case DW_TAG_condition:
6189       return "DW_TAG_condition";
6190     case DW_TAG_shared_type:
6191       return "DW_TAG_shared_type";
6192     case DW_TAG_MIPS_loop:
6193       return "DW_TAG_MIPS_loop";
6194     case DW_TAG_format_label:
6195       return "DW_TAG_format_label";
6196     case DW_TAG_function_template:
6197       return "DW_TAG_function_template";
6198     case DW_TAG_class_template:
6199       return "DW_TAG_class_template";
6200     case DW_TAG_GNU_BINCL:
6201       return "DW_TAG_GNU_BINCL";
6202     case DW_TAG_GNU_EINCL:
6203       return "DW_TAG_GNU_EINCL";
6204     default:
6205       return "DW_TAG_<unknown>";
6206     }
6207 }
6208
6209 /* Convert a DWARF attribute code into its string name.  */
6210
6211 static const char *
6212 dwarf_attr_name (unsigned int attr)
6213 {
6214   switch (attr)
6215     {
6216     case DW_AT_sibling:
6217       return "DW_AT_sibling";
6218     case DW_AT_location:
6219       return "DW_AT_location";
6220     case DW_AT_name:
6221       return "DW_AT_name";
6222     case DW_AT_ordering:
6223       return "DW_AT_ordering";
6224     case DW_AT_subscr_data:
6225       return "DW_AT_subscr_data";
6226     case DW_AT_byte_size:
6227       return "DW_AT_byte_size";
6228     case DW_AT_bit_offset:
6229       return "DW_AT_bit_offset";
6230     case DW_AT_bit_size:
6231       return "DW_AT_bit_size";
6232     case DW_AT_element_list:
6233       return "DW_AT_element_list";
6234     case DW_AT_stmt_list:
6235       return "DW_AT_stmt_list";
6236     case DW_AT_low_pc:
6237       return "DW_AT_low_pc";
6238     case DW_AT_high_pc:
6239       return "DW_AT_high_pc";
6240     case DW_AT_language:
6241       return "DW_AT_language";
6242     case DW_AT_member:
6243       return "DW_AT_member";
6244     case DW_AT_discr:
6245       return "DW_AT_discr";
6246     case DW_AT_discr_value:
6247       return "DW_AT_discr_value";
6248     case DW_AT_visibility:
6249       return "DW_AT_visibility";
6250     case DW_AT_import:
6251       return "DW_AT_import";
6252     case DW_AT_string_length:
6253       return "DW_AT_string_length";
6254     case DW_AT_common_reference:
6255       return "DW_AT_common_reference";
6256     case DW_AT_comp_dir:
6257       return "DW_AT_comp_dir";
6258     case DW_AT_const_value:
6259       return "DW_AT_const_value";
6260     case DW_AT_containing_type:
6261       return "DW_AT_containing_type";
6262     case DW_AT_default_value:
6263       return "DW_AT_default_value";
6264     case DW_AT_inline:
6265       return "DW_AT_inline";
6266     case DW_AT_is_optional:
6267       return "DW_AT_is_optional";
6268     case DW_AT_lower_bound:
6269       return "DW_AT_lower_bound";
6270     case DW_AT_producer:
6271       return "DW_AT_producer";
6272     case DW_AT_prototyped:
6273       return "DW_AT_prototyped";
6274     case DW_AT_return_addr:
6275       return "DW_AT_return_addr";
6276     case DW_AT_start_scope:
6277       return "DW_AT_start_scope";
6278     case DW_AT_bit_stride:
6279       return "DW_AT_bit_stride";
6280     case DW_AT_upper_bound:
6281       return "DW_AT_upper_bound";
6282     case DW_AT_abstract_origin:
6283       return "DW_AT_abstract_origin";
6284     case DW_AT_accessibility:
6285       return "DW_AT_accessibility";
6286     case DW_AT_address_class:
6287       return "DW_AT_address_class";
6288     case DW_AT_artificial:
6289       return "DW_AT_artificial";
6290     case DW_AT_base_types:
6291       return "DW_AT_base_types";
6292     case DW_AT_calling_convention:
6293       return "DW_AT_calling_convention";
6294     case DW_AT_count:
6295       return "DW_AT_count";
6296     case DW_AT_data_member_location:
6297       return "DW_AT_data_member_location";
6298     case DW_AT_decl_column:
6299       return "DW_AT_decl_column";
6300     case DW_AT_decl_file:
6301       return "DW_AT_decl_file";
6302     case DW_AT_decl_line:
6303       return "DW_AT_decl_line";
6304     case DW_AT_declaration:
6305       return "DW_AT_declaration";
6306     case DW_AT_discr_list:
6307       return "DW_AT_discr_list";
6308     case DW_AT_encoding:
6309       return "DW_AT_encoding";
6310     case DW_AT_external:
6311       return "DW_AT_external";
6312     case DW_AT_explicit:
6313       return "DW_AT_explicit";
6314     case DW_AT_frame_base:
6315       return "DW_AT_frame_base";
6316     case DW_AT_friend:
6317       return "DW_AT_friend";
6318     case DW_AT_identifier_case:
6319       return "DW_AT_identifier_case";
6320     case DW_AT_macro_info:
6321       return "DW_AT_macro_info";
6322     case DW_AT_namelist_items:
6323       return "DW_AT_namelist_items";
6324     case DW_AT_priority:
6325       return "DW_AT_priority";
6326     case DW_AT_segment:
6327       return "DW_AT_segment";
6328     case DW_AT_specification:
6329       return "DW_AT_specification";
6330     case DW_AT_static_link:
6331       return "DW_AT_static_link";
6332     case DW_AT_type:
6333       return "DW_AT_type";
6334     case DW_AT_use_location:
6335       return "DW_AT_use_location";
6336     case DW_AT_variable_parameter:
6337       return "DW_AT_variable_parameter";
6338     case DW_AT_virtuality:
6339       return "DW_AT_virtuality";
6340     case DW_AT_vtable_elem_location:
6341       return "DW_AT_vtable_elem_location";
6342
6343     case DW_AT_allocated:
6344       return "DW_AT_allocated";
6345     case DW_AT_associated:
6346       return "DW_AT_associated";
6347     case DW_AT_data_location:
6348       return "DW_AT_data_location";
6349     case DW_AT_byte_stride:
6350       return "DW_AT_byte_stride";
6351     case DW_AT_entry_pc:
6352       return "DW_AT_entry_pc";
6353     case DW_AT_use_UTF8:
6354       return "DW_AT_use_UTF8";
6355     case DW_AT_extension:
6356       return "DW_AT_extension";
6357     case DW_AT_ranges:
6358       return "DW_AT_ranges";
6359     case DW_AT_trampoline:
6360       return "DW_AT_trampoline";
6361     case DW_AT_call_column:
6362       return "DW_AT_call_column";
6363     case DW_AT_call_file:
6364       return "DW_AT_call_file";
6365     case DW_AT_call_line:
6366       return "DW_AT_call_line";
6367
6368     case DW_AT_MIPS_fde:
6369       return "DW_AT_MIPS_fde";
6370     case DW_AT_MIPS_loop_begin:
6371       return "DW_AT_MIPS_loop_begin";
6372     case DW_AT_MIPS_tail_loop_begin:
6373       return "DW_AT_MIPS_tail_loop_begin";
6374     case DW_AT_MIPS_epilog_begin:
6375       return "DW_AT_MIPS_epilog_begin";
6376     case DW_AT_MIPS_loop_unroll_factor:
6377       return "DW_AT_MIPS_loop_unroll_factor";
6378     case DW_AT_MIPS_software_pipeline_depth:
6379       return "DW_AT_MIPS_software_pipeline_depth";
6380     case DW_AT_MIPS_linkage_name:
6381       return "DW_AT_MIPS_linkage_name";
6382     case DW_AT_MIPS_stride:
6383       return "DW_AT_MIPS_stride";
6384     case DW_AT_MIPS_abstract_name:
6385       return "DW_AT_MIPS_abstract_name";
6386     case DW_AT_MIPS_clone_origin:
6387       return "DW_AT_MIPS_clone_origin";
6388     case DW_AT_MIPS_has_inlines:
6389       return "DW_AT_MIPS_has_inlines";
6390
6391     case DW_AT_sf_names:
6392       return "DW_AT_sf_names";
6393     case DW_AT_src_info:
6394       return "DW_AT_src_info";
6395     case DW_AT_mac_info:
6396       return "DW_AT_mac_info";
6397     case DW_AT_src_coords:
6398       return "DW_AT_src_coords";
6399     case DW_AT_body_begin:
6400       return "DW_AT_body_begin";
6401     case DW_AT_body_end:
6402       return "DW_AT_body_end";
6403     case DW_AT_GNU_vector:
6404       return "DW_AT_GNU_vector";
6405
6406     case DW_AT_VMS_rtnbeg_pd_address:
6407       return "DW_AT_VMS_rtnbeg_pd_address";
6408
6409     default:
6410       return "DW_AT_<unknown>";
6411     }
6412 }
6413
6414 /* Convert a DWARF value form code into its string name.  */
6415
6416 static const char *
6417 dwarf_form_name (unsigned int form)
6418 {
6419   switch (form)
6420     {
6421     case DW_FORM_addr:
6422       return "DW_FORM_addr";
6423     case DW_FORM_block2:
6424       return "DW_FORM_block2";
6425     case DW_FORM_block4:
6426       return "DW_FORM_block4";
6427     case DW_FORM_data2:
6428       return "DW_FORM_data2";
6429     case DW_FORM_data4:
6430       return "DW_FORM_data4";
6431     case DW_FORM_data8:
6432       return "DW_FORM_data8";
6433     case DW_FORM_string:
6434       return "DW_FORM_string";
6435     case DW_FORM_block:
6436       return "DW_FORM_block";
6437     case DW_FORM_block1:
6438       return "DW_FORM_block1";
6439     case DW_FORM_data1:
6440       return "DW_FORM_data1";
6441     case DW_FORM_flag:
6442       return "DW_FORM_flag";
6443     case DW_FORM_sdata:
6444       return "DW_FORM_sdata";
6445     case DW_FORM_strp:
6446       return "DW_FORM_strp";
6447     case DW_FORM_udata:
6448       return "DW_FORM_udata";
6449     case DW_FORM_ref_addr:
6450       return "DW_FORM_ref_addr";
6451     case DW_FORM_ref1:
6452       return "DW_FORM_ref1";
6453     case DW_FORM_ref2:
6454       return "DW_FORM_ref2";
6455     case DW_FORM_ref4:
6456       return "DW_FORM_ref4";
6457     case DW_FORM_ref8:
6458       return "DW_FORM_ref8";
6459     case DW_FORM_ref_udata:
6460       return "DW_FORM_ref_udata";
6461     case DW_FORM_indirect:
6462       return "DW_FORM_indirect";
6463     default:
6464       return "DW_FORM_<unknown>";
6465     }
6466 }
6467 \f
6468 /* Determine the "ultimate origin" of a decl.  The decl may be an inlined
6469    instance of an inlined instance of a decl which is local to an inline
6470    function, so we have to trace all of the way back through the origin chain
6471    to find out what sort of node actually served as the original seed for the
6472    given block.  */
6473
6474 static tree
6475 decl_ultimate_origin (const_tree decl)
6476 {
6477   if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
6478     return NULL_TREE;
6479
6480   /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
6481      nodes in the function to point to themselves; ignore that if
6482      we're trying to output the abstract instance of this function.  */
6483   if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
6484     return NULL_TREE;
6485
6486   /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
6487      most distant ancestor, this should never happen.  */
6488   gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
6489
6490   return DECL_ABSTRACT_ORIGIN (decl);
6491 }
6492
6493 /* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
6494    of a virtual function may refer to a base class, so we check the 'this'
6495    parameter.  */
6496
6497 static tree
6498 decl_class_context (tree decl)
6499 {
6500   tree context = NULL_TREE;
6501
6502   if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
6503     context = DECL_CONTEXT (decl);
6504   else
6505     context = TYPE_MAIN_VARIANT
6506       (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
6507
6508   if (context && !TYPE_P (context))
6509     context = NULL_TREE;
6510
6511   return context;
6512 }
6513 \f
6514 /* Add an attribute/value pair to a DIE.  */
6515
6516 static inline void
6517 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
6518 {
6519   /* Maybe this should be an assert?  */
6520   if (die == NULL)
6521     return;
6522
6523   if (die->die_attr == NULL)
6524     die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
6525   VEC_safe_push (dw_attr_node, gc, die->die_attr, attr);
6526 }
6527
6528 static inline enum dw_val_class
6529 AT_class (dw_attr_ref a)
6530 {
6531   return a->dw_attr_val.val_class;
6532 }
6533
6534 /* Add a flag value attribute to a DIE.  */
6535
6536 static inline void
6537 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
6538 {
6539   dw_attr_node attr;
6540
6541   attr.dw_attr = attr_kind;
6542   attr.dw_attr_val.val_class = dw_val_class_flag;
6543   attr.dw_attr_val.v.val_flag = flag;
6544   add_dwarf_attr (die, &attr);
6545 }
6546
6547 static inline unsigned
6548 AT_flag (dw_attr_ref a)
6549 {
6550   gcc_assert (a && AT_class (a) == dw_val_class_flag);
6551   return a->dw_attr_val.v.val_flag;
6552 }
6553
6554 /* Add a signed integer attribute value to a DIE.  */
6555
6556 static inline void
6557 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
6558 {
6559   dw_attr_node attr;
6560
6561   attr.dw_attr = attr_kind;
6562   attr.dw_attr_val.val_class = dw_val_class_const;
6563   attr.dw_attr_val.v.val_int = int_val;
6564   add_dwarf_attr (die, &attr);
6565 }
6566
6567 static inline HOST_WIDE_INT
6568 AT_int (dw_attr_ref a)
6569 {
6570   gcc_assert (a && AT_class (a) == dw_val_class_const);
6571   return a->dw_attr_val.v.val_int;
6572 }
6573
6574 /* Add an unsigned integer attribute value to a DIE.  */
6575
6576 static inline void
6577 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
6578                  unsigned HOST_WIDE_INT unsigned_val)
6579 {
6580   dw_attr_node attr;
6581
6582   attr.dw_attr = attr_kind;
6583   attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
6584   attr.dw_attr_val.v.val_unsigned = unsigned_val;
6585   add_dwarf_attr (die, &attr);
6586 }
6587
6588 static inline unsigned HOST_WIDE_INT
6589 AT_unsigned (dw_attr_ref a)
6590 {
6591   gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
6592   return a->dw_attr_val.v.val_unsigned;
6593 }
6594
6595 /* Add an unsigned double integer attribute value to a DIE.  */
6596
6597 static inline void
6598 add_AT_long_long (dw_die_ref die, enum dwarf_attribute attr_kind,
6599                   long unsigned int val_hi, long unsigned int val_low)
6600 {
6601   dw_attr_node attr;
6602
6603   attr.dw_attr = attr_kind;
6604   attr.dw_attr_val.val_class = dw_val_class_long_long;
6605   attr.dw_attr_val.v.val_long_long.hi = val_hi;
6606   attr.dw_attr_val.v.val_long_long.low = val_low;
6607   add_dwarf_attr (die, &attr);
6608 }
6609
6610 /* Add a floating point attribute value to a DIE and return it.  */
6611
6612 static inline void
6613 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
6614             unsigned int length, unsigned int elt_size, unsigned char *array)
6615 {
6616   dw_attr_node attr;
6617
6618   attr.dw_attr = attr_kind;
6619   attr.dw_attr_val.val_class = dw_val_class_vec;
6620   attr.dw_attr_val.v.val_vec.length = length;
6621   attr.dw_attr_val.v.val_vec.elt_size = elt_size;
6622   attr.dw_attr_val.v.val_vec.array = array;
6623   add_dwarf_attr (die, &attr);
6624 }
6625
6626 /* Hash and equality functions for debug_str_hash.  */
6627
6628 static hashval_t
6629 debug_str_do_hash (const void *x)
6630 {
6631   return htab_hash_string (((const struct indirect_string_node *)x)->str);
6632 }
6633
6634 static int
6635 debug_str_eq (const void *x1, const void *x2)
6636 {
6637   return strcmp ((((const struct indirect_string_node *)x1)->str),
6638                  (const char *)x2) == 0;
6639 }
6640
6641 static struct indirect_string_node *
6642 find_AT_string (const char *str)
6643 {
6644   struct indirect_string_node *node;
6645   void **slot;
6646
6647   if (! debug_str_hash)
6648     debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
6649                                       debug_str_eq, NULL);
6650
6651   slot = htab_find_slot_with_hash (debug_str_hash, str,
6652                                    htab_hash_string (str), INSERT);
6653   if (*slot == NULL)
6654     {
6655       node = (struct indirect_string_node *)
6656                ggc_alloc_cleared (sizeof (struct indirect_string_node));
6657       node->str = ggc_strdup (str);
6658       *slot = node;
6659     }
6660   else
6661     node = (struct indirect_string_node *) *slot;
6662
6663   node->refcount++;
6664   return node;
6665 }
6666
6667 /* Add a string attribute value to a DIE.  */
6668
6669 static inline void
6670 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
6671 {
6672   dw_attr_node attr;
6673   struct indirect_string_node *node;
6674
6675   node = find_AT_string (str);
6676
6677   attr.dw_attr = attr_kind;
6678   attr.dw_attr_val.val_class = dw_val_class_str;
6679   attr.dw_attr_val.v.val_str = node;
6680   add_dwarf_attr (die, &attr);
6681 }
6682
6683 static inline const char *
6684 AT_string (dw_attr_ref a)
6685 {
6686   gcc_assert (a && AT_class (a) == dw_val_class_str);
6687   return a->dw_attr_val.v.val_str->str;
6688 }
6689
6690 /* Find out whether a string should be output inline in DIE
6691    or out-of-line in .debug_str section.  */
6692
6693 static enum dwarf_form
6694 AT_string_form (dw_attr_ref a)
6695 {
6696   struct indirect_string_node *node;
6697   unsigned int len;
6698   char label[32];
6699
6700   gcc_assert (a && AT_class (a) == dw_val_class_str);
6701
6702   node = a->dw_attr_val.v.val_str;
6703   if (node->form)
6704     return node->form;
6705
6706   len = strlen (node->str) + 1;
6707
6708   /* If the string is shorter or equal to the size of the reference, it is
6709      always better to put it inline.  */
6710   if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
6711     return node->form = DW_FORM_string;
6712
6713   /* If we cannot expect the linker to merge strings in .debug_str
6714      section, only put it into .debug_str if it is worth even in this
6715      single module.  */
6716   if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
6717       || ((debug_str_section->common.flags & SECTION_MERGE) == 0
6718       && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
6719     return node->form = DW_FORM_string;
6720
6721   ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
6722   ++dw2_string_counter;
6723   node->label = xstrdup (label);
6724
6725   return node->form = DW_FORM_strp;
6726 }
6727
6728 /* Add a DIE reference attribute value to a DIE.  */
6729
6730 static inline void
6731 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
6732 {
6733   dw_attr_node attr;
6734
6735   attr.dw_attr = attr_kind;
6736   attr.dw_attr_val.val_class = dw_val_class_die_ref;
6737   attr.dw_attr_val.v.val_die_ref.die = targ_die;
6738   attr.dw_attr_val.v.val_die_ref.external = 0;
6739   add_dwarf_attr (die, &attr);
6740 }
6741
6742 /* Add an AT_specification attribute to a DIE, and also make the back
6743    pointer from the specification to the definition.  */
6744
6745 static inline void
6746 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
6747 {
6748   add_AT_die_ref (die, DW_AT_specification, targ_die);
6749   gcc_assert (!targ_die->die_definition);
6750   targ_die->die_definition = die;
6751 }
6752
6753 static inline dw_die_ref
6754 AT_ref (dw_attr_ref a)
6755 {
6756   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
6757   return a->dw_attr_val.v.val_die_ref.die;
6758 }
6759
6760 static inline int
6761 AT_ref_external (dw_attr_ref a)
6762 {
6763   if (a && AT_class (a) == dw_val_class_die_ref)
6764     return a->dw_attr_val.v.val_die_ref.external;
6765
6766   return 0;
6767 }
6768
6769 static inline void
6770 set_AT_ref_external (dw_attr_ref a, int i)
6771 {
6772   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
6773   a->dw_attr_val.v.val_die_ref.external = i;
6774 }
6775
6776 /* Add an FDE reference attribute value to a DIE.  */
6777
6778 static inline void
6779 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
6780 {
6781   dw_attr_node attr;
6782
6783   attr.dw_attr = attr_kind;
6784   attr.dw_attr_val.val_class = dw_val_class_fde_ref;
6785   attr.dw_attr_val.v.val_fde_index = targ_fde;
6786   add_dwarf_attr (die, &attr);
6787 }
6788
6789 /* Add a location description attribute value to a DIE.  */
6790
6791 static inline void
6792 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
6793 {
6794   dw_attr_node attr;
6795
6796   attr.dw_attr = attr_kind;
6797   attr.dw_attr_val.val_class = dw_val_class_loc;
6798   attr.dw_attr_val.v.val_loc = loc;
6799   add_dwarf_attr (die, &attr);
6800 }
6801
6802 static inline dw_loc_descr_ref
6803 AT_loc (dw_attr_ref a)
6804 {
6805   gcc_assert (a && AT_class (a) == dw_val_class_loc);
6806   return a->dw_attr_val.v.val_loc;
6807 }
6808
6809 static inline void
6810 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
6811 {
6812   dw_attr_node attr;
6813
6814   attr.dw_attr = attr_kind;
6815   attr.dw_attr_val.val_class = dw_val_class_loc_list;
6816   attr.dw_attr_val.v.val_loc_list = loc_list;
6817   add_dwarf_attr (die, &attr);
6818   have_location_lists = true;
6819 }
6820
6821 static inline dw_loc_list_ref
6822 AT_loc_list (dw_attr_ref a)
6823 {
6824   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
6825   return a->dw_attr_val.v.val_loc_list;
6826 }
6827
6828 /* Add an address constant attribute value to a DIE.  */
6829
6830 static inline void
6831 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
6832 {
6833   dw_attr_node attr;
6834
6835   attr.dw_attr = attr_kind;
6836   attr.dw_attr_val.val_class = dw_val_class_addr;
6837   attr.dw_attr_val.v.val_addr = addr;
6838   add_dwarf_attr (die, &attr);
6839 }
6840
6841 /* Get the RTX from to an address DIE attribute.  */
6842
6843 static inline rtx
6844 AT_addr (dw_attr_ref a)
6845 {
6846   gcc_assert (a && AT_class (a) == dw_val_class_addr);
6847   return a->dw_attr_val.v.val_addr;
6848 }
6849
6850 /* Add a file attribute value to a DIE.  */
6851
6852 static inline void
6853 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
6854              struct dwarf_file_data *fd)
6855 {
6856   dw_attr_node attr;
6857
6858   attr.dw_attr = attr_kind;
6859   attr.dw_attr_val.val_class = dw_val_class_file;
6860   attr.dw_attr_val.v.val_file = fd;
6861   add_dwarf_attr (die, &attr);
6862 }
6863
6864 /* Get the dwarf_file_data from a file DIE attribute.  */
6865
6866 static inline struct dwarf_file_data *
6867 AT_file (dw_attr_ref a)
6868 {
6869   gcc_assert (a && AT_class (a) == dw_val_class_file);
6870   return a->dw_attr_val.v.val_file;
6871 }
6872
6873 /* Add a label identifier attribute value to a DIE.  */
6874
6875 static inline void
6876 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
6877 {
6878   dw_attr_node attr;
6879
6880   attr.dw_attr = attr_kind;
6881   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
6882   attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
6883   add_dwarf_attr (die, &attr);
6884 }
6885
6886 /* Add a section offset attribute value to a DIE, an offset into the
6887    debug_line section.  */
6888
6889 static inline void
6890 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
6891                 const char *label)
6892 {
6893   dw_attr_node attr;
6894
6895   attr.dw_attr = attr_kind;
6896   attr.dw_attr_val.val_class = dw_val_class_lineptr;
6897   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
6898   add_dwarf_attr (die, &attr);
6899 }
6900
6901 /* Add a section offset attribute value to a DIE, an offset into the
6902    debug_macinfo section.  */
6903
6904 static inline void
6905 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
6906                const char *label)
6907 {
6908   dw_attr_node attr;
6909
6910   attr.dw_attr = attr_kind;
6911   attr.dw_attr_val.val_class = dw_val_class_macptr;
6912   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
6913   add_dwarf_attr (die, &attr);
6914 }
6915
6916 /* Add an offset attribute value to a DIE.  */
6917
6918 static inline void
6919 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
6920                unsigned HOST_WIDE_INT offset)
6921 {
6922   dw_attr_node attr;
6923
6924   attr.dw_attr = attr_kind;
6925   attr.dw_attr_val.val_class = dw_val_class_offset;
6926   attr.dw_attr_val.v.val_offset = offset;
6927   add_dwarf_attr (die, &attr);
6928 }
6929
6930 /* Add an range_list attribute value to a DIE.  */
6931
6932 static void
6933 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
6934                    long unsigned int offset)
6935 {
6936   dw_attr_node attr;
6937
6938   attr.dw_attr = attr_kind;
6939   attr.dw_attr_val.val_class = dw_val_class_range_list;
6940   attr.dw_attr_val.v.val_offset = offset;
6941   add_dwarf_attr (die, &attr);
6942 }
6943
6944 static inline const char *
6945 AT_lbl (dw_attr_ref a)
6946 {
6947   gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
6948                     || AT_class (a) == dw_val_class_lineptr
6949                     || AT_class (a) == dw_val_class_macptr));
6950   return a->dw_attr_val.v.val_lbl_id;
6951 }
6952
6953 /* Get the attribute of type attr_kind.  */
6954
6955 static dw_attr_ref
6956 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
6957 {
6958   dw_attr_ref a;
6959   unsigned ix;
6960   dw_die_ref spec = NULL;
6961
6962   if (! die)
6963     return NULL;
6964
6965   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6966     if (a->dw_attr == attr_kind)
6967       return a;
6968     else if (a->dw_attr == DW_AT_specification
6969              || a->dw_attr == DW_AT_abstract_origin)
6970       spec = AT_ref (a);
6971
6972   if (spec)
6973     return get_AT (spec, attr_kind);
6974
6975   return NULL;
6976 }
6977
6978 /* Return the "low pc" attribute value, typically associated with a subprogram
6979    DIE.  Return null if the "low pc" attribute is either not present, or if it
6980    cannot be represented as an assembler label identifier.  */
6981
6982 static inline const char *
6983 get_AT_low_pc (dw_die_ref die)
6984 {
6985   dw_attr_ref a = get_AT (die, DW_AT_low_pc);
6986
6987   return a ? AT_lbl (a) : NULL;
6988 }
6989
6990 /* Return the "high pc" attribute value, typically associated with a subprogram
6991    DIE.  Return null if the "high pc" attribute is either not present, or if it
6992    cannot be represented as an assembler label identifier.  */
6993
6994 static inline const char *
6995 get_AT_hi_pc (dw_die_ref die)
6996 {
6997   dw_attr_ref a = get_AT (die, DW_AT_high_pc);
6998
6999   return a ? AT_lbl (a) : NULL;
7000 }
7001
7002 /* Return the value of the string attribute designated by ATTR_KIND, or
7003    NULL if it is not present.  */
7004
7005 static inline const char *
7006 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
7007 {
7008   dw_attr_ref a = get_AT (die, attr_kind);
7009
7010   return a ? AT_string (a) : NULL;
7011 }
7012
7013 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
7014    if it is not present.  */
7015
7016 static inline int
7017 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
7018 {
7019   dw_attr_ref a = get_AT (die, attr_kind);
7020
7021   return a ? AT_flag (a) : 0;
7022 }
7023
7024 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
7025    if it is not present.  */
7026
7027 static inline unsigned
7028 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
7029 {
7030   dw_attr_ref a = get_AT (die, attr_kind);
7031
7032   return a ? AT_unsigned (a) : 0;
7033 }
7034
7035 static inline dw_die_ref
7036 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
7037 {
7038   dw_attr_ref a = get_AT (die, attr_kind);
7039
7040   return a ? AT_ref (a) : NULL;
7041 }
7042
7043 static inline struct dwarf_file_data *
7044 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
7045 {
7046   dw_attr_ref a = get_AT (die, attr_kind);
7047
7048   return a ? AT_file (a) : NULL;
7049 }
7050
7051 /* Return TRUE if the language is C or C++.  */
7052
7053 static inline bool
7054 is_c_family (void)
7055 {
7056   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
7057
7058   return (lang == DW_LANG_C || lang == DW_LANG_C89 || lang == DW_LANG_ObjC
7059           || lang == DW_LANG_C99
7060           || lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus);
7061 }
7062
7063 /* Return TRUE if the language is C++.  */
7064
7065 static inline bool
7066 is_cxx (void)
7067 {
7068   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
7069
7070   return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
7071 }
7072
7073 /* Return TRUE if the language is Fortran.  */
7074
7075 static inline bool
7076 is_fortran (void)
7077 {
7078   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
7079
7080   return (lang == DW_LANG_Fortran77
7081           || lang == DW_LANG_Fortran90
7082           || lang == DW_LANG_Fortran95);
7083 }
7084
7085 /* Return TRUE if the language is Java.  */
7086
7087 static inline bool
7088 is_java (void)
7089 {
7090   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
7091
7092   return lang == DW_LANG_Java;
7093 }
7094
7095 /* Return TRUE if the language is Ada.  */
7096
7097 static inline bool
7098 is_ada (void)
7099 {
7100   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
7101
7102   return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
7103 }
7104
7105 /* Remove the specified attribute if present.  */
7106
7107 static void
7108 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
7109 {
7110   dw_attr_ref a;
7111   unsigned ix;
7112
7113   if (! die)
7114     return;
7115
7116   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7117     if (a->dw_attr == attr_kind)
7118       {
7119         if (AT_class (a) == dw_val_class_str)
7120           if (a->dw_attr_val.v.val_str->refcount)
7121             a->dw_attr_val.v.val_str->refcount--;
7122
7123         /* VEC_ordered_remove should help reduce the number of abbrevs
7124            that are needed.  */
7125         VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
7126         return;
7127       }
7128 }
7129
7130 /* Remove CHILD from its parent.  PREV must have the property that
7131    PREV->DIE_SIB == CHILD.  Does not alter CHILD.  */
7132
7133 static void
7134 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
7135 {
7136   gcc_assert (child->die_parent == prev->die_parent);
7137   gcc_assert (prev->die_sib == child);
7138   if (prev == child)
7139     {
7140       gcc_assert (child->die_parent->die_child == child);
7141       prev = NULL;
7142     }
7143   else
7144     prev->die_sib = child->die_sib;
7145   if (child->die_parent->die_child == child)
7146     child->die_parent->die_child = prev;
7147 }
7148
7149 /* Remove child DIE whose die_tag is TAG.  Do nothing if no child
7150    matches TAG.  */
7151
7152 static void
7153 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
7154 {
7155   dw_die_ref c;
7156
7157   c = die->die_child;
7158   if (c) do {
7159     dw_die_ref prev = c;
7160     c = c->die_sib;
7161     while (c->die_tag == tag)
7162       {
7163         remove_child_with_prev (c, prev);
7164         /* Might have removed every child.  */
7165         if (c == c->die_sib)
7166           return;
7167         c = c->die_sib;
7168       }
7169   } while (c != die->die_child);
7170 }
7171
7172 /* Add a CHILD_DIE as the last child of DIE.  */
7173
7174 static void
7175 add_child_die (dw_die_ref die, dw_die_ref child_die)
7176 {
7177   /* FIXME this should probably be an assert.  */
7178   if (! die || ! child_die)
7179     return;
7180   gcc_assert (die != child_die);
7181
7182   child_die->die_parent = die;
7183   if (die->die_child)
7184     {
7185       child_die->die_sib = die->die_child->die_sib;
7186       die->die_child->die_sib = child_die;
7187     }
7188   else
7189     child_die->die_sib = child_die;
7190   die->die_child = child_die;
7191 }
7192
7193 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
7194    is the specification, to the end of PARENT's list of children.
7195    This is done by removing and re-adding it.  */
7196
7197 static void
7198 splice_child_die (dw_die_ref parent, dw_die_ref child)
7199 {
7200   dw_die_ref p;
7201
7202   /* We want the declaration DIE from inside the class, not the
7203      specification DIE at toplevel.  */
7204   if (child->die_parent != parent)
7205     {
7206       dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
7207
7208       if (tmp)
7209         child = tmp;
7210     }
7211
7212   gcc_assert (child->die_parent == parent
7213               || (child->die_parent
7214                   == get_AT_ref (parent, DW_AT_specification)));
7215
7216   for (p = child->die_parent->die_child; ; p = p->die_sib)
7217     if (p->die_sib == child)
7218       {
7219         remove_child_with_prev (child, p);
7220         break;
7221       }
7222
7223   add_child_die (parent, child);
7224 }
7225
7226 /* Return a pointer to a newly created DIE node.  */
7227
7228 static inline dw_die_ref
7229 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
7230 {
7231   dw_die_ref die = GGC_CNEW (die_node);
7232
7233   die->die_tag = tag_value;
7234
7235   if (parent_die != NULL)
7236     add_child_die (parent_die, die);
7237   else
7238     {
7239       limbo_die_node *limbo_node;
7240
7241       limbo_node = GGC_CNEW (limbo_die_node);
7242       limbo_node->die = die;
7243       limbo_node->created_for = t;
7244       limbo_node->next = limbo_die_list;
7245       limbo_die_list = limbo_node;
7246     }
7247
7248   return die;
7249 }
7250
7251 /* Return the DIE associated with the given type specifier.  */
7252
7253 static inline dw_die_ref
7254 lookup_type_die (tree type)
7255 {
7256   return TYPE_SYMTAB_DIE (type);
7257 }
7258
7259 /* Equate a DIE to a given type specifier.  */
7260
7261 static inline void
7262 equate_type_number_to_die (tree type, dw_die_ref type_die)
7263 {
7264   TYPE_SYMTAB_DIE (type) = type_die;
7265 }
7266
7267 /* Returns a hash value for X (which really is a die_struct).  */
7268
7269 static hashval_t
7270 decl_die_table_hash (const void *x)
7271 {
7272   return (hashval_t) ((const_dw_die_ref) x)->decl_id;
7273 }
7274
7275 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
7276
7277 static int
7278 decl_die_table_eq (const void *x, const void *y)
7279 {
7280   return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
7281 }
7282
7283 /* Return the DIE associated with a given declaration.  */
7284
7285 static inline dw_die_ref
7286 lookup_decl_die (tree decl)
7287 {
7288   return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
7289 }
7290
7291 /* Returns a hash value for X (which really is a var_loc_list).  */
7292
7293 static hashval_t
7294 decl_loc_table_hash (const void *x)
7295 {
7296   return (hashval_t) ((const var_loc_list *) x)->decl_id;
7297 }
7298
7299 /* Return nonzero if decl_id of var_loc_list X is the same as
7300    UID of decl *Y.  */
7301
7302 static int
7303 decl_loc_table_eq (const void *x, const void *y)
7304 {
7305   return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
7306 }
7307
7308 /* Return the var_loc list associated with a given declaration.  */
7309
7310 static inline var_loc_list *
7311 lookup_decl_loc (const_tree decl)
7312 {
7313   return (var_loc_list *)
7314     htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
7315 }
7316
7317 /* Equate a DIE to a particular declaration.  */
7318
7319 static void
7320 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
7321 {
7322   unsigned int decl_id = DECL_UID (decl);
7323   void **slot;
7324
7325   slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
7326   *slot = decl_die;
7327   decl_die->decl_id = decl_id;
7328 }
7329
7330 /* Add a variable location node to the linked list for DECL.  */
7331
7332 static void
7333 add_var_loc_to_decl (tree decl, struct var_loc_node *loc)
7334 {
7335   unsigned int decl_id = DECL_UID (decl);
7336   var_loc_list *temp;
7337   void **slot;
7338
7339   slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
7340   if (*slot == NULL)
7341     {
7342       temp = GGC_CNEW (var_loc_list);
7343       temp->decl_id = decl_id;
7344       *slot = temp;
7345     }
7346   else
7347     temp = (var_loc_list *) *slot;
7348
7349   if (temp->last)
7350     {
7351       /* If the current location is the same as the end of the list,
7352          and either both or neither of the locations is uninitialized,
7353          we have nothing to do.  */
7354       if ((!rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->last->var_loc_note),
7355                          NOTE_VAR_LOCATION_LOC (loc->var_loc_note)))
7356           || ((NOTE_VAR_LOCATION_STATUS (temp->last->var_loc_note)
7357                != NOTE_VAR_LOCATION_STATUS (loc->var_loc_note))
7358               && ((NOTE_VAR_LOCATION_STATUS (temp->last->var_loc_note)
7359                    == VAR_INIT_STATUS_UNINITIALIZED)
7360                   || (NOTE_VAR_LOCATION_STATUS (loc->var_loc_note)
7361                       == VAR_INIT_STATUS_UNINITIALIZED))))
7362         {
7363           /* Add LOC to the end of list and update LAST.  */
7364           temp->last->next = loc;
7365           temp->last = loc;
7366         }
7367     }
7368   /* Do not add empty location to the beginning of the list.  */
7369   else if (NOTE_VAR_LOCATION_LOC (loc->var_loc_note) != NULL_RTX)
7370     {
7371       temp->first = loc;
7372       temp->last = loc;
7373     }
7374 }
7375 \f
7376 /* Keep track of the number of spaces used to indent the
7377    output of the debugging routines that print the structure of
7378    the DIE internal representation.  */
7379 static int print_indent;
7380
7381 /* Indent the line the number of spaces given by print_indent.  */
7382
7383 static inline void
7384 print_spaces (FILE *outfile)
7385 {
7386   fprintf (outfile, "%*s", print_indent, "");
7387 }
7388
7389 /* Print the information associated with a given DIE, and its children.
7390    This routine is a debugging aid only.  */
7391
7392 static void
7393 print_die (dw_die_ref die, FILE *outfile)
7394 {
7395   dw_attr_ref a;
7396   dw_die_ref c;
7397   unsigned ix;
7398
7399   print_spaces (outfile);
7400   fprintf (outfile, "DIE %4ld: %s\n",
7401            die->die_offset, dwarf_tag_name (die->die_tag));
7402   print_spaces (outfile);
7403   fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
7404   fprintf (outfile, " offset: %ld\n", die->die_offset);
7405
7406   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7407     {
7408       print_spaces (outfile);
7409       fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
7410
7411       switch (AT_class (a))
7412         {
7413         case dw_val_class_addr:
7414           fprintf (outfile, "address");
7415           break;
7416         case dw_val_class_offset:
7417           fprintf (outfile, "offset");
7418           break;
7419         case dw_val_class_loc:
7420           fprintf (outfile, "location descriptor");
7421           break;
7422         case dw_val_class_loc_list:
7423           fprintf (outfile, "location list -> label:%s",
7424                    AT_loc_list (a)->ll_symbol);
7425           break;
7426         case dw_val_class_range_list:
7427           fprintf (outfile, "range list");
7428           break;
7429         case dw_val_class_const:
7430           fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
7431           break;
7432         case dw_val_class_unsigned_const:
7433           fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
7434           break;
7435         case dw_val_class_long_long:
7436           fprintf (outfile, "constant (%lu,%lu)",
7437                    a->dw_attr_val.v.val_long_long.hi,
7438                    a->dw_attr_val.v.val_long_long.low);
7439           break;
7440         case dw_val_class_vec:
7441           fprintf (outfile, "floating-point or vector constant");
7442           break;
7443         case dw_val_class_flag:
7444           fprintf (outfile, "%u", AT_flag (a));
7445           break;
7446         case dw_val_class_die_ref:
7447           if (AT_ref (a) != NULL)
7448             {
7449               if (AT_ref (a)->die_symbol)
7450                 fprintf (outfile, "die -> label: %s", AT_ref (a)->die_symbol);
7451               else
7452                 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
7453             }
7454           else
7455             fprintf (outfile, "die -> <null>");
7456           break;
7457         case dw_val_class_lbl_id:
7458         case dw_val_class_lineptr:
7459         case dw_val_class_macptr:
7460           fprintf (outfile, "label: %s", AT_lbl (a));
7461           break;
7462         case dw_val_class_str:
7463           if (AT_string (a) != NULL)
7464             fprintf (outfile, "\"%s\"", AT_string (a));
7465           else
7466             fprintf (outfile, "<null>");
7467           break;
7468         case dw_val_class_file:
7469           fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
7470                    AT_file (a)->emitted_number);
7471           break;
7472         default:
7473           break;
7474         }
7475
7476       fprintf (outfile, "\n");
7477     }
7478
7479   if (die->die_child != NULL)
7480     {
7481       print_indent += 4;
7482       FOR_EACH_CHILD (die, c, print_die (c, outfile));
7483       print_indent -= 4;
7484     }
7485   if (print_indent == 0)
7486     fprintf (outfile, "\n");
7487 }
7488
7489 /* Print the contents of the source code line number correspondence table.
7490    This routine is a debugging aid only.  */
7491
7492 static void
7493 print_dwarf_line_table (FILE *outfile)
7494 {
7495   unsigned i;
7496   dw_line_info_ref line_info;
7497
7498   fprintf (outfile, "\n\nDWARF source line information\n");
7499   for (i = 1; i < line_info_table_in_use; i++)
7500     {
7501       line_info = &line_info_table[i];
7502       fprintf (outfile, "%5d: %4ld %6ld\n", i,
7503                line_info->dw_file_num,
7504                line_info->dw_line_num);
7505     }
7506
7507   fprintf (outfile, "\n\n");
7508 }
7509
7510 /* Print the information collected for a given DIE.  */
7511
7512 void
7513 debug_dwarf_die (dw_die_ref die)
7514 {
7515   print_die (die, stderr);
7516 }
7517
7518 /* Print all DWARF information collected for the compilation unit.
7519    This routine is a debugging aid only.  */
7520
7521 void
7522 debug_dwarf (void)
7523 {
7524   print_indent = 0;
7525   print_die (comp_unit_die, stderr);
7526   if (! DWARF2_ASM_LINE_DEBUG_INFO)
7527     print_dwarf_line_table (stderr);
7528 }
7529 \f
7530 /* Start a new compilation unit DIE for an include file.  OLD_UNIT is the CU
7531    for the enclosing include file, if any.  BINCL_DIE is the DW_TAG_GNU_BINCL
7532    DIE that marks the start of the DIEs for this include file.  */
7533
7534 static dw_die_ref
7535 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
7536 {
7537   const char *filename = get_AT_string (bincl_die, DW_AT_name);
7538   dw_die_ref new_unit = gen_compile_unit_die (filename);
7539
7540   new_unit->die_sib = old_unit;
7541   return new_unit;
7542 }
7543
7544 /* Close an include-file CU and reopen the enclosing one.  */
7545
7546 static dw_die_ref
7547 pop_compile_unit (dw_die_ref old_unit)
7548 {
7549   dw_die_ref new_unit = old_unit->die_sib;
7550
7551   old_unit->die_sib = NULL;
7552   return new_unit;
7553 }
7554
7555 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
7556 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
7557
7558 /* Calculate the checksum of a location expression.  */
7559
7560 static inline void
7561 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
7562 {
7563   int tem;
7564
7565   tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
7566   CHECKSUM (tem);
7567   CHECKSUM (loc->dw_loc_oprnd1);
7568   CHECKSUM (loc->dw_loc_oprnd2);
7569 }
7570
7571 /* Calculate the checksum of an attribute.  */
7572
7573 static void
7574 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
7575 {
7576   dw_loc_descr_ref loc;
7577   rtx r;
7578
7579   CHECKSUM (at->dw_attr);
7580
7581   /* We don't care that this was compiled with a different compiler
7582      snapshot; if the output is the same, that's what matters.  */
7583   if (at->dw_attr == DW_AT_producer)
7584     return;
7585
7586   switch (AT_class (at))
7587     {
7588     case dw_val_class_const:
7589       CHECKSUM (at->dw_attr_val.v.val_int);
7590       break;
7591     case dw_val_class_unsigned_const:
7592       CHECKSUM (at->dw_attr_val.v.val_unsigned);
7593       break;
7594     case dw_val_class_long_long:
7595       CHECKSUM (at->dw_attr_val.v.val_long_long);
7596       break;
7597     case dw_val_class_vec:
7598       CHECKSUM (at->dw_attr_val.v.val_vec);
7599       break;
7600     case dw_val_class_flag:
7601       CHECKSUM (at->dw_attr_val.v.val_flag);
7602       break;
7603     case dw_val_class_str:
7604       CHECKSUM_STRING (AT_string (at));
7605       break;
7606
7607     case dw_val_class_addr:
7608       r = AT_addr (at);
7609       gcc_assert (GET_CODE (r) == SYMBOL_REF);
7610       CHECKSUM_STRING (XSTR (r, 0));
7611       break;
7612
7613     case dw_val_class_offset:
7614       CHECKSUM (at->dw_attr_val.v.val_offset);
7615       break;
7616
7617     case dw_val_class_loc:
7618       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
7619         loc_checksum (loc, ctx);
7620       break;
7621
7622     case dw_val_class_die_ref:
7623       die_checksum (AT_ref (at), ctx, mark);
7624       break;
7625
7626     case dw_val_class_fde_ref:
7627     case dw_val_class_lbl_id:
7628     case dw_val_class_lineptr:
7629     case dw_val_class_macptr:
7630       break;
7631
7632     case dw_val_class_file:
7633       CHECKSUM_STRING (AT_file (at)->filename);
7634       break;
7635
7636     default:
7637       break;
7638     }
7639 }
7640
7641 /* Calculate the checksum of a DIE.  */
7642
7643 static void
7644 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
7645 {
7646   dw_die_ref c;
7647   dw_attr_ref a;
7648   unsigned ix;
7649
7650   /* To avoid infinite recursion.  */
7651   if (die->die_mark)
7652     {
7653       CHECKSUM (die->die_mark);
7654       return;
7655     }
7656   die->die_mark = ++(*mark);
7657
7658   CHECKSUM (die->die_tag);
7659
7660   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7661     attr_checksum (a, ctx, mark);
7662
7663   FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
7664 }
7665
7666 #undef CHECKSUM
7667 #undef CHECKSUM_STRING
7668
7669 /* Do the location expressions look same?  */
7670 static inline int
7671 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
7672 {
7673   return loc1->dw_loc_opc == loc2->dw_loc_opc
7674          && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
7675          && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
7676 }
7677
7678 /* Do the values look the same?  */
7679 static int
7680 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
7681 {
7682   dw_loc_descr_ref loc1, loc2;
7683   rtx r1, r2;
7684
7685   if (v1->val_class != v2->val_class)
7686     return 0;
7687
7688   switch (v1->val_class)
7689     {
7690     case dw_val_class_const:
7691       return v1->v.val_int == v2->v.val_int;
7692     case dw_val_class_unsigned_const:
7693       return v1->v.val_unsigned == v2->v.val_unsigned;
7694     case dw_val_class_long_long:
7695       return v1->v.val_long_long.hi == v2->v.val_long_long.hi
7696              && v1->v.val_long_long.low == v2->v.val_long_long.low;
7697     case dw_val_class_vec:
7698       if (v1->v.val_vec.length != v2->v.val_vec.length
7699           || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
7700         return 0;
7701       if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
7702                   v1->v.val_vec.length * v1->v.val_vec.elt_size))
7703         return 0;
7704       return 1;
7705     case dw_val_class_flag:
7706       return v1->v.val_flag == v2->v.val_flag;
7707     case dw_val_class_str:
7708       return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
7709
7710     case dw_val_class_addr:
7711       r1 = v1->v.val_addr;
7712       r2 = v2->v.val_addr;
7713       if (GET_CODE (r1) != GET_CODE (r2))
7714         return 0;
7715       gcc_assert (GET_CODE (r1) == SYMBOL_REF);
7716       return !strcmp (XSTR (r1, 0), XSTR (r2, 0));
7717
7718     case dw_val_class_offset:
7719       return v1->v.val_offset == v2->v.val_offset;
7720
7721     case dw_val_class_loc:
7722       for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
7723            loc1 && loc2;
7724            loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
7725         if (!same_loc_p (loc1, loc2, mark))
7726           return 0;
7727       return !loc1 && !loc2;
7728
7729     case dw_val_class_die_ref:
7730       return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
7731
7732     case dw_val_class_fde_ref:
7733     case dw_val_class_lbl_id:
7734     case dw_val_class_lineptr:
7735     case dw_val_class_macptr:
7736       return 1;
7737
7738     case dw_val_class_file:
7739       return v1->v.val_file == v2->v.val_file;
7740
7741     default:
7742       return 1;
7743     }
7744 }
7745
7746 /* Do the attributes look the same?  */
7747
7748 static int
7749 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
7750 {
7751   if (at1->dw_attr != at2->dw_attr)
7752     return 0;
7753
7754   /* We don't care that this was compiled with a different compiler
7755      snapshot; if the output is the same, that's what matters. */
7756   if (at1->dw_attr == DW_AT_producer)
7757     return 1;
7758
7759   return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
7760 }
7761
7762 /* Do the dies look the same?  */
7763
7764 static int
7765 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
7766 {
7767   dw_die_ref c1, c2;
7768   dw_attr_ref a1;
7769   unsigned ix;
7770
7771   /* To avoid infinite recursion.  */
7772   if (die1->die_mark)
7773     return die1->die_mark == die2->die_mark;
7774   die1->die_mark = die2->die_mark = ++(*mark);
7775
7776   if (die1->die_tag != die2->die_tag)
7777     return 0;
7778
7779   if (VEC_length (dw_attr_node, die1->die_attr)
7780       != VEC_length (dw_attr_node, die2->die_attr))
7781     return 0;
7782
7783   for (ix = 0; VEC_iterate (dw_attr_node, die1->die_attr, ix, a1); ix++)
7784     if (!same_attr_p (a1, VEC_index (dw_attr_node, die2->die_attr, ix), mark))
7785       return 0;
7786
7787   c1 = die1->die_child;
7788   c2 = die2->die_child;
7789   if (! c1)
7790     {
7791       if (c2)
7792         return 0;
7793     }
7794   else
7795     for (;;)
7796       {
7797         if (!same_die_p (c1, c2, mark))
7798           return 0;
7799         c1 = c1->die_sib;
7800         c2 = c2->die_sib;
7801         if (c1 == die1->die_child)
7802           {
7803             if (c2 == die2->die_child)
7804               break;
7805             else
7806               return 0;
7807           }
7808     }
7809
7810   return 1;
7811 }
7812
7813 /* Do the dies look the same?  Wrapper around same_die_p.  */
7814
7815 static int
7816 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
7817 {
7818   int mark = 0;
7819   int ret = same_die_p (die1, die2, &mark);
7820
7821   unmark_all_dies (die1);
7822   unmark_all_dies (die2);
7823
7824   return ret;
7825 }
7826
7827 /* The prefix to attach to symbols on DIEs in the current comdat debug
7828    info section.  */
7829 static char *comdat_symbol_id;
7830
7831 /* The index of the current symbol within the current comdat CU.  */
7832 static unsigned int comdat_symbol_number;
7833
7834 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
7835    children, and set comdat_symbol_id accordingly.  */
7836
7837 static void
7838 compute_section_prefix (dw_die_ref unit_die)
7839 {
7840   const char *die_name = get_AT_string (unit_die, DW_AT_name);
7841   const char *base = die_name ? lbasename (die_name) : "anonymous";
7842   char *name = XALLOCAVEC (char, strlen (base) + 64);
7843   char *p;
7844   int i, mark;
7845   unsigned char checksum[16];
7846   struct md5_ctx ctx;
7847
7848   /* Compute the checksum of the DIE, then append part of it as hex digits to
7849      the name filename of the unit.  */
7850
7851   md5_init_ctx (&ctx);
7852   mark = 0;
7853   die_checksum (unit_die, &ctx, &mark);
7854   unmark_all_dies (unit_die);
7855   md5_finish_ctx (&ctx, checksum);
7856
7857   sprintf (name, "%s.", base);
7858   clean_symbol_name (name);
7859
7860   p = name + strlen (name);
7861   for (i = 0; i < 4; i++)
7862     {
7863       sprintf (p, "%.2x", checksum[i]);
7864       p += 2;
7865     }
7866
7867   comdat_symbol_id = unit_die->die_symbol = xstrdup (name);
7868   comdat_symbol_number = 0;
7869 }
7870
7871 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P.  */
7872
7873 static int
7874 is_type_die (dw_die_ref die)
7875 {
7876   switch (die->die_tag)
7877     {
7878     case DW_TAG_array_type:
7879     case DW_TAG_class_type:
7880     case DW_TAG_interface_type:
7881     case DW_TAG_enumeration_type:
7882     case DW_TAG_pointer_type:
7883     case DW_TAG_reference_type:
7884     case DW_TAG_string_type:
7885     case DW_TAG_structure_type:
7886     case DW_TAG_subroutine_type:
7887     case DW_TAG_union_type:
7888     case DW_TAG_ptr_to_member_type:
7889     case DW_TAG_set_type:
7890     case DW_TAG_subrange_type:
7891     case DW_TAG_base_type:
7892     case DW_TAG_const_type:
7893     case DW_TAG_file_type:
7894     case DW_TAG_packed_type:
7895     case DW_TAG_volatile_type:
7896     case DW_TAG_typedef:
7897       return 1;
7898     default:
7899       return 0;
7900     }
7901 }
7902
7903 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
7904    Basically, we want to choose the bits that are likely to be shared between
7905    compilations (types) and leave out the bits that are specific to individual
7906    compilations (functions).  */
7907
7908 static int
7909 is_comdat_die (dw_die_ref c)
7910 {
7911   /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
7912      we do for stabs.  The advantage is a greater likelihood of sharing between
7913      objects that don't include headers in the same order (and therefore would
7914      put the base types in a different comdat).  jason 8/28/00 */
7915
7916   if (c->die_tag == DW_TAG_base_type)
7917     return 0;
7918
7919   if (c->die_tag == DW_TAG_pointer_type
7920       || c->die_tag == DW_TAG_reference_type
7921       || c->die_tag == DW_TAG_const_type
7922       || c->die_tag == DW_TAG_volatile_type)
7923     {
7924       dw_die_ref t = get_AT_ref (c, DW_AT_type);
7925
7926       return t ? is_comdat_die (t) : 0;
7927     }
7928
7929   return is_type_die (c);
7930 }
7931
7932 /* Returns 1 iff C is the sort of DIE that might be referred to from another
7933    compilation unit.  */
7934
7935 static int
7936 is_symbol_die (dw_die_ref c)
7937 {
7938   return (is_type_die (c)
7939           || (get_AT (c, DW_AT_declaration)
7940               && !get_AT (c, DW_AT_specification))
7941           || c->die_tag == DW_TAG_namespace
7942           || c->die_tag == DW_TAG_module);
7943 }
7944
7945 static char *
7946 gen_internal_sym (const char *prefix)
7947 {
7948   char buf[256];
7949
7950   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
7951   return xstrdup (buf);
7952 }
7953
7954 /* Assign symbols to all worthy DIEs under DIE.  */
7955
7956 static void
7957 assign_symbol_names (dw_die_ref die)
7958 {
7959   dw_die_ref c;
7960
7961   if (is_symbol_die (die))
7962     {
7963       if (comdat_symbol_id)
7964         {
7965           char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
7966
7967           sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
7968                    comdat_symbol_id, comdat_symbol_number++);
7969           die->die_symbol = xstrdup (p);
7970         }
7971       else
7972         die->die_symbol = gen_internal_sym ("LDIE");
7973     }
7974
7975   FOR_EACH_CHILD (die, c, assign_symbol_names (c));
7976 }
7977
7978 struct cu_hash_table_entry
7979 {
7980   dw_die_ref cu;
7981   unsigned min_comdat_num, max_comdat_num;
7982   struct cu_hash_table_entry *next;
7983 };
7984
7985 /* Routines to manipulate hash table of CUs.  */
7986 static hashval_t
7987 htab_cu_hash (const void *of)
7988 {
7989   const struct cu_hash_table_entry *const entry =
7990     (const struct cu_hash_table_entry *) of;
7991
7992   return htab_hash_string (entry->cu->die_symbol);
7993 }
7994
7995 static int
7996 htab_cu_eq (const void *of1, const void *of2)
7997 {
7998   const struct cu_hash_table_entry *const entry1 =
7999     (const struct cu_hash_table_entry *) of1;
8000   const struct die_struct *const entry2 = (const struct die_struct *) of2;
8001
8002   return !strcmp (entry1->cu->die_symbol, entry2->die_symbol);
8003 }
8004
8005 static void
8006 htab_cu_del (void *what)
8007 {
8008   struct cu_hash_table_entry *next,
8009     *entry = (struct cu_hash_table_entry *) what;
8010
8011   while (entry)
8012     {
8013       next = entry->next;
8014       free (entry);
8015       entry = next;
8016     }
8017 }
8018
8019 /* Check whether we have already seen this CU and set up SYM_NUM
8020    accordingly.  */
8021 static int
8022 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
8023 {
8024   struct cu_hash_table_entry dummy;
8025   struct cu_hash_table_entry **slot, *entry, *last = &dummy;
8026
8027   dummy.max_comdat_num = 0;
8028
8029   slot = (struct cu_hash_table_entry **)
8030     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
8031         INSERT);
8032   entry = *slot;
8033
8034   for (; entry; last = entry, entry = entry->next)
8035     {
8036       if (same_die_p_wrap (cu, entry->cu))
8037         break;
8038     }
8039
8040   if (entry)
8041     {
8042       *sym_num = entry->min_comdat_num;
8043       return 1;
8044     }
8045
8046   entry = XCNEW (struct cu_hash_table_entry);
8047   entry->cu = cu;
8048   entry->min_comdat_num = *sym_num = last->max_comdat_num;
8049   entry->next = *slot;
8050   *slot = entry;
8051
8052   return 0;
8053 }
8054
8055 /* Record SYM_NUM to record of CU in HTABLE.  */
8056 static void
8057 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
8058 {
8059   struct cu_hash_table_entry **slot, *entry;
8060
8061   slot = (struct cu_hash_table_entry **)
8062     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
8063         NO_INSERT);
8064   entry = *slot;
8065
8066   entry->max_comdat_num = sym_num;
8067 }
8068
8069 /* Traverse the DIE (which is always comp_unit_die), and set up
8070    additional compilation units for each of the include files we see
8071    bracketed by BINCL/EINCL.  */
8072
8073 static void
8074 break_out_includes (dw_die_ref die)
8075 {
8076   dw_die_ref c;
8077   dw_die_ref unit = NULL;
8078   limbo_die_node *node, **pnode;
8079   htab_t cu_hash_table;
8080
8081   c = die->die_child;
8082   if (c) do {
8083     dw_die_ref prev = c;
8084     c = c->die_sib;
8085     while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
8086            || (unit && is_comdat_die (c)))
8087       {
8088         dw_die_ref next = c->die_sib;
8089
8090         /* This DIE is for a secondary CU; remove it from the main one.  */
8091         remove_child_with_prev (c, prev);
8092
8093         if (c->die_tag == DW_TAG_GNU_BINCL)
8094           unit = push_new_compile_unit (unit, c);
8095         else if (c->die_tag == DW_TAG_GNU_EINCL)
8096           unit = pop_compile_unit (unit);
8097         else
8098           add_child_die (unit, c);
8099         c = next;
8100         if (c == die->die_child)
8101           break;
8102       }
8103   } while (c != die->die_child);
8104
8105 #if 0
8106   /* We can only use this in debugging, since the frontend doesn't check
8107      to make sure that we leave every include file we enter.  */
8108   gcc_assert (!unit);
8109 #endif
8110
8111   assign_symbol_names (die);
8112   cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
8113   for (node = limbo_die_list, pnode = &limbo_die_list;
8114        node;
8115        node = node->next)
8116     {
8117       int is_dupl;
8118
8119       compute_section_prefix (node->die);
8120       is_dupl = check_duplicate_cu (node->die, cu_hash_table,
8121                         &comdat_symbol_number);
8122       assign_symbol_names (node->die);
8123       if (is_dupl)
8124         *pnode = node->next;
8125       else
8126         {
8127           pnode = &node->next;
8128           record_comdat_symbol_number (node->die, cu_hash_table,
8129                 comdat_symbol_number);
8130         }
8131     }
8132   htab_delete (cu_hash_table);
8133 }
8134
8135 /* Traverse the DIE and add a sibling attribute if it may have the
8136    effect of speeding up access to siblings.  To save some space,
8137    avoid generating sibling attributes for DIE's without children.  */
8138
8139 static void
8140 add_sibling_attributes (dw_die_ref die)
8141 {
8142   dw_die_ref c;
8143
8144   if (! die->die_child)
8145     return;
8146
8147   if (die->die_parent && die != die->die_parent->die_child)
8148     add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
8149
8150   FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
8151 }
8152
8153 /* Output all location lists for the DIE and its children.  */
8154
8155 static void
8156 output_location_lists (dw_die_ref die)
8157 {
8158   dw_die_ref c;
8159   dw_attr_ref a;
8160   unsigned ix;
8161
8162   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
8163     if (AT_class (a) == dw_val_class_loc_list)
8164       output_loc_list (AT_loc_list (a));
8165
8166   FOR_EACH_CHILD (die, c, output_location_lists (c));
8167 }
8168
8169 /* The format of each DIE (and its attribute value pairs) is encoded in an
8170    abbreviation table.  This routine builds the abbreviation table and assigns
8171    a unique abbreviation id for each abbreviation entry.  The children of each
8172    die are visited recursively.  */
8173
8174 static void
8175 build_abbrev_table (dw_die_ref die)
8176 {
8177   unsigned long abbrev_id;
8178   unsigned int n_alloc;
8179   dw_die_ref c;
8180   dw_attr_ref a;
8181   unsigned ix;
8182
8183   /* Scan the DIE references, and mark as external any that refer to
8184      DIEs from other CUs (i.e. those which are not marked).  */
8185   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
8186     if (AT_class (a) == dw_val_class_die_ref
8187         && AT_ref (a)->die_mark == 0)
8188       {
8189         gcc_assert (AT_ref (a)->die_symbol);
8190         set_AT_ref_external (a, 1);
8191       }
8192
8193   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8194     {
8195       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
8196       dw_attr_ref die_a, abbrev_a;
8197       unsigned ix;
8198       bool ok = true;
8199
8200       if (abbrev->die_tag != die->die_tag)
8201         continue;
8202       if ((abbrev->die_child != NULL) != (die->die_child != NULL))
8203         continue;
8204
8205       if (VEC_length (dw_attr_node, abbrev->die_attr)
8206           != VEC_length (dw_attr_node, die->die_attr))
8207         continue;
8208
8209       for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, die_a); ix++)
8210         {
8211           abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
8212           if ((abbrev_a->dw_attr != die_a->dw_attr)
8213               || (value_format (abbrev_a) != value_format (die_a)))
8214             {
8215               ok = false;
8216               break;
8217             }
8218         }
8219       if (ok)
8220         break;
8221     }
8222
8223   if (abbrev_id >= abbrev_die_table_in_use)
8224     {
8225       if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
8226         {
8227           n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
8228           abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
8229                                             n_alloc);
8230
8231           memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
8232                  (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
8233           abbrev_die_table_allocated = n_alloc;
8234         }
8235
8236       ++abbrev_die_table_in_use;
8237       abbrev_die_table[abbrev_id] = die;
8238     }
8239
8240   die->die_abbrev = abbrev_id;
8241   FOR_EACH_CHILD (die, c, build_abbrev_table (c));
8242 }
8243 \f
8244 /* Return the power-of-two number of bytes necessary to represent VALUE.  */
8245
8246 static int
8247 constant_size (unsigned HOST_WIDE_INT value)
8248 {
8249   int log;
8250
8251   if (value == 0)
8252     log = 0;
8253   else
8254     log = floor_log2 (value);
8255
8256   log = log / 8;
8257   log = 1 << (floor_log2 (log) + 1);
8258
8259   return log;
8260 }
8261
8262 /* Return the size of a DIE as it is represented in the
8263    .debug_info section.  */
8264
8265 static unsigned long
8266 size_of_die (dw_die_ref die)
8267 {
8268   unsigned long size = 0;
8269   dw_attr_ref a;
8270   unsigned ix;
8271
8272   size += size_of_uleb128 (die->die_abbrev);
8273   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
8274     {
8275       switch (AT_class (a))
8276         {
8277         case dw_val_class_addr:
8278           size += DWARF2_ADDR_SIZE;
8279           break;
8280         case dw_val_class_offset:
8281           size += DWARF_OFFSET_SIZE;
8282           break;
8283         case dw_val_class_loc:
8284           {
8285             unsigned long lsize = size_of_locs (AT_loc (a));
8286
8287             /* Block length.  */
8288             size += constant_size (lsize);
8289             size += lsize;
8290           }
8291           break;
8292         case dw_val_class_loc_list:
8293           size += DWARF_OFFSET_SIZE;
8294           break;
8295         case dw_val_class_range_list:
8296           size += DWARF_OFFSET_SIZE;
8297           break;
8298         case dw_val_class_const:
8299           size += size_of_sleb128 (AT_int (a));
8300           break;
8301         case dw_val_class_unsigned_const:
8302           size += constant_size (AT_unsigned (a));
8303           break;
8304         case dw_val_class_long_long:
8305           size += 1 + 2*HOST_BITS_PER_LONG/HOST_BITS_PER_CHAR; /* block */
8306           break;
8307         case dw_val_class_vec:
8308           size += constant_size (a->dw_attr_val.v.val_vec.length
8309                                  * a->dw_attr_val.v.val_vec.elt_size)
8310                   + a->dw_attr_val.v.val_vec.length
8311                     * a->dw_attr_val.v.val_vec.elt_size; /* block */
8312           break;
8313         case dw_val_class_flag:
8314           size += 1;
8315           break;
8316         case dw_val_class_die_ref:
8317           /* In DWARF2, DW_FORM_ref_addr is sized by target address length,
8318              whereas in DWARF3 it's always sized as an offset.  */
8319           if (AT_ref_external (a) && dwarf_version == 2)
8320             size += DWARF2_ADDR_SIZE;
8321           else
8322             size += DWARF_OFFSET_SIZE;
8323           break;
8324         case dw_val_class_fde_ref:
8325           size += DWARF_OFFSET_SIZE;
8326           break;
8327         case dw_val_class_lbl_id:
8328           size += DWARF2_ADDR_SIZE;
8329           break;
8330         case dw_val_class_lineptr:
8331         case dw_val_class_macptr:
8332           size += DWARF_OFFSET_SIZE;
8333           break;
8334         case dw_val_class_str:
8335           if (AT_string_form (a) == DW_FORM_strp)
8336             size += DWARF_OFFSET_SIZE;
8337           else
8338             size += strlen (a->dw_attr_val.v.val_str->str) + 1;
8339           break;
8340         case dw_val_class_file:
8341           size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
8342           break;
8343         default:
8344           gcc_unreachable ();
8345         }
8346     }
8347
8348   return size;
8349 }
8350
8351 /* Size the debugging information associated with a given DIE.  Visits the
8352    DIE's children recursively.  Updates the global variable next_die_offset, on
8353    each time through.  Uses the current value of next_die_offset to update the
8354    die_offset field in each DIE.  */
8355
8356 static void
8357 calc_die_sizes (dw_die_ref die)
8358 {
8359   dw_die_ref c;
8360
8361   die->die_offset = next_die_offset;
8362   next_die_offset += size_of_die (die);
8363
8364   FOR_EACH_CHILD (die, c, calc_die_sizes (c));
8365
8366   if (die->die_child != NULL)
8367     /* Count the null byte used to terminate sibling lists.  */
8368     next_die_offset += 1;
8369 }
8370
8371 /* Set the marks for a die and its children.  We do this so
8372    that we know whether or not a reference needs to use FORM_ref_addr; only
8373    DIEs in the same CU will be marked.  We used to clear out the offset
8374    and use that as the flag, but ran into ordering problems.  */
8375
8376 static void
8377 mark_dies (dw_die_ref die)
8378 {
8379   dw_die_ref c;
8380
8381   gcc_assert (!die->die_mark);
8382
8383   die->die_mark = 1;
8384   FOR_EACH_CHILD (die, c, mark_dies (c));
8385 }
8386
8387 /* Clear the marks for a die and its children.  */
8388
8389 static void
8390 unmark_dies (dw_die_ref die)
8391 {
8392   dw_die_ref c;
8393
8394   gcc_assert (die->die_mark);
8395
8396   die->die_mark = 0;
8397   FOR_EACH_CHILD (die, c, unmark_dies (c));
8398 }
8399
8400 /* Clear the marks for a die, its children and referred dies.  */
8401
8402 static void
8403 unmark_all_dies (dw_die_ref die)
8404 {
8405   dw_die_ref c;
8406   dw_attr_ref a;
8407   unsigned ix;
8408
8409   if (!die->die_mark)
8410     return;
8411   die->die_mark = 0;
8412
8413   FOR_EACH_CHILD (die, c, unmark_all_dies (c));
8414
8415   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
8416     if (AT_class (a) == dw_val_class_die_ref)
8417       unmark_all_dies (AT_ref (a));
8418 }
8419
8420 /* Return the size of the .debug_pubnames or .debug_pubtypes table
8421    generated for the compilation unit.  */
8422
8423 static unsigned long
8424 size_of_pubnames (VEC (pubname_entry, gc) * names)
8425 {
8426   unsigned long size;
8427   unsigned i;
8428   pubname_ref p;
8429
8430   size = DWARF_PUBNAMES_HEADER_SIZE;
8431   for (i = 0; VEC_iterate (pubname_entry, names, i, p); i++)
8432     if (names != pubtype_table
8433         || p->die->die_offset != 0
8434         || !flag_eliminate_unused_debug_types)
8435       size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
8436
8437   size += DWARF_OFFSET_SIZE;
8438   return size;
8439 }
8440
8441 /* Return the size of the information in the .debug_aranges section.  */
8442
8443 static unsigned long
8444 size_of_aranges (void)
8445 {
8446   unsigned long size;
8447
8448   size = DWARF_ARANGES_HEADER_SIZE;
8449
8450   /* Count the address/length pair for this compilation unit.  */
8451   if (text_section_used)
8452     size += 2 * DWARF2_ADDR_SIZE;
8453   if (cold_text_section_used)
8454     size += 2 * DWARF2_ADDR_SIZE;
8455   size += 2 * DWARF2_ADDR_SIZE * arange_table_in_use;
8456
8457   /* Count the two zero words used to terminated the address range table.  */
8458   size += 2 * DWARF2_ADDR_SIZE;
8459   return size;
8460 }
8461 \f
8462 /* Select the encoding of an attribute value.  */
8463
8464 static enum dwarf_form
8465 value_format (dw_attr_ref a)
8466 {
8467   switch (a->dw_attr_val.val_class)
8468     {
8469     case dw_val_class_addr:
8470       return DW_FORM_addr;
8471     case dw_val_class_range_list:
8472     case dw_val_class_offset:
8473     case dw_val_class_loc_list:
8474       switch (DWARF_OFFSET_SIZE)
8475         {
8476         case 4:
8477           return DW_FORM_data4;
8478         case 8:
8479           return DW_FORM_data8;
8480         default:
8481           gcc_unreachable ();
8482         }
8483     case dw_val_class_loc:
8484       switch (constant_size (size_of_locs (AT_loc (a))))
8485         {
8486         case 1:
8487           return DW_FORM_block1;
8488         case 2:
8489           return DW_FORM_block2;
8490         default:
8491           gcc_unreachable ();
8492         }
8493     case dw_val_class_const:
8494       return DW_FORM_sdata;
8495     case dw_val_class_unsigned_const:
8496       switch (constant_size (AT_unsigned (a)))
8497         {
8498         case 1:
8499           return DW_FORM_data1;
8500         case 2:
8501           return DW_FORM_data2;
8502         case 4:
8503           return DW_FORM_data4;
8504         case 8:
8505           return DW_FORM_data8;
8506         default:
8507           gcc_unreachable ();
8508         }
8509     case dw_val_class_long_long:
8510       return DW_FORM_block1;
8511     case dw_val_class_vec:
8512       switch (constant_size (a->dw_attr_val.v.val_vec.length
8513                              * a->dw_attr_val.v.val_vec.elt_size))
8514         {
8515         case 1:
8516           return DW_FORM_block1;
8517         case 2:
8518           return DW_FORM_block2;
8519         case 4:
8520           return DW_FORM_block4;
8521         default:
8522           gcc_unreachable ();
8523         }
8524     case dw_val_class_flag:
8525       return DW_FORM_flag;
8526     case dw_val_class_die_ref:
8527       if (AT_ref_external (a))
8528         return DW_FORM_ref_addr;
8529       else
8530         return DW_FORM_ref;
8531     case dw_val_class_fde_ref:
8532       return DW_FORM_data;
8533     case dw_val_class_lbl_id:
8534       return DW_FORM_addr;
8535     case dw_val_class_lineptr:
8536     case dw_val_class_macptr:
8537       return DW_FORM_data;
8538     case dw_val_class_str:
8539       return AT_string_form (a);
8540     case dw_val_class_file:
8541       switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
8542         {
8543         case 1:
8544           return DW_FORM_data1;
8545         case 2:
8546           return DW_FORM_data2;
8547         case 4:
8548           return DW_FORM_data4;
8549         default:
8550           gcc_unreachable ();
8551         }
8552
8553     default:
8554       gcc_unreachable ();
8555     }
8556 }
8557
8558 /* Output the encoding of an attribute value.  */
8559
8560 static void
8561 output_value_format (dw_attr_ref a)
8562 {
8563   enum dwarf_form form = value_format (a);
8564
8565   dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
8566 }
8567
8568 /* Output the .debug_abbrev section which defines the DIE abbreviation
8569    table.  */
8570
8571 static void
8572 output_abbrev_section (void)
8573 {
8574   unsigned long abbrev_id;
8575
8576   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8577     {
8578       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
8579       unsigned ix;
8580       dw_attr_ref a_attr;
8581
8582       dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
8583       dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
8584                                    dwarf_tag_name (abbrev->die_tag));
8585
8586       if (abbrev->die_child != NULL)
8587         dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
8588       else
8589         dw2_asm_output_data (1, DW_children_no, "DW_children_no");
8590
8591       for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
8592            ix++)
8593         {
8594           dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
8595                                        dwarf_attr_name (a_attr->dw_attr));
8596           output_value_format (a_attr);
8597         }
8598
8599       dw2_asm_output_data (1, 0, NULL);
8600       dw2_asm_output_data (1, 0, NULL);
8601     }
8602
8603   /* Terminate the table.  */
8604   dw2_asm_output_data (1, 0, NULL);
8605 }
8606
8607 /* Output a symbol we can use to refer to this DIE from another CU.  */
8608
8609 static inline void
8610 output_die_symbol (dw_die_ref die)
8611 {
8612   char *sym = die->die_symbol;
8613
8614   if (sym == 0)
8615     return;
8616
8617   if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
8618     /* We make these global, not weak; if the target doesn't support
8619        .linkonce, it doesn't support combining the sections, so debugging
8620        will break.  */
8621     targetm.asm_out.globalize_label (asm_out_file, sym);
8622
8623   ASM_OUTPUT_LABEL (asm_out_file, sym);
8624 }
8625
8626 /* Return a new location list, given the begin and end range, and the
8627    expression. gensym tells us whether to generate a new internal symbol for
8628    this location list node, which is done for the head of the list only.  */
8629
8630 static inline dw_loc_list_ref
8631 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
8632               const char *section, unsigned int gensym)
8633 {
8634   dw_loc_list_ref retlist = GGC_CNEW (dw_loc_list_node);
8635
8636   retlist->begin = begin;
8637   retlist->end = end;
8638   retlist->expr = expr;
8639   retlist->section = section;
8640   if (gensym)
8641     retlist->ll_symbol = gen_internal_sym ("LLST");
8642
8643   return retlist;
8644 }
8645
8646 /* Add a location description expression to a location list.  */
8647
8648 static inline void
8649 add_loc_descr_to_loc_list (dw_loc_list_ref *list_head, dw_loc_descr_ref descr,
8650                            const char *begin, const char *end,
8651                            const char *section)
8652 {
8653   dw_loc_list_ref *d;
8654
8655   /* Find the end of the chain.  */
8656   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
8657     ;
8658
8659   /* Add a new location list node to the list.  */
8660   *d = new_loc_list (descr, begin, end, section, 0);
8661 }
8662
8663 /* Output the location list given to us.  */
8664
8665 static void
8666 output_loc_list (dw_loc_list_ref list_head)
8667 {
8668   dw_loc_list_ref curr = list_head;
8669
8670   ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
8671
8672   /* Walk the location list, and output each range + expression.  */
8673   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
8674     {
8675       unsigned long size;
8676       /* Don't output an entry that starts and ends at the same address.  */
8677       if (strcmp (curr->begin, curr->end) == 0)
8678         continue;
8679       if (!have_multiple_function_sections)
8680         {
8681           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
8682                                 "Location list begin address (%s)",
8683                                 list_head->ll_symbol);
8684           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
8685                                 "Location list end address (%s)",
8686                                 list_head->ll_symbol);
8687         }
8688       else
8689         {
8690           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
8691                                "Location list begin address (%s)",
8692                                list_head->ll_symbol);
8693           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
8694                                "Location list end address (%s)",
8695                                list_head->ll_symbol);
8696         }
8697       size = size_of_locs (curr->expr);
8698
8699       /* Output the block length for this list of location operations.  */
8700       gcc_assert (size <= 0xffff);
8701       dw2_asm_output_data (2, size, "%s", "Location expression size");
8702
8703       output_loc_sequence (curr->expr);
8704     }
8705
8706   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8707                        "Location list terminator begin (%s)",
8708                        list_head->ll_symbol);
8709   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8710                        "Location list terminator end (%s)",
8711                        list_head->ll_symbol);
8712 }
8713
8714 /* Output the DIE and its attributes.  Called recursively to generate
8715    the definitions of each child DIE.  */
8716
8717 static void
8718 output_die (dw_die_ref die)
8719 {
8720   dw_attr_ref a;
8721   dw_die_ref c;
8722   unsigned long size;
8723   unsigned ix;
8724
8725   /* If someone in another CU might refer to us, set up a symbol for
8726      them to point to.  */
8727   if (die->die_symbol)
8728     output_die_symbol (die);
8729
8730   dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (0x%lx) %s)",
8731                                (unsigned long)die->die_offset,
8732                                dwarf_tag_name (die->die_tag));
8733
8734   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
8735     {
8736       const char *name = dwarf_attr_name (a->dw_attr);
8737
8738       switch (AT_class (a))
8739         {
8740         case dw_val_class_addr:
8741           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8742           break;
8743
8744         case dw_val_class_offset:
8745           dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8746                                "%s", name);
8747           break;
8748
8749         case dw_val_class_range_list:
8750           {
8751             char *p = strchr (ranges_section_label, '\0');
8752
8753             sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
8754                      a->dw_attr_val.v.val_offset);
8755             dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8756                                    debug_ranges_section, "%s", name);
8757             *p = '\0';
8758           }
8759           break;
8760
8761         case dw_val_class_loc:
8762           size = size_of_locs (AT_loc (a));
8763
8764           /* Output the block length for this list of location operations.  */
8765           dw2_asm_output_data (constant_size (size), size, "%s", name);
8766
8767           output_loc_sequence (AT_loc (a));
8768           break;
8769
8770         case dw_val_class_const:
8771           /* ??? It would be slightly more efficient to use a scheme like is
8772              used for unsigned constants below, but gdb 4.x does not sign
8773              extend.  Gdb 5.x does sign extend.  */
8774           dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8775           break;
8776
8777         case dw_val_class_unsigned_const:
8778           dw2_asm_output_data (constant_size (AT_unsigned (a)),
8779                                AT_unsigned (a), "%s", name);
8780           break;
8781
8782         case dw_val_class_long_long:
8783           {
8784             unsigned HOST_WIDE_INT first, second;
8785
8786             dw2_asm_output_data (1,
8787                                  2 * HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
8788                                  "%s", name);
8789
8790             if (WORDS_BIG_ENDIAN)
8791               {
8792                 first = a->dw_attr_val.v.val_long_long.hi;
8793                 second = a->dw_attr_val.v.val_long_long.low;
8794               }
8795             else
8796               {
8797                 first = a->dw_attr_val.v.val_long_long.low;
8798                 second = a->dw_attr_val.v.val_long_long.hi;
8799               }
8800
8801             dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
8802                                  first, "long long constant");
8803             dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
8804                                  second, NULL);
8805           }
8806           break;
8807
8808         case dw_val_class_vec:
8809           {
8810             unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
8811             unsigned int len = a->dw_attr_val.v.val_vec.length;
8812             unsigned int i;
8813             unsigned char *p;
8814
8815             dw2_asm_output_data (constant_size (len * elt_size),
8816                                  len * elt_size, "%s", name);
8817             if (elt_size > sizeof (HOST_WIDE_INT))
8818               {
8819                 elt_size /= 2;
8820                 len *= 2;
8821               }
8822             for (i = 0, p = a->dw_attr_val.v.val_vec.array;
8823                  i < len;
8824                  i++, p += elt_size)
8825               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
8826                                    "fp or vector constant word %u", i);
8827             break;
8828           }
8829
8830         case dw_val_class_flag:
8831           dw2_asm_output_data (1, AT_flag (a), "%s", name);
8832           break;
8833
8834         case dw_val_class_loc_list:
8835           {
8836             char *sym = AT_loc_list (a)->ll_symbol;
8837
8838             gcc_assert (sym);
8839             dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8840                                    "%s", name);
8841           }
8842           break;
8843
8844         case dw_val_class_die_ref:
8845           if (AT_ref_external (a))
8846             {
8847               char *sym = AT_ref (a)->die_symbol;
8848               int size;
8849
8850               gcc_assert (sym);
8851
8852               /* In DWARF2, DW_FORM_ref_addr is sized by target address
8853                  length, whereas in DWARF3 it's always sized as an offset.  */
8854               if (dwarf_version == 2)
8855                 size = DWARF2_ADDR_SIZE;
8856               else
8857                 size = DWARF_OFFSET_SIZE;
8858               dw2_asm_output_offset (size, sym, debug_info_section, "%s", name);
8859             }
8860           else
8861             {
8862               gcc_assert (AT_ref (a)->die_offset);
8863               dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
8864                                    "%s", name);
8865             }
8866           break;
8867
8868         case dw_val_class_fde_ref:
8869           {
8870             char l1[20];
8871
8872             ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
8873                                          a->dw_attr_val.v.val_fde_index * 2);
8874             dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
8875                                    "%s", name);
8876           }
8877           break;
8878
8879         case dw_val_class_lbl_id:
8880           dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8881           break;
8882
8883         case dw_val_class_lineptr:
8884           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8885                                  debug_line_section, "%s", name);
8886           break;
8887
8888         case dw_val_class_macptr:
8889           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8890                                  debug_macinfo_section, "%s", name);
8891           break;
8892
8893         case dw_val_class_str:
8894           if (AT_string_form (a) == DW_FORM_strp)
8895             dw2_asm_output_offset (DWARF_OFFSET_SIZE,
8896                                    a->dw_attr_val.v.val_str->label,
8897                                    debug_str_section,
8898                                    "%s: \"%s\"", name, AT_string (a));
8899           else
8900             dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
8901           break;
8902
8903         case dw_val_class_file:
8904           {
8905             int f = maybe_emit_file (a->dw_attr_val.v.val_file);
8906
8907             dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
8908                                  a->dw_attr_val.v.val_file->filename);
8909             break;
8910           }
8911
8912         default:
8913           gcc_unreachable ();
8914         }
8915     }
8916
8917   FOR_EACH_CHILD (die, c, output_die (c));
8918
8919   /* Add null byte to terminate sibling list.  */
8920   if (die->die_child != NULL)
8921     dw2_asm_output_data (1, 0, "end of children of DIE 0x%lx",
8922                          (unsigned long) die->die_offset);
8923 }
8924
8925 /* Output the compilation unit that appears at the beginning of the
8926    .debug_info section, and precedes the DIE descriptions.  */
8927
8928 static void
8929 output_compilation_unit_header (void)
8930 {
8931   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8932     dw2_asm_output_data (4, 0xffffffff,
8933       "Initial length escape value indicating 64-bit DWARF extension");
8934   dw2_asm_output_data (DWARF_OFFSET_SIZE,
8935                        next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
8936                        "Length of Compilation Unit Info");
8937   dw2_asm_output_data (2, dwarf_version, "DWARF version number");
8938   dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
8939                          debug_abbrev_section,
8940                          "Offset Into Abbrev. Section");
8941   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8942 }
8943
8944 /* Output the compilation unit DIE and its children.  */
8945
8946 static void
8947 output_comp_unit (dw_die_ref die, int output_if_empty)
8948 {
8949   const char *secname;
8950   char *oldsym, *tmp;
8951
8952   /* Unless we are outputting main CU, we may throw away empty ones.  */
8953   if (!output_if_empty && die->die_child == NULL)
8954     return;
8955
8956   /* Even if there are no children of this DIE, we must output the information
8957      about the compilation unit.  Otherwise, on an empty translation unit, we
8958      will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
8959      will then complain when examining the file.  First mark all the DIEs in
8960      this CU so we know which get local refs.  */
8961   mark_dies (die);
8962
8963   build_abbrev_table (die);
8964
8965   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
8966   next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8967   calc_die_sizes (die);
8968
8969   oldsym = die->die_symbol;
8970   if (oldsym)
8971     {
8972       tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
8973
8974       sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
8975       secname = tmp;
8976       die->die_symbol = NULL;
8977       switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
8978     }
8979   else
8980     switch_to_section (debug_info_section);
8981
8982   /* Output debugging information.  */
8983   output_compilation_unit_header ();
8984   output_die (die);
8985
8986   /* Leave the marks on the main CU, so we can check them in
8987      output_pubnames.  */
8988   if (oldsym)
8989     {
8990       unmark_dies (die);
8991       die->die_symbol = oldsym;
8992     }
8993 }
8994
8995 /* Return the DWARF2/3 pubname associated with a decl.  */
8996
8997 static const char *
8998 dwarf2_name (tree decl, int scope)
8999 {
9000   return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
9001 }
9002
9003 /* Add a new entry to .debug_pubnames if appropriate.  */
9004
9005 static void
9006 add_pubname_string (const char *str, dw_die_ref die)
9007 {
9008   pubname_entry e;
9009
9010   e.die = die;
9011   e.name = xstrdup (str);
9012   VEC_safe_push (pubname_entry, gc, pubname_table, &e);
9013 }
9014
9015 static void
9016 add_pubname (tree decl, dw_die_ref die)
9017 {
9018   if (TREE_PUBLIC (decl))
9019     add_pubname_string (dwarf2_name (decl, 1), die);
9020 }
9021
9022 /* Add a new entry to .debug_pubtypes if appropriate.  */
9023
9024 static void
9025 add_pubtype (tree decl, dw_die_ref die)
9026 {
9027   pubname_entry e;
9028
9029   e.name = NULL;
9030   if ((TREE_PUBLIC (decl)
9031        || die->die_parent == comp_unit_die)
9032       && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
9033     {
9034       e.die = die;
9035       if (TYPE_P (decl))
9036         {
9037           if (TYPE_NAME (decl))
9038             {
9039               if (TREE_CODE (TYPE_NAME (decl)) == IDENTIFIER_NODE)
9040                 e.name = IDENTIFIER_POINTER (TYPE_NAME (decl));
9041               else if (TREE_CODE (TYPE_NAME (decl)) == TYPE_DECL
9042                        && DECL_NAME (TYPE_NAME (decl)))
9043                 e.name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (decl)));
9044               else
9045                e.name = xstrdup ((const char *) get_AT_string (die, DW_AT_name));
9046             }
9047         }
9048       else
9049         e.name = xstrdup (dwarf2_name (decl, 1));
9050
9051       /* If we don't have a name for the type, there's no point in adding
9052          it to the table.  */
9053       if (e.name && e.name[0] != '\0')
9054         VEC_safe_push (pubname_entry, gc, pubtype_table, &e);
9055     }
9056 }
9057
9058 /* Output the public names table used to speed up access to externally
9059    visible names; or the public types table used to find type definitions.  */
9060
9061 static void
9062 output_pubnames (VEC (pubname_entry, gc) * names)
9063 {
9064   unsigned i;
9065   unsigned long pubnames_length = size_of_pubnames (names);
9066   pubname_ref pub;
9067
9068   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9069     dw2_asm_output_data (4, 0xffffffff,
9070       "Initial length escape value indicating 64-bit DWARF extension");
9071   if (names == pubname_table)
9072     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
9073                          "Length of Public Names Info");
9074   else
9075     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
9076                          "Length of Public Type Names Info");
9077   /* Version number for pubnames/pubtypes is still 2, even in DWARF3.  */
9078   dw2_asm_output_data (2, 2, "DWARF Version");
9079   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9080                          debug_info_section,
9081                          "Offset of Compilation Unit Info");
9082   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
9083                        "Compilation Unit Length");
9084
9085   for (i = 0; VEC_iterate (pubname_entry, names, i, pub); i++)
9086     {
9087       /* We shouldn't see pubnames for DIEs outside of the main CU.  */
9088       if (names == pubname_table)
9089         gcc_assert (pub->die->die_mark);
9090
9091       if (names != pubtype_table
9092           || pub->die->die_offset != 0
9093           || !flag_eliminate_unused_debug_types)
9094         {
9095           dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
9096                                "DIE offset");
9097
9098           dw2_asm_output_nstring (pub->name, -1, "external name");
9099         }
9100     }
9101
9102   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
9103 }
9104
9105 /* Add a new entry to .debug_aranges if appropriate.  */
9106
9107 static void
9108 add_arange (tree decl, dw_die_ref die)
9109 {
9110   if (! DECL_SECTION_NAME (decl))
9111     return;
9112
9113   if (arange_table_in_use == arange_table_allocated)
9114     {
9115       arange_table_allocated += ARANGE_TABLE_INCREMENT;
9116       arange_table = GGC_RESIZEVEC (dw_die_ref, arange_table,
9117                                     arange_table_allocated);
9118       memset (arange_table + arange_table_in_use, 0,
9119               ARANGE_TABLE_INCREMENT * sizeof (dw_die_ref));
9120     }
9121
9122   arange_table[arange_table_in_use++] = die;
9123 }
9124
9125 /* Output the information that goes into the .debug_aranges table.
9126    Namely, define the beginning and ending address range of the
9127    text section generated for this compilation unit.  */
9128
9129 static void
9130 output_aranges (void)
9131 {
9132   unsigned i;
9133   unsigned long aranges_length = size_of_aranges ();
9134
9135   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9136     dw2_asm_output_data (4, 0xffffffff,
9137       "Initial length escape value indicating 64-bit DWARF extension");
9138   dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
9139                        "Length of Address Ranges Info");
9140   /* Version number for aranges is still 2, even in DWARF3.  */
9141   dw2_asm_output_data (2, 2, "DWARF Version");
9142   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9143                          debug_info_section,
9144                          "Offset of Compilation Unit Info");
9145   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
9146   dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
9147
9148   /* We need to align to twice the pointer size here.  */
9149   if (DWARF_ARANGES_PAD_SIZE)
9150     {
9151       /* Pad using a 2 byte words so that padding is correct for any
9152          pointer size.  */
9153       dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
9154                            2 * DWARF2_ADDR_SIZE);
9155       for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
9156         dw2_asm_output_data (2, 0, NULL);
9157     }
9158
9159   /* It is necessary not to output these entries if the sections were
9160      not used; if the sections were not used, the length will be 0 and
9161      the address may end up as 0 if the section is discarded by ld
9162      --gc-sections, leaving an invalid (0, 0) entry that can be
9163      confused with the terminator.  */
9164   if (text_section_used)
9165     {
9166       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
9167       dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
9168                             text_section_label, "Length");
9169     }
9170   if (cold_text_section_used)
9171     {
9172       dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
9173                            "Address");
9174       dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
9175                             cold_text_section_label, "Length");
9176     }
9177
9178   for (i = 0; i < arange_table_in_use; i++)
9179     {
9180       dw_die_ref die = arange_table[i];
9181
9182       /* We shouldn't see aranges for DIEs outside of the main CU.  */
9183       gcc_assert (die->die_mark);
9184
9185       if (die->die_tag == DW_TAG_subprogram)
9186         {
9187           dw2_asm_output_addr (DWARF2_ADDR_SIZE, get_AT_low_pc (die),
9188                                "Address");
9189           dw2_asm_output_delta (DWARF2_ADDR_SIZE, get_AT_hi_pc (die),
9190                                 get_AT_low_pc (die), "Length");
9191         }
9192       else
9193         {
9194           /* A static variable; extract the symbol from DW_AT_location.
9195              Note that this code isn't currently hit, as we only emit
9196              aranges for functions (jason 9/23/99).  */
9197           dw_attr_ref a = get_AT (die, DW_AT_location);
9198           dw_loc_descr_ref loc;
9199
9200           gcc_assert (a && AT_class (a) == dw_val_class_loc);
9201
9202           loc = AT_loc (a);
9203           gcc_assert (loc->dw_loc_opc == DW_OP_addr);
9204
9205           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE,
9206                                    loc->dw_loc_oprnd1.v.val_addr, "Address");
9207           dw2_asm_output_data (DWARF2_ADDR_SIZE,
9208                                get_AT_unsigned (die, DW_AT_byte_size),
9209                                "Length");
9210         }
9211     }
9212
9213   /* Output the terminator words.  */
9214   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9215   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9216 }
9217
9218 /* Add a new entry to .debug_ranges.  Return the offset at which it
9219    was placed.  */
9220
9221 static unsigned int
9222 add_ranges_num (int num)
9223 {
9224   unsigned int in_use = ranges_table_in_use;
9225
9226   if (in_use == ranges_table_allocated)
9227     {
9228       ranges_table_allocated += RANGES_TABLE_INCREMENT;
9229       ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
9230                                     ranges_table_allocated);
9231       memset (ranges_table + ranges_table_in_use, 0,
9232               RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
9233     }
9234
9235   ranges_table[in_use].num = num;
9236   ranges_table_in_use = in_use + 1;
9237
9238   return in_use * 2 * DWARF2_ADDR_SIZE;
9239 }
9240
9241 /* Add a new entry to .debug_ranges corresponding to a block, or a
9242    range terminator if BLOCK is NULL.  */
9243
9244 static unsigned int
9245 add_ranges (const_tree block)
9246 {
9247   return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
9248 }
9249
9250 /* Add a new entry to .debug_ranges corresponding to a pair of
9251    labels.  */
9252
9253 static unsigned int
9254 add_ranges_by_labels (const char *begin, const char *end)
9255 {
9256   unsigned int in_use = ranges_by_label_in_use;
9257
9258   if (in_use == ranges_by_label_allocated)
9259     {
9260       ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
9261       ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
9262                                        ranges_by_label,
9263                                        ranges_by_label_allocated);
9264       memset (ranges_by_label + ranges_by_label_in_use, 0,
9265               RANGES_TABLE_INCREMENT
9266               * sizeof (struct dw_ranges_by_label_struct));
9267     }
9268
9269   ranges_by_label[in_use].begin = begin;
9270   ranges_by_label[in_use].end = end;
9271   ranges_by_label_in_use = in_use + 1;
9272
9273   return add_ranges_num (-(int)in_use - 1);
9274 }
9275
9276 static void
9277 output_ranges (void)
9278 {
9279   unsigned i;
9280   static const char *const start_fmt = "Offset 0x%x";
9281   const char *fmt = start_fmt;
9282
9283   for (i = 0; i < ranges_table_in_use; i++)
9284     {
9285       int block_num = ranges_table[i].num;
9286
9287       if (block_num > 0)
9288         {
9289           char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
9290           char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
9291
9292           ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
9293           ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
9294
9295           /* If all code is in the text section, then the compilation
9296              unit base address defaults to DW_AT_low_pc, which is the
9297              base of the text section.  */
9298           if (!have_multiple_function_sections)
9299             {
9300               dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
9301                                     text_section_label,
9302                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
9303               dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
9304                                     text_section_label, NULL);
9305             }
9306
9307           /* Otherwise, the compilation unit base address is zero,
9308              which allows us to use absolute addresses, and not worry
9309              about whether the target supports cross-section
9310              arithmetic.  */
9311           else
9312             {
9313               dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
9314                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
9315               dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
9316             }
9317
9318           fmt = NULL;
9319         }
9320
9321       /* Negative block_num stands for an index into ranges_by_label.  */
9322       else if (block_num < 0)
9323         {
9324           int lab_idx = - block_num - 1;
9325
9326           if (!have_multiple_function_sections)
9327             {
9328               gcc_unreachable ();
9329 #if 0
9330               /* If we ever use add_ranges_by_labels () for a single
9331                  function section, all we have to do is to take out
9332                  the #if 0 above.  */
9333               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9334                                     ranges_by_label[lab_idx].begin,
9335                                     text_section_label,
9336                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
9337               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9338                                     ranges_by_label[lab_idx].end,
9339                                     text_section_label, NULL);
9340 #endif
9341             }
9342           else
9343             {
9344               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9345                                    ranges_by_label[lab_idx].begin,
9346                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
9347               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9348                                    ranges_by_label[lab_idx].end,
9349                                    NULL);
9350             }
9351         }
9352       else
9353         {
9354           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9355           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9356           fmt = start_fmt;
9357         }
9358     }
9359 }
9360
9361 /* Data structure containing information about input files.  */
9362 struct file_info
9363 {
9364   const char *path;     /* Complete file name.  */
9365   const char *fname;    /* File name part.  */
9366   int length;           /* Length of entire string.  */
9367   struct dwarf_file_data * file_idx;    /* Index in input file table.  */
9368   int dir_idx;          /* Index in directory table.  */
9369 };
9370
9371 /* Data structure containing information about directories with source
9372    files.  */
9373 struct dir_info
9374 {
9375   const char *path;     /* Path including directory name.  */
9376   int length;           /* Path length.  */
9377   int prefix;           /* Index of directory entry which is a prefix.  */
9378   int count;            /* Number of files in this directory.  */
9379   int dir_idx;          /* Index of directory used as base.  */
9380 };
9381
9382 /* Callback function for file_info comparison.  We sort by looking at
9383    the directories in the path.  */
9384
9385 static int
9386 file_info_cmp (const void *p1, const void *p2)
9387 {
9388   const struct file_info *const s1 = (const struct file_info *) p1;
9389   const struct file_info *const s2 = (const struct file_info *) p2;
9390   const unsigned char *cp1;
9391   const unsigned char *cp2;
9392
9393   /* Take care of file names without directories.  We need to make sure that
9394      we return consistent values to qsort since some will get confused if
9395      we return the same value when identical operands are passed in opposite
9396      orders.  So if neither has a directory, return 0 and otherwise return
9397      1 or -1 depending on which one has the directory.  */
9398   if ((s1->path == s1->fname || s2->path == s2->fname))
9399     return (s2->path == s2->fname) - (s1->path == s1->fname);
9400
9401   cp1 = (const unsigned char *) s1->path;
9402   cp2 = (const unsigned char *) s2->path;
9403
9404   while (1)
9405     {
9406       ++cp1;
9407       ++cp2;
9408       /* Reached the end of the first path?  If so, handle like above.  */
9409       if ((cp1 == (const unsigned char *) s1->fname)
9410           || (cp2 == (const unsigned char *) s2->fname))
9411         return ((cp2 == (const unsigned char *) s2->fname)
9412                 - (cp1 == (const unsigned char *) s1->fname));
9413
9414       /* Character of current path component the same?  */
9415       else if (*cp1 != *cp2)
9416         return *cp1 - *cp2;
9417     }
9418 }
9419
9420 struct file_name_acquire_data
9421 {
9422   struct file_info *files;
9423   int used_files;
9424   int max_files;
9425 };
9426
9427 /* Traversal function for the hash table.  */
9428
9429 static int
9430 file_name_acquire (void ** slot, void *data)
9431 {
9432   struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
9433   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
9434   struct file_info *fi;
9435   const char *f;
9436
9437   gcc_assert (fnad->max_files >= d->emitted_number);
9438
9439   if (! d->emitted_number)
9440     return 1;
9441
9442   gcc_assert (fnad->max_files != fnad->used_files);
9443
9444   fi = fnad->files + fnad->used_files++;
9445
9446   /* Skip all leading "./".  */
9447   f = d->filename;
9448   while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
9449     f += 2;
9450
9451   /* Create a new array entry.  */
9452   fi->path = f;
9453   fi->length = strlen (f);
9454   fi->file_idx = d;
9455
9456   /* Search for the file name part.  */
9457   f = strrchr (f, DIR_SEPARATOR);
9458 #if defined (DIR_SEPARATOR_2)
9459   {
9460     char *g = strrchr (fi->path, DIR_SEPARATOR_2);
9461
9462     if (g != NULL)
9463       {
9464         if (f == NULL || f < g)
9465           f = g;
9466       }
9467   }
9468 #endif
9469
9470   fi->fname = f == NULL ? fi->path : f + 1;
9471   return 1;
9472 }
9473
9474 /* Output the directory table and the file name table.  We try to minimize
9475    the total amount of memory needed.  A heuristic is used to avoid large
9476    slowdowns with many input files.  */
9477
9478 static void
9479 output_file_names (void)
9480 {
9481   struct file_name_acquire_data fnad;
9482   int numfiles;
9483   struct file_info *files;
9484   struct dir_info *dirs;
9485   int *saved;
9486   int *savehere;
9487   int *backmap;
9488   int ndirs;
9489   int idx_offset;
9490   int i;
9491   int idx;
9492
9493   if (!last_emitted_file)
9494     {
9495       dw2_asm_output_data (1, 0, "End directory table");
9496       dw2_asm_output_data (1, 0, "End file name table");
9497       return;
9498     }
9499
9500   numfiles = last_emitted_file->emitted_number;
9501
9502   /* Allocate the various arrays we need.  */
9503   files = XALLOCAVEC (struct file_info, numfiles);
9504   dirs = XALLOCAVEC (struct dir_info, numfiles);
9505
9506   fnad.files = files;
9507   fnad.used_files = 0;
9508   fnad.max_files = numfiles;
9509   htab_traverse (file_table, file_name_acquire, &fnad);
9510   gcc_assert (fnad.used_files == fnad.max_files);
9511
9512   qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
9513
9514   /* Find all the different directories used.  */
9515   dirs[0].path = files[0].path;
9516   dirs[0].length = files[0].fname - files[0].path;
9517   dirs[0].prefix = -1;
9518   dirs[0].count = 1;
9519   dirs[0].dir_idx = 0;
9520   files[0].dir_idx = 0;
9521   ndirs = 1;
9522
9523   for (i = 1; i < numfiles; i++)
9524     if (files[i].fname - files[i].path == dirs[ndirs - 1].length
9525         && memcmp (dirs[ndirs - 1].path, files[i].path,
9526                    dirs[ndirs - 1].length) == 0)
9527       {
9528         /* Same directory as last entry.  */
9529         files[i].dir_idx = ndirs - 1;
9530         ++dirs[ndirs - 1].count;
9531       }
9532     else
9533       {
9534         int j;
9535
9536         /* This is a new directory.  */
9537         dirs[ndirs].path = files[i].path;
9538         dirs[ndirs].length = files[i].fname - files[i].path;
9539         dirs[ndirs].count = 1;
9540         dirs[ndirs].dir_idx = ndirs;
9541         files[i].dir_idx = ndirs;
9542
9543         /* Search for a prefix.  */
9544         dirs[ndirs].prefix = -1;
9545         for (j = 0; j < ndirs; j++)
9546           if (dirs[j].length < dirs[ndirs].length
9547               && dirs[j].length > 1
9548               && (dirs[ndirs].prefix == -1
9549                   || dirs[j].length > dirs[dirs[ndirs].prefix].length)
9550               && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
9551             dirs[ndirs].prefix = j;
9552
9553         ++ndirs;
9554       }
9555
9556   /* Now to the actual work.  We have to find a subset of the directories which
9557      allow expressing the file name using references to the directory table
9558      with the least amount of characters.  We do not do an exhaustive search
9559      where we would have to check out every combination of every single
9560      possible prefix.  Instead we use a heuristic which provides nearly optimal
9561      results in most cases and never is much off.  */
9562   saved = XALLOCAVEC (int, ndirs);
9563   savehere = XALLOCAVEC (int, ndirs);
9564
9565   memset (saved, '\0', ndirs * sizeof (saved[0]));
9566   for (i = 0; i < ndirs; i++)
9567     {
9568       int j;
9569       int total;
9570
9571       /* We can always save some space for the current directory.  But this
9572          does not mean it will be enough to justify adding the directory.  */
9573       savehere[i] = dirs[i].length;
9574       total = (savehere[i] - saved[i]) * dirs[i].count;
9575
9576       for (j = i + 1; j < ndirs; j++)
9577         {
9578           savehere[j] = 0;
9579           if (saved[j] < dirs[i].length)
9580             {
9581               /* Determine whether the dirs[i] path is a prefix of the
9582                  dirs[j] path.  */
9583               int k;
9584
9585               k = dirs[j].prefix;
9586               while (k != -1 && k != (int) i)
9587                 k = dirs[k].prefix;
9588
9589               if (k == (int) i)
9590                 {
9591                   /* Yes it is.  We can possibly save some memory by
9592                      writing the filenames in dirs[j] relative to
9593                      dirs[i].  */
9594                   savehere[j] = dirs[i].length;
9595                   total += (savehere[j] - saved[j]) * dirs[j].count;
9596                 }
9597             }
9598         }
9599
9600       /* Check whether we can save enough to justify adding the dirs[i]
9601          directory.  */
9602       if (total > dirs[i].length + 1)
9603         {
9604           /* It's worthwhile adding.  */
9605           for (j = i; j < ndirs; j++)
9606             if (savehere[j] > 0)
9607               {
9608                 /* Remember how much we saved for this directory so far.  */
9609                 saved[j] = savehere[j];
9610
9611                 /* Remember the prefix directory.  */
9612                 dirs[j].dir_idx = i;
9613               }
9614         }
9615     }
9616
9617   /* Emit the directory name table.  */
9618   idx = 1;
9619   idx_offset = dirs[0].length > 0 ? 1 : 0;
9620   for (i = 1 - idx_offset; i < ndirs; i++)
9621     dw2_asm_output_nstring (dirs[i].path,
9622                             dirs[i].length
9623                              - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
9624                             "Directory Entry: 0x%x", i + idx_offset);
9625
9626   dw2_asm_output_data (1, 0, "End directory table");
9627
9628   /* We have to emit them in the order of emitted_number since that's
9629      used in the debug info generation.  To do this efficiently we
9630      generate a back-mapping of the indices first.  */
9631   backmap = XALLOCAVEC (int, numfiles);
9632   for (i = 0; i < numfiles; i++)
9633     backmap[files[i].file_idx->emitted_number - 1] = i;
9634
9635   /* Now write all the file names.  */
9636   for (i = 0; i < numfiles; i++)
9637     {
9638       int file_idx = backmap[i];
9639       int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
9640
9641 #ifdef VMS_DEBUGGING_INFO
9642 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
9643
9644       /* Setting these fields can lead to debugger miscomparisons,
9645          but VMS Debug requires them to be set correctly.  */
9646
9647       int ver;
9648       long long cdt;
9649       long siz;
9650       int maxfilelen = strlen (files[file_idx].path)
9651                                + dirs[dir_idx].length
9652                                + MAX_VMS_VERSION_LEN + 1;
9653       char *filebuf = XALLOCAVEC (char, maxfilelen);
9654
9655       vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
9656       snprintf (filebuf, maxfilelen, "%s;%d",
9657                 files[file_idx].path + dirs[dir_idx].length, ver);
9658
9659       dw2_asm_output_nstring
9660         (filebuf, -1, "File Entry: 0x%x", (unsigned) i + 1);
9661
9662       /* Include directory index.  */
9663       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9664
9665       /* Modification time.  */
9666       dw2_asm_output_data_uleb128
9667         ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
9668           ? cdt : 0,
9669          NULL);
9670
9671       /* File length in bytes.  */
9672       dw2_asm_output_data_uleb128
9673         ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
9674           ? siz : 0,
9675          NULL);
9676 #else
9677       dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
9678                               "File Entry: 0x%x", (unsigned) i + 1);
9679
9680       /* Include directory index.  */
9681       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9682
9683       /* Modification time.  */
9684       dw2_asm_output_data_uleb128 (0, NULL);
9685
9686       /* File length in bytes.  */
9687       dw2_asm_output_data_uleb128 (0, NULL);
9688 #endif
9689     }
9690
9691   dw2_asm_output_data (1, 0, "End file name table");
9692 }
9693
9694
9695 /* Output the source line number correspondence information.  This
9696    information goes into the .debug_line section.  */
9697
9698 static void
9699 output_line_info (void)
9700 {
9701   char l1[20], l2[20], p1[20], p2[20];
9702   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
9703   char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
9704   unsigned opc;
9705   unsigned n_op_args;
9706   unsigned long lt_index;
9707   unsigned long current_line;
9708   long line_offset;
9709   long line_delta;
9710   unsigned long current_file;
9711   unsigned long function;
9712
9713   ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
9714   ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
9715   ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
9716   ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
9717
9718   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9719     dw2_asm_output_data (4, 0xffffffff,
9720       "Initial length escape value indicating 64-bit DWARF extension");
9721   dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
9722                         "Length of Source Line Info");
9723   ASM_OUTPUT_LABEL (asm_out_file, l1);
9724
9725   dw2_asm_output_data (2, dwarf_version, "DWARF Version");
9726   dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
9727   ASM_OUTPUT_LABEL (asm_out_file, p1);
9728
9729   /* Define the architecture-dependent minimum instruction length (in
9730    bytes).  In this implementation of DWARF, this field is used for
9731    information purposes only.  Since GCC generates assembly language,
9732    we have no a priori knowledge of how many instruction bytes are
9733    generated for each source line, and therefore can use only the
9734    DW_LNE_set_address and DW_LNS_fixed_advance_pc line information
9735    commands.  Accordingly, we fix this as `1', which is "correct
9736    enough" for all architectures, and don't let the target override.  */
9737   dw2_asm_output_data (1, 1,
9738                        "Minimum Instruction Length");
9739
9740   dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
9741                        "Default is_stmt_start flag");
9742   dw2_asm_output_data (1, DWARF_LINE_BASE,
9743                        "Line Base Value (Special Opcodes)");
9744   dw2_asm_output_data (1, DWARF_LINE_RANGE,
9745                        "Line Range Value (Special Opcodes)");
9746   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
9747                        "Special Opcode Base");
9748
9749   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
9750     {
9751       switch (opc)
9752         {
9753         case DW_LNS_advance_pc:
9754         case DW_LNS_advance_line:
9755         case DW_LNS_set_file:
9756         case DW_LNS_set_column:
9757         case DW_LNS_fixed_advance_pc:
9758           n_op_args = 1;
9759           break;
9760         default:
9761           n_op_args = 0;
9762           break;
9763         }
9764
9765       dw2_asm_output_data (1, n_op_args, "opcode: 0x%x has %d args",
9766                            opc, n_op_args);
9767     }
9768
9769   /* Write out the information about the files we use.  */
9770   output_file_names ();
9771   ASM_OUTPUT_LABEL (asm_out_file, p2);
9772
9773   /* We used to set the address register to the first location in the text
9774      section here, but that didn't accomplish anything since we already
9775      have a line note for the opening brace of the first function.  */
9776
9777   /* Generate the line number to PC correspondence table, encoded as
9778      a series of state machine operations.  */
9779   current_file = 1;
9780   current_line = 1;
9781
9782   if (cfun && in_cold_section_p)
9783     strcpy (prev_line_label, crtl->subsections.cold_section_label);
9784   else
9785     strcpy (prev_line_label, text_section_label);
9786   for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
9787     {
9788       dw_line_info_ref line_info = &line_info_table[lt_index];
9789
9790 #if 0
9791       /* Disable this optimization for now; GDB wants to see two line notes
9792          at the beginning of a function so it can find the end of the
9793          prologue.  */
9794
9795       /* Don't emit anything for redundant notes.  Just updating the
9796          address doesn't accomplish anything, because we already assume
9797          that anything after the last address is this line.  */
9798       if (line_info->dw_line_num == current_line
9799           && line_info->dw_file_num == current_file)
9800         continue;
9801 #endif
9802
9803       /* Emit debug info for the address of the current line.
9804
9805          Unfortunately, we have little choice here currently, and must always
9806          use the most general form.  GCC does not know the address delta
9807          itself, so we can't use DW_LNS_advance_pc.  Many ports do have length
9808          attributes which will give an upper bound on the address range.  We
9809          could perhaps use length attributes to determine when it is safe to
9810          use DW_LNS_fixed_advance_pc.  */
9811
9812       ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
9813       if (0)
9814         {
9815           /* This can handle deltas up to 0xffff.  This takes 3 bytes.  */
9816           dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
9817                                "DW_LNS_fixed_advance_pc");
9818           dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
9819         }
9820       else
9821         {
9822           /* This can handle any delta.  This takes
9823              4+DWARF2_ADDR_SIZE bytes.  */
9824           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9825           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9826           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9827           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9828         }
9829
9830       strcpy (prev_line_label, line_label);
9831
9832       /* Emit debug info for the source file of the current line, if
9833          different from the previous line.  */
9834       if (line_info->dw_file_num != current_file)
9835         {
9836           current_file = line_info->dw_file_num;
9837           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
9838           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
9839         }
9840
9841       /* Emit debug info for the current line number, choosing the encoding
9842          that uses the least amount of space.  */
9843       if (line_info->dw_line_num != current_line)
9844         {
9845           line_offset = line_info->dw_line_num - current_line;
9846           line_delta = line_offset - DWARF_LINE_BASE;
9847           current_line = line_info->dw_line_num;
9848           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
9849             /* This can handle deltas from -10 to 234, using the current
9850                definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.  This
9851                takes 1 byte.  */
9852             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
9853                                  "line %lu", current_line);
9854           else
9855             {
9856               /* This can handle any delta.  This takes at least 4 bytes,
9857                  depending on the value being encoded.  */
9858               dw2_asm_output_data (1, DW_LNS_advance_line,
9859                                    "advance to line %lu", current_line);
9860               dw2_asm_output_data_sleb128 (line_offset, NULL);
9861               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
9862             }
9863         }
9864       else
9865         /* We still need to start a new row, so output a copy insn.  */
9866         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
9867     }
9868
9869   /* Emit debug info for the address of the end of the function.  */
9870   if (0)
9871     {
9872       dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
9873                            "DW_LNS_fixed_advance_pc");
9874       dw2_asm_output_delta (2, text_end_label, prev_line_label, NULL);
9875     }
9876   else
9877     {
9878       dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9879       dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9880       dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9881       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_end_label, NULL);
9882     }
9883
9884   dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
9885   dw2_asm_output_data_uleb128 (1, NULL);
9886   dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
9887
9888   function = 0;
9889   current_file = 1;
9890   current_line = 1;
9891   for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
9892     {
9893       dw_separate_line_info_ref line_info
9894         = &separate_line_info_table[lt_index];
9895
9896 #if 0
9897       /* Don't emit anything for redundant notes.  */
9898       if (line_info->dw_line_num == current_line
9899           && line_info->dw_file_num == current_file
9900           && line_info->function == function)
9901         goto cont;
9902 #endif
9903
9904       /* Emit debug info for the address of the current line.  If this is
9905          a new function, or the first line of a function, then we need
9906          to handle it differently.  */
9907       ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
9908                                    lt_index);
9909       if (function != line_info->function)
9910         {
9911           function = line_info->function;
9912
9913           /* Set the address register to the first line in the function.  */
9914           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9915           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9916           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9917           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9918         }
9919       else
9920         {
9921           /* ??? See the DW_LNS_advance_pc comment above.  */
9922           if (0)
9923             {
9924               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
9925                                    "DW_LNS_fixed_advance_pc");
9926               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
9927             }
9928           else
9929             {
9930               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9931               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9932               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9933               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9934             }
9935         }
9936
9937       strcpy (prev_line_label, line_label);
9938
9939       /* Emit debug info for the source file of the current line, if
9940          different from the previous line.  */
9941       if (line_info->dw_file_num != current_file)
9942         {
9943           current_file = line_info->dw_file_num;
9944           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
9945           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
9946         }
9947
9948       /* Emit debug info for the current line number, choosing the encoding
9949          that uses the least amount of space.  */
9950       if (line_info->dw_line_num != current_line)
9951         {
9952           line_offset = line_info->dw_line_num - current_line;
9953           line_delta = line_offset - DWARF_LINE_BASE;
9954           current_line = line_info->dw_line_num;
9955           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
9956             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
9957                                  "line %lu", current_line);
9958           else
9959             {
9960               dw2_asm_output_data (1, DW_LNS_advance_line,
9961                                    "advance to line %lu", current_line);
9962               dw2_asm_output_data_sleb128 (line_offset, NULL);
9963               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
9964             }
9965         }
9966       else
9967         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
9968
9969 #if 0
9970     cont:
9971 #endif
9972
9973       lt_index++;
9974
9975       /* If we're done with a function, end its sequence.  */
9976       if (lt_index == separate_line_info_table_in_use
9977           || separate_line_info_table[lt_index].function != function)
9978         {
9979           current_file = 1;
9980           current_line = 1;
9981
9982           /* Emit debug info for the address of the end of the function.  */
9983           ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
9984           if (0)
9985             {
9986               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
9987                                    "DW_LNS_fixed_advance_pc");
9988               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
9989             }
9990           else
9991             {
9992               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9993               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9994               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9995               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9996             }
9997
9998           /* Output the marker for the end of this sequence.  */
9999           dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
10000           dw2_asm_output_data_uleb128 (1, NULL);
10001           dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
10002         }
10003     }
10004
10005   /* Output the marker for the end of the line number info.  */
10006   ASM_OUTPUT_LABEL (asm_out_file, l2);
10007 }
10008 \f
10009 /* Given a pointer to a tree node for some base type, return a pointer to
10010    a DIE that describes the given type.
10011
10012    This routine must only be called for GCC type nodes that correspond to
10013    Dwarf base (fundamental) types.  */
10014
10015 static dw_die_ref
10016 base_type_die (tree type)
10017 {
10018   dw_die_ref base_type_result;
10019   enum dwarf_type encoding;
10020
10021   if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
10022     return 0;
10023
10024   /* If this is a subtype that should not be emitted as a subrange type,
10025      use the base type.  See subrange_type_for_debug_p.  */
10026   if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
10027     type = TREE_TYPE (type);
10028
10029   switch (TREE_CODE (type))
10030     {
10031     case INTEGER_TYPE:
10032       if (TYPE_STRING_FLAG (type))
10033         {
10034           if (TYPE_UNSIGNED (type))
10035             encoding = DW_ATE_unsigned_char;
10036           else
10037             encoding = DW_ATE_signed_char;
10038         }
10039       else if (TYPE_UNSIGNED (type))
10040         encoding = DW_ATE_unsigned;
10041       else
10042         encoding = DW_ATE_signed;
10043       break;
10044
10045     case REAL_TYPE:
10046       if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
10047         encoding = DW_ATE_decimal_float;
10048       else
10049         encoding = DW_ATE_float;
10050       break;
10051
10052     case FIXED_POINT_TYPE:
10053       if (TYPE_UNSIGNED (type))
10054         encoding = DW_ATE_unsigned_fixed;
10055       else
10056         encoding = DW_ATE_signed_fixed;
10057       break;
10058
10059       /* Dwarf2 doesn't know anything about complex ints, so use
10060          a user defined type for it.  */
10061     case COMPLEX_TYPE:
10062       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
10063         encoding = DW_ATE_complex_float;
10064       else
10065         encoding = DW_ATE_lo_user;
10066       break;
10067
10068     case BOOLEAN_TYPE:
10069       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
10070       encoding = DW_ATE_boolean;
10071       break;
10072
10073     default:
10074       /* No other TREE_CODEs are Dwarf fundamental types.  */
10075       gcc_unreachable ();
10076     }
10077
10078   base_type_result = new_die (DW_TAG_base_type, comp_unit_die, type);
10079
10080   /* This probably indicates a bug.  */
10081   if (! TYPE_NAME (type))
10082     add_name_attribute (base_type_result, "__unknown__");
10083
10084   add_AT_unsigned (base_type_result, DW_AT_byte_size,
10085                    int_size_in_bytes (type));
10086   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
10087
10088   return base_type_result;
10089 }
10090
10091 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
10092    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
10093
10094 static inline int
10095 is_base_type (tree type)
10096 {
10097   switch (TREE_CODE (type))
10098     {
10099     case ERROR_MARK:
10100     case VOID_TYPE:
10101     case INTEGER_TYPE:
10102     case REAL_TYPE:
10103     case FIXED_POINT_TYPE:
10104     case COMPLEX_TYPE:
10105     case BOOLEAN_TYPE:
10106       return 1;
10107
10108     case ARRAY_TYPE:
10109     case RECORD_TYPE:
10110     case UNION_TYPE:
10111     case QUAL_UNION_TYPE:
10112     case ENUMERAL_TYPE:
10113     case FUNCTION_TYPE:
10114     case METHOD_TYPE:
10115     case POINTER_TYPE:
10116     case REFERENCE_TYPE:
10117     case OFFSET_TYPE:
10118     case LANG_TYPE:
10119     case VECTOR_TYPE:
10120       return 0;
10121
10122     default:
10123       gcc_unreachable ();
10124     }
10125
10126   return 0;
10127 }
10128
10129 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
10130    node, return the size in bits for the type if it is a constant, or else
10131    return the alignment for the type if the type's size is not constant, or
10132    else return BITS_PER_WORD if the type actually turns out to be an
10133    ERROR_MARK node.  */
10134
10135 static inline unsigned HOST_WIDE_INT
10136 simple_type_size_in_bits (const_tree type)
10137 {
10138   if (TREE_CODE (type) == ERROR_MARK)
10139     return BITS_PER_WORD;
10140   else if (TYPE_SIZE (type) == NULL_TREE)
10141     return 0;
10142   else if (host_integerp (TYPE_SIZE (type), 1))
10143     return tree_low_cst (TYPE_SIZE (type), 1);
10144   else
10145     return TYPE_ALIGN (type);
10146 }
10147
10148 /*  Given a pointer to a tree node for a subrange type, return a pointer
10149     to a DIE that describes the given type.  */
10150
10151 static dw_die_ref
10152 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
10153 {
10154   dw_die_ref subrange_die;
10155   const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
10156
10157   if (context_die == NULL)
10158     context_die = comp_unit_die;
10159
10160   subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
10161
10162   if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
10163     {
10164       /* The size of the subrange type and its base type do not match,
10165          so we need to generate a size attribute for the subrange type.  */
10166       add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
10167     }
10168
10169   if (low)
10170     add_bound_info (subrange_die, DW_AT_lower_bound, low);
10171   if (high)
10172     add_bound_info (subrange_die, DW_AT_upper_bound, high);
10173
10174   return subrange_die;
10175 }
10176
10177 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
10178    entry that chains various modifiers in front of the given type.  */
10179
10180 static dw_die_ref
10181 modified_type_die (tree type, int is_const_type, int is_volatile_type,
10182                    dw_die_ref context_die)
10183 {
10184   enum tree_code code = TREE_CODE (type);
10185   dw_die_ref mod_type_die;
10186   dw_die_ref sub_die = NULL;
10187   tree item_type = NULL;
10188   tree qualified_type;
10189   tree name, low, high;
10190
10191   if (code == ERROR_MARK)
10192     return NULL;
10193
10194   /* See if we already have the appropriately qualified variant of
10195      this type.  */
10196   qualified_type
10197     = get_qualified_type (type,
10198                           ((is_const_type ? TYPE_QUAL_CONST : 0)
10199                            | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
10200
10201   /* If we do, then we can just use its DIE, if it exists.  */
10202   if (qualified_type)
10203     {
10204       mod_type_die = lookup_type_die (qualified_type);
10205       if (mod_type_die)
10206         return mod_type_die;
10207     }
10208
10209   name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
10210
10211   /* Handle C typedef types.  */
10212   if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name))
10213     {
10214       tree dtype = TREE_TYPE (name);
10215
10216       if (qualified_type == dtype)
10217         {
10218           /* For a named type, use the typedef.  */
10219           gen_type_die (qualified_type, context_die);
10220           return lookup_type_die (qualified_type);
10221         }
10222       else if (is_const_type < TYPE_READONLY (dtype)
10223                || is_volatile_type < TYPE_VOLATILE (dtype)
10224                || (is_const_type <= TYPE_READONLY (dtype)
10225                    && is_volatile_type <= TYPE_VOLATILE (dtype)
10226                    && DECL_ORIGINAL_TYPE (name) != type))
10227         /* cv-unqualified version of named type.  Just use the unnamed
10228            type to which it refers.  */
10229         return modified_type_die (DECL_ORIGINAL_TYPE (name),
10230                                   is_const_type, is_volatile_type,
10231                                   context_die);
10232       /* Else cv-qualified version of named type; fall through.  */
10233     }
10234
10235   if (is_const_type)
10236     {
10237       mod_type_die = new_die (DW_TAG_const_type, comp_unit_die, type);
10238       sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
10239     }
10240   else if (is_volatile_type)
10241     {
10242       mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die, type);
10243       sub_die = modified_type_die (type, 0, 0, context_die);
10244     }
10245   else if (code == POINTER_TYPE)
10246     {
10247       mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die, type);
10248       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10249                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
10250       item_type = TREE_TYPE (type);
10251     }
10252   else if (code == REFERENCE_TYPE)
10253     {
10254       mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die, type);
10255       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10256                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
10257       item_type = TREE_TYPE (type);
10258     }
10259   else if (code == INTEGER_TYPE
10260            && TREE_TYPE (type) != NULL_TREE
10261            && subrange_type_for_debug_p (type, &low, &high))
10262     {
10263       mod_type_die = subrange_type_die (type, low, high, context_die);
10264       item_type = TREE_TYPE (type);
10265     }
10266   else if (is_base_type (type))
10267     mod_type_die = base_type_die (type);
10268   else
10269     {
10270       gen_type_die (type, context_die);
10271
10272       /* We have to get the type_main_variant here (and pass that to the
10273          `lookup_type_die' routine) because the ..._TYPE node we have
10274          might simply be a *copy* of some original type node (where the
10275          copy was created to help us keep track of typedef names) and
10276          that copy might have a different TYPE_UID from the original
10277          ..._TYPE node.  */
10278       if (TREE_CODE (type) != VECTOR_TYPE)
10279         return lookup_type_die (type_main_variant (type));
10280       else
10281         /* Vectors have the debugging information in the type,
10282            not the main variant.  */
10283         return lookup_type_die (type);
10284     }
10285
10286   /* Builtin types don't have a DECL_ORIGINAL_TYPE.  For those,
10287      don't output a DW_TAG_typedef, since there isn't one in the
10288      user's program; just attach a DW_AT_name to the type.  */
10289   if (name
10290       && (TREE_CODE (name) != TYPE_DECL
10291           || (TREE_TYPE (name) == qualified_type && DECL_NAME (name))))
10292     {
10293       if (TREE_CODE (name) == TYPE_DECL)
10294         /* Could just call add_name_and_src_coords_attributes here,
10295            but since this is a builtin type it doesn't have any
10296            useful source coordinates anyway.  */
10297         name = DECL_NAME (name);
10298       add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
10299     }
10300
10301   if (qualified_type)
10302     equate_type_number_to_die (qualified_type, mod_type_die);
10303
10304   if (item_type)
10305     /* We must do this after the equate_type_number_to_die call, in case
10306        this is a recursive type.  This ensures that the modified_type_die
10307        recursion will terminate even if the type is recursive.  Recursive
10308        types are possible in Ada.  */
10309     sub_die = modified_type_die (item_type,
10310                                  TYPE_READONLY (item_type),
10311                                  TYPE_VOLATILE (item_type),
10312                                  context_die);
10313
10314   if (sub_die != NULL)
10315     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
10316
10317   return mod_type_die;
10318 }
10319
10320 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
10321    an enumerated type.  */
10322
10323 static inline int
10324 type_is_enum (const_tree type)
10325 {
10326   return TREE_CODE (type) == ENUMERAL_TYPE;
10327 }
10328
10329 /* Return the DBX register number described by a given RTL node.  */
10330
10331 static unsigned int
10332 dbx_reg_number (const_rtx rtl)
10333 {
10334   unsigned regno = REGNO (rtl);
10335
10336   gcc_assert (regno < FIRST_PSEUDO_REGISTER);
10337
10338 #ifdef LEAF_REG_REMAP
10339   if (current_function_uses_only_leaf_regs)
10340     {
10341       int leaf_reg = LEAF_REG_REMAP (regno);
10342       if (leaf_reg != -1)
10343         regno = (unsigned) leaf_reg;
10344     }
10345 #endif
10346
10347   return DBX_REGISTER_NUMBER (regno);
10348 }
10349
10350 /* Optionally add a DW_OP_piece term to a location description expression.
10351    DW_OP_piece is only added if the location description expression already
10352    doesn't end with DW_OP_piece.  */
10353
10354 static void
10355 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
10356 {
10357   dw_loc_descr_ref loc;
10358
10359   if (*list_head != NULL)
10360     {
10361       /* Find the end of the chain.  */
10362       for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
10363         ;
10364
10365       if (loc->dw_loc_opc != DW_OP_piece)
10366         loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
10367     }
10368 }
10369
10370 /* Return a location descriptor that designates a machine register or
10371    zero if there is none.  */
10372
10373 static dw_loc_descr_ref
10374 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
10375 {
10376   rtx regs;
10377
10378   if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
10379     return 0;
10380
10381   regs = targetm.dwarf_register_span (rtl);
10382
10383   if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
10384     return multiple_reg_loc_descriptor (rtl, regs, initialized);
10385   else
10386     return one_reg_loc_descriptor (dbx_reg_number (rtl), initialized);
10387 }
10388
10389 /* Return a location descriptor that designates a machine register for
10390    a given hard register number.  */
10391
10392 static dw_loc_descr_ref
10393 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
10394 {
10395   dw_loc_descr_ref reg_loc_descr;
10396
10397   if (regno <= 31)
10398     reg_loc_descr
10399       = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
10400   else
10401     reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
10402
10403   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10404     add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10405
10406   return reg_loc_descr;
10407 }
10408
10409 /* Given an RTL of a register, return a location descriptor that
10410    designates a value that spans more than one register.  */
10411
10412 static dw_loc_descr_ref
10413 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
10414                              enum var_init_status initialized)
10415 {
10416   int nregs, size, i;
10417   unsigned reg;
10418   dw_loc_descr_ref loc_result = NULL;
10419
10420   reg = REGNO (rtl);
10421 #ifdef LEAF_REG_REMAP
10422   if (current_function_uses_only_leaf_regs)
10423     {
10424       int leaf_reg = LEAF_REG_REMAP (reg);
10425       if (leaf_reg != -1)
10426         reg = (unsigned) leaf_reg;
10427     }
10428 #endif
10429   gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
10430   nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
10431
10432   /* Simple, contiguous registers.  */
10433   if (regs == NULL_RTX)
10434     {
10435       size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
10436
10437       loc_result = NULL;
10438       while (nregs--)
10439         {
10440           dw_loc_descr_ref t;
10441
10442           t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
10443                                       VAR_INIT_STATUS_INITIALIZED);
10444           add_loc_descr (&loc_result, t);
10445           add_loc_descr_op_piece (&loc_result, size);
10446           ++reg;
10447         }
10448       return loc_result;
10449     }
10450
10451   /* Now onto stupid register sets in non contiguous locations.  */
10452
10453   gcc_assert (GET_CODE (regs) == PARALLEL);
10454
10455   size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
10456   loc_result = NULL;
10457
10458   for (i = 0; i < XVECLEN (regs, 0); ++i)
10459     {
10460       dw_loc_descr_ref t;
10461
10462       t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
10463                                   VAR_INIT_STATUS_INITIALIZED);
10464       add_loc_descr (&loc_result, t);
10465       size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
10466       add_loc_descr_op_piece (&loc_result, size);
10467     }
10468
10469   if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10470     add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10471   return loc_result;
10472 }
10473
10474 #endif /* DWARF2_DEBUGGING_INFO */
10475
10476 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
10477
10478 /* Return a location descriptor that designates a constant.  */
10479
10480 static dw_loc_descr_ref
10481 int_loc_descriptor (HOST_WIDE_INT i)
10482 {
10483   enum dwarf_location_atom op;
10484
10485   /* Pick the smallest representation of a constant, rather than just
10486      defaulting to the LEB encoding.  */
10487   if (i >= 0)
10488     {
10489       if (i <= 31)
10490         op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
10491       else if (i <= 0xff)
10492         op = DW_OP_const1u;
10493       else if (i <= 0xffff)
10494         op = DW_OP_const2u;
10495       else if (HOST_BITS_PER_WIDE_INT == 32
10496                || i <= 0xffffffff)
10497         op = DW_OP_const4u;
10498       else
10499         op = DW_OP_constu;
10500     }
10501   else
10502     {
10503       if (i >= -0x80)
10504         op = DW_OP_const1s;
10505       else if (i >= -0x8000)
10506         op = DW_OP_const2s;
10507       else if (HOST_BITS_PER_WIDE_INT == 32
10508                || i >= -0x80000000)
10509         op = DW_OP_const4s;
10510       else
10511         op = DW_OP_consts;
10512     }
10513
10514   return new_loc_descr (op, i, 0);
10515 }
10516 #endif
10517
10518 #ifdef DWARF2_DEBUGGING_INFO
10519
10520 /* Return a location descriptor that designates a base+offset location.  */
10521
10522 static dw_loc_descr_ref
10523 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
10524                  enum var_init_status initialized)
10525 {
10526   unsigned int regno;
10527   dw_loc_descr_ref result;
10528   dw_fde_ref fde = current_fde ();
10529
10530   /* We only use "frame base" when we're sure we're talking about the
10531      post-prologue local stack frame.  We do this by *not* running
10532      register elimination until this point, and recognizing the special
10533      argument pointer and soft frame pointer rtx's.  */
10534   if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
10535     {
10536       rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
10537
10538       if (elim != reg)
10539         {
10540           if (GET_CODE (elim) == PLUS)
10541             {
10542               offset += INTVAL (XEXP (elim, 1));
10543               elim = XEXP (elim, 0);
10544             }
10545           gcc_assert ((SUPPORTS_STACK_ALIGNMENT
10546                        && (elim == hard_frame_pointer_rtx
10547                            || elim == stack_pointer_rtx))
10548                       || elim == (frame_pointer_needed
10549                                   ? hard_frame_pointer_rtx
10550                                   : stack_pointer_rtx));
10551
10552           /* If drap register is used to align stack, use frame
10553              pointer + offset to access stack variables.  If stack
10554              is aligned without drap, use stack pointer + offset to
10555              access stack variables.  */
10556           if (crtl->stack_realign_tried
10557               && reg == frame_pointer_rtx)
10558             {
10559               int base_reg
10560                 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
10561                                       ? HARD_FRAME_POINTER_REGNUM
10562                                       : STACK_POINTER_REGNUM);
10563               return new_reg_loc_descr (base_reg, offset);
10564             }
10565
10566           offset += frame_pointer_fb_offset;
10567           return new_loc_descr (DW_OP_fbreg, offset, 0);
10568         }
10569     }
10570   else if (fde
10571            && fde->drap_reg != INVALID_REGNUM
10572            && (fde->drap_reg == REGNO (reg)
10573                || fde->vdrap_reg == REGNO (reg)))
10574     {
10575       /* Use cfa+offset to represent the location of arguments passed
10576          on stack when drap is used to align stack.  */
10577       return new_loc_descr (DW_OP_fbreg, offset, 0);
10578     }
10579
10580   regno = dbx_reg_number (reg);
10581   if (regno <= 31)
10582     result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
10583                             offset, 0);
10584   else
10585     result = new_loc_descr (DW_OP_bregx, regno, offset);
10586
10587   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10588     add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10589
10590   return result;
10591 }
10592
10593 /* Return true if this RTL expression describes a base+offset calculation.  */
10594
10595 static inline int
10596 is_based_loc (const_rtx rtl)
10597 {
10598   return (GET_CODE (rtl) == PLUS
10599           && ((REG_P (XEXP (rtl, 0))
10600                && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
10601                && CONST_INT_P (XEXP (rtl, 1)))));
10602 }
10603
10604 /* Return a descriptor that describes the concatenation of N locations
10605    used to form the address of a memory location.  */
10606
10607 static dw_loc_descr_ref
10608 concatn_mem_loc_descriptor (rtx concatn, enum machine_mode mode,
10609                             enum var_init_status initialized)
10610 {
10611   unsigned int i;
10612   dw_loc_descr_ref cc_loc_result = NULL;
10613   unsigned int n = XVECLEN (concatn, 0);
10614
10615   for (i = 0; i < n; ++i)
10616     {
10617       dw_loc_descr_ref ref;
10618       rtx x = XVECEXP (concatn, 0, i);
10619
10620       ref = mem_loc_descriptor (x, mode, VAR_INIT_STATUS_INITIALIZED);
10621       if (ref == NULL)
10622         return NULL;
10623
10624       add_loc_descr (&cc_loc_result, ref);
10625       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
10626     }
10627
10628   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10629     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10630
10631   return cc_loc_result;
10632 }
10633
10634 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
10635    failed.  */
10636
10637 static dw_loc_descr_ref
10638 tls_mem_loc_descriptor (rtx mem)
10639 {
10640   tree base;
10641   dw_loc_descr_ref loc_result;
10642
10643   if (MEM_EXPR (mem) == NULL_TREE || MEM_OFFSET (mem) == NULL_RTX)
10644     return NULL;
10645
10646   base = get_base_address (MEM_EXPR (mem));
10647   if (base == NULL
10648       || TREE_CODE (base) != VAR_DECL
10649       || !DECL_THREAD_LOCAL_P (base))
10650     return NULL;
10651
10652   loc_result = loc_descriptor_from_tree_1 (MEM_EXPR (mem), 2);
10653   if (loc_result == NULL)
10654     return NULL;
10655
10656   if (INTVAL (MEM_OFFSET (mem)))
10657     loc_descr_plus_const (&loc_result, INTVAL (MEM_OFFSET (mem)));
10658
10659   return loc_result;
10660 }
10661
10662 /* The following routine converts the RTL for a variable or parameter
10663    (resident in memory) into an equivalent Dwarf representation of a
10664    mechanism for getting the address of that same variable onto the top of a
10665    hypothetical "address evaluation" stack.
10666
10667    When creating memory location descriptors, we are effectively transforming
10668    the RTL for a memory-resident object into its Dwarf postfix expression
10669    equivalent.  This routine recursively descends an RTL tree, turning
10670    it into Dwarf postfix code as it goes.
10671
10672    MODE is the mode of the memory reference, needed to handle some
10673    autoincrement addressing modes.
10674
10675    CAN_USE_FBREG is a flag whether we can use DW_AT_frame_base in the
10676    location list for RTL.
10677
10678    Return 0 if we can't represent the location.  */
10679
10680 static dw_loc_descr_ref
10681 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
10682                     enum var_init_status initialized)
10683 {
10684   dw_loc_descr_ref mem_loc_result = NULL;
10685   enum dwarf_location_atom op;
10686
10687   /* Note that for a dynamically sized array, the location we will generate a
10688      description of here will be the lowest numbered location which is
10689      actually within the array.  That's *not* necessarily the same as the
10690      zeroth element of the array.  */
10691
10692   rtl = targetm.delegitimize_address (rtl);
10693
10694   switch (GET_CODE (rtl))
10695     {
10696     case POST_INC:
10697     case POST_DEC:
10698     case POST_MODIFY:
10699       /* POST_INC and POST_DEC can be handled just like a SUBREG.  So we
10700          just fall into the SUBREG code.  */
10701
10702       /* ... fall through ...  */
10703
10704     case SUBREG:
10705       /* The case of a subreg may arise when we have a local (register)
10706          variable or a formal (register) parameter which doesn't quite fill
10707          up an entire register.  For now, just assume that it is
10708          legitimate to make the Dwarf info refer to the whole register which
10709          contains the given subreg.  */
10710       rtl = XEXP (rtl, 0);
10711
10712       /* ... fall through ...  */
10713
10714     case REG:
10715       /* Whenever a register number forms a part of the description of the
10716          method for calculating the (dynamic) address of a memory resident
10717          object, DWARF rules require the register number be referred to as
10718          a "base register".  This distinction is not based in any way upon
10719          what category of register the hardware believes the given register
10720          belongs to.  This is strictly DWARF terminology we're dealing with
10721          here. Note that in cases where the location of a memory-resident
10722          data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
10723          OP_CONST (0)) the actual DWARF location descriptor that we generate
10724          may just be OP_BASEREG (basereg).  This may look deceptively like
10725          the object in question was allocated to a register (rather than in
10726          memory) so DWARF consumers need to be aware of the subtle
10727          distinction between OP_REG and OP_BASEREG.  */
10728       if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
10729         mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
10730       else if (stack_realign_drap
10731                && crtl->drap_reg
10732                && crtl->args.internal_arg_pointer == rtl
10733                && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
10734         {
10735           /* If RTL is internal_arg_pointer, which has been optimized
10736              out, use DRAP instead.  */
10737           mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
10738                                             VAR_INIT_STATUS_INITIALIZED);
10739         }
10740       break;
10741
10742     case MEM:
10743       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
10744                                            VAR_INIT_STATUS_INITIALIZED);
10745       if (mem_loc_result == NULL)
10746         mem_loc_result = tls_mem_loc_descriptor (rtl);
10747       if (mem_loc_result != 0)
10748         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
10749       break;
10750
10751     case LO_SUM:
10752          rtl = XEXP (rtl, 1);
10753
10754       /* ... fall through ...  */
10755
10756     case LABEL_REF:
10757       /* Some ports can transform a symbol ref into a label ref, because
10758          the symbol ref is too far away and has to be dumped into a constant
10759          pool.  */
10760     case CONST:
10761     case SYMBOL_REF:
10762       /* Alternatively, the symbol in the constant pool might be referenced
10763          by a different symbol.  */
10764       if (GET_CODE (rtl) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (rtl))
10765         {
10766           bool marked;
10767           rtx tmp = get_pool_constant_mark (rtl, &marked);
10768
10769           if (GET_CODE (tmp) == SYMBOL_REF)
10770             {
10771               rtl = tmp;
10772               if (CONSTANT_POOL_ADDRESS_P (tmp))
10773                 get_pool_constant_mark (tmp, &marked);
10774               else
10775                 marked = true;
10776             }
10777
10778           /* If all references to this pool constant were optimized away,
10779              it was not output and thus we can't represent it.
10780              FIXME: might try to use DW_OP_const_value here, though
10781              DW_OP_piece complicates it.  */
10782           if (!marked)
10783             return 0;
10784         }
10785
10786       mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
10787       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
10788       mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
10789       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
10790       break;
10791
10792     case PRE_MODIFY:
10793       /* Extract the PLUS expression nested inside and fall into
10794          PLUS code below.  */
10795       rtl = XEXP (rtl, 1);
10796       goto plus;
10797
10798     case PRE_INC:
10799     case PRE_DEC:
10800       /* Turn these into a PLUS expression and fall into the PLUS code
10801          below.  */
10802       rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
10803                           GEN_INT (GET_CODE (rtl) == PRE_INC
10804                                    ? GET_MODE_UNIT_SIZE (mode)
10805                                    : -GET_MODE_UNIT_SIZE (mode)));
10806
10807       /* ... fall through ...  */
10808
10809     case PLUS:
10810     plus:
10811       if (is_based_loc (rtl))
10812         mem_loc_result = based_loc_descr (XEXP (rtl, 0),
10813                                           INTVAL (XEXP (rtl, 1)),
10814                                           VAR_INIT_STATUS_INITIALIZED);
10815       else
10816         {
10817           mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode,
10818                                                VAR_INIT_STATUS_INITIALIZED);
10819           if (mem_loc_result == 0)
10820             break;
10821
10822           if (CONST_INT_P (XEXP (rtl, 1)))
10823             loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
10824           else
10825             {
10826               dw_loc_descr_ref mem_loc_result2
10827                 = mem_loc_descriptor (XEXP (rtl, 1), mode,
10828                                       VAR_INIT_STATUS_INITIALIZED);
10829               if (mem_loc_result2 == 0)
10830                 break;
10831               add_loc_descr (&mem_loc_result, mem_loc_result2);
10832               add_loc_descr (&mem_loc_result,
10833                              new_loc_descr (DW_OP_plus, 0, 0));
10834             }
10835         }
10836       break;
10837
10838     /* If a pseudo-reg is optimized away, it is possible for it to
10839        be replaced with a MEM containing a multiply or shift.  */
10840     case MULT:
10841       op = DW_OP_mul;
10842       goto do_binop;
10843
10844     case ASHIFT:
10845       op = DW_OP_shl;
10846       goto do_binop;
10847
10848     case ASHIFTRT:
10849       op = DW_OP_shra;
10850       goto do_binop;
10851
10852     case LSHIFTRT:
10853       op = DW_OP_shr;
10854       goto do_binop;
10855
10856     do_binop:
10857       {
10858         dw_loc_descr_ref op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
10859                                                    VAR_INIT_STATUS_INITIALIZED);
10860         dw_loc_descr_ref op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
10861                                                    VAR_INIT_STATUS_INITIALIZED);
10862
10863         if (op0 == 0 || op1 == 0)
10864           break;
10865
10866         mem_loc_result = op0;
10867         add_loc_descr (&mem_loc_result, op1);
10868         add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
10869         break;
10870       }
10871
10872     case CONST_INT:
10873       mem_loc_result = int_loc_descriptor (INTVAL (rtl));
10874       break;
10875
10876     case CONCATN:
10877       mem_loc_result = concatn_mem_loc_descriptor (rtl, mode,
10878                                                    VAR_INIT_STATUS_INITIALIZED);
10879       break;
10880
10881     case UNSPEC:
10882       /* If delegitimize_address couldn't do anything with the UNSPEC, we
10883          can't express it in the debug info.  This can happen e.g. with some
10884          TLS UNSPECs.  */
10885       break;
10886
10887     default:
10888       gcc_unreachable ();
10889     }
10890
10891   if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10892     add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10893
10894   return mem_loc_result;
10895 }
10896
10897 /* Return a descriptor that describes the concatenation of two locations.
10898    This is typically a complex variable.  */
10899
10900 static dw_loc_descr_ref
10901 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
10902 {
10903   dw_loc_descr_ref cc_loc_result = NULL;
10904   dw_loc_descr_ref x0_ref = loc_descriptor (x0, VAR_INIT_STATUS_INITIALIZED);
10905   dw_loc_descr_ref x1_ref = loc_descriptor (x1, VAR_INIT_STATUS_INITIALIZED);
10906
10907   if (x0_ref == 0 || x1_ref == 0)
10908     return 0;
10909
10910   cc_loc_result = x0_ref;
10911   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
10912
10913   add_loc_descr (&cc_loc_result, x1_ref);
10914   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
10915
10916   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10917     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10918
10919   return cc_loc_result;
10920 }
10921
10922 /* Return a descriptor that describes the concatenation of N
10923    locations.  */
10924
10925 static dw_loc_descr_ref
10926 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
10927 {
10928   unsigned int i;
10929   dw_loc_descr_ref cc_loc_result = NULL;
10930   unsigned int n = XVECLEN (concatn, 0);
10931
10932   for (i = 0; i < n; ++i)
10933     {
10934       dw_loc_descr_ref ref;
10935       rtx x = XVECEXP (concatn, 0, i);
10936
10937       ref = loc_descriptor (x, VAR_INIT_STATUS_INITIALIZED);
10938       if (ref == NULL)
10939         return NULL;
10940
10941       add_loc_descr (&cc_loc_result, ref);
10942       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
10943     }
10944
10945   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10946     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10947
10948   return cc_loc_result;
10949 }
10950
10951 /* Output a proper Dwarf location descriptor for a variable or parameter
10952    which is either allocated in a register or in a memory location.  For a
10953    register, we just generate an OP_REG and the register number.  For a
10954    memory location we provide a Dwarf postfix expression describing how to
10955    generate the (dynamic) address of the object onto the address stack.
10956
10957    If we don't know how to describe it, return 0.  */
10958
10959 static dw_loc_descr_ref
10960 loc_descriptor (rtx rtl, enum var_init_status initialized)
10961 {
10962   dw_loc_descr_ref loc_result = NULL;
10963
10964   switch (GET_CODE (rtl))
10965     {
10966     case SUBREG:
10967       /* The case of a subreg may arise when we have a local (register)
10968          variable or a formal (register) parameter which doesn't quite fill
10969          up an entire register.  For now, just assume that it is
10970          legitimate to make the Dwarf info refer to the whole register which
10971          contains the given subreg.  */
10972       rtl = SUBREG_REG (rtl);
10973
10974       /* ... fall through ...  */
10975
10976     case REG:
10977       loc_result = reg_loc_descriptor (rtl, initialized);
10978       break;
10979
10980     case MEM:
10981       loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
10982                                        initialized);
10983       if (loc_result == NULL)
10984         loc_result = tls_mem_loc_descriptor (rtl);
10985       break;
10986
10987     case CONCAT:
10988       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
10989                                           initialized);
10990       break;
10991
10992     case CONCATN:
10993       loc_result = concatn_loc_descriptor (rtl, initialized);
10994       break;
10995
10996     case VAR_LOCATION:
10997       /* Single part.  */
10998       if (GET_CODE (XEXP (rtl, 1)) != PARALLEL)
10999         {
11000           loc_result = loc_descriptor (XEXP (XEXP (rtl, 1), 0), initialized);
11001           break;
11002         }
11003
11004       rtl = XEXP (rtl, 1);
11005       /* FALLTHRU */
11006
11007     case PARALLEL:
11008       {
11009         rtvec par_elems = XVEC (rtl, 0);
11010         int num_elem = GET_NUM_ELEM (par_elems);
11011         enum machine_mode mode;
11012         int i;
11013
11014         /* Create the first one, so we have something to add to.  */
11015         loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
11016                                      initialized);
11017         if (loc_result == NULL)
11018           return NULL;
11019         mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
11020         add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
11021         for (i = 1; i < num_elem; i++)
11022           {
11023             dw_loc_descr_ref temp;
11024
11025             temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
11026                                    initialized);
11027             if (temp == NULL)
11028               return NULL;
11029             add_loc_descr (&loc_result, temp);
11030             mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
11031             add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
11032           }
11033       }
11034       break;
11035
11036     default:
11037       gcc_unreachable ();
11038     }
11039
11040   return loc_result;
11041 }
11042
11043 /* Similar, but generate the descriptor from trees instead of rtl.  This comes
11044    up particularly with variable length arrays.  WANT_ADDRESS is 2 if this is
11045    a top-level invocation of loc_descriptor_from_tree; is 1 if this is not a
11046    top-level invocation, and we require the address of LOC; is 0 if we require
11047    the value of LOC.  */
11048
11049 static dw_loc_descr_ref
11050 loc_descriptor_from_tree_1 (tree loc, int want_address)
11051 {
11052   dw_loc_descr_ref ret, ret1;
11053   int have_address = 0;
11054   enum dwarf_location_atom op;
11055
11056   /* ??? Most of the time we do not take proper care for sign/zero
11057      extending the values properly.  Hopefully this won't be a real
11058      problem...  */
11059
11060   switch (TREE_CODE (loc))
11061     {
11062     case ERROR_MARK:
11063       return 0;
11064
11065     case PLACEHOLDER_EXPR:
11066       /* This case involves extracting fields from an object to determine the
11067          position of other fields.  We don't try to encode this here.  The
11068          only user of this is Ada, which encodes the needed information using
11069          the names of types.  */
11070       return 0;
11071
11072     case CALL_EXPR:
11073       return 0;
11074
11075     case PREINCREMENT_EXPR:
11076     case PREDECREMENT_EXPR:
11077     case POSTINCREMENT_EXPR:
11078     case POSTDECREMENT_EXPR:
11079       /* There are no opcodes for these operations.  */
11080       return 0;
11081
11082     case ADDR_EXPR:
11083       /* If we already want an address, there's nothing we can do.  */
11084       if (want_address)
11085         return 0;
11086
11087       /* Otherwise, process the argument and look for the address.  */
11088       return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 1);
11089
11090     case VAR_DECL:
11091       if (DECL_THREAD_LOCAL_P (loc))
11092         {
11093           rtx rtl;
11094           enum dwarf_location_atom first_op;
11095           enum dwarf_location_atom second_op;
11096           bool dtprel = false;
11097
11098           if (targetm.have_tls)
11099             {
11100               /* If this is not defined, we have no way to emit the
11101                  data.  */
11102               if (!targetm.asm_out.output_dwarf_dtprel)
11103                 return 0;
11104
11105                /* The way DW_OP_GNU_push_tls_address is specified, we
11106                   can only look up addresses of objects in the current
11107                   module.  */
11108               if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
11109                 return 0;
11110               first_op = DW_OP_addr;
11111               dtprel = true;
11112               second_op = DW_OP_GNU_push_tls_address;
11113             }
11114           else
11115             {
11116               if (!targetm.emutls.debug_form_tls_address)
11117                 return 0;
11118               loc = emutls_decl (loc);
11119               first_op = DW_OP_addr;
11120               second_op = DW_OP_form_tls_address;
11121             }
11122
11123           rtl = rtl_for_decl_location (loc);
11124           if (rtl == NULL_RTX)
11125             return 0;
11126
11127           if (!MEM_P (rtl))
11128             return 0;
11129           rtl = XEXP (rtl, 0);
11130           if (! CONSTANT_P (rtl))
11131             return 0;
11132
11133           ret = new_loc_descr (first_op, 0, 0);
11134           ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
11135           ret->dw_loc_oprnd1.v.val_addr = rtl;
11136           ret->dtprel = dtprel;
11137
11138           ret1 = new_loc_descr (second_op, 0, 0);
11139           add_loc_descr (&ret, ret1);
11140
11141           have_address = 1;
11142           break;
11143         }
11144       /* FALLTHRU */
11145
11146     case PARM_DECL:
11147       if (DECL_HAS_VALUE_EXPR_P (loc))
11148         return loc_descriptor_from_tree_1 (DECL_VALUE_EXPR (loc),
11149                                            want_address);
11150       /* FALLTHRU */
11151
11152     case RESULT_DECL:
11153     case FUNCTION_DECL:
11154       {
11155         rtx rtl = rtl_for_decl_location (loc);
11156
11157         if (rtl == NULL_RTX)
11158           return 0;
11159         else if (CONST_INT_P (rtl))
11160           {
11161             HOST_WIDE_INT val = INTVAL (rtl);
11162             if (TYPE_UNSIGNED (TREE_TYPE (loc)))
11163               val &= GET_MODE_MASK (DECL_MODE (loc));
11164             ret = int_loc_descriptor (val);
11165           }
11166         else if (GET_CODE (rtl) == CONST_STRING)
11167           return 0;
11168         else if (CONSTANT_P (rtl))
11169           {
11170             ret = new_loc_descr (DW_OP_addr, 0, 0);
11171             ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
11172             ret->dw_loc_oprnd1.v.val_addr = rtl;
11173           }
11174         else
11175           {
11176             enum machine_mode mode;
11177
11178             /* Certain constructs can only be represented at top-level.  */
11179             if (want_address == 2)
11180               return loc_descriptor (rtl, VAR_INIT_STATUS_INITIALIZED);
11181
11182             mode = GET_MODE (rtl);
11183             if (MEM_P (rtl))
11184               {
11185                 rtl = XEXP (rtl, 0);
11186                 have_address = 1;
11187               }
11188             ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
11189           }
11190       }
11191       break;
11192
11193     case INDIRECT_REF:
11194       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
11195       have_address = 1;
11196       break;
11197
11198     case COMPOUND_EXPR:
11199       return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), want_address);
11200
11201     CASE_CONVERT:
11202     case VIEW_CONVERT_EXPR:
11203     case SAVE_EXPR:
11204     case MODIFY_EXPR:
11205       return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), want_address);
11206
11207     case COMPONENT_REF:
11208     case BIT_FIELD_REF:
11209     case ARRAY_REF:
11210     case ARRAY_RANGE_REF:
11211       {
11212         tree obj, offset;
11213         HOST_WIDE_INT bitsize, bitpos, bytepos;
11214         enum machine_mode mode;
11215         int volatilep;
11216         int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
11217
11218         obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
11219                                    &unsignedp, &volatilep, false);
11220
11221         if (obj == loc)
11222           return 0;
11223
11224         ret = loc_descriptor_from_tree_1 (obj, 1);
11225         if (ret == 0
11226             || bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
11227           return 0;
11228
11229         if (offset != NULL_TREE)
11230           {
11231             /* Variable offset.  */
11232             ret1 = loc_descriptor_from_tree_1 (offset, 0);
11233             if (ret1 == 0)
11234               return 0;
11235             add_loc_descr (&ret, ret1);
11236             add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
11237           }
11238
11239         bytepos = bitpos / BITS_PER_UNIT;
11240         loc_descr_plus_const (&ret, bytepos);
11241
11242         have_address = 1;
11243         break;
11244       }
11245
11246     case INTEGER_CST:
11247       if (host_integerp (loc, 0))
11248         ret = int_loc_descriptor (tree_low_cst (loc, 0));
11249       else
11250         return 0;
11251       break;
11252
11253     case CONSTRUCTOR:
11254       {
11255         /* Get an RTL for this, if something has been emitted.  */
11256         rtx rtl = lookup_constant_def (loc);
11257         enum machine_mode mode;
11258
11259         if (!rtl || !MEM_P (rtl))
11260           return 0;
11261         mode = GET_MODE (rtl);
11262         rtl = XEXP (rtl, 0);
11263         ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
11264         have_address = 1;
11265         break;
11266       }
11267
11268     case TRUTH_AND_EXPR:
11269     case TRUTH_ANDIF_EXPR:
11270     case BIT_AND_EXPR:
11271       op = DW_OP_and;
11272       goto do_binop;
11273
11274     case TRUTH_XOR_EXPR:
11275     case BIT_XOR_EXPR:
11276       op = DW_OP_xor;
11277       goto do_binop;
11278
11279     case TRUTH_OR_EXPR:
11280     case TRUTH_ORIF_EXPR:
11281     case BIT_IOR_EXPR:
11282       op = DW_OP_or;
11283       goto do_binop;
11284
11285     case FLOOR_DIV_EXPR:
11286     case CEIL_DIV_EXPR:
11287     case ROUND_DIV_EXPR:
11288     case TRUNC_DIV_EXPR:
11289       op = DW_OP_div;
11290       goto do_binop;
11291
11292     case MINUS_EXPR:
11293       op = DW_OP_minus;
11294       goto do_binop;
11295
11296     case FLOOR_MOD_EXPR:
11297     case CEIL_MOD_EXPR:
11298     case ROUND_MOD_EXPR:
11299     case TRUNC_MOD_EXPR:
11300       op = DW_OP_mod;
11301       goto do_binop;
11302
11303     case MULT_EXPR:
11304       op = DW_OP_mul;
11305       goto do_binop;
11306
11307     case LSHIFT_EXPR:
11308       op = DW_OP_shl;
11309       goto do_binop;
11310
11311     case RSHIFT_EXPR:
11312       op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
11313       goto do_binop;
11314
11315     case POINTER_PLUS_EXPR:
11316     case PLUS_EXPR:
11317       if (TREE_CODE (TREE_OPERAND (loc, 1)) == INTEGER_CST
11318           && host_integerp (TREE_OPERAND (loc, 1), 0))
11319         {
11320           ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
11321           if (ret == 0)
11322             return 0;
11323
11324           loc_descr_plus_const (&ret, tree_low_cst (TREE_OPERAND (loc, 1), 0));
11325           break;
11326         }
11327
11328       op = DW_OP_plus;
11329       goto do_binop;
11330
11331     case LE_EXPR:
11332       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
11333         return 0;
11334
11335       op = DW_OP_le;
11336       goto do_binop;
11337
11338     case GE_EXPR:
11339       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
11340         return 0;
11341
11342       op = DW_OP_ge;
11343       goto do_binop;
11344
11345     case LT_EXPR:
11346       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
11347         return 0;
11348
11349       op = DW_OP_lt;
11350       goto do_binop;
11351
11352     case GT_EXPR:
11353       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
11354         return 0;
11355
11356       op = DW_OP_gt;
11357       goto do_binop;
11358
11359     case EQ_EXPR:
11360       op = DW_OP_eq;
11361       goto do_binop;
11362
11363     case NE_EXPR:
11364       op = DW_OP_ne;
11365       goto do_binop;
11366
11367     do_binop:
11368       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
11369       ret1 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
11370       if (ret == 0 || ret1 == 0)
11371         return 0;
11372
11373       add_loc_descr (&ret, ret1);
11374       add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11375       break;
11376
11377     case TRUTH_NOT_EXPR:
11378     case BIT_NOT_EXPR:
11379       op = DW_OP_not;
11380       goto do_unop;
11381
11382     case ABS_EXPR:
11383       op = DW_OP_abs;
11384       goto do_unop;
11385
11386     case NEGATE_EXPR:
11387       op = DW_OP_neg;
11388       goto do_unop;
11389
11390     do_unop:
11391       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
11392       if (ret == 0)
11393         return 0;
11394
11395       add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11396       break;
11397
11398     case MIN_EXPR:
11399     case MAX_EXPR:
11400       {
11401         const enum tree_code code =
11402           TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
11403
11404         loc = build3 (COND_EXPR, TREE_TYPE (loc),
11405                       build2 (code, integer_type_node,
11406                               TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
11407                       TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
11408       }
11409
11410       /* ... fall through ...  */
11411
11412     case COND_EXPR:
11413       {
11414         dw_loc_descr_ref lhs
11415           = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
11416         dw_loc_descr_ref rhs
11417           = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 2), 0);
11418         dw_loc_descr_ref bra_node, jump_node, tmp;
11419
11420         ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
11421         if (ret == 0 || lhs == 0 || rhs == 0)
11422           return 0;
11423
11424         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
11425         add_loc_descr (&ret, bra_node);
11426
11427         add_loc_descr (&ret, rhs);
11428         jump_node = new_loc_descr (DW_OP_skip, 0, 0);
11429         add_loc_descr (&ret, jump_node);
11430
11431         add_loc_descr (&ret, lhs);
11432         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
11433         bra_node->dw_loc_oprnd1.v.val_loc = lhs;
11434
11435         /* ??? Need a node to point the skip at.  Use a nop.  */
11436         tmp = new_loc_descr (DW_OP_nop, 0, 0);
11437         add_loc_descr (&ret, tmp);
11438         jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
11439         jump_node->dw_loc_oprnd1.v.val_loc = tmp;
11440       }
11441       break;
11442
11443     case FIX_TRUNC_EXPR:
11444       return 0;
11445
11446     default:
11447       /* Leave front-end specific codes as simply unknown.  This comes
11448          up, for instance, with the C STMT_EXPR.  */
11449       if ((unsigned int) TREE_CODE (loc)
11450           >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
11451         return 0;
11452
11453 #ifdef ENABLE_CHECKING
11454       /* Otherwise this is a generic code; we should just lists all of
11455          these explicitly.  We forgot one.  */
11456       gcc_unreachable ();
11457 #else
11458       /* In a release build, we want to degrade gracefully: better to
11459          generate incomplete debugging information than to crash.  */
11460       return NULL;
11461 #endif
11462     }
11463
11464   /* Show if we can't fill the request for an address.  */
11465   if (want_address && !have_address)
11466     return 0;
11467
11468   /* If we've got an address and don't want one, dereference.  */
11469   if (!want_address && have_address && ret)
11470     {
11471       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
11472
11473       if (size > DWARF2_ADDR_SIZE || size == -1)
11474         return 0;
11475       else if (size == DWARF2_ADDR_SIZE)
11476         op = DW_OP_deref;
11477       else
11478         op = DW_OP_deref_size;
11479
11480       add_loc_descr (&ret, new_loc_descr (op, size, 0));
11481     }
11482
11483   return ret;
11484 }
11485
11486 static inline dw_loc_descr_ref
11487 loc_descriptor_from_tree (tree loc)
11488 {
11489   return loc_descriptor_from_tree_1 (loc, 2);
11490 }
11491
11492 /* Given a value, round it up to the lowest multiple of `boundary'
11493    which is not less than the value itself.  */
11494
11495 static inline HOST_WIDE_INT
11496 ceiling (HOST_WIDE_INT value, unsigned int boundary)
11497 {
11498   return (((value + boundary - 1) / boundary) * boundary);
11499 }
11500
11501 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
11502    pointer to the declared type for the relevant field variable, or return
11503    `integer_type_node' if the given node turns out to be an
11504    ERROR_MARK node.  */
11505
11506 static inline tree
11507 field_type (const_tree decl)
11508 {
11509   tree type;
11510
11511   if (TREE_CODE (decl) == ERROR_MARK)
11512     return integer_type_node;
11513
11514   type = DECL_BIT_FIELD_TYPE (decl);
11515   if (type == NULL_TREE)
11516     type = TREE_TYPE (decl);
11517
11518   return type;
11519 }
11520
11521 /* Given a pointer to a tree node, return the alignment in bits for
11522    it, or else return BITS_PER_WORD if the node actually turns out to
11523    be an ERROR_MARK node.  */
11524
11525 static inline unsigned
11526 simple_type_align_in_bits (const_tree type)
11527 {
11528   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
11529 }
11530
11531 static inline unsigned
11532 simple_decl_align_in_bits (const_tree decl)
11533 {
11534   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
11535 }
11536
11537 /* Return the result of rounding T up to ALIGN.  */
11538
11539 static inline HOST_WIDE_INT
11540 round_up_to_align (HOST_WIDE_INT t, unsigned int align)
11541 {
11542   /* We must be careful if T is negative because HOST_WIDE_INT can be
11543      either "above" or "below" unsigned int as per the C promotion
11544      rules, depending on the host, thus making the signedness of the
11545      direct multiplication and division unpredictable.  */
11546   unsigned HOST_WIDE_INT u = (unsigned HOST_WIDE_INT) t;
11547
11548   u += align - 1;
11549   u /= align;
11550   u *= align;
11551
11552   return (HOST_WIDE_INT) u;
11553 }
11554
11555 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
11556    lowest addressed byte of the "containing object" for the given FIELD_DECL,
11557    or return 0 if we are unable to determine what that offset is, either
11558    because the argument turns out to be a pointer to an ERROR_MARK node, or
11559    because the offset is actually variable.  (We can't handle the latter case
11560    just yet).  */
11561
11562 static HOST_WIDE_INT
11563 field_byte_offset (const_tree decl)
11564 {
11565   HOST_WIDE_INT object_offset_in_bits;
11566   HOST_WIDE_INT bitpos_int;
11567
11568   if (TREE_CODE (decl) == ERROR_MARK)
11569     return 0;
11570
11571   gcc_assert (TREE_CODE (decl) == FIELD_DECL);
11572
11573   /* We cannot yet cope with fields whose positions are variable, so
11574      for now, when we see such things, we simply return 0.  Someday, we may
11575      be able to handle such cases, but it will be damn difficult.  */
11576   if (! host_integerp (bit_position (decl), 0))
11577     return 0;
11578
11579   bitpos_int = int_bit_position (decl);
11580
11581 #ifdef PCC_BITFIELD_TYPE_MATTERS
11582   if (PCC_BITFIELD_TYPE_MATTERS)
11583     {
11584       tree type;
11585       tree field_size_tree;
11586       HOST_WIDE_INT deepest_bitpos;
11587       unsigned HOST_WIDE_INT field_size_in_bits;
11588       unsigned int type_align_in_bits;
11589       unsigned int decl_align_in_bits;
11590       unsigned HOST_WIDE_INT type_size_in_bits;
11591
11592       type = field_type (decl);
11593       type_size_in_bits = simple_type_size_in_bits (type);
11594       type_align_in_bits = simple_type_align_in_bits (type);
11595
11596       field_size_tree = DECL_SIZE (decl);
11597
11598       /* The size could be unspecified if there was an error, or for
11599          a flexible array member.  */
11600       if (!field_size_tree)
11601         field_size_tree = bitsize_zero_node;
11602
11603       /* If the size of the field is not constant, use the type size.  */
11604       if (host_integerp (field_size_tree, 1))
11605         field_size_in_bits = tree_low_cst (field_size_tree, 1);
11606       else
11607         field_size_in_bits = type_size_in_bits;
11608
11609       decl_align_in_bits = simple_decl_align_in_bits (decl);
11610
11611       /* The GCC front-end doesn't make any attempt to keep track of the
11612          starting bit offset (relative to the start of the containing
11613          structure type) of the hypothetical "containing object" for a
11614          bit-field.  Thus, when computing the byte offset value for the
11615          start of the "containing object" of a bit-field, we must deduce
11616          this information on our own. This can be rather tricky to do in
11617          some cases.  For example, handling the following structure type
11618          definition when compiling for an i386/i486 target (which only
11619          aligns long long's to 32-bit boundaries) can be very tricky:
11620
11621          struct S { int field1; long long field2:31; };
11622
11623          Fortunately, there is a simple rule-of-thumb which can be used
11624          in such cases.  When compiling for an i386/i486, GCC will
11625          allocate 8 bytes for the structure shown above.  It decides to
11626          do this based upon one simple rule for bit-field allocation.
11627          GCC allocates each "containing object" for each bit-field at
11628          the first (i.e. lowest addressed) legitimate alignment boundary
11629          (based upon the required minimum alignment for the declared
11630          type of the field) which it can possibly use, subject to the
11631          condition that there is still enough available space remaining
11632          in the containing object (when allocated at the selected point)
11633          to fully accommodate all of the bits of the bit-field itself.
11634
11635          This simple rule makes it obvious why GCC allocates 8 bytes for
11636          each object of the structure type shown above.  When looking
11637          for a place to allocate the "containing object" for `field2',
11638          the compiler simply tries to allocate a 64-bit "containing
11639          object" at each successive 32-bit boundary (starting at zero)
11640          until it finds a place to allocate that 64- bit field such that
11641          at least 31 contiguous (and previously unallocated) bits remain
11642          within that selected 64 bit field.  (As it turns out, for the
11643          example above, the compiler finds it is OK to allocate the
11644          "containing object" 64-bit field at bit-offset zero within the
11645          structure type.)
11646
11647          Here we attempt to work backwards from the limited set of facts
11648          we're given, and we try to deduce from those facts, where GCC
11649          must have believed that the containing object started (within
11650          the structure type). The value we deduce is then used (by the
11651          callers of this routine) to generate DW_AT_location and
11652          DW_AT_bit_offset attributes for fields (both bit-fields and, in
11653          the case of DW_AT_location, regular fields as well).  */
11654
11655       /* Figure out the bit-distance from the start of the structure to
11656          the "deepest" bit of the bit-field.  */
11657       deepest_bitpos = bitpos_int + field_size_in_bits;
11658
11659       /* This is the tricky part.  Use some fancy footwork to deduce
11660          where the lowest addressed bit of the containing object must
11661          be.  */
11662       object_offset_in_bits = deepest_bitpos - type_size_in_bits;
11663
11664       /* Round up to type_align by default.  This works best for
11665          bitfields.  */
11666       object_offset_in_bits
11667         = round_up_to_align (object_offset_in_bits, type_align_in_bits);
11668
11669       if (object_offset_in_bits > bitpos_int)
11670         {
11671           object_offset_in_bits = deepest_bitpos - type_size_in_bits;
11672
11673           /* Round up to decl_align instead.  */
11674           object_offset_in_bits
11675             = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
11676         }
11677     }
11678   else
11679 #endif
11680     object_offset_in_bits = bitpos_int;
11681
11682   return object_offset_in_bits / BITS_PER_UNIT;
11683 }
11684 \f
11685 /* The following routines define various Dwarf attributes and any data
11686    associated with them.  */
11687
11688 /* Add a location description attribute value to a DIE.
11689
11690    This emits location attributes suitable for whole variables and
11691    whole parameters.  Note that the location attributes for struct fields are
11692    generated by the routine `data_member_location_attribute' below.  */
11693
11694 static inline void
11695 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
11696                              dw_loc_descr_ref descr)
11697 {
11698   if (descr != 0)
11699     add_AT_loc (die, attr_kind, descr);
11700 }
11701
11702 /* Attach the specialized form of location attribute used for data members of
11703    struct and union types.  In the special case of a FIELD_DECL node which
11704    represents a bit-field, the "offset" part of this special location
11705    descriptor must indicate the distance in bytes from the lowest-addressed
11706    byte of the containing struct or union type to the lowest-addressed byte of
11707    the "containing object" for the bit-field.  (See the `field_byte_offset'
11708    function above).
11709
11710    For any given bit-field, the "containing object" is a hypothetical object
11711    (of some integral or enum type) within which the given bit-field lives.  The
11712    type of this hypothetical "containing object" is always the same as the
11713    declared type of the individual bit-field itself (for GCC anyway... the
11714    DWARF spec doesn't actually mandate this).  Note that it is the size (in
11715    bytes) of the hypothetical "containing object" which will be given in the
11716    DW_AT_byte_size attribute for this bit-field.  (See the
11717    `byte_size_attribute' function below.)  It is also used when calculating the
11718    value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
11719    function below.)  */
11720
11721 static void
11722 add_data_member_location_attribute (dw_die_ref die, tree decl)
11723 {
11724   HOST_WIDE_INT offset;
11725   dw_loc_descr_ref loc_descr = 0;
11726
11727   if (TREE_CODE (decl) == TREE_BINFO)
11728     {
11729       /* We're working on the TAG_inheritance for a base class.  */
11730       if (BINFO_VIRTUAL_P (decl) && is_cxx ())
11731         {
11732           /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
11733              aren't at a fixed offset from all (sub)objects of the same
11734              type.  We need to extract the appropriate offset from our
11735              vtable.  The following dwarf expression means
11736
11737                BaseAddr = ObAddr + *((*ObAddr) - Offset)
11738
11739              This is specific to the V3 ABI, of course.  */
11740
11741           dw_loc_descr_ref tmp;
11742
11743           /* Make a copy of the object address.  */
11744           tmp = new_loc_descr (DW_OP_dup, 0, 0);
11745           add_loc_descr (&loc_descr, tmp);
11746
11747           /* Extract the vtable address.  */
11748           tmp = new_loc_descr (DW_OP_deref, 0, 0);
11749           add_loc_descr (&loc_descr, tmp);
11750
11751           /* Calculate the address of the offset.  */
11752           offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
11753           gcc_assert (offset < 0);
11754
11755           tmp = int_loc_descriptor (-offset);
11756           add_loc_descr (&loc_descr, tmp);
11757           tmp = new_loc_descr (DW_OP_minus, 0, 0);
11758           add_loc_descr (&loc_descr, tmp);
11759
11760           /* Extract the offset.  */
11761           tmp = new_loc_descr (DW_OP_deref, 0, 0);
11762           add_loc_descr (&loc_descr, tmp);
11763
11764           /* Add it to the object address.  */
11765           tmp = new_loc_descr (DW_OP_plus, 0, 0);
11766           add_loc_descr (&loc_descr, tmp);
11767         }
11768       else
11769         offset = tree_low_cst (BINFO_OFFSET (decl), 0);
11770     }
11771   else
11772     offset = field_byte_offset (decl);
11773
11774   if (! loc_descr)
11775     {
11776       if (dwarf_version > 2)
11777         {
11778           /* Don't need to output a location expression, just the constant. */
11779           add_AT_int (die, DW_AT_data_member_location, offset);
11780           return;
11781         }
11782       else
11783         {
11784           enum dwarf_location_atom op;
11785           
11786           /* The DWARF2 standard says that we should assume that the structure
11787              address is already on the stack, so we can specify a structure
11788              field address by using DW_OP_plus_uconst.  */
11789           
11790 #ifdef MIPS_DEBUGGING_INFO
11791           /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
11792              operator correctly.  It works only if we leave the offset on the
11793              stack.  */
11794           op = DW_OP_constu;
11795 #else
11796           op = DW_OP_plus_uconst;
11797 #endif
11798           
11799           loc_descr = new_loc_descr (op, offset, 0);
11800         }
11801     }
11802
11803   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
11804 }
11805
11806 /* Writes integer values to dw_vec_const array.  */
11807
11808 static void
11809 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
11810 {
11811   while (size != 0)
11812     {
11813       *dest++ = val & 0xff;
11814       val >>= 8;
11815       --size;
11816     }
11817 }
11818
11819 /* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
11820
11821 static HOST_WIDE_INT
11822 extract_int (const unsigned char *src, unsigned int size)
11823 {
11824   HOST_WIDE_INT val = 0;
11825
11826   src += size;
11827   while (size != 0)
11828     {
11829       val <<= 8;
11830       val |= *--src & 0xff;
11831       --size;
11832     }
11833   return val;
11834 }
11835
11836 /* Writes floating point values to dw_vec_const array.  */
11837
11838 static void
11839 insert_float (const_rtx rtl, unsigned char *array)
11840 {
11841   REAL_VALUE_TYPE rv;
11842   long val[4];
11843   int i;
11844
11845   REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
11846   real_to_target (val, &rv, GET_MODE (rtl));
11847
11848   /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
11849   for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
11850     {
11851       insert_int (val[i], 4, array);
11852       array += 4;
11853     }
11854 }
11855
11856 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
11857    does not have a "location" either in memory or in a register.  These
11858    things can arise in GNU C when a constant is passed as an actual parameter
11859    to an inlined function.  They can also arise in C++ where declared
11860    constants do not necessarily get memory "homes".  */
11861
11862 static void
11863 add_const_value_attribute (dw_die_ref die, rtx rtl)
11864 {
11865   switch (GET_CODE (rtl))
11866     {
11867     case CONST_INT:
11868       {
11869         HOST_WIDE_INT val = INTVAL (rtl);
11870
11871         if (val < 0)
11872           add_AT_int (die, DW_AT_const_value, val);
11873         else
11874           add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
11875       }
11876       break;
11877
11878     case CONST_DOUBLE:
11879       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
11880          floating-point constant.  A CONST_DOUBLE is used whenever the
11881          constant requires more than one word in order to be adequately
11882          represented.  We output CONST_DOUBLEs as blocks.  */
11883       {
11884         enum machine_mode mode = GET_MODE (rtl);
11885
11886         if (SCALAR_FLOAT_MODE_P (mode))
11887           {
11888             unsigned int length = GET_MODE_SIZE (mode);
11889             unsigned char *array = GGC_NEWVEC (unsigned char, length);
11890
11891             insert_float (rtl, array);
11892             add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
11893           }
11894         else
11895           {
11896             /* ??? We really should be using HOST_WIDE_INT throughout.  */
11897             gcc_assert (HOST_BITS_PER_LONG == HOST_BITS_PER_WIDE_INT);
11898
11899             add_AT_long_long (die, DW_AT_const_value,
11900                               CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
11901           }
11902       }
11903       break;
11904
11905     case CONST_VECTOR:
11906       {
11907         enum machine_mode mode = GET_MODE (rtl);
11908         unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
11909         unsigned int length = CONST_VECTOR_NUNITS (rtl);
11910         unsigned char *array = GGC_NEWVEC (unsigned char, length * elt_size);
11911         unsigned int i;
11912         unsigned char *p;
11913
11914         switch (GET_MODE_CLASS (mode))
11915           {
11916           case MODE_VECTOR_INT:
11917             for (i = 0, p = array; i < length; i++, p += elt_size)
11918               {
11919                 rtx elt = CONST_VECTOR_ELT (rtl, i);
11920                 HOST_WIDE_INT lo, hi;
11921
11922                 switch (GET_CODE (elt))
11923                   {
11924                   case CONST_INT:
11925                     lo = INTVAL (elt);
11926                     hi = -(lo < 0);
11927                     break;
11928
11929                   case CONST_DOUBLE:
11930                     lo = CONST_DOUBLE_LOW (elt);
11931                     hi = CONST_DOUBLE_HIGH (elt);
11932                     break;
11933
11934                   default:
11935                     gcc_unreachable ();
11936                   }
11937
11938                 if (elt_size <= sizeof (HOST_WIDE_INT))
11939                   insert_int (lo, elt_size, p);
11940                 else
11941                   {
11942                     unsigned char *p0 = p;
11943                     unsigned char *p1 = p + sizeof (HOST_WIDE_INT);
11944
11945                     gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
11946                     if (WORDS_BIG_ENDIAN)
11947                       {
11948                         p0 = p1;
11949                         p1 = p;
11950                       }
11951                     insert_int (lo, sizeof (HOST_WIDE_INT), p0);
11952                     insert_int (hi, sizeof (HOST_WIDE_INT), p1);
11953                   }
11954               }
11955             break;
11956
11957           case MODE_VECTOR_FLOAT:
11958             for (i = 0, p = array; i < length; i++, p += elt_size)
11959               {
11960                 rtx elt = CONST_VECTOR_ELT (rtl, i);
11961                 insert_float (elt, p);
11962               }
11963             break;
11964
11965           default:
11966             gcc_unreachable ();
11967           }
11968
11969         add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
11970       }
11971       break;
11972
11973     case CONST_STRING:
11974       add_AT_string (die, DW_AT_const_value, XSTR (rtl, 0));
11975       break;
11976
11977     case SYMBOL_REF:
11978     case LABEL_REF:
11979     case CONST:
11980       add_AT_addr (die, DW_AT_const_value, rtl);
11981       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
11982       break;
11983
11984     case PLUS:
11985       /* In cases where an inlined instance of an inline function is passed
11986          the address of an `auto' variable (which is local to the caller) we
11987          can get a situation where the DECL_RTL of the artificial local
11988          variable (for the inlining) which acts as a stand-in for the
11989          corresponding formal parameter (of the inline function) will look
11990          like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
11991          exactly a compile-time constant expression, but it isn't the address
11992          of the (artificial) local variable either.  Rather, it represents the
11993          *value* which the artificial local variable always has during its
11994          lifetime.  We currently have no way to represent such quasi-constant
11995          values in Dwarf, so for now we just punt and generate nothing.  */
11996       break;
11997
11998     default:
11999       /* No other kinds of rtx should be possible here.  */
12000       gcc_unreachable ();
12001     }
12002
12003 }
12004
12005 /* Determine whether the evaluation of EXPR references any variables
12006    or functions which aren't otherwise used (and therefore may not be
12007    output).  */
12008 static tree
12009 reference_to_unused (tree * tp, int * walk_subtrees,
12010                      void * data ATTRIBUTE_UNUSED)
12011 {
12012   if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
12013     *walk_subtrees = 0;
12014
12015   if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
12016       && ! TREE_ASM_WRITTEN (*tp))
12017     return *tp;
12018   /* ???  The C++ FE emits debug information for using decls, so
12019      putting gcc_unreachable here falls over.  See PR31899.  For now
12020      be conservative.  */
12021   else if (!cgraph_global_info_ready
12022            && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
12023     return *tp;
12024   else if (DECL_P (*tp) && TREE_CODE (*tp) == VAR_DECL)
12025     {
12026       struct varpool_node *node = varpool_node (*tp);
12027       if (!node->needed)
12028         return *tp;
12029     }
12030   else if (DECL_P (*tp) && TREE_CODE (*tp) == FUNCTION_DECL
12031            && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
12032     {
12033       struct cgraph_node *node = cgraph_node (*tp);
12034       if (node->process || TREE_ASM_WRITTEN (*tp))
12035         return *tp;
12036     }
12037   else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
12038     return *tp;
12039
12040   return NULL_TREE;
12041 }
12042
12043 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
12044    for use in a later add_const_value_attribute call.  */
12045
12046 static rtx
12047 rtl_for_decl_init (tree init, tree type)
12048 {
12049   rtx rtl = NULL_RTX;
12050
12051   /* If a variable is initialized with a string constant without embedded
12052      zeros, build CONST_STRING.  */
12053   if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
12054     {
12055       tree enttype = TREE_TYPE (type);
12056       tree domain = TYPE_DOMAIN (type);
12057       enum machine_mode mode = TYPE_MODE (enttype);
12058
12059       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
12060           && domain
12061           && integer_zerop (TYPE_MIN_VALUE (domain))
12062           && compare_tree_int (TYPE_MAX_VALUE (domain),
12063                                TREE_STRING_LENGTH (init) - 1) == 0
12064           && ((size_t) TREE_STRING_LENGTH (init)
12065               == strlen (TREE_STRING_POINTER (init)) + 1))
12066         rtl = gen_rtx_CONST_STRING (VOIDmode,
12067                                     ggc_strdup (TREE_STRING_POINTER (init)));
12068     }
12069   /* Other aggregates, and complex values, could be represented using
12070      CONCAT: FIXME!  */
12071   else if (AGGREGATE_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
12072     ;
12073   /* Vectors only work if their mode is supported by the target.
12074      FIXME: generic vectors ought to work too.  */
12075   else if (TREE_CODE (type) == VECTOR_TYPE && TYPE_MODE (type) == BLKmode)
12076     ;
12077   /* If the initializer is something that we know will expand into an
12078      immediate RTL constant, expand it now.  We must be careful not to
12079      reference variables which won't be output.  */
12080   else if (initializer_constant_valid_p (init, type)
12081            && ! walk_tree (&init, reference_to_unused, NULL, NULL))
12082     {
12083       /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
12084          possible.  */
12085       if (TREE_CODE (type) == VECTOR_TYPE)
12086         switch (TREE_CODE (init))
12087           {
12088           case VECTOR_CST:
12089             break;
12090           case CONSTRUCTOR:
12091             if (TREE_CONSTANT (init))
12092               {
12093                 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
12094                 bool constant_p = true;
12095                 tree value;
12096                 unsigned HOST_WIDE_INT ix;
12097
12098                 /* Even when ctor is constant, it might contain non-*_CST
12099                    elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
12100                    belong into VECTOR_CST nodes.  */
12101                 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
12102                   if (!CONSTANT_CLASS_P (value))
12103                     {
12104                       constant_p = false;
12105                       break;
12106                     }
12107
12108                 if (constant_p)
12109                   {
12110                     init = build_vector_from_ctor (type, elts);
12111                     break;
12112                   }
12113               }
12114             /* FALLTHRU */
12115
12116           default:
12117             return NULL;
12118           }
12119
12120       rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
12121
12122       /* If expand_expr returns a MEM, it wasn't immediate.  */
12123       gcc_assert (!rtl || !MEM_P (rtl));
12124     }
12125
12126   return rtl;
12127 }
12128
12129 /* Generate RTL for the variable DECL to represent its location.  */
12130
12131 static rtx
12132 rtl_for_decl_location (tree decl)
12133 {
12134   rtx rtl;
12135
12136   /* Here we have to decide where we are going to say the parameter "lives"
12137      (as far as the debugger is concerned).  We only have a couple of
12138      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
12139
12140      DECL_RTL normally indicates where the parameter lives during most of the
12141      activation of the function.  If optimization is enabled however, this
12142      could be either NULL or else a pseudo-reg.  Both of those cases indicate
12143      that the parameter doesn't really live anywhere (as far as the code
12144      generation parts of GCC are concerned) during most of the function's
12145      activation.  That will happen (for example) if the parameter is never
12146      referenced within the function.
12147
12148      We could just generate a location descriptor here for all non-NULL
12149      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
12150      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
12151      where DECL_RTL is NULL or is a pseudo-reg.
12152
12153      Note however that we can only get away with using DECL_INCOMING_RTL as
12154      a backup substitute for DECL_RTL in certain limited cases.  In cases
12155      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
12156      we can be sure that the parameter was passed using the same type as it is
12157      declared to have within the function, and that its DECL_INCOMING_RTL
12158      points us to a place where a value of that type is passed.
12159
12160      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
12161      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
12162      because in these cases DECL_INCOMING_RTL points us to a value of some
12163      type which is *different* from the type of the parameter itself.  Thus,
12164      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
12165      such cases, the debugger would end up (for example) trying to fetch a
12166      `float' from a place which actually contains the first part of a
12167      `double'.  That would lead to really incorrect and confusing
12168      output at debug-time.
12169
12170      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
12171      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
12172      are a couple of exceptions however.  On little-endian machines we can
12173      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
12174      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
12175      an integral type that is smaller than TREE_TYPE (decl). These cases arise
12176      when (on a little-endian machine) a non-prototyped function has a
12177      parameter declared to be of type `short' or `char'.  In such cases,
12178      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
12179      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
12180      passed `int' value.  If the debugger then uses that address to fetch
12181      a `short' or a `char' (on a little-endian machine) the result will be
12182      the correct data, so we allow for such exceptional cases below.
12183
12184      Note that our goal here is to describe the place where the given formal
12185      parameter lives during most of the function's activation (i.e. between the
12186      end of the prologue and the start of the epilogue).  We'll do that as best
12187      as we can. Note however that if the given formal parameter is modified
12188      sometime during the execution of the function, then a stack backtrace (at
12189      debug-time) will show the function as having been called with the *new*
12190      value rather than the value which was originally passed in.  This happens
12191      rarely enough that it is not a major problem, but it *is* a problem, and
12192      I'd like to fix it.
12193
12194      A future version of dwarf2out.c may generate two additional attributes for
12195      any given DW_TAG_formal_parameter DIE which will describe the "passed
12196      type" and the "passed location" for the given formal parameter in addition
12197      to the attributes we now generate to indicate the "declared type" and the
12198      "active location" for each parameter.  This additional set of attributes
12199      could be used by debuggers for stack backtraces. Separately, note that
12200      sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
12201      This happens (for example) for inlined-instances of inline function formal
12202      parameters which are never referenced.  This really shouldn't be
12203      happening.  All PARM_DECL nodes should get valid non-NULL
12204      DECL_INCOMING_RTL values.  FIXME.  */
12205
12206   /* Use DECL_RTL as the "location" unless we find something better.  */
12207   rtl = DECL_RTL_IF_SET (decl);
12208
12209   /* When generating abstract instances, ignore everything except
12210      constants, symbols living in memory, and symbols living in
12211      fixed registers.  */
12212   if (! reload_completed)
12213     {
12214       if (rtl
12215           && (CONSTANT_P (rtl)
12216               || (MEM_P (rtl)
12217                   && CONSTANT_P (XEXP (rtl, 0)))
12218               || (REG_P (rtl)
12219                   && TREE_CODE (decl) == VAR_DECL
12220                   && TREE_STATIC (decl))))
12221         {
12222           rtl = targetm.delegitimize_address (rtl);
12223           return rtl;
12224         }
12225       rtl = NULL_RTX;
12226     }
12227   else if (TREE_CODE (decl) == PARM_DECL)
12228     {
12229       if (rtl == NULL_RTX || is_pseudo_reg (rtl))
12230         {
12231           tree declared_type = TREE_TYPE (decl);
12232           tree passed_type = DECL_ARG_TYPE (decl);
12233           enum machine_mode dmode = TYPE_MODE (declared_type);
12234           enum machine_mode pmode = TYPE_MODE (passed_type);
12235
12236           /* This decl represents a formal parameter which was optimized out.
12237              Note that DECL_INCOMING_RTL may be NULL in here, but we handle
12238              all cases where (rtl == NULL_RTX) just below.  */
12239           if (dmode == pmode)
12240             rtl = DECL_INCOMING_RTL (decl);
12241           else if (SCALAR_INT_MODE_P (dmode)
12242                    && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
12243                    && DECL_INCOMING_RTL (decl))
12244             {
12245               rtx inc = DECL_INCOMING_RTL (decl);
12246               if (REG_P (inc))
12247                 rtl = inc;
12248               else if (MEM_P (inc))
12249                 {
12250                   if (BYTES_BIG_ENDIAN)
12251                     rtl = adjust_address_nv (inc, dmode,
12252                                              GET_MODE_SIZE (pmode)
12253                                              - GET_MODE_SIZE (dmode));
12254                   else
12255                     rtl = inc;
12256                 }
12257             }
12258         }
12259
12260       /* If the parm was passed in registers, but lives on the stack, then
12261          make a big endian correction if the mode of the type of the
12262          parameter is not the same as the mode of the rtl.  */
12263       /* ??? This is the same series of checks that are made in dbxout.c before
12264          we reach the big endian correction code there.  It isn't clear if all
12265          of these checks are necessary here, but keeping them all is the safe
12266          thing to do.  */
12267       else if (MEM_P (rtl)
12268                && XEXP (rtl, 0) != const0_rtx
12269                && ! CONSTANT_P (XEXP (rtl, 0))
12270                /* Not passed in memory.  */
12271                && !MEM_P (DECL_INCOMING_RTL (decl))
12272                /* Not passed by invisible reference.  */
12273                && (!REG_P (XEXP (rtl, 0))
12274                    || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
12275                    || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
12276 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
12277                    || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
12278 #endif
12279                      )
12280                /* Big endian correction check.  */
12281                && BYTES_BIG_ENDIAN
12282                && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
12283                && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
12284                    < UNITS_PER_WORD))
12285         {
12286           int offset = (UNITS_PER_WORD
12287                         - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
12288
12289           rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
12290                              plus_constant (XEXP (rtl, 0), offset));
12291         }
12292     }
12293   else if (TREE_CODE (decl) == VAR_DECL
12294            && rtl
12295            && MEM_P (rtl)
12296            && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
12297            && BYTES_BIG_ENDIAN)
12298     {
12299       int rsize = GET_MODE_SIZE (GET_MODE (rtl));
12300       int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
12301
12302       /* If a variable is declared "register" yet is smaller than
12303          a register, then if we store the variable to memory, it
12304          looks like we're storing a register-sized value, when in
12305          fact we are not.  We need to adjust the offset of the
12306          storage location to reflect the actual value's bytes,
12307          else gdb will not be able to display it.  */
12308       if (rsize > dsize)
12309         rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
12310                            plus_constant (XEXP (rtl, 0), rsize-dsize));
12311     }
12312
12313   /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
12314      and will have been substituted directly into all expressions that use it.
12315      C does not have such a concept, but C++ and other languages do.  */
12316   if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
12317     rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
12318
12319   if (rtl)
12320     rtl = targetm.delegitimize_address (rtl);
12321
12322   /* If we don't look past the constant pool, we risk emitting a
12323      reference to a constant pool entry that isn't referenced from
12324      code, and thus is not emitted.  */
12325   if (rtl)
12326     rtl = avoid_constant_pool_reference (rtl);
12327
12328   return rtl;
12329 }
12330
12331 /* We need to figure out what section we should use as the base for the
12332    address ranges where a given location is valid.
12333    1. If this particular DECL has a section associated with it, use that.
12334    2. If this function has a section associated with it, use that.
12335    3. Otherwise, use the text section.
12336    XXX: If you split a variable across multiple sections, we won't notice.  */
12337
12338 static const char *
12339 secname_for_decl (const_tree decl)
12340 {
12341   const char *secname;
12342
12343   if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
12344     {
12345       tree sectree = DECL_SECTION_NAME (decl);
12346       secname = TREE_STRING_POINTER (sectree);
12347     }
12348   else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
12349     {
12350       tree sectree = DECL_SECTION_NAME (current_function_decl);
12351       secname = TREE_STRING_POINTER (sectree);
12352     }
12353   else if (cfun && in_cold_section_p)
12354     secname = crtl->subsections.cold_section_label;
12355   else
12356     secname = text_section_label;
12357
12358   return secname;
12359 }
12360
12361 /* Check whether decl is a Fortran COMMON symbol.  If not, NULL_TREE is
12362    returned.  If so, the decl for the COMMON block is returned, and the
12363    value is the offset into the common block for the symbol.  */
12364
12365 static tree
12366 fortran_common (tree decl, HOST_WIDE_INT *value)
12367 {
12368   tree val_expr, cvar;
12369   enum machine_mode mode;
12370   HOST_WIDE_INT bitsize, bitpos;
12371   tree offset;
12372   int volatilep = 0, unsignedp = 0;
12373
12374   /* If the decl isn't a VAR_DECL, or if it isn't public or static, or if
12375      it does not have a value (the offset into the common area), or if it
12376      is thread local (as opposed to global) then it isn't common, and shouldn't
12377      be handled as such.  */
12378   if (TREE_CODE (decl) != VAR_DECL
12379       || !TREE_PUBLIC (decl)
12380       || !TREE_STATIC (decl)
12381       || !DECL_HAS_VALUE_EXPR_P (decl)
12382       || !is_fortran ())
12383     return NULL_TREE;
12384
12385   val_expr = DECL_VALUE_EXPR (decl);
12386   if (TREE_CODE (val_expr) != COMPONENT_REF)
12387     return NULL_TREE;
12388
12389   cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
12390                               &mode, &unsignedp, &volatilep, true);
12391
12392   if (cvar == NULL_TREE
12393       || TREE_CODE (cvar) != VAR_DECL
12394       || DECL_ARTIFICIAL (cvar)
12395       || !TREE_PUBLIC (cvar))
12396     return NULL_TREE;
12397
12398   *value = 0;
12399   if (offset != NULL)
12400     {
12401       if (!host_integerp (offset, 0))
12402         return NULL_TREE;
12403       *value = tree_low_cst (offset, 0);
12404     }
12405   if (bitpos != 0)
12406     *value += bitpos / BITS_PER_UNIT;
12407
12408   return cvar;
12409 }
12410
12411 /* Dereference a location expression LOC if DECL is passed by invisible
12412    reference.  */
12413
12414 static dw_loc_descr_ref
12415 loc_by_reference (dw_loc_descr_ref loc, tree decl)
12416 {
12417   HOST_WIDE_INT size;
12418   enum dwarf_location_atom op;
12419
12420   if (loc == NULL)
12421     return NULL;
12422
12423   if ((TREE_CODE (decl) != PARM_DECL
12424        && TREE_CODE (decl) != RESULT_DECL
12425        && TREE_CODE (decl) != VAR_DECL)
12426       || !DECL_BY_REFERENCE (decl))
12427     return loc;
12428
12429   /* If loc is DW_OP_reg{0...31,x}, don't add DW_OP_deref, instead
12430      change it into corresponding DW_OP_breg{0...31,x} 0.  Then the
12431      location expression is considered to be address of a memory location,
12432      rather than the register itself.  */
12433   if (((loc->dw_loc_opc >= DW_OP_reg0 && loc->dw_loc_opc <= DW_OP_reg31)
12434        || loc->dw_loc_opc == DW_OP_regx)
12435       && (loc->dw_loc_next == NULL
12436           || (loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_uninit
12437               && loc->dw_loc_next->dw_loc_next == NULL)))
12438     {
12439       if (loc->dw_loc_opc == DW_OP_regx)
12440         {
12441           loc->dw_loc_opc = DW_OP_bregx;
12442           loc->dw_loc_oprnd2.v.val_int = 0;
12443         }
12444       else
12445         {
12446           loc->dw_loc_opc
12447             = (enum dwarf_location_atom)
12448               (loc->dw_loc_opc + (DW_OP_breg0 - DW_OP_reg0));
12449           loc->dw_loc_oprnd1.v.val_int = 0;
12450         }
12451       return loc;
12452     }
12453
12454   size = int_size_in_bytes (TREE_TYPE (decl));
12455   if (size > DWARF2_ADDR_SIZE || size == -1)
12456     return 0;
12457   else if (size == DWARF2_ADDR_SIZE)
12458     op = DW_OP_deref;
12459   else
12460     op = DW_OP_deref_size;
12461   add_loc_descr (&loc, new_loc_descr (op, size, 0));
12462   return loc;
12463 }
12464
12465 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
12466    data attribute for a variable or a parameter.  We generate the
12467    DW_AT_const_value attribute only in those cases where the given variable
12468    or parameter does not have a true "location" either in memory or in a
12469    register.  This can happen (for example) when a constant is passed as an
12470    actual argument in a call to an inline function.  (It's possible that
12471    these things can crop up in other ways also.)  Note that one type of
12472    constant value which can be passed into an inlined function is a constant
12473    pointer.  This can happen for example if an actual argument in an inlined
12474    function call evaluates to a compile-time constant address.  */
12475
12476 static void
12477 add_location_or_const_value_attribute (dw_die_ref die, tree decl,
12478                                        enum dwarf_attribute attr)
12479 {
12480   rtx rtl;
12481   dw_loc_descr_ref descr;
12482   var_loc_list *loc_list;
12483   struct var_loc_node *node;
12484   if (TREE_CODE (decl) == ERROR_MARK)
12485     return;
12486
12487   gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
12488               || TREE_CODE (decl) == RESULT_DECL);
12489
12490   /* See if we possibly have multiple locations for this variable.  */
12491   loc_list = lookup_decl_loc (decl);
12492
12493   /* If it truly has multiple locations, the first and last node will
12494      differ.  */
12495   if (loc_list && loc_list->first != loc_list->last)
12496     {
12497       const char *endname, *secname;
12498       dw_loc_list_ref list;
12499       rtx varloc;
12500       enum var_init_status initialized;
12501
12502       /* Now that we know what section we are using for a base,
12503          actually construct the list of locations.
12504          The first location information is what is passed to the
12505          function that creates the location list, and the remaining
12506          locations just get added on to that list.
12507          Note that we only know the start address for a location
12508          (IE location changes), so to build the range, we use
12509          the range [current location start, next location start].
12510          This means we have to special case the last node, and generate
12511          a range of [last location start, end of function label].  */
12512
12513       node = loc_list->first;
12514       varloc = NOTE_VAR_LOCATION (node->var_loc_note);
12515       secname = secname_for_decl (decl);
12516
12517       if (NOTE_VAR_LOCATION_LOC (node->var_loc_note))
12518         initialized = NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
12519       else
12520         initialized = VAR_INIT_STATUS_INITIALIZED;
12521
12522       descr = loc_by_reference (loc_descriptor (varloc, initialized), decl);
12523       list = new_loc_list (descr, node->label, node->next->label, secname, 1);
12524       node = node->next;
12525
12526       for (; node->next; node = node->next)
12527         if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
12528           {
12529             /* The variable has a location between NODE->LABEL and
12530                NODE->NEXT->LABEL.  */
12531             enum var_init_status initialized =
12532               NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
12533             varloc = NOTE_VAR_LOCATION (node->var_loc_note);
12534             descr = loc_by_reference (loc_descriptor (varloc, initialized),
12535                                       decl);
12536             add_loc_descr_to_loc_list (&list, descr,
12537                                        node->label, node->next->label, secname);
12538           }
12539
12540       /* If the variable has a location at the last label
12541          it keeps its location until the end of function.  */
12542       if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
12543         {
12544           char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
12545           enum var_init_status initialized =
12546             NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
12547
12548           varloc = NOTE_VAR_LOCATION (node->var_loc_note);
12549           if (!current_function_decl)
12550             endname = text_end_label;
12551           else
12552             {
12553               ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
12554                                            current_function_funcdef_no);
12555               endname = ggc_strdup (label_id);
12556             }
12557           descr = loc_by_reference (loc_descriptor (varloc, initialized),
12558                                     decl);
12559           add_loc_descr_to_loc_list (&list, descr,
12560                                      node->label, endname, secname);
12561         }
12562
12563       /* Finally, add the location list to the DIE, and we are done.  */
12564       add_AT_loc_list (die, attr, list);
12565       return;
12566     }
12567
12568   /* Try to get some constant RTL for this decl, and use that as the value of
12569      the location.  */
12570
12571   rtl = rtl_for_decl_location (decl);
12572   if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING))
12573     {
12574       add_const_value_attribute (die, rtl);
12575       return;
12576     }
12577
12578   /* If we have tried to generate the location otherwise, and it
12579      didn't work out (we wouldn't be here if we did), and we have a one entry
12580      location list, try generating a location from that.  */
12581   if (loc_list && loc_list->first)
12582     {
12583       enum var_init_status status;
12584       node = loc_list->first;
12585       status = NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
12586       descr = loc_descriptor (NOTE_VAR_LOCATION (node->var_loc_note), status);
12587       if (descr)
12588         {
12589           descr = loc_by_reference (descr, decl);
12590           add_AT_location_description (die, attr, descr);
12591           return;
12592         }
12593     }
12594
12595   /* We couldn't get any rtl, so try directly generating the location
12596      description from the tree.  */
12597   descr = loc_descriptor_from_tree (decl);
12598   if (descr)
12599     {
12600       descr = loc_by_reference (descr, decl);
12601       add_AT_location_description (die, attr, descr);
12602       return;
12603     }
12604   /* None of that worked, so it must not really have a location;
12605      try adding a constant value attribute from the DECL_INITIAL.  */
12606   tree_add_const_value_attribute (die, decl);
12607 }
12608
12609 /* Add VARIABLE and DIE into deferred locations list.  */
12610
12611 static void
12612 defer_location (tree variable, dw_die_ref die)
12613 {
12614   deferred_locations entry;
12615   entry.variable = variable;
12616   entry.die = die;
12617   VEC_safe_push (deferred_locations, gc, deferred_locations_list, &entry);
12618 }
12619
12620 /* Helper function for tree_add_const_value_attribute.  Natively encode
12621    initializer INIT into an array.  Return true if successful.  */
12622
12623 static bool
12624 native_encode_initializer (tree init, unsigned char *array, int size)
12625 {
12626   tree type;
12627
12628   if (init == NULL_TREE)
12629     return false;
12630
12631   STRIP_NOPS (init);
12632   switch (TREE_CODE (init))
12633     {
12634     case STRING_CST:
12635       type = TREE_TYPE (init);
12636       if (TREE_CODE (type) == ARRAY_TYPE)
12637         {
12638           tree enttype = TREE_TYPE (type);
12639           enum machine_mode mode = TYPE_MODE (enttype);
12640
12641           if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
12642             return false;
12643           if (int_size_in_bytes (type) != size)
12644             return false;
12645           if (size > TREE_STRING_LENGTH (init))
12646             {
12647               memcpy (array, TREE_STRING_POINTER (init),
12648                       TREE_STRING_LENGTH (init));
12649               memset (array + TREE_STRING_LENGTH (init),
12650                       '\0', size - TREE_STRING_LENGTH (init));
12651             }
12652           else
12653             memcpy (array, TREE_STRING_POINTER (init), size);
12654           return true;
12655         }
12656       return false;
12657     case CONSTRUCTOR:
12658       type = TREE_TYPE (init);
12659       if (int_size_in_bytes (type) != size)
12660         return false;
12661       if (TREE_CODE (type) == ARRAY_TYPE)
12662         {
12663           HOST_WIDE_INT min_index;
12664           unsigned HOST_WIDE_INT cnt;
12665           int curpos = 0, fieldsize;
12666           constructor_elt *ce;
12667
12668           if (TYPE_DOMAIN (type) == NULL_TREE
12669               || !host_integerp (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0))
12670             return false;
12671
12672           fieldsize = int_size_in_bytes (TREE_TYPE (type));
12673           if (fieldsize <= 0)
12674             return false;
12675
12676           min_index = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0);
12677           memset (array, '\0', size);
12678           for (cnt = 0;
12679                VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce);
12680                cnt++)
12681             {
12682               tree val = ce->value;
12683               tree index = ce->index;
12684               int pos = curpos;
12685               if (index && TREE_CODE (index) == RANGE_EXPR)
12686                 pos = (tree_low_cst (TREE_OPERAND (index, 0), 0) - min_index)
12687                       * fieldsize;
12688               else if (index)
12689                 pos = (tree_low_cst (index, 0) - min_index) * fieldsize;
12690
12691               if (val)
12692                 {
12693                   STRIP_NOPS (val);
12694                   if (!native_encode_initializer (val, array + pos, fieldsize))
12695                     return false;
12696                 }
12697               curpos = pos + fieldsize;
12698               if (index && TREE_CODE (index) == RANGE_EXPR)
12699                 {
12700                   int count = tree_low_cst (TREE_OPERAND (index, 1), 0)
12701                               - tree_low_cst (TREE_OPERAND (index, 0), 0);
12702                   while (count > 0)
12703                     {
12704                       if (val)
12705                         memcpy (array + curpos, array + pos, fieldsize);
12706                       curpos += fieldsize;
12707                     }
12708                 }
12709               gcc_assert (curpos <= size);
12710             }
12711           return true;
12712         }
12713       else if (TREE_CODE (type) == RECORD_TYPE
12714                || TREE_CODE (type) == UNION_TYPE)
12715         {
12716           tree field = NULL_TREE;
12717           unsigned HOST_WIDE_INT cnt;
12718           constructor_elt *ce;
12719
12720           if (int_size_in_bytes (type) != size)
12721             return false;
12722
12723           if (TREE_CODE (type) == RECORD_TYPE)
12724             field = TYPE_FIELDS (type);
12725
12726           for (cnt = 0;
12727                VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce);
12728                cnt++, field = field ? TREE_CHAIN (field) : 0)
12729             {
12730               tree val = ce->value;
12731               int pos, fieldsize;
12732
12733               if (ce->index != 0)
12734                 field = ce->index;
12735
12736               if (val)
12737                 STRIP_NOPS (val);
12738
12739               if (field == NULL_TREE || DECL_BIT_FIELD (field))
12740                 return false;
12741
12742               if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
12743                   && TYPE_DOMAIN (TREE_TYPE (field))
12744                   && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
12745                 return false;
12746               else if (DECL_SIZE_UNIT (field) == NULL_TREE
12747                        || !host_integerp (DECL_SIZE_UNIT (field), 0))
12748                 return false;
12749               fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 0);
12750               pos = int_byte_position (field);
12751               gcc_assert (pos + fieldsize <= size);
12752               if (val
12753                   && !native_encode_initializer (val, array + pos, fieldsize))
12754                 return false;
12755             }
12756           return true;
12757         }
12758       return false;
12759     case VIEW_CONVERT_EXPR:
12760     case NON_LVALUE_EXPR:
12761       return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
12762     default:
12763       return native_encode_expr (init, array, size) == size;
12764     }
12765 }
12766
12767 /* If we don't have a copy of this variable in memory for some reason (such
12768    as a C++ member constant that doesn't have an out-of-line definition),
12769    we should tell the debugger about the constant value.  */
12770
12771 static void
12772 tree_add_const_value_attribute (dw_die_ref var_die, tree decl)
12773 {
12774   tree init;
12775   tree type = TREE_TYPE (decl);
12776   rtx rtl;
12777
12778   if (TREE_CODE (decl) != VAR_DECL && TREE_CODE (decl) != CONST_DECL)
12779     return;
12780
12781   init = DECL_INITIAL (decl);
12782   if (TREE_READONLY (decl) && ! TREE_THIS_VOLATILE (decl) && init)
12783     /* OK */;
12784   else
12785     return;
12786
12787   rtl = rtl_for_decl_init (init, type);
12788   if (rtl)
12789     add_const_value_attribute (var_die, rtl);
12790   /* If the host and target are sane, try harder.  */
12791   else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
12792            && initializer_constant_valid_p (init, type))
12793     {
12794       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
12795       if (size > 0 && (int) size == size)
12796         {
12797           unsigned char *array = GGC_CNEWVEC (unsigned char, size);
12798
12799           if (native_encode_initializer (init, array, size))
12800             add_AT_vec (var_die, DW_AT_const_value, size, 1, array);
12801         }
12802     }
12803 }
12804
12805 /* Convert the CFI instructions for the current function into a
12806    location list.  This is used for DW_AT_frame_base when we targeting
12807    a dwarf2 consumer that does not support the dwarf3
12808    DW_OP_call_frame_cfa.  OFFSET is a constant to be added to all CFA
12809    expressions.  */
12810
12811 static dw_loc_list_ref
12812 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
12813 {
12814   dw_fde_ref fde;
12815   dw_loc_list_ref list, *list_tail;
12816   dw_cfi_ref cfi;
12817   dw_cfa_location last_cfa, next_cfa;
12818   const char *start_label, *last_label, *section;
12819   dw_cfa_location remember;
12820
12821   fde = current_fde ();
12822   gcc_assert (fde != NULL);
12823
12824   section = secname_for_decl (current_function_decl);
12825   list_tail = &list;
12826   list = NULL;
12827
12828   memset (&next_cfa, 0, sizeof (next_cfa));
12829   next_cfa.reg = INVALID_REGNUM;
12830   remember = next_cfa;
12831
12832   start_label = fde->dw_fde_begin;
12833
12834   /* ??? Bald assumption that the CIE opcode list does not contain
12835      advance opcodes.  */
12836   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
12837     lookup_cfa_1 (cfi, &next_cfa, &remember);
12838
12839   last_cfa = next_cfa;
12840   last_label = start_label;
12841
12842   for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
12843     switch (cfi->dw_cfi_opc)
12844       {
12845       case DW_CFA_set_loc:
12846       case DW_CFA_advance_loc1:
12847       case DW_CFA_advance_loc2:
12848       case DW_CFA_advance_loc4:
12849         if (!cfa_equal_p (&last_cfa, &next_cfa))
12850           {
12851             *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
12852                                        start_label, last_label, section,
12853                                        list == NULL);
12854
12855             list_tail = &(*list_tail)->dw_loc_next;
12856             last_cfa = next_cfa;
12857             start_label = last_label;
12858           }
12859         last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
12860         break;
12861
12862       case DW_CFA_advance_loc:
12863         /* The encoding is complex enough that we should never emit this.  */
12864         gcc_unreachable ();
12865
12866       default:
12867         lookup_cfa_1 (cfi, &next_cfa, &remember);
12868         break;
12869       }
12870
12871   if (!cfa_equal_p (&last_cfa, &next_cfa))
12872     {
12873       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
12874                                  start_label, last_label, section,
12875                                  list == NULL);
12876       list_tail = &(*list_tail)->dw_loc_next;
12877       start_label = last_label;
12878     }
12879   *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
12880                              start_label, fde->dw_fde_end, section,
12881                              list == NULL);
12882
12883   return list;
12884 }
12885
12886 /* Compute a displacement from the "steady-state frame pointer" to the
12887    frame base (often the same as the CFA), and store it in
12888    frame_pointer_fb_offset.  OFFSET is added to the displacement
12889    before the latter is negated.  */
12890
12891 static void
12892 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
12893 {
12894   rtx reg, elim;
12895
12896 #ifdef FRAME_POINTER_CFA_OFFSET
12897   reg = frame_pointer_rtx;
12898   offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
12899 #else
12900   reg = arg_pointer_rtx;
12901   offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
12902 #endif
12903
12904   elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
12905   if (GET_CODE (elim) == PLUS)
12906     {
12907       offset += INTVAL (XEXP (elim, 1));
12908       elim = XEXP (elim, 0);
12909     }
12910
12911   gcc_assert ((SUPPORTS_STACK_ALIGNMENT
12912                && (elim == hard_frame_pointer_rtx
12913                    || elim == stack_pointer_rtx))
12914               || elim == (frame_pointer_needed
12915                           ? hard_frame_pointer_rtx
12916                           : stack_pointer_rtx));
12917
12918   frame_pointer_fb_offset = -offset;
12919 }
12920
12921 /* Generate a DW_AT_name attribute given some string value to be included as
12922    the value of the attribute.  */
12923
12924 static void
12925 add_name_attribute (dw_die_ref die, const char *name_string)
12926 {
12927   if (name_string != NULL && *name_string != 0)
12928     {
12929       if (demangle_name_func)
12930         name_string = (*demangle_name_func) (name_string);
12931
12932       add_AT_string (die, DW_AT_name, name_string);
12933     }
12934 }
12935
12936 /* Generate a DW_AT_comp_dir attribute for DIE.  */
12937
12938 static void
12939 add_comp_dir_attribute (dw_die_ref die)
12940 {
12941   const char *wd = get_src_pwd ();
12942   char *wd1;
12943
12944   if (wd == NULL)
12945     return;
12946
12947   if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
12948     {
12949       int wdlen;
12950
12951       wdlen = strlen (wd);
12952       wd1 = GGC_NEWVEC (char, wdlen + 2);
12953       strcpy (wd1, wd);
12954       wd1 [wdlen] = DIR_SEPARATOR;
12955       wd1 [wdlen + 1] = 0;
12956       wd = wd1;
12957     }
12958
12959     add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
12960 }
12961
12962 /* Given a tree node describing an array bound (either lower or upper) output
12963    a representation for that bound.  */
12964
12965 static void
12966 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
12967 {
12968   switch (TREE_CODE (bound))
12969     {
12970     case ERROR_MARK:
12971       return;
12972
12973     /* All fixed-bounds are represented by INTEGER_CST nodes.  */
12974     case INTEGER_CST:
12975       if (! host_integerp (bound, 0)
12976           || (bound_attr == DW_AT_lower_bound
12977               && (((is_c_family () || is_java ()) &&  integer_zerop (bound))
12978                   || (is_fortran () && integer_onep (bound)))))
12979         /* Use the default.  */
12980         ;
12981       else
12982         add_AT_unsigned (subrange_die, bound_attr, tree_low_cst (bound, 0));
12983       break;
12984
12985     CASE_CONVERT:
12986     case VIEW_CONVERT_EXPR:
12987       add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
12988       break;
12989
12990     case SAVE_EXPR:
12991       break;
12992
12993     case VAR_DECL:
12994     case PARM_DECL:
12995     case RESULT_DECL:
12996       {
12997         dw_die_ref decl_die = lookup_decl_die (bound);
12998         dw_loc_descr_ref loc;
12999
13000         /* ??? Can this happen, or should the variable have been bound
13001            first?  Probably it can, since I imagine that we try to create
13002            the types of parameters in the order in which they exist in
13003            the list, and won't have created a forward reference to a
13004            later parameter.  */
13005         if (decl_die != NULL)
13006           add_AT_die_ref (subrange_die, bound_attr, decl_die);
13007         else
13008           {
13009             loc = loc_descriptor_from_tree_1 (bound, 0);
13010             add_AT_location_description (subrange_die, bound_attr, loc);
13011           }
13012         break;
13013       }
13014
13015     default:
13016       {
13017         /* Otherwise try to create a stack operation procedure to
13018            evaluate the value of the array bound.  */
13019
13020         dw_die_ref ctx, decl_die;
13021         dw_loc_descr_ref loc;
13022
13023         loc = loc_descriptor_from_tree (bound);
13024         if (loc == NULL)
13025           break;
13026
13027         if (current_function_decl == 0)
13028           ctx = comp_unit_die;
13029         else
13030           ctx = lookup_decl_die (current_function_decl);
13031
13032         decl_die = new_die (DW_TAG_variable, ctx, bound);
13033         add_AT_flag (decl_die, DW_AT_artificial, 1);
13034         add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
13035         add_AT_loc (decl_die, DW_AT_location, loc);
13036
13037         add_AT_die_ref (subrange_die, bound_attr, decl_die);
13038         break;
13039       }
13040     }
13041 }
13042
13043 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
13044    possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
13045    Note that the block of subscript information for an array type also
13046    includes information about the element type of the given array type.  */
13047
13048 static void
13049 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
13050 {
13051   unsigned dimension_number;
13052   tree lower, upper;
13053   dw_die_ref subrange_die;
13054
13055   for (dimension_number = 0;
13056        TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
13057        type = TREE_TYPE (type), dimension_number++)
13058     {
13059       tree domain = TYPE_DOMAIN (type);
13060
13061       if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
13062         break;
13063
13064       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
13065          and (in GNU C only) variable bounds.  Handle all three forms
13066          here.  */
13067       subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
13068       if (domain)
13069         {
13070           /* We have an array type with specified bounds.  */
13071           lower = TYPE_MIN_VALUE (domain);
13072           upper = TYPE_MAX_VALUE (domain);
13073
13074           /* Define the index type.  */
13075           if (TREE_TYPE (domain))
13076             {
13077               /* ??? This is probably an Ada unnamed subrange type.  Ignore the
13078                  TREE_TYPE field.  We can't emit debug info for this
13079                  because it is an unnamed integral type.  */
13080               if (TREE_CODE (domain) == INTEGER_TYPE
13081                   && TYPE_NAME (domain) == NULL_TREE
13082                   && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
13083                   && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
13084                 ;
13085               else
13086                 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
13087                                     type_die);
13088             }
13089
13090           /* ??? If upper is NULL, the array has unspecified length,
13091              but it does have a lower bound.  This happens with Fortran
13092                dimension arr(N:*)
13093              Since the debugger is definitely going to need to know N
13094              to produce useful results, go ahead and output the lower
13095              bound solo, and hope the debugger can cope.  */
13096
13097           add_bound_info (subrange_die, DW_AT_lower_bound, lower);
13098           if (upper)
13099             add_bound_info (subrange_die, DW_AT_upper_bound, upper);
13100         }
13101
13102       /* Otherwise we have an array type with an unspecified length.  The
13103          DWARF-2 spec does not say how to handle this; let's just leave out the
13104          bounds.  */
13105     }
13106 }
13107
13108 static void
13109 add_byte_size_attribute (dw_die_ref die, tree tree_node)
13110 {
13111   unsigned size;
13112
13113   switch (TREE_CODE (tree_node))
13114     {
13115     case ERROR_MARK:
13116       size = 0;
13117       break;
13118     case ENUMERAL_TYPE:
13119     case RECORD_TYPE:
13120     case UNION_TYPE:
13121     case QUAL_UNION_TYPE:
13122       size = int_size_in_bytes (tree_node);
13123       break;
13124     case FIELD_DECL:
13125       /* For a data member of a struct or union, the DW_AT_byte_size is
13126          generally given as the number of bytes normally allocated for an
13127          object of the *declared* type of the member itself.  This is true
13128          even for bit-fields.  */
13129       size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
13130       break;
13131     default:
13132       gcc_unreachable ();
13133     }
13134
13135   /* Note that `size' might be -1 when we get to this point.  If it is, that
13136      indicates that the byte size of the entity in question is variable.  We
13137      have no good way of expressing this fact in Dwarf at the present time,
13138      so just let the -1 pass on through.  */
13139   add_AT_unsigned (die, DW_AT_byte_size, size);
13140 }
13141
13142 /* For a FIELD_DECL node which represents a bit-field, output an attribute
13143    which specifies the distance in bits from the highest order bit of the
13144    "containing object" for the bit-field to the highest order bit of the
13145    bit-field itself.
13146
13147    For any given bit-field, the "containing object" is a hypothetical object
13148    (of some integral or enum type) within which the given bit-field lives.  The
13149    type of this hypothetical "containing object" is always the same as the
13150    declared type of the individual bit-field itself.  The determination of the
13151    exact location of the "containing object" for a bit-field is rather
13152    complicated.  It's handled by the `field_byte_offset' function (above).
13153
13154    Note that it is the size (in bytes) of the hypothetical "containing object"
13155    which will be given in the DW_AT_byte_size attribute for this bit-field.
13156    (See `byte_size_attribute' above).  */
13157
13158 static inline void
13159 add_bit_offset_attribute (dw_die_ref die, tree decl)
13160 {
13161   HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
13162   tree type = DECL_BIT_FIELD_TYPE (decl);
13163   HOST_WIDE_INT bitpos_int;
13164   HOST_WIDE_INT highest_order_object_bit_offset;
13165   HOST_WIDE_INT highest_order_field_bit_offset;
13166   HOST_WIDE_INT unsigned bit_offset;
13167
13168   /* Must be a field and a bit field.  */
13169   gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
13170
13171   /* We can't yet handle bit-fields whose offsets are variable, so if we
13172      encounter such things, just return without generating any attribute
13173      whatsoever.  Likewise for variable or too large size.  */
13174   if (! host_integerp (bit_position (decl), 0)
13175       || ! host_integerp (DECL_SIZE (decl), 1))
13176     return;
13177
13178   bitpos_int = int_bit_position (decl);
13179
13180   /* Note that the bit offset is always the distance (in bits) from the
13181      highest-order bit of the "containing object" to the highest-order bit of
13182      the bit-field itself.  Since the "high-order end" of any object or field
13183      is different on big-endian and little-endian machines, the computation
13184      below must take account of these differences.  */
13185   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
13186   highest_order_field_bit_offset = bitpos_int;
13187
13188   if (! BYTES_BIG_ENDIAN)
13189     {
13190       highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
13191       highest_order_object_bit_offset += simple_type_size_in_bits (type);
13192     }
13193
13194   bit_offset
13195     = (! BYTES_BIG_ENDIAN
13196        ? highest_order_object_bit_offset - highest_order_field_bit_offset
13197        : highest_order_field_bit_offset - highest_order_object_bit_offset);
13198
13199   add_AT_unsigned (die, DW_AT_bit_offset, bit_offset);
13200 }
13201
13202 /* For a FIELD_DECL node which represents a bit field, output an attribute
13203    which specifies the length in bits of the given field.  */
13204
13205 static inline void
13206 add_bit_size_attribute (dw_die_ref die, tree decl)
13207 {
13208   /* Must be a field and a bit field.  */
13209   gcc_assert (TREE_CODE (decl) == FIELD_DECL
13210               && DECL_BIT_FIELD_TYPE (decl));
13211
13212   if (host_integerp (DECL_SIZE (decl), 1))
13213     add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
13214 }
13215
13216 /* If the compiled language is ANSI C, then add a 'prototyped'
13217    attribute, if arg types are given for the parameters of a function.  */
13218
13219 static inline void
13220 add_prototyped_attribute (dw_die_ref die, tree func_type)
13221 {
13222   if (get_AT_unsigned (comp_unit_die, DW_AT_language) == DW_LANG_C89
13223       && TYPE_ARG_TYPES (func_type) != NULL)
13224     add_AT_flag (die, DW_AT_prototyped, 1);
13225 }
13226
13227 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
13228    by looking in either the type declaration or object declaration
13229    equate table.  */
13230
13231 static inline dw_die_ref
13232 add_abstract_origin_attribute (dw_die_ref die, tree origin)
13233 {
13234   dw_die_ref origin_die = NULL;
13235
13236   if (TREE_CODE (origin) != FUNCTION_DECL)
13237     {
13238       /* We may have gotten separated from the block for the inlined
13239          function, if we're in an exception handler or some such; make
13240          sure that the abstract function has been written out.
13241
13242          Doing this for nested functions is wrong, however; functions are
13243          distinct units, and our context might not even be inline.  */
13244       tree fn = origin;
13245
13246       if (TYPE_P (fn))
13247         fn = TYPE_STUB_DECL (fn);
13248
13249       fn = decl_function_context (fn);
13250       if (fn)
13251         dwarf2out_abstract_function (fn);
13252     }
13253
13254   if (DECL_P (origin))
13255     origin_die = lookup_decl_die (origin);
13256   else if (TYPE_P (origin))
13257     origin_die = lookup_type_die (origin);
13258
13259   /* XXX: Functions that are never lowered don't always have correct block
13260      trees (in the case of java, they simply have no block tree, in some other
13261      languages).  For these functions, there is nothing we can really do to
13262      output correct debug info for inlined functions in all cases.  Rather
13263      than die, we'll just produce deficient debug info now, in that we will
13264      have variables without a proper abstract origin.  In the future, when all
13265      functions are lowered, we should re-add a gcc_assert (origin_die)
13266      here.  */
13267
13268   if (origin_die)
13269     add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
13270   return origin_die;
13271 }
13272
13273 /* We do not currently support the pure_virtual attribute.  */
13274
13275 static inline void
13276 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
13277 {
13278   if (DECL_VINDEX (func_decl))
13279     {
13280       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
13281
13282       if (host_integerp (DECL_VINDEX (func_decl), 0))
13283         add_AT_loc (die, DW_AT_vtable_elem_location,
13284                     new_loc_descr (DW_OP_constu,
13285                                    tree_low_cst (DECL_VINDEX (func_decl), 0),
13286                                    0));
13287
13288       /* GNU extension: Record what type this method came from originally.  */
13289       if (debug_info_level > DINFO_LEVEL_TERSE)
13290         add_AT_die_ref (die, DW_AT_containing_type,
13291                         lookup_type_die (DECL_CONTEXT (func_decl)));
13292     }
13293 }
13294 \f
13295 /* Add source coordinate attributes for the given decl.  */
13296
13297 static void
13298 add_src_coords_attributes (dw_die_ref die, tree decl)
13299 {
13300   expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
13301
13302   add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
13303   add_AT_unsigned (die, DW_AT_decl_line, s.line);
13304 }
13305
13306 /* Add a DW_AT_name attribute and source coordinate attribute for the
13307    given decl, but only if it actually has a name.  */
13308
13309 static void
13310 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
13311 {
13312   tree decl_name;
13313
13314   decl_name = DECL_NAME (decl);
13315   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
13316     {
13317       add_name_attribute (die, dwarf2_name (decl, 0));
13318       if (! DECL_ARTIFICIAL (decl))
13319         add_src_coords_attributes (die, decl);
13320
13321       if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
13322           && TREE_PUBLIC (decl)
13323           && !DECL_ABSTRACT (decl)
13324           && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
13325           && !is_fortran ())
13326         {
13327           /* Defer until we have an assembler name set.  */
13328           if (!DECL_ASSEMBLER_NAME_SET_P (decl))
13329             {
13330               limbo_die_node *asm_name;
13331
13332               asm_name = GGC_CNEW (limbo_die_node);
13333               asm_name->die = die;
13334               asm_name->created_for = decl;
13335               asm_name->next = deferred_asm_name;
13336               deferred_asm_name = asm_name;
13337             }
13338           else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
13339             add_AT_string (die, DW_AT_MIPS_linkage_name,
13340                            IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
13341         }
13342     }
13343
13344 #ifdef VMS_DEBUGGING_INFO
13345   /* Get the function's name, as described by its RTL.  This may be different
13346      from the DECL_NAME name used in the source file.  */
13347   if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
13348     {
13349       add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
13350                    XEXP (DECL_RTL (decl), 0));
13351       VEC_safe_push (rtx, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
13352     }
13353 #endif
13354 }
13355
13356 /* Push a new declaration scope.  */
13357
13358 static void
13359 push_decl_scope (tree scope)
13360 {
13361   VEC_safe_push (tree, gc, decl_scope_table, scope);
13362 }
13363
13364 /* Pop a declaration scope.  */
13365
13366 static inline void
13367 pop_decl_scope (void)
13368 {
13369   VEC_pop (tree, decl_scope_table);
13370 }
13371
13372 /* Return the DIE for the scope that immediately contains this type.
13373    Non-named types get global scope.  Named types nested in other
13374    types get their containing scope if it's open, or global scope
13375    otherwise.  All other types (i.e. function-local named types) get
13376    the current active scope.  */
13377
13378 static dw_die_ref
13379 scope_die_for (tree t, dw_die_ref context_die)
13380 {
13381   dw_die_ref scope_die = NULL;
13382   tree containing_scope;
13383   int i;
13384
13385   /* Non-types always go in the current scope.  */
13386   gcc_assert (TYPE_P (t));
13387
13388   containing_scope = TYPE_CONTEXT (t);
13389
13390   /* Use the containing namespace if it was passed in (for a declaration).  */
13391   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
13392     {
13393       if (context_die == lookup_decl_die (containing_scope))
13394         /* OK */;
13395       else
13396         containing_scope = NULL_TREE;
13397     }
13398
13399   /* Ignore function type "scopes" from the C frontend.  They mean that
13400      a tagged type is local to a parmlist of a function declarator, but
13401      that isn't useful to DWARF.  */
13402   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
13403     containing_scope = NULL_TREE;
13404
13405   if (containing_scope == NULL_TREE)
13406     scope_die = comp_unit_die;
13407   else if (TYPE_P (containing_scope))
13408     {
13409       /* For types, we can just look up the appropriate DIE.  But
13410          first we check to see if we're in the middle of emitting it
13411          so we know where the new DIE should go.  */
13412       for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
13413         if (VEC_index (tree, decl_scope_table, i) == containing_scope)
13414           break;
13415
13416       if (i < 0)
13417         {
13418           gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
13419                       || TREE_ASM_WRITTEN (containing_scope));
13420
13421           /* If none of the current dies are suitable, we get file scope.  */
13422           scope_die = comp_unit_die;
13423         }
13424       else
13425         scope_die = lookup_type_die (containing_scope);
13426     }
13427   else
13428     scope_die = context_die;
13429
13430   return scope_die;
13431 }
13432
13433 /* Returns nonzero if CONTEXT_DIE is internal to a function.  */
13434
13435 static inline int
13436 local_scope_p (dw_die_ref context_die)
13437 {
13438   for (; context_die; context_die = context_die->die_parent)
13439     if (context_die->die_tag == DW_TAG_inlined_subroutine
13440         || context_die->die_tag == DW_TAG_subprogram)
13441       return 1;
13442
13443   return 0;
13444 }
13445
13446 /* Returns nonzero if CONTEXT_DIE is a class.  */
13447
13448 static inline int
13449 class_scope_p (dw_die_ref context_die)
13450 {
13451   return (context_die
13452           && (context_die->die_tag == DW_TAG_structure_type
13453               || context_die->die_tag == DW_TAG_class_type
13454               || context_die->die_tag == DW_TAG_interface_type
13455               || context_die->die_tag == DW_TAG_union_type));
13456 }
13457
13458 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
13459    whether or not to treat a DIE in this context as a declaration.  */
13460
13461 static inline int
13462 class_or_namespace_scope_p (dw_die_ref context_die)
13463 {
13464   return (class_scope_p (context_die)
13465           || (context_die && context_die->die_tag == DW_TAG_namespace));
13466 }
13467
13468 /* Many forms of DIEs require a "type description" attribute.  This
13469    routine locates the proper "type descriptor" die for the type given
13470    by 'type', and adds a DW_AT_type attribute below the given die.  */
13471
13472 static void
13473 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
13474                     int decl_volatile, dw_die_ref context_die)
13475 {
13476   enum tree_code code  = TREE_CODE (type);
13477   dw_die_ref type_die  = NULL;
13478
13479   /* ??? If this type is an unnamed subrange type of an integral, floating-point
13480      or fixed-point type, use the inner type.  This is because we have no
13481      support for unnamed types in base_type_die.  This can happen if this is
13482      an Ada subrange type.  Correct solution is emit a subrange type die.  */
13483   if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
13484       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
13485     type = TREE_TYPE (type), code = TREE_CODE (type);
13486
13487   if (code == ERROR_MARK
13488       /* Handle a special case.  For functions whose return type is void, we
13489          generate *no* type attribute.  (Note that no object may have type
13490          `void', so this only applies to function return types).  */
13491       || code == VOID_TYPE)
13492     return;
13493
13494   type_die = modified_type_die (type,
13495                                 decl_const || TYPE_READONLY (type),
13496                                 decl_volatile || TYPE_VOLATILE (type),
13497                                 context_die);
13498
13499   if (type_die != NULL)
13500     add_AT_die_ref (object_die, DW_AT_type, type_die);
13501 }
13502
13503 /* Given an object die, add the calling convention attribute for the
13504    function call type.  */
13505 static void
13506 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
13507 {
13508   enum dwarf_calling_convention value = DW_CC_normal;
13509
13510   value = ((enum dwarf_calling_convention)
13511            targetm.dwarf_calling_convention (TREE_TYPE (decl)));
13512
13513   /* DWARF doesn't provide a way to identify a program's source-level
13514      entry point.  DW_AT_calling_convention attributes are only meant
13515      to describe functions' calling conventions.  However, lacking a
13516      better way to signal the Fortran main program, we use this for the
13517      time being, following existing custom.  */
13518   if (is_fortran ()
13519       && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
13520     value = DW_CC_program;
13521
13522   /* Only add the attribute if the backend requests it, and
13523      is not DW_CC_normal.  */
13524   if (value && (value != DW_CC_normal))
13525     add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
13526 }
13527
13528 /* Given a tree pointer to a struct, class, union, or enum type node, return
13529    a pointer to the (string) tag name for the given type, or zero if the type
13530    was declared without a tag.  */
13531
13532 static const char *
13533 type_tag (const_tree type)
13534 {
13535   const char *name = 0;
13536
13537   if (TYPE_NAME (type) != 0)
13538     {
13539       tree t = 0;
13540
13541       /* Find the IDENTIFIER_NODE for the type name.  */
13542       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
13543         t = TYPE_NAME (type);
13544
13545       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
13546          a TYPE_DECL node, regardless of whether or not a `typedef' was
13547          involved.  */
13548       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
13549                && ! DECL_IGNORED_P (TYPE_NAME (type)))
13550         {
13551           /* We want to be extra verbose.  Don't call dwarf_name if
13552              DECL_NAME isn't set.  The default hook for decl_printable_name
13553              doesn't like that, and in this context it's correct to return
13554              0, instead of "<anonymous>" or the like.  */
13555           if (DECL_NAME (TYPE_NAME (type)))
13556             name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
13557         }
13558
13559       /* Now get the name as a string, or invent one.  */
13560       if (!name && t != 0)
13561         name = IDENTIFIER_POINTER (t);
13562     }
13563
13564   return (name == 0 || *name == '\0') ? 0 : name;
13565 }
13566
13567 /* Return the type associated with a data member, make a special check
13568    for bit field types.  */
13569
13570 static inline tree
13571 member_declared_type (const_tree member)
13572 {
13573   return (DECL_BIT_FIELD_TYPE (member)
13574           ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
13575 }
13576
13577 /* Get the decl's label, as described by its RTL. This may be different
13578    from the DECL_NAME name used in the source file.  */
13579
13580 #if 0
13581 static const char *
13582 decl_start_label (tree decl)
13583 {
13584   rtx x;
13585   const char *fnname;
13586
13587   x = DECL_RTL (decl);
13588   gcc_assert (MEM_P (x));
13589
13590   x = XEXP (x, 0);
13591   gcc_assert (GET_CODE (x) == SYMBOL_REF);
13592
13593   fnname = XSTR (x, 0);
13594   return fnname;
13595 }
13596 #endif
13597 \f
13598 /* These routines generate the internal representation of the DIE's for
13599    the compilation unit.  Debugging information is collected by walking
13600    the declaration trees passed in from dwarf2out_decl().  */
13601
13602 static void
13603 gen_array_type_die (tree type, dw_die_ref context_die)
13604 {
13605   dw_die_ref scope_die = scope_die_for (type, context_die);
13606   dw_die_ref array_die;
13607
13608   /* GNU compilers represent multidimensional array types as sequences of one
13609      dimensional array types whose element types are themselves array types.
13610      We sometimes squish that down to a single array_type DIE with multiple
13611      subscripts in the Dwarf debugging info.  The draft Dwarf specification
13612      say that we are allowed to do this kind of compression in C, because
13613      there is no difference between an array of arrays and a multidimensional
13614      array.  We don't do this for Ada to remain as close as possible to the
13615      actual representation, which is especially important against the language
13616      flexibilty wrt arrays of variable size.  */
13617
13618   bool collapse_nested_arrays = !is_ada ();
13619   tree element_type;
13620
13621   /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
13622      DW_TAG_string_type doesn't have DW_AT_type attribute).  */
13623   if (TYPE_STRING_FLAG (type)
13624       && TREE_CODE (type) == ARRAY_TYPE
13625       && is_fortran ()
13626       && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
13627     {
13628       HOST_WIDE_INT size;
13629
13630       array_die = new_die (DW_TAG_string_type, scope_die, type);
13631       add_name_attribute (array_die, type_tag (type));
13632       equate_type_number_to_die (type, array_die);
13633       size = int_size_in_bytes (type);
13634       if (size >= 0)
13635         add_AT_unsigned (array_die, DW_AT_byte_size, size);
13636       else if (TYPE_DOMAIN (type) != NULL_TREE
13637                && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
13638                && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
13639         {
13640           tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
13641           dw_loc_descr_ref loc = loc_descriptor_from_tree (szdecl);
13642
13643           size = int_size_in_bytes (TREE_TYPE (szdecl));
13644           if (loc && size > 0)
13645             {
13646               add_AT_loc (array_die, DW_AT_string_length, loc);
13647               if (size != DWARF2_ADDR_SIZE)
13648                 add_AT_unsigned (array_die, DW_AT_byte_size, size);
13649             }
13650         }
13651       return;
13652     }
13653
13654   /* ??? The SGI dwarf reader fails for array of array of enum types
13655      (e.g. const enum machine_mode insn_operand_mode[2][10]) unless the inner
13656      array type comes before the outer array type.  We thus call gen_type_die
13657      before we new_die and must prevent nested array types collapsing for this
13658      target.  */
13659
13660 #ifdef MIPS_DEBUGGING_INFO
13661   gen_type_die (TREE_TYPE (type), context_die);
13662   collapse_nested_arrays = false;
13663 #endif
13664
13665   array_die = new_die (DW_TAG_array_type, scope_die, type);
13666   add_name_attribute (array_die, type_tag (type));
13667   equate_type_number_to_die (type, array_die);
13668
13669   if (TREE_CODE (type) == VECTOR_TYPE)
13670     {
13671       /* The frontend feeds us a representation for the vector as a struct
13672          containing an array.  Pull out the array type.  */
13673       type = TREE_TYPE (TYPE_FIELDS (TYPE_DEBUG_REPRESENTATION_TYPE (type)));
13674       add_AT_flag (array_die, DW_AT_GNU_vector, 1);
13675     }
13676
13677   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
13678   if (is_fortran ()
13679       && TREE_CODE (type) == ARRAY_TYPE
13680       && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
13681       && !TYPE_STRING_FLAG (TREE_TYPE (type)))
13682     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
13683
13684 #if 0
13685   /* We default the array ordering.  SDB will probably do
13686      the right things even if DW_AT_ordering is not present.  It's not even
13687      an issue until we start to get into multidimensional arrays anyway.  If
13688      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
13689      then we'll have to put the DW_AT_ordering attribute back in.  (But if
13690      and when we find out that we need to put these in, we will only do so
13691      for multidimensional arrays.  */
13692   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
13693 #endif
13694
13695 #ifdef MIPS_DEBUGGING_INFO
13696   /* The SGI compilers handle arrays of unknown bound by setting
13697      AT_declaration and not emitting any subrange DIEs.  */
13698   if (! TYPE_DOMAIN (type))
13699     add_AT_flag (array_die, DW_AT_declaration, 1);
13700   else
13701 #endif
13702     add_subscript_info (array_die, type, collapse_nested_arrays);
13703
13704   /* Add representation of the type of the elements of this array type and
13705      emit the corresponding DIE if we haven't done it already.  */  
13706   element_type = TREE_TYPE (type);
13707   if (collapse_nested_arrays)
13708     while (TREE_CODE (element_type) == ARRAY_TYPE)
13709       {
13710         if (TYPE_STRING_FLAG (element_type) && is_fortran ())
13711           break;
13712         element_type = TREE_TYPE (element_type);
13713       }
13714
13715 #ifndef MIPS_DEBUGGING_INFO
13716   gen_type_die (element_type, context_die);
13717 #endif
13718
13719   add_type_attribute (array_die, element_type, 0, 0, context_die);
13720
13721   if (get_AT (array_die, DW_AT_name))
13722     add_pubtype (type, array_die);
13723 }
13724
13725 static dw_loc_descr_ref
13726 descr_info_loc (tree val, tree base_decl)
13727 {
13728   HOST_WIDE_INT size;
13729   dw_loc_descr_ref loc, loc2;
13730   enum dwarf_location_atom op;
13731
13732   if (val == base_decl)
13733     return new_loc_descr (DW_OP_push_object_address, 0, 0);
13734
13735   switch (TREE_CODE (val))
13736     {
13737     CASE_CONVERT:
13738       return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
13739     case VAR_DECL:
13740       return loc_descriptor_from_tree_1 (val, 0);
13741     case INTEGER_CST:
13742       if (host_integerp (val, 0))
13743         return int_loc_descriptor (tree_low_cst (val, 0));
13744       break;
13745     case INDIRECT_REF:
13746       size = int_size_in_bytes (TREE_TYPE (val));
13747       if (size < 0)
13748         break;
13749       loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
13750       if (!loc)
13751         break;
13752       if (size == DWARF2_ADDR_SIZE)
13753         add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
13754       else
13755         add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
13756       return loc;
13757     case POINTER_PLUS_EXPR:
13758     case PLUS_EXPR:
13759       if (host_integerp (TREE_OPERAND (val, 1), 1)
13760           && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
13761              < 16384)
13762         {
13763           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
13764           if (!loc)
13765             break;
13766           loc_descr_plus_const (&loc, tree_low_cst (TREE_OPERAND (val, 1), 0));
13767         }
13768       else
13769         {
13770           op = DW_OP_plus;
13771         do_binop:
13772           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
13773           if (!loc)
13774             break;
13775           loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
13776           if (!loc2)
13777             break;
13778           add_loc_descr (&loc, loc2);
13779           add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
13780         }
13781       return loc;
13782     case MINUS_EXPR:
13783       op = DW_OP_minus;
13784       goto do_binop;
13785     case MULT_EXPR:
13786       op = DW_OP_mul;
13787       goto do_binop;
13788     case EQ_EXPR:
13789       op = DW_OP_eq;
13790       goto do_binop;
13791     case NE_EXPR:
13792       op = DW_OP_ne;
13793       goto do_binop;
13794     default:
13795       break;
13796     }
13797   return NULL;
13798 }
13799
13800 static void
13801 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
13802                       tree val, tree base_decl)
13803 {
13804   dw_loc_descr_ref loc;
13805
13806   if (host_integerp (val, 0))
13807     {
13808       add_AT_unsigned (die, attr, tree_low_cst (val, 0));
13809       return;
13810     }
13811
13812   loc = descr_info_loc (val, base_decl);
13813   if (!loc)
13814     return;
13815
13816   add_AT_loc (die, attr, loc);
13817 }
13818
13819 /* This routine generates DIE for array with hidden descriptor, details
13820    are filled into *info by a langhook.  */
13821
13822 static void
13823 gen_descr_array_type_die (tree type, struct array_descr_info *info,
13824                           dw_die_ref context_die)
13825 {
13826   dw_die_ref scope_die = scope_die_for (type, context_die);
13827   dw_die_ref array_die;
13828   int dim;
13829
13830   array_die = new_die (DW_TAG_array_type, scope_die, type);
13831   add_name_attribute (array_die, type_tag (type));
13832   equate_type_number_to_die (type, array_die);
13833
13834   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
13835   if (is_fortran ()
13836       && info->ndimensions >= 2)
13837     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
13838
13839   if (info->data_location)
13840     add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
13841                           info->base_decl);
13842   if (info->associated)
13843     add_descr_info_field (array_die, DW_AT_associated, info->associated,
13844                           info->base_decl);
13845   if (info->allocated)
13846     add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
13847                           info->base_decl);
13848
13849   for (dim = 0; dim < info->ndimensions; dim++)
13850     {
13851       dw_die_ref subrange_die
13852         = new_die (DW_TAG_subrange_type, array_die, NULL);
13853
13854       if (info->dimen[dim].lower_bound)
13855         {
13856           /* If it is the default value, omit it.  */
13857           if ((is_c_family () || is_java ())
13858               && integer_zerop (info->dimen[dim].lower_bound))
13859             ;
13860           else if (is_fortran ()
13861                    && integer_onep (info->dimen[dim].lower_bound))
13862             ;
13863           else
13864             add_descr_info_field (subrange_die, DW_AT_lower_bound,
13865                                   info->dimen[dim].lower_bound,
13866                                   info->base_decl);
13867         }
13868       if (info->dimen[dim].upper_bound)
13869         add_descr_info_field (subrange_die, DW_AT_upper_bound,
13870                               info->dimen[dim].upper_bound,
13871                               info->base_decl);
13872       if (info->dimen[dim].stride)
13873         add_descr_info_field (subrange_die, DW_AT_byte_stride,
13874                               info->dimen[dim].stride,
13875                               info->base_decl);
13876     }
13877
13878   gen_type_die (info->element_type, context_die);
13879   add_type_attribute (array_die, info->element_type, 0, 0, context_die);
13880
13881   if (get_AT (array_die, DW_AT_name))
13882     add_pubtype (type, array_die);
13883 }
13884
13885 #if 0
13886 static void
13887 gen_entry_point_die (tree decl, dw_die_ref context_die)
13888 {
13889   tree origin = decl_ultimate_origin (decl);
13890   dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
13891
13892   if (origin != NULL)
13893     add_abstract_origin_attribute (decl_die, origin);
13894   else
13895     {
13896       add_name_and_src_coords_attributes (decl_die, decl);
13897       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
13898                           0, 0, context_die);
13899     }
13900
13901   if (DECL_ABSTRACT (decl))
13902     equate_decl_number_to_die (decl, decl_die);
13903   else
13904     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
13905 }
13906 #endif
13907
13908 /* Walk through the list of incomplete types again, trying once more to
13909    emit full debugging info for them.  */
13910
13911 static void
13912 retry_incomplete_types (void)
13913 {
13914   int i;
13915
13916   for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
13917     gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die);
13918 }
13919
13920 /* Determine what tag to use for a record type.  */
13921
13922 static enum dwarf_tag
13923 record_type_tag (tree type)
13924 {
13925   if (! lang_hooks.types.classify_record)
13926     return DW_TAG_structure_type;
13927
13928   switch (lang_hooks.types.classify_record (type))
13929     {
13930     case RECORD_IS_STRUCT:
13931       return DW_TAG_structure_type;
13932
13933     case RECORD_IS_CLASS:
13934       return DW_TAG_class_type;
13935
13936     case RECORD_IS_INTERFACE:
13937       return DW_TAG_interface_type;
13938
13939     default:
13940       gcc_unreachable ();
13941     }
13942 }
13943
13944 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
13945    include all of the information about the enumeration values also. Each
13946    enumerated type name/value is listed as a child of the enumerated type
13947    DIE.  */
13948
13949 static dw_die_ref
13950 gen_enumeration_type_die (tree type, dw_die_ref context_die)
13951 {
13952   dw_die_ref type_die = lookup_type_die (type);
13953
13954   if (type_die == NULL)
13955     {
13956       type_die = new_die (DW_TAG_enumeration_type,
13957                           scope_die_for (type, context_die), type);
13958       equate_type_number_to_die (type, type_die);
13959       add_name_attribute (type_die, type_tag (type));
13960     }
13961   else if (! TYPE_SIZE (type))
13962     return type_die;
13963   else
13964     remove_AT (type_die, DW_AT_declaration);
13965
13966   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
13967      given enum type is incomplete, do not generate the DW_AT_byte_size
13968      attribute or the DW_AT_element_list attribute.  */
13969   if (TYPE_SIZE (type))
13970     {
13971       tree link;
13972
13973       TREE_ASM_WRITTEN (type) = 1;
13974       add_byte_size_attribute (type_die, type);
13975       if (TYPE_STUB_DECL (type) != NULL_TREE)
13976         add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
13977
13978       /* If the first reference to this type was as the return type of an
13979          inline function, then it may not have a parent.  Fix this now.  */
13980       if (type_die->die_parent == NULL)
13981         add_child_die (scope_die_for (type, context_die), type_die);
13982
13983       for (link = TYPE_VALUES (type);
13984            link != NULL; link = TREE_CHAIN (link))
13985         {
13986           dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
13987           tree value = TREE_VALUE (link);
13988
13989           add_name_attribute (enum_die,
13990                               IDENTIFIER_POINTER (TREE_PURPOSE (link)));
13991
13992           if (TREE_CODE (value) == CONST_DECL)
13993             value = DECL_INITIAL (value);
13994
13995           if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
13996             /* DWARF2 does not provide a way of indicating whether or
13997                not enumeration constants are signed or unsigned.  GDB
13998                always assumes the values are signed, so we output all
13999                values as if they were signed.  That means that
14000                enumeration constants with very large unsigned values
14001                will appear to have negative values in the debugger.  */
14002             add_AT_int (enum_die, DW_AT_const_value,
14003                         tree_low_cst (value, tree_int_cst_sgn (value) > 0));
14004         }
14005     }
14006   else
14007     add_AT_flag (type_die, DW_AT_declaration, 1);
14008
14009   if (get_AT (type_die, DW_AT_name))
14010     add_pubtype (type, type_die);
14011
14012   return type_die;
14013 }
14014
14015 /* Generate a DIE to represent either a real live formal parameter decl or to
14016    represent just the type of some formal parameter position in some function
14017    type.
14018
14019    Note that this routine is a bit unusual because its argument may be a
14020    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
14021    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
14022    node.  If it's the former then this function is being called to output a
14023    DIE to represent a formal parameter object (or some inlining thereof).  If
14024    it's the latter, then this function is only being called to output a
14025    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
14026    argument type of some subprogram type.  */
14027
14028 static dw_die_ref
14029 gen_formal_parameter_die (tree node, tree origin, dw_die_ref context_die)
14030 {
14031   tree node_or_origin = node ? node : origin;
14032   dw_die_ref parm_die
14033     = new_die (DW_TAG_formal_parameter, context_die, node);
14034
14035   switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
14036     {
14037     case tcc_declaration:
14038       if (!origin)
14039         origin = decl_ultimate_origin (node);
14040       if (origin != NULL)
14041         add_abstract_origin_attribute (parm_die, origin);
14042       else
14043         {
14044           tree type = TREE_TYPE (node);
14045           add_name_and_src_coords_attributes (parm_die, node);
14046           if (DECL_BY_REFERENCE (node))
14047             add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
14048                                 context_die);
14049           else
14050             add_type_attribute (parm_die, type,
14051                                 TREE_READONLY (node),
14052                                 TREE_THIS_VOLATILE (node),
14053                                 context_die);
14054           if (DECL_ARTIFICIAL (node))
14055             add_AT_flag (parm_die, DW_AT_artificial, 1);
14056         }
14057
14058       if (node && node != origin)
14059         equate_decl_number_to_die (node, parm_die);
14060       if (! DECL_ABSTRACT (node_or_origin))
14061         add_location_or_const_value_attribute (parm_die, node_or_origin,
14062                                                DW_AT_location);
14063
14064       break;
14065
14066     case tcc_type:
14067       /* We were called with some kind of a ..._TYPE node.  */
14068       add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
14069       break;
14070
14071     default:
14072       gcc_unreachable ();
14073     }
14074
14075   return parm_die;
14076 }
14077
14078 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
14079    at the end of an (ANSI prototyped) formal parameters list.  */
14080
14081 static void
14082 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
14083 {
14084   new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
14085 }
14086
14087 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
14088    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
14089    parameters as specified in some function type specification (except for
14090    those which appear as part of a function *definition*).  */
14091
14092 static void
14093 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
14094 {
14095   tree link;
14096   tree formal_type = NULL;
14097   tree first_parm_type;
14098   tree arg;
14099
14100   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
14101     {
14102       arg = DECL_ARGUMENTS (function_or_method_type);
14103       function_or_method_type = TREE_TYPE (function_or_method_type);
14104     }
14105   else
14106     arg = NULL_TREE;
14107
14108   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
14109
14110   /* Make our first pass over the list of formal parameter types and output a
14111      DW_TAG_formal_parameter DIE for each one.  */
14112   for (link = first_parm_type; link; )
14113     {
14114       dw_die_ref parm_die;
14115
14116       formal_type = TREE_VALUE (link);
14117       if (formal_type == void_type_node)
14118         break;
14119
14120       /* Output a (nameless) DIE to represent the formal parameter itself.  */
14121       parm_die = gen_formal_parameter_die (formal_type, NULL, context_die);
14122       if ((TREE_CODE (function_or_method_type) == METHOD_TYPE
14123            && link == first_parm_type)
14124           || (arg && DECL_ARTIFICIAL (arg)))
14125         add_AT_flag (parm_die, DW_AT_artificial, 1);
14126
14127       link = TREE_CHAIN (link);
14128       if (arg)
14129         arg = TREE_CHAIN (arg);
14130     }
14131
14132   /* If this function type has an ellipsis, add a
14133      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
14134   if (formal_type != void_type_node)
14135     gen_unspecified_parameters_die (function_or_method_type, context_die);
14136
14137   /* Make our second (and final) pass over the list of formal parameter types
14138      and output DIEs to represent those types (as necessary).  */
14139   for (link = TYPE_ARG_TYPES (function_or_method_type);
14140        link && TREE_VALUE (link);
14141        link = TREE_CHAIN (link))
14142     gen_type_die (TREE_VALUE (link), context_die);
14143 }
14144
14145 /* We want to generate the DIE for TYPE so that we can generate the
14146    die for MEMBER, which has been defined; we will need to refer back
14147    to the member declaration nested within TYPE.  If we're trying to
14148    generate minimal debug info for TYPE, processing TYPE won't do the
14149    trick; we need to attach the member declaration by hand.  */
14150
14151 static void
14152 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
14153 {
14154   gen_type_die (type, context_die);
14155
14156   /* If we're trying to avoid duplicate debug info, we may not have
14157      emitted the member decl for this function.  Emit it now.  */
14158   if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
14159       && ! lookup_decl_die (member))
14160     {
14161       dw_die_ref type_die;
14162       gcc_assert (!decl_ultimate_origin (member));
14163
14164       push_decl_scope (type);
14165       type_die = lookup_type_die (type);
14166       if (TREE_CODE (member) == FUNCTION_DECL)
14167         gen_subprogram_die (member, type_die);
14168       else if (TREE_CODE (member) == FIELD_DECL)
14169         {
14170           /* Ignore the nameless fields that are used to skip bits but handle
14171              C++ anonymous unions and structs.  */
14172           if (DECL_NAME (member) != NULL_TREE
14173               || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
14174               || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
14175             {
14176               gen_type_die (member_declared_type (member), type_die);
14177               gen_field_die (member, type_die);
14178             }
14179         }
14180       else
14181         gen_variable_die (member, NULL_TREE, type_die);
14182
14183       pop_decl_scope ();
14184     }
14185 }
14186
14187 /* Generate the DWARF2 info for the "abstract" instance of a function which we
14188    may later generate inlined and/or out-of-line instances of.  */
14189
14190 static void
14191 dwarf2out_abstract_function (tree decl)
14192 {
14193   dw_die_ref old_die;
14194   tree save_fn;
14195   tree context;
14196   int was_abstract = DECL_ABSTRACT (decl);
14197
14198   /* Make sure we have the actual abstract inline, not a clone.  */
14199   decl = DECL_ORIGIN (decl);
14200   htab_empty (decl_loc_table);
14201
14202   old_die = lookup_decl_die (decl);
14203   if (old_die && get_AT (old_die, DW_AT_inline))
14204     /* We've already generated the abstract instance.  */
14205     return;
14206
14207   /* Be sure we've emitted the in-class declaration DIE (if any) first, so
14208      we don't get confused by DECL_ABSTRACT.  */
14209   if (debug_info_level > DINFO_LEVEL_TERSE)
14210     {
14211       context = decl_class_context (decl);
14212       if (context)
14213         gen_type_die_for_member
14214           (context, decl, decl_function_context (decl) ? NULL : comp_unit_die);
14215     }
14216
14217   /* Pretend we've just finished compiling this function.  */
14218   save_fn = current_function_decl;
14219   current_function_decl = decl;
14220   push_cfun (DECL_STRUCT_FUNCTION (decl));
14221
14222   set_decl_abstract_flags (decl, 1);
14223   dwarf2out_decl (decl);
14224   if (! was_abstract)
14225     set_decl_abstract_flags (decl, 0);
14226
14227   current_function_decl = save_fn;
14228   pop_cfun ();
14229 }
14230
14231 /* Helper function of premark_used_types() which gets called through
14232    htab_traverse_resize().
14233
14234    Marks the DIE of a given type in *SLOT as perennial, so it never gets
14235    marked as unused by prune_unused_types.  */
14236 static int
14237 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
14238 {
14239   tree type;
14240   dw_die_ref die;
14241
14242   type = (tree) *slot;
14243   die = lookup_type_die (type);
14244   if (die != NULL)
14245     die->die_perennial_p = 1;
14246   return 1;
14247 }
14248
14249 /* Mark all members of used_types_hash as perennial.  */
14250 static void
14251 premark_used_types (void)
14252 {
14253   if (cfun && cfun->used_types_hash)
14254     htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
14255 }
14256
14257 /* Generate a DIE to represent a declared function (either file-scope or
14258    block-local).  */
14259
14260 static void
14261 gen_subprogram_die (tree decl, dw_die_ref context_die)
14262 {
14263   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
14264   tree origin = decl_ultimate_origin (decl);
14265   dw_die_ref subr_die;
14266   tree fn_arg_types;
14267   tree outer_scope;
14268   dw_die_ref old_die = lookup_decl_die (decl);
14269   int declaration = (current_function_decl != decl
14270                      || class_or_namespace_scope_p (context_die));
14271
14272   premark_used_types ();
14273
14274   /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
14275      started to generate the abstract instance of an inline, decided to output
14276      its containing class, and proceeded to emit the declaration of the inline
14277      from the member list for the class.  If so, DECLARATION takes priority;
14278      we'll get back to the abstract instance when done with the class.  */
14279
14280   /* The class-scope declaration DIE must be the primary DIE.  */
14281   if (origin && declaration && class_or_namespace_scope_p (context_die))
14282     {
14283       origin = NULL;
14284       gcc_assert (!old_die);
14285     }
14286
14287   /* Now that the C++ front end lazily declares artificial member fns, we
14288      might need to retrofit the declaration into its class.  */
14289   if (!declaration && !origin && !old_die
14290       && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
14291       && !class_or_namespace_scope_p (context_die)
14292       && debug_info_level > DINFO_LEVEL_TERSE)
14293     old_die = force_decl_die (decl);
14294
14295   if (origin != NULL)
14296     {
14297       gcc_assert (!declaration || local_scope_p (context_die));
14298
14299       /* Fixup die_parent for the abstract instance of a nested
14300          inline function.  */
14301       if (old_die && old_die->die_parent == NULL)
14302         add_child_die (context_die, old_die);
14303
14304       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
14305       add_abstract_origin_attribute (subr_die, origin);
14306     }
14307   else if (old_die)
14308     {
14309       expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
14310       struct dwarf_file_data * file_index = lookup_filename (s.file);
14311
14312       if (!get_AT_flag (old_die, DW_AT_declaration)
14313           /* We can have a normal definition following an inline one in the
14314              case of redefinition of GNU C extern inlines.
14315              It seems reasonable to use AT_specification in this case.  */
14316           && !get_AT (old_die, DW_AT_inline))
14317         {
14318           /* Detect and ignore this case, where we are trying to output
14319              something we have already output.  */
14320           return;
14321         }
14322
14323       /* If the definition comes from the same place as the declaration,
14324          maybe use the old DIE.  We always want the DIE for this function
14325          that has the *_pc attributes to be under comp_unit_die so the
14326          debugger can find it.  We also need to do this for abstract
14327          instances of inlines, since the spec requires the out-of-line copy
14328          to have the same parent.  For local class methods, this doesn't
14329          apply; we just use the old DIE.  */
14330       if ((old_die->die_parent == comp_unit_die || context_die == NULL)
14331           && (DECL_ARTIFICIAL (decl)
14332               || (get_AT_file (old_die, DW_AT_decl_file) == file_index
14333                   && (get_AT_unsigned (old_die, DW_AT_decl_line)
14334                       == (unsigned) s.line))))
14335         {
14336           subr_die = old_die;
14337
14338           /* Clear out the declaration attribute and the formal parameters.
14339              Do not remove all children, because it is possible that this
14340              declaration die was forced using force_decl_die(). In such
14341              cases die that forced declaration die (e.g. TAG_imported_module)
14342              is one of the children that we do not want to remove.  */
14343           remove_AT (subr_die, DW_AT_declaration);
14344           remove_child_TAG (subr_die, DW_TAG_formal_parameter);
14345         }
14346       else
14347         {
14348           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
14349           add_AT_specification (subr_die, old_die);
14350           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
14351             add_AT_file (subr_die, DW_AT_decl_file, file_index);
14352           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
14353             add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
14354         }
14355     }
14356   else
14357     {
14358       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
14359
14360       if (TREE_PUBLIC (decl))
14361         add_AT_flag (subr_die, DW_AT_external, 1);
14362
14363       add_name_and_src_coords_attributes (subr_die, decl);
14364       if (debug_info_level > DINFO_LEVEL_TERSE)
14365         {
14366           add_prototyped_attribute (subr_die, TREE_TYPE (decl));
14367           add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
14368                               0, 0, context_die);
14369         }
14370
14371       add_pure_or_virtual_attribute (subr_die, decl);
14372       if (DECL_ARTIFICIAL (decl))
14373         add_AT_flag (subr_die, DW_AT_artificial, 1);
14374
14375       if (TREE_PROTECTED (decl))
14376         add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_protected);
14377       else if (TREE_PRIVATE (decl))
14378         add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_private);
14379     }
14380
14381   if (declaration)
14382     {
14383       if (!old_die || !get_AT (old_die, DW_AT_inline))
14384         {
14385           add_AT_flag (subr_die, DW_AT_declaration, 1);
14386
14387           /* If this is an explicit function declaration then generate
14388              a DW_AT_explicit attribute.  */
14389           if (lang_hooks.decls.function_decl_explicit_p (decl))
14390             add_AT_flag (subr_die, DW_AT_explicit, 1);
14391
14392           /* The first time we see a member function, it is in the context of
14393              the class to which it belongs.  We make sure of this by emitting
14394              the class first.  The next time is the definition, which is
14395              handled above.  The two may come from the same source text.
14396
14397              Note that force_decl_die() forces function declaration die. It is
14398              later reused to represent definition.  */
14399           equate_decl_number_to_die (decl, subr_die);
14400         }
14401     }
14402   else if (DECL_ABSTRACT (decl))
14403     {
14404       if (DECL_DECLARED_INLINE_P (decl))
14405         {
14406           if (cgraph_function_possibly_inlined_p (decl))
14407             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
14408           else
14409             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
14410         }
14411       else
14412         {
14413           if (cgraph_function_possibly_inlined_p (decl))
14414             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
14415           else
14416             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
14417         }
14418
14419       if (DECL_DECLARED_INLINE_P (decl)
14420           && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
14421         add_AT_flag (subr_die, DW_AT_artificial, 1);
14422
14423       equate_decl_number_to_die (decl, subr_die);
14424     }
14425   else if (!DECL_EXTERNAL (decl))
14426     {
14427       HOST_WIDE_INT cfa_fb_offset;
14428
14429       if (!old_die || !get_AT (old_die, DW_AT_inline))
14430         equate_decl_number_to_die (decl, subr_die);
14431
14432       if (!flag_reorder_blocks_and_partition)
14433         {
14434           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
14435                                        current_function_funcdef_no);
14436           add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
14437           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
14438                                        current_function_funcdef_no);
14439           add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
14440
14441           add_pubname (decl, subr_die);
14442           add_arange (decl, subr_die);
14443         }
14444       else
14445         {  /* Do nothing for now; maybe need to duplicate die, one for
14446               hot section and one for cold section, then use the hot/cold
14447               section begin/end labels to generate the aranges...  */
14448           /*
14449             add_AT_lbl_id (subr_die, DW_AT_low_pc, hot_section_label);
14450             add_AT_lbl_id (subr_die, DW_AT_high_pc, hot_section_end_label);
14451             add_AT_lbl_id (subr_die, DW_AT_lo_user, unlikely_section_label);
14452             add_AT_lbl_id (subr_die, DW_AT_hi_user, cold_section_end_label);
14453
14454             add_pubname (decl, subr_die);
14455             add_arange (decl, subr_die);
14456             add_arange (decl, subr_die);
14457            */
14458         }
14459
14460 #ifdef MIPS_DEBUGGING_INFO
14461       /* Add a reference to the FDE for this routine.  */
14462       add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
14463 #endif
14464
14465       cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
14466
14467       /* We define the "frame base" as the function's CFA.  This is more
14468          convenient for several reasons: (1) It's stable across the prologue
14469          and epilogue, which makes it better than just a frame pointer,
14470          (2) With dwarf3, there exists a one-byte encoding that allows us
14471          to reference the .debug_frame data by proxy, but failing that,
14472          (3) We can at least reuse the code inspection and interpretation
14473          code that determines the CFA position at various points in the
14474          function.  */
14475       if (dwarf_version >= 3)
14476         {
14477           dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
14478           add_AT_loc (subr_die, DW_AT_frame_base, op);
14479         }
14480       else
14481         {
14482           dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
14483           if (list->dw_loc_next)
14484             add_AT_loc_list (subr_die, DW_AT_frame_base, list);
14485           else
14486             add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
14487         }
14488
14489       /* Compute a displacement from the "steady-state frame pointer" to
14490          the CFA.  The former is what all stack slots and argument slots
14491          will reference in the rtl; the later is what we've told the
14492          debugger about.  We'll need to adjust all frame_base references
14493          by this displacement.  */
14494       compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
14495
14496       if (cfun->static_chain_decl)
14497         add_AT_location_description (subr_die, DW_AT_static_link,
14498                  loc_descriptor_from_tree (cfun->static_chain_decl));
14499     }
14500
14501   /* Now output descriptions of the arguments for this function. This gets
14502      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
14503      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
14504      `...' at the end of the formal parameter list.  In order to find out if
14505      there was a trailing ellipsis or not, we must instead look at the type
14506      associated with the FUNCTION_DECL.  This will be a node of type
14507      FUNCTION_TYPE. If the chain of type nodes hanging off of this
14508      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
14509      an ellipsis at the end.  */
14510
14511   /* In the case where we are describing a mere function declaration, all we
14512      need to do here (and all we *can* do here) is to describe the *types* of
14513      its formal parameters.  */
14514   if (debug_info_level <= DINFO_LEVEL_TERSE)
14515     ;
14516   else if (declaration)
14517     gen_formal_types_die (decl, subr_die);
14518   else
14519     {
14520       /* Generate DIEs to represent all known formal parameters.  */
14521       tree arg_decls = DECL_ARGUMENTS (decl);
14522       tree parm;
14523
14524       /* When generating DIEs, generate the unspecified_parameters DIE
14525          instead if we come across the arg "__builtin_va_alist" */
14526       for (parm = arg_decls; parm; parm = TREE_CHAIN (parm))
14527         if (TREE_CODE (parm) == PARM_DECL)
14528           {
14529             if (DECL_NAME (parm)
14530                 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (parm)),
14531                             "__builtin_va_alist"))
14532               gen_unspecified_parameters_die (parm, subr_die);
14533             else
14534               gen_decl_die (parm, NULL, subr_die);
14535           }
14536
14537       /* Decide whether we need an unspecified_parameters DIE at the end.
14538          There are 2 more cases to do this for: 1) the ansi ... declaration -
14539          this is detectable when the end of the arg list is not a
14540          void_type_node 2) an unprototyped function declaration (not a
14541          definition).  This just means that we have no info about the
14542          parameters at all.  */
14543       fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
14544       if (fn_arg_types != NULL)
14545         {
14546           /* This is the prototyped case, check for....  */
14547           if (TREE_VALUE (tree_last (fn_arg_types)) != void_type_node)
14548             gen_unspecified_parameters_die (decl, subr_die);
14549         }
14550       else if (DECL_INITIAL (decl) == NULL_TREE)
14551         gen_unspecified_parameters_die (decl, subr_die);
14552     }
14553
14554   /* Output Dwarf info for all of the stuff within the body of the function
14555      (if it has one - it may be just a declaration).  */
14556   outer_scope = DECL_INITIAL (decl);
14557
14558   /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
14559      a function.  This BLOCK actually represents the outermost binding contour
14560      for the function, i.e. the contour in which the function's formal
14561      parameters and labels get declared. Curiously, it appears that the front
14562      end doesn't actually put the PARM_DECL nodes for the current function onto
14563      the BLOCK_VARS list for this outer scope, but are strung off of the
14564      DECL_ARGUMENTS list for the function instead.
14565
14566      The BLOCK_VARS list for the `outer_scope' does provide us with a list of
14567      the LABEL_DECL nodes for the function however, and we output DWARF info
14568      for those in decls_for_scope.  Just within the `outer_scope' there will be
14569      a BLOCK node representing the function's outermost pair of curly braces,
14570      and any blocks used for the base and member initializers of a C++
14571      constructor function.  */
14572   if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
14573     {
14574       /* Emit a DW_TAG_variable DIE for a named return value.  */
14575       if (DECL_NAME (DECL_RESULT (decl)))
14576         gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
14577
14578       current_function_has_inlines = 0;
14579       decls_for_scope (outer_scope, subr_die, 0);
14580
14581 #if 0 && defined (MIPS_DEBUGGING_INFO)
14582       if (current_function_has_inlines)
14583         {
14584           add_AT_flag (subr_die, DW_AT_MIPS_has_inlines, 1);
14585           if (! comp_unit_has_inlines)
14586             {
14587               add_AT_flag (comp_unit_die, DW_AT_MIPS_has_inlines, 1);
14588               comp_unit_has_inlines = 1;
14589             }
14590         }
14591 #endif
14592     }
14593   /* Add the calling convention attribute if requested.  */
14594   add_calling_convention_attribute (subr_die, decl);
14595
14596 }
14597
14598 /* Returns a hash value for X (which really is a die_struct).  */
14599
14600 static hashval_t
14601 common_block_die_table_hash (const void *x)
14602 {
14603   const_dw_die_ref d = (const_dw_die_ref) x;
14604   return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
14605 }
14606
14607 /* Return nonzero if decl_id and die_parent of die_struct X is the same
14608    as decl_id and die_parent of die_struct Y.  */
14609
14610 static int
14611 common_block_die_table_eq (const void *x, const void *y)
14612 {
14613   const_dw_die_ref d = (const_dw_die_ref) x;
14614   const_dw_die_ref e = (const_dw_die_ref) y;
14615   return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
14616 }
14617
14618 /* Generate a DIE to represent a declared data object.
14619    Either DECL or ORIGIN must be non-null.  */
14620
14621 static void
14622 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
14623 {
14624   HOST_WIDE_INT off;
14625   tree com_decl;
14626   tree decl_or_origin = decl ? decl : origin;
14627   dw_die_ref var_die;
14628   dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
14629   dw_die_ref origin_die;
14630   int declaration = (DECL_EXTERNAL (decl_or_origin)
14631                      /* If DECL is COMDAT and has not actually been
14632                         emitted, we cannot take its address; there
14633                         might end up being no definition anywhere in
14634                         the program.  For example, consider the C++
14635                         test case:
14636
14637                           template <class T>
14638                           struct S { static const int i = 7; };
14639
14640                           template <class T>
14641                           const int S<T>::i;
14642
14643                           int f() { return S<int>::i; }
14644
14645                         Here, S<int>::i is not DECL_EXTERNAL, but no
14646                         definition is required, so the compiler will
14647                         not emit a definition.  */
14648                      || (TREE_CODE (decl_or_origin) == VAR_DECL
14649                          && DECL_COMDAT (decl_or_origin)
14650                          && !TREE_ASM_WRITTEN (decl_or_origin))
14651                      || class_or_namespace_scope_p (context_die));
14652
14653   if (!origin)
14654     origin = decl_ultimate_origin (decl);
14655
14656   com_decl = fortran_common (decl_or_origin, &off);
14657
14658   /* Symbol in common gets emitted as a child of the common block, in the form
14659      of a data member.  */
14660   if (com_decl)
14661     {
14662       tree field;
14663       dw_die_ref com_die;
14664       dw_loc_descr_ref loc;
14665       die_node com_die_arg;
14666
14667       var_die = lookup_decl_die (decl_or_origin);
14668       if (var_die)
14669         {
14670           if (get_AT (var_die, DW_AT_location) == NULL)
14671             {
14672               loc = loc_descriptor_from_tree (com_decl);
14673               if (loc)
14674                 {
14675                   if (off)
14676                     {
14677                       /* Optimize the common case.  */
14678                       if (loc->dw_loc_opc == DW_OP_addr
14679                           && loc->dw_loc_next == NULL
14680                           && GET_CODE (loc->dw_loc_oprnd1.v.val_addr)
14681                              == SYMBOL_REF)
14682                         loc->dw_loc_oprnd1.v.val_addr
14683                           = plus_constant (loc->dw_loc_oprnd1.v.val_addr, off);
14684                         else
14685                           loc_descr_plus_const (&loc, off);
14686                     }
14687                   add_AT_loc (var_die, DW_AT_location, loc);
14688                   remove_AT (var_die, DW_AT_declaration);
14689                 }
14690             }
14691           return;
14692         }
14693
14694       if (common_block_die_table == NULL)
14695         common_block_die_table
14696           = htab_create_ggc (10, common_block_die_table_hash,
14697                              common_block_die_table_eq, NULL);
14698
14699       field = TREE_OPERAND (DECL_VALUE_EXPR (decl), 0);
14700       com_die_arg.decl_id = DECL_UID (com_decl);
14701       com_die_arg.die_parent = context_die;
14702       com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
14703       loc = loc_descriptor_from_tree (com_decl);
14704       if (com_die == NULL)
14705         {
14706           const char *cnam
14707             = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
14708           void **slot;
14709
14710           com_die = new_die (DW_TAG_common_block, context_die, decl);
14711           add_name_and_src_coords_attributes (com_die, com_decl);
14712           if (loc)
14713             {
14714               add_AT_loc (com_die, DW_AT_location, loc);
14715               /* Avoid sharing the same loc descriptor between
14716                  DW_TAG_common_block and DW_TAG_variable.  */
14717               loc = loc_descriptor_from_tree (com_decl);
14718             }
14719           else if (DECL_EXTERNAL (decl))
14720             add_AT_flag (com_die, DW_AT_declaration, 1);
14721           add_pubname_string (cnam, com_die); /* ??? needed? */
14722           com_die->decl_id = DECL_UID (com_decl);
14723           slot = htab_find_slot (common_block_die_table, com_die, INSERT);
14724           *slot = (void *) com_die;
14725         }
14726       else if (get_AT (com_die, DW_AT_location) == NULL && loc)
14727         {
14728           add_AT_loc (com_die, DW_AT_location, loc);
14729           loc = loc_descriptor_from_tree (com_decl);
14730           remove_AT (com_die, DW_AT_declaration);
14731         }
14732       var_die = new_die (DW_TAG_variable, com_die, decl);
14733       add_name_and_src_coords_attributes (var_die, decl);
14734       add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
14735                           TREE_THIS_VOLATILE (decl), context_die);
14736       add_AT_flag (var_die, DW_AT_external, 1);
14737       if (loc)
14738         {
14739           if (off)
14740             {
14741               /* Optimize the common case.  */
14742               if (loc->dw_loc_opc == DW_OP_addr
14743                   && loc->dw_loc_next == NULL
14744                   && GET_CODE (loc->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
14745                 loc->dw_loc_oprnd1.v.val_addr
14746                   = plus_constant (loc->dw_loc_oprnd1.v.val_addr, off);
14747               else
14748                 loc_descr_plus_const (&loc, off);
14749             }
14750           add_AT_loc (var_die, DW_AT_location, loc);
14751         }
14752       else if (DECL_EXTERNAL (decl))
14753         add_AT_flag (var_die, DW_AT_declaration, 1);
14754       equate_decl_number_to_die (decl, var_die);
14755       return;
14756     }
14757
14758   /* If the compiler emitted a definition for the DECL declaration
14759      and if we already emitted a DIE for it, don't emit a second
14760      DIE for it again.  */
14761   if (old_die
14762       && declaration
14763       && old_die->die_parent == context_die)
14764     return;
14765
14766   /* For static data members, the declaration in the class is supposed
14767      to have DW_TAG_member tag; the specification should still be
14768      DW_TAG_variable referencing the DW_TAG_member DIE.  */
14769   if (declaration && class_scope_p (context_die))
14770     var_die = new_die (DW_TAG_member, context_die, decl);
14771   else
14772     var_die = new_die (DW_TAG_variable, context_die, decl);
14773
14774   origin_die = NULL;
14775   if (origin != NULL)
14776     origin_die = add_abstract_origin_attribute (var_die, origin);
14777
14778   /* Loop unrolling can create multiple blocks that refer to the same
14779      static variable, so we must test for the DW_AT_declaration flag.
14780
14781      ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
14782      copy decls and set the DECL_ABSTRACT flag on them instead of
14783      sharing them.
14784
14785      ??? Duplicated blocks have been rewritten to use .debug_ranges.
14786
14787      ??? The declare_in_namespace support causes us to get two DIEs for one
14788      variable, both of which are declarations.  We want to avoid considering
14789      one to be a specification, so we must test that this DIE is not a
14790      declaration.  */
14791   else if (old_die && TREE_STATIC (decl) && ! declaration
14792            && get_AT_flag (old_die, DW_AT_declaration) == 1)
14793     {
14794       /* This is a definition of a C++ class level static.  */
14795       add_AT_specification (var_die, old_die);
14796       if (DECL_NAME (decl))
14797         {
14798           expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
14799           struct dwarf_file_data * file_index = lookup_filename (s.file);
14800
14801           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
14802             add_AT_file (var_die, DW_AT_decl_file, file_index);
14803
14804           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
14805             add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
14806         }
14807     }
14808   else
14809     {
14810       tree type = TREE_TYPE (decl);
14811
14812       add_name_and_src_coords_attributes (var_die, decl);
14813       if ((TREE_CODE (decl) == PARM_DECL
14814            || TREE_CODE (decl) == RESULT_DECL
14815            || TREE_CODE (decl) == VAR_DECL)
14816           && DECL_BY_REFERENCE (decl))
14817         add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
14818       else
14819         add_type_attribute (var_die, type, TREE_READONLY (decl),
14820                             TREE_THIS_VOLATILE (decl), context_die);
14821
14822       if (TREE_PUBLIC (decl))
14823         add_AT_flag (var_die, DW_AT_external, 1);
14824
14825       if (DECL_ARTIFICIAL (decl))
14826         add_AT_flag (var_die, DW_AT_artificial, 1);
14827
14828       if (TREE_PROTECTED (decl))
14829         add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_protected);
14830       else if (TREE_PRIVATE (decl))
14831         add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_private);
14832     }
14833
14834   if (declaration)
14835     add_AT_flag (var_die, DW_AT_declaration, 1);
14836
14837   if (decl && (DECL_ABSTRACT (decl) || declaration))
14838     equate_decl_number_to_die (decl, var_die);
14839
14840   if (! declaration
14841       && (! DECL_ABSTRACT (decl_or_origin)
14842           /* Local static vars are shared between all clones/inlines,
14843              so emit DW_AT_location on the abstract DIE if DECL_RTL is
14844              already set.  */
14845           || (TREE_CODE (decl_or_origin) == VAR_DECL
14846               && TREE_STATIC (decl_or_origin)
14847               && DECL_RTL_SET_P (decl_or_origin)))
14848       /* When abstract origin already has DW_AT_location attribute, no need
14849          to add it again.  */
14850       && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
14851     {
14852       if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
14853           && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
14854         defer_location (decl_or_origin, var_die);
14855       else
14856         add_location_or_const_value_attribute (var_die,
14857                                                decl_or_origin,
14858                                                DW_AT_location);
14859       add_pubname (decl_or_origin, var_die);
14860     }
14861   else
14862     tree_add_const_value_attribute (var_die, decl_or_origin);
14863 }
14864
14865 /* Generate a DIE to represent a named constant.  */
14866
14867 static void
14868 gen_const_die (tree decl, dw_die_ref context_die)
14869 {
14870   dw_die_ref const_die;
14871   tree type = TREE_TYPE (decl);
14872
14873   const_die = new_die (DW_TAG_constant, context_die, decl);
14874   add_name_and_src_coords_attributes (const_die, decl);
14875   add_type_attribute (const_die, type, 1, 0, context_die);
14876   if (TREE_PUBLIC (decl))
14877     add_AT_flag (const_die, DW_AT_external, 1);
14878   if (DECL_ARTIFICIAL (decl))
14879     add_AT_flag (const_die, DW_AT_artificial, 1);
14880   tree_add_const_value_attribute (const_die, decl);
14881 }
14882
14883 /* Generate a DIE to represent a label identifier.  */
14884
14885 static void
14886 gen_label_die (tree decl, dw_die_ref context_die)
14887 {
14888   tree origin = decl_ultimate_origin (decl);
14889   dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
14890   rtx insn;
14891   char label[MAX_ARTIFICIAL_LABEL_BYTES];
14892
14893   if (origin != NULL)
14894     add_abstract_origin_attribute (lbl_die, origin);
14895   else
14896     add_name_and_src_coords_attributes (lbl_die, decl);
14897
14898   if (DECL_ABSTRACT (decl))
14899     equate_decl_number_to_die (decl, lbl_die);
14900   else
14901     {
14902       insn = DECL_RTL_IF_SET (decl);
14903
14904       /* Deleted labels are programmer specified labels which have been
14905          eliminated because of various optimizations.  We still emit them
14906          here so that it is possible to put breakpoints on them.  */
14907       if (insn
14908           && (LABEL_P (insn)
14909               || ((NOTE_P (insn)
14910                    && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
14911         {
14912           /* When optimization is enabled (via -O) some parts of the compiler
14913              (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
14914              represent source-level labels which were explicitly declared by
14915              the user.  This really shouldn't be happening though, so catch
14916              it if it ever does happen.  */
14917           gcc_assert (!INSN_DELETED_P (insn));
14918
14919           ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
14920           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
14921         }
14922     }
14923 }
14924
14925 /* A helper function for gen_inlined_subroutine_die.  Add source coordinate
14926    attributes to the DIE for a block STMT, to describe where the inlined
14927    function was called from.  This is similar to add_src_coords_attributes.  */
14928
14929 static inline void
14930 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
14931 {
14932   expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
14933
14934   add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
14935   add_AT_unsigned (die, DW_AT_call_line, s.line);
14936 }
14937
14938
14939 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
14940    Add low_pc and high_pc attributes to the DIE for a block STMT.  */
14941
14942 static inline void
14943 add_high_low_attributes (tree stmt, dw_die_ref die)
14944 {
14945   char label[MAX_ARTIFICIAL_LABEL_BYTES];
14946
14947   if (BLOCK_FRAGMENT_CHAIN (stmt))
14948     {
14949       tree chain;
14950
14951       if (inlined_function_outer_scope_p (stmt))
14952         {
14953           ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
14954                                        BLOCK_NUMBER (stmt));
14955           add_AT_lbl_id (die, DW_AT_entry_pc, label);
14956         }
14957
14958       add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
14959
14960       chain = BLOCK_FRAGMENT_CHAIN (stmt);
14961       do
14962         {
14963           add_ranges (chain);
14964           chain = BLOCK_FRAGMENT_CHAIN (chain);
14965         }
14966       while (chain);
14967       add_ranges (NULL);
14968     }
14969   else
14970     {
14971       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
14972                                    BLOCK_NUMBER (stmt));
14973       add_AT_lbl_id (die, DW_AT_low_pc, label);
14974       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
14975                                    BLOCK_NUMBER (stmt));
14976       add_AT_lbl_id (die, DW_AT_high_pc, label);
14977     }
14978 }
14979
14980 /* Generate a DIE for a lexical block.  */
14981
14982 static void
14983 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
14984 {
14985   dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
14986
14987   if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
14988     add_high_low_attributes (stmt, stmt_die);
14989
14990   decls_for_scope (stmt, stmt_die, depth);
14991 }
14992
14993 /* Generate a DIE for an inlined subprogram.  */
14994
14995 static void
14996 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
14997 {
14998   tree decl = block_ultimate_origin (stmt);
14999
15000   /* Emit info for the abstract instance first, if we haven't yet.  We
15001      must emit this even if the block is abstract, otherwise when we
15002      emit the block below (or elsewhere), we may end up trying to emit
15003      a die whose origin die hasn't been emitted, and crashing.  */
15004   dwarf2out_abstract_function (decl);
15005
15006   if (! BLOCK_ABSTRACT (stmt))
15007     {
15008       dw_die_ref subr_die
15009         = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
15010
15011       add_abstract_origin_attribute (subr_die, decl);
15012       if (TREE_ASM_WRITTEN (stmt))
15013         add_high_low_attributes (stmt, subr_die);
15014       add_call_src_coords_attributes (stmt, subr_die);
15015
15016       decls_for_scope (stmt, subr_die, depth);
15017       current_function_has_inlines = 1;
15018     }
15019   else
15020     /* We may get here if we're the outer block of function A that was
15021        inlined into function B that was inlined into function C.  When
15022        generating debugging info for C, dwarf2out_abstract_function(B)
15023        would mark all inlined blocks as abstract, including this one.
15024        So, we wouldn't (and shouldn't) expect labels to be generated
15025        for this one.  Instead, just emit debugging info for
15026        declarations within the block.  This is particularly important
15027        in the case of initializers of arguments passed from B to us:
15028        if they're statement expressions containing declarations, we
15029        wouldn't generate dies for their abstract variables, and then,
15030        when generating dies for the real variables, we'd die (pun
15031        intended :-)  */
15032     gen_lexical_block_die (stmt, context_die, depth);
15033 }
15034
15035 /* Generate a DIE for a field in a record, or structure.  */
15036
15037 static void
15038 gen_field_die (tree decl, dw_die_ref context_die)
15039 {
15040   dw_die_ref decl_die;
15041
15042   if (TREE_TYPE (decl) == error_mark_node)
15043     return;
15044
15045   decl_die = new_die (DW_TAG_member, context_die, decl);
15046   add_name_and_src_coords_attributes (decl_die, decl);
15047   add_type_attribute (decl_die, member_declared_type (decl),
15048                       TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
15049                       context_die);
15050
15051   if (DECL_BIT_FIELD_TYPE (decl))
15052     {
15053       add_byte_size_attribute (decl_die, decl);
15054       add_bit_size_attribute (decl_die, decl);
15055       add_bit_offset_attribute (decl_die, decl);
15056     }
15057
15058   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
15059     add_data_member_location_attribute (decl_die, decl);
15060
15061   if (DECL_ARTIFICIAL (decl))
15062     add_AT_flag (decl_die, DW_AT_artificial, 1);
15063
15064   if (TREE_PROTECTED (decl))
15065     add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_protected);
15066   else if (TREE_PRIVATE (decl))
15067     add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_private);
15068
15069   /* Equate decl number to die, so that we can look up this decl later on.  */
15070   equate_decl_number_to_die (decl, decl_die);
15071 }
15072
15073 #if 0
15074 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
15075    Use modified_type_die instead.
15076    We keep this code here just in case these types of DIEs may be needed to
15077    represent certain things in other languages (e.g. Pascal) someday.  */
15078
15079 static void
15080 gen_pointer_type_die (tree type, dw_die_ref context_die)
15081 {
15082   dw_die_ref ptr_die
15083     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
15084
15085   equate_type_number_to_die (type, ptr_die);
15086   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
15087   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
15088 }
15089
15090 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
15091    Use modified_type_die instead.
15092    We keep this code here just in case these types of DIEs may be needed to
15093    represent certain things in other languages (e.g. Pascal) someday.  */
15094
15095 static void
15096 gen_reference_type_die (tree type, dw_die_ref context_die)
15097 {
15098   dw_die_ref ref_die
15099     = new_die (DW_TAG_reference_type, scope_die_for (type, context_die), type);
15100
15101   equate_type_number_to_die (type, ref_die);
15102   add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
15103   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
15104 }
15105 #endif
15106
15107 /* Generate a DIE for a pointer to a member type.  */
15108
15109 static void
15110 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
15111 {
15112   dw_die_ref ptr_die
15113     = new_die (DW_TAG_ptr_to_member_type,
15114                scope_die_for (type, context_die), type);
15115
15116   equate_type_number_to_die (type, ptr_die);
15117   add_AT_die_ref (ptr_die, DW_AT_containing_type,
15118                   lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
15119   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
15120 }
15121
15122 /* Generate the DIE for the compilation unit.  */
15123
15124 static dw_die_ref
15125 gen_compile_unit_die (const char *filename)
15126 {
15127   dw_die_ref die;
15128   char producer[250];
15129   const char *language_string = lang_hooks.name;
15130   int language;
15131
15132   die = new_die (DW_TAG_compile_unit, NULL, NULL);
15133
15134   if (filename)
15135     {
15136       add_name_attribute (die, filename);
15137       /* Don't add cwd for <built-in>.  */
15138       if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
15139         add_comp_dir_attribute (die);
15140     }
15141
15142   sprintf (producer, "%s %s", language_string, version_string);
15143
15144 #ifdef MIPS_DEBUGGING_INFO
15145   /* The MIPS/SGI compilers place the 'cc' command line options in the producer
15146      string.  The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
15147      not appear in the producer string, the debugger reaches the conclusion
15148      that the object file is stripped and has no debugging information.
15149      To get the MIPS/SGI debugger to believe that there is debugging
15150      information in the object file, we add a -g to the producer string.  */
15151   if (debug_info_level > DINFO_LEVEL_TERSE)
15152     strcat (producer, " -g");
15153 #endif
15154
15155   add_AT_string (die, DW_AT_producer, producer);
15156
15157   if (strcmp (language_string, "GNU C++") == 0)
15158     language = DW_LANG_C_plus_plus;
15159   else if (strcmp (language_string, "GNU Ada") == 0)
15160     language = DW_LANG_Ada95;
15161   else if (strcmp (language_string, "GNU F77") == 0)
15162     language = DW_LANG_Fortran77;
15163   else if (strcmp (language_string, "GNU Fortran") == 0)
15164     language = DW_LANG_Fortran95;
15165   else if (strcmp (language_string, "GNU Pascal") == 0)
15166     language = DW_LANG_Pascal83;
15167   else if (strcmp (language_string, "GNU Java") == 0)
15168     language = DW_LANG_Java;
15169   else if (strcmp (language_string, "GNU Objective-C") == 0)
15170     language = DW_LANG_ObjC;
15171   else if (strcmp (language_string, "GNU Objective-C++") == 0)
15172     language = DW_LANG_ObjC_plus_plus;
15173   else
15174     language = DW_LANG_C89;
15175
15176   add_AT_unsigned (die, DW_AT_language, language);
15177   return die;
15178 }
15179
15180 /* Generate the DIE for a base class.  */
15181
15182 static void
15183 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
15184 {
15185   dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
15186
15187   add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
15188   add_data_member_location_attribute (die, binfo);
15189
15190   if (BINFO_VIRTUAL_P (binfo))
15191     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
15192
15193   if (access == access_public_node)
15194     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
15195   else if (access == access_protected_node)
15196     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
15197 }
15198
15199 /* Generate a DIE for a class member.  */
15200
15201 static void
15202 gen_member_die (tree type, dw_die_ref context_die)
15203 {
15204   tree member;
15205   tree binfo = TYPE_BINFO (type);
15206   dw_die_ref child;
15207
15208   /* If this is not an incomplete type, output descriptions of each of its
15209      members. Note that as we output the DIEs necessary to represent the
15210      members of this record or union type, we will also be trying to output
15211      DIEs to represent the *types* of those members. However the `type'
15212      function (above) will specifically avoid generating type DIEs for member
15213      types *within* the list of member DIEs for this (containing) type except
15214      for those types (of members) which are explicitly marked as also being
15215      members of this (containing) type themselves.  The g++ front- end can
15216      force any given type to be treated as a member of some other (containing)
15217      type by setting the TYPE_CONTEXT of the given (member) type to point to
15218      the TREE node representing the appropriate (containing) type.  */
15219
15220   /* First output info about the base classes.  */
15221   if (binfo)
15222     {
15223       VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
15224       int i;
15225       tree base;
15226
15227       for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
15228         gen_inheritance_die (base,
15229                              (accesses ? VEC_index (tree, accesses, i)
15230                               : access_public_node), context_die);
15231     }
15232
15233   /* Now output info about the data members and type members.  */
15234   for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member))
15235     {
15236       /* If we thought we were generating minimal debug info for TYPE
15237          and then changed our minds, some of the member declarations
15238          may have already been defined.  Don't define them again, but
15239          do put them in the right order.  */
15240
15241       child = lookup_decl_die (member);
15242       if (child)
15243         splice_child_die (context_die, child);
15244       else
15245         gen_decl_die (member, NULL, context_die);
15246     }
15247
15248   /* Now output info about the function members (if any).  */
15249   for (member = TYPE_METHODS (type); member; member = TREE_CHAIN (member))
15250     {
15251       /* Don't include clones in the member list.  */
15252       if (DECL_ABSTRACT_ORIGIN (member))
15253         continue;
15254
15255       child = lookup_decl_die (member);
15256       if (child)
15257         splice_child_die (context_die, child);
15258       else
15259         gen_decl_die (member, NULL, context_die);
15260     }
15261 }
15262
15263 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
15264    is set, we pretend that the type was never defined, so we only get the
15265    member DIEs needed by later specification DIEs.  */
15266
15267 static void
15268 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
15269                                 enum debug_info_usage usage)
15270 {
15271   dw_die_ref type_die = lookup_type_die (type);
15272   dw_die_ref scope_die = 0;
15273   int nested = 0;
15274   int complete = (TYPE_SIZE (type)
15275                   && (! TYPE_STUB_DECL (type)
15276                       || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
15277   int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
15278   complete = complete && should_emit_struct_debug (type, usage);
15279
15280   if (type_die && ! complete)
15281     return;
15282
15283   if (TYPE_CONTEXT (type) != NULL_TREE
15284       && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
15285           || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
15286     nested = 1;
15287
15288   scope_die = scope_die_for (type, context_die);
15289
15290   if (! type_die || (nested && scope_die == comp_unit_die))
15291     /* First occurrence of type or toplevel definition of nested class.  */
15292     {
15293       dw_die_ref old_die = type_die;
15294
15295       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
15296                           ? record_type_tag (type) : DW_TAG_union_type,
15297                           scope_die, type);
15298       equate_type_number_to_die (type, type_die);
15299       if (old_die)
15300         add_AT_specification (type_die, old_die);
15301       else
15302         add_name_attribute (type_die, type_tag (type));
15303     }
15304   else
15305     remove_AT (type_die, DW_AT_declaration);
15306
15307   /* If this type has been completed, then give it a byte_size attribute and
15308      then give a list of members.  */
15309   if (complete && !ns_decl)
15310     {
15311       /* Prevent infinite recursion in cases where the type of some member of
15312          this type is expressed in terms of this type itself.  */
15313       TREE_ASM_WRITTEN (type) = 1;
15314       add_byte_size_attribute (type_die, type);
15315       if (TYPE_STUB_DECL (type) != NULL_TREE)
15316         add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
15317
15318       /* If the first reference to this type was as the return type of an
15319          inline function, then it may not have a parent.  Fix this now.  */
15320       if (type_die->die_parent == NULL)
15321         add_child_die (scope_die, type_die);
15322
15323       push_decl_scope (type);
15324       gen_member_die (type, type_die);
15325       pop_decl_scope ();
15326
15327       /* GNU extension: Record what type our vtable lives in.  */
15328       if (TYPE_VFIELD (type))
15329         {
15330           tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
15331
15332           gen_type_die (vtype, context_die);
15333           add_AT_die_ref (type_die, DW_AT_containing_type,
15334                           lookup_type_die (vtype));
15335         }
15336     }
15337   else
15338     {
15339       add_AT_flag (type_die, DW_AT_declaration, 1);
15340
15341       /* We don't need to do this for function-local types.  */
15342       if (TYPE_STUB_DECL (type)
15343           && ! decl_function_context (TYPE_STUB_DECL (type)))
15344         VEC_safe_push (tree, gc, incomplete_types, type);
15345     }
15346
15347   if (get_AT (type_die, DW_AT_name))
15348     add_pubtype (type, type_die);
15349 }
15350
15351 /* Generate a DIE for a subroutine _type_.  */
15352
15353 static void
15354 gen_subroutine_type_die (tree type, dw_die_ref context_die)
15355 {
15356   tree return_type = TREE_TYPE (type);
15357   dw_die_ref subr_die
15358     = new_die (DW_TAG_subroutine_type,
15359                scope_die_for (type, context_die), type);
15360
15361   equate_type_number_to_die (type, subr_die);
15362   add_prototyped_attribute (subr_die, type);
15363   add_type_attribute (subr_die, return_type, 0, 0, context_die);
15364   gen_formal_types_die (type, subr_die);
15365
15366   if (get_AT (subr_die, DW_AT_name))
15367     add_pubtype (type, subr_die);
15368 }
15369
15370 /* Generate a DIE for a type definition.  */
15371
15372 static void
15373 gen_typedef_die (tree decl, dw_die_ref context_die)
15374 {
15375   dw_die_ref type_die;
15376   tree origin;
15377
15378   if (TREE_ASM_WRITTEN (decl))
15379     return;
15380
15381   TREE_ASM_WRITTEN (decl) = 1;
15382   type_die = new_die (DW_TAG_typedef, context_die, decl);
15383   origin = decl_ultimate_origin (decl);
15384   if (origin != NULL)
15385     add_abstract_origin_attribute (type_die, origin);
15386   else
15387     {
15388       tree type;
15389
15390       add_name_and_src_coords_attributes (type_die, decl);
15391       if (DECL_ORIGINAL_TYPE (decl))
15392         {
15393           type = DECL_ORIGINAL_TYPE (decl);
15394
15395           gcc_assert (type != TREE_TYPE (decl));
15396           equate_type_number_to_die (TREE_TYPE (decl), type_die);
15397         }
15398       else
15399         type = TREE_TYPE (decl);
15400
15401       add_type_attribute (type_die, type, TREE_READONLY (decl),
15402                           TREE_THIS_VOLATILE (decl), context_die);
15403     }
15404
15405   if (DECL_ABSTRACT (decl))
15406     equate_decl_number_to_die (decl, type_die);
15407
15408   if (get_AT (type_die, DW_AT_name))
15409     add_pubtype (decl, type_die);
15410 }
15411
15412 /* Generate a type description DIE.  */
15413
15414 static void
15415 gen_type_die_with_usage (tree type, dw_die_ref context_die,
15416                                 enum debug_info_usage usage)
15417 {
15418   int need_pop;
15419   struct array_descr_info info;
15420
15421   if (type == NULL_TREE || type == error_mark_node)
15422     return;
15423
15424   /* If TYPE is a typedef type variant, let's generate debug info
15425      for the parent typedef which TYPE is a type of.  */
15426   if (TYPE_NAME (type) && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
15427       && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
15428     {
15429       if (TREE_ASM_WRITTEN (type))
15430         return;
15431
15432       /* Prevent broken recursion; we can't hand off to the same type.  */
15433       gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
15434
15435       /* Use the DIE of the containing namespace as the parent DIE of
15436          the type description DIE we want to generate.  */
15437       if (DECL_CONTEXT (TYPE_NAME (type))
15438           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
15439         context_die = lookup_decl_die (DECL_CONTEXT (TYPE_NAME (type)));
15440
15441       TREE_ASM_WRITTEN (type) = 1;
15442       gen_decl_die (TYPE_NAME (type), NULL, context_die);
15443       return;
15444     }
15445
15446   /* If this is an array type with hidden descriptor, handle it first.  */
15447   if (!TREE_ASM_WRITTEN (type)
15448       && lang_hooks.types.get_array_descr_info
15449       && lang_hooks.types.get_array_descr_info (type, &info))
15450     {
15451       gen_descr_array_type_die (type, &info, context_die);
15452       TREE_ASM_WRITTEN (type) = 1;
15453       return;
15454     }
15455
15456   /* We are going to output a DIE to represent the unqualified version
15457      of this type (i.e. without any const or volatile qualifiers) so
15458      get the main variant (i.e. the unqualified version) of this type
15459      now.  (Vectors are special because the debugging info is in the
15460      cloned type itself).  */
15461   if (TREE_CODE (type) != VECTOR_TYPE)
15462     type = type_main_variant (type);
15463
15464   if (TREE_ASM_WRITTEN (type))
15465     return;
15466
15467   switch (TREE_CODE (type))
15468     {
15469     case ERROR_MARK:
15470       break;
15471
15472     case POINTER_TYPE:
15473     case REFERENCE_TYPE:
15474       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
15475          ensures that the gen_type_die recursion will terminate even if the
15476          type is recursive.  Recursive types are possible in Ada.  */
15477       /* ??? We could perhaps do this for all types before the switch
15478          statement.  */
15479       TREE_ASM_WRITTEN (type) = 1;
15480
15481       /* For these types, all that is required is that we output a DIE (or a
15482          set of DIEs) to represent the "basis" type.  */
15483       gen_type_die_with_usage (TREE_TYPE (type), context_die,
15484                                 DINFO_USAGE_IND_USE);
15485       break;
15486
15487     case OFFSET_TYPE:
15488       /* This code is used for C++ pointer-to-data-member types.
15489          Output a description of the relevant class type.  */
15490       gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
15491                                         DINFO_USAGE_IND_USE);
15492
15493       /* Output a description of the type of the object pointed to.  */
15494       gen_type_die_with_usage (TREE_TYPE (type), context_die,
15495                                         DINFO_USAGE_IND_USE);
15496
15497       /* Now output a DIE to represent this pointer-to-data-member type
15498          itself.  */
15499       gen_ptr_to_mbr_type_die (type, context_die);
15500       break;
15501
15502     case FUNCTION_TYPE:
15503       /* Force out return type (in case it wasn't forced out already).  */
15504       gen_type_die_with_usage (TREE_TYPE (type), context_die,
15505                                         DINFO_USAGE_DIR_USE);
15506       gen_subroutine_type_die (type, context_die);
15507       break;
15508
15509     case METHOD_TYPE:
15510       /* Force out return type (in case it wasn't forced out already).  */
15511       gen_type_die_with_usage (TREE_TYPE (type), context_die,
15512                                         DINFO_USAGE_DIR_USE);
15513       gen_subroutine_type_die (type, context_die);
15514       break;
15515
15516     case ARRAY_TYPE:
15517       gen_array_type_die (type, context_die);
15518       break;
15519
15520     case VECTOR_TYPE:
15521       gen_array_type_die (type, context_die);
15522       break;
15523
15524     case ENUMERAL_TYPE:
15525     case RECORD_TYPE:
15526     case UNION_TYPE:
15527     case QUAL_UNION_TYPE:
15528       /* If this is a nested type whose containing class hasn't been written
15529          out yet, writing it out will cover this one, too.  This does not apply
15530          to instantiations of member class templates; they need to be added to
15531          the containing class as they are generated.  FIXME: This hurts the
15532          idea of combining type decls from multiple TUs, since we can't predict
15533          what set of template instantiations we'll get.  */
15534       if (TYPE_CONTEXT (type)
15535           && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
15536           && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
15537         {
15538           gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
15539
15540           if (TREE_ASM_WRITTEN (type))
15541             return;
15542
15543           /* If that failed, attach ourselves to the stub.  */
15544           push_decl_scope (TYPE_CONTEXT (type));
15545           context_die = lookup_type_die (TYPE_CONTEXT (type));
15546           need_pop = 1;
15547         }
15548       else
15549         {
15550           context_die = declare_in_namespace (type, context_die);
15551           need_pop = 0;
15552         }
15553
15554       if (TREE_CODE (type) == ENUMERAL_TYPE)
15555         {
15556           /* This might have been written out by the call to
15557              declare_in_namespace.  */
15558           if (!TREE_ASM_WRITTEN (type))
15559             gen_enumeration_type_die (type, context_die);
15560         }
15561       else
15562         gen_struct_or_union_type_die (type, context_die, usage);
15563
15564       if (need_pop)
15565         pop_decl_scope ();
15566
15567       /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
15568          it up if it is ever completed.  gen_*_type_die will set it for us
15569          when appropriate.  */
15570       return;
15571
15572     case VOID_TYPE:
15573     case INTEGER_TYPE:
15574     case REAL_TYPE:
15575     case FIXED_POINT_TYPE:
15576     case COMPLEX_TYPE:
15577     case BOOLEAN_TYPE:
15578       /* No DIEs needed for fundamental types.  */
15579       break;
15580
15581     case LANG_TYPE:
15582       /* No Dwarf representation currently defined.  */
15583       break;
15584
15585     default:
15586       gcc_unreachable ();
15587     }
15588
15589   TREE_ASM_WRITTEN (type) = 1;
15590 }
15591
15592 static void
15593 gen_type_die (tree type, dw_die_ref context_die)
15594 {
15595   gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
15596 }
15597
15598 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
15599    things which are local to the given block.  */
15600
15601 static void
15602 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
15603 {
15604   int must_output_die = 0;
15605   bool inlined_func;
15606
15607   /* Ignore blocks that are NULL.  */
15608   if (stmt == NULL_TREE)
15609     return;
15610
15611   inlined_func = inlined_function_outer_scope_p (stmt);
15612
15613   /* If the block is one fragment of a non-contiguous block, do not
15614      process the variables, since they will have been done by the
15615      origin block.  Do process subblocks.  */
15616   if (BLOCK_FRAGMENT_ORIGIN (stmt))
15617     {
15618       tree sub;
15619
15620       for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
15621         gen_block_die (sub, context_die, depth + 1);
15622
15623       return;
15624     }
15625
15626   /* Determine if we need to output any Dwarf DIEs at all to represent this
15627      block.  */
15628   if (inlined_func)
15629     /* The outer scopes for inlinings *must* always be represented.  We
15630        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
15631     must_output_die = 1;
15632   else
15633     {
15634       /* Determine if this block directly contains any "significant"
15635          local declarations which we will need to output DIEs for.  */
15636       if (debug_info_level > DINFO_LEVEL_TERSE)
15637         /* We are not in terse mode so *any* local declaration counts
15638            as being a "significant" one.  */
15639         must_output_die = ((BLOCK_VARS (stmt) != NULL
15640                             || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
15641                            && (TREE_USED (stmt)
15642                                || TREE_ASM_WRITTEN (stmt)
15643                                || BLOCK_ABSTRACT (stmt)));
15644       else if ((TREE_USED (stmt)
15645                 || TREE_ASM_WRITTEN (stmt)
15646                 || BLOCK_ABSTRACT (stmt))
15647                && !dwarf2out_ignore_block (stmt))
15648         must_output_die = 1;
15649     }
15650
15651   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
15652      DIE for any block which contains no significant local declarations at
15653      all.  Rather, in such cases we just call `decls_for_scope' so that any
15654      needed Dwarf info for any sub-blocks will get properly generated. Note
15655      that in terse mode, our definition of what constitutes a "significant"
15656      local declaration gets restricted to include only inlined function
15657      instances and local (nested) function definitions.  */
15658   if (must_output_die)
15659     {
15660       if (inlined_func)
15661         gen_inlined_subroutine_die (stmt, context_die, depth);
15662       else
15663         gen_lexical_block_die (stmt, context_die, depth);
15664     }
15665   else
15666     decls_for_scope (stmt, context_die, depth);
15667 }
15668
15669 /* Process variable DECL (or variable with origin ORIGIN) within
15670    block STMT and add it to CONTEXT_DIE.  */
15671 static void
15672 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
15673 {
15674   dw_die_ref die;
15675   tree decl_or_origin = decl ? decl : origin;
15676   tree ultimate_origin = origin ? decl_ultimate_origin (origin) : NULL;
15677
15678   if (ultimate_origin)
15679     origin = ultimate_origin;
15680
15681   if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
15682     die = lookup_decl_die (decl_or_origin);
15683   else if (TREE_CODE (decl_or_origin) == TYPE_DECL
15684            && TYPE_DECL_IS_STUB (decl_or_origin))
15685     die = lookup_type_die (TREE_TYPE (decl_or_origin));
15686   else
15687     die = NULL;
15688
15689   if (die != NULL && die->die_parent == NULL)
15690     add_child_die (context_die, die);
15691   else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
15692     dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
15693                                          stmt, context_die);
15694   else
15695     gen_decl_die (decl, origin, context_die);
15696 }
15697
15698 /* Generate all of the decls declared within a given scope and (recursively)
15699    all of its sub-blocks.  */
15700
15701 static void
15702 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
15703 {
15704   tree decl;
15705   unsigned int i;
15706   tree subblocks;
15707
15708   /* Ignore NULL blocks.  */
15709   if (stmt == NULL_TREE)
15710     return;
15711
15712   /* Output the DIEs to represent all of the data objects and typedefs
15713      declared directly within this block but not within any nested
15714      sub-blocks.  Also, nested function and tag DIEs have been
15715      generated with a parent of NULL; fix that up now.  */
15716   for (decl = BLOCK_VARS (stmt); decl != NULL; decl = TREE_CHAIN (decl))
15717     process_scope_var (stmt, decl, NULL_TREE, context_die);
15718   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
15719     process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
15720                        context_die);
15721
15722   /* If we're at -g1, we're not interested in subblocks.  */
15723   if (debug_info_level <= DINFO_LEVEL_TERSE)
15724     return;
15725
15726   /* Output the DIEs to represent all sub-blocks (and the items declared
15727      therein) of this block.  */
15728   for (subblocks = BLOCK_SUBBLOCKS (stmt);
15729        subblocks != NULL;
15730        subblocks = BLOCK_CHAIN (subblocks))
15731     gen_block_die (subblocks, context_die, depth + 1);
15732 }
15733
15734 /* Is this a typedef we can avoid emitting?  */
15735
15736 static inline int
15737 is_redundant_typedef (const_tree decl)
15738 {
15739   if (TYPE_DECL_IS_STUB (decl))
15740     return 1;
15741
15742   if (DECL_ARTIFICIAL (decl)
15743       && DECL_CONTEXT (decl)
15744       && is_tagged_type (DECL_CONTEXT (decl))
15745       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
15746       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
15747     /* Also ignore the artificial member typedef for the class name.  */
15748     return 1;
15749
15750   return 0;
15751 }
15752
15753 /* Returns the DIE for a context.  */
15754
15755 static inline dw_die_ref
15756 get_context_die (tree context)
15757 {
15758   if (context)
15759     {
15760       /* Find die that represents this context.  */
15761       if (TYPE_P (context))
15762         return force_type_die (context);
15763       else
15764         return force_decl_die (context);
15765     }
15766   return comp_unit_die;
15767 }
15768
15769 /* Returns the DIE for decl.  A DIE will always be returned.  */
15770
15771 static dw_die_ref
15772 force_decl_die (tree decl)
15773 {
15774   dw_die_ref decl_die;
15775   unsigned saved_external_flag;
15776   tree save_fn = NULL_TREE;
15777   decl_die = lookup_decl_die (decl);
15778   if (!decl_die)
15779     {
15780       dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
15781
15782       decl_die = lookup_decl_die (decl);
15783       if (decl_die)
15784         return decl_die;
15785
15786       switch (TREE_CODE (decl))
15787         {
15788         case FUNCTION_DECL:
15789           /* Clear current_function_decl, so that gen_subprogram_die thinks
15790              that this is a declaration. At this point, we just want to force
15791              declaration die.  */
15792           save_fn = current_function_decl;
15793           current_function_decl = NULL_TREE;
15794           gen_subprogram_die (decl, context_die);
15795           current_function_decl = save_fn;
15796           break;
15797
15798         case VAR_DECL:
15799           /* Set external flag to force declaration die. Restore it after
15800            gen_decl_die() call.  */
15801           saved_external_flag = DECL_EXTERNAL (decl);
15802           DECL_EXTERNAL (decl) = 1;
15803           gen_decl_die (decl, NULL, context_die);
15804           DECL_EXTERNAL (decl) = saved_external_flag;
15805           break;
15806
15807         case NAMESPACE_DECL:
15808           dwarf2out_decl (decl);
15809           break;
15810
15811         default:
15812           gcc_unreachable ();
15813         }
15814
15815       /* We should be able to find the DIE now.  */
15816       if (!decl_die)
15817         decl_die = lookup_decl_die (decl);
15818       gcc_assert (decl_die);
15819     }
15820
15821   return decl_die;
15822 }
15823
15824 /* Returns the DIE for TYPE, that must not be a base type.  A DIE is
15825    always returned.  */
15826
15827 static dw_die_ref
15828 force_type_die (tree type)
15829 {
15830   dw_die_ref type_die;
15831
15832   type_die = lookup_type_die (type);
15833   if (!type_die)
15834     {
15835       dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
15836
15837       type_die = modified_type_die (type, TYPE_READONLY (type),
15838                                     TYPE_VOLATILE (type), context_die);
15839       gcc_assert (type_die);
15840     }
15841   return type_die;
15842 }
15843
15844 /* Force out any required namespaces to be able to output DECL,
15845    and return the new context_die for it, if it's changed.  */
15846
15847 static dw_die_ref
15848 setup_namespace_context (tree thing, dw_die_ref context_die)
15849 {
15850   tree context = (DECL_P (thing)
15851                   ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
15852   if (context && TREE_CODE (context) == NAMESPACE_DECL)
15853     /* Force out the namespace.  */
15854     context_die = force_decl_die (context);
15855
15856   return context_die;
15857 }
15858
15859 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
15860    type) within its namespace, if appropriate.
15861
15862    For compatibility with older debuggers, namespace DIEs only contain
15863    declarations; all definitions are emitted at CU scope.  */
15864
15865 static dw_die_ref
15866 declare_in_namespace (tree thing, dw_die_ref context_die)
15867 {
15868   dw_die_ref ns_context;
15869
15870   if (debug_info_level <= DINFO_LEVEL_TERSE)
15871     return context_die;
15872
15873   /* If this decl is from an inlined function, then don't try to emit it in its
15874      namespace, as we will get confused.  It would have already been emitted
15875      when the abstract instance of the inline function was emitted anyways.  */
15876   if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
15877     return context_die;
15878
15879   ns_context = setup_namespace_context (thing, context_die);
15880
15881   if (ns_context != context_die)
15882     {
15883       if (is_fortran ())
15884         return ns_context;
15885       if (DECL_P (thing))
15886         gen_decl_die (thing, NULL, ns_context);
15887       else
15888         gen_type_die (thing, ns_context);
15889     }
15890   return context_die;
15891 }
15892
15893 /* Generate a DIE for a namespace or namespace alias.  */
15894
15895 static void
15896 gen_namespace_die (tree decl, dw_die_ref context_die)
15897 {
15898   dw_die_ref namespace_die;
15899
15900   /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
15901      they are an alias of.  */
15902   if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
15903     {
15904       /* Output a real namespace or module.  */
15905       context_die = setup_namespace_context (decl, comp_unit_die);
15906       namespace_die = new_die (is_fortran ()
15907                                ? DW_TAG_module : DW_TAG_namespace,
15908                                context_die, decl);
15909       /* For Fortran modules defined in different CU don't add src coords.  */
15910       if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
15911         add_name_attribute (namespace_die, dwarf2_name (decl, 0));
15912       else
15913         add_name_and_src_coords_attributes (namespace_die, decl);
15914       if (DECL_EXTERNAL (decl))
15915         add_AT_flag (namespace_die, DW_AT_declaration, 1);
15916       equate_decl_number_to_die (decl, namespace_die);
15917     }
15918   else
15919     {
15920       /* Output a namespace alias.  */
15921
15922       /* Force out the namespace we are an alias of, if necessary.  */
15923       dw_die_ref origin_die
15924         = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
15925
15926       if (DECL_CONTEXT (decl) == NULL_TREE
15927           || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
15928         context_die = setup_namespace_context (decl, comp_unit_die);
15929       /* Now create the namespace alias DIE.  */
15930       namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
15931       add_name_and_src_coords_attributes (namespace_die, decl);
15932       add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
15933       equate_decl_number_to_die (decl, namespace_die);
15934     }
15935 }
15936
15937 /* Generate Dwarf debug information for a decl described by DECL.  */
15938
15939 static void
15940 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
15941 {
15942   tree decl_or_origin = decl ? decl : origin;
15943   tree class_origin = NULL;
15944
15945   if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
15946     return;
15947
15948   switch (TREE_CODE (decl_or_origin))
15949     {
15950     case ERROR_MARK:
15951       break;
15952
15953     case CONST_DECL:
15954       if (!is_fortran ())
15955         {
15956           /* The individual enumerators of an enum type get output when we output
15957              the Dwarf representation of the relevant enum type itself.  */
15958           break;
15959         }
15960
15961       /* Emit its type.  */
15962       gen_type_die (TREE_TYPE (decl), context_die);
15963
15964       /* And its containing namespace.  */
15965       context_die = declare_in_namespace (decl, context_die);
15966
15967       gen_const_die (decl, context_die);
15968       break;
15969
15970     case FUNCTION_DECL:
15971       /* Don't output any DIEs to represent mere function declarations,
15972          unless they are class members or explicit block externs.  */
15973       if (DECL_INITIAL (decl_or_origin) == NULL_TREE
15974           && DECL_CONTEXT (decl_or_origin) == NULL_TREE
15975           && (current_function_decl == NULL_TREE
15976               || DECL_ARTIFICIAL (decl_or_origin)))
15977         break;
15978
15979 #if 0
15980       /* FIXME */
15981       /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
15982          on local redeclarations of global functions.  That seems broken.  */
15983       if (current_function_decl != decl)
15984         /* This is only a declaration.  */;
15985 #endif
15986
15987       /* If we're emitting a clone, emit info for the abstract instance.  */
15988       if (origin || DECL_ORIGIN (decl) != decl)
15989         dwarf2out_abstract_function (origin ? origin : DECL_ABSTRACT_ORIGIN (decl));
15990
15991       /* If we're emitting an out-of-line copy of an inline function,
15992          emit info for the abstract instance and set up to refer to it.  */
15993       else if (cgraph_function_possibly_inlined_p (decl)
15994                && ! DECL_ABSTRACT (decl)
15995                && ! class_or_namespace_scope_p (context_die)
15996                /* dwarf2out_abstract_function won't emit a die if this is just
15997                   a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
15998                   that case, because that works only if we have a die.  */
15999                && DECL_INITIAL (decl) != NULL_TREE)
16000         {
16001           dwarf2out_abstract_function (decl);
16002           set_decl_origin_self (decl);
16003         }
16004
16005       /* Otherwise we're emitting the primary DIE for this decl.  */
16006       else if (debug_info_level > DINFO_LEVEL_TERSE)
16007         {
16008           /* Before we describe the FUNCTION_DECL itself, make sure that we
16009              have described its return type.  */
16010           gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
16011
16012           /* And its virtual context.  */
16013           if (DECL_VINDEX (decl) != NULL_TREE)
16014             gen_type_die (DECL_CONTEXT (decl), context_die);
16015
16016           /* And its containing type.  */
16017           if (!origin)
16018             origin = decl_class_context (decl);
16019           if (origin != NULL_TREE)
16020             gen_type_die_for_member (origin, decl, context_die);
16021
16022           /* And its containing namespace.  */
16023           context_die = declare_in_namespace (decl, context_die);
16024         }
16025
16026       /* Now output a DIE to represent the function itself.  */
16027       if (decl)
16028         gen_subprogram_die (decl, context_die);
16029       break;
16030
16031     case TYPE_DECL:
16032       /* If we are in terse mode, don't generate any DIEs to represent any
16033          actual typedefs.  */
16034       if (debug_info_level <= DINFO_LEVEL_TERSE)
16035         break;
16036
16037       /* In the special case of a TYPE_DECL node representing the declaration
16038          of some type tag, if the given TYPE_DECL is marked as having been
16039          instantiated from some other (original) TYPE_DECL node (e.g. one which
16040          was generated within the original definition of an inline function) we
16041          used to generate a special (abbreviated) DW_TAG_structure_type,
16042          DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  But nothing
16043          should be actually referencing those DIEs, as variable DIEs with that
16044          type would be emitted already in the abstract origin, so it was always
16045          removed during unused type prunning.  Don't add anything in this
16046          case.  */
16047       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
16048         break;
16049
16050       if (is_redundant_typedef (decl))
16051         gen_type_die (TREE_TYPE (decl), context_die);
16052       else
16053         /* Output a DIE to represent the typedef itself.  */
16054         gen_typedef_die (decl, context_die);
16055       break;
16056
16057     case LABEL_DECL:
16058       if (debug_info_level >= DINFO_LEVEL_NORMAL)
16059         gen_label_die (decl, context_die);
16060       break;
16061
16062     case VAR_DECL:
16063     case RESULT_DECL:
16064       /* If we are in terse mode, don't generate any DIEs to represent any
16065          variable declarations or definitions.  */
16066       if (debug_info_level <= DINFO_LEVEL_TERSE)
16067         break;
16068
16069       /* Output any DIEs that are needed to specify the type of this data
16070          object.  */
16071       if ((TREE_CODE (decl_or_origin) == RESULT_DECL
16072            || TREE_CODE (decl_or_origin) == VAR_DECL)
16073           && DECL_BY_REFERENCE (decl_or_origin))
16074         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
16075       else
16076         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
16077
16078       /* And its containing type.  */
16079       class_origin = decl_class_context (decl_or_origin);
16080       if (class_origin != NULL_TREE)
16081         gen_type_die_for_member (class_origin, decl_or_origin, context_die);
16082
16083       /* And its containing namespace.  */
16084       context_die = declare_in_namespace (decl_or_origin, context_die);
16085
16086       /* Now output the DIE to represent the data object itself.  This gets
16087          complicated because of the possibility that the VAR_DECL really
16088          represents an inlined instance of a formal parameter for an inline
16089          function.  */
16090       if (!origin)
16091         origin = decl_ultimate_origin (decl);
16092       if (origin != NULL_TREE && TREE_CODE (origin) == PARM_DECL)
16093         gen_formal_parameter_die (decl, origin, context_die);
16094       else
16095         gen_variable_die (decl, origin, context_die);
16096       break;
16097
16098     case FIELD_DECL:
16099       /* Ignore the nameless fields that are used to skip bits but handle C++
16100          anonymous unions and structs.  */
16101       if (DECL_NAME (decl) != NULL_TREE
16102           || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
16103           || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
16104         {
16105           gen_type_die (member_declared_type (decl), context_die);
16106           gen_field_die (decl, context_die);
16107         }
16108       break;
16109
16110     case PARM_DECL:
16111       if (DECL_BY_REFERENCE (decl_or_origin))
16112         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
16113       else
16114         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
16115       gen_formal_parameter_die (decl, origin, context_die);
16116       break;
16117
16118     case NAMESPACE_DECL:
16119     case IMPORTED_DECL:
16120       gen_namespace_die (decl, context_die);
16121       break;
16122
16123     default:
16124       /* Probably some frontend-internal decl.  Assume we don't care.  */
16125       gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
16126       break;
16127     }
16128 }
16129 \f
16130 /* Output debug information for global decl DECL.  Called from toplev.c after
16131    compilation proper has finished.  */
16132
16133 static void
16134 dwarf2out_global_decl (tree decl)
16135 {
16136   /* Output DWARF2 information for file-scope tentative data object
16137      declarations, file-scope (extern) function declarations (which
16138      had no corresponding body) and file-scope tagged type declarations
16139      and definitions which have not yet been forced out.  */
16140   if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
16141     dwarf2out_decl (decl);
16142 }
16143
16144 /* Output debug information for type decl DECL.  Called from toplev.c
16145    and from language front ends (to record built-in types).  */
16146 static void
16147 dwarf2out_type_decl (tree decl, int local)
16148 {
16149   if (!local)
16150     dwarf2out_decl (decl);
16151 }
16152
16153 /* Output debug information for imported module or decl DECL.
16154    NAME is non-NULL name in the lexical block if the decl has been renamed.
16155    LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
16156    that DECL belongs to.
16157    LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK.  */
16158 static void
16159 dwarf2out_imported_module_or_decl_1 (tree decl,
16160                                      tree name,
16161                                      tree lexical_block,
16162                                      dw_die_ref lexical_block_die)
16163 {
16164   expanded_location xloc;
16165   dw_die_ref imported_die = NULL;
16166   dw_die_ref at_import_die;
16167
16168   if (TREE_CODE (decl) == IMPORTED_DECL)
16169     {
16170       xloc = expand_location (DECL_SOURCE_LOCATION (decl));
16171       decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
16172       gcc_assert (decl);
16173     }
16174   else
16175     xloc = expand_location (input_location);
16176
16177   if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
16178     {
16179       if (is_base_type (TREE_TYPE (decl)))
16180         at_import_die = base_type_die (TREE_TYPE (decl));
16181       else
16182         at_import_die = force_type_die (TREE_TYPE (decl));
16183       /* For namespace N { typedef void T; } using N::T; base_type_die
16184          returns NULL, but DW_TAG_imported_declaration requires
16185          the DW_AT_import tag.  Force creation of DW_TAG_typedef.  */
16186       if (!at_import_die)
16187         {
16188           gcc_assert (TREE_CODE (decl) == TYPE_DECL);
16189           gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
16190           at_import_die = lookup_type_die (TREE_TYPE (decl));
16191           gcc_assert (at_import_die);
16192         }
16193     }
16194   else
16195     {
16196       at_import_die = lookup_decl_die (decl);
16197       if (!at_import_die)
16198         {
16199           /* If we're trying to avoid duplicate debug info, we may not have
16200              emitted the member decl for this field.  Emit it now.  */
16201           if (TREE_CODE (decl) == FIELD_DECL)
16202             {
16203               tree type = DECL_CONTEXT (decl);
16204
16205               if (TYPE_CONTEXT (type)
16206                   && TYPE_P (TYPE_CONTEXT (type))
16207                   && !should_emit_struct_debug (TYPE_CONTEXT (type),
16208                                                 DINFO_USAGE_DIR_USE))
16209                 return;
16210               gen_type_die_for_member (type, decl,
16211                                        get_context_die (TYPE_CONTEXT (type)));
16212             }
16213           at_import_die = force_decl_die (decl);
16214         }
16215     }
16216
16217   if (TREE_CODE (decl) == NAMESPACE_DECL)
16218     imported_die = new_die (DW_TAG_imported_module,
16219                             lexical_block_die,
16220                             lexical_block);
16221   else
16222     imported_die = new_die (DW_TAG_imported_declaration,
16223                             lexical_block_die,
16224                             lexical_block);
16225
16226   add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
16227   add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
16228   if (name)
16229     add_AT_string (imported_die, DW_AT_name,
16230                    IDENTIFIER_POINTER (name));
16231   add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
16232 }
16233
16234 /* Output debug information for imported module or decl DECL.
16235    NAME is non-NULL name in context if the decl has been renamed.
16236    CHILD is true if decl is one of the renamed decls as part of
16237    importing whole module.  */
16238
16239 static void
16240 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
16241                                    bool child)
16242 {
16243   /* dw_die_ref at_import_die;  */
16244   dw_die_ref scope_die;
16245
16246   if (debug_info_level <= DINFO_LEVEL_TERSE)
16247     return;
16248
16249   gcc_assert (decl);
16250
16251   /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
16252      We need decl DIE for reference and scope die. First, get DIE for the decl
16253      itself.  */
16254
16255   /* Get the scope die for decl context. Use comp_unit_die for global module
16256      or decl. If die is not found for non globals, force new die.  */
16257   if (context
16258       && TYPE_P (context)
16259       && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
16260     return;
16261   scope_die = get_context_die (context);
16262
16263   if (child)
16264     {
16265       gcc_assert (scope_die->die_child);
16266       gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
16267       gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
16268       scope_die = scope_die->die_child;
16269     }
16270
16271   /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
16272   dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
16273
16274 }
16275
16276 /* Write the debugging output for DECL.  */
16277
16278 void
16279 dwarf2out_decl (tree decl)
16280 {
16281   dw_die_ref context_die = comp_unit_die;
16282
16283   switch (TREE_CODE (decl))
16284     {
16285     case ERROR_MARK:
16286       return;
16287
16288     case FUNCTION_DECL:
16289       /* What we would really like to do here is to filter out all mere
16290          file-scope declarations of file-scope functions which are never
16291          referenced later within this translation unit (and keep all of ones
16292          that *are* referenced later on) but we aren't clairvoyant, so we have
16293          no idea which functions will be referenced in the future (i.e. later
16294          on within the current translation unit). So here we just ignore all
16295          file-scope function declarations which are not also definitions.  If
16296          and when the debugger needs to know something about these functions,
16297          it will have to hunt around and find the DWARF information associated
16298          with the definition of the function.
16299
16300          We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
16301          nodes represent definitions and which ones represent mere
16302          declarations.  We have to check DECL_INITIAL instead. That's because
16303          the C front-end supports some weird semantics for "extern inline"
16304          function definitions.  These can get inlined within the current
16305          translation unit (and thus, we need to generate Dwarf info for their
16306          abstract instances so that the Dwarf info for the concrete inlined
16307          instances can have something to refer to) but the compiler never
16308          generates any out-of-lines instances of such things (despite the fact
16309          that they *are* definitions).
16310
16311          The important point is that the C front-end marks these "extern
16312          inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
16313          them anyway. Note that the C++ front-end also plays some similar games
16314          for inline function definitions appearing within include files which
16315          also contain `#pragma interface' pragmas.  */
16316       if (DECL_INITIAL (decl) == NULL_TREE)
16317         return;
16318
16319       /* If we're a nested function, initially use a parent of NULL; if we're
16320          a plain function, this will be fixed up in decls_for_scope.  If
16321          we're a method, it will be ignored, since we already have a DIE.  */
16322       if (decl_function_context (decl)
16323           /* But if we're in terse mode, we don't care about scope.  */
16324           && debug_info_level > DINFO_LEVEL_TERSE)
16325         context_die = NULL;
16326       break;
16327
16328     case VAR_DECL:
16329       /* Ignore this VAR_DECL if it refers to a file-scope extern data object
16330          declaration and if the declaration was never even referenced from
16331          within this entire compilation unit.  We suppress these DIEs in
16332          order to save space in the .debug section (by eliminating entries
16333          which are probably useless).  Note that we must not suppress
16334          block-local extern declarations (whether used or not) because that
16335          would screw-up the debugger's name lookup mechanism and cause it to
16336          miss things which really ought to be in scope at a given point.  */
16337       if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
16338         return;
16339
16340       /* For local statics lookup proper context die.  */
16341       if (TREE_STATIC (decl) && decl_function_context (decl))
16342         context_die = lookup_decl_die (DECL_CONTEXT (decl));
16343
16344       /* If we are in terse mode, don't generate any DIEs to represent any
16345          variable declarations or definitions.  */
16346       if (debug_info_level <= DINFO_LEVEL_TERSE)
16347         return;
16348       break;
16349
16350     case CONST_DECL:
16351       if (debug_info_level <= DINFO_LEVEL_TERSE)
16352         return;
16353       if (!is_fortran ())
16354         return;
16355       if (TREE_STATIC (decl) && decl_function_context (decl))
16356         context_die = lookup_decl_die (DECL_CONTEXT (decl));
16357       break;
16358
16359     case NAMESPACE_DECL:
16360     case IMPORTED_DECL:
16361       if (debug_info_level <= DINFO_LEVEL_TERSE)
16362         return;
16363       if (lookup_decl_die (decl) != NULL)
16364         return;
16365       break;
16366
16367     case TYPE_DECL:
16368       /* Don't emit stubs for types unless they are needed by other DIEs.  */
16369       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
16370         return;
16371
16372       /* Don't bother trying to generate any DIEs to represent any of the
16373          normal built-in types for the language we are compiling.  */
16374       if (DECL_IS_BUILTIN (decl))
16375         {
16376           /* OK, we need to generate one for `bool' so GDB knows what type
16377              comparisons have.  */
16378           if (is_cxx ()
16379               && TREE_CODE (TREE_TYPE (decl)) == BOOLEAN_TYPE
16380               && ! DECL_IGNORED_P (decl))
16381             modified_type_die (TREE_TYPE (decl), 0, 0, NULL);
16382
16383           return;
16384         }
16385
16386       /* If we are in terse mode, don't generate any DIEs for types.  */
16387       if (debug_info_level <= DINFO_LEVEL_TERSE)
16388         return;
16389
16390       /* If we're a function-scope tag, initially use a parent of NULL;
16391          this will be fixed up in decls_for_scope.  */
16392       if (decl_function_context (decl))
16393         context_die = NULL;
16394
16395       break;
16396
16397     default:
16398       return;
16399     }
16400
16401   gen_decl_die (decl, NULL, context_die);
16402 }
16403
16404 /* Output a marker (i.e. a label) for the beginning of the generated code for
16405    a lexical block.  */
16406
16407 static void
16408 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
16409                        unsigned int blocknum)
16410 {
16411   switch_to_section (current_function_section ());
16412   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
16413 }
16414
16415 /* Output a marker (i.e. a label) for the end of the generated code for a
16416    lexical block.  */
16417
16418 static void
16419 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
16420 {
16421   switch_to_section (current_function_section ());
16422   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
16423 }
16424
16425 /* Returns nonzero if it is appropriate not to emit any debugging
16426    information for BLOCK, because it doesn't contain any instructions.
16427
16428    Don't allow this for blocks with nested functions or local classes
16429    as we would end up with orphans, and in the presence of scheduling
16430    we may end up calling them anyway.  */
16431
16432 static bool
16433 dwarf2out_ignore_block (const_tree block)
16434 {
16435   tree decl;
16436   unsigned int i;
16437
16438   for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
16439     if (TREE_CODE (decl) == FUNCTION_DECL
16440         || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
16441       return 0;
16442   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
16443     {
16444       decl = BLOCK_NONLOCALIZED_VAR (block, i);
16445       if (TREE_CODE (decl) == FUNCTION_DECL
16446           || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
16447       return 0;
16448     }
16449
16450   return 1;
16451 }
16452
16453 /* Hash table routines for file_hash.  */
16454
16455 static int
16456 file_table_eq (const void *p1_p, const void *p2_p)
16457 {
16458   const struct dwarf_file_data *const p1 =
16459     (const struct dwarf_file_data *) p1_p;
16460   const char *const p2 = (const char *) p2_p;
16461   return strcmp (p1->filename, p2) == 0;
16462 }
16463
16464 static hashval_t
16465 file_table_hash (const void *p_p)
16466 {
16467   const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
16468   return htab_hash_string (p->filename);
16469 }
16470
16471 /* Lookup FILE_NAME (in the list of filenames that we know about here in
16472    dwarf2out.c) and return its "index".  The index of each (known) filename is
16473    just a unique number which is associated with only that one filename.  We
16474    need such numbers for the sake of generating labels (in the .debug_sfnames
16475    section) and references to those files numbers (in the .debug_srcinfo
16476    and.debug_macinfo sections).  If the filename given as an argument is not
16477    found in our current list, add it to the list and assign it the next
16478    available unique index number.  In order to speed up searches, we remember
16479    the index of the filename was looked up last.  This handles the majority of
16480    all searches.  */
16481
16482 static struct dwarf_file_data *
16483 lookup_filename (const char *file_name)
16484 {
16485   void ** slot;
16486   struct dwarf_file_data * created;
16487
16488   /* Check to see if the file name that was searched on the previous
16489      call matches this file name.  If so, return the index.  */
16490   if (file_table_last_lookup
16491       && (file_name == file_table_last_lookup->filename
16492           || strcmp (file_table_last_lookup->filename, file_name) == 0))
16493     return file_table_last_lookup;
16494
16495   /* Didn't match the previous lookup, search the table.  */
16496   slot = htab_find_slot_with_hash (file_table, file_name,
16497                                    htab_hash_string (file_name), INSERT);
16498   if (*slot)
16499     return (struct dwarf_file_data *) *slot;
16500
16501   created = GGC_NEW (struct dwarf_file_data);
16502   created->filename = file_name;
16503   created->emitted_number = 0;
16504   *slot = created;
16505   return created;
16506 }
16507
16508 /* If the assembler will construct the file table, then translate the compiler
16509    internal file table number into the assembler file table number, and emit
16510    a .file directive if we haven't already emitted one yet.  The file table
16511    numbers are different because we prune debug info for unused variables and
16512    types, which may include filenames.  */
16513
16514 static int
16515 maybe_emit_file (struct dwarf_file_data * fd)
16516 {
16517   if (! fd->emitted_number)
16518     {
16519       if (last_emitted_file)
16520         fd->emitted_number = last_emitted_file->emitted_number + 1;
16521       else
16522         fd->emitted_number = 1;
16523       last_emitted_file = fd;
16524
16525       if (DWARF2_ASM_LINE_DEBUG_INFO)
16526         {
16527           fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
16528           output_quoted_string (asm_out_file,
16529                                 remap_debug_filename (fd->filename));
16530           fputc ('\n', asm_out_file);
16531         }
16532     }
16533
16534   return fd->emitted_number;
16535 }
16536
16537 /* Replace DW_AT_name for the decl with name.  */
16538  
16539 static void
16540 dwarf2out_set_name (tree decl, tree name)
16541 {
16542   dw_die_ref die;
16543   dw_attr_ref attr;
16544
16545   die = TYPE_SYMTAB_DIE (decl);
16546   if (!die)
16547     return;
16548
16549   attr = get_AT (die, DW_AT_name);
16550   if (attr)
16551     {
16552       struct indirect_string_node *node;
16553
16554       node = find_AT_string (dwarf2_name (name, 0));
16555       /* replace the string.  */
16556       attr->dw_attr_val.v.val_str = node;
16557     }
16558
16559   else
16560     add_name_attribute (die, dwarf2_name (name, 0));
16561 }
16562
16563 /* Called by the final INSN scan whenever we see a var location.  We
16564    use it to drop labels in the right places, and throw the location in
16565    our lookup table.  */
16566
16567 static void
16568 dwarf2out_var_location (rtx loc_note)
16569 {
16570   char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
16571   struct var_loc_node *newloc;
16572   rtx next_real;
16573   static const char *last_label;
16574   static bool last_in_cold_section_p;
16575   tree decl;
16576
16577   if (!DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
16578     return;
16579
16580   next_real = next_real_insn (loc_note);
16581   /* If there are no instructions which would be affected by this note,
16582      don't do anything.  */
16583   if (next_real == NULL_RTX)
16584     return;
16585
16586   newloc = GGC_CNEW (struct var_loc_node);
16587   /* If there were no real insns between note we processed last time
16588      and this note, use the label we emitted last time.  */
16589   if (last_var_location_insn != NULL_RTX
16590       && last_var_location_insn == next_real
16591       && last_in_cold_section_p == in_cold_section_p)
16592     newloc->label = last_label;
16593   else
16594     {
16595       ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
16596       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
16597       loclabel_num++;
16598       newloc->label = ggc_strdup (loclabel);
16599     }
16600   newloc->var_loc_note = loc_note;
16601   newloc->next = NULL;
16602
16603   if (cfun && in_cold_section_p)
16604     newloc->section_label = crtl->subsections.cold_section_label;
16605   else
16606     newloc->section_label = text_section_label;
16607
16608   last_var_location_insn = next_real;
16609   last_label = newloc->label;
16610   last_in_cold_section_p = in_cold_section_p;
16611   decl = NOTE_VAR_LOCATION_DECL (loc_note);
16612   add_var_loc_to_decl (decl, newloc);
16613 }
16614
16615 /* We need to reset the locations at the beginning of each
16616    function. We can't do this in the end_function hook, because the
16617    declarations that use the locations won't have been output when
16618    that hook is called.  Also compute have_multiple_function_sections here.  */
16619
16620 static void
16621 dwarf2out_begin_function (tree fun)
16622 {
16623   htab_empty (decl_loc_table);
16624
16625   if (function_section (fun) != text_section)
16626     have_multiple_function_sections = true;
16627
16628   dwarf2out_note_section_used ();
16629 }
16630
16631 /* Output a label to mark the beginning of a source code line entry
16632    and record information relating to this source line, in
16633    'line_info_table' for later output of the .debug_line section.  */
16634
16635 static void
16636 dwarf2out_source_line (unsigned int line, const char *filename,
16637                        int discriminator, bool is_stmt)
16638 {
16639   static bool last_is_stmt = true;
16640
16641   if (debug_info_level >= DINFO_LEVEL_NORMAL
16642       && line != 0)
16643     {
16644       int file_num = maybe_emit_file (lookup_filename (filename));
16645
16646       switch_to_section (current_function_section ());
16647
16648       /* If requested, emit something human-readable.  */
16649       if (flag_debug_asm)
16650         fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
16651                  filename, line);
16652
16653       if (DWARF2_ASM_LINE_DEBUG_INFO)
16654         {
16655           /* Emit the .loc directive understood by GNU as.  */
16656           fprintf (asm_out_file, "\t.loc %d %d 0", file_num, line);
16657           if (is_stmt != last_is_stmt)
16658             {
16659               fprintf (asm_out_file, " is_stmt %d", is_stmt ? 1 : 0);
16660               last_is_stmt = is_stmt;
16661             }
16662           if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
16663             fprintf (asm_out_file, " discriminator %d", discriminator);
16664           fputc ('\n', asm_out_file);
16665
16666           /* Indicate that line number info exists.  */
16667           line_info_table_in_use++;
16668         }
16669       else if (function_section (current_function_decl) != text_section)
16670         {
16671           dw_separate_line_info_ref line_info;
16672           targetm.asm_out.internal_label (asm_out_file,
16673                                           SEPARATE_LINE_CODE_LABEL,
16674                                           separate_line_info_table_in_use);
16675
16676           /* Expand the line info table if necessary.  */
16677           if (separate_line_info_table_in_use
16678               == separate_line_info_table_allocated)
16679             {
16680               separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
16681               separate_line_info_table
16682                 = GGC_RESIZEVEC (dw_separate_line_info_entry,
16683                                  separate_line_info_table,
16684                                  separate_line_info_table_allocated);
16685               memset (separate_line_info_table
16686                        + separate_line_info_table_in_use,
16687                       0,
16688                       (LINE_INFO_TABLE_INCREMENT
16689                        * sizeof (dw_separate_line_info_entry)));
16690             }
16691
16692           /* Add the new entry at the end of the line_info_table.  */
16693           line_info
16694             = &separate_line_info_table[separate_line_info_table_in_use++];
16695           line_info->dw_file_num = file_num;
16696           line_info->dw_line_num = line;
16697           line_info->function = current_function_funcdef_no;
16698         }
16699       else
16700         {
16701           dw_line_info_ref line_info;
16702
16703           targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL,
16704                                      line_info_table_in_use);
16705
16706           /* Expand the line info table if necessary.  */
16707           if (line_info_table_in_use == line_info_table_allocated)
16708             {
16709               line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
16710               line_info_table
16711                 = GGC_RESIZEVEC (dw_line_info_entry, line_info_table,
16712                                  line_info_table_allocated);
16713               memset (line_info_table + line_info_table_in_use, 0,
16714                       LINE_INFO_TABLE_INCREMENT * sizeof (dw_line_info_entry));
16715             }
16716
16717           /* Add the new entry at the end of the line_info_table.  */
16718           line_info = &line_info_table[line_info_table_in_use++];
16719           line_info->dw_file_num = file_num;
16720           line_info->dw_line_num = line;
16721         }
16722     }
16723 }
16724
16725 /* Record the beginning of a new source file.  */
16726
16727 static void
16728 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
16729 {
16730   if (flag_eliminate_dwarf2_dups)
16731     {
16732       /* Record the beginning of the file for break_out_includes.  */
16733       dw_die_ref bincl_die;
16734
16735       bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die, NULL);
16736       add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
16737     }
16738
16739   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
16740     {
16741       int file_num = maybe_emit_file (lookup_filename (filename));
16742
16743       switch_to_section (debug_macinfo_section);
16744       dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
16745       dw2_asm_output_data_uleb128 (lineno, "Included from line number %d",
16746                                    lineno);
16747
16748       dw2_asm_output_data_uleb128 (file_num, "file %s", filename);
16749     }
16750 }
16751
16752 /* Record the end of a source file.  */
16753
16754 static void
16755 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
16756 {
16757   if (flag_eliminate_dwarf2_dups)
16758     /* Record the end of the file for break_out_includes.  */
16759     new_die (DW_TAG_GNU_EINCL, comp_unit_die, NULL);
16760
16761   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
16762     {
16763       switch_to_section (debug_macinfo_section);
16764       dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
16765     }
16766 }
16767
16768 /* Called from debug_define in toplev.c.  The `buffer' parameter contains
16769    the tail part of the directive line, i.e. the part which is past the
16770    initial whitespace, #, whitespace, directive-name, whitespace part.  */
16771
16772 static void
16773 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
16774                   const char *buffer ATTRIBUTE_UNUSED)
16775 {
16776   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
16777     {
16778       switch_to_section (debug_macinfo_section);
16779       dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
16780       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
16781       dw2_asm_output_nstring (buffer, -1, "The macro");
16782     }
16783 }
16784
16785 /* Called from debug_undef in toplev.c.  The `buffer' parameter contains
16786    the tail part of the directive line, i.e. the part which is past the
16787    initial whitespace, #, whitespace, directive-name, whitespace part.  */
16788
16789 static void
16790 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
16791                  const char *buffer ATTRIBUTE_UNUSED)
16792 {
16793   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
16794     {
16795       switch_to_section (debug_macinfo_section);
16796       dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
16797       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
16798       dw2_asm_output_nstring (buffer, -1, "The macro");
16799     }
16800 }
16801
16802 /* Set up for Dwarf output at the start of compilation.  */
16803
16804 static void
16805 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
16806 {
16807   /* Allocate the file_table.  */
16808   file_table = htab_create_ggc (50, file_table_hash,
16809                                 file_table_eq, NULL);
16810
16811   /* Allocate the decl_die_table.  */
16812   decl_die_table = htab_create_ggc (10, decl_die_table_hash,
16813                                     decl_die_table_eq, NULL);
16814
16815   /* Allocate the decl_loc_table.  */
16816   decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
16817                                     decl_loc_table_eq, NULL);
16818
16819   /* Allocate the initial hunk of the decl_scope_table.  */
16820   decl_scope_table = VEC_alloc (tree, gc, 256);
16821
16822   /* Allocate the initial hunk of the abbrev_die_table.  */
16823   abbrev_die_table = GGC_CNEWVEC (dw_die_ref, ABBREV_DIE_TABLE_INCREMENT);
16824   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
16825   /* Zero-th entry is allocated, but unused.  */
16826   abbrev_die_table_in_use = 1;
16827
16828   /* Allocate the initial hunk of the line_info_table.  */
16829   line_info_table = GGC_CNEWVEC (dw_line_info_entry, LINE_INFO_TABLE_INCREMENT);
16830   line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
16831
16832   /* Zero-th entry is allocated, but unused.  */
16833   line_info_table_in_use = 1;
16834
16835   /* Allocate the pubtypes and pubnames vectors.  */
16836   pubname_table = VEC_alloc (pubname_entry, gc, 32);
16837   pubtype_table = VEC_alloc (pubname_entry, gc, 32);
16838
16839   /* Generate the initial DIE for the .debug section.  Note that the (string)
16840      value given in the DW_AT_name attribute of the DW_TAG_compile_unit DIE
16841      will (typically) be a relative pathname and that this pathname should be
16842      taken as being relative to the directory from which the compiler was
16843      invoked when the given (base) source file was compiled.  We will fill
16844      in this value in dwarf2out_finish.  */
16845   comp_unit_die = gen_compile_unit_die (NULL);
16846
16847   incomplete_types = VEC_alloc (tree, gc, 64);
16848
16849   used_rtx_array = VEC_alloc (rtx, gc, 32);
16850
16851   debug_info_section = get_section (DEBUG_INFO_SECTION,
16852                                     SECTION_DEBUG, NULL);
16853   debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
16854                                       SECTION_DEBUG, NULL);
16855   debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
16856                                        SECTION_DEBUG, NULL);
16857   debug_macinfo_section = get_section (DEBUG_MACINFO_SECTION,
16858                                        SECTION_DEBUG, NULL);
16859   debug_line_section = get_section (DEBUG_LINE_SECTION,
16860                                     SECTION_DEBUG, NULL);
16861   debug_loc_section = get_section (DEBUG_LOC_SECTION,
16862                                    SECTION_DEBUG, NULL);
16863   debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
16864                                         SECTION_DEBUG, NULL);
16865   debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
16866                                         SECTION_DEBUG, NULL);
16867   debug_str_section = get_section (DEBUG_STR_SECTION,
16868                                    DEBUG_STR_SECTION_FLAGS, NULL);
16869   debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
16870                                       SECTION_DEBUG, NULL);
16871   debug_frame_section = get_section (DEBUG_FRAME_SECTION,
16872                                      SECTION_DEBUG, NULL);
16873
16874   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
16875   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
16876                                DEBUG_ABBREV_SECTION_LABEL, 0);
16877   ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
16878   ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
16879                                COLD_TEXT_SECTION_LABEL, 0);
16880   ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
16881
16882   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
16883                                DEBUG_INFO_SECTION_LABEL, 0);
16884   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
16885                                DEBUG_LINE_SECTION_LABEL, 0);
16886   ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
16887                                DEBUG_RANGES_SECTION_LABEL, 0);
16888   switch_to_section (debug_abbrev_section);
16889   ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
16890   switch_to_section (debug_info_section);
16891   ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
16892   switch_to_section (debug_line_section);
16893   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
16894
16895   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
16896     {
16897       switch_to_section (debug_macinfo_section);
16898       ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
16899                                    DEBUG_MACINFO_SECTION_LABEL, 0);
16900       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
16901     }
16902
16903   switch_to_section (text_section);
16904   ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
16905   if (flag_reorder_blocks_and_partition)
16906     {
16907       cold_text_section = unlikely_text_section ();
16908       switch_to_section (cold_text_section);
16909       ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
16910     }
16911 }
16912
16913 /* A helper function for dwarf2out_finish called through
16914    ht_forall.  Emit one queued .debug_str string.  */
16915
16916 static int
16917 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
16918 {
16919   struct indirect_string_node *node = (struct indirect_string_node *) *h;
16920
16921   if (node->form == DW_FORM_strp)
16922     {
16923       switch_to_section (debug_str_section);
16924       ASM_OUTPUT_LABEL (asm_out_file, node->label);
16925       assemble_string (node->str, strlen (node->str) + 1);
16926     }
16927
16928   return 1;
16929 }
16930
16931 #if ENABLE_ASSERT_CHECKING
16932 /* Verify that all marks are clear.  */
16933
16934 static void
16935 verify_marks_clear (dw_die_ref die)
16936 {
16937   dw_die_ref c;
16938
16939   gcc_assert (! die->die_mark);
16940   FOR_EACH_CHILD (die, c, verify_marks_clear (c));
16941 }
16942 #endif /* ENABLE_ASSERT_CHECKING */
16943
16944 /* Clear the marks for a die and its children.
16945    Be cool if the mark isn't set.  */
16946
16947 static void
16948 prune_unmark_dies (dw_die_ref die)
16949 {
16950   dw_die_ref c;
16951
16952   if (die->die_mark)
16953     die->die_mark = 0;
16954   FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
16955 }
16956
16957 /* Given DIE that we're marking as used, find any other dies
16958    it references as attributes and mark them as used.  */
16959
16960 static void
16961 prune_unused_types_walk_attribs (dw_die_ref die)
16962 {
16963   dw_attr_ref a;
16964   unsigned ix;
16965
16966   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
16967     {
16968       if (a->dw_attr_val.val_class == dw_val_class_die_ref)
16969         {
16970           /* A reference to another DIE.
16971              Make sure that it will get emitted.  */
16972           prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
16973         }
16974       /* Set the string's refcount to 0 so that prune_unused_types_mark
16975          accounts properly for it.  */
16976       if (AT_class (a) == dw_val_class_str)
16977         a->dw_attr_val.v.val_str->refcount = 0;
16978     }
16979 }
16980
16981
16982 /* Mark DIE as being used.  If DOKIDS is true, then walk down
16983    to DIE's children.  */
16984
16985 static void
16986 prune_unused_types_mark (dw_die_ref die, int dokids)
16987 {
16988   dw_die_ref c;
16989
16990   if (die->die_mark == 0)
16991     {
16992       /* We haven't done this node yet.  Mark it as used.  */
16993       die->die_mark = 1;
16994
16995       /* We also have to mark its parents as used.
16996          (But we don't want to mark our parents' kids due to this.)  */
16997       if (die->die_parent)
16998         prune_unused_types_mark (die->die_parent, 0);
16999
17000       /* Mark any referenced nodes.  */
17001       prune_unused_types_walk_attribs (die);
17002
17003       /* If this node is a specification,
17004          also mark the definition, if it exists.  */
17005       if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
17006         prune_unused_types_mark (die->die_definition, 1);
17007     }
17008
17009   if (dokids && die->die_mark != 2)
17010     {
17011       /* We need to walk the children, but haven't done so yet.
17012          Remember that we've walked the kids.  */
17013       die->die_mark = 2;
17014
17015       /* If this is an array type, we need to make sure our
17016          kids get marked, even if they're types.  */
17017       if (die->die_tag == DW_TAG_array_type)
17018         FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
17019       else
17020         FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
17021     }
17022 }
17023
17024 /* For local classes, look if any static member functions were emitted
17025    and if so, mark them.  */
17026
17027 static void
17028 prune_unused_types_walk_local_classes (dw_die_ref die)
17029 {
17030   dw_die_ref c;
17031
17032   if (die->die_mark == 2)
17033     return;
17034
17035   switch (die->die_tag)
17036     {
17037     case DW_TAG_structure_type:
17038     case DW_TAG_union_type:
17039     case DW_TAG_class_type:
17040       break;
17041
17042     case DW_TAG_subprogram:
17043       if (!get_AT_flag (die, DW_AT_declaration)
17044           || die->die_definition != NULL)
17045         prune_unused_types_mark (die, 1);
17046       return;
17047
17048     default:
17049       return;
17050     }
17051
17052   /* Mark children.  */
17053   FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
17054 }
17055
17056 /* Walk the tree DIE and mark types that we actually use.  */
17057
17058 static void
17059 prune_unused_types_walk (dw_die_ref die)
17060 {
17061   dw_die_ref c;
17062
17063   /* Don't do anything if this node is already marked and
17064      children have been marked as well.  */
17065   if (die->die_mark == 2)
17066     return;
17067
17068   switch (die->die_tag)
17069     {
17070     case DW_TAG_structure_type:
17071     case DW_TAG_union_type:
17072     case DW_TAG_class_type:
17073       if (die->die_perennial_p)
17074         break;
17075
17076       for (c = die->die_parent; c; c = c->die_parent)
17077         if (c->die_tag == DW_TAG_subprogram)
17078           break;
17079
17080       /* Finding used static member functions inside of classes
17081          is needed just for local classes, because for other classes
17082          static member function DIEs with DW_AT_specification
17083          are emitted outside of the DW_TAG_*_type.  If we ever change
17084          it, we'd need to call this even for non-local classes.  */
17085       if (c)
17086         prune_unused_types_walk_local_classes (die);
17087
17088       /* It's a type node --- don't mark it.  */
17089       return;
17090
17091     case DW_TAG_const_type:
17092     case DW_TAG_packed_type:
17093     case DW_TAG_pointer_type:
17094     case DW_TAG_reference_type:
17095     case DW_TAG_volatile_type:
17096     case DW_TAG_typedef:
17097     case DW_TAG_array_type:
17098     case DW_TAG_interface_type:
17099     case DW_TAG_friend:
17100     case DW_TAG_variant_part:
17101     case DW_TAG_enumeration_type:
17102     case DW_TAG_subroutine_type:
17103     case DW_TAG_string_type:
17104     case DW_TAG_set_type:
17105     case DW_TAG_subrange_type:
17106     case DW_TAG_ptr_to_member_type:
17107     case DW_TAG_file_type:
17108       if (die->die_perennial_p)
17109         break;
17110
17111       /* It's a type node --- don't mark it.  */
17112       return;
17113
17114     default:
17115       /* Mark everything else.  */
17116       break;
17117   }
17118
17119   if (die->die_mark == 0)
17120     {
17121       die->die_mark = 1;
17122
17123       /* Now, mark any dies referenced from here.  */
17124       prune_unused_types_walk_attribs (die);
17125     }
17126
17127   die->die_mark = 2;
17128
17129   /* Mark children.  */
17130   FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
17131 }
17132
17133 /* Increment the string counts on strings referred to from DIE's
17134    attributes.  */
17135
17136 static void
17137 prune_unused_types_update_strings (dw_die_ref die)
17138 {
17139   dw_attr_ref a;
17140   unsigned ix;
17141
17142   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
17143     if (AT_class (a) == dw_val_class_str)
17144       {
17145         struct indirect_string_node *s = a->dw_attr_val.v.val_str;
17146         s->refcount++;
17147         /* Avoid unnecessarily putting strings that are used less than
17148            twice in the hash table.  */
17149         if (s->refcount
17150             == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
17151           {
17152             void ** slot;
17153             slot = htab_find_slot_with_hash (debug_str_hash, s->str,
17154                                              htab_hash_string (s->str),
17155                                              INSERT);
17156             gcc_assert (*slot == NULL);
17157             *slot = s;
17158           }
17159       }
17160 }
17161
17162 /* Remove from the tree DIE any dies that aren't marked.  */
17163
17164 static void
17165 prune_unused_types_prune (dw_die_ref die)
17166 {
17167   dw_die_ref c;
17168
17169   gcc_assert (die->die_mark);
17170   prune_unused_types_update_strings (die);
17171
17172   if (! die->die_child)
17173     return;
17174
17175   c = die->die_child;
17176   do {
17177     dw_die_ref prev = c;
17178     for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
17179       if (c == die->die_child)
17180         {
17181           /* No marked children between 'prev' and the end of the list.  */
17182           if (prev == c)
17183             /* No marked children at all.  */
17184             die->die_child = NULL;
17185           else
17186             {
17187               prev->die_sib = c->die_sib;
17188               die->die_child = prev;
17189             }
17190           return;
17191         }
17192
17193     if (c != prev->die_sib)
17194       prev->die_sib = c;
17195     prune_unused_types_prune (c);
17196   } while (c != die->die_child);
17197 }
17198
17199
17200 /* Remove dies representing declarations that we never use.  */
17201
17202 static void
17203 prune_unused_types (void)
17204 {
17205   unsigned int i;
17206   limbo_die_node *node;
17207   pubname_ref pub;
17208
17209 #if ENABLE_ASSERT_CHECKING
17210   /* All the marks should already be clear.  */
17211   verify_marks_clear (comp_unit_die);
17212   for (node = limbo_die_list; node; node = node->next)
17213     verify_marks_clear (node->die);
17214 #endif /* ENABLE_ASSERT_CHECKING */
17215
17216   /* Set the mark on nodes that are actually used.  */
17217   prune_unused_types_walk (comp_unit_die);
17218   for (node = limbo_die_list; node; node = node->next)
17219     prune_unused_types_walk (node->die);
17220
17221   /* Also set the mark on nodes referenced from the
17222      pubname_table or arange_table.  */
17223   for (i = 0; VEC_iterate (pubname_entry, pubname_table, i, pub); i++)
17224     prune_unused_types_mark (pub->die, 1);
17225   for (i = 0; i < arange_table_in_use; i++)
17226     prune_unused_types_mark (arange_table[i], 1);
17227
17228   /* Get rid of nodes that aren't marked; and update the string counts.  */
17229   if (debug_str_hash)
17230     htab_empty (debug_str_hash);
17231   prune_unused_types_prune (comp_unit_die);
17232   for (node = limbo_die_list; node; node = node->next)
17233     prune_unused_types_prune (node->die);
17234
17235   /* Leave the marks clear.  */
17236   prune_unmark_dies (comp_unit_die);
17237   for (node = limbo_die_list; node; node = node->next)
17238     prune_unmark_dies (node->die);
17239 }
17240
17241 /* Set the parameter to true if there are any relative pathnames in
17242    the file table.  */
17243 static int
17244 file_table_relative_p (void ** slot, void *param)
17245 {
17246   bool *p = (bool *) param;
17247   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
17248   if (!IS_ABSOLUTE_PATH (d->filename))
17249     {
17250       *p = true;
17251       return 0;
17252     }
17253   return 1;
17254 }
17255
17256 /* Move a DW_AT_MIPS_linkage_name attribute just added to dw_die_ref
17257    to the location it would have been added, should we know its
17258    DECL_ASSEMBLER_NAME when we added other attributes.  This will
17259    probably improve compactness of debug info, removing equivalent
17260    abbrevs, and hide any differences caused by deferring the
17261    computation of the assembler name, triggered by e.g. PCH.  */
17262
17263 static inline void
17264 move_linkage_attr (dw_die_ref die)
17265 {
17266   unsigned ix = VEC_length (dw_attr_node, die->die_attr);
17267   dw_attr_node linkage = *VEC_index (dw_attr_node, die->die_attr, ix - 1);
17268
17269   gcc_assert (linkage.dw_attr == DW_AT_MIPS_linkage_name);
17270
17271   while (--ix > 0)
17272     {
17273       dw_attr_node *prev = VEC_index (dw_attr_node, die->die_attr, ix - 1);
17274
17275       if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
17276         break;
17277     }
17278
17279   if (ix != VEC_length (dw_attr_node, die->die_attr) - 1)
17280     {
17281       VEC_pop (dw_attr_node, die->die_attr);
17282       VEC_quick_insert (dw_attr_node, die->die_attr, ix, &linkage);
17283     }
17284 }
17285
17286 /* Output stuff that dwarf requires at the end of every file,
17287    and generate the DWARF-2 debugging info.  */
17288
17289 static void
17290 dwarf2out_finish (const char *filename)
17291 {
17292   limbo_die_node *node, *next_node;
17293   dw_die_ref die = 0;
17294   unsigned int i;
17295
17296   /* Add the name for the main input file now.  We delayed this from
17297      dwarf2out_init to avoid complications with PCH.  */
17298   add_name_attribute (comp_unit_die, remap_debug_filename (filename));
17299   if (!IS_ABSOLUTE_PATH (filename))
17300     add_comp_dir_attribute (comp_unit_die);
17301   else if (get_AT (comp_unit_die, DW_AT_comp_dir) == NULL)
17302     {
17303       bool p = false;
17304       htab_traverse (file_table, file_table_relative_p, &p);
17305       if (p)
17306         add_comp_dir_attribute (comp_unit_die);
17307     }
17308
17309   for (i = 0; i < VEC_length (deferred_locations, deferred_locations_list); i++)
17310     {
17311       add_location_or_const_value_attribute (
17312         VEC_index (deferred_locations, deferred_locations_list, i)->die,
17313         VEC_index (deferred_locations, deferred_locations_list, i)->variable,
17314         DW_AT_location);
17315     }
17316
17317   /* Traverse the limbo die list, and add parent/child links.  The only
17318      dies without parents that should be here are concrete instances of
17319      inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
17320      For concrete instances, we can get the parent die from the abstract
17321      instance.  */
17322   for (node = limbo_die_list; node; node = next_node)
17323     {
17324       next_node = node->next;
17325       die = node->die;
17326
17327       if (die->die_parent == NULL)
17328         {
17329           dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
17330
17331           if (origin)
17332             add_child_die (origin->die_parent, die);
17333           else if (die == comp_unit_die)
17334             ;
17335           else if (errorcount > 0 || sorrycount > 0)
17336             /* It's OK to be confused by errors in the input.  */
17337             add_child_die (comp_unit_die, die);
17338           else
17339             {
17340               /* In certain situations, the lexical block containing a
17341                  nested function can be optimized away, which results
17342                  in the nested function die being orphaned.  Likewise
17343                  with the return type of that nested function.  Force
17344                  this to be a child of the containing function.
17345
17346                  It may happen that even the containing function got fully
17347                  inlined and optimized out.  In that case we are lost and
17348                  assign the empty child.  This should not be big issue as
17349                  the function is likely unreachable too.  */
17350               tree context = NULL_TREE;
17351
17352               gcc_assert (node->created_for);
17353
17354               if (DECL_P (node->created_for))
17355                 context = DECL_CONTEXT (node->created_for);
17356               else if (TYPE_P (node->created_for))
17357                 context = TYPE_CONTEXT (node->created_for);
17358
17359               gcc_assert (context
17360                           && (TREE_CODE (context) == FUNCTION_DECL
17361                               || TREE_CODE (context) == NAMESPACE_DECL));
17362
17363               origin = lookup_decl_die (context);
17364               if (origin)
17365                 add_child_die (origin, die);
17366               else
17367                 add_child_die (comp_unit_die, die);
17368             }
17369         }
17370     }
17371
17372   limbo_die_list = NULL;
17373
17374   for (node = deferred_asm_name; node; node = node->next)
17375     {
17376       tree decl = node->created_for;
17377       if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
17378         {
17379           add_AT_string (node->die, DW_AT_MIPS_linkage_name,
17380                          IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
17381           move_linkage_attr (node->die);
17382         }
17383     }
17384
17385   deferred_asm_name = NULL;
17386
17387   /* Walk through the list of incomplete types again, trying once more to
17388      emit full debugging info for them.  */
17389   retry_incomplete_types ();
17390
17391   if (flag_eliminate_unused_debug_types)
17392     prune_unused_types ();
17393
17394   /* Generate separate CUs for each of the include files we've seen.
17395      They will go into limbo_die_list.  */
17396   if (flag_eliminate_dwarf2_dups)
17397     break_out_includes (comp_unit_die);
17398
17399   /* Traverse the DIE's and add add sibling attributes to those DIE's
17400      that have children.  */
17401   add_sibling_attributes (comp_unit_die);
17402   for (node = limbo_die_list; node; node = node->next)
17403     add_sibling_attributes (node->die);
17404
17405   /* Output a terminator label for the .text section.  */
17406   switch_to_section (text_section);
17407   targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
17408   if (flag_reorder_blocks_and_partition)
17409     {
17410       switch_to_section (unlikely_text_section ());
17411       targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
17412     }
17413
17414   /* We can only use the low/high_pc attributes if all of the code was
17415      in .text.  */
17416   if (!have_multiple_function_sections)
17417     {
17418       add_AT_lbl_id (comp_unit_die, DW_AT_low_pc, text_section_label);
17419       add_AT_lbl_id (comp_unit_die, DW_AT_high_pc, text_end_label);
17420     }
17421
17422   else
17423     {
17424       unsigned fde_idx = 0;
17425
17426       /* We need to give .debug_loc and .debug_ranges an appropriate
17427          "base address".  Use zero so that these addresses become
17428          absolute.  Historically, we've emitted the unexpected
17429          DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
17430          Emit both to give time for other tools to adapt.  */
17431       add_AT_addr (comp_unit_die, DW_AT_low_pc, const0_rtx);
17432       add_AT_addr (comp_unit_die, DW_AT_entry_pc, const0_rtx);
17433
17434       add_AT_range_list (comp_unit_die, DW_AT_ranges,
17435                          add_ranges_by_labels (text_section_label,
17436                                                text_end_label));
17437       if (flag_reorder_blocks_and_partition)
17438         add_ranges_by_labels (cold_text_section_label,
17439                               cold_end_label);
17440
17441       for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
17442         {
17443           dw_fde_ref fde = &fde_table[fde_idx];
17444
17445           if (fde->dw_fde_switched_sections)
17446             {
17447               if (!fde->in_std_section)
17448                 add_ranges_by_labels (fde->dw_fde_hot_section_label,
17449                                       fde->dw_fde_hot_section_end_label);
17450               if (!fde->cold_in_std_section)
17451                 add_ranges_by_labels (fde->dw_fde_unlikely_section_label,
17452                                       fde->dw_fde_unlikely_section_end_label);
17453             }
17454           else if (!fde->in_std_section)
17455             add_ranges_by_labels (fde->dw_fde_begin,
17456                                   fde->dw_fde_end);
17457         }
17458
17459       add_ranges (NULL);
17460     }
17461
17462   /* Output location list section if necessary.  */
17463   if (have_location_lists)
17464     {
17465       /* Output the location lists info.  */
17466       switch_to_section (debug_loc_section);
17467       ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
17468                                    DEBUG_LOC_SECTION_LABEL, 0);
17469       ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
17470       output_location_lists (die);
17471     }
17472
17473   if (debug_info_level >= DINFO_LEVEL_NORMAL)
17474     add_AT_lineptr (comp_unit_die, DW_AT_stmt_list,
17475                     debug_line_section_label);
17476
17477   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
17478     add_AT_macptr (comp_unit_die, DW_AT_macro_info, macinfo_section_label);
17479
17480   /* Output all of the compilation units.  We put the main one last so that
17481      the offsets are available to output_pubnames.  */
17482   for (node = limbo_die_list; node; node = node->next)
17483     output_comp_unit (node->die, 0);
17484
17485   /* Output the main compilation unit if non-empty or if .debug_macinfo
17486      has been emitted.  */
17487   output_comp_unit (comp_unit_die, debug_info_level >= DINFO_LEVEL_VERBOSE);
17488
17489   /* Output the abbreviation table.  */
17490   switch_to_section (debug_abbrev_section);
17491   output_abbrev_section ();
17492
17493   /* Output public names table if necessary.  */
17494   if (!VEC_empty (pubname_entry, pubname_table))
17495     {
17496       switch_to_section (debug_pubnames_section);
17497       output_pubnames (pubname_table);
17498     }
17499
17500   /* Output public types table if necessary.  */
17501   /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
17502      It shouldn't hurt to emit it always, since pure DWARF2 consumers
17503      simply won't look for the section.  */
17504   if (!VEC_empty (pubname_entry, pubtype_table))
17505     {
17506       switch_to_section (debug_pubtypes_section);
17507       output_pubnames (pubtype_table);
17508     }
17509
17510   /* Output the address range information.  We only put functions in the arange
17511      table, so don't write it out if we don't have any.  */
17512   if (fde_table_in_use)
17513     {
17514       switch_to_section (debug_aranges_section);
17515       output_aranges ();
17516     }
17517
17518   /* Output ranges section if necessary.  */
17519   if (ranges_table_in_use)
17520     {
17521       switch_to_section (debug_ranges_section);
17522       ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
17523       output_ranges ();
17524     }
17525
17526   /* Output the source line correspondence table.  We must do this
17527      even if there is no line information.  Otherwise, on an empty
17528      translation unit, we will generate a present, but empty,
17529      .debug_info section.  IRIX 6.5 `nm' will then complain when
17530      examining the file.  This is done late so that any filenames
17531      used by the debug_info section are marked as 'used'.  */
17532   if (! DWARF2_ASM_LINE_DEBUG_INFO)
17533     {
17534       switch_to_section (debug_line_section);
17535       output_line_info ();
17536     }
17537
17538   /* Have to end the macro section.  */
17539   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
17540     {
17541       switch_to_section (debug_macinfo_section);
17542       dw2_asm_output_data (1, 0, "End compilation unit");
17543     }
17544
17545   /* If we emitted any DW_FORM_strp form attribute, output the string
17546      table too.  */
17547   if (debug_str_hash)
17548     htab_traverse (debug_str_hash, output_indirect_string, NULL);
17549 }
17550 #else
17551
17552 /* This should never be used, but its address is needed for comparisons.  */
17553 const struct gcc_debug_hooks dwarf2_debug_hooks =
17554 {
17555   0,            /* init */
17556   0,            /* finish */
17557   0,            /* define */
17558   0,            /* undef */
17559   0,            /* start_source_file */
17560   0,            /* end_source_file */
17561   0,            /* begin_block */
17562   0,            /* end_block */
17563   0,            /* ignore_block */
17564   0,            /* source_line */
17565   0,            /* begin_prologue */
17566   0,            /* end_prologue */
17567   0,            /* end_epilogue */
17568   0,            /* begin_function */
17569   0,            /* end_function */
17570   0,            /* function_decl */
17571   0,            /* global_decl */
17572   0,            /* type_decl */
17573   0,            /* imported_module_or_decl */
17574   0,            /* deferred_inline_function */
17575   0,            /* outlining_inline_function */
17576   0,            /* label */
17577   0,            /* handle_pch */
17578   0,            /* var_location */
17579   0,            /* switch_text_section */
17580   0,            /* set_name */
17581   0             /* start_end_main_source_file */
17582 };
17583
17584 #endif /* DWARF2_DEBUGGING_INFO */
17585
17586 #include "gt-dwarf2out.h"