OSDN Git Service

Emit DWARF for template parameters (PR debug/30161)
[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 [BACK] 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 start label
2937    for collect2 the first time around.  */
2938
2939 static void
2940 switch_to_eh_frame_section (bool back)
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
2984       if (!back)
2985         {
2986           label = get_file_function_name ("F");
2987           ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
2988           targetm.asm_out.globalize_label (asm_out_file,
2989                                            IDENTIFIER_POINTER (label));
2990           ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
2991         }
2992     }
2993 }
2994
2995 /* Switch [BACK] to the eh or debug frame table section, depending on
2996    FOR_EH.  */
2997
2998 static void
2999 switch_to_frame_table_section (int for_eh, bool back)
3000 {
3001   if (for_eh)
3002     switch_to_eh_frame_section (back);
3003   else
3004     {
3005       if (!debug_frame_section)
3006         debug_frame_section = get_section (DEBUG_FRAME_SECTION,
3007                                            SECTION_DEBUG, NULL);
3008       switch_to_section (debug_frame_section);
3009     }
3010 }
3011
3012 /* Output a Call Frame Information opcode and its operand(s).  */
3013
3014 static void
3015 output_cfi (dw_cfi_ref cfi, dw_fde_ref fde, int for_eh)
3016 {
3017   unsigned long r;
3018   HOST_WIDE_INT off;
3019
3020   if (cfi->dw_cfi_opc == DW_CFA_advance_loc)
3021     dw2_asm_output_data (1, (cfi->dw_cfi_opc
3022                              | (cfi->dw_cfi_oprnd1.dw_cfi_offset & 0x3f)),
3023                          "DW_CFA_advance_loc " HOST_WIDE_INT_PRINT_HEX,
3024                          ((unsigned HOST_WIDE_INT)
3025                           cfi->dw_cfi_oprnd1.dw_cfi_offset));
3026   else if (cfi->dw_cfi_opc == DW_CFA_offset)
3027     {
3028       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3029       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
3030                            "DW_CFA_offset, column 0x%lx", r);
3031       off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3032       dw2_asm_output_data_uleb128 (off, NULL);
3033     }
3034   else if (cfi->dw_cfi_opc == DW_CFA_restore)
3035     {
3036       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3037       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
3038                            "DW_CFA_restore, column 0x%lx", r);
3039     }
3040   else
3041     {
3042       dw2_asm_output_data (1, cfi->dw_cfi_opc,
3043                            "%s", dwarf_cfi_name (cfi->dw_cfi_opc));
3044
3045       switch (cfi->dw_cfi_opc)
3046         {
3047         case DW_CFA_set_loc:
3048           if (for_eh)
3049             dw2_asm_output_encoded_addr_rtx (
3050                 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0),
3051                 gen_rtx_SYMBOL_REF (Pmode, cfi->dw_cfi_oprnd1.dw_cfi_addr),
3052                 false, NULL);
3053           else
3054             dw2_asm_output_addr (DWARF2_ADDR_SIZE,
3055                                  cfi->dw_cfi_oprnd1.dw_cfi_addr, NULL);
3056           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3057           break;
3058
3059         case DW_CFA_advance_loc1:
3060           dw2_asm_output_delta (1, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3061                                 fde->dw_fde_current_label, NULL);
3062           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3063           break;
3064
3065         case DW_CFA_advance_loc2:
3066           dw2_asm_output_delta (2, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3067                                 fde->dw_fde_current_label, NULL);
3068           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3069           break;
3070
3071         case DW_CFA_advance_loc4:
3072           dw2_asm_output_delta (4, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3073                                 fde->dw_fde_current_label, NULL);
3074           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3075           break;
3076
3077         case DW_CFA_MIPS_advance_loc8:
3078           dw2_asm_output_delta (8, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3079                                 fde->dw_fde_current_label, NULL);
3080           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3081           break;
3082
3083         case DW_CFA_offset_extended:
3084           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3085           dw2_asm_output_data_uleb128 (r, NULL);
3086           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3087           dw2_asm_output_data_uleb128 (off, NULL);
3088           break;
3089
3090         case DW_CFA_def_cfa:
3091           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3092           dw2_asm_output_data_uleb128 (r, NULL);
3093           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
3094           break;
3095
3096         case DW_CFA_offset_extended_sf:
3097           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3098           dw2_asm_output_data_uleb128 (r, NULL);
3099           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3100           dw2_asm_output_data_sleb128 (off, NULL);
3101           break;
3102
3103         case DW_CFA_def_cfa_sf:
3104           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3105           dw2_asm_output_data_uleb128 (r, NULL);
3106           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3107           dw2_asm_output_data_sleb128 (off, NULL);
3108           break;
3109
3110         case DW_CFA_restore_extended:
3111         case DW_CFA_undefined:
3112         case DW_CFA_same_value:
3113         case DW_CFA_def_cfa_register:
3114           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3115           dw2_asm_output_data_uleb128 (r, NULL);
3116           break;
3117
3118         case DW_CFA_register:
3119           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3120           dw2_asm_output_data_uleb128 (r, NULL);
3121           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, for_eh);
3122           dw2_asm_output_data_uleb128 (r, NULL);
3123           break;
3124
3125         case DW_CFA_def_cfa_offset:
3126         case DW_CFA_GNU_args_size:
3127           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
3128           break;
3129
3130         case DW_CFA_def_cfa_offset_sf:
3131           off = div_data_align (cfi->dw_cfi_oprnd1.dw_cfi_offset);
3132           dw2_asm_output_data_sleb128 (off, NULL);
3133           break;
3134
3135         case DW_CFA_GNU_window_save:
3136           break;
3137
3138         case DW_CFA_def_cfa_expression:
3139         case DW_CFA_expression:
3140           output_cfa_loc (cfi);
3141           break;
3142
3143         case DW_CFA_GNU_negative_offset_extended:
3144           /* Obsoleted by DW_CFA_offset_extended_sf.  */
3145           gcc_unreachable ();
3146
3147         default:
3148           break;
3149         }
3150     }
3151 }
3152
3153 /* Similar, but do it via assembler directives instead.  */
3154
3155 static void
3156 output_cfi_directive (dw_cfi_ref cfi)
3157 {
3158   unsigned long r, r2;
3159
3160   switch (cfi->dw_cfi_opc)
3161     {
3162     case DW_CFA_advance_loc:
3163     case DW_CFA_advance_loc1:
3164     case DW_CFA_advance_loc2:
3165     case DW_CFA_advance_loc4:
3166     case DW_CFA_MIPS_advance_loc8:
3167     case DW_CFA_set_loc:
3168       /* Should only be created by add_fde_cfi in a code path not
3169          followed when emitting via directives.  The assembler is
3170          going to take care of this for us.  */
3171       gcc_unreachable ();
3172
3173     case DW_CFA_offset:
3174     case DW_CFA_offset_extended:
3175     case DW_CFA_offset_extended_sf:
3176       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3177       fprintf (asm_out_file, "\t.cfi_offset %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
3178                r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
3179       break;
3180
3181     case DW_CFA_restore:
3182     case DW_CFA_restore_extended:
3183       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3184       fprintf (asm_out_file, "\t.cfi_restore %lu\n", r);
3185       break;
3186
3187     case DW_CFA_undefined:
3188       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3189       fprintf (asm_out_file, "\t.cfi_undefined %lu\n", r);
3190       break;
3191
3192     case DW_CFA_same_value:
3193       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3194       fprintf (asm_out_file, "\t.cfi_same_value %lu\n", r);
3195       break;
3196
3197     case DW_CFA_def_cfa:
3198     case DW_CFA_def_cfa_sf:
3199       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3200       fprintf (asm_out_file, "\t.cfi_def_cfa %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
3201                r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
3202       break;
3203
3204     case DW_CFA_def_cfa_register:
3205       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3206       fprintf (asm_out_file, "\t.cfi_def_cfa_register %lu\n", r);
3207       break;
3208
3209     case DW_CFA_register:
3210       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3211       r2 = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, 1);
3212       fprintf (asm_out_file, "\t.cfi_register %lu, %lu\n", r, r2);
3213       break;
3214
3215     case DW_CFA_def_cfa_offset:
3216     case DW_CFA_def_cfa_offset_sf:
3217       fprintf (asm_out_file, "\t.cfi_def_cfa_offset "
3218                HOST_WIDE_INT_PRINT_DEC"\n",
3219                cfi->dw_cfi_oprnd1.dw_cfi_offset);
3220       break;
3221
3222     case DW_CFA_remember_state:
3223       fprintf (asm_out_file, "\t.cfi_remember_state\n");
3224       break;
3225     case DW_CFA_restore_state:
3226       fprintf (asm_out_file, "\t.cfi_restore_state\n");
3227       break;
3228
3229     case DW_CFA_GNU_args_size:
3230       fprintf (asm_out_file, "\t.cfi_escape 0x%x,", DW_CFA_GNU_args_size);
3231       dw2_asm_output_data_uleb128_raw (cfi->dw_cfi_oprnd1.dw_cfi_offset);
3232       if (flag_debug_asm)
3233         fprintf (asm_out_file, "\t%s args_size "HOST_WIDE_INT_PRINT_DEC,
3234                  ASM_COMMENT_START, cfi->dw_cfi_oprnd1.dw_cfi_offset);
3235       fputc ('\n', asm_out_file);
3236       break;
3237
3238     case DW_CFA_GNU_window_save:
3239       fprintf (asm_out_file, "\t.cfi_window_save\n");
3240       break;
3241
3242     case DW_CFA_def_cfa_expression:
3243     case DW_CFA_expression:
3244       fprintf (asm_out_file, "\t.cfi_escape 0x%x,", cfi->dw_cfi_opc);
3245       output_cfa_loc_raw (cfi);
3246       fputc ('\n', asm_out_file);
3247       break;
3248
3249     default:
3250       gcc_unreachable ();
3251     }
3252 }
3253
3254 DEF_VEC_P (dw_cfi_ref);
3255 DEF_VEC_ALLOC_P (dw_cfi_ref, heap);
3256
3257 /* Output CFIs to bring current FDE to the same state as after executing
3258    CFIs in CFI chain.  DO_CFI_ASM is true if .cfi_* directives shall
3259    be emitted, false otherwise.  If it is false, FDE and FOR_EH are the
3260    other arguments to pass to output_cfi.  */
3261
3262 static void
3263 output_cfis (dw_cfi_ref cfi, bool do_cfi_asm, dw_fde_ref fde, bool for_eh)
3264 {
3265   struct dw_cfi_struct cfi_buf;
3266   dw_cfi_ref cfi2;
3267   dw_cfi_ref cfi_args_size = NULL, cfi_cfa = NULL, cfi_cfa_offset = NULL;
3268   VEC (dw_cfi_ref, heap) *regs = VEC_alloc (dw_cfi_ref, heap, 32);
3269   unsigned int len, idx;
3270
3271   for (;; cfi = cfi->dw_cfi_next)
3272     switch (cfi ? cfi->dw_cfi_opc : DW_CFA_nop)
3273       {
3274       case DW_CFA_advance_loc:
3275       case DW_CFA_advance_loc1:
3276       case DW_CFA_advance_loc2:
3277       case DW_CFA_advance_loc4:
3278       case DW_CFA_MIPS_advance_loc8:
3279       case DW_CFA_set_loc:
3280         /* All advances should be ignored.  */
3281         break;
3282       case DW_CFA_remember_state:
3283         {
3284           dw_cfi_ref args_size = cfi_args_size;
3285
3286           /* Skip everything between .cfi_remember_state and
3287              .cfi_restore_state.  */
3288           for (cfi2 = cfi->dw_cfi_next; cfi2; cfi2 = cfi2->dw_cfi_next)
3289             if (cfi2->dw_cfi_opc == DW_CFA_restore_state)
3290               break;
3291             else if (cfi2->dw_cfi_opc == DW_CFA_GNU_args_size)
3292               args_size = cfi2;
3293             else
3294               gcc_assert (cfi2->dw_cfi_opc != DW_CFA_remember_state);
3295
3296           if (cfi2 == NULL)
3297             goto flush_all;
3298           else
3299             {
3300               cfi = cfi2;
3301               cfi_args_size = args_size;
3302             }
3303           break;
3304         }
3305       case DW_CFA_GNU_args_size:
3306         cfi_args_size = cfi;
3307         break;
3308       case DW_CFA_GNU_window_save:
3309         goto flush_all;
3310       case DW_CFA_offset:
3311       case DW_CFA_offset_extended:
3312       case DW_CFA_offset_extended_sf:
3313       case DW_CFA_restore:
3314       case DW_CFA_restore_extended:
3315       case DW_CFA_undefined:
3316       case DW_CFA_same_value:
3317       case DW_CFA_register:
3318       case DW_CFA_val_offset:
3319       case DW_CFA_val_offset_sf:
3320       case DW_CFA_expression:
3321       case DW_CFA_val_expression:
3322       case DW_CFA_GNU_negative_offset_extended:
3323         if (VEC_length (dw_cfi_ref, regs) <= cfi->dw_cfi_oprnd1.dw_cfi_reg_num)
3324           VEC_safe_grow_cleared (dw_cfi_ref, heap, regs,
3325                                  cfi->dw_cfi_oprnd1.dw_cfi_reg_num + 1);
3326         VEC_replace (dw_cfi_ref, regs, cfi->dw_cfi_oprnd1.dw_cfi_reg_num, cfi);
3327         break;
3328       case DW_CFA_def_cfa:
3329       case DW_CFA_def_cfa_sf:
3330       case DW_CFA_def_cfa_expression:
3331         cfi_cfa = cfi;
3332         cfi_cfa_offset = cfi;
3333         break;
3334       case DW_CFA_def_cfa_register:
3335         cfi_cfa = cfi;
3336         break;
3337       case DW_CFA_def_cfa_offset:
3338       case DW_CFA_def_cfa_offset_sf:
3339         cfi_cfa_offset = cfi;
3340         break;
3341       case DW_CFA_nop:
3342         gcc_assert (cfi == NULL);
3343       flush_all:
3344         len = VEC_length (dw_cfi_ref, regs);
3345         for (idx = 0; idx < len; idx++)
3346           {
3347             cfi2 = VEC_replace (dw_cfi_ref, regs, idx, NULL);
3348             if (cfi2 != NULL
3349                 && cfi2->dw_cfi_opc != DW_CFA_restore
3350                 && cfi2->dw_cfi_opc != DW_CFA_restore_extended)
3351               {
3352                 if (do_cfi_asm)
3353                   output_cfi_directive (cfi2);
3354                 else
3355                   output_cfi (cfi2, fde, for_eh);
3356               }
3357           }
3358         if (cfi_cfa && cfi_cfa_offset && cfi_cfa_offset != cfi_cfa)
3359           {
3360             gcc_assert (cfi_cfa->dw_cfi_opc != DW_CFA_def_cfa_expression);
3361             cfi_buf = *cfi_cfa;
3362             switch (cfi_cfa_offset->dw_cfi_opc)
3363               {
3364               case DW_CFA_def_cfa_offset:
3365                 cfi_buf.dw_cfi_opc = DW_CFA_def_cfa;
3366                 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd1;
3367                 break;
3368               case DW_CFA_def_cfa_offset_sf:
3369                 cfi_buf.dw_cfi_opc = DW_CFA_def_cfa_sf;
3370                 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd1;
3371                 break;
3372               case DW_CFA_def_cfa:
3373               case DW_CFA_def_cfa_sf:
3374                 cfi_buf.dw_cfi_opc = cfi_cfa_offset->dw_cfi_opc;
3375                 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd2;
3376                 break;
3377               default:
3378                 gcc_unreachable ();
3379               }
3380             cfi_cfa = &cfi_buf;
3381           }
3382         else if (cfi_cfa_offset)
3383           cfi_cfa = cfi_cfa_offset;
3384         if (cfi_cfa)
3385           {
3386             if (do_cfi_asm)
3387               output_cfi_directive (cfi_cfa);
3388             else
3389               output_cfi (cfi_cfa, fde, for_eh);
3390           }
3391         cfi_cfa = NULL;
3392         cfi_cfa_offset = NULL;
3393         if (cfi_args_size
3394             && cfi_args_size->dw_cfi_oprnd1.dw_cfi_offset)
3395           {
3396             if (do_cfi_asm)
3397               output_cfi_directive (cfi_args_size);
3398             else
3399               output_cfi (cfi_args_size, fde, for_eh);
3400           }
3401         cfi_args_size = NULL;
3402         if (cfi == NULL)
3403           {
3404             VEC_free (dw_cfi_ref, heap, regs);
3405             return;
3406           }
3407         else if (do_cfi_asm)
3408           output_cfi_directive (cfi);
3409         else
3410           output_cfi (cfi, fde, for_eh);
3411         break;
3412       default:
3413         gcc_unreachable ();
3414     }
3415 }
3416
3417 /* Output one FDE.  */
3418
3419 static void
3420 output_fde (dw_fde_ref fde, bool for_eh, bool second,
3421             char *section_start_label, int fde_encoding, char *augmentation,
3422             bool any_lsda_needed, int lsda_encoding)
3423 {
3424   const char *begin, *end;
3425   static unsigned int j;
3426   char l1[20], l2[20];
3427   dw_cfi_ref cfi;
3428
3429   targetm.asm_out.unwind_label (asm_out_file, fde->decl, for_eh,
3430                                 /* empty */ 0);
3431   targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
3432                                   for_eh + j);
3433   ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
3434   ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
3435   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
3436     dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
3437                          " indicating 64-bit DWARF extension");
3438   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
3439                         "FDE Length");
3440   ASM_OUTPUT_LABEL (asm_out_file, l1);
3441
3442   if (for_eh)
3443     dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
3444   else
3445     dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
3446                            debug_frame_section, "FDE CIE offset");
3447
3448   if (!fde->dw_fde_switched_sections)
3449     {
3450       begin = fde->dw_fde_begin;
3451       end = fde->dw_fde_end;
3452     }
3453   else
3454     {
3455       /* For the first section, prefer dw_fde_begin over
3456          dw_fde_{hot,cold}_section_label, as the latter
3457          might be separated from the real start of the
3458          function by alignment padding.  */
3459       if (!second)
3460         begin = fde->dw_fde_begin;
3461       else if (fde->dw_fde_switched_cold_to_hot)
3462         begin = fde->dw_fde_hot_section_label;
3463       else
3464         begin = fde->dw_fde_unlikely_section_label;
3465       if (second ^ fde->dw_fde_switched_cold_to_hot)
3466         end = fde->dw_fde_unlikely_section_end_label;
3467       else
3468         end = fde->dw_fde_hot_section_end_label;
3469     }
3470
3471   if (for_eh)
3472     {
3473       rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
3474       SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
3475       dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
3476                                        "FDE initial location");
3477       dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
3478                             end, begin, "FDE address range");
3479     }
3480   else
3481     {
3482       dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
3483       dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
3484     }
3485
3486   if (augmentation[0])
3487     {
3488       if (any_lsda_needed)
3489         {
3490           int size = size_of_encoded_value (lsda_encoding);
3491
3492           if (lsda_encoding == DW_EH_PE_aligned)
3493             {
3494               int offset = (  4         /* Length */
3495                             + 4         /* CIE offset */
3496                             + 2 * size_of_encoded_value (fde_encoding)
3497                             + 1         /* Augmentation size */ );
3498               int pad = -offset & (PTR_SIZE - 1);
3499
3500               size += pad;
3501               gcc_assert (size_of_uleb128 (size) == 1);
3502             }
3503
3504           dw2_asm_output_data_uleb128 (size, "Augmentation size");
3505
3506           if (fde->uses_eh_lsda)
3507             {
3508               ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
3509                                            fde->funcdef_number);
3510               dw2_asm_output_encoded_addr_rtx (lsda_encoding,
3511                                                gen_rtx_SYMBOL_REF (Pmode, l1),
3512                                                false,
3513                                                "Language Specific Data Area");
3514             }
3515           else
3516             {
3517               if (lsda_encoding == DW_EH_PE_aligned)
3518                 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
3519               dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
3520                                    "Language Specific Data Area (none)");
3521             }
3522         }
3523       else
3524         dw2_asm_output_data_uleb128 (0, "Augmentation size");
3525     }
3526
3527   /* Loop through the Call Frame Instructions associated with
3528      this FDE.  */
3529   fde->dw_fde_current_label = begin;
3530   if (!fde->dw_fde_switched_sections)
3531     for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
3532       output_cfi (cfi, fde, for_eh);
3533   else if (!second)
3534     {
3535       if (fde->dw_fde_switch_cfi)
3536         for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
3537           {
3538             output_cfi (cfi, fde, for_eh);
3539             if (cfi == fde->dw_fde_switch_cfi)
3540               break;
3541           }
3542     }
3543   else
3544     {
3545       dw_cfi_ref cfi_next = fde->dw_fde_cfi;
3546
3547       if (fde->dw_fde_switch_cfi)
3548         {
3549           cfi_next = fde->dw_fde_switch_cfi->dw_cfi_next;
3550           fde->dw_fde_switch_cfi->dw_cfi_next = NULL;
3551           output_cfis (fde->dw_fde_cfi, false, fde, for_eh);
3552           fde->dw_fde_switch_cfi->dw_cfi_next = cfi_next;
3553         }
3554       for (cfi = cfi_next; cfi != NULL; cfi = cfi->dw_cfi_next)
3555         output_cfi (cfi, fde, for_eh);
3556     }
3557
3558   /* If we are to emit a ref/link from function bodies to their frame tables,
3559      do it now.  This is typically performed to make sure that tables
3560      associated with functions are dragged with them and not discarded in
3561      garbage collecting links. We need to do this on a per function basis to
3562      cope with -ffunction-sections.  */
3563
3564 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
3565   /* Switch to the function section, emit the ref to the tables, and
3566      switch *back* into the table section.  */
3567   switch_to_section (function_section (fde->decl));
3568   ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
3569   switch_to_frame_table_section (for_eh, true);
3570 #endif
3571
3572   /* Pad the FDE out to an address sized boundary.  */
3573   ASM_OUTPUT_ALIGN (asm_out_file,
3574                     floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
3575   ASM_OUTPUT_LABEL (asm_out_file, l2);
3576
3577   j += 2;
3578 }
3579
3580 /* Output the call frame information used to record information
3581    that relates to calculating the frame pointer, and records the
3582    location of saved registers.  */
3583
3584 static void
3585 output_call_frame_info (int for_eh)
3586 {
3587   unsigned int i;
3588   dw_fde_ref fde;
3589   dw_cfi_ref cfi;
3590   char l1[20], l2[20], section_start_label[20];
3591   bool any_lsda_needed = false;
3592   char augmentation[6];
3593   int augmentation_size;
3594   int fde_encoding = DW_EH_PE_absptr;
3595   int per_encoding = DW_EH_PE_absptr;
3596   int lsda_encoding = DW_EH_PE_absptr;
3597   int return_reg;
3598   int dw_cie_version;
3599
3600   /* Don't emit a CIE if there won't be any FDEs.  */
3601   if (fde_table_in_use == 0)
3602     return;
3603
3604   /* Nothing to do if the assembler's doing it all.  */
3605   if (dwarf2out_do_cfi_asm ())
3606     return;
3607
3608   /* If we make FDEs linkonce, we may have to emit an empty label for
3609      an FDE that wouldn't otherwise be emitted.  We want to avoid
3610      having an FDE kept around when the function it refers to is
3611      discarded.  Example where this matters: a primary function
3612      template in C++ requires EH information, but an explicit
3613      specialization doesn't.  */
3614   if (TARGET_USES_WEAK_UNWIND_INFO
3615       && ! flag_asynchronous_unwind_tables
3616       && flag_exceptions
3617       && for_eh)
3618     for (i = 0; i < fde_table_in_use; i++)
3619       if ((fde_table[i].nothrow || fde_table[i].all_throwers_are_sibcalls)
3620           && !fde_table[i].uses_eh_lsda
3621           && ! DECL_WEAK (fde_table[i].decl))
3622         targetm.asm_out.unwind_label (asm_out_file, fde_table[i].decl,
3623                                       for_eh, /* empty */ 1);
3624
3625   /* If we don't have any functions we'll want to unwind out of, don't
3626      emit any EH unwind information.  Note that if exceptions aren't
3627      enabled, we won't have collected nothrow information, and if we
3628      asked for asynchronous tables, we always want this info.  */
3629   if (for_eh)
3630     {
3631       bool any_eh_needed = !flag_exceptions || flag_asynchronous_unwind_tables;
3632
3633       for (i = 0; i < fde_table_in_use; i++)
3634         if (fde_table[i].uses_eh_lsda)
3635           any_eh_needed = any_lsda_needed = true;
3636         else if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
3637           any_eh_needed = true;
3638         else if (! fde_table[i].nothrow
3639                  && ! fde_table[i].all_throwers_are_sibcalls)
3640           any_eh_needed = true;
3641
3642       if (! any_eh_needed)
3643         return;
3644     }
3645
3646   /* We're going to be generating comments, so turn on app.  */
3647   if (flag_debug_asm)
3648     app_enable ();
3649
3650   /* Switch to the proper frame section, first time.  */
3651   switch_to_frame_table_section (for_eh, false);
3652
3653   ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
3654   ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
3655
3656   /* Output the CIE.  */
3657   ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
3658   ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
3659   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
3660     dw2_asm_output_data (4, 0xffffffff,
3661       "Initial length escape value indicating 64-bit DWARF extension");
3662   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
3663                         "Length of Common Information Entry");
3664   ASM_OUTPUT_LABEL (asm_out_file, l1);
3665
3666   /* Now that the CIE pointer is PC-relative for EH,
3667      use 0 to identify the CIE.  */
3668   dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
3669                        (for_eh ? 0 : DWARF_CIE_ID),
3670                        "CIE Identifier Tag");
3671
3672   /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
3673      use CIE version 1, unless that would produce incorrect results
3674      due to overflowing the return register column.  */
3675   return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
3676   dw_cie_version = 1;
3677   if (return_reg >= 256 || dwarf_version > 2)
3678     dw_cie_version = 3;
3679   dw2_asm_output_data (1, dw_cie_version, "CIE Version");
3680
3681   augmentation[0] = 0;
3682   augmentation_size = 0;
3683   if (for_eh)
3684     {
3685       char *p;
3686
3687       /* Augmentation:
3688          z      Indicates that a uleb128 is present to size the
3689                 augmentation section.
3690          L      Indicates the encoding (and thus presence) of
3691                 an LSDA pointer in the FDE augmentation.
3692          R      Indicates a non-default pointer encoding for
3693                 FDE code pointers.
3694          P      Indicates the presence of an encoding + language
3695                 personality routine in the CIE augmentation.  */
3696
3697       fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
3698       per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
3699       lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
3700
3701       p = augmentation + 1;
3702       if (eh_personality_libfunc)
3703         {
3704           *p++ = 'P';
3705           augmentation_size += 1 + size_of_encoded_value (per_encoding);
3706           assemble_external_libcall (eh_personality_libfunc);
3707         }
3708       if (any_lsda_needed)
3709         {
3710           *p++ = 'L';
3711           augmentation_size += 1;
3712         }
3713       if (fde_encoding != DW_EH_PE_absptr)
3714         {
3715           *p++ = 'R';
3716           augmentation_size += 1;
3717         }
3718       if (p > augmentation + 1)
3719         {
3720           augmentation[0] = 'z';
3721           *p = '\0';
3722         }
3723
3724       /* Ug.  Some platforms can't do unaligned dynamic relocations at all.  */
3725       if (eh_personality_libfunc && per_encoding == DW_EH_PE_aligned)
3726         {
3727           int offset = (  4             /* Length */
3728                         + 4             /* CIE Id */
3729                         + 1             /* CIE version */
3730                         + strlen (augmentation) + 1     /* Augmentation */
3731                         + size_of_uleb128 (1)           /* Code alignment */
3732                         + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
3733                         + 1             /* RA column */
3734                         + 1             /* Augmentation size */
3735                         + 1             /* Personality encoding */ );
3736           int pad = -offset & (PTR_SIZE - 1);
3737
3738           augmentation_size += pad;
3739
3740           /* Augmentations should be small, so there's scarce need to
3741              iterate for a solution.  Die if we exceed one uleb128 byte.  */
3742           gcc_assert (size_of_uleb128 (augmentation_size) == 1);
3743         }
3744     }
3745
3746   dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
3747   dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
3748   dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
3749                                "CIE Data Alignment Factor");
3750
3751   if (dw_cie_version == 1)
3752     dw2_asm_output_data (1, return_reg, "CIE RA Column");
3753   else
3754     dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
3755
3756   if (augmentation[0])
3757     {
3758       dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
3759       if (eh_personality_libfunc)
3760         {
3761           dw2_asm_output_data (1, per_encoding, "Personality (%s)",
3762                                eh_data_format_name (per_encoding));
3763           dw2_asm_output_encoded_addr_rtx (per_encoding,
3764                                            eh_personality_libfunc,
3765                                            true, NULL);
3766         }
3767
3768       if (any_lsda_needed)
3769         dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
3770                              eh_data_format_name (lsda_encoding));
3771
3772       if (fde_encoding != DW_EH_PE_absptr)
3773         dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
3774                              eh_data_format_name (fde_encoding));
3775     }
3776
3777   for (cfi = cie_cfi_head; cfi != NULL; cfi = cfi->dw_cfi_next)
3778     output_cfi (cfi, NULL, for_eh);
3779
3780   /* Pad the CIE out to an address sized boundary.  */
3781   ASM_OUTPUT_ALIGN (asm_out_file,
3782                     floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
3783   ASM_OUTPUT_LABEL (asm_out_file, l2);
3784
3785   /* Loop through all of the FDE's.  */
3786   for (i = 0; i < fde_table_in_use; i++)
3787     {
3788       unsigned int k;
3789       fde = &fde_table[i];
3790
3791       /* Don't emit EH unwind info for leaf functions that don't need it.  */
3792       if (for_eh && !flag_asynchronous_unwind_tables && flag_exceptions
3793           && (fde->nothrow || fde->all_throwers_are_sibcalls)
3794           && ! (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
3795           && !fde->uses_eh_lsda)
3796         continue;
3797
3798       for (k = 0; k < (fde->dw_fde_switched_sections ? 2 : 1); k++)
3799         output_fde (fde, for_eh, k, section_start_label, fde_encoding,
3800                     augmentation, any_lsda_needed, lsda_encoding);
3801     }
3802
3803   if (for_eh && targetm.terminate_dw2_eh_frame_info)
3804     dw2_asm_output_data (4, 0, "End of Table");
3805 #ifdef MIPS_DEBUGGING_INFO
3806   /* Work around Irix 6 assembler bug whereby labels at the end of a section
3807      get a value of 0.  Putting .align 0 after the label fixes it.  */
3808   ASM_OUTPUT_ALIGN (asm_out_file, 0);
3809 #endif
3810
3811   /* Turn off app to make assembly quicker.  */
3812   if (flag_debug_asm)
3813     app_disable ();
3814 }
3815
3816 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed.  */
3817
3818 static void
3819 dwarf2out_do_cfi_startproc (bool second)
3820 {
3821   int enc;
3822   rtx ref;
3823
3824   fprintf (asm_out_file, "\t.cfi_startproc\n");
3825
3826   if (eh_personality_libfunc)
3827     {
3828       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
3829       ref = eh_personality_libfunc;
3830
3831       /* ??? The GAS support isn't entirely consistent.  We have to
3832          handle indirect support ourselves, but PC-relative is done
3833          in the assembler.  Further, the assembler can't handle any
3834          of the weirder relocation types.  */
3835       if (enc & DW_EH_PE_indirect)
3836         ref = dw2_force_const_mem (ref, true);
3837
3838       fprintf (asm_out_file, "\t.cfi_personality 0x%x,", enc);
3839       output_addr_const (asm_out_file, ref);
3840       fputc ('\n', asm_out_file);
3841     }
3842
3843   if (crtl->uses_eh_lsda)
3844     {
3845       char lab[20];
3846
3847       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
3848       ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
3849                                    current_function_funcdef_no);
3850       ref = gen_rtx_SYMBOL_REF (Pmode, lab);
3851       SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
3852
3853       if (enc & DW_EH_PE_indirect)
3854         ref = dw2_force_const_mem (ref, true);
3855
3856       fprintf (asm_out_file, "\t.cfi_lsda 0x%x,", enc);
3857       output_addr_const (asm_out_file, ref);
3858       fputc ('\n', asm_out_file);
3859     }
3860 }
3861
3862 /* Output a marker (i.e. a label) for the beginning of a function, before
3863    the prologue.  */
3864
3865 void
3866 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
3867                           const char *file ATTRIBUTE_UNUSED)
3868 {
3869   char label[MAX_ARTIFICIAL_LABEL_BYTES];
3870   char * dup_label;
3871   dw_fde_ref fde;
3872   section *fnsec;
3873
3874   current_function_func_begin_label = NULL;
3875
3876 #ifdef TARGET_UNWIND_INFO
3877   /* ??? current_function_func_begin_label is also used by except.c
3878      for call-site information.  We must emit this label if it might
3879      be used.  */
3880   if ((! flag_exceptions || USING_SJLJ_EXCEPTIONS)
3881       && ! dwarf2out_do_frame ())
3882     return;
3883 #else
3884   if (! dwarf2out_do_frame ())
3885     return;
3886 #endif
3887
3888   fnsec = function_section (current_function_decl);
3889   switch_to_section (fnsec);
3890   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
3891                                current_function_funcdef_no);
3892   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
3893                           current_function_funcdef_no);
3894   dup_label = xstrdup (label);
3895   current_function_func_begin_label = dup_label;
3896
3897 #ifdef TARGET_UNWIND_INFO
3898   /* We can elide the fde allocation if we're not emitting debug info.  */
3899   if (! dwarf2out_do_frame ())
3900     return;
3901 #endif
3902
3903   /* Expand the fde table if necessary.  */
3904   if (fde_table_in_use == fde_table_allocated)
3905     {
3906       fde_table_allocated += FDE_TABLE_INCREMENT;
3907       fde_table = GGC_RESIZEVEC (dw_fde_node, fde_table, fde_table_allocated);
3908       memset (fde_table + fde_table_in_use, 0,
3909               FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
3910     }
3911
3912   /* Record the FDE associated with this function.  */
3913   current_funcdef_fde = fde_table_in_use;
3914
3915   /* Add the new FDE at the end of the fde_table.  */
3916   fde = &fde_table[fde_table_in_use++];
3917   fde->decl = current_function_decl;
3918   fde->dw_fde_begin = dup_label;
3919   fde->dw_fde_current_label = dup_label;
3920   fde->dw_fde_hot_section_label = NULL;
3921   fde->dw_fde_hot_section_end_label = NULL;
3922   fde->dw_fde_unlikely_section_label = NULL;
3923   fde->dw_fde_unlikely_section_end_label = NULL;
3924   fde->dw_fde_switched_sections = 0;
3925   fde->dw_fde_switched_cold_to_hot = 0;
3926   fde->dw_fde_end = NULL;
3927   fde->dw_fde_cfi = NULL;
3928   fde->dw_fde_switch_cfi = NULL;
3929   fde->funcdef_number = current_function_funcdef_no;
3930   fde->nothrow = crtl->nothrow;
3931   fde->uses_eh_lsda = crtl->uses_eh_lsda;
3932   fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
3933   fde->drap_reg = INVALID_REGNUM;
3934   fde->vdrap_reg = INVALID_REGNUM;
3935   if (flag_reorder_blocks_and_partition)
3936     {
3937       section *unlikelysec;
3938       if (first_function_block_is_cold)
3939         fde->in_std_section = 1;
3940       else
3941         fde->in_std_section
3942           = (fnsec == text_section
3943              || (cold_text_section && fnsec == cold_text_section));
3944       unlikelysec = unlikely_text_section ();
3945       fde->cold_in_std_section
3946         = (unlikelysec == text_section
3947            || (cold_text_section && unlikelysec == cold_text_section));
3948     }
3949   else
3950     {
3951       fde->in_std_section
3952         = (fnsec == text_section
3953            || (cold_text_section && fnsec == cold_text_section));
3954       fde->cold_in_std_section = 0;
3955     }
3956
3957   args_size = old_args_size = 0;
3958
3959   /* We only want to output line number information for the genuine dwarf2
3960      prologue case, not the eh frame case.  */
3961 #ifdef DWARF2_DEBUGGING_INFO
3962   if (file)
3963     dwarf2out_source_line (line, file, 0, true);
3964 #endif
3965
3966   if (dwarf2out_do_cfi_asm ())
3967     dwarf2out_do_cfi_startproc (false);
3968 }
3969
3970 /* Output a marker (i.e. a label) for the absolute end of the generated code
3971    for a function definition.  This gets called *after* the epilogue code has
3972    been generated.  */
3973
3974 void
3975 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
3976                         const char *file ATTRIBUTE_UNUSED)
3977 {
3978   dw_fde_ref fde;
3979   char label[MAX_ARTIFICIAL_LABEL_BYTES];
3980
3981 #ifdef DWARF2_DEBUGGING_INFO
3982   last_var_location_insn = NULL_RTX;
3983 #endif
3984
3985   if (dwarf2out_do_cfi_asm ())
3986     fprintf (asm_out_file, "\t.cfi_endproc\n");
3987
3988   /* Output a label to mark the endpoint of the code generated for this
3989      function.  */
3990   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
3991                                current_function_funcdef_no);
3992   ASM_OUTPUT_LABEL (asm_out_file, label);
3993   fde = current_fde ();
3994   gcc_assert (fde != NULL);
3995   fde->dw_fde_end = xstrdup (label);
3996 }
3997
3998 void
3999 dwarf2out_frame_init (void)
4000 {
4001   /* Allocate the initial hunk of the fde_table.  */
4002   fde_table = GGC_CNEWVEC (dw_fde_node, FDE_TABLE_INCREMENT);
4003   fde_table_allocated = FDE_TABLE_INCREMENT;
4004   fde_table_in_use = 0;
4005
4006   /* Generate the CFA instructions common to all FDE's.  Do it now for the
4007      sake of lookup_cfa.  */
4008
4009   /* On entry, the Canonical Frame Address is at SP.  */
4010   dwarf2out_def_cfa (NULL, STACK_POINTER_REGNUM, INCOMING_FRAME_SP_OFFSET);
4011
4012 #ifdef DWARF2_UNWIND_INFO
4013   if (DWARF2_UNWIND_INFO || DWARF2_FRAME_INFO)
4014     initial_return_save (INCOMING_RETURN_ADDR_RTX);
4015 #endif
4016 }
4017
4018 void
4019 dwarf2out_frame_finish (void)
4020 {
4021   /* Output call frame information.  */
4022   if (DWARF2_FRAME_INFO)
4023     output_call_frame_info (0);
4024
4025 #ifndef TARGET_UNWIND_INFO
4026   /* Output another copy for the unwinder.  */
4027   if (! USING_SJLJ_EXCEPTIONS && (flag_unwind_tables || flag_exceptions))
4028     output_call_frame_info (1);
4029 #endif
4030 }
4031
4032 /* Note that the current function section is being used for code.  */
4033
4034 static void
4035 dwarf2out_note_section_used (void)
4036 {
4037   section *sec = current_function_section ();
4038   if (sec == text_section)
4039     text_section_used = true;
4040   else if (sec == cold_text_section)
4041     cold_text_section_used = true;
4042 }
4043
4044 void
4045 dwarf2out_switch_text_section (void)
4046 {
4047   dw_fde_ref fde = current_fde ();
4048
4049   gcc_assert (cfun && fde && !fde->dw_fde_switched_sections);
4050
4051   fde->dw_fde_switched_sections = 1;
4052   fde->dw_fde_switched_cold_to_hot = !in_cold_section_p;
4053
4054   fde->dw_fde_hot_section_label = crtl->subsections.hot_section_label;
4055   fde->dw_fde_hot_section_end_label = crtl->subsections.hot_section_end_label;
4056   fde->dw_fde_unlikely_section_label = crtl->subsections.cold_section_label;
4057   fde->dw_fde_unlikely_section_end_label = crtl->subsections.cold_section_end_label;
4058   have_multiple_function_sections = true;
4059
4060   /* Reset the current label on switching text sections, so that we
4061      don't attempt to advance_loc4 between labels in different sections.  */
4062   fde->dw_fde_current_label = NULL;
4063
4064   /* There is no need to mark used sections when not debugging.  */
4065   if (cold_text_section != NULL)
4066     dwarf2out_note_section_used ();
4067
4068   if (dwarf2out_do_cfi_asm ())
4069     fprintf (asm_out_file, "\t.cfi_endproc\n");
4070
4071   /* Now do the real section switch.  */
4072   switch_to_section (current_function_section ());
4073
4074   if (dwarf2out_do_cfi_asm ())
4075     {
4076       dwarf2out_do_cfi_startproc (true);
4077       /* As this is a different FDE, insert all current CFI instructions
4078          again.  */
4079       output_cfis (fde->dw_fde_cfi, true, fde, true);
4080     }
4081   else
4082     {
4083       dw_cfi_ref cfi = fde->dw_fde_cfi;
4084
4085       cfi = fde->dw_fde_cfi;
4086       if (cfi)
4087         while (cfi->dw_cfi_next != NULL)
4088           cfi = cfi->dw_cfi_next;
4089       fde->dw_fde_switch_cfi = cfi;
4090     }
4091 }
4092 #endif
4093 \f
4094 /* And now, the subset of the debugging information support code necessary
4095    for emitting location expressions.  */
4096
4097 /* Data about a single source file.  */
4098 struct GTY(()) dwarf_file_data {
4099   const char * filename;
4100   int emitted_number;
4101 };
4102
4103 typedef struct dw_val_struct *dw_val_ref;
4104 typedef struct die_struct *dw_die_ref;
4105 typedef const struct die_struct *const_dw_die_ref;
4106 typedef struct dw_loc_descr_struct *dw_loc_descr_ref;
4107 typedef struct dw_loc_list_struct *dw_loc_list_ref;
4108
4109 typedef struct GTY(()) deferred_locations_struct
4110 {
4111   tree variable;
4112   dw_die_ref die;
4113 } deferred_locations;
4114
4115 DEF_VEC_O(deferred_locations);
4116 DEF_VEC_ALLOC_O(deferred_locations,gc);
4117
4118 static GTY(()) VEC(deferred_locations, gc) *deferred_locations_list;
4119
4120 /* Each DIE may have a series of attribute/value pairs.  Values
4121    can take on several forms.  The forms that are used in this
4122    implementation are listed below.  */
4123
4124 enum dw_val_class
4125 {
4126   dw_val_class_addr,
4127   dw_val_class_offset,
4128   dw_val_class_loc,
4129   dw_val_class_loc_list,
4130   dw_val_class_range_list,
4131   dw_val_class_const,
4132   dw_val_class_unsigned_const,
4133   dw_val_class_long_long,
4134   dw_val_class_vec,
4135   dw_val_class_flag,
4136   dw_val_class_die_ref,
4137   dw_val_class_fde_ref,
4138   dw_val_class_lbl_id,
4139   dw_val_class_lineptr,
4140   dw_val_class_str,
4141   dw_val_class_macptr,
4142   dw_val_class_file
4143 };
4144
4145 /* Describe a double word constant value.  */
4146 /* ??? Every instance of long_long in the code really means CONST_DOUBLE.  */
4147
4148 typedef struct GTY(()) dw_long_long_struct {
4149   unsigned long hi;
4150   unsigned long low;
4151 }
4152 dw_long_long_const;
4153
4154 /* Describe a floating point constant value, or a vector constant value.  */
4155
4156 typedef struct GTY(()) dw_vec_struct {
4157   unsigned char * GTY((length ("%h.length"))) array;
4158   unsigned length;
4159   unsigned elt_size;
4160 }
4161 dw_vec_const;
4162
4163 /* The dw_val_node describes an attribute's value, as it is
4164    represented internally.  */
4165
4166 typedef struct GTY(()) dw_val_struct {
4167   enum dw_val_class val_class;
4168   union dw_val_struct_union
4169     {
4170       rtx GTY ((tag ("dw_val_class_addr"))) val_addr;
4171       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_offset"))) val_offset;
4172       dw_loc_list_ref GTY ((tag ("dw_val_class_loc_list"))) val_loc_list;
4173       dw_loc_descr_ref GTY ((tag ("dw_val_class_loc"))) val_loc;
4174       HOST_WIDE_INT GTY ((default)) val_int;
4175       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_unsigned_const"))) val_unsigned;
4176       dw_long_long_const GTY ((tag ("dw_val_class_long_long"))) val_long_long;
4177       dw_vec_const GTY ((tag ("dw_val_class_vec"))) val_vec;
4178       struct dw_val_die_union
4179         {
4180           dw_die_ref die;
4181           int external;
4182         } GTY ((tag ("dw_val_class_die_ref"))) val_die_ref;
4183       unsigned GTY ((tag ("dw_val_class_fde_ref"))) val_fde_index;
4184       struct indirect_string_node * GTY ((tag ("dw_val_class_str"))) val_str;
4185       char * GTY ((tag ("dw_val_class_lbl_id"))) val_lbl_id;
4186       unsigned char GTY ((tag ("dw_val_class_flag"))) val_flag;
4187       struct dwarf_file_data * GTY ((tag ("dw_val_class_file"))) val_file;
4188     }
4189   GTY ((desc ("%1.val_class"))) v;
4190 }
4191 dw_val_node;
4192
4193 /* Locations in memory are described using a sequence of stack machine
4194    operations.  */
4195
4196 typedef struct GTY(()) dw_loc_descr_struct {
4197   dw_loc_descr_ref dw_loc_next;
4198   ENUM_BITFIELD (dwarf_location_atom) dw_loc_opc : 8;
4199   /* Used to distinguish DW_OP_addr with a direct symbol relocation
4200      from DW_OP_addr with a dtp-relative symbol relocation.  */
4201   unsigned int dtprel : 1;
4202   int dw_loc_addr;
4203   dw_val_node dw_loc_oprnd1;
4204   dw_val_node dw_loc_oprnd2;
4205 }
4206 dw_loc_descr_node;
4207
4208 /* Location lists are ranges + location descriptions for that range,
4209    so you can track variables that are in different places over
4210    their entire life.  */
4211 typedef struct GTY(()) dw_loc_list_struct {
4212   dw_loc_list_ref dw_loc_next;
4213   const char *begin; /* Label for begin address of range */
4214   const char *end;  /* Label for end address of range */
4215   char *ll_symbol; /* Label for beginning of location list.
4216                       Only on head of list */
4217   const char *section; /* Section this loclist is relative to */
4218   dw_loc_descr_ref expr;
4219 } dw_loc_list_node;
4220
4221 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
4222
4223 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
4224
4225 /* Convert a DWARF stack opcode into its string name.  */
4226
4227 static const char *
4228 dwarf_stack_op_name (unsigned int op)
4229 {
4230   switch (op)
4231     {
4232     case DW_OP_addr:
4233       return "DW_OP_addr";
4234     case DW_OP_deref:
4235       return "DW_OP_deref";
4236     case DW_OP_const1u:
4237       return "DW_OP_const1u";
4238     case DW_OP_const1s:
4239       return "DW_OP_const1s";
4240     case DW_OP_const2u:
4241       return "DW_OP_const2u";
4242     case DW_OP_const2s:
4243       return "DW_OP_const2s";
4244     case DW_OP_const4u:
4245       return "DW_OP_const4u";
4246     case DW_OP_const4s:
4247       return "DW_OP_const4s";
4248     case DW_OP_const8u:
4249       return "DW_OP_const8u";
4250     case DW_OP_const8s:
4251       return "DW_OP_const8s";
4252     case DW_OP_constu:
4253       return "DW_OP_constu";
4254     case DW_OP_consts:
4255       return "DW_OP_consts";
4256     case DW_OP_dup:
4257       return "DW_OP_dup";
4258     case DW_OP_drop:
4259       return "DW_OP_drop";
4260     case DW_OP_over:
4261       return "DW_OP_over";
4262     case DW_OP_pick:
4263       return "DW_OP_pick";
4264     case DW_OP_swap:
4265       return "DW_OP_swap";
4266     case DW_OP_rot:
4267       return "DW_OP_rot";
4268     case DW_OP_xderef:
4269       return "DW_OP_xderef";
4270     case DW_OP_abs:
4271       return "DW_OP_abs";
4272     case DW_OP_and:
4273       return "DW_OP_and";
4274     case DW_OP_div:
4275       return "DW_OP_div";
4276     case DW_OP_minus:
4277       return "DW_OP_minus";
4278     case DW_OP_mod:
4279       return "DW_OP_mod";
4280     case DW_OP_mul:
4281       return "DW_OP_mul";
4282     case DW_OP_neg:
4283       return "DW_OP_neg";
4284     case DW_OP_not:
4285       return "DW_OP_not";
4286     case DW_OP_or:
4287       return "DW_OP_or";
4288     case DW_OP_plus:
4289       return "DW_OP_plus";
4290     case DW_OP_plus_uconst:
4291       return "DW_OP_plus_uconst";
4292     case DW_OP_shl:
4293       return "DW_OP_shl";
4294     case DW_OP_shr:
4295       return "DW_OP_shr";
4296     case DW_OP_shra:
4297       return "DW_OP_shra";
4298     case DW_OP_xor:
4299       return "DW_OP_xor";
4300     case DW_OP_bra:
4301       return "DW_OP_bra";
4302     case DW_OP_eq:
4303       return "DW_OP_eq";
4304     case DW_OP_ge:
4305       return "DW_OP_ge";
4306     case DW_OP_gt:
4307       return "DW_OP_gt";
4308     case DW_OP_le:
4309       return "DW_OP_le";
4310     case DW_OP_lt:
4311       return "DW_OP_lt";
4312     case DW_OP_ne:
4313       return "DW_OP_ne";
4314     case DW_OP_skip:
4315       return "DW_OP_skip";
4316     case DW_OP_lit0:
4317       return "DW_OP_lit0";
4318     case DW_OP_lit1:
4319       return "DW_OP_lit1";
4320     case DW_OP_lit2:
4321       return "DW_OP_lit2";
4322     case DW_OP_lit3:
4323       return "DW_OP_lit3";
4324     case DW_OP_lit4:
4325       return "DW_OP_lit4";
4326     case DW_OP_lit5:
4327       return "DW_OP_lit5";
4328     case DW_OP_lit6:
4329       return "DW_OP_lit6";
4330     case DW_OP_lit7:
4331       return "DW_OP_lit7";
4332     case DW_OP_lit8:
4333       return "DW_OP_lit8";
4334     case DW_OP_lit9:
4335       return "DW_OP_lit9";
4336     case DW_OP_lit10:
4337       return "DW_OP_lit10";
4338     case DW_OP_lit11:
4339       return "DW_OP_lit11";
4340     case DW_OP_lit12:
4341       return "DW_OP_lit12";
4342     case DW_OP_lit13:
4343       return "DW_OP_lit13";
4344     case DW_OP_lit14:
4345       return "DW_OP_lit14";
4346     case DW_OP_lit15:
4347       return "DW_OP_lit15";
4348     case DW_OP_lit16:
4349       return "DW_OP_lit16";
4350     case DW_OP_lit17:
4351       return "DW_OP_lit17";
4352     case DW_OP_lit18:
4353       return "DW_OP_lit18";
4354     case DW_OP_lit19:
4355       return "DW_OP_lit19";
4356     case DW_OP_lit20:
4357       return "DW_OP_lit20";
4358     case DW_OP_lit21:
4359       return "DW_OP_lit21";
4360     case DW_OP_lit22:
4361       return "DW_OP_lit22";
4362     case DW_OP_lit23:
4363       return "DW_OP_lit23";
4364     case DW_OP_lit24:
4365       return "DW_OP_lit24";
4366     case DW_OP_lit25:
4367       return "DW_OP_lit25";
4368     case DW_OP_lit26:
4369       return "DW_OP_lit26";
4370     case DW_OP_lit27:
4371       return "DW_OP_lit27";
4372     case DW_OP_lit28:
4373       return "DW_OP_lit28";
4374     case DW_OP_lit29:
4375       return "DW_OP_lit29";
4376     case DW_OP_lit30:
4377       return "DW_OP_lit30";
4378     case DW_OP_lit31:
4379       return "DW_OP_lit31";
4380     case DW_OP_reg0:
4381       return "DW_OP_reg0";
4382     case DW_OP_reg1:
4383       return "DW_OP_reg1";
4384     case DW_OP_reg2:
4385       return "DW_OP_reg2";
4386     case DW_OP_reg3:
4387       return "DW_OP_reg3";
4388     case DW_OP_reg4:
4389       return "DW_OP_reg4";
4390     case DW_OP_reg5:
4391       return "DW_OP_reg5";
4392     case DW_OP_reg6:
4393       return "DW_OP_reg6";
4394     case DW_OP_reg7:
4395       return "DW_OP_reg7";
4396     case DW_OP_reg8:
4397       return "DW_OP_reg8";
4398     case DW_OP_reg9:
4399       return "DW_OP_reg9";
4400     case DW_OP_reg10:
4401       return "DW_OP_reg10";
4402     case DW_OP_reg11:
4403       return "DW_OP_reg11";
4404     case DW_OP_reg12:
4405       return "DW_OP_reg12";
4406     case DW_OP_reg13:
4407       return "DW_OP_reg13";
4408     case DW_OP_reg14:
4409       return "DW_OP_reg14";
4410     case DW_OP_reg15:
4411       return "DW_OP_reg15";
4412     case DW_OP_reg16:
4413       return "DW_OP_reg16";
4414     case DW_OP_reg17:
4415       return "DW_OP_reg17";
4416     case DW_OP_reg18:
4417       return "DW_OP_reg18";
4418     case DW_OP_reg19:
4419       return "DW_OP_reg19";
4420     case DW_OP_reg20:
4421       return "DW_OP_reg20";
4422     case DW_OP_reg21:
4423       return "DW_OP_reg21";
4424     case DW_OP_reg22:
4425       return "DW_OP_reg22";
4426     case DW_OP_reg23:
4427       return "DW_OP_reg23";
4428     case DW_OP_reg24:
4429       return "DW_OP_reg24";
4430     case DW_OP_reg25:
4431       return "DW_OP_reg25";
4432     case DW_OP_reg26:
4433       return "DW_OP_reg26";
4434     case DW_OP_reg27:
4435       return "DW_OP_reg27";
4436     case DW_OP_reg28:
4437       return "DW_OP_reg28";
4438     case DW_OP_reg29:
4439       return "DW_OP_reg29";
4440     case DW_OP_reg30:
4441       return "DW_OP_reg30";
4442     case DW_OP_reg31:
4443       return "DW_OP_reg31";
4444     case DW_OP_breg0:
4445       return "DW_OP_breg0";
4446     case DW_OP_breg1:
4447       return "DW_OP_breg1";
4448     case DW_OP_breg2:
4449       return "DW_OP_breg2";
4450     case DW_OP_breg3:
4451       return "DW_OP_breg3";
4452     case DW_OP_breg4:
4453       return "DW_OP_breg4";
4454     case DW_OP_breg5:
4455       return "DW_OP_breg5";
4456     case DW_OP_breg6:
4457       return "DW_OP_breg6";
4458     case DW_OP_breg7:
4459       return "DW_OP_breg7";
4460     case DW_OP_breg8:
4461       return "DW_OP_breg8";
4462     case DW_OP_breg9:
4463       return "DW_OP_breg9";
4464     case DW_OP_breg10:
4465       return "DW_OP_breg10";
4466     case DW_OP_breg11:
4467       return "DW_OP_breg11";
4468     case DW_OP_breg12:
4469       return "DW_OP_breg12";
4470     case DW_OP_breg13:
4471       return "DW_OP_breg13";
4472     case DW_OP_breg14:
4473       return "DW_OP_breg14";
4474     case DW_OP_breg15:
4475       return "DW_OP_breg15";
4476     case DW_OP_breg16:
4477       return "DW_OP_breg16";
4478     case DW_OP_breg17:
4479       return "DW_OP_breg17";
4480     case DW_OP_breg18:
4481       return "DW_OP_breg18";
4482     case DW_OP_breg19:
4483       return "DW_OP_breg19";
4484     case DW_OP_breg20:
4485       return "DW_OP_breg20";
4486     case DW_OP_breg21:
4487       return "DW_OP_breg21";
4488     case DW_OP_breg22:
4489       return "DW_OP_breg22";
4490     case DW_OP_breg23:
4491       return "DW_OP_breg23";
4492     case DW_OP_breg24:
4493       return "DW_OP_breg24";
4494     case DW_OP_breg25:
4495       return "DW_OP_breg25";
4496     case DW_OP_breg26:
4497       return "DW_OP_breg26";
4498     case DW_OP_breg27:
4499       return "DW_OP_breg27";
4500     case DW_OP_breg28:
4501       return "DW_OP_breg28";
4502     case DW_OP_breg29:
4503       return "DW_OP_breg29";
4504     case DW_OP_breg30:
4505       return "DW_OP_breg30";
4506     case DW_OP_breg31:
4507       return "DW_OP_breg31";
4508     case DW_OP_regx:
4509       return "DW_OP_regx";
4510     case DW_OP_fbreg:
4511       return "DW_OP_fbreg";
4512     case DW_OP_bregx:
4513       return "DW_OP_bregx";
4514     case DW_OP_piece:
4515       return "DW_OP_piece";
4516     case DW_OP_deref_size:
4517       return "DW_OP_deref_size";
4518     case DW_OP_xderef_size:
4519       return "DW_OP_xderef_size";
4520     case DW_OP_nop:
4521       return "DW_OP_nop";
4522
4523     case DW_OP_push_object_address:
4524       return "DW_OP_push_object_address";
4525     case DW_OP_call2:
4526       return "DW_OP_call2";
4527     case DW_OP_call4:
4528       return "DW_OP_call4";
4529     case DW_OP_call_ref:
4530       return "DW_OP_call_ref";
4531     case DW_OP_form_tls_address:
4532       return "DW_OP_form_tls_address";
4533     case DW_OP_call_frame_cfa:
4534       return "DW_OP_call_frame_cfa";
4535     case DW_OP_bit_piece:
4536       return "DW_OP_bit_piece";
4537
4538     case DW_OP_GNU_push_tls_address:
4539       return "DW_OP_GNU_push_tls_address";
4540     case DW_OP_GNU_uninit:
4541       return "DW_OP_GNU_uninit";
4542     case DW_OP_GNU_encoded_addr:
4543       return "DW_OP_GNU_encoded_addr";
4544
4545     default:
4546       return "OP_<unknown>";
4547     }
4548 }
4549
4550 /* Return a pointer to a newly allocated location description.  Location
4551    descriptions are simple expression terms that can be strung
4552    together to form more complicated location (address) descriptions.  */
4553
4554 static inline dw_loc_descr_ref
4555 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
4556                unsigned HOST_WIDE_INT oprnd2)
4557 {
4558   dw_loc_descr_ref descr = GGC_CNEW (dw_loc_descr_node);
4559
4560   descr->dw_loc_opc = op;
4561   descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
4562   descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
4563   descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
4564   descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
4565
4566   return descr;
4567 }
4568
4569 /* Return a pointer to a newly allocated location description for
4570    REG and OFFSET.  */
4571
4572 static inline dw_loc_descr_ref
4573 new_reg_loc_descr (unsigned int reg,  unsigned HOST_WIDE_INT offset)
4574 {
4575   if (reg <= 31)
4576     return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
4577                           offset, 0);
4578   else
4579     return new_loc_descr (DW_OP_bregx, reg, offset);
4580 }
4581
4582 /* Add a location description term to a location description expression.  */
4583
4584 static inline void
4585 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
4586 {
4587   dw_loc_descr_ref *d;
4588
4589   /* Find the end of the chain.  */
4590   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
4591     ;
4592
4593   *d = descr;
4594 }
4595
4596 /* Add a constant OFFSET to a location expression.  */
4597
4598 static void
4599 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
4600 {
4601   dw_loc_descr_ref loc;
4602   HOST_WIDE_INT *p;
4603
4604   gcc_assert (*list_head != NULL);
4605
4606   if (!offset)
4607     return;
4608
4609   /* Find the end of the chain.  */
4610   for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
4611     ;
4612
4613   p = NULL;
4614   if (loc->dw_loc_opc == DW_OP_fbreg
4615       || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
4616     p = &loc->dw_loc_oprnd1.v.val_int;
4617   else if (loc->dw_loc_opc == DW_OP_bregx)
4618     p = &loc->dw_loc_oprnd2.v.val_int;
4619
4620   /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
4621      offset.  Don't optimize if an signed integer overflow would happen.  */
4622   if (p != NULL
4623       && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
4624           || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
4625     *p += offset;
4626
4627   else if (offset > 0)
4628     loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
4629
4630   else
4631     {
4632       loc->dw_loc_next = int_loc_descriptor (offset);
4633       add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_plus, 0, 0));
4634     }
4635 }
4636
4637 /* Return the size of a location descriptor.  */
4638
4639 static unsigned long
4640 size_of_loc_descr (dw_loc_descr_ref loc)
4641 {
4642   unsigned long size = 1;
4643
4644   switch (loc->dw_loc_opc)
4645     {
4646     case DW_OP_addr:
4647       size += DWARF2_ADDR_SIZE;
4648       break;
4649     case DW_OP_const1u:
4650     case DW_OP_const1s:
4651       size += 1;
4652       break;
4653     case DW_OP_const2u:
4654     case DW_OP_const2s:
4655       size += 2;
4656       break;
4657     case DW_OP_const4u:
4658     case DW_OP_const4s:
4659       size += 4;
4660       break;
4661     case DW_OP_const8u:
4662     case DW_OP_const8s:
4663       size += 8;
4664       break;
4665     case DW_OP_constu:
4666       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4667       break;
4668     case DW_OP_consts:
4669       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4670       break;
4671     case DW_OP_pick:
4672       size += 1;
4673       break;
4674     case DW_OP_plus_uconst:
4675       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4676       break;
4677     case DW_OP_skip:
4678     case DW_OP_bra:
4679       size += 2;
4680       break;
4681     case DW_OP_breg0:
4682     case DW_OP_breg1:
4683     case DW_OP_breg2:
4684     case DW_OP_breg3:
4685     case DW_OP_breg4:
4686     case DW_OP_breg5:
4687     case DW_OP_breg6:
4688     case DW_OP_breg7:
4689     case DW_OP_breg8:
4690     case DW_OP_breg9:
4691     case DW_OP_breg10:
4692     case DW_OP_breg11:
4693     case DW_OP_breg12:
4694     case DW_OP_breg13:
4695     case DW_OP_breg14:
4696     case DW_OP_breg15:
4697     case DW_OP_breg16:
4698     case DW_OP_breg17:
4699     case DW_OP_breg18:
4700     case DW_OP_breg19:
4701     case DW_OP_breg20:
4702     case DW_OP_breg21:
4703     case DW_OP_breg22:
4704     case DW_OP_breg23:
4705     case DW_OP_breg24:
4706     case DW_OP_breg25:
4707     case DW_OP_breg26:
4708     case DW_OP_breg27:
4709     case DW_OP_breg28:
4710     case DW_OP_breg29:
4711     case DW_OP_breg30:
4712     case DW_OP_breg31:
4713       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4714       break;
4715     case DW_OP_regx:
4716       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4717       break;
4718     case DW_OP_fbreg:
4719       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4720       break;
4721     case DW_OP_bregx:
4722       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4723       size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
4724       break;
4725     case DW_OP_piece:
4726       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4727       break;
4728     case DW_OP_deref_size:
4729     case DW_OP_xderef_size:
4730       size += 1;
4731       break;
4732     case DW_OP_call2:
4733       size += 2;
4734       break;
4735     case DW_OP_call4:
4736       size += 4;
4737       break;
4738     case DW_OP_call_ref:
4739       size += DWARF2_ADDR_SIZE;
4740       break;
4741     default:
4742       break;
4743     }
4744
4745   return size;
4746 }
4747
4748 /* Return the size of a series of location descriptors.  */
4749
4750 static unsigned long
4751 size_of_locs (dw_loc_descr_ref loc)
4752 {
4753   dw_loc_descr_ref l;
4754   unsigned long size;
4755
4756   /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
4757      field, to avoid writing to a PCH file.  */
4758   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
4759     {
4760       if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
4761         break;
4762       size += size_of_loc_descr (l);
4763     }
4764   if (! l)
4765     return size;
4766
4767   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
4768     {
4769       l->dw_loc_addr = size;
4770       size += size_of_loc_descr (l);
4771     }
4772
4773   return size;
4774 }
4775
4776 /* Output location description stack opcode's operands (if any).  */
4777
4778 static void
4779 output_loc_operands (dw_loc_descr_ref loc)
4780 {
4781   dw_val_ref val1 = &loc->dw_loc_oprnd1;
4782   dw_val_ref val2 = &loc->dw_loc_oprnd2;
4783
4784   switch (loc->dw_loc_opc)
4785     {
4786 #ifdef DWARF2_DEBUGGING_INFO
4787     case DW_OP_const2u:
4788     case DW_OP_const2s:
4789       dw2_asm_output_data (2, val1->v.val_int, NULL);
4790       break;
4791     case DW_OP_const4u:
4792     case DW_OP_const4s:
4793       dw2_asm_output_data (4, val1->v.val_int, NULL);
4794       break;
4795     case DW_OP_const8u:
4796     case DW_OP_const8s:
4797       gcc_assert (HOST_BITS_PER_LONG >= 64);
4798       dw2_asm_output_data (8, val1->v.val_int, NULL);
4799       break;
4800     case DW_OP_skip:
4801     case DW_OP_bra:
4802       {
4803         int offset;
4804
4805         gcc_assert (val1->val_class == dw_val_class_loc);
4806         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
4807
4808         dw2_asm_output_data (2, offset, NULL);
4809       }
4810       break;
4811 #else
4812     case DW_OP_const2u:
4813     case DW_OP_const2s:
4814     case DW_OP_const4u:
4815     case DW_OP_const4s:
4816     case DW_OP_const8u:
4817     case DW_OP_const8s:
4818     case DW_OP_skip:
4819     case DW_OP_bra:
4820       /* We currently don't make any attempt to make sure these are
4821          aligned properly like we do for the main unwind info, so
4822          don't support emitting things larger than a byte if we're
4823          only doing unwinding.  */
4824       gcc_unreachable ();
4825 #endif
4826     case DW_OP_const1u:
4827     case DW_OP_const1s:
4828       dw2_asm_output_data (1, val1->v.val_int, NULL);
4829       break;
4830     case DW_OP_constu:
4831       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4832       break;
4833     case DW_OP_consts:
4834       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
4835       break;
4836     case DW_OP_pick:
4837       dw2_asm_output_data (1, val1->v.val_int, NULL);
4838       break;
4839     case DW_OP_plus_uconst:
4840       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4841       break;
4842     case DW_OP_breg0:
4843     case DW_OP_breg1:
4844     case DW_OP_breg2:
4845     case DW_OP_breg3:
4846     case DW_OP_breg4:
4847     case DW_OP_breg5:
4848     case DW_OP_breg6:
4849     case DW_OP_breg7:
4850     case DW_OP_breg8:
4851     case DW_OP_breg9:
4852     case DW_OP_breg10:
4853     case DW_OP_breg11:
4854     case DW_OP_breg12:
4855     case DW_OP_breg13:
4856     case DW_OP_breg14:
4857     case DW_OP_breg15:
4858     case DW_OP_breg16:
4859     case DW_OP_breg17:
4860     case DW_OP_breg18:
4861     case DW_OP_breg19:
4862     case DW_OP_breg20:
4863     case DW_OP_breg21:
4864     case DW_OP_breg22:
4865     case DW_OP_breg23:
4866     case DW_OP_breg24:
4867     case DW_OP_breg25:
4868     case DW_OP_breg26:
4869     case DW_OP_breg27:
4870     case DW_OP_breg28:
4871     case DW_OP_breg29:
4872     case DW_OP_breg30:
4873     case DW_OP_breg31:
4874       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
4875       break;
4876     case DW_OP_regx:
4877       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4878       break;
4879     case DW_OP_fbreg:
4880       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
4881       break;
4882     case DW_OP_bregx:
4883       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4884       dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
4885       break;
4886     case DW_OP_piece:
4887       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4888       break;
4889     case DW_OP_deref_size:
4890     case DW_OP_xderef_size:
4891       dw2_asm_output_data (1, val1->v.val_int, NULL);
4892       break;
4893
4894     case DW_OP_addr:
4895       if (loc->dtprel)
4896         {
4897           if (targetm.asm_out.output_dwarf_dtprel)
4898             {
4899               targetm.asm_out.output_dwarf_dtprel (asm_out_file,
4900                                                    DWARF2_ADDR_SIZE,
4901                                                    val1->v.val_addr);
4902               fputc ('\n', asm_out_file);
4903             }
4904           else
4905             gcc_unreachable ();
4906         }
4907       else
4908         {
4909 #ifdef DWARF2_DEBUGGING_INFO
4910           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
4911 #else
4912           gcc_unreachable ();
4913 #endif
4914         }
4915       break;
4916
4917     default:
4918       /* Other codes have no operands.  */
4919       break;
4920     }
4921 }
4922
4923 /* Output a sequence of location operations.  */
4924
4925 static void
4926 output_loc_sequence (dw_loc_descr_ref loc)
4927 {
4928   for (; loc != NULL; loc = loc->dw_loc_next)
4929     {
4930       /* Output the opcode.  */
4931       dw2_asm_output_data (1, loc->dw_loc_opc,
4932                            "%s", dwarf_stack_op_name (loc->dw_loc_opc));
4933
4934       /* Output the operand(s) (if any).  */
4935       output_loc_operands (loc);
4936     }
4937 }
4938
4939 /* Output location description stack opcode's operands (if any).
4940    The output is single bytes on a line, suitable for .cfi_escape.  */
4941
4942 static void
4943 output_loc_operands_raw (dw_loc_descr_ref loc)
4944 {
4945   dw_val_ref val1 = &loc->dw_loc_oprnd1;
4946   dw_val_ref val2 = &loc->dw_loc_oprnd2;
4947
4948   switch (loc->dw_loc_opc)
4949     {
4950     case DW_OP_addr:
4951       /* We cannot output addresses in .cfi_escape, only bytes.  */
4952       gcc_unreachable ();
4953
4954     case DW_OP_const1u:
4955     case DW_OP_const1s:
4956     case DW_OP_pick:
4957     case DW_OP_deref_size:
4958     case DW_OP_xderef_size:
4959       fputc (',', asm_out_file);
4960       dw2_asm_output_data_raw (1, val1->v.val_int);
4961       break;
4962
4963     case DW_OP_const2u:
4964     case DW_OP_const2s:
4965       fputc (',', asm_out_file);
4966       dw2_asm_output_data_raw (2, val1->v.val_int);
4967       break;
4968
4969     case DW_OP_const4u:
4970     case DW_OP_const4s:
4971       fputc (',', asm_out_file);
4972       dw2_asm_output_data_raw (4, val1->v.val_int);
4973       break;
4974
4975     case DW_OP_const8u:
4976     case DW_OP_const8s:
4977       gcc_assert (HOST_BITS_PER_LONG >= 64);
4978       fputc (',', asm_out_file);
4979       dw2_asm_output_data_raw (8, val1->v.val_int);
4980       break;
4981
4982     case DW_OP_skip:
4983     case DW_OP_bra:
4984       {
4985         int offset;
4986
4987         gcc_assert (val1->val_class == dw_val_class_loc);
4988         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
4989
4990         fputc (',', asm_out_file);
4991         dw2_asm_output_data_raw (2, offset);
4992       }
4993       break;
4994
4995     case DW_OP_constu:
4996     case DW_OP_plus_uconst:
4997     case DW_OP_regx:
4998     case DW_OP_piece:
4999       fputc (',', asm_out_file);
5000       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
5001       break;
5002
5003     case DW_OP_consts:
5004     case DW_OP_breg0:
5005     case DW_OP_breg1:
5006     case DW_OP_breg2:
5007     case DW_OP_breg3:
5008     case DW_OP_breg4:
5009     case DW_OP_breg5:
5010     case DW_OP_breg6:
5011     case DW_OP_breg7:
5012     case DW_OP_breg8:
5013     case DW_OP_breg9:
5014     case DW_OP_breg10:
5015     case DW_OP_breg11:
5016     case DW_OP_breg12:
5017     case DW_OP_breg13:
5018     case DW_OP_breg14:
5019     case DW_OP_breg15:
5020     case DW_OP_breg16:
5021     case DW_OP_breg17:
5022     case DW_OP_breg18:
5023     case DW_OP_breg19:
5024     case DW_OP_breg20:
5025     case DW_OP_breg21:
5026     case DW_OP_breg22:
5027     case DW_OP_breg23:
5028     case DW_OP_breg24:
5029     case DW_OP_breg25:
5030     case DW_OP_breg26:
5031     case DW_OP_breg27:
5032     case DW_OP_breg28:
5033     case DW_OP_breg29:
5034     case DW_OP_breg30:
5035     case DW_OP_breg31:
5036     case DW_OP_fbreg:
5037       fputc (',', asm_out_file);
5038       dw2_asm_output_data_sleb128_raw (val1->v.val_int);
5039       break;
5040
5041     case DW_OP_bregx:
5042       fputc (',', asm_out_file);
5043       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
5044       fputc (',', asm_out_file);
5045       dw2_asm_output_data_sleb128_raw (val2->v.val_int);
5046       break;
5047
5048     default:
5049       /* Other codes have no operands.  */
5050       break;
5051     }
5052 }
5053
5054 static void
5055 output_loc_sequence_raw (dw_loc_descr_ref loc)
5056 {
5057   while (1)
5058     {
5059       /* Output the opcode.  */
5060       fprintf (asm_out_file, "0x%x", loc->dw_loc_opc);
5061       output_loc_operands_raw (loc);
5062
5063       if (!loc->dw_loc_next)
5064         break;
5065       loc = loc->dw_loc_next;
5066
5067       fputc (',', asm_out_file);
5068     }
5069 }
5070
5071 /* This routine will generate the correct assembly data for a location
5072    description based on a cfi entry with a complex address.  */
5073
5074 static void
5075 output_cfa_loc (dw_cfi_ref cfi)
5076 {
5077   dw_loc_descr_ref loc;
5078   unsigned long size;
5079
5080   if (cfi->dw_cfi_opc == DW_CFA_expression)
5081     dw2_asm_output_data (1, cfi->dw_cfi_oprnd2.dw_cfi_reg_num, NULL);
5082
5083   /* Output the size of the block.  */
5084   loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
5085   size = size_of_locs (loc);
5086   dw2_asm_output_data_uleb128 (size, NULL);
5087
5088   /* Now output the operations themselves.  */
5089   output_loc_sequence (loc);
5090 }
5091
5092 /* Similar, but used for .cfi_escape.  */
5093
5094 static void
5095 output_cfa_loc_raw (dw_cfi_ref cfi)
5096 {
5097   dw_loc_descr_ref loc;
5098   unsigned long size;
5099
5100   if (cfi->dw_cfi_opc == DW_CFA_expression)
5101     fprintf (asm_out_file, "0x%x,", cfi->dw_cfi_oprnd2.dw_cfi_reg_num);
5102
5103   /* Output the size of the block.  */
5104   loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
5105   size = size_of_locs (loc);
5106   dw2_asm_output_data_uleb128_raw (size);
5107   fputc (',', asm_out_file);
5108
5109   /* Now output the operations themselves.  */
5110   output_loc_sequence_raw (loc);
5111 }
5112
5113 /* This function builds a dwarf location descriptor sequence from a
5114    dw_cfa_location, adding the given OFFSET to the result of the
5115    expression.  */
5116
5117 static struct dw_loc_descr_struct *
5118 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
5119 {
5120   struct dw_loc_descr_struct *head, *tmp;
5121
5122   offset += cfa->offset;
5123
5124   if (cfa->indirect)
5125     {
5126       head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
5127       head->dw_loc_oprnd1.val_class = dw_val_class_const;
5128       tmp = new_loc_descr (DW_OP_deref, 0, 0);
5129       add_loc_descr (&head, tmp);
5130       if (offset != 0)
5131         {
5132           tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
5133           add_loc_descr (&head, tmp);
5134         }
5135     }
5136   else
5137     head = new_reg_loc_descr (cfa->reg, offset);
5138
5139   return head;
5140 }
5141
5142 /* This function builds a dwarf location descriptor sequence for
5143    the address at OFFSET from the CFA when stack is aligned to
5144    ALIGNMENT byte.  */
5145
5146 static struct dw_loc_descr_struct *
5147 build_cfa_aligned_loc (HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
5148 {
5149   struct dw_loc_descr_struct *head;
5150   unsigned int dwarf_fp
5151     = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
5152
5153  /* When CFA is defined as FP+OFFSET, emulate stack alignment.  */
5154   if (cfa.reg == HARD_FRAME_POINTER_REGNUM && cfa.indirect == 0)
5155     {
5156       head = new_reg_loc_descr (dwarf_fp, 0);
5157       add_loc_descr (&head, int_loc_descriptor (alignment));
5158       add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
5159       loc_descr_plus_const (&head, offset);
5160     }
5161   else
5162     head = new_reg_loc_descr (dwarf_fp, offset);
5163   return head;
5164 }
5165
5166 /* This function fills in aa dw_cfa_location structure from a dwarf location
5167    descriptor sequence.  */
5168
5169 static void
5170 get_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_struct *loc)
5171 {
5172   struct dw_loc_descr_struct *ptr;
5173   cfa->offset = 0;
5174   cfa->base_offset = 0;
5175   cfa->indirect = 0;
5176   cfa->reg = -1;
5177
5178   for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
5179     {
5180       enum dwarf_location_atom op = ptr->dw_loc_opc;
5181
5182       switch (op)
5183         {
5184         case DW_OP_reg0:
5185         case DW_OP_reg1:
5186         case DW_OP_reg2:
5187         case DW_OP_reg3:
5188         case DW_OP_reg4:
5189         case DW_OP_reg5:
5190         case DW_OP_reg6:
5191         case DW_OP_reg7:
5192         case DW_OP_reg8:
5193         case DW_OP_reg9:
5194         case DW_OP_reg10:
5195         case DW_OP_reg11:
5196         case DW_OP_reg12:
5197         case DW_OP_reg13:
5198         case DW_OP_reg14:
5199         case DW_OP_reg15:
5200         case DW_OP_reg16:
5201         case DW_OP_reg17:
5202         case DW_OP_reg18:
5203         case DW_OP_reg19:
5204         case DW_OP_reg20:
5205         case DW_OP_reg21:
5206         case DW_OP_reg22:
5207         case DW_OP_reg23:
5208         case DW_OP_reg24:
5209         case DW_OP_reg25:
5210         case DW_OP_reg26:
5211         case DW_OP_reg27:
5212         case DW_OP_reg28:
5213         case DW_OP_reg29:
5214         case DW_OP_reg30:
5215         case DW_OP_reg31:
5216           cfa->reg = op - DW_OP_reg0;
5217           break;
5218         case DW_OP_regx:
5219           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
5220           break;
5221         case DW_OP_breg0:
5222         case DW_OP_breg1:
5223         case DW_OP_breg2:
5224         case DW_OP_breg3:
5225         case DW_OP_breg4:
5226         case DW_OP_breg5:
5227         case DW_OP_breg6:
5228         case DW_OP_breg7:
5229         case DW_OP_breg8:
5230         case DW_OP_breg9:
5231         case DW_OP_breg10:
5232         case DW_OP_breg11:
5233         case DW_OP_breg12:
5234         case DW_OP_breg13:
5235         case DW_OP_breg14:
5236         case DW_OP_breg15:
5237         case DW_OP_breg16:
5238         case DW_OP_breg17:
5239         case DW_OP_breg18:
5240         case DW_OP_breg19:
5241         case DW_OP_breg20:
5242         case DW_OP_breg21:
5243         case DW_OP_breg22:
5244         case DW_OP_breg23:
5245         case DW_OP_breg24:
5246         case DW_OP_breg25:
5247         case DW_OP_breg26:
5248         case DW_OP_breg27:
5249         case DW_OP_breg28:
5250         case DW_OP_breg29:
5251         case DW_OP_breg30:
5252         case DW_OP_breg31:
5253           cfa->reg = op - DW_OP_breg0;
5254           cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
5255           break;
5256         case DW_OP_bregx:
5257           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
5258           cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
5259           break;
5260         case DW_OP_deref:
5261           cfa->indirect = 1;
5262           break;
5263         case DW_OP_plus_uconst:
5264           cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
5265           break;
5266         default:
5267           internal_error ("DW_LOC_OP %s not implemented",
5268                           dwarf_stack_op_name (ptr->dw_loc_opc));
5269         }
5270     }
5271 }
5272 #endif /* .debug_frame support */
5273 \f
5274 /* And now, the support for symbolic debugging information.  */
5275 #ifdef DWARF2_DEBUGGING_INFO
5276
5277 /* .debug_str support.  */
5278 static int output_indirect_string (void **, void *);
5279
5280 static void dwarf2out_init (const char *);
5281 static void dwarf2out_finish (const char *);
5282 static void dwarf2out_define (unsigned int, const char *);
5283 static void dwarf2out_undef (unsigned int, const char *);
5284 static void dwarf2out_start_source_file (unsigned, const char *);
5285 static void dwarf2out_end_source_file (unsigned);
5286 static void dwarf2out_begin_block (unsigned, unsigned);
5287 static void dwarf2out_end_block (unsigned, unsigned);
5288 static bool dwarf2out_ignore_block (const_tree);
5289 static void dwarf2out_global_decl (tree);
5290 static void dwarf2out_type_decl (tree, int);
5291 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
5292 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
5293                                                  dw_die_ref);
5294 static void dwarf2out_abstract_function (tree);
5295 static void dwarf2out_var_location (rtx);
5296 static void dwarf2out_begin_function (tree);
5297 static void dwarf2out_set_name (tree, tree);
5298
5299 /* The debug hooks structure.  */
5300
5301 const struct gcc_debug_hooks dwarf2_debug_hooks =
5302 {
5303   dwarf2out_init,
5304   dwarf2out_finish,
5305   dwarf2out_define,
5306   dwarf2out_undef,
5307   dwarf2out_start_source_file,
5308   dwarf2out_end_source_file,
5309   dwarf2out_begin_block,
5310   dwarf2out_end_block,
5311   dwarf2out_ignore_block,
5312   dwarf2out_source_line,
5313   dwarf2out_begin_prologue,
5314   debug_nothing_int_charstar,   /* end_prologue */
5315   dwarf2out_end_epilogue,
5316   dwarf2out_begin_function,
5317   debug_nothing_int,            /* end_function */
5318   dwarf2out_decl,               /* function_decl */
5319   dwarf2out_global_decl,
5320   dwarf2out_type_decl,          /* type_decl */
5321   dwarf2out_imported_module_or_decl,
5322   debug_nothing_tree,           /* deferred_inline_function */
5323   /* The DWARF 2 backend tries to reduce debugging bloat by not
5324      emitting the abstract description of inline functions until
5325      something tries to reference them.  */
5326   dwarf2out_abstract_function,  /* outlining_inline_function */
5327   debug_nothing_rtx,            /* label */
5328   debug_nothing_int,            /* handle_pch */
5329   dwarf2out_var_location,
5330   dwarf2out_switch_text_section,
5331   dwarf2out_set_name,
5332   1                             /* start_end_main_source_file */
5333 };
5334 #endif
5335 \f
5336 /* NOTE: In the comments in this file, many references are made to
5337    "Debugging Information Entries".  This term is abbreviated as `DIE'
5338    throughout the remainder of this file.  */
5339
5340 /* An internal representation of the DWARF output is built, and then
5341    walked to generate the DWARF debugging info.  The walk of the internal
5342    representation is done after the entire program has been compiled.
5343    The types below are used to describe the internal representation.  */
5344
5345 /* Various DIE's use offsets relative to the beginning of the
5346    .debug_info section to refer to each other.  */
5347
5348 typedef long int dw_offset;
5349
5350 /* Define typedefs here to avoid circular dependencies.  */
5351
5352 typedef struct dw_attr_struct *dw_attr_ref;
5353 typedef struct dw_line_info_struct *dw_line_info_ref;
5354 typedef struct dw_separate_line_info_struct *dw_separate_line_info_ref;
5355 typedef struct pubname_struct *pubname_ref;
5356 typedef struct dw_ranges_struct *dw_ranges_ref;
5357 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
5358
5359 /* Each entry in the line_info_table maintains the file and
5360    line number associated with the label generated for that
5361    entry.  The label gives the PC value associated with
5362    the line number entry.  */
5363
5364 typedef struct GTY(()) dw_line_info_struct {
5365   unsigned long dw_file_num;
5366   unsigned long dw_line_num;
5367 }
5368 dw_line_info_entry;
5369
5370 /* Line information for functions in separate sections; each one gets its
5371    own sequence.  */
5372 typedef struct GTY(()) dw_separate_line_info_struct {
5373   unsigned long dw_file_num;
5374   unsigned long dw_line_num;
5375   unsigned long function;
5376 }
5377 dw_separate_line_info_entry;
5378
5379 /* Each DIE attribute has a field specifying the attribute kind,
5380    a link to the next attribute in the chain, and an attribute value.
5381    Attributes are typically linked below the DIE they modify.  */
5382
5383 typedef struct GTY(()) dw_attr_struct {
5384   enum dwarf_attribute dw_attr;
5385   dw_val_node dw_attr_val;
5386 }
5387 dw_attr_node;
5388
5389 DEF_VEC_O(dw_attr_node);
5390 DEF_VEC_ALLOC_O(dw_attr_node,gc);
5391
5392 /* The Debugging Information Entry (DIE) structure.  DIEs form a tree.
5393    The children of each node form a circular list linked by
5394    die_sib.  die_child points to the node *before* the "first" child node.  */
5395
5396 typedef struct GTY((chain_circular ("%h.die_sib"))) die_struct {
5397   enum dwarf_tag die_tag;
5398   char *die_symbol;
5399   VEC(dw_attr_node,gc) * die_attr;
5400   dw_die_ref die_parent;
5401   dw_die_ref die_child;
5402   dw_die_ref die_sib;
5403   dw_die_ref die_definition; /* ref from a specification to its definition */
5404   dw_offset die_offset;
5405   unsigned long die_abbrev;
5406   int die_mark;
5407   /* Die is used and must not be pruned as unused.  */
5408   int die_perennial_p;
5409   unsigned int decl_id;
5410 }
5411 die_node;
5412
5413 /* Evaluate 'expr' while 'c' is set to each child of DIE in order.  */
5414 #define FOR_EACH_CHILD(die, c, expr) do {       \
5415   c = die->die_child;                           \
5416   if (c) do {                                   \
5417     c = c->die_sib;                             \
5418     expr;                                       \
5419   } while (c != die->die_child);                \
5420 } while (0)
5421
5422 /* The pubname structure */
5423
5424 typedef struct GTY(()) pubname_struct {
5425   dw_die_ref die;
5426   const char *name;
5427 }
5428 pubname_entry;
5429
5430 DEF_VEC_O(pubname_entry);
5431 DEF_VEC_ALLOC_O(pubname_entry, gc);
5432
5433 struct GTY(()) dw_ranges_struct {
5434   /* If this is positive, it's a block number, otherwise it's a
5435      bitwise-negated index into dw_ranges_by_label.  */
5436   int num;
5437 };
5438
5439 struct GTY(()) dw_ranges_by_label_struct {
5440   const char *begin;
5441   const char *end;
5442 };
5443
5444 /* The limbo die list structure.  */
5445 typedef struct GTY(()) limbo_die_struct {
5446   dw_die_ref die;
5447   tree created_for;
5448   struct limbo_die_struct *next;
5449 }
5450 limbo_die_node;
5451
5452 /* How to start an assembler comment.  */
5453 #ifndef ASM_COMMENT_START
5454 #define ASM_COMMENT_START ";#"
5455 #endif
5456
5457 /* Define a macro which returns nonzero for a TYPE_DECL which was
5458    implicitly generated for a tagged type.
5459
5460    Note that unlike the gcc front end (which generates a NULL named
5461    TYPE_DECL node for each complete tagged type, each array type, and
5462    each function type node created) the g++ front end generates a
5463    _named_ TYPE_DECL node for each tagged type node created.
5464    These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
5465    generate a DW_TAG_typedef DIE for them.  */
5466
5467 #define TYPE_DECL_IS_STUB(decl)                         \
5468   (DECL_NAME (decl) == NULL_TREE                        \
5469    || (DECL_ARTIFICIAL (decl)                           \
5470        && is_tagged_type (TREE_TYPE (decl))             \
5471        && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl)))  \
5472            /* This is necessary for stub decls that     \
5473               appear in nested inline functions.  */    \
5474            || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
5475                && (decl_ultimate_origin (decl)          \
5476                    == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
5477
5478 /* Information concerning the compilation unit's programming
5479    language, and compiler version.  */
5480
5481 /* Fixed size portion of the DWARF compilation unit header.  */
5482 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
5483   (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
5484
5485 /* Fixed size portion of public names info.  */
5486 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
5487
5488 /* Fixed size portion of the address range info.  */
5489 #define DWARF_ARANGES_HEADER_SIZE                                       \
5490   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4,      \
5491                 DWARF2_ADDR_SIZE * 2)                                   \
5492    - DWARF_INITIAL_LENGTH_SIZE)
5493
5494 /* Size of padding portion in the address range info.  It must be
5495    aligned to twice the pointer size.  */
5496 #define DWARF_ARANGES_PAD_SIZE \
5497   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
5498                 DWARF2_ADDR_SIZE * 2)                              \
5499    - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
5500
5501 /* Use assembler line directives if available.  */
5502 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
5503 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
5504 #define DWARF2_ASM_LINE_DEBUG_INFO 1
5505 #else
5506 #define DWARF2_ASM_LINE_DEBUG_INFO 0
5507 #endif
5508 #endif
5509
5510 /* Minimum line offset in a special line info. opcode.
5511    This value was chosen to give a reasonable range of values.  */
5512 #define DWARF_LINE_BASE  -10
5513
5514 /* First special line opcode - leave room for the standard opcodes.  */
5515 #define DWARF_LINE_OPCODE_BASE  10
5516
5517 /* Range of line offsets in a special line info. opcode.  */
5518 #define DWARF_LINE_RANGE  (254-DWARF_LINE_OPCODE_BASE+1)
5519
5520 /* Flag that indicates the initial value of the is_stmt_start flag.
5521    In the present implementation, we do not mark any lines as
5522    the beginning of a source statement, because that information
5523    is not made available by the GCC front-end.  */
5524 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
5525
5526 #ifdef DWARF2_DEBUGGING_INFO
5527 /* This location is used by calc_die_sizes() to keep track
5528    the offset of each DIE within the .debug_info section.  */
5529 static unsigned long next_die_offset;
5530 #endif
5531
5532 /* Record the root of the DIE's built for the current compilation unit.  */
5533 static GTY(()) dw_die_ref comp_unit_die;
5534
5535 /* A list of DIEs with a NULL parent waiting to be relocated.  */
5536 static GTY(()) limbo_die_node *limbo_die_list;
5537
5538 /* A list of DIEs for which we may have to generate
5539    DW_AT_MIPS_linkage_name once their DECL_ASSEMBLER_NAMEs are
5540    set.  */
5541 static GTY(()) limbo_die_node *deferred_asm_name;
5542
5543 /* Filenames referenced by this compilation unit.  */
5544 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
5545
5546 /* A hash table of references to DIE's that describe declarations.
5547    The key is a DECL_UID() which is a unique number identifying each decl.  */
5548 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
5549
5550 /* A hash table of references to DIE's that describe COMMON blocks.
5551    The key is DECL_UID() ^ die_parent.  */
5552 static GTY ((param_is (struct die_struct))) htab_t common_block_die_table;
5553
5554 typedef struct GTY(()) die_arg_entry_struct {
5555     dw_die_ref die;
5556     tree arg;
5557 } die_arg_entry;
5558
5559 DEF_VEC_O(die_arg_entry);
5560 DEF_VEC_ALLOC_O(die_arg_entry,gc);
5561
5562 /* Node of the variable location list.  */
5563 struct GTY ((chain_next ("%h.next"))) var_loc_node {
5564   rtx GTY (()) var_loc_note;
5565   const char * GTY (()) label;
5566   const char * GTY (()) section_label;
5567   struct var_loc_node * GTY (()) next;
5568 };
5569
5570 /* Variable location list.  */
5571 struct GTY (()) var_loc_list_def {
5572   struct var_loc_node * GTY (()) first;
5573
5574   /* Do not mark the last element of the chained list because
5575      it is marked through the chain.  */
5576   struct var_loc_node * GTY ((skip ("%h"))) last;
5577
5578   /* DECL_UID of the variable decl.  */
5579   unsigned int decl_id;
5580 };
5581 typedef struct var_loc_list_def var_loc_list;
5582
5583
5584 /* Table of decl location linked lists.  */
5585 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
5586
5587 /* A pointer to the base of a list of references to DIE's that
5588    are uniquely identified by their tag, presence/absence of
5589    children DIE's, and list of attribute/value pairs.  */
5590 static GTY((length ("abbrev_die_table_allocated")))
5591   dw_die_ref *abbrev_die_table;
5592
5593 /* Number of elements currently allocated for abbrev_die_table.  */
5594 static GTY(()) unsigned abbrev_die_table_allocated;
5595
5596 /* Number of elements in type_die_table currently in use.  */
5597 static GTY(()) unsigned abbrev_die_table_in_use;
5598
5599 /* Size (in elements) of increments by which we may expand the
5600    abbrev_die_table.  */
5601 #define ABBREV_DIE_TABLE_INCREMENT 256
5602
5603 /* A pointer to the base of a table that contains line information
5604    for each source code line in .text in the compilation unit.  */
5605 static GTY((length ("line_info_table_allocated")))
5606      dw_line_info_ref line_info_table;
5607
5608 /* Number of elements currently allocated for line_info_table.  */
5609 static GTY(()) unsigned line_info_table_allocated;
5610
5611 /* Number of elements in line_info_table currently in use.  */
5612 static GTY(()) unsigned line_info_table_in_use;
5613
5614 /* A pointer to the base of a table that contains line information
5615    for each source code line outside of .text in the compilation unit.  */
5616 static GTY ((length ("separate_line_info_table_allocated")))
5617      dw_separate_line_info_ref separate_line_info_table;
5618
5619 /* Number of elements currently allocated for separate_line_info_table.  */
5620 static GTY(()) unsigned separate_line_info_table_allocated;
5621
5622 /* Number of elements in separate_line_info_table currently in use.  */
5623 static GTY(()) unsigned separate_line_info_table_in_use;
5624
5625 /* Size (in elements) of increments by which we may expand the
5626    line_info_table.  */
5627 #define LINE_INFO_TABLE_INCREMENT 1024
5628
5629 /* A pointer to the base of a table that contains a list of publicly
5630    accessible names.  */
5631 static GTY (()) VEC (pubname_entry, gc) *  pubname_table;
5632
5633 /* A pointer to the base of a table that contains a list of publicly
5634    accessible types.  */
5635 static GTY (()) VEC (pubname_entry, gc) * pubtype_table;
5636
5637 /* Array of dies for which we should generate .debug_arange info.  */
5638 static GTY((length ("arange_table_allocated"))) dw_die_ref *arange_table;
5639
5640 /* Number of elements currently allocated for arange_table.  */
5641 static GTY(()) unsigned arange_table_allocated;
5642
5643 /* Number of elements in arange_table currently in use.  */
5644 static GTY(()) unsigned arange_table_in_use;
5645
5646 /* Size (in elements) of increments by which we may expand the
5647    arange_table.  */
5648 #define ARANGE_TABLE_INCREMENT 64
5649
5650 /* Array of dies for which we should generate .debug_ranges info.  */
5651 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
5652
5653 /* Number of elements currently allocated for ranges_table.  */
5654 static GTY(()) unsigned ranges_table_allocated;
5655
5656 /* Number of elements in ranges_table currently in use.  */
5657 static GTY(()) unsigned ranges_table_in_use;
5658
5659 /* Array of pairs of labels referenced in ranges_table.  */
5660 static GTY ((length ("ranges_by_label_allocated")))
5661      dw_ranges_by_label_ref ranges_by_label;
5662
5663 /* Number of elements currently allocated for ranges_by_label.  */
5664 static GTY(()) unsigned ranges_by_label_allocated;
5665
5666 /* Number of elements in ranges_by_label currently in use.  */
5667 static GTY(()) unsigned ranges_by_label_in_use;
5668
5669 /* Size (in elements) of increments by which we may expand the
5670    ranges_table.  */
5671 #define RANGES_TABLE_INCREMENT 64
5672
5673 /* Whether we have location lists that need outputting */
5674 static GTY(()) bool have_location_lists;
5675
5676 /* Unique label counter.  */
5677 static GTY(()) unsigned int loclabel_num;
5678
5679 #ifdef DWARF2_DEBUGGING_INFO
5680 /* Record whether the function being analyzed contains inlined functions.  */
5681 static int current_function_has_inlines;
5682 #endif
5683 #if 0 && defined (MIPS_DEBUGGING_INFO)
5684 static int comp_unit_has_inlines;
5685 #endif
5686
5687 /* The last file entry emitted by maybe_emit_file().  */
5688 static GTY(()) struct dwarf_file_data * last_emitted_file;
5689
5690 /* Number of internal labels generated by gen_internal_sym().  */
5691 static GTY(()) int label_num;
5692
5693 /* Cached result of previous call to lookup_filename.  */
5694 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
5695
5696 #ifdef DWARF2_DEBUGGING_INFO
5697
5698 /* Offset from the "steady-state frame pointer" to the frame base,
5699    within the current function.  */
5700 static HOST_WIDE_INT frame_pointer_fb_offset;
5701
5702 static GTY(()) VEC(die_arg_entry,gc) *tmpl_value_parm_die_table;
5703
5704 /* Forward declarations for functions defined in this file.  */
5705
5706 static int is_pseudo_reg (const_rtx);
5707 static tree type_main_variant (tree);
5708 static int is_tagged_type (const_tree);
5709 static const char *dwarf_tag_name (unsigned);
5710 static const char *dwarf_attr_name (unsigned);
5711 static const char *dwarf_form_name (unsigned);
5712 static tree decl_ultimate_origin (const_tree);
5713 static tree decl_class_context (tree);
5714 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
5715 static inline enum dw_val_class AT_class (dw_attr_ref);
5716 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
5717 static inline unsigned AT_flag (dw_attr_ref);
5718 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
5719 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
5720 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
5721 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
5722 static void add_AT_long_long (dw_die_ref, enum dwarf_attribute, unsigned long,
5723                               unsigned long);
5724 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
5725                                unsigned int, unsigned char *);
5726 static hashval_t debug_str_do_hash (const void *);
5727 static int debug_str_eq (const void *, const void *);
5728 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
5729 static inline const char *AT_string (dw_attr_ref);
5730 static enum dwarf_form AT_string_form (dw_attr_ref);
5731 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
5732 static void add_AT_specification (dw_die_ref, dw_die_ref);
5733 static inline dw_die_ref AT_ref (dw_attr_ref);
5734 static inline int AT_ref_external (dw_attr_ref);
5735 static inline void set_AT_ref_external (dw_attr_ref, int);
5736 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
5737 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
5738 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
5739 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
5740                              dw_loc_list_ref);
5741 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
5742 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
5743 static inline rtx AT_addr (dw_attr_ref);
5744 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
5745 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
5746 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
5747 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
5748                            unsigned HOST_WIDE_INT);
5749 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
5750                                unsigned long);
5751 static inline const char *AT_lbl (dw_attr_ref);
5752 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
5753 static const char *get_AT_low_pc (dw_die_ref);
5754 static const char *get_AT_hi_pc (dw_die_ref);
5755 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
5756 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
5757 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
5758 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
5759 static bool is_c_family (void);
5760 static bool is_cxx (void);
5761 static bool is_java (void);
5762 static bool is_fortran (void);
5763 static bool is_ada (void);
5764 static void remove_AT (dw_die_ref, enum dwarf_attribute);
5765 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
5766 static void add_child_die (dw_die_ref, dw_die_ref);
5767 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
5768 static dw_die_ref lookup_type_die (tree);
5769 static void equate_type_number_to_die (tree, dw_die_ref);
5770 static hashval_t decl_die_table_hash (const void *);
5771 static int decl_die_table_eq (const void *, const void *);
5772 static dw_die_ref lookup_decl_die (tree);
5773 static hashval_t common_block_die_table_hash (const void *);
5774 static int common_block_die_table_eq (const void *, const void *);
5775 static hashval_t decl_loc_table_hash (const void *);
5776 static int decl_loc_table_eq (const void *, const void *);
5777 static var_loc_list *lookup_decl_loc (const_tree);
5778 static void equate_decl_number_to_die (tree, dw_die_ref);
5779 static void add_var_loc_to_decl (tree, struct var_loc_node *);
5780 static void print_spaces (FILE *);
5781 static void print_die (dw_die_ref, FILE *);
5782 static void print_dwarf_line_table (FILE *);
5783 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
5784 static dw_die_ref pop_compile_unit (dw_die_ref);
5785 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
5786 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
5787 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
5788 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
5789 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
5790 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
5791 static int same_die_p (dw_die_ref, dw_die_ref, int *);
5792 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
5793 static void compute_section_prefix (dw_die_ref);
5794 static int is_type_die (dw_die_ref);
5795 static int is_comdat_die (dw_die_ref);
5796 static int is_symbol_die (dw_die_ref);
5797 static void assign_symbol_names (dw_die_ref);
5798 static void break_out_includes (dw_die_ref);
5799 static hashval_t htab_cu_hash (const void *);
5800 static int htab_cu_eq (const void *, const void *);
5801 static void htab_cu_del (void *);
5802 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
5803 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
5804 static void add_sibling_attributes (dw_die_ref);
5805 static void build_abbrev_table (dw_die_ref);
5806 static void output_location_lists (dw_die_ref);
5807 static int constant_size (unsigned HOST_WIDE_INT);
5808 static unsigned long size_of_die (dw_die_ref);
5809 static void calc_die_sizes (dw_die_ref);
5810 static void mark_dies (dw_die_ref);
5811 static void unmark_dies (dw_die_ref);
5812 static void unmark_all_dies (dw_die_ref);
5813 static unsigned long size_of_pubnames (VEC (pubname_entry,gc) *);
5814 static unsigned long size_of_aranges (void);
5815 static enum dwarf_form value_format (dw_attr_ref);
5816 static void output_value_format (dw_attr_ref);
5817 static void output_abbrev_section (void);
5818 static void output_die_symbol (dw_die_ref);
5819 static void output_die (dw_die_ref);
5820 static void output_compilation_unit_header (void);
5821 static void output_comp_unit (dw_die_ref, int);
5822 static const char *dwarf2_name (tree, int);
5823 static void add_pubname (tree, dw_die_ref);
5824 static void add_pubname_string (const char *, dw_die_ref);
5825 static void add_pubtype (tree, dw_die_ref);
5826 static void output_pubnames (VEC (pubname_entry,gc) *);
5827 static void add_arange (tree, dw_die_ref);
5828 static void output_aranges (void);
5829 static unsigned int add_ranges_num (int);
5830 static unsigned int add_ranges (const_tree);
5831 static unsigned int add_ranges_by_labels (const char *, const char *);
5832 static void output_ranges (void);
5833 static void output_line_info (void);
5834 static void output_file_names (void);
5835 static dw_die_ref base_type_die (tree);
5836 static int is_base_type (tree);
5837 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
5838 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
5839 static dw_die_ref generic_parameter_die (tree, tree, dw_die_ref, int);
5840 static int type_is_enum (const_tree);
5841 static unsigned int dbx_reg_number (const_rtx);
5842 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
5843 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
5844 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
5845                                                 enum var_init_status);
5846 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
5847                                                      enum var_init_status);
5848 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
5849                                          enum var_init_status);
5850 static int is_based_loc (const_rtx);
5851 static dw_loc_descr_ref mem_loc_descriptor (rtx, enum machine_mode mode,
5852                                             enum var_init_status);
5853 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
5854                                                enum var_init_status);
5855 static dw_loc_descr_ref loc_descriptor (rtx, enum var_init_status);
5856 static dw_loc_descr_ref loc_descriptor_from_tree_1 (tree, int);
5857 static dw_loc_descr_ref loc_descriptor_from_tree (tree);
5858 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
5859 static tree field_type (const_tree);
5860 static unsigned int simple_type_align_in_bits (const_tree);
5861 static unsigned int simple_decl_align_in_bits (const_tree);
5862 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
5863 static HOST_WIDE_INT field_byte_offset (const_tree);
5864 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
5865                                          dw_loc_descr_ref);
5866 static void add_data_member_location_attribute (dw_die_ref, tree);
5867 static void add_const_value_attribute (dw_die_ref, rtx);
5868 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
5869 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
5870 static void insert_float (const_rtx, unsigned char *);
5871 static rtx rtl_for_decl_location (tree);
5872 static void add_location_or_const_value_attribute (dw_die_ref, tree,
5873                                                    enum dwarf_attribute);
5874 static void tree_add_const_value_attribute (dw_die_ref, tree);
5875 static void tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
5876 static void add_name_attribute (dw_die_ref, const char *);
5877 static void add_comp_dir_attribute (dw_die_ref);
5878 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
5879 static void add_subscript_info (dw_die_ref, tree, bool);
5880 static void add_byte_size_attribute (dw_die_ref, tree);
5881 static void add_bit_offset_attribute (dw_die_ref, tree);
5882 static void add_bit_size_attribute (dw_die_ref, tree);
5883 static void add_prototyped_attribute (dw_die_ref, tree);
5884 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
5885 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
5886 static void add_src_coords_attributes (dw_die_ref, tree);
5887 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
5888 static void push_decl_scope (tree);
5889 static void pop_decl_scope (void);
5890 static dw_die_ref scope_die_for (tree, dw_die_ref);
5891 static inline int local_scope_p (dw_die_ref);
5892 static inline int class_scope_p (dw_die_ref);
5893 static inline int class_or_namespace_scope_p (dw_die_ref);
5894 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
5895 static void add_calling_convention_attribute (dw_die_ref, tree);
5896 static const char *type_tag (const_tree);
5897 static tree member_declared_type (const_tree);
5898 #if 0
5899 static const char *decl_start_label (tree);
5900 #endif
5901 static void gen_array_type_die (tree, dw_die_ref);
5902 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
5903 #if 0
5904 static void gen_entry_point_die (tree, dw_die_ref);
5905 #endif
5906 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
5907 static dw_die_ref gen_formal_parameter_die (tree, tree, dw_die_ref);
5908 static void gen_unspecified_parameters_die (tree, dw_die_ref);
5909 static void gen_formal_types_die (tree, dw_die_ref);
5910 static void gen_subprogram_die (tree, dw_die_ref);
5911 static void gen_variable_die (tree, tree, dw_die_ref);
5912 static void gen_const_die (tree, dw_die_ref);
5913 static void gen_label_die (tree, dw_die_ref);
5914 static void gen_lexical_block_die (tree, dw_die_ref, int);
5915 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
5916 static void gen_field_die (tree, dw_die_ref);
5917 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
5918 static dw_die_ref gen_compile_unit_die (const char *);
5919 static void gen_inheritance_die (tree, tree, dw_die_ref);
5920 static void gen_member_die (tree, dw_die_ref);
5921 static void gen_struct_or_union_type_die (tree, dw_die_ref,
5922                                                 enum debug_info_usage);
5923 static void gen_subroutine_type_die (tree, dw_die_ref);
5924 static void gen_typedef_die (tree, dw_die_ref);
5925 static void gen_type_die (tree, dw_die_ref);
5926 static void gen_block_die (tree, dw_die_ref, int);
5927 static void decls_for_scope (tree, dw_die_ref, int);
5928 static int is_redundant_typedef (const_tree);
5929 static inline dw_die_ref get_context_die (tree);
5930 static void gen_namespace_die (tree, dw_die_ref);
5931 static void gen_decl_die (tree, tree, dw_die_ref);
5932 static dw_die_ref force_decl_die (tree);
5933 static dw_die_ref force_type_die (tree);
5934 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
5935 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
5936 static struct dwarf_file_data * lookup_filename (const char *);
5937 static void retry_incomplete_types (void);
5938 static void gen_type_die_for_member (tree, tree, dw_die_ref);
5939 static tree make_ith_pack_parameter_name (tree, int);
5940 static void gen_generic_params_dies (tree);
5941 static void splice_child_die (dw_die_ref, dw_die_ref);
5942 static int file_info_cmp (const void *, const void *);
5943 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
5944                                      const char *, const char *, unsigned);
5945 static void add_loc_descr_to_loc_list (dw_loc_list_ref *, dw_loc_descr_ref,
5946                                        const char *, const char *,
5947                                        const char *);
5948 static void output_loc_list (dw_loc_list_ref);
5949 static char *gen_internal_sym (const char *);
5950
5951 static void prune_unmark_dies (dw_die_ref);
5952 static void prune_unused_types_mark (dw_die_ref, int);
5953 static void prune_unused_types_walk (dw_die_ref);
5954 static void prune_unused_types_walk_attribs (dw_die_ref);
5955 static void prune_unused_types_prune (dw_die_ref);
5956 static void prune_unused_types (void);
5957 static int maybe_emit_file (struct dwarf_file_data *fd);
5958 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
5959 static void gen_remaining_tmpl_value_param_die_attribute (void);
5960
5961 /* Section names used to hold DWARF debugging information.  */
5962 #ifndef DEBUG_INFO_SECTION
5963 #define DEBUG_INFO_SECTION      ".debug_info"
5964 #endif
5965 #ifndef DEBUG_ABBREV_SECTION
5966 #define DEBUG_ABBREV_SECTION    ".debug_abbrev"
5967 #endif
5968 #ifndef DEBUG_ARANGES_SECTION
5969 #define DEBUG_ARANGES_SECTION   ".debug_aranges"
5970 #endif
5971 #ifndef DEBUG_MACINFO_SECTION
5972 #define DEBUG_MACINFO_SECTION   ".debug_macinfo"
5973 #endif
5974 #ifndef DEBUG_LINE_SECTION
5975 #define DEBUG_LINE_SECTION      ".debug_line"
5976 #endif
5977 #ifndef DEBUG_LOC_SECTION
5978 #define DEBUG_LOC_SECTION       ".debug_loc"
5979 #endif
5980 #ifndef DEBUG_PUBNAMES_SECTION
5981 #define DEBUG_PUBNAMES_SECTION  ".debug_pubnames"
5982 #endif
5983 #ifndef DEBUG_PUBTYPES_SECTION
5984 #define DEBUG_PUBTYPES_SECTION  ".debug_pubtypes"
5985 #endif
5986 #ifndef DEBUG_STR_SECTION
5987 #define DEBUG_STR_SECTION       ".debug_str"
5988 #endif
5989 #ifndef DEBUG_RANGES_SECTION
5990 #define DEBUG_RANGES_SECTION    ".debug_ranges"
5991 #endif
5992
5993 /* Standard ELF section names for compiled code and data.  */
5994 #ifndef TEXT_SECTION_NAME
5995 #define TEXT_SECTION_NAME       ".text"
5996 #endif
5997
5998 /* Section flags for .debug_str section.  */
5999 #define DEBUG_STR_SECTION_FLAGS \
6000   (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings               \
6001    ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1        \
6002    : SECTION_DEBUG)
6003
6004 /* Labels we insert at beginning sections we can reference instead of
6005    the section names themselves.  */
6006
6007 #ifndef TEXT_SECTION_LABEL
6008 #define TEXT_SECTION_LABEL              "Ltext"
6009 #endif
6010 #ifndef COLD_TEXT_SECTION_LABEL
6011 #define COLD_TEXT_SECTION_LABEL         "Ltext_cold"
6012 #endif
6013 #ifndef DEBUG_LINE_SECTION_LABEL
6014 #define DEBUG_LINE_SECTION_LABEL        "Ldebug_line"
6015 #endif
6016 #ifndef DEBUG_INFO_SECTION_LABEL
6017 #define DEBUG_INFO_SECTION_LABEL        "Ldebug_info"
6018 #endif
6019 #ifndef DEBUG_ABBREV_SECTION_LABEL
6020 #define DEBUG_ABBREV_SECTION_LABEL      "Ldebug_abbrev"
6021 #endif
6022 #ifndef DEBUG_LOC_SECTION_LABEL
6023 #define DEBUG_LOC_SECTION_LABEL         "Ldebug_loc"
6024 #endif
6025 #ifndef DEBUG_RANGES_SECTION_LABEL
6026 #define DEBUG_RANGES_SECTION_LABEL      "Ldebug_ranges"
6027 #endif
6028 #ifndef DEBUG_MACINFO_SECTION_LABEL
6029 #define DEBUG_MACINFO_SECTION_LABEL     "Ldebug_macinfo"
6030 #endif
6031
6032 /* Definitions of defaults for formats and names of various special
6033    (artificial) labels which may be generated within this file (when the -g
6034    options is used and DWARF2_DEBUGGING_INFO is in effect.
6035    If necessary, these may be overridden from within the tm.h file, but
6036    typically, overriding these defaults is unnecessary.  */
6037
6038 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
6039 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6040 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6041 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
6042 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6043 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6044 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6045 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6046 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6047 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
6048
6049 #ifndef TEXT_END_LABEL
6050 #define TEXT_END_LABEL          "Letext"
6051 #endif
6052 #ifndef COLD_END_LABEL
6053 #define COLD_END_LABEL          "Letext_cold"
6054 #endif
6055 #ifndef BLOCK_BEGIN_LABEL
6056 #define BLOCK_BEGIN_LABEL       "LBB"
6057 #endif
6058 #ifndef BLOCK_END_LABEL
6059 #define BLOCK_END_LABEL         "LBE"
6060 #endif
6061 #ifndef LINE_CODE_LABEL
6062 #define LINE_CODE_LABEL         "LM"
6063 #endif
6064 #ifndef SEPARATE_LINE_CODE_LABEL
6065 #define SEPARATE_LINE_CODE_LABEL        "LSM"
6066 #endif
6067
6068 \f
6069 /* We allow a language front-end to designate a function that is to be
6070    called to "demangle" any name before it is put into a DIE.  */
6071
6072 static const char *(*demangle_name_func) (const char *);
6073
6074 void
6075 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
6076 {
6077   demangle_name_func = func;
6078 }
6079
6080 /* Test if rtl node points to a pseudo register.  */
6081
6082 static inline int
6083 is_pseudo_reg (const_rtx rtl)
6084 {
6085   return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
6086           || (GET_CODE (rtl) == SUBREG
6087               && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
6088 }
6089
6090 /* Return a reference to a type, with its const and volatile qualifiers
6091    removed.  */
6092
6093 static inline tree
6094 type_main_variant (tree type)
6095 {
6096   type = TYPE_MAIN_VARIANT (type);
6097
6098   /* ??? There really should be only one main variant among any group of
6099      variants of a given type (and all of the MAIN_VARIANT values for all
6100      members of the group should point to that one type) but sometimes the C
6101      front-end messes this up for array types, so we work around that bug
6102      here.  */
6103   if (TREE_CODE (type) == ARRAY_TYPE)
6104     while (type != TYPE_MAIN_VARIANT (type))
6105       type = TYPE_MAIN_VARIANT (type);
6106
6107   return type;
6108 }
6109
6110 /* Return nonzero if the given type node represents a tagged type.  */
6111
6112 static inline int
6113 is_tagged_type (const_tree type)
6114 {
6115   enum tree_code code = TREE_CODE (type);
6116
6117   return (code == RECORD_TYPE || code == UNION_TYPE
6118           || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
6119 }
6120
6121 /* Convert a DIE tag into its string name.  */
6122
6123 static const char *
6124 dwarf_tag_name (unsigned int tag)
6125 {
6126   switch (tag)
6127     {
6128     case DW_TAG_padding:
6129       return "DW_TAG_padding";
6130     case DW_TAG_array_type:
6131       return "DW_TAG_array_type";
6132     case DW_TAG_class_type:
6133       return "DW_TAG_class_type";
6134     case DW_TAG_entry_point:
6135       return "DW_TAG_entry_point";
6136     case DW_TAG_enumeration_type:
6137       return "DW_TAG_enumeration_type";
6138     case DW_TAG_formal_parameter:
6139       return "DW_TAG_formal_parameter";
6140     case DW_TAG_imported_declaration:
6141       return "DW_TAG_imported_declaration";
6142     case DW_TAG_label:
6143       return "DW_TAG_label";
6144     case DW_TAG_lexical_block:
6145       return "DW_TAG_lexical_block";
6146     case DW_TAG_member:
6147       return "DW_TAG_member";
6148     case DW_TAG_pointer_type:
6149       return "DW_TAG_pointer_type";
6150     case DW_TAG_reference_type:
6151       return "DW_TAG_reference_type";
6152     case DW_TAG_compile_unit:
6153       return "DW_TAG_compile_unit";
6154     case DW_TAG_string_type:
6155       return "DW_TAG_string_type";
6156     case DW_TAG_structure_type:
6157       return "DW_TAG_structure_type";
6158     case DW_TAG_subroutine_type:
6159       return "DW_TAG_subroutine_type";
6160     case DW_TAG_typedef:
6161       return "DW_TAG_typedef";
6162     case DW_TAG_union_type:
6163       return "DW_TAG_union_type";
6164     case DW_TAG_unspecified_parameters:
6165       return "DW_TAG_unspecified_parameters";
6166     case DW_TAG_variant:
6167       return "DW_TAG_variant";
6168     case DW_TAG_common_block:
6169       return "DW_TAG_common_block";
6170     case DW_TAG_common_inclusion:
6171       return "DW_TAG_common_inclusion";
6172     case DW_TAG_inheritance:
6173       return "DW_TAG_inheritance";
6174     case DW_TAG_inlined_subroutine:
6175       return "DW_TAG_inlined_subroutine";
6176     case DW_TAG_module:
6177       return "DW_TAG_module";
6178     case DW_TAG_ptr_to_member_type:
6179       return "DW_TAG_ptr_to_member_type";
6180     case DW_TAG_set_type:
6181       return "DW_TAG_set_type";
6182     case DW_TAG_subrange_type:
6183       return "DW_TAG_subrange_type";
6184     case DW_TAG_with_stmt:
6185       return "DW_TAG_with_stmt";
6186     case DW_TAG_access_declaration:
6187       return "DW_TAG_access_declaration";
6188     case DW_TAG_base_type:
6189       return "DW_TAG_base_type";
6190     case DW_TAG_catch_block:
6191       return "DW_TAG_catch_block";
6192     case DW_TAG_const_type:
6193       return "DW_TAG_const_type";
6194     case DW_TAG_constant:
6195       return "DW_TAG_constant";
6196     case DW_TAG_enumerator:
6197       return "DW_TAG_enumerator";
6198     case DW_TAG_file_type:
6199       return "DW_TAG_file_type";
6200     case DW_TAG_friend:
6201       return "DW_TAG_friend";
6202     case DW_TAG_namelist:
6203       return "DW_TAG_namelist";
6204     case DW_TAG_namelist_item:
6205       return "DW_TAG_namelist_item";
6206     case DW_TAG_packed_type:
6207       return "DW_TAG_packed_type";
6208     case DW_TAG_subprogram:
6209       return "DW_TAG_subprogram";
6210     case DW_TAG_template_type_param:
6211       return "DW_TAG_template_type_param";
6212     case DW_TAG_template_value_param:
6213       return "DW_TAG_template_value_param";
6214     case DW_TAG_thrown_type:
6215       return "DW_TAG_thrown_type";
6216     case DW_TAG_try_block:
6217       return "DW_TAG_try_block";
6218     case DW_TAG_variant_part:
6219       return "DW_TAG_variant_part";
6220     case DW_TAG_variable:
6221       return "DW_TAG_variable";
6222     case DW_TAG_volatile_type:
6223       return "DW_TAG_volatile_type";
6224     case DW_TAG_dwarf_procedure:
6225       return "DW_TAG_dwarf_procedure";
6226     case DW_TAG_restrict_type:
6227       return "DW_TAG_restrict_type";
6228     case DW_TAG_interface_type:
6229       return "DW_TAG_interface_type";
6230     case DW_TAG_namespace:
6231       return "DW_TAG_namespace";
6232     case DW_TAG_imported_module:
6233       return "DW_TAG_imported_module";
6234     case DW_TAG_unspecified_type:
6235       return "DW_TAG_unspecified_type";
6236     case DW_TAG_partial_unit:
6237       return "DW_TAG_partial_unit";
6238     case DW_TAG_imported_unit:
6239       return "DW_TAG_imported_unit";
6240     case DW_TAG_condition:
6241       return "DW_TAG_condition";
6242     case DW_TAG_shared_type:
6243       return "DW_TAG_shared_type";
6244     case DW_TAG_MIPS_loop:
6245       return "DW_TAG_MIPS_loop";
6246     case DW_TAG_format_label:
6247       return "DW_TAG_format_label";
6248     case DW_TAG_function_template:
6249       return "DW_TAG_function_template";
6250     case DW_TAG_class_template:
6251       return "DW_TAG_class_template";
6252     case DW_TAG_GNU_BINCL:
6253       return "DW_TAG_GNU_BINCL";
6254     case DW_TAG_GNU_EINCL:
6255       return "DW_TAG_GNU_EINCL";
6256     case DW_TAG_GNU_template_template_param:
6257       return "DW_TAG_GNU_template_template_param";
6258     default:
6259       return "DW_TAG_<unknown>";
6260     }
6261 }
6262
6263 /* Convert a DWARF attribute code into its string name.  */
6264
6265 static const char *
6266 dwarf_attr_name (unsigned int attr)
6267 {
6268   switch (attr)
6269     {
6270     case DW_AT_sibling:
6271       return "DW_AT_sibling";
6272     case DW_AT_location:
6273       return "DW_AT_location";
6274     case DW_AT_name:
6275       return "DW_AT_name";
6276     case DW_AT_ordering:
6277       return "DW_AT_ordering";
6278     case DW_AT_subscr_data:
6279       return "DW_AT_subscr_data";
6280     case DW_AT_byte_size:
6281       return "DW_AT_byte_size";
6282     case DW_AT_bit_offset:
6283       return "DW_AT_bit_offset";
6284     case DW_AT_bit_size:
6285       return "DW_AT_bit_size";
6286     case DW_AT_element_list:
6287       return "DW_AT_element_list";
6288     case DW_AT_stmt_list:
6289       return "DW_AT_stmt_list";
6290     case DW_AT_low_pc:
6291       return "DW_AT_low_pc";
6292     case DW_AT_high_pc:
6293       return "DW_AT_high_pc";
6294     case DW_AT_language:
6295       return "DW_AT_language";
6296     case DW_AT_member:
6297       return "DW_AT_member";
6298     case DW_AT_discr:
6299       return "DW_AT_discr";
6300     case DW_AT_discr_value:
6301       return "DW_AT_discr_value";
6302     case DW_AT_visibility:
6303       return "DW_AT_visibility";
6304     case DW_AT_import:
6305       return "DW_AT_import";
6306     case DW_AT_string_length:
6307       return "DW_AT_string_length";
6308     case DW_AT_common_reference:
6309       return "DW_AT_common_reference";
6310     case DW_AT_comp_dir:
6311       return "DW_AT_comp_dir";
6312     case DW_AT_const_value:
6313       return "DW_AT_const_value";
6314     case DW_AT_containing_type:
6315       return "DW_AT_containing_type";
6316     case DW_AT_default_value:
6317       return "DW_AT_default_value";
6318     case DW_AT_inline:
6319       return "DW_AT_inline";
6320     case DW_AT_is_optional:
6321       return "DW_AT_is_optional";
6322     case DW_AT_lower_bound:
6323       return "DW_AT_lower_bound";
6324     case DW_AT_producer:
6325       return "DW_AT_producer";
6326     case DW_AT_prototyped:
6327       return "DW_AT_prototyped";
6328     case DW_AT_return_addr:
6329       return "DW_AT_return_addr";
6330     case DW_AT_start_scope:
6331       return "DW_AT_start_scope";
6332     case DW_AT_bit_stride:
6333       return "DW_AT_bit_stride";
6334     case DW_AT_upper_bound:
6335       return "DW_AT_upper_bound";
6336     case DW_AT_abstract_origin:
6337       return "DW_AT_abstract_origin";
6338     case DW_AT_accessibility:
6339       return "DW_AT_accessibility";
6340     case DW_AT_address_class:
6341       return "DW_AT_address_class";
6342     case DW_AT_artificial:
6343       return "DW_AT_artificial";
6344     case DW_AT_base_types:
6345       return "DW_AT_base_types";
6346     case DW_AT_calling_convention:
6347       return "DW_AT_calling_convention";
6348     case DW_AT_count:
6349       return "DW_AT_count";
6350     case DW_AT_data_member_location:
6351       return "DW_AT_data_member_location";
6352     case DW_AT_decl_column:
6353       return "DW_AT_decl_column";
6354     case DW_AT_decl_file:
6355       return "DW_AT_decl_file";
6356     case DW_AT_decl_line:
6357       return "DW_AT_decl_line";
6358     case DW_AT_declaration:
6359       return "DW_AT_declaration";
6360     case DW_AT_discr_list:
6361       return "DW_AT_discr_list";
6362     case DW_AT_encoding:
6363       return "DW_AT_encoding";
6364     case DW_AT_external:
6365       return "DW_AT_external";
6366     case DW_AT_explicit:
6367       return "DW_AT_explicit";
6368     case DW_AT_frame_base:
6369       return "DW_AT_frame_base";
6370     case DW_AT_friend:
6371       return "DW_AT_friend";
6372     case DW_AT_identifier_case:
6373       return "DW_AT_identifier_case";
6374     case DW_AT_macro_info:
6375       return "DW_AT_macro_info";
6376     case DW_AT_namelist_items:
6377       return "DW_AT_namelist_items";
6378     case DW_AT_priority:
6379       return "DW_AT_priority";
6380     case DW_AT_segment:
6381       return "DW_AT_segment";
6382     case DW_AT_specification:
6383       return "DW_AT_specification";
6384     case DW_AT_static_link:
6385       return "DW_AT_static_link";
6386     case DW_AT_type:
6387       return "DW_AT_type";
6388     case DW_AT_use_location:
6389       return "DW_AT_use_location";
6390     case DW_AT_variable_parameter:
6391       return "DW_AT_variable_parameter";
6392     case DW_AT_virtuality:
6393       return "DW_AT_virtuality";
6394     case DW_AT_vtable_elem_location:
6395       return "DW_AT_vtable_elem_location";
6396
6397     case DW_AT_allocated:
6398       return "DW_AT_allocated";
6399     case DW_AT_associated:
6400       return "DW_AT_associated";
6401     case DW_AT_data_location:
6402       return "DW_AT_data_location";
6403     case DW_AT_byte_stride:
6404       return "DW_AT_byte_stride";
6405     case DW_AT_entry_pc:
6406       return "DW_AT_entry_pc";
6407     case DW_AT_use_UTF8:
6408       return "DW_AT_use_UTF8";
6409     case DW_AT_extension:
6410       return "DW_AT_extension";
6411     case DW_AT_ranges:
6412       return "DW_AT_ranges";
6413     case DW_AT_trampoline:
6414       return "DW_AT_trampoline";
6415     case DW_AT_call_column:
6416       return "DW_AT_call_column";
6417     case DW_AT_call_file:
6418       return "DW_AT_call_file";
6419     case DW_AT_call_line:
6420       return "DW_AT_call_line";
6421
6422     case DW_AT_MIPS_fde:
6423       return "DW_AT_MIPS_fde";
6424     case DW_AT_MIPS_loop_begin:
6425       return "DW_AT_MIPS_loop_begin";
6426     case DW_AT_MIPS_tail_loop_begin:
6427       return "DW_AT_MIPS_tail_loop_begin";
6428     case DW_AT_MIPS_epilog_begin:
6429       return "DW_AT_MIPS_epilog_begin";
6430     case DW_AT_MIPS_loop_unroll_factor:
6431       return "DW_AT_MIPS_loop_unroll_factor";
6432     case DW_AT_MIPS_software_pipeline_depth:
6433       return "DW_AT_MIPS_software_pipeline_depth";
6434     case DW_AT_MIPS_linkage_name:
6435       return "DW_AT_MIPS_linkage_name";
6436     case DW_AT_MIPS_stride:
6437       return "DW_AT_MIPS_stride";
6438     case DW_AT_MIPS_abstract_name:
6439       return "DW_AT_MIPS_abstract_name";
6440     case DW_AT_MIPS_clone_origin:
6441       return "DW_AT_MIPS_clone_origin";
6442     case DW_AT_MIPS_has_inlines:
6443       return "DW_AT_MIPS_has_inlines";
6444
6445     case DW_AT_sf_names:
6446       return "DW_AT_sf_names";
6447     case DW_AT_src_info:
6448       return "DW_AT_src_info";
6449     case DW_AT_mac_info:
6450       return "DW_AT_mac_info";
6451     case DW_AT_src_coords:
6452       return "DW_AT_src_coords";
6453     case DW_AT_body_begin:
6454       return "DW_AT_body_begin";
6455     case DW_AT_body_end:
6456       return "DW_AT_body_end";
6457     case DW_AT_GNU_vector:
6458       return "DW_AT_GNU_vector";
6459     case DW_AT_GNU_template_name:
6460       return "DW_AT_GNU_template_name";
6461
6462     case DW_AT_VMS_rtnbeg_pd_address:
6463       return "DW_AT_VMS_rtnbeg_pd_address";
6464
6465     default:
6466       return "DW_AT_<unknown>";
6467     }
6468 }
6469
6470 /* Convert a DWARF value form code into its string name.  */
6471
6472 static const char *
6473 dwarf_form_name (unsigned int form)
6474 {
6475   switch (form)
6476     {
6477     case DW_FORM_addr:
6478       return "DW_FORM_addr";
6479     case DW_FORM_block2:
6480       return "DW_FORM_block2";
6481     case DW_FORM_block4:
6482       return "DW_FORM_block4";
6483     case DW_FORM_data2:
6484       return "DW_FORM_data2";
6485     case DW_FORM_data4:
6486       return "DW_FORM_data4";
6487     case DW_FORM_data8:
6488       return "DW_FORM_data8";
6489     case DW_FORM_string:
6490       return "DW_FORM_string";
6491     case DW_FORM_block:
6492       return "DW_FORM_block";
6493     case DW_FORM_block1:
6494       return "DW_FORM_block1";
6495     case DW_FORM_data1:
6496       return "DW_FORM_data1";
6497     case DW_FORM_flag:
6498       return "DW_FORM_flag";
6499     case DW_FORM_sdata:
6500       return "DW_FORM_sdata";
6501     case DW_FORM_strp:
6502       return "DW_FORM_strp";
6503     case DW_FORM_udata:
6504       return "DW_FORM_udata";
6505     case DW_FORM_ref_addr:
6506       return "DW_FORM_ref_addr";
6507     case DW_FORM_ref1:
6508       return "DW_FORM_ref1";
6509     case DW_FORM_ref2:
6510       return "DW_FORM_ref2";
6511     case DW_FORM_ref4:
6512       return "DW_FORM_ref4";
6513     case DW_FORM_ref8:
6514       return "DW_FORM_ref8";
6515     case DW_FORM_ref_udata:
6516       return "DW_FORM_ref_udata";
6517     case DW_FORM_indirect:
6518       return "DW_FORM_indirect";
6519     default:
6520       return "DW_FORM_<unknown>";
6521     }
6522 }
6523 \f
6524 /* Determine the "ultimate origin" of a decl.  The decl may be an inlined
6525    instance of an inlined instance of a decl which is local to an inline
6526    function, so we have to trace all of the way back through the origin chain
6527    to find out what sort of node actually served as the original seed for the
6528    given block.  */
6529
6530 static tree
6531 decl_ultimate_origin (const_tree decl)
6532 {
6533   if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
6534     return NULL_TREE;
6535
6536   /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
6537      nodes in the function to point to themselves; ignore that if
6538      we're trying to output the abstract instance of this function.  */
6539   if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
6540     return NULL_TREE;
6541
6542   /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
6543      most distant ancestor, this should never happen.  */
6544   gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
6545
6546   return DECL_ABSTRACT_ORIGIN (decl);
6547 }
6548
6549 /* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
6550    of a virtual function may refer to a base class, so we check the 'this'
6551    parameter.  */
6552
6553 static tree
6554 decl_class_context (tree decl)
6555 {
6556   tree context = NULL_TREE;
6557
6558   if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
6559     context = DECL_CONTEXT (decl);
6560   else
6561     context = TYPE_MAIN_VARIANT
6562       (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
6563
6564   if (context && !TYPE_P (context))
6565     context = NULL_TREE;
6566
6567   return context;
6568 }
6569 \f
6570 /* Add an attribute/value pair to a DIE.  */
6571
6572 static inline void
6573 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
6574 {
6575   /* Maybe this should be an assert?  */
6576   if (die == NULL)
6577     return;
6578
6579   if (die->die_attr == NULL)
6580     die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
6581   VEC_safe_push (dw_attr_node, gc, die->die_attr, attr);
6582 }
6583
6584 static inline enum dw_val_class
6585 AT_class (dw_attr_ref a)
6586 {
6587   return a->dw_attr_val.val_class;
6588 }
6589
6590 /* Add a flag value attribute to a DIE.  */
6591
6592 static inline void
6593 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
6594 {
6595   dw_attr_node attr;
6596
6597   attr.dw_attr = attr_kind;
6598   attr.dw_attr_val.val_class = dw_val_class_flag;
6599   attr.dw_attr_val.v.val_flag = flag;
6600   add_dwarf_attr (die, &attr);
6601 }
6602
6603 static inline unsigned
6604 AT_flag (dw_attr_ref a)
6605 {
6606   gcc_assert (a && AT_class (a) == dw_val_class_flag);
6607   return a->dw_attr_val.v.val_flag;
6608 }
6609
6610 /* Add a signed integer attribute value to a DIE.  */
6611
6612 static inline void
6613 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
6614 {
6615   dw_attr_node attr;
6616
6617   attr.dw_attr = attr_kind;
6618   attr.dw_attr_val.val_class = dw_val_class_const;
6619   attr.dw_attr_val.v.val_int = int_val;
6620   add_dwarf_attr (die, &attr);
6621 }
6622
6623 static inline HOST_WIDE_INT
6624 AT_int (dw_attr_ref a)
6625 {
6626   gcc_assert (a && AT_class (a) == dw_val_class_const);
6627   return a->dw_attr_val.v.val_int;
6628 }
6629
6630 /* Add an unsigned integer attribute value to a DIE.  */
6631
6632 static inline void
6633 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
6634                  unsigned HOST_WIDE_INT unsigned_val)
6635 {
6636   dw_attr_node attr;
6637
6638   attr.dw_attr = attr_kind;
6639   attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
6640   attr.dw_attr_val.v.val_unsigned = unsigned_val;
6641   add_dwarf_attr (die, &attr);
6642 }
6643
6644 static inline unsigned HOST_WIDE_INT
6645 AT_unsigned (dw_attr_ref a)
6646 {
6647   gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
6648   return a->dw_attr_val.v.val_unsigned;
6649 }
6650
6651 /* Add an unsigned double integer attribute value to a DIE.  */
6652
6653 static inline void
6654 add_AT_long_long (dw_die_ref die, enum dwarf_attribute attr_kind,
6655                   long unsigned int val_hi, long unsigned int val_low)
6656 {
6657   dw_attr_node attr;
6658
6659   attr.dw_attr = attr_kind;
6660   attr.dw_attr_val.val_class = dw_val_class_long_long;
6661   attr.dw_attr_val.v.val_long_long.hi = val_hi;
6662   attr.dw_attr_val.v.val_long_long.low = val_low;
6663   add_dwarf_attr (die, &attr);
6664 }
6665
6666 /* Add a floating point attribute value to a DIE and return it.  */
6667
6668 static inline void
6669 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
6670             unsigned int length, unsigned int elt_size, unsigned char *array)
6671 {
6672   dw_attr_node attr;
6673
6674   attr.dw_attr = attr_kind;
6675   attr.dw_attr_val.val_class = dw_val_class_vec;
6676   attr.dw_attr_val.v.val_vec.length = length;
6677   attr.dw_attr_val.v.val_vec.elt_size = elt_size;
6678   attr.dw_attr_val.v.val_vec.array = array;
6679   add_dwarf_attr (die, &attr);
6680 }
6681
6682 /* Hash and equality functions for debug_str_hash.  */
6683
6684 static hashval_t
6685 debug_str_do_hash (const void *x)
6686 {
6687   return htab_hash_string (((const struct indirect_string_node *)x)->str);
6688 }
6689
6690 static int
6691 debug_str_eq (const void *x1, const void *x2)
6692 {
6693   return strcmp ((((const struct indirect_string_node *)x1)->str),
6694                  (const char *)x2) == 0;
6695 }
6696
6697 static struct indirect_string_node *
6698 find_AT_string (const char *str)
6699 {
6700   struct indirect_string_node *node;
6701   void **slot;
6702
6703   if (! debug_str_hash)
6704     debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
6705                                       debug_str_eq, NULL);
6706
6707   slot = htab_find_slot_with_hash (debug_str_hash, str,
6708                                    htab_hash_string (str), INSERT);
6709   if (*slot == NULL)
6710     {
6711       node = (struct indirect_string_node *)
6712                ggc_alloc_cleared (sizeof (struct indirect_string_node));
6713       node->str = ggc_strdup (str);
6714       *slot = node;
6715     }
6716   else
6717     node = (struct indirect_string_node *) *slot;
6718
6719   node->refcount++;
6720   return node;
6721 }
6722
6723 /* Add a string attribute value to a DIE.  */
6724
6725 static inline void
6726 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
6727 {
6728   dw_attr_node attr;
6729   struct indirect_string_node *node;
6730
6731   node = find_AT_string (str);
6732
6733   attr.dw_attr = attr_kind;
6734   attr.dw_attr_val.val_class = dw_val_class_str;
6735   attr.dw_attr_val.v.val_str = node;
6736   add_dwarf_attr (die, &attr);
6737 }
6738
6739 static inline const char *
6740 AT_string (dw_attr_ref a)
6741 {
6742   gcc_assert (a && AT_class (a) == dw_val_class_str);
6743   return a->dw_attr_val.v.val_str->str;
6744 }
6745
6746 /* Find out whether a string should be output inline in DIE
6747    or out-of-line in .debug_str section.  */
6748
6749 static enum dwarf_form
6750 AT_string_form (dw_attr_ref a)
6751 {
6752   struct indirect_string_node *node;
6753   unsigned int len;
6754   char label[32];
6755
6756   gcc_assert (a && AT_class (a) == dw_val_class_str);
6757
6758   node = a->dw_attr_val.v.val_str;
6759   if (node->form)
6760     return node->form;
6761
6762   len = strlen (node->str) + 1;
6763
6764   /* If the string is shorter or equal to the size of the reference, it is
6765      always better to put it inline.  */
6766   if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
6767     return node->form = DW_FORM_string;
6768
6769   /* If we cannot expect the linker to merge strings in .debug_str
6770      section, only put it into .debug_str if it is worth even in this
6771      single module.  */
6772   if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
6773       || ((debug_str_section->common.flags & SECTION_MERGE) == 0
6774       && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
6775     return node->form = DW_FORM_string;
6776
6777   ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
6778   ++dw2_string_counter;
6779   node->label = xstrdup (label);
6780
6781   return node->form = DW_FORM_strp;
6782 }
6783
6784 /* Add a DIE reference attribute value to a DIE.  */
6785
6786 static inline void
6787 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
6788 {
6789   dw_attr_node attr;
6790
6791   attr.dw_attr = attr_kind;
6792   attr.dw_attr_val.val_class = dw_val_class_die_ref;
6793   attr.dw_attr_val.v.val_die_ref.die = targ_die;
6794   attr.dw_attr_val.v.val_die_ref.external = 0;
6795   add_dwarf_attr (die, &attr);
6796 }
6797
6798 /* Add an AT_specification attribute to a DIE, and also make the back
6799    pointer from the specification to the definition.  */
6800
6801 static inline void
6802 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
6803 {
6804   add_AT_die_ref (die, DW_AT_specification, targ_die);
6805   gcc_assert (!targ_die->die_definition);
6806   targ_die->die_definition = die;
6807 }
6808
6809 static inline dw_die_ref
6810 AT_ref (dw_attr_ref a)
6811 {
6812   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
6813   return a->dw_attr_val.v.val_die_ref.die;
6814 }
6815
6816 static inline int
6817 AT_ref_external (dw_attr_ref a)
6818 {
6819   if (a && AT_class (a) == dw_val_class_die_ref)
6820     return a->dw_attr_val.v.val_die_ref.external;
6821
6822   return 0;
6823 }
6824
6825 static inline void
6826 set_AT_ref_external (dw_attr_ref a, int i)
6827 {
6828   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
6829   a->dw_attr_val.v.val_die_ref.external = i;
6830 }
6831
6832 /* Add an FDE reference attribute value to a DIE.  */
6833
6834 static inline void
6835 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
6836 {
6837   dw_attr_node attr;
6838
6839   attr.dw_attr = attr_kind;
6840   attr.dw_attr_val.val_class = dw_val_class_fde_ref;
6841   attr.dw_attr_val.v.val_fde_index = targ_fde;
6842   add_dwarf_attr (die, &attr);
6843 }
6844
6845 /* Add a location description attribute value to a DIE.  */
6846
6847 static inline void
6848 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
6849 {
6850   dw_attr_node attr;
6851
6852   attr.dw_attr = attr_kind;
6853   attr.dw_attr_val.val_class = dw_val_class_loc;
6854   attr.dw_attr_val.v.val_loc = loc;
6855   add_dwarf_attr (die, &attr);
6856 }
6857
6858 static inline dw_loc_descr_ref
6859 AT_loc (dw_attr_ref a)
6860 {
6861   gcc_assert (a && AT_class (a) == dw_val_class_loc);
6862   return a->dw_attr_val.v.val_loc;
6863 }
6864
6865 static inline void
6866 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
6867 {
6868   dw_attr_node attr;
6869
6870   attr.dw_attr = attr_kind;
6871   attr.dw_attr_val.val_class = dw_val_class_loc_list;
6872   attr.dw_attr_val.v.val_loc_list = loc_list;
6873   add_dwarf_attr (die, &attr);
6874   have_location_lists = true;
6875 }
6876
6877 static inline dw_loc_list_ref
6878 AT_loc_list (dw_attr_ref a)
6879 {
6880   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
6881   return a->dw_attr_val.v.val_loc_list;
6882 }
6883
6884 /* Add an address constant attribute value to a DIE.  */
6885
6886 static inline void
6887 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
6888 {
6889   dw_attr_node attr;
6890
6891   attr.dw_attr = attr_kind;
6892   attr.dw_attr_val.val_class = dw_val_class_addr;
6893   attr.dw_attr_val.v.val_addr = addr;
6894   add_dwarf_attr (die, &attr);
6895 }
6896
6897 /* Get the RTX from to an address DIE attribute.  */
6898
6899 static inline rtx
6900 AT_addr (dw_attr_ref a)
6901 {
6902   gcc_assert (a && AT_class (a) == dw_val_class_addr);
6903   return a->dw_attr_val.v.val_addr;
6904 }
6905
6906 /* Add a file attribute value to a DIE.  */
6907
6908 static inline void
6909 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
6910              struct dwarf_file_data *fd)
6911 {
6912   dw_attr_node attr;
6913
6914   attr.dw_attr = attr_kind;
6915   attr.dw_attr_val.val_class = dw_val_class_file;
6916   attr.dw_attr_val.v.val_file = fd;
6917   add_dwarf_attr (die, &attr);
6918 }
6919
6920 /* Get the dwarf_file_data from a file DIE attribute.  */
6921
6922 static inline struct dwarf_file_data *
6923 AT_file (dw_attr_ref a)
6924 {
6925   gcc_assert (a && AT_class (a) == dw_val_class_file);
6926   return a->dw_attr_val.v.val_file;
6927 }
6928
6929 /* Add a label identifier attribute value to a DIE.  */
6930
6931 static inline void
6932 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
6933 {
6934   dw_attr_node attr;
6935
6936   attr.dw_attr = attr_kind;
6937   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
6938   attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
6939   add_dwarf_attr (die, &attr);
6940 }
6941
6942 /* Add a section offset attribute value to a DIE, an offset into the
6943    debug_line section.  */
6944
6945 static inline void
6946 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
6947                 const char *label)
6948 {
6949   dw_attr_node attr;
6950
6951   attr.dw_attr = attr_kind;
6952   attr.dw_attr_val.val_class = dw_val_class_lineptr;
6953   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
6954   add_dwarf_attr (die, &attr);
6955 }
6956
6957 /* Add a section offset attribute value to a DIE, an offset into the
6958    debug_macinfo section.  */
6959
6960 static inline void
6961 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
6962                const char *label)
6963 {
6964   dw_attr_node attr;
6965
6966   attr.dw_attr = attr_kind;
6967   attr.dw_attr_val.val_class = dw_val_class_macptr;
6968   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
6969   add_dwarf_attr (die, &attr);
6970 }
6971
6972 /* Add an offset attribute value to a DIE.  */
6973
6974 static inline void
6975 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
6976                unsigned HOST_WIDE_INT offset)
6977 {
6978   dw_attr_node attr;
6979
6980   attr.dw_attr = attr_kind;
6981   attr.dw_attr_val.val_class = dw_val_class_offset;
6982   attr.dw_attr_val.v.val_offset = offset;
6983   add_dwarf_attr (die, &attr);
6984 }
6985
6986 /* Add an range_list attribute value to a DIE.  */
6987
6988 static void
6989 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
6990                    long unsigned int offset)
6991 {
6992   dw_attr_node attr;
6993
6994   attr.dw_attr = attr_kind;
6995   attr.dw_attr_val.val_class = dw_val_class_range_list;
6996   attr.dw_attr_val.v.val_offset = offset;
6997   add_dwarf_attr (die, &attr);
6998 }
6999
7000 static inline const char *
7001 AT_lbl (dw_attr_ref a)
7002 {
7003   gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
7004                     || AT_class (a) == dw_val_class_lineptr
7005                     || AT_class (a) == dw_val_class_macptr));
7006   return a->dw_attr_val.v.val_lbl_id;
7007 }
7008
7009 /* Get the attribute of type attr_kind.  */
7010
7011 static dw_attr_ref
7012 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
7013 {
7014   dw_attr_ref a;
7015   unsigned ix;
7016   dw_die_ref spec = NULL;
7017
7018   if (! die)
7019     return NULL;
7020
7021   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7022     if (a->dw_attr == attr_kind)
7023       return a;
7024     else if (a->dw_attr == DW_AT_specification
7025              || a->dw_attr == DW_AT_abstract_origin)
7026       spec = AT_ref (a);
7027
7028   if (spec)
7029     return get_AT (spec, attr_kind);
7030
7031   return NULL;
7032 }
7033
7034 /* Return the "low pc" attribute value, typically associated with a subprogram
7035    DIE.  Return null if the "low pc" attribute is either not present, or if it
7036    cannot be represented as an assembler label identifier.  */
7037
7038 static inline const char *
7039 get_AT_low_pc (dw_die_ref die)
7040 {
7041   dw_attr_ref a = get_AT (die, DW_AT_low_pc);
7042
7043   return a ? AT_lbl (a) : NULL;
7044 }
7045
7046 /* Return the "high pc" attribute value, typically associated with a subprogram
7047    DIE.  Return null if the "high pc" attribute is either not present, or if it
7048    cannot be represented as an assembler label identifier.  */
7049
7050 static inline const char *
7051 get_AT_hi_pc (dw_die_ref die)
7052 {
7053   dw_attr_ref a = get_AT (die, DW_AT_high_pc);
7054
7055   return a ? AT_lbl (a) : NULL;
7056 }
7057
7058 /* Return the value of the string attribute designated by ATTR_KIND, or
7059    NULL if it is not present.  */
7060
7061 static inline const char *
7062 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
7063 {
7064   dw_attr_ref a = get_AT (die, attr_kind);
7065
7066   return a ? AT_string (a) : NULL;
7067 }
7068
7069 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
7070    if it is not present.  */
7071
7072 static inline int
7073 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
7074 {
7075   dw_attr_ref a = get_AT (die, attr_kind);
7076
7077   return a ? AT_flag (a) : 0;
7078 }
7079
7080 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
7081    if it is not present.  */
7082
7083 static inline unsigned
7084 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
7085 {
7086   dw_attr_ref a = get_AT (die, attr_kind);
7087
7088   return a ? AT_unsigned (a) : 0;
7089 }
7090
7091 static inline dw_die_ref
7092 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
7093 {
7094   dw_attr_ref a = get_AT (die, attr_kind);
7095
7096   return a ? AT_ref (a) : NULL;
7097 }
7098
7099 static inline struct dwarf_file_data *
7100 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
7101 {
7102   dw_attr_ref a = get_AT (die, attr_kind);
7103
7104   return a ? AT_file (a) : NULL;
7105 }
7106
7107 /* Return TRUE if the language is C or C++.  */
7108
7109 static inline bool
7110 is_c_family (void)
7111 {
7112   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
7113
7114   return (lang == DW_LANG_C || lang == DW_LANG_C89 || lang == DW_LANG_ObjC
7115           || lang == DW_LANG_C99
7116           || lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus);
7117 }
7118
7119 /* Return TRUE if the language is C++.  */
7120
7121 static inline bool
7122 is_cxx (void)
7123 {
7124   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
7125
7126   return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
7127 }
7128
7129 /* Return TRUE if the language is Fortran.  */
7130
7131 static inline bool
7132 is_fortran (void)
7133 {
7134   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
7135
7136   return (lang == DW_LANG_Fortran77
7137           || lang == DW_LANG_Fortran90
7138           || lang == DW_LANG_Fortran95);
7139 }
7140
7141 /* Return TRUE if the language is Java.  */
7142
7143 static inline bool
7144 is_java (void)
7145 {
7146   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
7147
7148   return lang == DW_LANG_Java;
7149 }
7150
7151 /* Return TRUE if the language is Ada.  */
7152
7153 static inline bool
7154 is_ada (void)
7155 {
7156   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
7157
7158   return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
7159 }
7160
7161 /* Remove the specified attribute if present.  */
7162
7163 static void
7164 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
7165 {
7166   dw_attr_ref a;
7167   unsigned ix;
7168
7169   if (! die)
7170     return;
7171
7172   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7173     if (a->dw_attr == attr_kind)
7174       {
7175         if (AT_class (a) == dw_val_class_str)
7176           if (a->dw_attr_val.v.val_str->refcount)
7177             a->dw_attr_val.v.val_str->refcount--;
7178
7179         /* VEC_ordered_remove should help reduce the number of abbrevs
7180            that are needed.  */
7181         VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
7182         return;
7183       }
7184 }
7185
7186 /* Remove CHILD from its parent.  PREV must have the property that
7187    PREV->DIE_SIB == CHILD.  Does not alter CHILD.  */
7188
7189 static void
7190 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
7191 {
7192   gcc_assert (child->die_parent == prev->die_parent);
7193   gcc_assert (prev->die_sib == child);
7194   if (prev == child)
7195     {
7196       gcc_assert (child->die_parent->die_child == child);
7197       prev = NULL;
7198     }
7199   else
7200     prev->die_sib = child->die_sib;
7201   if (child->die_parent->die_child == child)
7202     child->die_parent->die_child = prev;
7203 }
7204
7205 /* Remove child DIE whose die_tag is TAG.  Do nothing if no child
7206    matches TAG.  */
7207
7208 static void
7209 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
7210 {
7211   dw_die_ref c;
7212
7213   c = die->die_child;
7214   if (c) do {
7215     dw_die_ref prev = c;
7216     c = c->die_sib;
7217     while (c->die_tag == tag)
7218       {
7219         remove_child_with_prev (c, prev);
7220         /* Might have removed every child.  */
7221         if (c == c->die_sib)
7222           return;
7223         c = c->die_sib;
7224       }
7225   } while (c != die->die_child);
7226 }
7227
7228 /* Add a CHILD_DIE as the last child of DIE.  */
7229
7230 static void
7231 add_child_die (dw_die_ref die, dw_die_ref child_die)
7232 {
7233   /* FIXME this should probably be an assert.  */
7234   if (! die || ! child_die)
7235     return;
7236   gcc_assert (die != child_die);
7237
7238   child_die->die_parent = die;
7239   if (die->die_child)
7240     {
7241       child_die->die_sib = die->die_child->die_sib;
7242       die->die_child->die_sib = child_die;
7243     }
7244   else
7245     child_die->die_sib = child_die;
7246   die->die_child = child_die;
7247 }
7248
7249 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
7250    is the specification, to the end of PARENT's list of children.
7251    This is done by removing and re-adding it.  */
7252
7253 static void
7254 splice_child_die (dw_die_ref parent, dw_die_ref child)
7255 {
7256   dw_die_ref p;
7257
7258   /* We want the declaration DIE from inside the class, not the
7259      specification DIE at toplevel.  */
7260   if (child->die_parent != parent)
7261     {
7262       dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
7263
7264       if (tmp)
7265         child = tmp;
7266     }
7267
7268   gcc_assert (child->die_parent == parent
7269               || (child->die_parent
7270                   == get_AT_ref (parent, DW_AT_specification)));
7271
7272   for (p = child->die_parent->die_child; ; p = p->die_sib)
7273     if (p->die_sib == child)
7274       {
7275         remove_child_with_prev (child, p);
7276         break;
7277       }
7278
7279   add_child_die (parent, child);
7280 }
7281
7282 /* Return a pointer to a newly created DIE node.  */
7283
7284 static inline dw_die_ref
7285 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
7286 {
7287   dw_die_ref die = GGC_CNEW (die_node);
7288
7289   die->die_tag = tag_value;
7290
7291   if (parent_die != NULL)
7292     add_child_die (parent_die, die);
7293   else
7294     {
7295       limbo_die_node *limbo_node;
7296
7297       limbo_node = GGC_CNEW (limbo_die_node);
7298       limbo_node->die = die;
7299       limbo_node->created_for = t;
7300       limbo_node->next = limbo_die_list;
7301       limbo_die_list = limbo_node;
7302     }
7303
7304   return die;
7305 }
7306
7307 /* Return the DIE associated with the given type specifier.  */
7308
7309 static inline dw_die_ref
7310 lookup_type_die (tree type)
7311 {
7312   return TYPE_SYMTAB_DIE (type);
7313 }
7314
7315 /* Equate a DIE to a given type specifier.  */
7316
7317 static inline void
7318 equate_type_number_to_die (tree type, dw_die_ref type_die)
7319 {
7320   TYPE_SYMTAB_DIE (type) = type_die;
7321 }
7322
7323 /* Returns a hash value for X (which really is a die_struct).  */
7324
7325 static hashval_t
7326 decl_die_table_hash (const void *x)
7327 {
7328   return (hashval_t) ((const_dw_die_ref) x)->decl_id;
7329 }
7330
7331 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
7332
7333 static int
7334 decl_die_table_eq (const void *x, const void *y)
7335 {
7336   return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
7337 }
7338
7339 /* Return the DIE associated with a given declaration.  */
7340
7341 static inline dw_die_ref
7342 lookup_decl_die (tree decl)
7343 {
7344   return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
7345 }
7346
7347 /* Returns a hash value for X (which really is a var_loc_list).  */
7348
7349 static hashval_t
7350 decl_loc_table_hash (const void *x)
7351 {
7352   return (hashval_t) ((const var_loc_list *) x)->decl_id;
7353 }
7354
7355 /* Return nonzero if decl_id of var_loc_list X is the same as
7356    UID of decl *Y.  */
7357
7358 static int
7359 decl_loc_table_eq (const void *x, const void *y)
7360 {
7361   return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
7362 }
7363
7364 /* Return the var_loc list associated with a given declaration.  */
7365
7366 static inline var_loc_list *
7367 lookup_decl_loc (const_tree decl)
7368 {
7369   return (var_loc_list *)
7370     htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
7371 }
7372
7373 /* Equate a DIE to a particular declaration.  */
7374
7375 static void
7376 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
7377 {
7378   unsigned int decl_id = DECL_UID (decl);
7379   void **slot;
7380
7381   slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
7382   *slot = decl_die;
7383   decl_die->decl_id = decl_id;
7384 }
7385
7386 /* Add a variable location node to the linked list for DECL.  */
7387
7388 static void
7389 add_var_loc_to_decl (tree decl, struct var_loc_node *loc)
7390 {
7391   unsigned int decl_id = DECL_UID (decl);
7392   var_loc_list *temp;
7393   void **slot;
7394
7395   slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
7396   if (*slot == NULL)
7397     {
7398       temp = GGC_CNEW (var_loc_list);
7399       temp->decl_id = decl_id;
7400       *slot = temp;
7401     }
7402   else
7403     temp = (var_loc_list *) *slot;
7404
7405   if (temp->last)
7406     {
7407       /* If the current location is the same as the end of the list,
7408          and either both or neither of the locations is uninitialized,
7409          we have nothing to do.  */
7410       if ((!rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->last->var_loc_note),
7411                          NOTE_VAR_LOCATION_LOC (loc->var_loc_note)))
7412           || ((NOTE_VAR_LOCATION_STATUS (temp->last->var_loc_note)
7413                != NOTE_VAR_LOCATION_STATUS (loc->var_loc_note))
7414               && ((NOTE_VAR_LOCATION_STATUS (temp->last->var_loc_note)
7415                    == VAR_INIT_STATUS_UNINITIALIZED)
7416                   || (NOTE_VAR_LOCATION_STATUS (loc->var_loc_note)
7417                       == VAR_INIT_STATUS_UNINITIALIZED))))
7418         {
7419           /* Add LOC to the end of list and update LAST.  */
7420           temp->last->next = loc;
7421           temp->last = loc;
7422         }
7423     }
7424   /* Do not add empty location to the beginning of the list.  */
7425   else if (NOTE_VAR_LOCATION_LOC (loc->var_loc_note) != NULL_RTX)
7426     {
7427       temp->first = loc;
7428       temp->last = loc;
7429     }
7430 }
7431 \f
7432 /* Keep track of the number of spaces used to indent the
7433    output of the debugging routines that print the structure of
7434    the DIE internal representation.  */
7435 static int print_indent;
7436
7437 /* Indent the line the number of spaces given by print_indent.  */
7438
7439 static inline void
7440 print_spaces (FILE *outfile)
7441 {
7442   fprintf (outfile, "%*s", print_indent, "");
7443 }
7444
7445 /* Print the information associated with a given DIE, and its children.
7446    This routine is a debugging aid only.  */
7447
7448 static void
7449 print_die (dw_die_ref die, FILE *outfile)
7450 {
7451   dw_attr_ref a;
7452   dw_die_ref c;
7453   unsigned ix;
7454
7455   print_spaces (outfile);
7456   fprintf (outfile, "DIE %4ld: %s\n",
7457            die->die_offset, dwarf_tag_name (die->die_tag));
7458   print_spaces (outfile);
7459   fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
7460   fprintf (outfile, " offset: %ld\n", die->die_offset);
7461
7462   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7463     {
7464       print_spaces (outfile);
7465       fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
7466
7467       switch (AT_class (a))
7468         {
7469         case dw_val_class_addr:
7470           fprintf (outfile, "address");
7471           break;
7472         case dw_val_class_offset:
7473           fprintf (outfile, "offset");
7474           break;
7475         case dw_val_class_loc:
7476           fprintf (outfile, "location descriptor");
7477           break;
7478         case dw_val_class_loc_list:
7479           fprintf (outfile, "location list -> label:%s",
7480                    AT_loc_list (a)->ll_symbol);
7481           break;
7482         case dw_val_class_range_list:
7483           fprintf (outfile, "range list");
7484           break;
7485         case dw_val_class_const:
7486           fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
7487           break;
7488         case dw_val_class_unsigned_const:
7489           fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
7490           break;
7491         case dw_val_class_long_long:
7492           fprintf (outfile, "constant (%lu,%lu)",
7493                    a->dw_attr_val.v.val_long_long.hi,
7494                    a->dw_attr_val.v.val_long_long.low);
7495           break;
7496         case dw_val_class_vec:
7497           fprintf (outfile, "floating-point or vector constant");
7498           break;
7499         case dw_val_class_flag:
7500           fprintf (outfile, "%u", AT_flag (a));
7501           break;
7502         case dw_val_class_die_ref:
7503           if (AT_ref (a) != NULL)
7504             {
7505               if (AT_ref (a)->die_symbol)
7506                 fprintf (outfile, "die -> label: %s", AT_ref (a)->die_symbol);
7507               else
7508                 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
7509             }
7510           else
7511             fprintf (outfile, "die -> <null>");
7512           break;
7513         case dw_val_class_lbl_id:
7514         case dw_val_class_lineptr:
7515         case dw_val_class_macptr:
7516           fprintf (outfile, "label: %s", AT_lbl (a));
7517           break;
7518         case dw_val_class_str:
7519           if (AT_string (a) != NULL)
7520             fprintf (outfile, "\"%s\"", AT_string (a));
7521           else
7522             fprintf (outfile, "<null>");
7523           break;
7524         case dw_val_class_file:
7525           fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
7526                    AT_file (a)->emitted_number);
7527           break;
7528         default:
7529           break;
7530         }
7531
7532       fprintf (outfile, "\n");
7533     }
7534
7535   if (die->die_child != NULL)
7536     {
7537       print_indent += 4;
7538       FOR_EACH_CHILD (die, c, print_die (c, outfile));
7539       print_indent -= 4;
7540     }
7541   if (print_indent == 0)
7542     fprintf (outfile, "\n");
7543 }
7544
7545 /* Print the contents of the source code line number correspondence table.
7546    This routine is a debugging aid only.  */
7547
7548 static void
7549 print_dwarf_line_table (FILE *outfile)
7550 {
7551   unsigned i;
7552   dw_line_info_ref line_info;
7553
7554   fprintf (outfile, "\n\nDWARF source line information\n");
7555   for (i = 1; i < line_info_table_in_use; i++)
7556     {
7557       line_info = &line_info_table[i];
7558       fprintf (outfile, "%5d: %4ld %6ld\n", i,
7559                line_info->dw_file_num,
7560                line_info->dw_line_num);
7561     }
7562
7563   fprintf (outfile, "\n\n");
7564 }
7565
7566 /* Print the information collected for a given DIE.  */
7567
7568 void
7569 debug_dwarf_die (dw_die_ref die)
7570 {
7571   print_die (die, stderr);
7572 }
7573
7574 /* Print all DWARF information collected for the compilation unit.
7575    This routine is a debugging aid only.  */
7576
7577 void
7578 debug_dwarf (void)
7579 {
7580   print_indent = 0;
7581   print_die (comp_unit_die, stderr);
7582   if (! DWARF2_ASM_LINE_DEBUG_INFO)
7583     print_dwarf_line_table (stderr);
7584 }
7585 \f
7586 /* Start a new compilation unit DIE for an include file.  OLD_UNIT is the CU
7587    for the enclosing include file, if any.  BINCL_DIE is the DW_TAG_GNU_BINCL
7588    DIE that marks the start of the DIEs for this include file.  */
7589
7590 static dw_die_ref
7591 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
7592 {
7593   const char *filename = get_AT_string (bincl_die, DW_AT_name);
7594   dw_die_ref new_unit = gen_compile_unit_die (filename);
7595
7596   new_unit->die_sib = old_unit;
7597   return new_unit;
7598 }
7599
7600 /* Close an include-file CU and reopen the enclosing one.  */
7601
7602 static dw_die_ref
7603 pop_compile_unit (dw_die_ref old_unit)
7604 {
7605   dw_die_ref new_unit = old_unit->die_sib;
7606
7607   old_unit->die_sib = NULL;
7608   return new_unit;
7609 }
7610
7611 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
7612 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
7613
7614 /* Calculate the checksum of a location expression.  */
7615
7616 static inline void
7617 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
7618 {
7619   int tem;
7620
7621   tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
7622   CHECKSUM (tem);
7623   CHECKSUM (loc->dw_loc_oprnd1);
7624   CHECKSUM (loc->dw_loc_oprnd2);
7625 }
7626
7627 /* Calculate the checksum of an attribute.  */
7628
7629 static void
7630 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
7631 {
7632   dw_loc_descr_ref loc;
7633   rtx r;
7634
7635   CHECKSUM (at->dw_attr);
7636
7637   /* We don't care that this was compiled with a different compiler
7638      snapshot; if the output is the same, that's what matters.  */
7639   if (at->dw_attr == DW_AT_producer)
7640     return;
7641
7642   switch (AT_class (at))
7643     {
7644     case dw_val_class_const:
7645       CHECKSUM (at->dw_attr_val.v.val_int);
7646       break;
7647     case dw_val_class_unsigned_const:
7648       CHECKSUM (at->dw_attr_val.v.val_unsigned);
7649       break;
7650     case dw_val_class_long_long:
7651       CHECKSUM (at->dw_attr_val.v.val_long_long);
7652       break;
7653     case dw_val_class_vec:
7654       CHECKSUM (at->dw_attr_val.v.val_vec);
7655       break;
7656     case dw_val_class_flag:
7657       CHECKSUM (at->dw_attr_val.v.val_flag);
7658       break;
7659     case dw_val_class_str:
7660       CHECKSUM_STRING (AT_string (at));
7661       break;
7662
7663     case dw_val_class_addr:
7664       r = AT_addr (at);
7665       gcc_assert (GET_CODE (r) == SYMBOL_REF);
7666       CHECKSUM_STRING (XSTR (r, 0));
7667       break;
7668
7669     case dw_val_class_offset:
7670       CHECKSUM (at->dw_attr_val.v.val_offset);
7671       break;
7672
7673     case dw_val_class_loc:
7674       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
7675         loc_checksum (loc, ctx);
7676       break;
7677
7678     case dw_val_class_die_ref:
7679       die_checksum (AT_ref (at), ctx, mark);
7680       break;
7681
7682     case dw_val_class_fde_ref:
7683     case dw_val_class_lbl_id:
7684     case dw_val_class_lineptr:
7685     case dw_val_class_macptr:
7686       break;
7687
7688     case dw_val_class_file:
7689       CHECKSUM_STRING (AT_file (at)->filename);
7690       break;
7691
7692     default:
7693       break;
7694     }
7695 }
7696
7697 /* Calculate the checksum of a DIE.  */
7698
7699 static void
7700 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
7701 {
7702   dw_die_ref c;
7703   dw_attr_ref a;
7704   unsigned ix;
7705
7706   /* To avoid infinite recursion.  */
7707   if (die->die_mark)
7708     {
7709       CHECKSUM (die->die_mark);
7710       return;
7711     }
7712   die->die_mark = ++(*mark);
7713
7714   CHECKSUM (die->die_tag);
7715
7716   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7717     attr_checksum (a, ctx, mark);
7718
7719   FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
7720 }
7721
7722 #undef CHECKSUM
7723 #undef CHECKSUM_STRING
7724
7725 /* Do the location expressions look same?  */
7726 static inline int
7727 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
7728 {
7729   return loc1->dw_loc_opc == loc2->dw_loc_opc
7730          && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
7731          && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
7732 }
7733
7734 /* Do the values look the same?  */
7735 static int
7736 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
7737 {
7738   dw_loc_descr_ref loc1, loc2;
7739   rtx r1, r2;
7740
7741   if (v1->val_class != v2->val_class)
7742     return 0;
7743
7744   switch (v1->val_class)
7745     {
7746     case dw_val_class_const:
7747       return v1->v.val_int == v2->v.val_int;
7748     case dw_val_class_unsigned_const:
7749       return v1->v.val_unsigned == v2->v.val_unsigned;
7750     case dw_val_class_long_long:
7751       return v1->v.val_long_long.hi == v2->v.val_long_long.hi
7752              && v1->v.val_long_long.low == v2->v.val_long_long.low;
7753     case dw_val_class_vec:
7754       if (v1->v.val_vec.length != v2->v.val_vec.length
7755           || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
7756         return 0;
7757       if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
7758                   v1->v.val_vec.length * v1->v.val_vec.elt_size))
7759         return 0;
7760       return 1;
7761     case dw_val_class_flag:
7762       return v1->v.val_flag == v2->v.val_flag;
7763     case dw_val_class_str:
7764       return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
7765
7766     case dw_val_class_addr:
7767       r1 = v1->v.val_addr;
7768       r2 = v2->v.val_addr;
7769       if (GET_CODE (r1) != GET_CODE (r2))
7770         return 0;
7771       gcc_assert (GET_CODE (r1) == SYMBOL_REF);
7772       return !strcmp (XSTR (r1, 0), XSTR (r2, 0));
7773
7774     case dw_val_class_offset:
7775       return v1->v.val_offset == v2->v.val_offset;
7776
7777     case dw_val_class_loc:
7778       for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
7779            loc1 && loc2;
7780            loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
7781         if (!same_loc_p (loc1, loc2, mark))
7782           return 0;
7783       return !loc1 && !loc2;
7784
7785     case dw_val_class_die_ref:
7786       return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
7787
7788     case dw_val_class_fde_ref:
7789     case dw_val_class_lbl_id:
7790     case dw_val_class_lineptr:
7791     case dw_val_class_macptr:
7792       return 1;
7793
7794     case dw_val_class_file:
7795       return v1->v.val_file == v2->v.val_file;
7796
7797     default:
7798       return 1;
7799     }
7800 }
7801
7802 /* Do the attributes look the same?  */
7803
7804 static int
7805 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
7806 {
7807   if (at1->dw_attr != at2->dw_attr)
7808     return 0;
7809
7810   /* We don't care that this was compiled with a different compiler
7811      snapshot; if the output is the same, that's what matters. */
7812   if (at1->dw_attr == DW_AT_producer)
7813     return 1;
7814
7815   return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
7816 }
7817
7818 /* Do the dies look the same?  */
7819
7820 static int
7821 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
7822 {
7823   dw_die_ref c1, c2;
7824   dw_attr_ref a1;
7825   unsigned ix;
7826
7827   /* To avoid infinite recursion.  */
7828   if (die1->die_mark)
7829     return die1->die_mark == die2->die_mark;
7830   die1->die_mark = die2->die_mark = ++(*mark);
7831
7832   if (die1->die_tag != die2->die_tag)
7833     return 0;
7834
7835   if (VEC_length (dw_attr_node, die1->die_attr)
7836       != VEC_length (dw_attr_node, die2->die_attr))
7837     return 0;
7838
7839   for (ix = 0; VEC_iterate (dw_attr_node, die1->die_attr, ix, a1); ix++)
7840     if (!same_attr_p (a1, VEC_index (dw_attr_node, die2->die_attr, ix), mark))
7841       return 0;
7842
7843   c1 = die1->die_child;
7844   c2 = die2->die_child;
7845   if (! c1)
7846     {
7847       if (c2)
7848         return 0;
7849     }
7850   else
7851     for (;;)
7852       {
7853         if (!same_die_p (c1, c2, mark))
7854           return 0;
7855         c1 = c1->die_sib;
7856         c2 = c2->die_sib;
7857         if (c1 == die1->die_child)
7858           {
7859             if (c2 == die2->die_child)
7860               break;
7861             else
7862               return 0;
7863           }
7864     }
7865
7866   return 1;
7867 }
7868
7869 /* Do the dies look the same?  Wrapper around same_die_p.  */
7870
7871 static int
7872 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
7873 {
7874   int mark = 0;
7875   int ret = same_die_p (die1, die2, &mark);
7876
7877   unmark_all_dies (die1);
7878   unmark_all_dies (die2);
7879
7880   return ret;
7881 }
7882
7883 /* The prefix to attach to symbols on DIEs in the current comdat debug
7884    info section.  */
7885 static char *comdat_symbol_id;
7886
7887 /* The index of the current symbol within the current comdat CU.  */
7888 static unsigned int comdat_symbol_number;
7889
7890 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
7891    children, and set comdat_symbol_id accordingly.  */
7892
7893 static void
7894 compute_section_prefix (dw_die_ref unit_die)
7895 {
7896   const char *die_name = get_AT_string (unit_die, DW_AT_name);
7897   const char *base = die_name ? lbasename (die_name) : "anonymous";
7898   char *name = XALLOCAVEC (char, strlen (base) + 64);
7899   char *p;
7900   int i, mark;
7901   unsigned char checksum[16];
7902   struct md5_ctx ctx;
7903
7904   /* Compute the checksum of the DIE, then append part of it as hex digits to
7905      the name filename of the unit.  */
7906
7907   md5_init_ctx (&ctx);
7908   mark = 0;
7909   die_checksum (unit_die, &ctx, &mark);
7910   unmark_all_dies (unit_die);
7911   md5_finish_ctx (&ctx, checksum);
7912
7913   sprintf (name, "%s.", base);
7914   clean_symbol_name (name);
7915
7916   p = name + strlen (name);
7917   for (i = 0; i < 4; i++)
7918     {
7919       sprintf (p, "%.2x", checksum[i]);
7920       p += 2;
7921     }
7922
7923   comdat_symbol_id = unit_die->die_symbol = xstrdup (name);
7924   comdat_symbol_number = 0;
7925 }
7926
7927 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P.  */
7928
7929 static int
7930 is_type_die (dw_die_ref die)
7931 {
7932   switch (die->die_tag)
7933     {
7934     case DW_TAG_array_type:
7935     case DW_TAG_class_type:
7936     case DW_TAG_interface_type:
7937     case DW_TAG_enumeration_type:
7938     case DW_TAG_pointer_type:
7939     case DW_TAG_reference_type:
7940     case DW_TAG_string_type:
7941     case DW_TAG_structure_type:
7942     case DW_TAG_subroutine_type:
7943     case DW_TAG_union_type:
7944     case DW_TAG_ptr_to_member_type:
7945     case DW_TAG_set_type:
7946     case DW_TAG_subrange_type:
7947     case DW_TAG_base_type:
7948     case DW_TAG_const_type:
7949     case DW_TAG_file_type:
7950     case DW_TAG_packed_type:
7951     case DW_TAG_volatile_type:
7952     case DW_TAG_typedef:
7953       return 1;
7954     default:
7955       return 0;
7956     }
7957 }
7958
7959 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
7960    Basically, we want to choose the bits that are likely to be shared between
7961    compilations (types) and leave out the bits that are specific to individual
7962    compilations (functions).  */
7963
7964 static int
7965 is_comdat_die (dw_die_ref c)
7966 {
7967   /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
7968      we do for stabs.  The advantage is a greater likelihood of sharing between
7969      objects that don't include headers in the same order (and therefore would
7970      put the base types in a different comdat).  jason 8/28/00 */
7971
7972   if (c->die_tag == DW_TAG_base_type)
7973     return 0;
7974
7975   if (c->die_tag == DW_TAG_pointer_type
7976       || c->die_tag == DW_TAG_reference_type
7977       || c->die_tag == DW_TAG_const_type
7978       || c->die_tag == DW_TAG_volatile_type)
7979     {
7980       dw_die_ref t = get_AT_ref (c, DW_AT_type);
7981
7982       return t ? is_comdat_die (t) : 0;
7983     }
7984
7985   return is_type_die (c);
7986 }
7987
7988 /* Returns 1 iff C is the sort of DIE that might be referred to from another
7989    compilation unit.  */
7990
7991 static int
7992 is_symbol_die (dw_die_ref c)
7993 {
7994   return (is_type_die (c)
7995           || (get_AT (c, DW_AT_declaration)
7996               && !get_AT (c, DW_AT_specification))
7997           || c->die_tag == DW_TAG_namespace
7998           || c->die_tag == DW_TAG_module);
7999 }
8000
8001 static char *
8002 gen_internal_sym (const char *prefix)
8003 {
8004   char buf[256];
8005
8006   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
8007   return xstrdup (buf);
8008 }
8009
8010 /* Assign symbols to all worthy DIEs under DIE.  */
8011
8012 static void
8013 assign_symbol_names (dw_die_ref die)
8014 {
8015   dw_die_ref c;
8016
8017   if (is_symbol_die (die))
8018     {
8019       if (comdat_symbol_id)
8020         {
8021           char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
8022
8023           sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
8024                    comdat_symbol_id, comdat_symbol_number++);
8025           die->die_symbol = xstrdup (p);
8026         }
8027       else
8028         die->die_symbol = gen_internal_sym ("LDIE");
8029     }
8030
8031   FOR_EACH_CHILD (die, c, assign_symbol_names (c));
8032 }
8033
8034 struct cu_hash_table_entry
8035 {
8036   dw_die_ref cu;
8037   unsigned min_comdat_num, max_comdat_num;
8038   struct cu_hash_table_entry *next;
8039 };
8040
8041 /* Routines to manipulate hash table of CUs.  */
8042 static hashval_t
8043 htab_cu_hash (const void *of)
8044 {
8045   const struct cu_hash_table_entry *const entry =
8046     (const struct cu_hash_table_entry *) of;
8047
8048   return htab_hash_string (entry->cu->die_symbol);
8049 }
8050
8051 static int
8052 htab_cu_eq (const void *of1, const void *of2)
8053 {
8054   const struct cu_hash_table_entry *const entry1 =
8055     (const struct cu_hash_table_entry *) of1;
8056   const struct die_struct *const entry2 = (const struct die_struct *) of2;
8057
8058   return !strcmp (entry1->cu->die_symbol, entry2->die_symbol);
8059 }
8060
8061 static void
8062 htab_cu_del (void *what)
8063 {
8064   struct cu_hash_table_entry *next,
8065     *entry = (struct cu_hash_table_entry *) what;
8066
8067   while (entry)
8068     {
8069       next = entry->next;
8070       free (entry);
8071       entry = next;
8072     }
8073 }
8074
8075 /* Check whether we have already seen this CU and set up SYM_NUM
8076    accordingly.  */
8077 static int
8078 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
8079 {
8080   struct cu_hash_table_entry dummy;
8081   struct cu_hash_table_entry **slot, *entry, *last = &dummy;
8082
8083   dummy.max_comdat_num = 0;
8084
8085   slot = (struct cu_hash_table_entry **)
8086     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
8087         INSERT);
8088   entry = *slot;
8089
8090   for (; entry; last = entry, entry = entry->next)
8091     {
8092       if (same_die_p_wrap (cu, entry->cu))
8093         break;
8094     }
8095
8096   if (entry)
8097     {
8098       *sym_num = entry->min_comdat_num;
8099       return 1;
8100     }
8101
8102   entry = XCNEW (struct cu_hash_table_entry);
8103   entry->cu = cu;
8104   entry->min_comdat_num = *sym_num = last->max_comdat_num;
8105   entry->next = *slot;
8106   *slot = entry;
8107
8108   return 0;
8109 }
8110
8111 /* Record SYM_NUM to record of CU in HTABLE.  */
8112 static void
8113 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
8114 {
8115   struct cu_hash_table_entry **slot, *entry;
8116
8117   slot = (struct cu_hash_table_entry **)
8118     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
8119         NO_INSERT);
8120   entry = *slot;
8121
8122   entry->max_comdat_num = sym_num;
8123 }
8124
8125 /* Traverse the DIE (which is always comp_unit_die), and set up
8126    additional compilation units for each of the include files we see
8127    bracketed by BINCL/EINCL.  */
8128
8129 static void
8130 break_out_includes (dw_die_ref die)
8131 {
8132   dw_die_ref c;
8133   dw_die_ref unit = NULL;
8134   limbo_die_node *node, **pnode;
8135   htab_t cu_hash_table;
8136
8137   c = die->die_child;
8138   if (c) do {
8139     dw_die_ref prev = c;
8140     c = c->die_sib;
8141     while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
8142            || (unit && is_comdat_die (c)))
8143       {
8144         dw_die_ref next = c->die_sib;
8145
8146         /* This DIE is for a secondary CU; remove it from the main one.  */
8147         remove_child_with_prev (c, prev);
8148
8149         if (c->die_tag == DW_TAG_GNU_BINCL)
8150           unit = push_new_compile_unit (unit, c);
8151         else if (c->die_tag == DW_TAG_GNU_EINCL)
8152           unit = pop_compile_unit (unit);
8153         else
8154           add_child_die (unit, c);
8155         c = next;
8156         if (c == die->die_child)
8157           break;
8158       }
8159   } while (c != die->die_child);
8160
8161 #if 0
8162   /* We can only use this in debugging, since the frontend doesn't check
8163      to make sure that we leave every include file we enter.  */
8164   gcc_assert (!unit);
8165 #endif
8166
8167   assign_symbol_names (die);
8168   cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
8169   for (node = limbo_die_list, pnode = &limbo_die_list;
8170        node;
8171        node = node->next)
8172     {
8173       int is_dupl;
8174
8175       compute_section_prefix (node->die);
8176       is_dupl = check_duplicate_cu (node->die, cu_hash_table,
8177                         &comdat_symbol_number);
8178       assign_symbol_names (node->die);
8179       if (is_dupl)
8180         *pnode = node->next;
8181       else
8182         {
8183           pnode = &node->next;
8184           record_comdat_symbol_number (node->die, cu_hash_table,
8185                 comdat_symbol_number);
8186         }
8187     }
8188   htab_delete (cu_hash_table);
8189 }
8190
8191 /* Traverse the DIE and add a sibling attribute if it may have the
8192    effect of speeding up access to siblings.  To save some space,
8193    avoid generating sibling attributes for DIE's without children.  */
8194
8195 static void
8196 add_sibling_attributes (dw_die_ref die)
8197 {
8198   dw_die_ref c;
8199
8200   if (! die->die_child)
8201     return;
8202
8203   if (die->die_parent && die != die->die_parent->die_child)
8204     add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
8205
8206   FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
8207 }
8208
8209 /* Output all location lists for the DIE and its children.  */
8210
8211 static void
8212 output_location_lists (dw_die_ref die)
8213 {
8214   dw_die_ref c;
8215   dw_attr_ref a;
8216   unsigned ix;
8217
8218   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
8219     if (AT_class (a) == dw_val_class_loc_list)
8220       output_loc_list (AT_loc_list (a));
8221
8222   FOR_EACH_CHILD (die, c, output_location_lists (c));
8223 }
8224
8225 /* The format of each DIE (and its attribute value pairs) is encoded in an
8226    abbreviation table.  This routine builds the abbreviation table and assigns
8227    a unique abbreviation id for each abbreviation entry.  The children of each
8228    die are visited recursively.  */
8229
8230 static void
8231 build_abbrev_table (dw_die_ref die)
8232 {
8233   unsigned long abbrev_id;
8234   unsigned int n_alloc;
8235   dw_die_ref c;
8236   dw_attr_ref a;
8237   unsigned ix;
8238
8239   /* Scan the DIE references, and mark as external any that refer to
8240      DIEs from other CUs (i.e. those which are not marked).  */
8241   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
8242     if (AT_class (a) == dw_val_class_die_ref
8243         && AT_ref (a)->die_mark == 0)
8244       {
8245         gcc_assert (AT_ref (a)->die_symbol);
8246         set_AT_ref_external (a, 1);
8247       }
8248
8249   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8250     {
8251       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
8252       dw_attr_ref die_a, abbrev_a;
8253       unsigned ix;
8254       bool ok = true;
8255
8256       if (abbrev->die_tag != die->die_tag)
8257         continue;
8258       if ((abbrev->die_child != NULL) != (die->die_child != NULL))
8259         continue;
8260
8261       if (VEC_length (dw_attr_node, abbrev->die_attr)
8262           != VEC_length (dw_attr_node, die->die_attr))
8263         continue;
8264
8265       for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, die_a); ix++)
8266         {
8267           abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
8268           if ((abbrev_a->dw_attr != die_a->dw_attr)
8269               || (value_format (abbrev_a) != value_format (die_a)))
8270             {
8271               ok = false;
8272               break;
8273             }
8274         }
8275       if (ok)
8276         break;
8277     }
8278
8279   if (abbrev_id >= abbrev_die_table_in_use)
8280     {
8281       if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
8282         {
8283           n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
8284           abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
8285                                             n_alloc);
8286
8287           memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
8288                  (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
8289           abbrev_die_table_allocated = n_alloc;
8290         }
8291
8292       ++abbrev_die_table_in_use;
8293       abbrev_die_table[abbrev_id] = die;
8294     }
8295
8296   die->die_abbrev = abbrev_id;
8297   FOR_EACH_CHILD (die, c, build_abbrev_table (c));
8298 }
8299 \f
8300 /* Return the power-of-two number of bytes necessary to represent VALUE.  */
8301
8302 static int
8303 constant_size (unsigned HOST_WIDE_INT value)
8304 {
8305   int log;
8306
8307   if (value == 0)
8308     log = 0;
8309   else
8310     log = floor_log2 (value);
8311
8312   log = log / 8;
8313   log = 1 << (floor_log2 (log) + 1);
8314
8315   return log;
8316 }
8317
8318 /* Return the size of a DIE as it is represented in the
8319    .debug_info section.  */
8320
8321 static unsigned long
8322 size_of_die (dw_die_ref die)
8323 {
8324   unsigned long size = 0;
8325   dw_attr_ref a;
8326   unsigned ix;
8327
8328   size += size_of_uleb128 (die->die_abbrev);
8329   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
8330     {
8331       switch (AT_class (a))
8332         {
8333         case dw_val_class_addr:
8334           size += DWARF2_ADDR_SIZE;
8335           break;
8336         case dw_val_class_offset:
8337           size += DWARF_OFFSET_SIZE;
8338           break;
8339         case dw_val_class_loc:
8340           {
8341             unsigned long lsize = size_of_locs (AT_loc (a));
8342
8343             /* Block length.  */
8344             size += constant_size (lsize);
8345             size += lsize;
8346           }
8347           break;
8348         case dw_val_class_loc_list:
8349           size += DWARF_OFFSET_SIZE;
8350           break;
8351         case dw_val_class_range_list:
8352           size += DWARF_OFFSET_SIZE;
8353           break;
8354         case dw_val_class_const:
8355           size += size_of_sleb128 (AT_int (a));
8356           break;
8357         case dw_val_class_unsigned_const:
8358           size += constant_size (AT_unsigned (a));
8359           break;
8360         case dw_val_class_long_long:
8361           size += 1 + 2*HOST_BITS_PER_LONG/HOST_BITS_PER_CHAR; /* block */
8362           break;
8363         case dw_val_class_vec:
8364           size += constant_size (a->dw_attr_val.v.val_vec.length
8365                                  * a->dw_attr_val.v.val_vec.elt_size)
8366                   + a->dw_attr_val.v.val_vec.length
8367                     * a->dw_attr_val.v.val_vec.elt_size; /* block */
8368           break;
8369         case dw_val_class_flag:
8370           size += 1;
8371           break;
8372         case dw_val_class_die_ref:
8373           /* In DWARF2, DW_FORM_ref_addr is sized by target address length,
8374              whereas in DWARF3 it's always sized as an offset.  */
8375           if (AT_ref_external (a) && dwarf_version == 2)
8376             size += DWARF2_ADDR_SIZE;
8377           else
8378             size += DWARF_OFFSET_SIZE;
8379           break;
8380         case dw_val_class_fde_ref:
8381           size += DWARF_OFFSET_SIZE;
8382           break;
8383         case dw_val_class_lbl_id:
8384           size += DWARF2_ADDR_SIZE;
8385           break;
8386         case dw_val_class_lineptr:
8387         case dw_val_class_macptr:
8388           size += DWARF_OFFSET_SIZE;
8389           break;
8390         case dw_val_class_str:
8391           if (AT_string_form (a) == DW_FORM_strp)
8392             size += DWARF_OFFSET_SIZE;
8393           else
8394             size += strlen (a->dw_attr_val.v.val_str->str) + 1;
8395           break;
8396         case dw_val_class_file:
8397           size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
8398           break;
8399         default:
8400           gcc_unreachable ();
8401         }
8402     }
8403
8404   return size;
8405 }
8406
8407 /* Size the debugging information associated with a given DIE.  Visits the
8408    DIE's children recursively.  Updates the global variable next_die_offset, on
8409    each time through.  Uses the current value of next_die_offset to update the
8410    die_offset field in each DIE.  */
8411
8412 static void
8413 calc_die_sizes (dw_die_ref die)
8414 {
8415   dw_die_ref c;
8416
8417   die->die_offset = next_die_offset;
8418   next_die_offset += size_of_die (die);
8419
8420   FOR_EACH_CHILD (die, c, calc_die_sizes (c));
8421
8422   if (die->die_child != NULL)
8423     /* Count the null byte used to terminate sibling lists.  */
8424     next_die_offset += 1;
8425 }
8426
8427 /* Set the marks for a die and its children.  We do this so
8428    that we know whether or not a reference needs to use FORM_ref_addr; only
8429    DIEs in the same CU will be marked.  We used to clear out the offset
8430    and use that as the flag, but ran into ordering problems.  */
8431
8432 static void
8433 mark_dies (dw_die_ref die)
8434 {
8435   dw_die_ref c;
8436
8437   gcc_assert (!die->die_mark);
8438
8439   die->die_mark = 1;
8440   FOR_EACH_CHILD (die, c, mark_dies (c));
8441 }
8442
8443 /* Clear the marks for a die and its children.  */
8444
8445 static void
8446 unmark_dies (dw_die_ref die)
8447 {
8448   dw_die_ref c;
8449
8450   gcc_assert (die->die_mark);
8451
8452   die->die_mark = 0;
8453   FOR_EACH_CHILD (die, c, unmark_dies (c));
8454 }
8455
8456 /* Clear the marks for a die, its children and referred dies.  */
8457
8458 static void
8459 unmark_all_dies (dw_die_ref die)
8460 {
8461   dw_die_ref c;
8462   dw_attr_ref a;
8463   unsigned ix;
8464
8465   if (!die->die_mark)
8466     return;
8467   die->die_mark = 0;
8468
8469   FOR_EACH_CHILD (die, c, unmark_all_dies (c));
8470
8471   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
8472     if (AT_class (a) == dw_val_class_die_ref)
8473       unmark_all_dies (AT_ref (a));
8474 }
8475
8476 /* Return the size of the .debug_pubnames or .debug_pubtypes table
8477    generated for the compilation unit.  */
8478
8479 static unsigned long
8480 size_of_pubnames (VEC (pubname_entry, gc) * names)
8481 {
8482   unsigned long size;
8483   unsigned i;
8484   pubname_ref p;
8485
8486   size = DWARF_PUBNAMES_HEADER_SIZE;
8487   for (i = 0; VEC_iterate (pubname_entry, names, i, p); i++)
8488     if (names != pubtype_table
8489         || p->die->die_offset != 0
8490         || !flag_eliminate_unused_debug_types)
8491       size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
8492
8493   size += DWARF_OFFSET_SIZE;
8494   return size;
8495 }
8496
8497 /* Return the size of the information in the .debug_aranges section.  */
8498
8499 static unsigned long
8500 size_of_aranges (void)
8501 {
8502   unsigned long size;
8503
8504   size = DWARF_ARANGES_HEADER_SIZE;
8505
8506   /* Count the address/length pair for this compilation unit.  */
8507   if (text_section_used)
8508     size += 2 * DWARF2_ADDR_SIZE;
8509   if (cold_text_section_used)
8510     size += 2 * DWARF2_ADDR_SIZE;
8511   size += 2 * DWARF2_ADDR_SIZE * arange_table_in_use;
8512
8513   /* Count the two zero words used to terminated the address range table.  */
8514   size += 2 * DWARF2_ADDR_SIZE;
8515   return size;
8516 }
8517 \f
8518 /* Select the encoding of an attribute value.  */
8519
8520 static enum dwarf_form
8521 value_format (dw_attr_ref a)
8522 {
8523   switch (a->dw_attr_val.val_class)
8524     {
8525     case dw_val_class_addr:
8526       return DW_FORM_addr;
8527     case dw_val_class_range_list:
8528     case dw_val_class_offset:
8529     case dw_val_class_loc_list:
8530       switch (DWARF_OFFSET_SIZE)
8531         {
8532         case 4:
8533           return DW_FORM_data4;
8534         case 8:
8535           return DW_FORM_data8;
8536         default:
8537           gcc_unreachable ();
8538         }
8539     case dw_val_class_loc:
8540       switch (constant_size (size_of_locs (AT_loc (a))))
8541         {
8542         case 1:
8543           return DW_FORM_block1;
8544         case 2:
8545           return DW_FORM_block2;
8546         default:
8547           gcc_unreachable ();
8548         }
8549     case dw_val_class_const:
8550       return DW_FORM_sdata;
8551     case dw_val_class_unsigned_const:
8552       switch (constant_size (AT_unsigned (a)))
8553         {
8554         case 1:
8555           return DW_FORM_data1;
8556         case 2:
8557           return DW_FORM_data2;
8558         case 4:
8559           return DW_FORM_data4;
8560         case 8:
8561           return DW_FORM_data8;
8562         default:
8563           gcc_unreachable ();
8564         }
8565     case dw_val_class_long_long:
8566       return DW_FORM_block1;
8567     case dw_val_class_vec:
8568       switch (constant_size (a->dw_attr_val.v.val_vec.length
8569                              * a->dw_attr_val.v.val_vec.elt_size))
8570         {
8571         case 1:
8572           return DW_FORM_block1;
8573         case 2:
8574           return DW_FORM_block2;
8575         case 4:
8576           return DW_FORM_block4;
8577         default:
8578           gcc_unreachable ();
8579         }
8580     case dw_val_class_flag:
8581       return DW_FORM_flag;
8582     case dw_val_class_die_ref:
8583       if (AT_ref_external (a))
8584         return DW_FORM_ref_addr;
8585       else
8586         return DW_FORM_ref;
8587     case dw_val_class_fde_ref:
8588       return DW_FORM_data;
8589     case dw_val_class_lbl_id:
8590       return DW_FORM_addr;
8591     case dw_val_class_lineptr:
8592     case dw_val_class_macptr:
8593       return DW_FORM_data;
8594     case dw_val_class_str:
8595       return AT_string_form (a);
8596     case dw_val_class_file:
8597       switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
8598         {
8599         case 1:
8600           return DW_FORM_data1;
8601         case 2:
8602           return DW_FORM_data2;
8603         case 4:
8604           return DW_FORM_data4;
8605         default:
8606           gcc_unreachable ();
8607         }
8608
8609     default:
8610       gcc_unreachable ();
8611     }
8612 }
8613
8614 /* Output the encoding of an attribute value.  */
8615
8616 static void
8617 output_value_format (dw_attr_ref a)
8618 {
8619   enum dwarf_form form = value_format (a);
8620
8621   dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
8622 }
8623
8624 /* Output the .debug_abbrev section which defines the DIE abbreviation
8625    table.  */
8626
8627 static void
8628 output_abbrev_section (void)
8629 {
8630   unsigned long abbrev_id;
8631
8632   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8633     {
8634       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
8635       unsigned ix;
8636       dw_attr_ref a_attr;
8637
8638       dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
8639       dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
8640                                    dwarf_tag_name (abbrev->die_tag));
8641
8642       if (abbrev->die_child != NULL)
8643         dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
8644       else
8645         dw2_asm_output_data (1, DW_children_no, "DW_children_no");
8646
8647       for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
8648            ix++)
8649         {
8650           dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
8651                                        dwarf_attr_name (a_attr->dw_attr));
8652           output_value_format (a_attr);
8653         }
8654
8655       dw2_asm_output_data (1, 0, NULL);
8656       dw2_asm_output_data (1, 0, NULL);
8657     }
8658
8659   /* Terminate the table.  */
8660   dw2_asm_output_data (1, 0, NULL);
8661 }
8662
8663 /* Output a symbol we can use to refer to this DIE from another CU.  */
8664
8665 static inline void
8666 output_die_symbol (dw_die_ref die)
8667 {
8668   char *sym = die->die_symbol;
8669
8670   if (sym == 0)
8671     return;
8672
8673   if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
8674     /* We make these global, not weak; if the target doesn't support
8675        .linkonce, it doesn't support combining the sections, so debugging
8676        will break.  */
8677     targetm.asm_out.globalize_label (asm_out_file, sym);
8678
8679   ASM_OUTPUT_LABEL (asm_out_file, sym);
8680 }
8681
8682 /* Return a new location list, given the begin and end range, and the
8683    expression. gensym tells us whether to generate a new internal symbol for
8684    this location list node, which is done for the head of the list only.  */
8685
8686 static inline dw_loc_list_ref
8687 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
8688               const char *section, unsigned int gensym)
8689 {
8690   dw_loc_list_ref retlist = GGC_CNEW (dw_loc_list_node);
8691
8692   retlist->begin = begin;
8693   retlist->end = end;
8694   retlist->expr = expr;
8695   retlist->section = section;
8696   if (gensym)
8697     retlist->ll_symbol = gen_internal_sym ("LLST");
8698
8699   return retlist;
8700 }
8701
8702 /* Add a location description expression to a location list.  */
8703
8704 static inline void
8705 add_loc_descr_to_loc_list (dw_loc_list_ref *list_head, dw_loc_descr_ref descr,
8706                            const char *begin, const char *end,
8707                            const char *section)
8708 {
8709   dw_loc_list_ref *d;
8710
8711   /* Find the end of the chain.  */
8712   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
8713     ;
8714
8715   /* Add a new location list node to the list.  */
8716   *d = new_loc_list (descr, begin, end, section, 0);
8717 }
8718
8719 /* Output the location list given to us.  */
8720
8721 static void
8722 output_loc_list (dw_loc_list_ref list_head)
8723 {
8724   dw_loc_list_ref curr = list_head;
8725
8726   ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
8727
8728   /* Walk the location list, and output each range + expression.  */
8729   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
8730     {
8731       unsigned long size;
8732       /* Don't output an entry that starts and ends at the same address.  */
8733       if (strcmp (curr->begin, curr->end) == 0)
8734         continue;
8735       if (!have_multiple_function_sections)
8736         {
8737           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
8738                                 "Location list begin address (%s)",
8739                                 list_head->ll_symbol);
8740           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
8741                                 "Location list end address (%s)",
8742                                 list_head->ll_symbol);
8743         }
8744       else
8745         {
8746           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
8747                                "Location list begin address (%s)",
8748                                list_head->ll_symbol);
8749           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
8750                                "Location list end address (%s)",
8751                                list_head->ll_symbol);
8752         }
8753       size = size_of_locs (curr->expr);
8754
8755       /* Output the block length for this list of location operations.  */
8756       gcc_assert (size <= 0xffff);
8757       dw2_asm_output_data (2, size, "%s", "Location expression size");
8758
8759       output_loc_sequence (curr->expr);
8760     }
8761
8762   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8763                        "Location list terminator begin (%s)",
8764                        list_head->ll_symbol);
8765   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8766                        "Location list terminator end (%s)",
8767                        list_head->ll_symbol);
8768 }
8769
8770 /* Output the DIE and its attributes.  Called recursively to generate
8771    the definitions of each child DIE.  */
8772
8773 static void
8774 output_die (dw_die_ref die)
8775 {
8776   dw_attr_ref a;
8777   dw_die_ref c;
8778   unsigned long size;
8779   unsigned ix;
8780
8781   /* If someone in another CU might refer to us, set up a symbol for
8782      them to point to.  */
8783   if (die->die_symbol)
8784     output_die_symbol (die);
8785
8786   dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (0x%lx) %s)",
8787                                (unsigned long)die->die_offset,
8788                                dwarf_tag_name (die->die_tag));
8789
8790   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
8791     {
8792       const char *name = dwarf_attr_name (a->dw_attr);
8793
8794       switch (AT_class (a))
8795         {
8796         case dw_val_class_addr:
8797           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8798           break;
8799
8800         case dw_val_class_offset:
8801           dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8802                                "%s", name);
8803           break;
8804
8805         case dw_val_class_range_list:
8806           {
8807             char *p = strchr (ranges_section_label, '\0');
8808
8809             sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
8810                      a->dw_attr_val.v.val_offset);
8811             dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8812                                    debug_ranges_section, "%s", name);
8813             *p = '\0';
8814           }
8815           break;
8816
8817         case dw_val_class_loc:
8818           size = size_of_locs (AT_loc (a));
8819
8820           /* Output the block length for this list of location operations.  */
8821           dw2_asm_output_data (constant_size (size), size, "%s", name);
8822
8823           output_loc_sequence (AT_loc (a));
8824           break;
8825
8826         case dw_val_class_const:
8827           /* ??? It would be slightly more efficient to use a scheme like is
8828              used for unsigned constants below, but gdb 4.x does not sign
8829              extend.  Gdb 5.x does sign extend.  */
8830           dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8831           break;
8832
8833         case dw_val_class_unsigned_const:
8834           dw2_asm_output_data (constant_size (AT_unsigned (a)),
8835                                AT_unsigned (a), "%s", name);
8836           break;
8837
8838         case dw_val_class_long_long:
8839           {
8840             unsigned HOST_WIDE_INT first, second;
8841
8842             dw2_asm_output_data (1,
8843                                  2 * HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
8844                                  "%s", name);
8845
8846             if (WORDS_BIG_ENDIAN)
8847               {
8848                 first = a->dw_attr_val.v.val_long_long.hi;
8849                 second = a->dw_attr_val.v.val_long_long.low;
8850               }
8851             else
8852               {
8853                 first = a->dw_attr_val.v.val_long_long.low;
8854                 second = a->dw_attr_val.v.val_long_long.hi;
8855               }
8856
8857             dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
8858                                  first, "long long constant");
8859             dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
8860                                  second, NULL);
8861           }
8862           break;
8863
8864         case dw_val_class_vec:
8865           {
8866             unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
8867             unsigned int len = a->dw_attr_val.v.val_vec.length;
8868             unsigned int i;
8869             unsigned char *p;
8870
8871             dw2_asm_output_data (constant_size (len * elt_size),
8872                                  len * elt_size, "%s", name);
8873             if (elt_size > sizeof (HOST_WIDE_INT))
8874               {
8875                 elt_size /= 2;
8876                 len *= 2;
8877               }
8878             for (i = 0, p = a->dw_attr_val.v.val_vec.array;
8879                  i < len;
8880                  i++, p += elt_size)
8881               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
8882                                    "fp or vector constant word %u", i);
8883             break;
8884           }
8885
8886         case dw_val_class_flag:
8887           dw2_asm_output_data (1, AT_flag (a), "%s", name);
8888           break;
8889
8890         case dw_val_class_loc_list:
8891           {
8892             char *sym = AT_loc_list (a)->ll_symbol;
8893
8894             gcc_assert (sym);
8895             dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8896                                    "%s", name);
8897           }
8898           break;
8899
8900         case dw_val_class_die_ref:
8901           if (AT_ref_external (a))
8902             {
8903               char *sym = AT_ref (a)->die_symbol;
8904               int size;
8905
8906               gcc_assert (sym);
8907
8908               /* In DWARF2, DW_FORM_ref_addr is sized by target address
8909                  length, whereas in DWARF3 it's always sized as an offset.  */
8910               if (dwarf_version == 2)
8911                 size = DWARF2_ADDR_SIZE;
8912               else
8913                 size = DWARF_OFFSET_SIZE;
8914               dw2_asm_output_offset (size, sym, debug_info_section, "%s", name);
8915             }
8916           else
8917             {
8918               gcc_assert (AT_ref (a)->die_offset);
8919               dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
8920                                    "%s", name);
8921             }
8922           break;
8923
8924         case dw_val_class_fde_ref:
8925           {
8926             char l1[20];
8927
8928             ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
8929                                          a->dw_attr_val.v.val_fde_index * 2);
8930             dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
8931                                    "%s", name);
8932           }
8933           break;
8934
8935         case dw_val_class_lbl_id:
8936           dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8937           break;
8938
8939         case dw_val_class_lineptr:
8940           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8941                                  debug_line_section, "%s", name);
8942           break;
8943
8944         case dw_val_class_macptr:
8945           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8946                                  debug_macinfo_section, "%s", name);
8947           break;
8948
8949         case dw_val_class_str:
8950           if (AT_string_form (a) == DW_FORM_strp)
8951             dw2_asm_output_offset (DWARF_OFFSET_SIZE,
8952                                    a->dw_attr_val.v.val_str->label,
8953                                    debug_str_section,
8954                                    "%s: \"%s\"", name, AT_string (a));
8955           else
8956             dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
8957           break;
8958
8959         case dw_val_class_file:
8960           {
8961             int f = maybe_emit_file (a->dw_attr_val.v.val_file);
8962
8963             dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
8964                                  a->dw_attr_val.v.val_file->filename);
8965             break;
8966           }
8967
8968         default:
8969           gcc_unreachable ();
8970         }
8971     }
8972
8973   FOR_EACH_CHILD (die, c, output_die (c));
8974
8975   /* Add null byte to terminate sibling list.  */
8976   if (die->die_child != NULL)
8977     dw2_asm_output_data (1, 0, "end of children of DIE 0x%lx",
8978                          (unsigned long) die->die_offset);
8979 }
8980
8981 /* Output the compilation unit that appears at the beginning of the
8982    .debug_info section, and precedes the DIE descriptions.  */
8983
8984 static void
8985 output_compilation_unit_header (void)
8986 {
8987   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8988     dw2_asm_output_data (4, 0xffffffff,
8989       "Initial length escape value indicating 64-bit DWARF extension");
8990   dw2_asm_output_data (DWARF_OFFSET_SIZE,
8991                        next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
8992                        "Length of Compilation Unit Info");
8993   dw2_asm_output_data (2, dwarf_version, "DWARF version number");
8994   dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
8995                          debug_abbrev_section,
8996                          "Offset Into Abbrev. Section");
8997   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8998 }
8999
9000 /* Output the compilation unit DIE and its children.  */
9001
9002 static void
9003 output_comp_unit (dw_die_ref die, int output_if_empty)
9004 {
9005   const char *secname;
9006   char *oldsym, *tmp;
9007
9008   /* Unless we are outputting main CU, we may throw away empty ones.  */
9009   if (!output_if_empty && die->die_child == NULL)
9010     return;
9011
9012   /* Even if there are no children of this DIE, we must output the information
9013      about the compilation unit.  Otherwise, on an empty translation unit, we
9014      will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
9015      will then complain when examining the file.  First mark all the DIEs in
9016      this CU so we know which get local refs.  */
9017   mark_dies (die);
9018
9019   build_abbrev_table (die);
9020
9021   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
9022   next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
9023   calc_die_sizes (die);
9024
9025   oldsym = die->die_symbol;
9026   if (oldsym)
9027     {
9028       tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
9029
9030       sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
9031       secname = tmp;
9032       die->die_symbol = NULL;
9033       switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9034     }
9035   else
9036     switch_to_section (debug_info_section);
9037
9038   /* Output debugging information.  */
9039   output_compilation_unit_header ();
9040   output_die (die);
9041
9042   /* Leave the marks on the main CU, so we can check them in
9043      output_pubnames.  */
9044   if (oldsym)
9045     {
9046       unmark_dies (die);
9047       die->die_symbol = oldsym;
9048     }
9049 }
9050
9051 /* Return the DWARF2/3 pubname associated with a decl.  */
9052
9053 static const char *
9054 dwarf2_name (tree decl, int scope)
9055 {
9056   return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
9057 }
9058
9059 /* Add a new entry to .debug_pubnames if appropriate.  */
9060
9061 static void
9062 add_pubname_string (const char *str, dw_die_ref die)
9063 {
9064   pubname_entry e;
9065
9066   e.die = die;
9067   e.name = xstrdup (str);
9068   VEC_safe_push (pubname_entry, gc, pubname_table, &e);
9069 }
9070
9071 static void
9072 add_pubname (tree decl, dw_die_ref die)
9073 {
9074   if (TREE_PUBLIC (decl))
9075     add_pubname_string (dwarf2_name (decl, 1), die);
9076 }
9077
9078 /* Add a new entry to .debug_pubtypes if appropriate.  */
9079
9080 static void
9081 add_pubtype (tree decl, dw_die_ref die)
9082 {
9083   pubname_entry e;
9084
9085   e.name = NULL;
9086   if ((TREE_PUBLIC (decl)
9087        || die->die_parent == comp_unit_die)
9088       && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
9089     {
9090       e.die = die;
9091       if (TYPE_P (decl))
9092         {
9093           if (TYPE_NAME (decl))
9094             {
9095               if (TREE_CODE (TYPE_NAME (decl)) == IDENTIFIER_NODE)
9096                 e.name = IDENTIFIER_POINTER (TYPE_NAME (decl));
9097               else if (TREE_CODE (TYPE_NAME (decl)) == TYPE_DECL
9098                        && DECL_NAME (TYPE_NAME (decl)))
9099                 e.name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (decl)));
9100               else
9101                e.name = xstrdup ((const char *) get_AT_string (die, DW_AT_name));
9102             }
9103         }
9104       else
9105         e.name = xstrdup (dwarf2_name (decl, 1));
9106
9107       /* If we don't have a name for the type, there's no point in adding
9108          it to the table.  */
9109       if (e.name && e.name[0] != '\0')
9110         VEC_safe_push (pubname_entry, gc, pubtype_table, &e);
9111     }
9112 }
9113
9114 /* Output the public names table used to speed up access to externally
9115    visible names; or the public types table used to find type definitions.  */
9116
9117 static void
9118 output_pubnames (VEC (pubname_entry, gc) * names)
9119 {
9120   unsigned i;
9121   unsigned long pubnames_length = size_of_pubnames (names);
9122   pubname_ref pub;
9123
9124   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9125     dw2_asm_output_data (4, 0xffffffff,
9126       "Initial length escape value indicating 64-bit DWARF extension");
9127   if (names == pubname_table)
9128     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
9129                          "Length of Public Names Info");
9130   else
9131     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
9132                          "Length of Public Type Names Info");
9133   /* Version number for pubnames/pubtypes is still 2, even in DWARF3.  */
9134   dw2_asm_output_data (2, 2, "DWARF Version");
9135   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9136                          debug_info_section,
9137                          "Offset of Compilation Unit Info");
9138   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
9139                        "Compilation Unit Length");
9140
9141   for (i = 0; VEC_iterate (pubname_entry, names, i, pub); i++)
9142     {
9143       /* We shouldn't see pubnames for DIEs outside of the main CU.  */
9144       if (names == pubname_table)
9145         gcc_assert (pub->die->die_mark);
9146
9147       if (names != pubtype_table
9148           || pub->die->die_offset != 0
9149           || !flag_eliminate_unused_debug_types)
9150         {
9151           dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
9152                                "DIE offset");
9153
9154           dw2_asm_output_nstring (pub->name, -1, "external name");
9155         }
9156     }
9157
9158   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
9159 }
9160
9161 /* Add a new entry to .debug_aranges if appropriate.  */
9162
9163 static void
9164 add_arange (tree decl, dw_die_ref die)
9165 {
9166   if (! DECL_SECTION_NAME (decl))
9167     return;
9168
9169   if (arange_table_in_use == arange_table_allocated)
9170     {
9171       arange_table_allocated += ARANGE_TABLE_INCREMENT;
9172       arange_table = GGC_RESIZEVEC (dw_die_ref, arange_table,
9173                                     arange_table_allocated);
9174       memset (arange_table + arange_table_in_use, 0,
9175               ARANGE_TABLE_INCREMENT * sizeof (dw_die_ref));
9176     }
9177
9178   arange_table[arange_table_in_use++] = die;
9179 }
9180
9181 /* Output the information that goes into the .debug_aranges table.
9182    Namely, define the beginning and ending address range of the
9183    text section generated for this compilation unit.  */
9184
9185 static void
9186 output_aranges (void)
9187 {
9188   unsigned i;
9189   unsigned long aranges_length = size_of_aranges ();
9190
9191   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9192     dw2_asm_output_data (4, 0xffffffff,
9193       "Initial length escape value indicating 64-bit DWARF extension");
9194   dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
9195                        "Length of Address Ranges Info");
9196   /* Version number for aranges is still 2, even in DWARF3.  */
9197   dw2_asm_output_data (2, 2, "DWARF Version");
9198   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9199                          debug_info_section,
9200                          "Offset of Compilation Unit Info");
9201   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
9202   dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
9203
9204   /* We need to align to twice the pointer size here.  */
9205   if (DWARF_ARANGES_PAD_SIZE)
9206     {
9207       /* Pad using a 2 byte words so that padding is correct for any
9208          pointer size.  */
9209       dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
9210                            2 * DWARF2_ADDR_SIZE);
9211       for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
9212         dw2_asm_output_data (2, 0, NULL);
9213     }
9214
9215   /* It is necessary not to output these entries if the sections were
9216      not used; if the sections were not used, the length will be 0 and
9217      the address may end up as 0 if the section is discarded by ld
9218      --gc-sections, leaving an invalid (0, 0) entry that can be
9219      confused with the terminator.  */
9220   if (text_section_used)
9221     {
9222       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
9223       dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
9224                             text_section_label, "Length");
9225     }
9226   if (cold_text_section_used)
9227     {
9228       dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
9229                            "Address");
9230       dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
9231                             cold_text_section_label, "Length");
9232     }
9233
9234   for (i = 0; i < arange_table_in_use; i++)
9235     {
9236       dw_die_ref die = arange_table[i];
9237
9238       /* We shouldn't see aranges for DIEs outside of the main CU.  */
9239       gcc_assert (die->die_mark);
9240
9241       if (die->die_tag == DW_TAG_subprogram)
9242         {
9243           dw2_asm_output_addr (DWARF2_ADDR_SIZE, get_AT_low_pc (die),
9244                                "Address");
9245           dw2_asm_output_delta (DWARF2_ADDR_SIZE, get_AT_hi_pc (die),
9246                                 get_AT_low_pc (die), "Length");
9247         }
9248       else
9249         {
9250           /* A static variable; extract the symbol from DW_AT_location.
9251              Note that this code isn't currently hit, as we only emit
9252              aranges for functions (jason 9/23/99).  */
9253           dw_attr_ref a = get_AT (die, DW_AT_location);
9254           dw_loc_descr_ref loc;
9255
9256           gcc_assert (a && AT_class (a) == dw_val_class_loc);
9257
9258           loc = AT_loc (a);
9259           gcc_assert (loc->dw_loc_opc == DW_OP_addr);
9260
9261           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE,
9262                                    loc->dw_loc_oprnd1.v.val_addr, "Address");
9263           dw2_asm_output_data (DWARF2_ADDR_SIZE,
9264                                get_AT_unsigned (die, DW_AT_byte_size),
9265                                "Length");
9266         }
9267     }
9268
9269   /* Output the terminator words.  */
9270   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9271   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9272 }
9273
9274 /* Add a new entry to .debug_ranges.  Return the offset at which it
9275    was placed.  */
9276
9277 static unsigned int
9278 add_ranges_num (int num)
9279 {
9280   unsigned int in_use = ranges_table_in_use;
9281
9282   if (in_use == ranges_table_allocated)
9283     {
9284       ranges_table_allocated += RANGES_TABLE_INCREMENT;
9285       ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
9286                                     ranges_table_allocated);
9287       memset (ranges_table + ranges_table_in_use, 0,
9288               RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
9289     }
9290
9291   ranges_table[in_use].num = num;
9292   ranges_table_in_use = in_use + 1;
9293
9294   return in_use * 2 * DWARF2_ADDR_SIZE;
9295 }
9296
9297 /* Add a new entry to .debug_ranges corresponding to a block, or a
9298    range terminator if BLOCK is NULL.  */
9299
9300 static unsigned int
9301 add_ranges (const_tree block)
9302 {
9303   return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
9304 }
9305
9306 /* Add a new entry to .debug_ranges corresponding to a pair of
9307    labels.  */
9308
9309 static unsigned int
9310 add_ranges_by_labels (const char *begin, const char *end)
9311 {
9312   unsigned int in_use = ranges_by_label_in_use;
9313
9314   if (in_use == ranges_by_label_allocated)
9315     {
9316       ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
9317       ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
9318                                        ranges_by_label,
9319                                        ranges_by_label_allocated);
9320       memset (ranges_by_label + ranges_by_label_in_use, 0,
9321               RANGES_TABLE_INCREMENT
9322               * sizeof (struct dw_ranges_by_label_struct));
9323     }
9324
9325   ranges_by_label[in_use].begin = begin;
9326   ranges_by_label[in_use].end = end;
9327   ranges_by_label_in_use = in_use + 1;
9328
9329   return add_ranges_num (-(int)in_use - 1);
9330 }
9331
9332 static void
9333 output_ranges (void)
9334 {
9335   unsigned i;
9336   static const char *const start_fmt = "Offset 0x%x";
9337   const char *fmt = start_fmt;
9338
9339   for (i = 0; i < ranges_table_in_use; i++)
9340     {
9341       int block_num = ranges_table[i].num;
9342
9343       if (block_num > 0)
9344         {
9345           char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
9346           char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
9347
9348           ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
9349           ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
9350
9351           /* If all code is in the text section, then the compilation
9352              unit base address defaults to DW_AT_low_pc, which is the
9353              base of the text section.  */
9354           if (!have_multiple_function_sections)
9355             {
9356               dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
9357                                     text_section_label,
9358                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
9359               dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
9360                                     text_section_label, NULL);
9361             }
9362
9363           /* Otherwise, the compilation unit base address is zero,
9364              which allows us to use absolute addresses, and not worry
9365              about whether the target supports cross-section
9366              arithmetic.  */
9367           else
9368             {
9369               dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
9370                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
9371               dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
9372             }
9373
9374           fmt = NULL;
9375         }
9376
9377       /* Negative block_num stands for an index into ranges_by_label.  */
9378       else if (block_num < 0)
9379         {
9380           int lab_idx = - block_num - 1;
9381
9382           if (!have_multiple_function_sections)
9383             {
9384               gcc_unreachable ();
9385 #if 0
9386               /* If we ever use add_ranges_by_labels () for a single
9387                  function section, all we have to do is to take out
9388                  the #if 0 above.  */
9389               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9390                                     ranges_by_label[lab_idx].begin,
9391                                     text_section_label,
9392                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
9393               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9394                                     ranges_by_label[lab_idx].end,
9395                                     text_section_label, NULL);
9396 #endif
9397             }
9398           else
9399             {
9400               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9401                                    ranges_by_label[lab_idx].begin,
9402                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
9403               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9404                                    ranges_by_label[lab_idx].end,
9405                                    NULL);
9406             }
9407         }
9408       else
9409         {
9410           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9411           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9412           fmt = start_fmt;
9413         }
9414     }
9415 }
9416
9417 /* Data structure containing information about input files.  */
9418 struct file_info
9419 {
9420   const char *path;     /* Complete file name.  */
9421   const char *fname;    /* File name part.  */
9422   int length;           /* Length of entire string.  */
9423   struct dwarf_file_data * file_idx;    /* Index in input file table.  */
9424   int dir_idx;          /* Index in directory table.  */
9425 };
9426
9427 /* Data structure containing information about directories with source
9428    files.  */
9429 struct dir_info
9430 {
9431   const char *path;     /* Path including directory name.  */
9432   int length;           /* Path length.  */
9433   int prefix;           /* Index of directory entry which is a prefix.  */
9434   int count;            /* Number of files in this directory.  */
9435   int dir_idx;          /* Index of directory used as base.  */
9436 };
9437
9438 /* Callback function for file_info comparison.  We sort by looking at
9439    the directories in the path.  */
9440
9441 static int
9442 file_info_cmp (const void *p1, const void *p2)
9443 {
9444   const struct file_info *const s1 = (const struct file_info *) p1;
9445   const struct file_info *const s2 = (const struct file_info *) p2;
9446   const unsigned char *cp1;
9447   const unsigned char *cp2;
9448
9449   /* Take care of file names without directories.  We need to make sure that
9450      we return consistent values to qsort since some will get confused if
9451      we return the same value when identical operands are passed in opposite
9452      orders.  So if neither has a directory, return 0 and otherwise return
9453      1 or -1 depending on which one has the directory.  */
9454   if ((s1->path == s1->fname || s2->path == s2->fname))
9455     return (s2->path == s2->fname) - (s1->path == s1->fname);
9456
9457   cp1 = (const unsigned char *) s1->path;
9458   cp2 = (const unsigned char *) s2->path;
9459
9460   while (1)
9461     {
9462       ++cp1;
9463       ++cp2;
9464       /* Reached the end of the first path?  If so, handle like above.  */
9465       if ((cp1 == (const unsigned char *) s1->fname)
9466           || (cp2 == (const unsigned char *) s2->fname))
9467         return ((cp2 == (const unsigned char *) s2->fname)
9468                 - (cp1 == (const unsigned char *) s1->fname));
9469
9470       /* Character of current path component the same?  */
9471       else if (*cp1 != *cp2)
9472         return *cp1 - *cp2;
9473     }
9474 }
9475
9476 struct file_name_acquire_data
9477 {
9478   struct file_info *files;
9479   int used_files;
9480   int max_files;
9481 };
9482
9483 /* Traversal function for the hash table.  */
9484
9485 static int
9486 file_name_acquire (void ** slot, void *data)
9487 {
9488   struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
9489   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
9490   struct file_info *fi;
9491   const char *f;
9492
9493   gcc_assert (fnad->max_files >= d->emitted_number);
9494
9495   if (! d->emitted_number)
9496     return 1;
9497
9498   gcc_assert (fnad->max_files != fnad->used_files);
9499
9500   fi = fnad->files + fnad->used_files++;
9501
9502   /* Skip all leading "./".  */
9503   f = d->filename;
9504   while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
9505     f += 2;
9506
9507   /* Create a new array entry.  */
9508   fi->path = f;
9509   fi->length = strlen (f);
9510   fi->file_idx = d;
9511
9512   /* Search for the file name part.  */
9513   f = strrchr (f, DIR_SEPARATOR);
9514 #if defined (DIR_SEPARATOR_2)
9515   {
9516     char *g = strrchr (fi->path, DIR_SEPARATOR_2);
9517
9518     if (g != NULL)
9519       {
9520         if (f == NULL || f < g)
9521           f = g;
9522       }
9523   }
9524 #endif
9525
9526   fi->fname = f == NULL ? fi->path : f + 1;
9527   return 1;
9528 }
9529
9530 /* Output the directory table and the file name table.  We try to minimize
9531    the total amount of memory needed.  A heuristic is used to avoid large
9532    slowdowns with many input files.  */
9533
9534 static void
9535 output_file_names (void)
9536 {
9537   struct file_name_acquire_data fnad;
9538   int numfiles;
9539   struct file_info *files;
9540   struct dir_info *dirs;
9541   int *saved;
9542   int *savehere;
9543   int *backmap;
9544   int ndirs;
9545   int idx_offset;
9546   int i;
9547   int idx;
9548
9549   if (!last_emitted_file)
9550     {
9551       dw2_asm_output_data (1, 0, "End directory table");
9552       dw2_asm_output_data (1, 0, "End file name table");
9553       return;
9554     }
9555
9556   numfiles = last_emitted_file->emitted_number;
9557
9558   /* Allocate the various arrays we need.  */
9559   files = XALLOCAVEC (struct file_info, numfiles);
9560   dirs = XALLOCAVEC (struct dir_info, numfiles);
9561
9562   fnad.files = files;
9563   fnad.used_files = 0;
9564   fnad.max_files = numfiles;
9565   htab_traverse (file_table, file_name_acquire, &fnad);
9566   gcc_assert (fnad.used_files == fnad.max_files);
9567
9568   qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
9569
9570   /* Find all the different directories used.  */
9571   dirs[0].path = files[0].path;
9572   dirs[0].length = files[0].fname - files[0].path;
9573   dirs[0].prefix = -1;
9574   dirs[0].count = 1;
9575   dirs[0].dir_idx = 0;
9576   files[0].dir_idx = 0;
9577   ndirs = 1;
9578
9579   for (i = 1; i < numfiles; i++)
9580     if (files[i].fname - files[i].path == dirs[ndirs - 1].length
9581         && memcmp (dirs[ndirs - 1].path, files[i].path,
9582                    dirs[ndirs - 1].length) == 0)
9583       {
9584         /* Same directory as last entry.  */
9585         files[i].dir_idx = ndirs - 1;
9586         ++dirs[ndirs - 1].count;
9587       }
9588     else
9589       {
9590         int j;
9591
9592         /* This is a new directory.  */
9593         dirs[ndirs].path = files[i].path;
9594         dirs[ndirs].length = files[i].fname - files[i].path;
9595         dirs[ndirs].count = 1;
9596         dirs[ndirs].dir_idx = ndirs;
9597         files[i].dir_idx = ndirs;
9598
9599         /* Search for a prefix.  */
9600         dirs[ndirs].prefix = -1;
9601         for (j = 0; j < ndirs; j++)
9602           if (dirs[j].length < dirs[ndirs].length
9603               && dirs[j].length > 1
9604               && (dirs[ndirs].prefix == -1
9605                   || dirs[j].length > dirs[dirs[ndirs].prefix].length)
9606               && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
9607             dirs[ndirs].prefix = j;
9608
9609         ++ndirs;
9610       }
9611
9612   /* Now to the actual work.  We have to find a subset of the directories which
9613      allow expressing the file name using references to the directory table
9614      with the least amount of characters.  We do not do an exhaustive search
9615      where we would have to check out every combination of every single
9616      possible prefix.  Instead we use a heuristic which provides nearly optimal
9617      results in most cases and never is much off.  */
9618   saved = XALLOCAVEC (int, ndirs);
9619   savehere = XALLOCAVEC (int, ndirs);
9620
9621   memset (saved, '\0', ndirs * sizeof (saved[0]));
9622   for (i = 0; i < ndirs; i++)
9623     {
9624       int j;
9625       int total;
9626
9627       /* We can always save some space for the current directory.  But this
9628          does not mean it will be enough to justify adding the directory.  */
9629       savehere[i] = dirs[i].length;
9630       total = (savehere[i] - saved[i]) * dirs[i].count;
9631
9632       for (j = i + 1; j < ndirs; j++)
9633         {
9634           savehere[j] = 0;
9635           if (saved[j] < dirs[i].length)
9636             {
9637               /* Determine whether the dirs[i] path is a prefix of the
9638                  dirs[j] path.  */
9639               int k;
9640
9641               k = dirs[j].prefix;
9642               while (k != -1 && k != (int) i)
9643                 k = dirs[k].prefix;
9644
9645               if (k == (int) i)
9646                 {
9647                   /* Yes it is.  We can possibly save some memory by
9648                      writing the filenames in dirs[j] relative to
9649                      dirs[i].  */
9650                   savehere[j] = dirs[i].length;
9651                   total += (savehere[j] - saved[j]) * dirs[j].count;
9652                 }
9653             }
9654         }
9655
9656       /* Check whether we can save enough to justify adding the dirs[i]
9657          directory.  */
9658       if (total > dirs[i].length + 1)
9659         {
9660           /* It's worthwhile adding.  */
9661           for (j = i; j < ndirs; j++)
9662             if (savehere[j] > 0)
9663               {
9664                 /* Remember how much we saved for this directory so far.  */
9665                 saved[j] = savehere[j];
9666
9667                 /* Remember the prefix directory.  */
9668                 dirs[j].dir_idx = i;
9669               }
9670         }
9671     }
9672
9673   /* Emit the directory name table.  */
9674   idx = 1;
9675   idx_offset = dirs[0].length > 0 ? 1 : 0;
9676   for (i = 1 - idx_offset; i < ndirs; i++)
9677     dw2_asm_output_nstring (dirs[i].path,
9678                             dirs[i].length
9679                              - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
9680                             "Directory Entry: 0x%x", i + idx_offset);
9681
9682   dw2_asm_output_data (1, 0, "End directory table");
9683
9684   /* We have to emit them in the order of emitted_number since that's
9685      used in the debug info generation.  To do this efficiently we
9686      generate a back-mapping of the indices first.  */
9687   backmap = XALLOCAVEC (int, numfiles);
9688   for (i = 0; i < numfiles; i++)
9689     backmap[files[i].file_idx->emitted_number - 1] = i;
9690
9691   /* Now write all the file names.  */
9692   for (i = 0; i < numfiles; i++)
9693     {
9694       int file_idx = backmap[i];
9695       int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
9696
9697 #ifdef VMS_DEBUGGING_INFO
9698 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
9699
9700       /* Setting these fields can lead to debugger miscomparisons,
9701          but VMS Debug requires them to be set correctly.  */
9702
9703       int ver;
9704       long long cdt;
9705       long siz;
9706       int maxfilelen = strlen (files[file_idx].path)
9707                                + dirs[dir_idx].length
9708                                + MAX_VMS_VERSION_LEN + 1;
9709       char *filebuf = XALLOCAVEC (char, maxfilelen);
9710
9711       vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
9712       snprintf (filebuf, maxfilelen, "%s;%d",
9713                 files[file_idx].path + dirs[dir_idx].length, ver);
9714
9715       dw2_asm_output_nstring
9716         (filebuf, -1, "File Entry: 0x%x", (unsigned) i + 1);
9717
9718       /* Include directory index.  */
9719       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9720
9721       /* Modification time.  */
9722       dw2_asm_output_data_uleb128
9723         ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
9724           ? cdt : 0,
9725          NULL);
9726
9727       /* File length in bytes.  */
9728       dw2_asm_output_data_uleb128
9729         ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
9730           ? siz : 0,
9731          NULL);
9732 #else
9733       dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
9734                               "File Entry: 0x%x", (unsigned) i + 1);
9735
9736       /* Include directory index.  */
9737       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9738
9739       /* Modification time.  */
9740       dw2_asm_output_data_uleb128 (0, NULL);
9741
9742       /* File length in bytes.  */
9743       dw2_asm_output_data_uleb128 (0, NULL);
9744 #endif
9745     }
9746
9747   dw2_asm_output_data (1, 0, "End file name table");
9748 }
9749
9750
9751 /* Output the source line number correspondence information.  This
9752    information goes into the .debug_line section.  */
9753
9754 static void
9755 output_line_info (void)
9756 {
9757   char l1[20], l2[20], p1[20], p2[20];
9758   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
9759   char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
9760   unsigned opc;
9761   unsigned n_op_args;
9762   unsigned long lt_index;
9763   unsigned long current_line;
9764   long line_offset;
9765   long line_delta;
9766   unsigned long current_file;
9767   unsigned long function;
9768
9769   ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
9770   ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
9771   ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
9772   ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
9773
9774   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9775     dw2_asm_output_data (4, 0xffffffff,
9776       "Initial length escape value indicating 64-bit DWARF extension");
9777   dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
9778                         "Length of Source Line Info");
9779   ASM_OUTPUT_LABEL (asm_out_file, l1);
9780
9781   dw2_asm_output_data (2, dwarf_version, "DWARF Version");
9782   dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
9783   ASM_OUTPUT_LABEL (asm_out_file, p1);
9784
9785   /* Define the architecture-dependent minimum instruction length (in
9786    bytes).  In this implementation of DWARF, this field is used for
9787    information purposes only.  Since GCC generates assembly language,
9788    we have no a priori knowledge of how many instruction bytes are
9789    generated for each source line, and therefore can use only the
9790    DW_LNE_set_address and DW_LNS_fixed_advance_pc line information
9791    commands.  Accordingly, we fix this as `1', which is "correct
9792    enough" for all architectures, and don't let the target override.  */
9793   dw2_asm_output_data (1, 1,
9794                        "Minimum Instruction Length");
9795
9796   dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
9797                        "Default is_stmt_start flag");
9798   dw2_asm_output_data (1, DWARF_LINE_BASE,
9799                        "Line Base Value (Special Opcodes)");
9800   dw2_asm_output_data (1, DWARF_LINE_RANGE,
9801                        "Line Range Value (Special Opcodes)");
9802   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
9803                        "Special Opcode Base");
9804
9805   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
9806     {
9807       switch (opc)
9808         {
9809         case DW_LNS_advance_pc:
9810         case DW_LNS_advance_line:
9811         case DW_LNS_set_file:
9812         case DW_LNS_set_column:
9813         case DW_LNS_fixed_advance_pc:
9814           n_op_args = 1;
9815           break;
9816         default:
9817           n_op_args = 0;
9818           break;
9819         }
9820
9821       dw2_asm_output_data (1, n_op_args, "opcode: 0x%x has %d args",
9822                            opc, n_op_args);
9823     }
9824
9825   /* Write out the information about the files we use.  */
9826   output_file_names ();
9827   ASM_OUTPUT_LABEL (asm_out_file, p2);
9828
9829   /* We used to set the address register to the first location in the text
9830      section here, but that didn't accomplish anything since we already
9831      have a line note for the opening brace of the first function.  */
9832
9833   /* Generate the line number to PC correspondence table, encoded as
9834      a series of state machine operations.  */
9835   current_file = 1;
9836   current_line = 1;
9837
9838   if (cfun && in_cold_section_p)
9839     strcpy (prev_line_label, crtl->subsections.cold_section_label);
9840   else
9841     strcpy (prev_line_label, text_section_label);
9842   for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
9843     {
9844       dw_line_info_ref line_info = &line_info_table[lt_index];
9845
9846 #if 0
9847       /* Disable this optimization for now; GDB wants to see two line notes
9848          at the beginning of a function so it can find the end of the
9849          prologue.  */
9850
9851       /* Don't emit anything for redundant notes.  Just updating the
9852          address doesn't accomplish anything, because we already assume
9853          that anything after the last address is this line.  */
9854       if (line_info->dw_line_num == current_line
9855           && line_info->dw_file_num == current_file)
9856         continue;
9857 #endif
9858
9859       /* Emit debug info for the address of the current line.
9860
9861          Unfortunately, we have little choice here currently, and must always
9862          use the most general form.  GCC does not know the address delta
9863          itself, so we can't use DW_LNS_advance_pc.  Many ports do have length
9864          attributes which will give an upper bound on the address range.  We
9865          could perhaps use length attributes to determine when it is safe to
9866          use DW_LNS_fixed_advance_pc.  */
9867
9868       ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
9869       if (0)
9870         {
9871           /* This can handle deltas up to 0xffff.  This takes 3 bytes.  */
9872           dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
9873                                "DW_LNS_fixed_advance_pc");
9874           dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
9875         }
9876       else
9877         {
9878           /* This can handle any delta.  This takes
9879              4+DWARF2_ADDR_SIZE bytes.  */
9880           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9881           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9882           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9883           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9884         }
9885
9886       strcpy (prev_line_label, line_label);
9887
9888       /* Emit debug info for the source file of the current line, if
9889          different from the previous line.  */
9890       if (line_info->dw_file_num != current_file)
9891         {
9892           current_file = line_info->dw_file_num;
9893           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
9894           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
9895         }
9896
9897       /* Emit debug info for the current line number, choosing the encoding
9898          that uses the least amount of space.  */
9899       if (line_info->dw_line_num != current_line)
9900         {
9901           line_offset = line_info->dw_line_num - current_line;
9902           line_delta = line_offset - DWARF_LINE_BASE;
9903           current_line = line_info->dw_line_num;
9904           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
9905             /* This can handle deltas from -10 to 234, using the current
9906                definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.  This
9907                takes 1 byte.  */
9908             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
9909                                  "line %lu", current_line);
9910           else
9911             {
9912               /* This can handle any delta.  This takes at least 4 bytes,
9913                  depending on the value being encoded.  */
9914               dw2_asm_output_data (1, DW_LNS_advance_line,
9915                                    "advance to line %lu", current_line);
9916               dw2_asm_output_data_sleb128 (line_offset, NULL);
9917               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
9918             }
9919         }
9920       else
9921         /* We still need to start a new row, so output a copy insn.  */
9922         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
9923     }
9924
9925   /* Emit debug info for the address of the end of the function.  */
9926   if (0)
9927     {
9928       dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
9929                            "DW_LNS_fixed_advance_pc");
9930       dw2_asm_output_delta (2, text_end_label, prev_line_label, NULL);
9931     }
9932   else
9933     {
9934       dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9935       dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9936       dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9937       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_end_label, NULL);
9938     }
9939
9940   dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
9941   dw2_asm_output_data_uleb128 (1, NULL);
9942   dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
9943
9944   function = 0;
9945   current_file = 1;
9946   current_line = 1;
9947   for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
9948     {
9949       dw_separate_line_info_ref line_info
9950         = &separate_line_info_table[lt_index];
9951
9952 #if 0
9953       /* Don't emit anything for redundant notes.  */
9954       if (line_info->dw_line_num == current_line
9955           && line_info->dw_file_num == current_file
9956           && line_info->function == function)
9957         goto cont;
9958 #endif
9959
9960       /* Emit debug info for the address of the current line.  If this is
9961          a new function, or the first line of a function, then we need
9962          to handle it differently.  */
9963       ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
9964                                    lt_index);
9965       if (function != line_info->function)
9966         {
9967           function = line_info->function;
9968
9969           /* Set the address register to the first line in the function.  */
9970           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9971           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9972           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9973           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9974         }
9975       else
9976         {
9977           /* ??? See the DW_LNS_advance_pc comment above.  */
9978           if (0)
9979             {
9980               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
9981                                    "DW_LNS_fixed_advance_pc");
9982               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
9983             }
9984           else
9985             {
9986               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9987               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9988               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9989               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9990             }
9991         }
9992
9993       strcpy (prev_line_label, line_label);
9994
9995       /* Emit debug info for the source file of the current line, if
9996          different from the previous line.  */
9997       if (line_info->dw_file_num != current_file)
9998         {
9999           current_file = line_info->dw_file_num;
10000           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
10001           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
10002         }
10003
10004       /* Emit debug info for the current line number, choosing the encoding
10005          that uses the least amount of space.  */
10006       if (line_info->dw_line_num != current_line)
10007         {
10008           line_offset = line_info->dw_line_num - current_line;
10009           line_delta = line_offset - DWARF_LINE_BASE;
10010           current_line = line_info->dw_line_num;
10011           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
10012             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
10013                                  "line %lu", current_line);
10014           else
10015             {
10016               dw2_asm_output_data (1, DW_LNS_advance_line,
10017                                    "advance to line %lu", current_line);
10018               dw2_asm_output_data_sleb128 (line_offset, NULL);
10019               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
10020             }
10021         }
10022       else
10023         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
10024
10025 #if 0
10026     cont:
10027 #endif
10028
10029       lt_index++;
10030
10031       /* If we're done with a function, end its sequence.  */
10032       if (lt_index == separate_line_info_table_in_use
10033           || separate_line_info_table[lt_index].function != function)
10034         {
10035           current_file = 1;
10036           current_line = 1;
10037
10038           /* Emit debug info for the address of the end of the function.  */
10039           ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
10040           if (0)
10041             {
10042               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
10043                                    "DW_LNS_fixed_advance_pc");
10044               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
10045             }
10046           else
10047             {
10048               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
10049               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10050               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10051               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
10052             }
10053
10054           /* Output the marker for the end of this sequence.  */
10055           dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
10056           dw2_asm_output_data_uleb128 (1, NULL);
10057           dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
10058         }
10059     }
10060
10061   /* Output the marker for the end of the line number info.  */
10062   ASM_OUTPUT_LABEL (asm_out_file, l2);
10063 }
10064 \f
10065 /* Given a pointer to a tree node for some base type, return a pointer to
10066    a DIE that describes the given type.
10067
10068    This routine must only be called for GCC type nodes that correspond to
10069    Dwarf base (fundamental) types.  */
10070
10071 static dw_die_ref
10072 base_type_die (tree type)
10073 {
10074   dw_die_ref base_type_result;
10075   enum dwarf_type encoding;
10076
10077   if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
10078     return 0;
10079
10080   /* If this is a subtype that should not be emitted as a subrange type,
10081      use the base type.  See subrange_type_for_debug_p.  */
10082   if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
10083     type = TREE_TYPE (type);
10084
10085   switch (TREE_CODE (type))
10086     {
10087     case INTEGER_TYPE:
10088       if (TYPE_STRING_FLAG (type))
10089         {
10090           if (TYPE_UNSIGNED (type))
10091             encoding = DW_ATE_unsigned_char;
10092           else
10093             encoding = DW_ATE_signed_char;
10094         }
10095       else if (TYPE_UNSIGNED (type))
10096         encoding = DW_ATE_unsigned;
10097       else
10098         encoding = DW_ATE_signed;
10099       break;
10100
10101     case REAL_TYPE:
10102       if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
10103         encoding = DW_ATE_decimal_float;
10104       else
10105         encoding = DW_ATE_float;
10106       break;
10107
10108     case FIXED_POINT_TYPE:
10109       if (TYPE_UNSIGNED (type))
10110         encoding = DW_ATE_unsigned_fixed;
10111       else
10112         encoding = DW_ATE_signed_fixed;
10113       break;
10114
10115       /* Dwarf2 doesn't know anything about complex ints, so use
10116          a user defined type for it.  */
10117     case COMPLEX_TYPE:
10118       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
10119         encoding = DW_ATE_complex_float;
10120       else
10121         encoding = DW_ATE_lo_user;
10122       break;
10123
10124     case BOOLEAN_TYPE:
10125       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
10126       encoding = DW_ATE_boolean;
10127       break;
10128
10129     default:
10130       /* No other TREE_CODEs are Dwarf fundamental types.  */
10131       gcc_unreachable ();
10132     }
10133
10134   base_type_result = new_die (DW_TAG_base_type, comp_unit_die, type);
10135
10136   /* This probably indicates a bug.  */
10137   if (! TYPE_NAME (type))
10138     add_name_attribute (base_type_result, "__unknown__");
10139
10140   add_AT_unsigned (base_type_result, DW_AT_byte_size,
10141                    int_size_in_bytes (type));
10142   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
10143
10144   return base_type_result;
10145 }
10146
10147 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
10148    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
10149
10150 static inline int
10151 is_base_type (tree type)
10152 {
10153   switch (TREE_CODE (type))
10154     {
10155     case ERROR_MARK:
10156     case VOID_TYPE:
10157     case INTEGER_TYPE:
10158     case REAL_TYPE:
10159     case FIXED_POINT_TYPE:
10160     case COMPLEX_TYPE:
10161     case BOOLEAN_TYPE:
10162       return 1;
10163
10164     case ARRAY_TYPE:
10165     case RECORD_TYPE:
10166     case UNION_TYPE:
10167     case QUAL_UNION_TYPE:
10168     case ENUMERAL_TYPE:
10169     case FUNCTION_TYPE:
10170     case METHOD_TYPE:
10171     case POINTER_TYPE:
10172     case REFERENCE_TYPE:
10173     case OFFSET_TYPE:
10174     case LANG_TYPE:
10175     case VECTOR_TYPE:
10176       return 0;
10177
10178     default:
10179       gcc_unreachable ();
10180     }
10181
10182   return 0;
10183 }
10184
10185 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
10186    node, return the size in bits for the type if it is a constant, or else
10187    return the alignment for the type if the type's size is not constant, or
10188    else return BITS_PER_WORD if the type actually turns out to be an
10189    ERROR_MARK node.  */
10190
10191 static inline unsigned HOST_WIDE_INT
10192 simple_type_size_in_bits (const_tree type)
10193 {
10194   if (TREE_CODE (type) == ERROR_MARK)
10195     return BITS_PER_WORD;
10196   else if (TYPE_SIZE (type) == NULL_TREE)
10197     return 0;
10198   else if (host_integerp (TYPE_SIZE (type), 1))
10199     return tree_low_cst (TYPE_SIZE (type), 1);
10200   else
10201     return TYPE_ALIGN (type);
10202 }
10203
10204 /*  Given a pointer to a tree node for a subrange type, return a pointer
10205     to a DIE that describes the given type.  */
10206
10207 static dw_die_ref
10208 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
10209 {
10210   dw_die_ref subrange_die;
10211   const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
10212
10213   if (context_die == NULL)
10214     context_die = comp_unit_die;
10215
10216   subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
10217
10218   if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
10219     {
10220       /* The size of the subrange type and its base type do not match,
10221          so we need to generate a size attribute for the subrange type.  */
10222       add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
10223     }
10224
10225   if (low)
10226     add_bound_info (subrange_die, DW_AT_lower_bound, low);
10227   if (high)
10228     add_bound_info (subrange_die, DW_AT_upper_bound, high);
10229
10230   return subrange_die;
10231 }
10232
10233 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
10234    entry that chains various modifiers in front of the given type.  */
10235
10236 static dw_die_ref
10237 modified_type_die (tree type, int is_const_type, int is_volatile_type,
10238                    dw_die_ref context_die)
10239 {
10240   enum tree_code code = TREE_CODE (type);
10241   dw_die_ref mod_type_die;
10242   dw_die_ref sub_die = NULL;
10243   tree item_type = NULL;
10244   tree qualified_type;
10245   tree name, low, high;
10246
10247   if (code == ERROR_MARK)
10248     return NULL;
10249
10250   /* See if we already have the appropriately qualified variant of
10251      this type.  */
10252   qualified_type
10253     = get_qualified_type (type,
10254                           ((is_const_type ? TYPE_QUAL_CONST : 0)
10255                            | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
10256
10257   /* If we do, then we can just use its DIE, if it exists.  */
10258   if (qualified_type)
10259     {
10260       mod_type_die = lookup_type_die (qualified_type);
10261       if (mod_type_die)
10262         return mod_type_die;
10263     }
10264
10265   name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
10266
10267   /* Handle C typedef types.  */
10268   if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name))
10269     {
10270       tree dtype = TREE_TYPE (name);
10271
10272       if (qualified_type == dtype)
10273         {
10274           /* For a named type, use the typedef.  */
10275           gen_type_die (qualified_type, context_die);
10276           return lookup_type_die (qualified_type);
10277         }
10278       else if (is_const_type < TYPE_READONLY (dtype)
10279                || is_volatile_type < TYPE_VOLATILE (dtype)
10280                || (is_const_type <= TYPE_READONLY (dtype)
10281                    && is_volatile_type <= TYPE_VOLATILE (dtype)
10282                    && DECL_ORIGINAL_TYPE (name) != type))
10283         /* cv-unqualified version of named type.  Just use the unnamed
10284            type to which it refers.  */
10285         return modified_type_die (DECL_ORIGINAL_TYPE (name),
10286                                   is_const_type, is_volatile_type,
10287                                   context_die);
10288       /* Else cv-qualified version of named type; fall through.  */
10289     }
10290
10291   if (is_const_type)
10292     {
10293       mod_type_die = new_die (DW_TAG_const_type, comp_unit_die, type);
10294       sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
10295     }
10296   else if (is_volatile_type)
10297     {
10298       mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die, type);
10299       sub_die = modified_type_die (type, 0, 0, context_die);
10300     }
10301   else if (code == POINTER_TYPE)
10302     {
10303       mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die, type);
10304       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10305                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
10306       item_type = TREE_TYPE (type);
10307     }
10308   else if (code == REFERENCE_TYPE)
10309     {
10310       mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die, type);
10311       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10312                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
10313       item_type = TREE_TYPE (type);
10314     }
10315   else if (code == INTEGER_TYPE
10316            && TREE_TYPE (type) != NULL_TREE
10317            && subrange_type_for_debug_p (type, &low, &high))
10318     {
10319       mod_type_die = subrange_type_die (type, low, high, context_die);
10320       item_type = TREE_TYPE (type);
10321     }
10322   else if (is_base_type (type))
10323     mod_type_die = base_type_die (type);
10324   else
10325     {
10326       gen_type_die (type, context_die);
10327
10328       /* We have to get the type_main_variant here (and pass that to the
10329          `lookup_type_die' routine) because the ..._TYPE node we have
10330          might simply be a *copy* of some original type node (where the
10331          copy was created to help us keep track of typedef names) and
10332          that copy might have a different TYPE_UID from the original
10333          ..._TYPE node.  */
10334       if (TREE_CODE (type) != VECTOR_TYPE)
10335         return lookup_type_die (type_main_variant (type));
10336       else
10337         /* Vectors have the debugging information in the type,
10338            not the main variant.  */
10339         return lookup_type_die (type);
10340     }
10341
10342   /* Builtin types don't have a DECL_ORIGINAL_TYPE.  For those,
10343      don't output a DW_TAG_typedef, since there isn't one in the
10344      user's program; just attach a DW_AT_name to the type.  */
10345   if (name
10346       && (TREE_CODE (name) != TYPE_DECL
10347           || (TREE_TYPE (name) == qualified_type && DECL_NAME (name))))
10348     {
10349       if (TREE_CODE (name) == TYPE_DECL)
10350         /* Could just call add_name_and_src_coords_attributes here,
10351            but since this is a builtin type it doesn't have any
10352            useful source coordinates anyway.  */
10353         name = DECL_NAME (name);
10354       add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
10355     }
10356
10357   if (qualified_type)
10358     equate_type_number_to_die (qualified_type, mod_type_die);
10359
10360   if (item_type)
10361     /* We must do this after the equate_type_number_to_die call, in case
10362        this is a recursive type.  This ensures that the modified_type_die
10363        recursion will terminate even if the type is recursive.  Recursive
10364        types are possible in Ada.  */
10365     sub_die = modified_type_die (item_type,
10366                                  TYPE_READONLY (item_type),
10367                                  TYPE_VOLATILE (item_type),
10368                                  context_die);
10369
10370   if (sub_die != NULL)
10371     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
10372
10373   return mod_type_die;
10374 }
10375
10376 /* Generate a new name for the parameter pack name NAME (an
10377    IDENTIFIER_NODE) that incorporates its */
10378
10379 static tree
10380 make_ith_pack_parameter_name (tree name, int i)
10381 {
10382   /* Munge the name to include the parameter index.  */
10383 #define NUMBUF_LEN 128
10384   char numbuf[NUMBUF_LEN];
10385   char* newname;
10386   int newname_len;
10387
10388   snprintf (numbuf, NUMBUF_LEN, "%i", i);
10389   newname_len = IDENTIFIER_LENGTH (name)
10390                 + strnlen (numbuf, NUMBUF_LEN) + 2;
10391   newname = (char*) alloca (newname_len);
10392   snprintf (newname, newname_len,
10393             "%s#%i", IDENTIFIER_POINTER (name), i);
10394   return get_identifier (newname);
10395 }
10396
10397 /* Generate DIEs for the generic parameters of T.
10398    T must be either a generic type or a generic function.
10399    See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more.  */
10400
10401 static void
10402 gen_generic_params_dies (tree t)
10403 {
10404   tree parms, args;
10405   int parms_num, i;
10406   dw_die_ref die = NULL;
10407
10408   if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
10409     return;
10410
10411   if (TYPE_P (t))
10412     die = lookup_type_die (t);
10413   else if (DECL_P (t))
10414     die = lookup_decl_die (t);
10415
10416   gcc_assert (die);
10417
10418   parms = lang_hooks.get_innermost_generic_parms (t);
10419   if (!parms)
10420     /* T has no generic parameter. It means T is neither a generic type
10421        or function. End of story.  */
10422     return;
10423
10424   parms_num = TREE_VEC_LENGTH (parms);
10425   args = lang_hooks.get_innermost_generic_args (t);
10426   for (i = 0; i < parms_num; i++)
10427     {
10428       tree parm, arg;
10429
10430       parm = TREE_VEC_ELT (parms, i);
10431       arg = TREE_VEC_ELT (args, i);
10432       if (parm && TREE_VALUE (parm) && arg)
10433         {
10434           tree pack_elems =
10435             lang_hooks.types.get_argument_pack_elems (arg);
10436           if (pack_elems)
10437             {
10438               /* So ARG is an argument pack and the elements of that pack
10439                  are stored in PACK_ELEMS.  */
10440               int i, len;
10441
10442               len = TREE_VEC_LENGTH (pack_elems);
10443               for (i = 0; i < len; i++)
10444                 generic_parameter_die (TREE_VALUE (parm),
10445                                        TREE_VEC_ELT (pack_elems, i),
10446                                        die, i);
10447             }
10448           else /* Arg is not an argument pack.  */
10449             generic_parameter_die (TREE_VALUE (parm),
10450                                    arg, die,
10451                                    -1/* Not a param pack.  */);
10452         }
10453     }
10454 }
10455
10456 /* Create and return a DIE for PARM which should be
10457    the representation of a generic type parameter.
10458    For instance, in the C++ front end, PARM would be a template parameter.
10459    ARG is the argument to PARM.
10460    PARENT_DIE is the parent DIE which the new created DIE should be added to,
10461    as a child node.
10462    PACK_ELEM_INDEX is >= 0 if PARM is a generic parameter pack, and if ARG
10463    is one of the unpacked elements of the parameter PACK. In that case,
10464    PACK_ELEM_INDEX is the index of ARG in the parameter pack.  */
10465
10466 static dw_die_ref
10467 generic_parameter_die (tree parm, tree arg, dw_die_ref parent_die,
10468                        int pack_elem_index)
10469 {
10470   dw_die_ref tmpl_die = NULL;
10471   const char *name = NULL;
10472
10473   if (!parm || !DECL_NAME (parm) || !arg)
10474     return NULL;
10475
10476   /* We support non-type generic parameters and arguments,
10477      type generic parameters and arguments, as well as
10478      generic generic parameters (a.k.a. template template parameters in C++)
10479      and arguments.  */
10480   if (TREE_CODE (parm) == PARM_DECL)
10481     /* PARM is a nontype generic parameter  */
10482     tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
10483   else if (TREE_CODE (parm) == TYPE_DECL)
10484     /* PARM is a type generic parameter.  */
10485     tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
10486   else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10487     /* PARM is a generic generic parameter.
10488        Its DIE is a GNU extension. It shall have a
10489        DW_AT_name attribute to represent the name of the template template
10490        parameter, and a DW_AT_GNU_template_name attribute to represent the
10491        name of the template template argument.  */
10492     tmpl_die = new_die (DW_TAG_GNU_template_template_param,
10493                         parent_die, parm);
10494   else
10495     gcc_unreachable ();
10496
10497   if (tmpl_die)
10498     {
10499       tree tmpl_type;
10500
10501       if (pack_elem_index >= 0)
10502         {
10503           /* PARM is an element of a parameter pack.
10504              Generate a name for it.  */
10505           tree identifier = make_ith_pack_parameter_name (DECL_NAME (parm),
10506                                                           pack_elem_index);
10507           if (identifier)
10508             name = IDENTIFIER_POINTER (identifier);
10509         }
10510       else
10511         name = IDENTIFIER_POINTER (DECL_NAME (parm));
10512
10513       gcc_assert (name);
10514       add_AT_string (tmpl_die, DW_AT_name, name);
10515
10516       if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10517         {
10518           /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
10519              TMPL_DIE should have a child DW_AT_type attribute that is set
10520              to the type of the argument to PARM, which is ARG.
10521              If PARM is a type generic parameter, TMPL_DIE should have a
10522              child DW_AT_type that is set to ARG.  */
10523           tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
10524           add_type_attribute (tmpl_die, tmpl_type, 0,
10525                               TREE_THIS_VOLATILE (tmpl_type),
10526                               parent_die);
10527         }
10528       else
10529         {
10530           /* So TMPL_DIE is a DIE representing a
10531              a generic generic template parameter, a.k.a template template
10532              parameter in C++ and arg is a template.  */
10533
10534           /* The DW_AT_GNU_template_name attribute of the DIE must be set
10535              to the name of the argument.  */
10536           name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
10537           add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
10538         }
10539
10540       if (TREE_CODE (parm) == PARM_DECL)
10541         /* So PARM is a non-type generic parameter.
10542            DWARF3 5.6.8 says we must set a DW_AT_const_value child
10543            attribute of TMPL_DIE which value represents the value
10544            of ARG.
10545            We must be careful here:
10546            The value of ARG might reference some function decls.
10547            We might currently be emitting debug info for a generic
10548            type and types are emitted before function decls, we don't
10549            know if the function decls referenced by ARG will actually be
10550            emitted after cgraph computations.
10551            So must defer the generation of the DW_AT_const_value to
10552            after cgraph is ready.  */
10553         append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
10554     }
10555
10556   return tmpl_die;
10557 }
10558
10559 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
10560    an enumerated type.  */
10561
10562 static inline int
10563 type_is_enum (const_tree type)
10564 {
10565   return TREE_CODE (type) == ENUMERAL_TYPE;
10566 }
10567
10568 /* Return the DBX register number described by a given RTL node.  */
10569
10570 static unsigned int
10571 dbx_reg_number (const_rtx rtl)
10572 {
10573   unsigned regno = REGNO (rtl);
10574
10575   gcc_assert (regno < FIRST_PSEUDO_REGISTER);
10576
10577 #ifdef LEAF_REG_REMAP
10578   if (current_function_uses_only_leaf_regs)
10579     {
10580       int leaf_reg = LEAF_REG_REMAP (regno);
10581       if (leaf_reg != -1)
10582         regno = (unsigned) leaf_reg;
10583     }
10584 #endif
10585
10586   return DBX_REGISTER_NUMBER (regno);
10587 }
10588
10589 /* Optionally add a DW_OP_piece term to a location description expression.
10590    DW_OP_piece is only added if the location description expression already
10591    doesn't end with DW_OP_piece.  */
10592
10593 static void
10594 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
10595 {
10596   dw_loc_descr_ref loc;
10597
10598   if (*list_head != NULL)
10599     {
10600       /* Find the end of the chain.  */
10601       for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
10602         ;
10603
10604       if (loc->dw_loc_opc != DW_OP_piece)
10605         loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
10606     }
10607 }
10608
10609 /* Return a location descriptor that designates a machine register or
10610    zero if there is none.  */
10611
10612 static dw_loc_descr_ref
10613 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
10614 {
10615   rtx regs;
10616
10617   if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
10618     return 0;
10619
10620   regs = targetm.dwarf_register_span (rtl);
10621
10622   if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
10623     return multiple_reg_loc_descriptor (rtl, regs, initialized);
10624   else
10625     return one_reg_loc_descriptor (dbx_reg_number (rtl), initialized);
10626 }
10627
10628 /* Return a location descriptor that designates a machine register for
10629    a given hard register number.  */
10630
10631 static dw_loc_descr_ref
10632 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
10633 {
10634   dw_loc_descr_ref reg_loc_descr;
10635
10636   if (regno <= 31)
10637     reg_loc_descr
10638       = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
10639   else
10640     reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
10641
10642   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10643     add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10644
10645   return reg_loc_descr;
10646 }
10647
10648 /* Given an RTL of a register, return a location descriptor that
10649    designates a value that spans more than one register.  */
10650
10651 static dw_loc_descr_ref
10652 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
10653                              enum var_init_status initialized)
10654 {
10655   int nregs, size, i;
10656   unsigned reg;
10657   dw_loc_descr_ref loc_result = NULL;
10658
10659   reg = REGNO (rtl);
10660 #ifdef LEAF_REG_REMAP
10661   if (current_function_uses_only_leaf_regs)
10662     {
10663       int leaf_reg = LEAF_REG_REMAP (reg);
10664       if (leaf_reg != -1)
10665         reg = (unsigned) leaf_reg;
10666     }
10667 #endif
10668   gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
10669   nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
10670
10671   /* Simple, contiguous registers.  */
10672   if (regs == NULL_RTX)
10673     {
10674       size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
10675
10676       loc_result = NULL;
10677       while (nregs--)
10678         {
10679           dw_loc_descr_ref t;
10680
10681           t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
10682                                       VAR_INIT_STATUS_INITIALIZED);
10683           add_loc_descr (&loc_result, t);
10684           add_loc_descr_op_piece (&loc_result, size);
10685           ++reg;
10686         }
10687       return loc_result;
10688     }
10689
10690   /* Now onto stupid register sets in non contiguous locations.  */
10691
10692   gcc_assert (GET_CODE (regs) == PARALLEL);
10693
10694   size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
10695   loc_result = NULL;
10696
10697   for (i = 0; i < XVECLEN (regs, 0); ++i)
10698     {
10699       dw_loc_descr_ref t;
10700
10701       t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
10702                                   VAR_INIT_STATUS_INITIALIZED);
10703       add_loc_descr (&loc_result, t);
10704       size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
10705       add_loc_descr_op_piece (&loc_result, size);
10706     }
10707
10708   if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10709     add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10710   return loc_result;
10711 }
10712
10713 #endif /* DWARF2_DEBUGGING_INFO */
10714
10715 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
10716
10717 /* Return a location descriptor that designates a constant.  */
10718
10719 static dw_loc_descr_ref
10720 int_loc_descriptor (HOST_WIDE_INT i)
10721 {
10722   enum dwarf_location_atom op;
10723
10724   /* Pick the smallest representation of a constant, rather than just
10725      defaulting to the LEB encoding.  */
10726   if (i >= 0)
10727     {
10728       if (i <= 31)
10729         op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
10730       else if (i <= 0xff)
10731         op = DW_OP_const1u;
10732       else if (i <= 0xffff)
10733         op = DW_OP_const2u;
10734       else if (HOST_BITS_PER_WIDE_INT == 32
10735                || i <= 0xffffffff)
10736         op = DW_OP_const4u;
10737       else
10738         op = DW_OP_constu;
10739     }
10740   else
10741     {
10742       if (i >= -0x80)
10743         op = DW_OP_const1s;
10744       else if (i >= -0x8000)
10745         op = DW_OP_const2s;
10746       else if (HOST_BITS_PER_WIDE_INT == 32
10747                || i >= -0x80000000)
10748         op = DW_OP_const4s;
10749       else
10750         op = DW_OP_consts;
10751     }
10752
10753   return new_loc_descr (op, i, 0);
10754 }
10755 #endif
10756
10757 #ifdef DWARF2_DEBUGGING_INFO
10758
10759 /* Return a location descriptor that designates a base+offset location.  */
10760
10761 static dw_loc_descr_ref
10762 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
10763                  enum var_init_status initialized)
10764 {
10765   unsigned int regno;
10766   dw_loc_descr_ref result;
10767   dw_fde_ref fde = current_fde ();
10768
10769   /* We only use "frame base" when we're sure we're talking about the
10770      post-prologue local stack frame.  We do this by *not* running
10771      register elimination until this point, and recognizing the special
10772      argument pointer and soft frame pointer rtx's.  */
10773   if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
10774     {
10775       rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
10776
10777       if (elim != reg)
10778         {
10779           if (GET_CODE (elim) == PLUS)
10780             {
10781               offset += INTVAL (XEXP (elim, 1));
10782               elim = XEXP (elim, 0);
10783             }
10784           gcc_assert ((SUPPORTS_STACK_ALIGNMENT
10785                        && (elim == hard_frame_pointer_rtx
10786                            || elim == stack_pointer_rtx))
10787                       || elim == (frame_pointer_needed
10788                                   ? hard_frame_pointer_rtx
10789                                   : stack_pointer_rtx));
10790
10791           /* If drap register is used to align stack, use frame
10792              pointer + offset to access stack variables.  If stack
10793              is aligned without drap, use stack pointer + offset to
10794              access stack variables.  */
10795           if (crtl->stack_realign_tried
10796               && reg == frame_pointer_rtx)
10797             {
10798               int base_reg
10799                 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
10800                                       ? HARD_FRAME_POINTER_REGNUM
10801                                       : STACK_POINTER_REGNUM);
10802               return new_reg_loc_descr (base_reg, offset);
10803             }
10804
10805           offset += frame_pointer_fb_offset;
10806           return new_loc_descr (DW_OP_fbreg, offset, 0);
10807         }
10808     }
10809   else if (fde
10810            && fde->drap_reg != INVALID_REGNUM
10811            && (fde->drap_reg == REGNO (reg)
10812                || fde->vdrap_reg == REGNO (reg)))
10813     {
10814       /* Use cfa+offset to represent the location of arguments passed
10815          on stack when drap is used to align stack.  */
10816       return new_loc_descr (DW_OP_fbreg, offset, 0);
10817     }
10818
10819   regno = dbx_reg_number (reg);
10820   if (regno <= 31)
10821     result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
10822                             offset, 0);
10823   else
10824     result = new_loc_descr (DW_OP_bregx, regno, offset);
10825
10826   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10827     add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10828
10829   return result;
10830 }
10831
10832 /* Return true if this RTL expression describes a base+offset calculation.  */
10833
10834 static inline int
10835 is_based_loc (const_rtx rtl)
10836 {
10837   return (GET_CODE (rtl) == PLUS
10838           && ((REG_P (XEXP (rtl, 0))
10839                && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
10840                && CONST_INT_P (XEXP (rtl, 1)))));
10841 }
10842
10843 /* Return a descriptor that describes the concatenation of N locations
10844    used to form the address of a memory location.  */
10845
10846 static dw_loc_descr_ref
10847 concatn_mem_loc_descriptor (rtx concatn, enum machine_mode mode,
10848                             enum var_init_status initialized)
10849 {
10850   unsigned int i;
10851   dw_loc_descr_ref cc_loc_result = NULL;
10852   unsigned int n = XVECLEN (concatn, 0);
10853
10854   for (i = 0; i < n; ++i)
10855     {
10856       dw_loc_descr_ref ref;
10857       rtx x = XVECEXP (concatn, 0, i);
10858
10859       ref = mem_loc_descriptor (x, mode, VAR_INIT_STATUS_INITIALIZED);
10860       if (ref == NULL)
10861         return NULL;
10862
10863       add_loc_descr (&cc_loc_result, ref);
10864       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
10865     }
10866
10867   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10868     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10869
10870   return cc_loc_result;
10871 }
10872
10873 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
10874    failed.  */
10875
10876 static dw_loc_descr_ref
10877 tls_mem_loc_descriptor (rtx mem)
10878 {
10879   tree base;
10880   dw_loc_descr_ref loc_result;
10881
10882   if (MEM_EXPR (mem) == NULL_TREE || MEM_OFFSET (mem) == NULL_RTX)
10883     return NULL;
10884
10885   base = get_base_address (MEM_EXPR (mem));
10886   if (base == NULL
10887       || TREE_CODE (base) != VAR_DECL
10888       || !DECL_THREAD_LOCAL_P (base))
10889     return NULL;
10890
10891   loc_result = loc_descriptor_from_tree_1 (MEM_EXPR (mem), 2);
10892   if (loc_result == NULL)
10893     return NULL;
10894
10895   if (INTVAL (MEM_OFFSET (mem)))
10896     loc_descr_plus_const (&loc_result, INTVAL (MEM_OFFSET (mem)));
10897
10898   return loc_result;
10899 }
10900
10901 /* The following routine converts the RTL for a variable or parameter
10902    (resident in memory) into an equivalent Dwarf representation of a
10903    mechanism for getting the address of that same variable onto the top of a
10904    hypothetical "address evaluation" stack.
10905
10906    When creating memory location descriptors, we are effectively transforming
10907    the RTL for a memory-resident object into its Dwarf postfix expression
10908    equivalent.  This routine recursively descends an RTL tree, turning
10909    it into Dwarf postfix code as it goes.
10910
10911    MODE is the mode of the memory reference, needed to handle some
10912    autoincrement addressing modes.
10913
10914    CAN_USE_FBREG is a flag whether we can use DW_AT_frame_base in the
10915    location list for RTL.
10916
10917    Return 0 if we can't represent the location.  */
10918
10919 static dw_loc_descr_ref
10920 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
10921                     enum var_init_status initialized)
10922 {
10923   dw_loc_descr_ref mem_loc_result = NULL;
10924   enum dwarf_location_atom op;
10925
10926   /* Note that for a dynamically sized array, the location we will generate a
10927      description of here will be the lowest numbered location which is
10928      actually within the array.  That's *not* necessarily the same as the
10929      zeroth element of the array.  */
10930
10931   rtl = targetm.delegitimize_address (rtl);
10932
10933   switch (GET_CODE (rtl))
10934     {
10935     case POST_INC:
10936     case POST_DEC:
10937     case POST_MODIFY:
10938       /* POST_INC and POST_DEC can be handled just like a SUBREG.  So we
10939          just fall into the SUBREG code.  */
10940
10941       /* ... fall through ...  */
10942
10943     case SUBREG:
10944       /* The case of a subreg may arise when we have a local (register)
10945          variable or a formal (register) parameter which doesn't quite fill
10946          up an entire register.  For now, just assume that it is
10947          legitimate to make the Dwarf info refer to the whole register which
10948          contains the given subreg.  */
10949       rtl = XEXP (rtl, 0);
10950
10951       /* ... fall through ...  */
10952
10953     case REG:
10954       /* Whenever a register number forms a part of the description of the
10955          method for calculating the (dynamic) address of a memory resident
10956          object, DWARF rules require the register number be referred to as
10957          a "base register".  This distinction is not based in any way upon
10958          what category of register the hardware believes the given register
10959          belongs to.  This is strictly DWARF terminology we're dealing with
10960          here. Note that in cases where the location of a memory-resident
10961          data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
10962          OP_CONST (0)) the actual DWARF location descriptor that we generate
10963          may just be OP_BASEREG (basereg).  This may look deceptively like
10964          the object in question was allocated to a register (rather than in
10965          memory) so DWARF consumers need to be aware of the subtle
10966          distinction between OP_REG and OP_BASEREG.  */
10967       if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
10968         mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
10969       else if (stack_realign_drap
10970                && crtl->drap_reg
10971                && crtl->args.internal_arg_pointer == rtl
10972                && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
10973         {
10974           /* If RTL is internal_arg_pointer, which has been optimized
10975              out, use DRAP instead.  */
10976           mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
10977                                             VAR_INIT_STATUS_INITIALIZED);
10978         }
10979       break;
10980
10981     case MEM:
10982       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
10983                                            VAR_INIT_STATUS_INITIALIZED);
10984       if (mem_loc_result == NULL)
10985         mem_loc_result = tls_mem_loc_descriptor (rtl);
10986       if (mem_loc_result != 0)
10987         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
10988       break;
10989
10990     case LO_SUM:
10991          rtl = XEXP (rtl, 1);
10992
10993       /* ... fall through ...  */
10994
10995     case LABEL_REF:
10996       /* Some ports can transform a symbol ref into a label ref, because
10997          the symbol ref is too far away and has to be dumped into a constant
10998          pool.  */
10999     case CONST:
11000     case SYMBOL_REF:
11001       /* Alternatively, the symbol in the constant pool might be referenced
11002          by a different symbol.  */
11003       if (GET_CODE (rtl) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (rtl))
11004         {
11005           bool marked;
11006           rtx tmp = get_pool_constant_mark (rtl, &marked);
11007
11008           if (GET_CODE (tmp) == SYMBOL_REF)
11009             {
11010               rtl = tmp;
11011               if (CONSTANT_POOL_ADDRESS_P (tmp))
11012                 get_pool_constant_mark (tmp, &marked);
11013               else
11014                 marked = true;
11015             }
11016
11017           /* If all references to this pool constant were optimized away,
11018              it was not output and thus we can't represent it.
11019              FIXME: might try to use DW_OP_const_value here, though
11020              DW_OP_piece complicates it.  */
11021           if (!marked)
11022             return 0;
11023         }
11024
11025       mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
11026       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
11027       mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
11028       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
11029       break;
11030
11031     case PRE_MODIFY:
11032       /* Extract the PLUS expression nested inside and fall into
11033          PLUS code below.  */
11034       rtl = XEXP (rtl, 1);
11035       goto plus;
11036
11037     case PRE_INC:
11038     case PRE_DEC:
11039       /* Turn these into a PLUS expression and fall into the PLUS code
11040          below.  */
11041       rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
11042                           GEN_INT (GET_CODE (rtl) == PRE_INC
11043                                    ? GET_MODE_UNIT_SIZE (mode)
11044                                    : -GET_MODE_UNIT_SIZE (mode)));
11045
11046       /* ... fall through ...  */
11047
11048     case PLUS:
11049     plus:
11050       if (is_based_loc (rtl))
11051         mem_loc_result = based_loc_descr (XEXP (rtl, 0),
11052                                           INTVAL (XEXP (rtl, 1)),
11053                                           VAR_INIT_STATUS_INITIALIZED);
11054       else
11055         {
11056           mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode,
11057                                                VAR_INIT_STATUS_INITIALIZED);
11058           if (mem_loc_result == 0)
11059             break;
11060
11061           if (CONST_INT_P (XEXP (rtl, 1)))
11062             loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
11063           else
11064             {
11065               dw_loc_descr_ref mem_loc_result2
11066                 = mem_loc_descriptor (XEXP (rtl, 1), mode,
11067                                       VAR_INIT_STATUS_INITIALIZED);
11068               if (mem_loc_result2 == 0)
11069                 break;
11070               add_loc_descr (&mem_loc_result, mem_loc_result2);
11071               add_loc_descr (&mem_loc_result,
11072                              new_loc_descr (DW_OP_plus, 0, 0));
11073             }
11074         }
11075       break;
11076
11077     /* If a pseudo-reg is optimized away, it is possible for it to
11078        be replaced with a MEM containing a multiply or shift.  */
11079     case MULT:
11080       op = DW_OP_mul;
11081       goto do_binop;
11082
11083     case ASHIFT:
11084       op = DW_OP_shl;
11085       goto do_binop;
11086
11087     case ASHIFTRT:
11088       op = DW_OP_shra;
11089       goto do_binop;
11090
11091     case LSHIFTRT:
11092       op = DW_OP_shr;
11093       goto do_binop;
11094
11095     do_binop:
11096       {
11097         dw_loc_descr_ref op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
11098                                                    VAR_INIT_STATUS_INITIALIZED);
11099         dw_loc_descr_ref op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
11100                                                    VAR_INIT_STATUS_INITIALIZED);
11101
11102         if (op0 == 0 || op1 == 0)
11103           break;
11104
11105         mem_loc_result = op0;
11106         add_loc_descr (&mem_loc_result, op1);
11107         add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
11108         break;
11109       }
11110
11111     case CONST_INT:
11112       mem_loc_result = int_loc_descriptor (INTVAL (rtl));
11113       break;
11114
11115     case CONCATN:
11116       mem_loc_result = concatn_mem_loc_descriptor (rtl, mode,
11117                                                    VAR_INIT_STATUS_INITIALIZED);
11118       break;
11119
11120     case UNSPEC:
11121       /* If delegitimize_address couldn't do anything with the UNSPEC, we
11122          can't express it in the debug info.  This can happen e.g. with some
11123          TLS UNSPECs.  */
11124       break;
11125
11126     default:
11127       gcc_unreachable ();
11128     }
11129
11130   if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
11131     add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11132
11133   return mem_loc_result;
11134 }
11135
11136 /* Return a descriptor that describes the concatenation of two locations.
11137    This is typically a complex variable.  */
11138
11139 static dw_loc_descr_ref
11140 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
11141 {
11142   dw_loc_descr_ref cc_loc_result = NULL;
11143   dw_loc_descr_ref x0_ref = loc_descriptor (x0, VAR_INIT_STATUS_INITIALIZED);
11144   dw_loc_descr_ref x1_ref = loc_descriptor (x1, VAR_INIT_STATUS_INITIALIZED);
11145
11146   if (x0_ref == 0 || x1_ref == 0)
11147     return 0;
11148
11149   cc_loc_result = x0_ref;
11150   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
11151
11152   add_loc_descr (&cc_loc_result, x1_ref);
11153   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
11154
11155   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11156     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11157
11158   return cc_loc_result;
11159 }
11160
11161 /* Return a descriptor that describes the concatenation of N
11162    locations.  */
11163
11164 static dw_loc_descr_ref
11165 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
11166 {
11167   unsigned int i;
11168   dw_loc_descr_ref cc_loc_result = NULL;
11169   unsigned int n = XVECLEN (concatn, 0);
11170
11171   for (i = 0; i < n; ++i)
11172     {
11173       dw_loc_descr_ref ref;
11174       rtx x = XVECEXP (concatn, 0, i);
11175
11176       ref = loc_descriptor (x, VAR_INIT_STATUS_INITIALIZED);
11177       if (ref == NULL)
11178         return NULL;
11179
11180       add_loc_descr (&cc_loc_result, ref);
11181       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
11182     }
11183
11184   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
11185     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11186
11187   return cc_loc_result;
11188 }
11189
11190 /* Output a proper Dwarf location descriptor for a variable or parameter
11191    which is either allocated in a register or in a memory location.  For a
11192    register, we just generate an OP_REG and the register number.  For a
11193    memory location we provide a Dwarf postfix expression describing how to
11194    generate the (dynamic) address of the object onto the address stack.
11195
11196    If we don't know how to describe it, return 0.  */
11197
11198 static dw_loc_descr_ref
11199 loc_descriptor (rtx rtl, enum var_init_status initialized)
11200 {
11201   dw_loc_descr_ref loc_result = NULL;
11202
11203   switch (GET_CODE (rtl))
11204     {
11205     case SUBREG:
11206       /* The case of a subreg may arise when we have a local (register)
11207          variable or a formal (register) parameter which doesn't quite fill
11208          up an entire register.  For now, just assume that it is
11209          legitimate to make the Dwarf info refer to the whole register which
11210          contains the given subreg.  */
11211       rtl = SUBREG_REG (rtl);
11212
11213       /* ... fall through ...  */
11214
11215     case REG:
11216       loc_result = reg_loc_descriptor (rtl, initialized);
11217       break;
11218
11219     case MEM:
11220       loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
11221                                        initialized);
11222       if (loc_result == NULL)
11223         loc_result = tls_mem_loc_descriptor (rtl);
11224       break;
11225
11226     case CONCAT:
11227       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
11228                                           initialized);
11229       break;
11230
11231     case CONCATN:
11232       loc_result = concatn_loc_descriptor (rtl, initialized);
11233       break;
11234
11235     case VAR_LOCATION:
11236       /* Single part.  */
11237       if (GET_CODE (XEXP (rtl, 1)) != PARALLEL)
11238         {
11239           loc_result = loc_descriptor (XEXP (XEXP (rtl, 1), 0), initialized);
11240           break;
11241         }
11242
11243       rtl = XEXP (rtl, 1);
11244       /* FALLTHRU */
11245
11246     case PARALLEL:
11247       {
11248         rtvec par_elems = XVEC (rtl, 0);
11249         int num_elem = GET_NUM_ELEM (par_elems);
11250         enum machine_mode mode;
11251         int i;
11252
11253         /* Create the first one, so we have something to add to.  */
11254         loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
11255                                      initialized);
11256         if (loc_result == NULL)
11257           return NULL;
11258         mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
11259         add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
11260         for (i = 1; i < num_elem; i++)
11261           {
11262             dw_loc_descr_ref temp;
11263
11264             temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
11265                                    initialized);
11266             if (temp == NULL)
11267               return NULL;
11268             add_loc_descr (&loc_result, temp);
11269             mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
11270             add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
11271           }
11272       }
11273       break;
11274
11275     default:
11276       gcc_unreachable ();
11277     }
11278
11279   return loc_result;
11280 }
11281
11282 /* Similar, but generate the descriptor from trees instead of rtl.  This comes
11283    up particularly with variable length arrays.  WANT_ADDRESS is 2 if this is
11284    a top-level invocation of loc_descriptor_from_tree; is 1 if this is not a
11285    top-level invocation, and we require the address of LOC; is 0 if we require
11286    the value of LOC.  */
11287
11288 static dw_loc_descr_ref
11289 loc_descriptor_from_tree_1 (tree loc, int want_address)
11290 {
11291   dw_loc_descr_ref ret, ret1;
11292   int have_address = 0;
11293   enum dwarf_location_atom op;
11294
11295   /* ??? Most of the time we do not take proper care for sign/zero
11296      extending the values properly.  Hopefully this won't be a real
11297      problem...  */
11298
11299   switch (TREE_CODE (loc))
11300     {
11301     case ERROR_MARK:
11302       return 0;
11303
11304     case PLACEHOLDER_EXPR:
11305       /* This case involves extracting fields from an object to determine the
11306          position of other fields.  We don't try to encode this here.  The
11307          only user of this is Ada, which encodes the needed information using
11308          the names of types.  */
11309       return 0;
11310
11311     case CALL_EXPR:
11312       return 0;
11313
11314     case PREINCREMENT_EXPR:
11315     case PREDECREMENT_EXPR:
11316     case POSTINCREMENT_EXPR:
11317     case POSTDECREMENT_EXPR:
11318       /* There are no opcodes for these operations.  */
11319       return 0;
11320
11321     case ADDR_EXPR:
11322       /* If we already want an address, there's nothing we can do.  */
11323       if (want_address)
11324         return 0;
11325
11326       /* Otherwise, process the argument and look for the address.  */
11327       return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 1);
11328
11329     case VAR_DECL:
11330       if (DECL_THREAD_LOCAL_P (loc))
11331         {
11332           rtx rtl;
11333           enum dwarf_location_atom first_op;
11334           enum dwarf_location_atom second_op;
11335           bool dtprel = false;
11336
11337           if (targetm.have_tls)
11338             {
11339               /* If this is not defined, we have no way to emit the
11340                  data.  */
11341               if (!targetm.asm_out.output_dwarf_dtprel)
11342                 return 0;
11343
11344                /* The way DW_OP_GNU_push_tls_address is specified, we
11345                   can only look up addresses of objects in the current
11346                   module.  */
11347               if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
11348                 return 0;
11349               first_op = DW_OP_addr;
11350               dtprel = true;
11351               second_op = DW_OP_GNU_push_tls_address;
11352             }
11353           else
11354             {
11355               if (!targetm.emutls.debug_form_tls_address)
11356                 return 0;
11357               loc = emutls_decl (loc);
11358               first_op = DW_OP_addr;
11359               second_op = DW_OP_form_tls_address;
11360             }
11361
11362           rtl = rtl_for_decl_location (loc);
11363           if (rtl == NULL_RTX)
11364             return 0;
11365
11366           if (!MEM_P (rtl))
11367             return 0;
11368           rtl = XEXP (rtl, 0);
11369           if (! CONSTANT_P (rtl))
11370             return 0;
11371
11372           ret = new_loc_descr (first_op, 0, 0);
11373           ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
11374           ret->dw_loc_oprnd1.v.val_addr = rtl;
11375           ret->dtprel = dtprel;
11376
11377           ret1 = new_loc_descr (second_op, 0, 0);
11378           add_loc_descr (&ret, ret1);
11379
11380           have_address = 1;
11381           break;
11382         }
11383       /* FALLTHRU */
11384
11385     case PARM_DECL:
11386       if (DECL_HAS_VALUE_EXPR_P (loc))
11387         return loc_descriptor_from_tree_1 (DECL_VALUE_EXPR (loc),
11388                                            want_address);
11389       /* FALLTHRU */
11390
11391     case RESULT_DECL:
11392     case FUNCTION_DECL:
11393       {
11394         rtx rtl = rtl_for_decl_location (loc);
11395
11396         if (rtl == NULL_RTX)
11397           return 0;
11398         else if (CONST_INT_P (rtl))
11399           {
11400             HOST_WIDE_INT val = INTVAL (rtl);
11401             if (TYPE_UNSIGNED (TREE_TYPE (loc)))
11402               val &= GET_MODE_MASK (DECL_MODE (loc));
11403             ret = int_loc_descriptor (val);
11404           }
11405         else if (GET_CODE (rtl) == CONST_STRING)
11406           return 0;
11407         else if (CONSTANT_P (rtl))
11408           {
11409             ret = new_loc_descr (DW_OP_addr, 0, 0);
11410             ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
11411             ret->dw_loc_oprnd1.v.val_addr = rtl;
11412           }
11413         else
11414           {
11415             enum machine_mode mode;
11416
11417             /* Certain constructs can only be represented at top-level.  */
11418             if (want_address == 2)
11419               return loc_descriptor (rtl, VAR_INIT_STATUS_INITIALIZED);
11420
11421             mode = GET_MODE (rtl);
11422             if (MEM_P (rtl))
11423               {
11424                 rtl = XEXP (rtl, 0);
11425                 have_address = 1;
11426               }
11427             ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
11428           }
11429       }
11430       break;
11431
11432     case INDIRECT_REF:
11433       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
11434       have_address = 1;
11435       break;
11436
11437     case COMPOUND_EXPR:
11438       return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), want_address);
11439
11440     CASE_CONVERT:
11441     case VIEW_CONVERT_EXPR:
11442     case SAVE_EXPR:
11443     case MODIFY_EXPR:
11444       return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), want_address);
11445
11446     case COMPONENT_REF:
11447     case BIT_FIELD_REF:
11448     case ARRAY_REF:
11449     case ARRAY_RANGE_REF:
11450       {
11451         tree obj, offset;
11452         HOST_WIDE_INT bitsize, bitpos, bytepos;
11453         enum machine_mode mode;
11454         int volatilep;
11455         int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
11456
11457         obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
11458                                    &unsignedp, &volatilep, false);
11459
11460         if (obj == loc)
11461           return 0;
11462
11463         ret = loc_descriptor_from_tree_1 (obj, 1);
11464         if (ret == 0
11465             || bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
11466           return 0;
11467
11468         if (offset != NULL_TREE)
11469           {
11470             /* Variable offset.  */
11471             ret1 = loc_descriptor_from_tree_1 (offset, 0);
11472             if (ret1 == 0)
11473               return 0;
11474             add_loc_descr (&ret, ret1);
11475             add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
11476           }
11477
11478         bytepos = bitpos / BITS_PER_UNIT;
11479         loc_descr_plus_const (&ret, bytepos);
11480
11481         have_address = 1;
11482         break;
11483       }
11484
11485     case INTEGER_CST:
11486       if (host_integerp (loc, 0))
11487         ret = int_loc_descriptor (tree_low_cst (loc, 0));
11488       else
11489         return 0;
11490       break;
11491
11492     case CONSTRUCTOR:
11493       {
11494         /* Get an RTL for this, if something has been emitted.  */
11495         rtx rtl = lookup_constant_def (loc);
11496         enum machine_mode mode;
11497
11498         if (!rtl || !MEM_P (rtl))
11499           return 0;
11500         mode = GET_MODE (rtl);
11501         rtl = XEXP (rtl, 0);
11502         ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
11503         have_address = 1;
11504         break;
11505       }
11506
11507     case TRUTH_AND_EXPR:
11508     case TRUTH_ANDIF_EXPR:
11509     case BIT_AND_EXPR:
11510       op = DW_OP_and;
11511       goto do_binop;
11512
11513     case TRUTH_XOR_EXPR:
11514     case BIT_XOR_EXPR:
11515       op = DW_OP_xor;
11516       goto do_binop;
11517
11518     case TRUTH_OR_EXPR:
11519     case TRUTH_ORIF_EXPR:
11520     case BIT_IOR_EXPR:
11521       op = DW_OP_or;
11522       goto do_binop;
11523
11524     case FLOOR_DIV_EXPR:
11525     case CEIL_DIV_EXPR:
11526     case ROUND_DIV_EXPR:
11527     case TRUNC_DIV_EXPR:
11528       op = DW_OP_div;
11529       goto do_binop;
11530
11531     case MINUS_EXPR:
11532       op = DW_OP_minus;
11533       goto do_binop;
11534
11535     case FLOOR_MOD_EXPR:
11536     case CEIL_MOD_EXPR:
11537     case ROUND_MOD_EXPR:
11538     case TRUNC_MOD_EXPR:
11539       op = DW_OP_mod;
11540       goto do_binop;
11541
11542     case MULT_EXPR:
11543       op = DW_OP_mul;
11544       goto do_binop;
11545
11546     case LSHIFT_EXPR:
11547       op = DW_OP_shl;
11548       goto do_binop;
11549
11550     case RSHIFT_EXPR:
11551       op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
11552       goto do_binop;
11553
11554     case POINTER_PLUS_EXPR:
11555     case PLUS_EXPR:
11556       if (TREE_CODE (TREE_OPERAND (loc, 1)) == INTEGER_CST
11557           && host_integerp (TREE_OPERAND (loc, 1), 0))
11558         {
11559           ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
11560           if (ret == 0)
11561             return 0;
11562
11563           loc_descr_plus_const (&ret, tree_low_cst (TREE_OPERAND (loc, 1), 0));
11564           break;
11565         }
11566
11567       op = DW_OP_plus;
11568       goto do_binop;
11569
11570     case LE_EXPR:
11571       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
11572         return 0;
11573
11574       op = DW_OP_le;
11575       goto do_binop;
11576
11577     case GE_EXPR:
11578       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
11579         return 0;
11580
11581       op = DW_OP_ge;
11582       goto do_binop;
11583
11584     case LT_EXPR:
11585       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
11586         return 0;
11587
11588       op = DW_OP_lt;
11589       goto do_binop;
11590
11591     case GT_EXPR:
11592       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
11593         return 0;
11594
11595       op = DW_OP_gt;
11596       goto do_binop;
11597
11598     case EQ_EXPR:
11599       op = DW_OP_eq;
11600       goto do_binop;
11601
11602     case NE_EXPR:
11603       op = DW_OP_ne;
11604       goto do_binop;
11605
11606     do_binop:
11607       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
11608       ret1 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
11609       if (ret == 0 || ret1 == 0)
11610         return 0;
11611
11612       add_loc_descr (&ret, ret1);
11613       add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11614       break;
11615
11616     case TRUTH_NOT_EXPR:
11617     case BIT_NOT_EXPR:
11618       op = DW_OP_not;
11619       goto do_unop;
11620
11621     case ABS_EXPR:
11622       op = DW_OP_abs;
11623       goto do_unop;
11624
11625     case NEGATE_EXPR:
11626       op = DW_OP_neg;
11627       goto do_unop;
11628
11629     do_unop:
11630       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
11631       if (ret == 0)
11632         return 0;
11633
11634       add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11635       break;
11636
11637     case MIN_EXPR:
11638     case MAX_EXPR:
11639       {
11640         const enum tree_code code =
11641           TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
11642
11643         loc = build3 (COND_EXPR, TREE_TYPE (loc),
11644                       build2 (code, integer_type_node,
11645                               TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
11646                       TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
11647       }
11648
11649       /* ... fall through ...  */
11650
11651     case COND_EXPR:
11652       {
11653         dw_loc_descr_ref lhs
11654           = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
11655         dw_loc_descr_ref rhs
11656           = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 2), 0);
11657         dw_loc_descr_ref bra_node, jump_node, tmp;
11658
11659         ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
11660         if (ret == 0 || lhs == 0 || rhs == 0)
11661           return 0;
11662
11663         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
11664         add_loc_descr (&ret, bra_node);
11665
11666         add_loc_descr (&ret, rhs);
11667         jump_node = new_loc_descr (DW_OP_skip, 0, 0);
11668         add_loc_descr (&ret, jump_node);
11669
11670         add_loc_descr (&ret, lhs);
11671         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
11672         bra_node->dw_loc_oprnd1.v.val_loc = lhs;
11673
11674         /* ??? Need a node to point the skip at.  Use a nop.  */
11675         tmp = new_loc_descr (DW_OP_nop, 0, 0);
11676         add_loc_descr (&ret, tmp);
11677         jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
11678         jump_node->dw_loc_oprnd1.v.val_loc = tmp;
11679       }
11680       break;
11681
11682     case FIX_TRUNC_EXPR:
11683       return 0;
11684
11685     default:
11686       /* Leave front-end specific codes as simply unknown.  This comes
11687          up, for instance, with the C STMT_EXPR.  */
11688       if ((unsigned int) TREE_CODE (loc)
11689           >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
11690         return 0;
11691
11692 #ifdef ENABLE_CHECKING
11693       /* Otherwise this is a generic code; we should just lists all of
11694          these explicitly.  We forgot one.  */
11695       gcc_unreachable ();
11696 #else
11697       /* In a release build, we want to degrade gracefully: better to
11698          generate incomplete debugging information than to crash.  */
11699       return NULL;
11700 #endif
11701     }
11702
11703   /* Show if we can't fill the request for an address.  */
11704   if (want_address && !have_address)
11705     return 0;
11706
11707   /* If we've got an address and don't want one, dereference.  */
11708   if (!want_address && have_address && ret)
11709     {
11710       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
11711
11712       if (size > DWARF2_ADDR_SIZE || size == -1)
11713         return 0;
11714       else if (size == DWARF2_ADDR_SIZE)
11715         op = DW_OP_deref;
11716       else
11717         op = DW_OP_deref_size;
11718
11719       add_loc_descr (&ret, new_loc_descr (op, size, 0));
11720     }
11721
11722   return ret;
11723 }
11724
11725 static inline dw_loc_descr_ref
11726 loc_descriptor_from_tree (tree loc)
11727 {
11728   return loc_descriptor_from_tree_1 (loc, 2);
11729 }
11730
11731 /* Given a value, round it up to the lowest multiple of `boundary'
11732    which is not less than the value itself.  */
11733
11734 static inline HOST_WIDE_INT
11735 ceiling (HOST_WIDE_INT value, unsigned int boundary)
11736 {
11737   return (((value + boundary - 1) / boundary) * boundary);
11738 }
11739
11740 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
11741    pointer to the declared type for the relevant field variable, or return
11742    `integer_type_node' if the given node turns out to be an
11743    ERROR_MARK node.  */
11744
11745 static inline tree
11746 field_type (const_tree decl)
11747 {
11748   tree type;
11749
11750   if (TREE_CODE (decl) == ERROR_MARK)
11751     return integer_type_node;
11752
11753   type = DECL_BIT_FIELD_TYPE (decl);
11754   if (type == NULL_TREE)
11755     type = TREE_TYPE (decl);
11756
11757   return type;
11758 }
11759
11760 /* Given a pointer to a tree node, return the alignment in bits for
11761    it, or else return BITS_PER_WORD if the node actually turns out to
11762    be an ERROR_MARK node.  */
11763
11764 static inline unsigned
11765 simple_type_align_in_bits (const_tree type)
11766 {
11767   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
11768 }
11769
11770 static inline unsigned
11771 simple_decl_align_in_bits (const_tree decl)
11772 {
11773   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
11774 }
11775
11776 /* Return the result of rounding T up to ALIGN.  */
11777
11778 static inline HOST_WIDE_INT
11779 round_up_to_align (HOST_WIDE_INT t, unsigned int align)
11780 {
11781   /* We must be careful if T is negative because HOST_WIDE_INT can be
11782      either "above" or "below" unsigned int as per the C promotion
11783      rules, depending on the host, thus making the signedness of the
11784      direct multiplication and division unpredictable.  */
11785   unsigned HOST_WIDE_INT u = (unsigned HOST_WIDE_INT) t;
11786
11787   u += align - 1;
11788   u /= align;
11789   u *= align;
11790
11791   return (HOST_WIDE_INT) u;
11792 }
11793
11794 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
11795    lowest addressed byte of the "containing object" for the given FIELD_DECL,
11796    or return 0 if we are unable to determine what that offset is, either
11797    because the argument turns out to be a pointer to an ERROR_MARK node, or
11798    because the offset is actually variable.  (We can't handle the latter case
11799    just yet).  */
11800
11801 static HOST_WIDE_INT
11802 field_byte_offset (const_tree decl)
11803 {
11804   HOST_WIDE_INT object_offset_in_bits;
11805   HOST_WIDE_INT bitpos_int;
11806
11807   if (TREE_CODE (decl) == ERROR_MARK)
11808     return 0;
11809
11810   gcc_assert (TREE_CODE (decl) == FIELD_DECL);
11811
11812   /* We cannot yet cope with fields whose positions are variable, so
11813      for now, when we see such things, we simply return 0.  Someday, we may
11814      be able to handle such cases, but it will be damn difficult.  */
11815   if (! host_integerp (bit_position (decl), 0))
11816     return 0;
11817
11818   bitpos_int = int_bit_position (decl);
11819
11820 #ifdef PCC_BITFIELD_TYPE_MATTERS
11821   if (PCC_BITFIELD_TYPE_MATTERS)
11822     {
11823       tree type;
11824       tree field_size_tree;
11825       HOST_WIDE_INT deepest_bitpos;
11826       unsigned HOST_WIDE_INT field_size_in_bits;
11827       unsigned int type_align_in_bits;
11828       unsigned int decl_align_in_bits;
11829       unsigned HOST_WIDE_INT type_size_in_bits;
11830
11831       type = field_type (decl);
11832       type_size_in_bits = simple_type_size_in_bits (type);
11833       type_align_in_bits = simple_type_align_in_bits (type);
11834
11835       field_size_tree = DECL_SIZE (decl);
11836
11837       /* The size could be unspecified if there was an error, or for
11838          a flexible array member.  */
11839       if (!field_size_tree)
11840         field_size_tree = bitsize_zero_node;
11841
11842       /* If the size of the field is not constant, use the type size.  */
11843       if (host_integerp (field_size_tree, 1))
11844         field_size_in_bits = tree_low_cst (field_size_tree, 1);
11845       else
11846         field_size_in_bits = type_size_in_bits;
11847
11848       decl_align_in_bits = simple_decl_align_in_bits (decl);
11849
11850       /* The GCC front-end doesn't make any attempt to keep track of the
11851          starting bit offset (relative to the start of the containing
11852          structure type) of the hypothetical "containing object" for a
11853          bit-field.  Thus, when computing the byte offset value for the
11854          start of the "containing object" of a bit-field, we must deduce
11855          this information on our own. This can be rather tricky to do in
11856          some cases.  For example, handling the following structure type
11857          definition when compiling for an i386/i486 target (which only
11858          aligns long long's to 32-bit boundaries) can be very tricky:
11859
11860          struct S { int field1; long long field2:31; };
11861
11862          Fortunately, there is a simple rule-of-thumb which can be used
11863          in such cases.  When compiling for an i386/i486, GCC will
11864          allocate 8 bytes for the structure shown above.  It decides to
11865          do this based upon one simple rule for bit-field allocation.
11866          GCC allocates each "containing object" for each bit-field at
11867          the first (i.e. lowest addressed) legitimate alignment boundary
11868          (based upon the required minimum alignment for the declared
11869          type of the field) which it can possibly use, subject to the
11870          condition that there is still enough available space remaining
11871          in the containing object (when allocated at the selected point)
11872          to fully accommodate all of the bits of the bit-field itself.
11873
11874          This simple rule makes it obvious why GCC allocates 8 bytes for
11875          each object of the structure type shown above.  When looking
11876          for a place to allocate the "containing object" for `field2',
11877          the compiler simply tries to allocate a 64-bit "containing
11878          object" at each successive 32-bit boundary (starting at zero)
11879          until it finds a place to allocate that 64- bit field such that
11880          at least 31 contiguous (and previously unallocated) bits remain
11881          within that selected 64 bit field.  (As it turns out, for the
11882          example above, the compiler finds it is OK to allocate the
11883          "containing object" 64-bit field at bit-offset zero within the
11884          structure type.)
11885
11886          Here we attempt to work backwards from the limited set of facts
11887          we're given, and we try to deduce from those facts, where GCC
11888          must have believed that the containing object started (within
11889          the structure type). The value we deduce is then used (by the
11890          callers of this routine) to generate DW_AT_location and
11891          DW_AT_bit_offset attributes for fields (both bit-fields and, in
11892          the case of DW_AT_location, regular fields as well).  */
11893
11894       /* Figure out the bit-distance from the start of the structure to
11895          the "deepest" bit of the bit-field.  */
11896       deepest_bitpos = bitpos_int + field_size_in_bits;
11897
11898       /* This is the tricky part.  Use some fancy footwork to deduce
11899          where the lowest addressed bit of the containing object must
11900          be.  */
11901       object_offset_in_bits = deepest_bitpos - type_size_in_bits;
11902
11903       /* Round up to type_align by default.  This works best for
11904          bitfields.  */
11905       object_offset_in_bits
11906         = round_up_to_align (object_offset_in_bits, type_align_in_bits);
11907
11908       if (object_offset_in_bits > bitpos_int)
11909         {
11910           object_offset_in_bits = deepest_bitpos - type_size_in_bits;
11911
11912           /* Round up to decl_align instead.  */
11913           object_offset_in_bits
11914             = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
11915         }
11916     }
11917   else
11918 #endif
11919     object_offset_in_bits = bitpos_int;
11920
11921   return object_offset_in_bits / BITS_PER_UNIT;
11922 }
11923 \f
11924 /* The following routines define various Dwarf attributes and any data
11925    associated with them.  */
11926
11927 /* Add a location description attribute value to a DIE.
11928
11929    This emits location attributes suitable for whole variables and
11930    whole parameters.  Note that the location attributes for struct fields are
11931    generated by the routine `data_member_location_attribute' below.  */
11932
11933 static inline void
11934 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
11935                              dw_loc_descr_ref descr)
11936 {
11937   if (descr != 0)
11938     add_AT_loc (die, attr_kind, descr);
11939 }
11940
11941 /* Attach the specialized form of location attribute used for data members of
11942    struct and union types.  In the special case of a FIELD_DECL node which
11943    represents a bit-field, the "offset" part of this special location
11944    descriptor must indicate the distance in bytes from the lowest-addressed
11945    byte of the containing struct or union type to the lowest-addressed byte of
11946    the "containing object" for the bit-field.  (See the `field_byte_offset'
11947    function above).
11948
11949    For any given bit-field, the "containing object" is a hypothetical object
11950    (of some integral or enum type) within which the given bit-field lives.  The
11951    type of this hypothetical "containing object" is always the same as the
11952    declared type of the individual bit-field itself (for GCC anyway... the
11953    DWARF spec doesn't actually mandate this).  Note that it is the size (in
11954    bytes) of the hypothetical "containing object" which will be given in the
11955    DW_AT_byte_size attribute for this bit-field.  (See the
11956    `byte_size_attribute' function below.)  It is also used when calculating the
11957    value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
11958    function below.)  */
11959
11960 static void
11961 add_data_member_location_attribute (dw_die_ref die, tree decl)
11962 {
11963   HOST_WIDE_INT offset;
11964   dw_loc_descr_ref loc_descr = 0;
11965
11966   if (TREE_CODE (decl) == TREE_BINFO)
11967     {
11968       /* We're working on the TAG_inheritance for a base class.  */
11969       if (BINFO_VIRTUAL_P (decl) && is_cxx ())
11970         {
11971           /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
11972              aren't at a fixed offset from all (sub)objects of the same
11973              type.  We need to extract the appropriate offset from our
11974              vtable.  The following dwarf expression means
11975
11976                BaseAddr = ObAddr + *((*ObAddr) - Offset)
11977
11978              This is specific to the V3 ABI, of course.  */
11979
11980           dw_loc_descr_ref tmp;
11981
11982           /* Make a copy of the object address.  */
11983           tmp = new_loc_descr (DW_OP_dup, 0, 0);
11984           add_loc_descr (&loc_descr, tmp);
11985
11986           /* Extract the vtable address.  */
11987           tmp = new_loc_descr (DW_OP_deref, 0, 0);
11988           add_loc_descr (&loc_descr, tmp);
11989
11990           /* Calculate the address of the offset.  */
11991           offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
11992           gcc_assert (offset < 0);
11993
11994           tmp = int_loc_descriptor (-offset);
11995           add_loc_descr (&loc_descr, tmp);
11996           tmp = new_loc_descr (DW_OP_minus, 0, 0);
11997           add_loc_descr (&loc_descr, tmp);
11998
11999           /* Extract the offset.  */
12000           tmp = new_loc_descr (DW_OP_deref, 0, 0);
12001           add_loc_descr (&loc_descr, tmp);
12002
12003           /* Add it to the object address.  */
12004           tmp = new_loc_descr (DW_OP_plus, 0, 0);
12005           add_loc_descr (&loc_descr, tmp);
12006         }
12007       else
12008         offset = tree_low_cst (BINFO_OFFSET (decl), 0);
12009     }
12010   else
12011     offset = field_byte_offset (decl);
12012
12013   if (! loc_descr)
12014     {
12015       if (dwarf_version > 2)
12016         {
12017           /* Don't need to output a location expression, just the constant. */
12018           add_AT_int (die, DW_AT_data_member_location, offset);
12019           return;
12020         }
12021       else
12022         {
12023           enum dwarf_location_atom op;
12024           
12025           /* The DWARF2 standard says that we should assume that the structure
12026              address is already on the stack, so we can specify a structure
12027              field address by using DW_OP_plus_uconst.  */
12028           
12029 #ifdef MIPS_DEBUGGING_INFO
12030           /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
12031              operator correctly.  It works only if we leave the offset on the
12032              stack.  */
12033           op = DW_OP_constu;
12034 #else
12035           op = DW_OP_plus_uconst;
12036 #endif
12037           
12038           loc_descr = new_loc_descr (op, offset, 0);
12039         }
12040     }
12041
12042   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
12043 }
12044
12045 /* Writes integer values to dw_vec_const array.  */
12046
12047 static void
12048 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
12049 {
12050   while (size != 0)
12051     {
12052       *dest++ = val & 0xff;
12053       val >>= 8;
12054       --size;
12055     }
12056 }
12057
12058 /* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
12059
12060 static HOST_WIDE_INT
12061 extract_int (const unsigned char *src, unsigned int size)
12062 {
12063   HOST_WIDE_INT val = 0;
12064
12065   src += size;
12066   while (size != 0)
12067     {
12068       val <<= 8;
12069       val |= *--src & 0xff;
12070       --size;
12071     }
12072   return val;
12073 }
12074
12075 /* Writes floating point values to dw_vec_const array.  */
12076
12077 static void
12078 insert_float (const_rtx rtl, unsigned char *array)
12079 {
12080   REAL_VALUE_TYPE rv;
12081   long val[4];
12082   int i;
12083
12084   REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
12085   real_to_target (val, &rv, GET_MODE (rtl));
12086
12087   /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
12088   for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
12089     {
12090       insert_int (val[i], 4, array);
12091       array += 4;
12092     }
12093 }
12094
12095 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
12096    does not have a "location" either in memory or in a register.  These
12097    things can arise in GNU C when a constant is passed as an actual parameter
12098    to an inlined function.  They can also arise in C++ where declared
12099    constants do not necessarily get memory "homes".  */
12100
12101 static void
12102 add_const_value_attribute (dw_die_ref die, rtx rtl)
12103 {
12104   switch (GET_CODE (rtl))
12105     {
12106     case CONST_INT:
12107       {
12108         HOST_WIDE_INT val = INTVAL (rtl);
12109
12110         if (val < 0)
12111           add_AT_int (die, DW_AT_const_value, val);
12112         else
12113           add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
12114       }
12115       break;
12116
12117     case CONST_DOUBLE:
12118       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
12119          floating-point constant.  A CONST_DOUBLE is used whenever the
12120          constant requires more than one word in order to be adequately
12121          represented.  We output CONST_DOUBLEs as blocks.  */
12122       {
12123         enum machine_mode mode = GET_MODE (rtl);
12124
12125         if (SCALAR_FLOAT_MODE_P (mode))
12126           {
12127             unsigned int length = GET_MODE_SIZE (mode);
12128             unsigned char *array = GGC_NEWVEC (unsigned char, length);
12129
12130             insert_float (rtl, array);
12131             add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
12132           }
12133         else
12134           {
12135             /* ??? We really should be using HOST_WIDE_INT throughout.  */
12136             gcc_assert (HOST_BITS_PER_LONG == HOST_BITS_PER_WIDE_INT);
12137
12138             add_AT_long_long (die, DW_AT_const_value,
12139                               CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
12140           }
12141       }
12142       break;
12143
12144     case CONST_VECTOR:
12145       {
12146         enum machine_mode mode = GET_MODE (rtl);
12147         unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
12148         unsigned int length = CONST_VECTOR_NUNITS (rtl);
12149         unsigned char *array = GGC_NEWVEC (unsigned char, length * elt_size);
12150         unsigned int i;
12151         unsigned char *p;
12152
12153         switch (GET_MODE_CLASS (mode))
12154           {
12155           case MODE_VECTOR_INT:
12156             for (i = 0, p = array; i < length; i++, p += elt_size)
12157               {
12158                 rtx elt = CONST_VECTOR_ELT (rtl, i);
12159                 HOST_WIDE_INT lo, hi;
12160
12161                 switch (GET_CODE (elt))
12162                   {
12163                   case CONST_INT:
12164                     lo = INTVAL (elt);
12165                     hi = -(lo < 0);
12166                     break;
12167
12168                   case CONST_DOUBLE:
12169                     lo = CONST_DOUBLE_LOW (elt);
12170                     hi = CONST_DOUBLE_HIGH (elt);
12171                     break;
12172
12173                   default:
12174                     gcc_unreachable ();
12175                   }
12176
12177                 if (elt_size <= sizeof (HOST_WIDE_INT))
12178                   insert_int (lo, elt_size, p);
12179                 else
12180                   {
12181                     unsigned char *p0 = p;
12182                     unsigned char *p1 = p + sizeof (HOST_WIDE_INT);
12183
12184                     gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
12185                     if (WORDS_BIG_ENDIAN)
12186                       {
12187                         p0 = p1;
12188                         p1 = p;
12189                       }
12190                     insert_int (lo, sizeof (HOST_WIDE_INT), p0);
12191                     insert_int (hi, sizeof (HOST_WIDE_INT), p1);
12192                   }
12193               }
12194             break;
12195
12196           case MODE_VECTOR_FLOAT:
12197             for (i = 0, p = array; i < length; i++, p += elt_size)
12198               {
12199                 rtx elt = CONST_VECTOR_ELT (rtl, i);
12200                 insert_float (elt, p);
12201               }
12202             break;
12203
12204           default:
12205             gcc_unreachable ();
12206           }
12207
12208         add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
12209       }
12210       break;
12211
12212     case CONST_STRING:
12213       add_AT_string (die, DW_AT_const_value, XSTR (rtl, 0));
12214       break;
12215
12216     case SYMBOL_REF:
12217     case LABEL_REF:
12218     case CONST:
12219       add_AT_addr (die, DW_AT_const_value, rtl);
12220       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
12221       break;
12222
12223     case PLUS:
12224       /* In cases where an inlined instance of an inline function is passed
12225          the address of an `auto' variable (which is local to the caller) we
12226          can get a situation where the DECL_RTL of the artificial local
12227          variable (for the inlining) which acts as a stand-in for the
12228          corresponding formal parameter (of the inline function) will look
12229          like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
12230          exactly a compile-time constant expression, but it isn't the address
12231          of the (artificial) local variable either.  Rather, it represents the
12232          *value* which the artificial local variable always has during its
12233          lifetime.  We currently have no way to represent such quasi-constant
12234          values in Dwarf, so for now we just punt and generate nothing.  */
12235       break;
12236
12237     default:
12238       /* No other kinds of rtx should be possible here.  */
12239       gcc_unreachable ();
12240     }
12241
12242 }
12243
12244 /* Determine whether the evaluation of EXPR references any variables
12245    or functions which aren't otherwise used (and therefore may not be
12246    output).  */
12247 static tree
12248 reference_to_unused (tree * tp, int * walk_subtrees,
12249                      void * data ATTRIBUTE_UNUSED)
12250 {
12251   if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
12252     *walk_subtrees = 0;
12253
12254   if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
12255       && ! TREE_ASM_WRITTEN (*tp))
12256     return *tp;
12257   /* ???  The C++ FE emits debug information for using decls, so
12258      putting gcc_unreachable here falls over.  See PR31899.  For now
12259      be conservative.  */
12260   else if (!cgraph_global_info_ready
12261            && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
12262     return *tp;
12263   else if (TREE_CODE (*tp) == VAR_DECL)
12264     {
12265       struct varpool_node *node = varpool_node (*tp);
12266       if (!node->needed)
12267         return *tp;
12268     }
12269   else if (TREE_CODE (*tp) == FUNCTION_DECL
12270            && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
12271     {
12272       /* The call graph machinery must have finished analyzing,
12273          optimizing and gimplifying the CU by now.
12274          So if *TP has no call graph node associated
12275          to it, it means *TP will not be emitted.  */
12276       if (!cgraph_get_node (*tp))
12277         return *tp;
12278     }
12279   else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
12280     return *tp;
12281
12282   return NULL_TREE;
12283 }
12284
12285 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
12286    for use in a later add_const_value_attribute call.  */
12287
12288 static rtx
12289 rtl_for_decl_init (tree init, tree type)
12290 {
12291   rtx rtl = NULL_RTX;
12292
12293   /* If a variable is initialized with a string constant without embedded
12294      zeros, build CONST_STRING.  */
12295   if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
12296     {
12297       tree enttype = TREE_TYPE (type);
12298       tree domain = TYPE_DOMAIN (type);
12299       enum machine_mode mode = TYPE_MODE (enttype);
12300
12301       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
12302           && domain
12303           && integer_zerop (TYPE_MIN_VALUE (domain))
12304           && compare_tree_int (TYPE_MAX_VALUE (domain),
12305                                TREE_STRING_LENGTH (init) - 1) == 0
12306           && ((size_t) TREE_STRING_LENGTH (init)
12307               == strlen (TREE_STRING_POINTER (init)) + 1))
12308         rtl = gen_rtx_CONST_STRING (VOIDmode,
12309                                     ggc_strdup (TREE_STRING_POINTER (init)));
12310     }
12311   /* Other aggregates, and complex values, could be represented using
12312      CONCAT: FIXME!  */
12313   else if (AGGREGATE_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
12314     ;
12315   /* Vectors only work if their mode is supported by the target.
12316      FIXME: generic vectors ought to work too.  */
12317   else if (TREE_CODE (type) == VECTOR_TYPE && TYPE_MODE (type) == BLKmode)
12318     ;
12319   /* If the initializer is something that we know will expand into an
12320      immediate RTL constant, expand it now.  We must be careful not to
12321      reference variables which won't be output.  */
12322   else if (initializer_constant_valid_p (init, type)
12323            && ! walk_tree (&init, reference_to_unused, NULL, NULL))
12324     {
12325       /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
12326          possible.  */
12327       if (TREE_CODE (type) == VECTOR_TYPE)
12328         switch (TREE_CODE (init))
12329           {
12330           case VECTOR_CST:
12331             break;
12332           case CONSTRUCTOR:
12333             if (TREE_CONSTANT (init))
12334               {
12335                 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
12336                 bool constant_p = true;
12337                 tree value;
12338                 unsigned HOST_WIDE_INT ix;
12339
12340                 /* Even when ctor is constant, it might contain non-*_CST
12341                    elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
12342                    belong into VECTOR_CST nodes.  */
12343                 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
12344                   if (!CONSTANT_CLASS_P (value))
12345                     {
12346                       constant_p = false;
12347                       break;
12348                     }
12349
12350                 if (constant_p)
12351                   {
12352                     init = build_vector_from_ctor (type, elts);
12353                     break;
12354                   }
12355               }
12356             /* FALLTHRU */
12357
12358           default:
12359             return NULL;
12360           }
12361
12362       rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
12363
12364       /* If expand_expr returns a MEM, it wasn't immediate.  */
12365       gcc_assert (!rtl || !MEM_P (rtl));
12366     }
12367
12368   return rtl;
12369 }
12370
12371 /* Generate RTL for the variable DECL to represent its location.  */
12372
12373 static rtx
12374 rtl_for_decl_location (tree decl)
12375 {
12376   rtx rtl;
12377
12378   /* Here we have to decide where we are going to say the parameter "lives"
12379      (as far as the debugger is concerned).  We only have a couple of
12380      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
12381
12382      DECL_RTL normally indicates where the parameter lives during most of the
12383      activation of the function.  If optimization is enabled however, this
12384      could be either NULL or else a pseudo-reg.  Both of those cases indicate
12385      that the parameter doesn't really live anywhere (as far as the code
12386      generation parts of GCC are concerned) during most of the function's
12387      activation.  That will happen (for example) if the parameter is never
12388      referenced within the function.
12389
12390      We could just generate a location descriptor here for all non-NULL
12391      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
12392      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
12393      where DECL_RTL is NULL or is a pseudo-reg.
12394
12395      Note however that we can only get away with using DECL_INCOMING_RTL as
12396      a backup substitute for DECL_RTL in certain limited cases.  In cases
12397      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
12398      we can be sure that the parameter was passed using the same type as it is
12399      declared to have within the function, and that its DECL_INCOMING_RTL
12400      points us to a place where a value of that type is passed.
12401
12402      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
12403      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
12404      because in these cases DECL_INCOMING_RTL points us to a value of some
12405      type which is *different* from the type of the parameter itself.  Thus,
12406      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
12407      such cases, the debugger would end up (for example) trying to fetch a
12408      `float' from a place which actually contains the first part of a
12409      `double'.  That would lead to really incorrect and confusing
12410      output at debug-time.
12411
12412      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
12413      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
12414      are a couple of exceptions however.  On little-endian machines we can
12415      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
12416      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
12417      an integral type that is smaller than TREE_TYPE (decl). These cases arise
12418      when (on a little-endian machine) a non-prototyped function has a
12419      parameter declared to be of type `short' or `char'.  In such cases,
12420      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
12421      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
12422      passed `int' value.  If the debugger then uses that address to fetch
12423      a `short' or a `char' (on a little-endian machine) the result will be
12424      the correct data, so we allow for such exceptional cases below.
12425
12426      Note that our goal here is to describe the place where the given formal
12427      parameter lives during most of the function's activation (i.e. between the
12428      end of the prologue and the start of the epilogue).  We'll do that as best
12429      as we can. Note however that if the given formal parameter is modified
12430      sometime during the execution of the function, then a stack backtrace (at
12431      debug-time) will show the function as having been called with the *new*
12432      value rather than the value which was originally passed in.  This happens
12433      rarely enough that it is not a major problem, but it *is* a problem, and
12434      I'd like to fix it.
12435
12436      A future version of dwarf2out.c may generate two additional attributes for
12437      any given DW_TAG_formal_parameter DIE which will describe the "passed
12438      type" and the "passed location" for the given formal parameter in addition
12439      to the attributes we now generate to indicate the "declared type" and the
12440      "active location" for each parameter.  This additional set of attributes
12441      could be used by debuggers for stack backtraces. Separately, note that
12442      sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
12443      This happens (for example) for inlined-instances of inline function formal
12444      parameters which are never referenced.  This really shouldn't be
12445      happening.  All PARM_DECL nodes should get valid non-NULL
12446      DECL_INCOMING_RTL values.  FIXME.  */
12447
12448   /* Use DECL_RTL as the "location" unless we find something better.  */
12449   rtl = DECL_RTL_IF_SET (decl);
12450
12451   /* When generating abstract instances, ignore everything except
12452      constants, symbols living in memory, and symbols living in
12453      fixed registers.  */
12454   if (! reload_completed)
12455     {
12456       if (rtl
12457           && (CONSTANT_P (rtl)
12458               || (MEM_P (rtl)
12459                   && CONSTANT_P (XEXP (rtl, 0)))
12460               || (REG_P (rtl)
12461                   && TREE_CODE (decl) == VAR_DECL
12462                   && TREE_STATIC (decl))))
12463         {
12464           rtl = targetm.delegitimize_address (rtl);
12465           return rtl;
12466         }
12467       rtl = NULL_RTX;
12468     }
12469   else if (TREE_CODE (decl) == PARM_DECL)
12470     {
12471       if (rtl == NULL_RTX || is_pseudo_reg (rtl))
12472         {
12473           tree declared_type = TREE_TYPE (decl);
12474           tree passed_type = DECL_ARG_TYPE (decl);
12475           enum machine_mode dmode = TYPE_MODE (declared_type);
12476           enum machine_mode pmode = TYPE_MODE (passed_type);
12477
12478           /* This decl represents a formal parameter which was optimized out.
12479              Note that DECL_INCOMING_RTL may be NULL in here, but we handle
12480              all cases where (rtl == NULL_RTX) just below.  */
12481           if (dmode == pmode)
12482             rtl = DECL_INCOMING_RTL (decl);
12483           else if (SCALAR_INT_MODE_P (dmode)
12484                    && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
12485                    && DECL_INCOMING_RTL (decl))
12486             {
12487               rtx inc = DECL_INCOMING_RTL (decl);
12488               if (REG_P (inc))
12489                 rtl = inc;
12490               else if (MEM_P (inc))
12491                 {
12492                   if (BYTES_BIG_ENDIAN)
12493                     rtl = adjust_address_nv (inc, dmode,
12494                                              GET_MODE_SIZE (pmode)
12495                                              - GET_MODE_SIZE (dmode));
12496                   else
12497                     rtl = inc;
12498                 }
12499             }
12500         }
12501
12502       /* If the parm was passed in registers, but lives on the stack, then
12503          make a big endian correction if the mode of the type of the
12504          parameter is not the same as the mode of the rtl.  */
12505       /* ??? This is the same series of checks that are made in dbxout.c before
12506          we reach the big endian correction code there.  It isn't clear if all
12507          of these checks are necessary here, but keeping them all is the safe
12508          thing to do.  */
12509       else if (MEM_P (rtl)
12510                && XEXP (rtl, 0) != const0_rtx
12511                && ! CONSTANT_P (XEXP (rtl, 0))
12512                /* Not passed in memory.  */
12513                && !MEM_P (DECL_INCOMING_RTL (decl))
12514                /* Not passed by invisible reference.  */
12515                && (!REG_P (XEXP (rtl, 0))
12516                    || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
12517                    || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
12518 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
12519                    || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
12520 #endif
12521                      )
12522                /* Big endian correction check.  */
12523                && BYTES_BIG_ENDIAN
12524                && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
12525                && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
12526                    < UNITS_PER_WORD))
12527         {
12528           int offset = (UNITS_PER_WORD
12529                         - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
12530
12531           rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
12532                              plus_constant (XEXP (rtl, 0), offset));
12533         }
12534     }
12535   else if (TREE_CODE (decl) == VAR_DECL
12536            && rtl
12537            && MEM_P (rtl)
12538            && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
12539            && BYTES_BIG_ENDIAN)
12540     {
12541       int rsize = GET_MODE_SIZE (GET_MODE (rtl));
12542       int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
12543
12544       /* If a variable is declared "register" yet is smaller than
12545          a register, then if we store the variable to memory, it
12546          looks like we're storing a register-sized value, when in
12547          fact we are not.  We need to adjust the offset of the
12548          storage location to reflect the actual value's bytes,
12549          else gdb will not be able to display it.  */
12550       if (rsize > dsize)
12551         rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
12552                            plus_constant (XEXP (rtl, 0), rsize-dsize));
12553     }
12554
12555   /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
12556      and will have been substituted directly into all expressions that use it.
12557      C does not have such a concept, but C++ and other languages do.  */
12558   if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
12559     rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
12560
12561   if (rtl)
12562     rtl = targetm.delegitimize_address (rtl);
12563
12564   /* If we don't look past the constant pool, we risk emitting a
12565      reference to a constant pool entry that isn't referenced from
12566      code, and thus is not emitted.  */
12567   if (rtl)
12568     rtl = avoid_constant_pool_reference (rtl);
12569
12570   return rtl;
12571 }
12572
12573 /* We need to figure out what section we should use as the base for the
12574    address ranges where a given location is valid.
12575    1. If this particular DECL has a section associated with it, use that.
12576    2. If this function has a section associated with it, use that.
12577    3. Otherwise, use the text section.
12578    XXX: If you split a variable across multiple sections, we won't notice.  */
12579
12580 static const char *
12581 secname_for_decl (const_tree decl)
12582 {
12583   const char *secname;
12584
12585   if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
12586     {
12587       tree sectree = DECL_SECTION_NAME (decl);
12588       secname = TREE_STRING_POINTER (sectree);
12589     }
12590   else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
12591     {
12592       tree sectree = DECL_SECTION_NAME (current_function_decl);
12593       secname = TREE_STRING_POINTER (sectree);
12594     }
12595   else if (cfun && in_cold_section_p)
12596     secname = crtl->subsections.cold_section_label;
12597   else
12598     secname = text_section_label;
12599
12600   return secname;
12601 }
12602
12603 /* Check whether decl is a Fortran COMMON symbol.  If not, NULL_TREE is
12604    returned.  If so, the decl for the COMMON block is returned, and the
12605    value is the offset into the common block for the symbol.  */
12606
12607 static tree
12608 fortran_common (tree decl, HOST_WIDE_INT *value)
12609 {
12610   tree val_expr, cvar;
12611   enum machine_mode mode;
12612   HOST_WIDE_INT bitsize, bitpos;
12613   tree offset;
12614   int volatilep = 0, unsignedp = 0;
12615
12616   /* If the decl isn't a VAR_DECL, or if it isn't public or static, or if
12617      it does not have a value (the offset into the common area), or if it
12618      is thread local (as opposed to global) then it isn't common, and shouldn't
12619      be handled as such.  */
12620   if (TREE_CODE (decl) != VAR_DECL
12621       || !TREE_PUBLIC (decl)
12622       || !TREE_STATIC (decl)
12623       || !DECL_HAS_VALUE_EXPR_P (decl)
12624       || !is_fortran ())
12625     return NULL_TREE;
12626
12627   val_expr = DECL_VALUE_EXPR (decl);
12628   if (TREE_CODE (val_expr) != COMPONENT_REF)
12629     return NULL_TREE;
12630
12631   cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
12632                               &mode, &unsignedp, &volatilep, true);
12633
12634   if (cvar == NULL_TREE
12635       || TREE_CODE (cvar) != VAR_DECL
12636       || DECL_ARTIFICIAL (cvar)
12637       || !TREE_PUBLIC (cvar))
12638     return NULL_TREE;
12639
12640   *value = 0;
12641   if (offset != NULL)
12642     {
12643       if (!host_integerp (offset, 0))
12644         return NULL_TREE;
12645       *value = tree_low_cst (offset, 0);
12646     }
12647   if (bitpos != 0)
12648     *value += bitpos / BITS_PER_UNIT;
12649
12650   return cvar;
12651 }
12652
12653 /* Dereference a location expression LOC if DECL is passed by invisible
12654    reference.  */
12655
12656 static dw_loc_descr_ref
12657 loc_by_reference (dw_loc_descr_ref loc, tree decl)
12658 {
12659   HOST_WIDE_INT size;
12660   enum dwarf_location_atom op;
12661
12662   if (loc == NULL)
12663     return NULL;
12664
12665   if ((TREE_CODE (decl) != PARM_DECL
12666        && TREE_CODE (decl) != RESULT_DECL
12667        && TREE_CODE (decl) != VAR_DECL)
12668       || !DECL_BY_REFERENCE (decl))
12669     return loc;
12670
12671   /* If loc is DW_OP_reg{0...31,x}, don't add DW_OP_deref, instead
12672      change it into corresponding DW_OP_breg{0...31,x} 0.  Then the
12673      location expression is considered to be address of a memory location,
12674      rather than the register itself.  */
12675   if (((loc->dw_loc_opc >= DW_OP_reg0 && loc->dw_loc_opc <= DW_OP_reg31)
12676        || loc->dw_loc_opc == DW_OP_regx)
12677       && (loc->dw_loc_next == NULL
12678           || (loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_uninit
12679               && loc->dw_loc_next->dw_loc_next == NULL)))
12680     {
12681       if (loc->dw_loc_opc == DW_OP_regx)
12682         {
12683           loc->dw_loc_opc = DW_OP_bregx;
12684           loc->dw_loc_oprnd2.v.val_int = 0;
12685         }
12686       else
12687         {
12688           loc->dw_loc_opc
12689             = (enum dwarf_location_atom)
12690               (loc->dw_loc_opc + (DW_OP_breg0 - DW_OP_reg0));
12691           loc->dw_loc_oprnd1.v.val_int = 0;
12692         }
12693       return loc;
12694     }
12695
12696   size = int_size_in_bytes (TREE_TYPE (decl));
12697   if (size > DWARF2_ADDR_SIZE || size == -1)
12698     return 0;
12699   else if (size == DWARF2_ADDR_SIZE)
12700     op = DW_OP_deref;
12701   else
12702     op = DW_OP_deref_size;
12703   add_loc_descr (&loc, new_loc_descr (op, size, 0));
12704   return loc;
12705 }
12706
12707 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
12708    data attribute for a variable or a parameter.  We generate the
12709    DW_AT_const_value attribute only in those cases where the given variable
12710    or parameter does not have a true "location" either in memory or in a
12711    register.  This can happen (for example) when a constant is passed as an
12712    actual argument in a call to an inline function.  (It's possible that
12713    these things can crop up in other ways also.)  Note that one type of
12714    constant value which can be passed into an inlined function is a constant
12715    pointer.  This can happen for example if an actual argument in an inlined
12716    function call evaluates to a compile-time constant address.  */
12717
12718 static void
12719 add_location_or_const_value_attribute (dw_die_ref die, tree decl,
12720                                        enum dwarf_attribute attr)
12721 {
12722   rtx rtl;
12723   dw_loc_descr_ref descr;
12724   var_loc_list *loc_list;
12725   struct var_loc_node *node;
12726   if (TREE_CODE (decl) == ERROR_MARK)
12727     return;
12728
12729   gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
12730               || TREE_CODE (decl) == RESULT_DECL);
12731
12732   /* See if we possibly have multiple locations for this variable.  */
12733   loc_list = lookup_decl_loc (decl);
12734
12735   /* If it truly has multiple locations, the first and last node will
12736      differ.  */
12737   if (loc_list && loc_list->first != loc_list->last)
12738     {
12739       const char *endname, *secname;
12740       dw_loc_list_ref list;
12741       rtx varloc;
12742       enum var_init_status initialized;
12743
12744       /* Now that we know what section we are using for a base,
12745          actually construct the list of locations.
12746          The first location information is what is passed to the
12747          function that creates the location list, and the remaining
12748          locations just get added on to that list.
12749          Note that we only know the start address for a location
12750          (IE location changes), so to build the range, we use
12751          the range [current location start, next location start].
12752          This means we have to special case the last node, and generate
12753          a range of [last location start, end of function label].  */
12754
12755       node = loc_list->first;
12756       varloc = NOTE_VAR_LOCATION (node->var_loc_note);
12757       secname = secname_for_decl (decl);
12758
12759       if (NOTE_VAR_LOCATION_LOC (node->var_loc_note))
12760         initialized = NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
12761       else
12762         initialized = VAR_INIT_STATUS_INITIALIZED;
12763
12764       descr = loc_by_reference (loc_descriptor (varloc, initialized), decl);
12765       list = new_loc_list (descr, node->label, node->next->label, secname, 1);
12766       node = node->next;
12767
12768       for (; node->next; node = node->next)
12769         if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
12770           {
12771             /* The variable has a location between NODE->LABEL and
12772                NODE->NEXT->LABEL.  */
12773             enum var_init_status initialized =
12774               NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
12775             varloc = NOTE_VAR_LOCATION (node->var_loc_note);
12776             descr = loc_by_reference (loc_descriptor (varloc, initialized),
12777                                       decl);
12778             add_loc_descr_to_loc_list (&list, descr,
12779                                        node->label, node->next->label, secname);
12780           }
12781
12782       /* If the variable has a location at the last label
12783          it keeps its location until the end of function.  */
12784       if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
12785         {
12786           char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
12787           enum var_init_status initialized =
12788             NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
12789
12790           varloc = NOTE_VAR_LOCATION (node->var_loc_note);
12791           if (!current_function_decl)
12792             endname = text_end_label;
12793           else
12794             {
12795               ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
12796                                            current_function_funcdef_no);
12797               endname = ggc_strdup (label_id);
12798             }
12799           descr = loc_by_reference (loc_descriptor (varloc, initialized),
12800                                     decl);
12801           add_loc_descr_to_loc_list (&list, descr,
12802                                      node->label, endname, secname);
12803         }
12804
12805       /* Finally, add the location list to the DIE, and we are done.  */
12806       add_AT_loc_list (die, attr, list);
12807       return;
12808     }
12809
12810   /* Try to get some constant RTL for this decl, and use that as the value of
12811      the location.  */
12812
12813   rtl = rtl_for_decl_location (decl);
12814   if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING))
12815     {
12816       add_const_value_attribute (die, rtl);
12817       return;
12818     }
12819
12820   /* If we have tried to generate the location otherwise, and it
12821      didn't work out (we wouldn't be here if we did), and we have a one entry
12822      location list, try generating a location from that.  */
12823   if (loc_list && loc_list->first)
12824     {
12825       enum var_init_status status;
12826       node = loc_list->first;
12827       status = NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
12828       descr = loc_descriptor (NOTE_VAR_LOCATION (node->var_loc_note), status);
12829       if (descr)
12830         {
12831           descr = loc_by_reference (descr, decl);
12832           add_AT_location_description (die, attr, descr);
12833           return;
12834         }
12835     }
12836
12837   /* We couldn't get any rtl, so try directly generating the location
12838      description from the tree.  */
12839   descr = loc_descriptor_from_tree (decl);
12840   if (descr)
12841     {
12842       descr = loc_by_reference (descr, decl);
12843       add_AT_location_description (die, attr, descr);
12844       return;
12845     }
12846   /* None of that worked, so it must not really have a location;
12847      try adding a constant value attribute from the DECL_INITIAL.  */
12848   tree_add_const_value_attribute_for_decl (die, decl);
12849 }
12850
12851 /* Add VARIABLE and DIE into deferred locations list.  */
12852
12853 static void
12854 defer_location (tree variable, dw_die_ref die)
12855 {
12856   deferred_locations entry;
12857   entry.variable = variable;
12858   entry.die = die;
12859   VEC_safe_push (deferred_locations, gc, deferred_locations_list, &entry);
12860 }
12861
12862 /* Helper function for tree_add_const_value_attribute.  Natively encode
12863    initializer INIT into an array.  Return true if successful.  */
12864
12865 static bool
12866 native_encode_initializer (tree init, unsigned char *array, int size)
12867 {
12868   tree type;
12869
12870   if (init == NULL_TREE)
12871     return false;
12872
12873   STRIP_NOPS (init);
12874   switch (TREE_CODE (init))
12875     {
12876     case STRING_CST:
12877       type = TREE_TYPE (init);
12878       if (TREE_CODE (type) == ARRAY_TYPE)
12879         {
12880           tree enttype = TREE_TYPE (type);
12881           enum machine_mode mode = TYPE_MODE (enttype);
12882
12883           if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
12884             return false;
12885           if (int_size_in_bytes (type) != size)
12886             return false;
12887           if (size > TREE_STRING_LENGTH (init))
12888             {
12889               memcpy (array, TREE_STRING_POINTER (init),
12890                       TREE_STRING_LENGTH (init));
12891               memset (array + TREE_STRING_LENGTH (init),
12892                       '\0', size - TREE_STRING_LENGTH (init));
12893             }
12894           else
12895             memcpy (array, TREE_STRING_POINTER (init), size);
12896           return true;
12897         }
12898       return false;
12899     case CONSTRUCTOR:
12900       type = TREE_TYPE (init);
12901       if (int_size_in_bytes (type) != size)
12902         return false;
12903       if (TREE_CODE (type) == ARRAY_TYPE)
12904         {
12905           HOST_WIDE_INT min_index;
12906           unsigned HOST_WIDE_INT cnt;
12907           int curpos = 0, fieldsize;
12908           constructor_elt *ce;
12909
12910           if (TYPE_DOMAIN (type) == NULL_TREE
12911               || !host_integerp (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0))
12912             return false;
12913
12914           fieldsize = int_size_in_bytes (TREE_TYPE (type));
12915           if (fieldsize <= 0)
12916             return false;
12917
12918           min_index = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0);
12919           memset (array, '\0', size);
12920           for (cnt = 0;
12921                VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce);
12922                cnt++)
12923             {
12924               tree val = ce->value;
12925               tree index = ce->index;
12926               int pos = curpos;
12927               if (index && TREE_CODE (index) == RANGE_EXPR)
12928                 pos = (tree_low_cst (TREE_OPERAND (index, 0), 0) - min_index)
12929                       * fieldsize;
12930               else if (index)
12931                 pos = (tree_low_cst (index, 0) - min_index) * fieldsize;
12932
12933               if (val)
12934                 {
12935                   STRIP_NOPS (val);
12936                   if (!native_encode_initializer (val, array + pos, fieldsize))
12937                     return false;
12938                 }
12939               curpos = pos + fieldsize;
12940               if (index && TREE_CODE (index) == RANGE_EXPR)
12941                 {
12942                   int count = tree_low_cst (TREE_OPERAND (index, 1), 0)
12943                               - tree_low_cst (TREE_OPERAND (index, 0), 0);
12944                   while (count > 0)
12945                     {
12946                       if (val)
12947                         memcpy (array + curpos, array + pos, fieldsize);
12948                       curpos += fieldsize;
12949                     }
12950                 }
12951               gcc_assert (curpos <= size);
12952             }
12953           return true;
12954         }
12955       else if (TREE_CODE (type) == RECORD_TYPE
12956                || TREE_CODE (type) == UNION_TYPE)
12957         {
12958           tree field = NULL_TREE;
12959           unsigned HOST_WIDE_INT cnt;
12960           constructor_elt *ce;
12961
12962           if (int_size_in_bytes (type) != size)
12963             return false;
12964
12965           if (TREE_CODE (type) == RECORD_TYPE)
12966             field = TYPE_FIELDS (type);
12967
12968           for (cnt = 0;
12969                VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce);
12970                cnt++, field = field ? TREE_CHAIN (field) : 0)
12971             {
12972               tree val = ce->value;
12973               int pos, fieldsize;
12974
12975               if (ce->index != 0)
12976                 field = ce->index;
12977
12978               if (val)
12979                 STRIP_NOPS (val);
12980
12981               if (field == NULL_TREE || DECL_BIT_FIELD (field))
12982                 return false;
12983
12984               if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
12985                   && TYPE_DOMAIN (TREE_TYPE (field))
12986                   && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
12987                 return false;
12988               else if (DECL_SIZE_UNIT (field) == NULL_TREE
12989                        || !host_integerp (DECL_SIZE_UNIT (field), 0))
12990                 return false;
12991               fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 0);
12992               pos = int_byte_position (field);
12993               gcc_assert (pos + fieldsize <= size);
12994               if (val
12995                   && !native_encode_initializer (val, array + pos, fieldsize))
12996                 return false;
12997             }
12998           return true;
12999         }
13000       return false;
13001     case VIEW_CONVERT_EXPR:
13002     case NON_LVALUE_EXPR:
13003       return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
13004     default:
13005       return native_encode_expr (init, array, size) == size;
13006     }
13007 }
13008
13009 /* Attach a DW_AT_const_value attribute to DIE. The value of the
13010    attribute is the const value T.  */
13011
13012 static void
13013 tree_add_const_value_attribute (dw_die_ref die, tree t)
13014 {
13015   tree init;
13016   tree type = TREE_TYPE (t);
13017   rtx rtl;
13018
13019   if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
13020     return;
13021
13022   init = t;
13023   gcc_assert (!DECL_P (init));
13024
13025   rtl = rtl_for_decl_init (init, type);
13026   if (rtl)
13027     add_const_value_attribute (die, rtl);
13028   /* If the host and target are sane, try harder.  */
13029   else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
13030            && initializer_constant_valid_p (init, type))
13031     {
13032       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
13033       if (size > 0 && (int) size == size)
13034         {
13035           unsigned char *array = GGC_CNEWVEC (unsigned char, size);
13036
13037           if (native_encode_initializer (init, array, size))
13038             add_AT_vec (die, DW_AT_const_value, size, 1, array);
13039         }
13040     }
13041 }
13042
13043 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
13044    attribute is the const value of T, where T is an integral constant
13045    variable with static storage duration
13046    (so it can't be a PARM_DECL or a RESULT_DECL).  */
13047
13048 static void
13049 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
13050 {
13051
13052   if (!decl
13053       || (TREE_CODE (decl) != VAR_DECL
13054           && TREE_CODE (decl) != CONST_DECL))
13055     return;
13056
13057     if (TREE_READONLY (decl)
13058         && ! TREE_THIS_VOLATILE (decl)
13059         && DECL_INITIAL (decl))
13060       /* OK */;
13061     else
13062       return;
13063
13064     tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
13065 }
13066
13067 /* Convert the CFI instructions for the current function into a
13068    location list.  This is used for DW_AT_frame_base when we targeting
13069    a dwarf2 consumer that does not support the dwarf3
13070    DW_OP_call_frame_cfa.  OFFSET is a constant to be added to all CFA
13071    expressions.  */
13072
13073 static dw_loc_list_ref
13074 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
13075 {
13076   dw_fde_ref fde;
13077   dw_loc_list_ref list, *list_tail;
13078   dw_cfi_ref cfi;
13079   dw_cfa_location last_cfa, next_cfa;
13080   const char *start_label, *last_label, *section;
13081   dw_cfa_location remember;
13082
13083   fde = current_fde ();
13084   gcc_assert (fde != NULL);
13085
13086   section = secname_for_decl (current_function_decl);
13087   list_tail = &list;
13088   list = NULL;
13089
13090   memset (&next_cfa, 0, sizeof (next_cfa));
13091   next_cfa.reg = INVALID_REGNUM;
13092   remember = next_cfa;
13093
13094   start_label = fde->dw_fde_begin;
13095
13096   /* ??? Bald assumption that the CIE opcode list does not contain
13097      advance opcodes.  */
13098   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
13099     lookup_cfa_1 (cfi, &next_cfa, &remember);
13100
13101   last_cfa = next_cfa;
13102   last_label = start_label;
13103
13104   for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
13105     switch (cfi->dw_cfi_opc)
13106       {
13107       case DW_CFA_set_loc:
13108       case DW_CFA_advance_loc1:
13109       case DW_CFA_advance_loc2:
13110       case DW_CFA_advance_loc4:
13111         if (!cfa_equal_p (&last_cfa, &next_cfa))
13112           {
13113             *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
13114                                        start_label, last_label, section,
13115                                        list == NULL);
13116
13117             list_tail = &(*list_tail)->dw_loc_next;
13118             last_cfa = next_cfa;
13119             start_label = last_label;
13120           }
13121         last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
13122         break;
13123
13124       case DW_CFA_advance_loc:
13125         /* The encoding is complex enough that we should never emit this.  */
13126         gcc_unreachable ();
13127
13128       default:
13129         lookup_cfa_1 (cfi, &next_cfa, &remember);
13130         break;
13131       }
13132
13133   if (!cfa_equal_p (&last_cfa, &next_cfa))
13134     {
13135       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
13136                                  start_label, last_label, section,
13137                                  list == NULL);
13138       list_tail = &(*list_tail)->dw_loc_next;
13139       start_label = last_label;
13140     }
13141   *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
13142                              start_label, fde->dw_fde_end, section,
13143                              list == NULL);
13144
13145   return list;
13146 }
13147
13148 /* Compute a displacement from the "steady-state frame pointer" to the
13149    frame base (often the same as the CFA), and store it in
13150    frame_pointer_fb_offset.  OFFSET is added to the displacement
13151    before the latter is negated.  */
13152
13153 static void
13154 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
13155 {
13156   rtx reg, elim;
13157
13158 #ifdef FRAME_POINTER_CFA_OFFSET
13159   reg = frame_pointer_rtx;
13160   offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
13161 #else
13162   reg = arg_pointer_rtx;
13163   offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
13164 #endif
13165
13166   elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
13167   if (GET_CODE (elim) == PLUS)
13168     {
13169       offset += INTVAL (XEXP (elim, 1));
13170       elim = XEXP (elim, 0);
13171     }
13172
13173   gcc_assert ((SUPPORTS_STACK_ALIGNMENT
13174                && (elim == hard_frame_pointer_rtx
13175                    || elim == stack_pointer_rtx))
13176               || elim == (frame_pointer_needed
13177                           ? hard_frame_pointer_rtx
13178                           : stack_pointer_rtx));
13179
13180   frame_pointer_fb_offset = -offset;
13181 }
13182
13183 /* Generate a DW_AT_name attribute given some string value to be included as
13184    the value of the attribute.  */
13185
13186 static void
13187 add_name_attribute (dw_die_ref die, const char *name_string)
13188 {
13189   if (name_string != NULL && *name_string != 0)
13190     {
13191       if (demangle_name_func)
13192         name_string = (*demangle_name_func) (name_string);
13193
13194       add_AT_string (die, DW_AT_name, name_string);
13195     }
13196 }
13197
13198 /* Generate a DW_AT_comp_dir attribute for DIE.  */
13199
13200 static void
13201 add_comp_dir_attribute (dw_die_ref die)
13202 {
13203   const char *wd = get_src_pwd ();
13204   char *wd1;
13205
13206   if (wd == NULL)
13207     return;
13208
13209   if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
13210     {
13211       int wdlen;
13212
13213       wdlen = strlen (wd);
13214       wd1 = GGC_NEWVEC (char, wdlen + 2);
13215       strcpy (wd1, wd);
13216       wd1 [wdlen] = DIR_SEPARATOR;
13217       wd1 [wdlen + 1] = 0;
13218       wd = wd1;
13219     }
13220
13221     add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
13222 }
13223
13224 /* Given a tree node describing an array bound (either lower or upper) output
13225    a representation for that bound.  */
13226
13227 static void
13228 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
13229 {
13230   switch (TREE_CODE (bound))
13231     {
13232     case ERROR_MARK:
13233       return;
13234
13235     /* All fixed-bounds are represented by INTEGER_CST nodes.  */
13236     case INTEGER_CST:
13237       if (! host_integerp (bound, 0)
13238           || (bound_attr == DW_AT_lower_bound
13239               && (((is_c_family () || is_java ()) &&  integer_zerop (bound))
13240                   || (is_fortran () && integer_onep (bound)))))
13241         /* Use the default.  */
13242         ;
13243       else
13244         add_AT_unsigned (subrange_die, bound_attr, tree_low_cst (bound, 0));
13245       break;
13246
13247     CASE_CONVERT:
13248     case VIEW_CONVERT_EXPR:
13249       add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
13250       break;
13251
13252     case SAVE_EXPR:
13253       break;
13254
13255     case VAR_DECL:
13256     case PARM_DECL:
13257     case RESULT_DECL:
13258       {
13259         dw_die_ref decl_die = lookup_decl_die (bound);
13260         dw_loc_descr_ref loc;
13261
13262         /* ??? Can this happen, or should the variable have been bound
13263            first?  Probably it can, since I imagine that we try to create
13264            the types of parameters in the order in which they exist in
13265            the list, and won't have created a forward reference to a
13266            later parameter.  */
13267         if (decl_die != NULL)
13268           add_AT_die_ref (subrange_die, bound_attr, decl_die);
13269         else
13270           {
13271             loc = loc_descriptor_from_tree_1 (bound, 0);
13272             add_AT_location_description (subrange_die, bound_attr, loc);
13273           }
13274         break;
13275       }
13276
13277     default:
13278       {
13279         /* Otherwise try to create a stack operation procedure to
13280            evaluate the value of the array bound.  */
13281
13282         dw_die_ref ctx, decl_die;
13283         dw_loc_descr_ref loc;
13284
13285         loc = loc_descriptor_from_tree (bound);
13286         if (loc == NULL)
13287           break;
13288
13289         if (current_function_decl == 0)
13290           ctx = comp_unit_die;
13291         else
13292           ctx = lookup_decl_die (current_function_decl);
13293
13294         decl_die = new_die (DW_TAG_variable, ctx, bound);
13295         add_AT_flag (decl_die, DW_AT_artificial, 1);
13296         add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
13297         add_AT_loc (decl_die, DW_AT_location, loc);
13298
13299         add_AT_die_ref (subrange_die, bound_attr, decl_die);
13300         break;
13301       }
13302     }
13303 }
13304
13305 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
13306    possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
13307    Note that the block of subscript information for an array type also
13308    includes information about the element type of the given array type.  */
13309
13310 static void
13311 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
13312 {
13313   unsigned dimension_number;
13314   tree lower, upper;
13315   dw_die_ref subrange_die;
13316
13317   for (dimension_number = 0;
13318        TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
13319        type = TREE_TYPE (type), dimension_number++)
13320     {
13321       tree domain = TYPE_DOMAIN (type);
13322
13323       if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
13324         break;
13325
13326       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
13327          and (in GNU C only) variable bounds.  Handle all three forms
13328          here.  */
13329       subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
13330       if (domain)
13331         {
13332           /* We have an array type with specified bounds.  */
13333           lower = TYPE_MIN_VALUE (domain);
13334           upper = TYPE_MAX_VALUE (domain);
13335
13336           /* Define the index type.  */
13337           if (TREE_TYPE (domain))
13338             {
13339               /* ??? This is probably an Ada unnamed subrange type.  Ignore the
13340                  TREE_TYPE field.  We can't emit debug info for this
13341                  because it is an unnamed integral type.  */
13342               if (TREE_CODE (domain) == INTEGER_TYPE
13343                   && TYPE_NAME (domain) == NULL_TREE
13344                   && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
13345                   && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
13346                 ;
13347               else
13348                 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
13349                                     type_die);
13350             }
13351
13352           /* ??? If upper is NULL, the array has unspecified length,
13353              but it does have a lower bound.  This happens with Fortran
13354                dimension arr(N:*)
13355              Since the debugger is definitely going to need to know N
13356              to produce useful results, go ahead and output the lower
13357              bound solo, and hope the debugger can cope.  */
13358
13359           add_bound_info (subrange_die, DW_AT_lower_bound, lower);
13360           if (upper)
13361             add_bound_info (subrange_die, DW_AT_upper_bound, upper);
13362         }
13363
13364       /* Otherwise we have an array type with an unspecified length.  The
13365          DWARF-2 spec does not say how to handle this; let's just leave out the
13366          bounds.  */
13367     }
13368 }
13369
13370 static void
13371 add_byte_size_attribute (dw_die_ref die, tree tree_node)
13372 {
13373   unsigned size;
13374
13375   switch (TREE_CODE (tree_node))
13376     {
13377     case ERROR_MARK:
13378       size = 0;
13379       break;
13380     case ENUMERAL_TYPE:
13381     case RECORD_TYPE:
13382     case UNION_TYPE:
13383     case QUAL_UNION_TYPE:
13384       size = int_size_in_bytes (tree_node);
13385       break;
13386     case FIELD_DECL:
13387       /* For a data member of a struct or union, the DW_AT_byte_size is
13388          generally given as the number of bytes normally allocated for an
13389          object of the *declared* type of the member itself.  This is true
13390          even for bit-fields.  */
13391       size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
13392       break;
13393     default:
13394       gcc_unreachable ();
13395     }
13396
13397   /* Note that `size' might be -1 when we get to this point.  If it is, that
13398      indicates that the byte size of the entity in question is variable.  We
13399      have no good way of expressing this fact in Dwarf at the present time,
13400      so just let the -1 pass on through.  */
13401   add_AT_unsigned (die, DW_AT_byte_size, size);
13402 }
13403
13404 /* For a FIELD_DECL node which represents a bit-field, output an attribute
13405    which specifies the distance in bits from the highest order bit of the
13406    "containing object" for the bit-field to the highest order bit of the
13407    bit-field itself.
13408
13409    For any given bit-field, the "containing object" is a hypothetical object
13410    (of some integral or enum type) within which the given bit-field lives.  The
13411    type of this hypothetical "containing object" is always the same as the
13412    declared type of the individual bit-field itself.  The determination of the
13413    exact location of the "containing object" for a bit-field is rather
13414    complicated.  It's handled by the `field_byte_offset' function (above).
13415
13416    Note that it is the size (in bytes) of the hypothetical "containing object"
13417    which will be given in the DW_AT_byte_size attribute for this bit-field.
13418    (See `byte_size_attribute' above).  */
13419
13420 static inline void
13421 add_bit_offset_attribute (dw_die_ref die, tree decl)
13422 {
13423   HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
13424   tree type = DECL_BIT_FIELD_TYPE (decl);
13425   HOST_WIDE_INT bitpos_int;
13426   HOST_WIDE_INT highest_order_object_bit_offset;
13427   HOST_WIDE_INT highest_order_field_bit_offset;
13428   HOST_WIDE_INT unsigned bit_offset;
13429
13430   /* Must be a field and a bit field.  */
13431   gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
13432
13433   /* We can't yet handle bit-fields whose offsets are variable, so if we
13434      encounter such things, just return without generating any attribute
13435      whatsoever.  Likewise for variable or too large size.  */
13436   if (! host_integerp (bit_position (decl), 0)
13437       || ! host_integerp (DECL_SIZE (decl), 1))
13438     return;
13439
13440   bitpos_int = int_bit_position (decl);
13441
13442   /* Note that the bit offset is always the distance (in bits) from the
13443      highest-order bit of the "containing object" to the highest-order bit of
13444      the bit-field itself.  Since the "high-order end" of any object or field
13445      is different on big-endian and little-endian machines, the computation
13446      below must take account of these differences.  */
13447   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
13448   highest_order_field_bit_offset = bitpos_int;
13449
13450   if (! BYTES_BIG_ENDIAN)
13451     {
13452       highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
13453       highest_order_object_bit_offset += simple_type_size_in_bits (type);
13454     }
13455
13456   bit_offset
13457     = (! BYTES_BIG_ENDIAN
13458        ? highest_order_object_bit_offset - highest_order_field_bit_offset
13459        : highest_order_field_bit_offset - highest_order_object_bit_offset);
13460
13461   add_AT_unsigned (die, DW_AT_bit_offset, bit_offset);
13462 }
13463
13464 /* For a FIELD_DECL node which represents a bit field, output an attribute
13465    which specifies the length in bits of the given field.  */
13466
13467 static inline void
13468 add_bit_size_attribute (dw_die_ref die, tree decl)
13469 {
13470   /* Must be a field and a bit field.  */
13471   gcc_assert (TREE_CODE (decl) == FIELD_DECL
13472               && DECL_BIT_FIELD_TYPE (decl));
13473
13474   if (host_integerp (DECL_SIZE (decl), 1))
13475     add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
13476 }
13477
13478 /* If the compiled language is ANSI C, then add a 'prototyped'
13479    attribute, if arg types are given for the parameters of a function.  */
13480
13481 static inline void
13482 add_prototyped_attribute (dw_die_ref die, tree func_type)
13483 {
13484   if (get_AT_unsigned (comp_unit_die, DW_AT_language) == DW_LANG_C89
13485       && TYPE_ARG_TYPES (func_type) != NULL)
13486     add_AT_flag (die, DW_AT_prototyped, 1);
13487 }
13488
13489 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
13490    by looking in either the type declaration or object declaration
13491    equate table.  */
13492
13493 static inline dw_die_ref
13494 add_abstract_origin_attribute (dw_die_ref die, tree origin)
13495 {
13496   dw_die_ref origin_die = NULL;
13497
13498   if (TREE_CODE (origin) != FUNCTION_DECL)
13499     {
13500       /* We may have gotten separated from the block for the inlined
13501          function, if we're in an exception handler or some such; make
13502          sure that the abstract function has been written out.
13503
13504          Doing this for nested functions is wrong, however; functions are
13505          distinct units, and our context might not even be inline.  */
13506       tree fn = origin;
13507
13508       if (TYPE_P (fn))
13509         fn = TYPE_STUB_DECL (fn);
13510
13511       fn = decl_function_context (fn);
13512       if (fn)
13513         dwarf2out_abstract_function (fn);
13514     }
13515
13516   if (DECL_P (origin))
13517     origin_die = lookup_decl_die (origin);
13518   else if (TYPE_P (origin))
13519     origin_die = lookup_type_die (origin);
13520
13521   /* XXX: Functions that are never lowered don't always have correct block
13522      trees (in the case of java, they simply have no block tree, in some other
13523      languages).  For these functions, there is nothing we can really do to
13524      output correct debug info for inlined functions in all cases.  Rather
13525      than die, we'll just produce deficient debug info now, in that we will
13526      have variables without a proper abstract origin.  In the future, when all
13527      functions are lowered, we should re-add a gcc_assert (origin_die)
13528      here.  */
13529
13530   if (origin_die)
13531     add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
13532   return origin_die;
13533 }
13534
13535 /* We do not currently support the pure_virtual attribute.  */
13536
13537 static inline void
13538 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
13539 {
13540   if (DECL_VINDEX (func_decl))
13541     {
13542       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
13543
13544       if (host_integerp (DECL_VINDEX (func_decl), 0))
13545         add_AT_loc (die, DW_AT_vtable_elem_location,
13546                     new_loc_descr (DW_OP_constu,
13547                                    tree_low_cst (DECL_VINDEX (func_decl), 0),
13548                                    0));
13549
13550       /* GNU extension: Record what type this method came from originally.  */
13551       if (debug_info_level > DINFO_LEVEL_TERSE)
13552         add_AT_die_ref (die, DW_AT_containing_type,
13553                         lookup_type_die (DECL_CONTEXT (func_decl)));
13554     }
13555 }
13556 \f
13557 /* Add source coordinate attributes for the given decl.  */
13558
13559 static void
13560 add_src_coords_attributes (dw_die_ref die, tree decl)
13561 {
13562   expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
13563
13564   add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
13565   add_AT_unsigned (die, DW_AT_decl_line, s.line);
13566 }
13567
13568 /* Add a DW_AT_name attribute and source coordinate attribute for the
13569    given decl, but only if it actually has a name.  */
13570
13571 static void
13572 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
13573 {
13574   tree decl_name;
13575
13576   decl_name = DECL_NAME (decl);
13577   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
13578     {
13579       add_name_attribute (die, dwarf2_name (decl, 0));
13580       if (! DECL_ARTIFICIAL (decl))
13581         add_src_coords_attributes (die, decl);
13582
13583       if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
13584           && TREE_PUBLIC (decl)
13585           && !DECL_ABSTRACT (decl)
13586           && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
13587           && !is_fortran ())
13588         {
13589           /* Defer until we have an assembler name set.  */
13590           if (!DECL_ASSEMBLER_NAME_SET_P (decl))
13591             {
13592               limbo_die_node *asm_name;
13593
13594               asm_name = GGC_CNEW (limbo_die_node);
13595               asm_name->die = die;
13596               asm_name->created_for = decl;
13597               asm_name->next = deferred_asm_name;
13598               deferred_asm_name = asm_name;
13599             }
13600           else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
13601             add_AT_string (die, DW_AT_MIPS_linkage_name,
13602                            IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
13603         }
13604     }
13605
13606 #ifdef VMS_DEBUGGING_INFO
13607   /* Get the function's name, as described by its RTL.  This may be different
13608      from the DECL_NAME name used in the source file.  */
13609   if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
13610     {
13611       add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
13612                    XEXP (DECL_RTL (decl), 0));
13613       VEC_safe_push (rtx, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
13614     }
13615 #endif
13616 }
13617
13618 /* Push a new declaration scope.  */
13619
13620 static void
13621 push_decl_scope (tree scope)
13622 {
13623   VEC_safe_push (tree, gc, decl_scope_table, scope);
13624 }
13625
13626 /* Pop a declaration scope.  */
13627
13628 static inline void
13629 pop_decl_scope (void)
13630 {
13631   VEC_pop (tree, decl_scope_table);
13632 }
13633
13634 /* Return the DIE for the scope that immediately contains this type.
13635    Non-named types get global scope.  Named types nested in other
13636    types get their containing scope if it's open, or global scope
13637    otherwise.  All other types (i.e. function-local named types) get
13638    the current active scope.  */
13639
13640 static dw_die_ref
13641 scope_die_for (tree t, dw_die_ref context_die)
13642 {
13643   dw_die_ref scope_die = NULL;
13644   tree containing_scope;
13645   int i;
13646
13647   /* Non-types always go in the current scope.  */
13648   gcc_assert (TYPE_P (t));
13649
13650   containing_scope = TYPE_CONTEXT (t);
13651
13652   /* Use the containing namespace if it was passed in (for a declaration).  */
13653   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
13654     {
13655       if (context_die == lookup_decl_die (containing_scope))
13656         /* OK */;
13657       else
13658         containing_scope = NULL_TREE;
13659     }
13660
13661   /* Ignore function type "scopes" from the C frontend.  They mean that
13662      a tagged type is local to a parmlist of a function declarator, but
13663      that isn't useful to DWARF.  */
13664   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
13665     containing_scope = NULL_TREE;
13666
13667   if (containing_scope == NULL_TREE)
13668     scope_die = comp_unit_die;
13669   else if (TYPE_P (containing_scope))
13670     {
13671       /* For types, we can just look up the appropriate DIE.  But
13672          first we check to see if we're in the middle of emitting it
13673          so we know where the new DIE should go.  */
13674       for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
13675         if (VEC_index (tree, decl_scope_table, i) == containing_scope)
13676           break;
13677
13678       if (i < 0)
13679         {
13680           gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
13681                       || TREE_ASM_WRITTEN (containing_scope));
13682
13683           /* If none of the current dies are suitable, we get file scope.  */
13684           scope_die = comp_unit_die;
13685         }
13686       else
13687         scope_die = lookup_type_die (containing_scope);
13688     }
13689   else
13690     scope_die = context_die;
13691
13692   return scope_die;
13693 }
13694
13695 /* Returns nonzero if CONTEXT_DIE is internal to a function.  */
13696
13697 static inline int
13698 local_scope_p (dw_die_ref context_die)
13699 {
13700   for (; context_die; context_die = context_die->die_parent)
13701     if (context_die->die_tag == DW_TAG_inlined_subroutine
13702         || context_die->die_tag == DW_TAG_subprogram)
13703       return 1;
13704
13705   return 0;
13706 }
13707
13708 /* Returns nonzero if CONTEXT_DIE is a class.  */
13709
13710 static inline int
13711 class_scope_p (dw_die_ref context_die)
13712 {
13713   return (context_die
13714           && (context_die->die_tag == DW_TAG_structure_type
13715               || context_die->die_tag == DW_TAG_class_type
13716               || context_die->die_tag == DW_TAG_interface_type
13717               || context_die->die_tag == DW_TAG_union_type));
13718 }
13719
13720 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
13721    whether or not to treat a DIE in this context as a declaration.  */
13722
13723 static inline int
13724 class_or_namespace_scope_p (dw_die_ref context_die)
13725 {
13726   return (class_scope_p (context_die)
13727           || (context_die && context_die->die_tag == DW_TAG_namespace));
13728 }
13729
13730 /* Many forms of DIEs require a "type description" attribute.  This
13731    routine locates the proper "type descriptor" die for the type given
13732    by 'type', and adds a DW_AT_type attribute below the given die.  */
13733
13734 static void
13735 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
13736                     int decl_volatile, dw_die_ref context_die)
13737 {
13738   enum tree_code code  = TREE_CODE (type);
13739   dw_die_ref type_die  = NULL;
13740
13741   /* ??? If this type is an unnamed subrange type of an integral, floating-point
13742      or fixed-point type, use the inner type.  This is because we have no
13743      support for unnamed types in base_type_die.  This can happen if this is
13744      an Ada subrange type.  Correct solution is emit a subrange type die.  */
13745   if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
13746       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
13747     type = TREE_TYPE (type), code = TREE_CODE (type);
13748
13749   if (code == ERROR_MARK
13750       /* Handle a special case.  For functions whose return type is void, we
13751          generate *no* type attribute.  (Note that no object may have type
13752          `void', so this only applies to function return types).  */
13753       || code == VOID_TYPE)
13754     return;
13755
13756   type_die = modified_type_die (type,
13757                                 decl_const || TYPE_READONLY (type),
13758                                 decl_volatile || TYPE_VOLATILE (type),
13759                                 context_die);
13760
13761   if (type_die != NULL)
13762     add_AT_die_ref (object_die, DW_AT_type, type_die);
13763 }
13764
13765 /* Given an object die, add the calling convention attribute for the
13766    function call type.  */
13767 static void
13768 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
13769 {
13770   enum dwarf_calling_convention value = DW_CC_normal;
13771
13772   value = ((enum dwarf_calling_convention)
13773            targetm.dwarf_calling_convention (TREE_TYPE (decl)));
13774
13775   /* DWARF doesn't provide a way to identify a program's source-level
13776      entry point.  DW_AT_calling_convention attributes are only meant
13777      to describe functions' calling conventions.  However, lacking a
13778      better way to signal the Fortran main program, we use this for the
13779      time being, following existing custom.  */
13780   if (is_fortran ()
13781       && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
13782     value = DW_CC_program;
13783
13784   /* Only add the attribute if the backend requests it, and
13785      is not DW_CC_normal.  */
13786   if (value && (value != DW_CC_normal))
13787     add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
13788 }
13789
13790 /* Given a tree pointer to a struct, class, union, or enum type node, return
13791    a pointer to the (string) tag name for the given type, or zero if the type
13792    was declared without a tag.  */
13793
13794 static const char *
13795 type_tag (const_tree type)
13796 {
13797   const char *name = 0;
13798
13799   if (TYPE_NAME (type) != 0)
13800     {
13801       tree t = 0;
13802
13803       /* Find the IDENTIFIER_NODE for the type name.  */
13804       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
13805         t = TYPE_NAME (type);
13806
13807       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
13808          a TYPE_DECL node, regardless of whether or not a `typedef' was
13809          involved.  */
13810       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
13811                && ! DECL_IGNORED_P (TYPE_NAME (type)))
13812         {
13813           /* We want to be extra verbose.  Don't call dwarf_name if
13814              DECL_NAME isn't set.  The default hook for decl_printable_name
13815              doesn't like that, and in this context it's correct to return
13816              0, instead of "<anonymous>" or the like.  */
13817           if (DECL_NAME (TYPE_NAME (type)))
13818             name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
13819         }
13820
13821       /* Now get the name as a string, or invent one.  */
13822       if (!name && t != 0)
13823         name = IDENTIFIER_POINTER (t);
13824     }
13825
13826   return (name == 0 || *name == '\0') ? 0 : name;
13827 }
13828
13829 /* Return the type associated with a data member, make a special check
13830    for bit field types.  */
13831
13832 static inline tree
13833 member_declared_type (const_tree member)
13834 {
13835   return (DECL_BIT_FIELD_TYPE (member)
13836           ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
13837 }
13838
13839 /* Get the decl's label, as described by its RTL. This may be different
13840    from the DECL_NAME name used in the source file.  */
13841
13842 #if 0
13843 static const char *
13844 decl_start_label (tree decl)
13845 {
13846   rtx x;
13847   const char *fnname;
13848
13849   x = DECL_RTL (decl);
13850   gcc_assert (MEM_P (x));
13851
13852   x = XEXP (x, 0);
13853   gcc_assert (GET_CODE (x) == SYMBOL_REF);
13854
13855   fnname = XSTR (x, 0);
13856   return fnname;
13857 }
13858 #endif
13859 \f
13860 /* These routines generate the internal representation of the DIE's for
13861    the compilation unit.  Debugging information is collected by walking
13862    the declaration trees passed in from dwarf2out_decl().  */
13863
13864 static void
13865 gen_array_type_die (tree type, dw_die_ref context_die)
13866 {
13867   dw_die_ref scope_die = scope_die_for (type, context_die);
13868   dw_die_ref array_die;
13869
13870   /* GNU compilers represent multidimensional array types as sequences of one
13871      dimensional array types whose element types are themselves array types.
13872      We sometimes squish that down to a single array_type DIE with multiple
13873      subscripts in the Dwarf debugging info.  The draft Dwarf specification
13874      say that we are allowed to do this kind of compression in C, because
13875      there is no difference between an array of arrays and a multidimensional
13876      array.  We don't do this for Ada to remain as close as possible to the
13877      actual representation, which is especially important against the language
13878      flexibilty wrt arrays of variable size.  */
13879
13880   bool collapse_nested_arrays = !is_ada ();
13881   tree element_type;
13882
13883   /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
13884      DW_TAG_string_type doesn't have DW_AT_type attribute).  */
13885   if (TYPE_STRING_FLAG (type)
13886       && TREE_CODE (type) == ARRAY_TYPE
13887       && is_fortran ()
13888       && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
13889     {
13890       HOST_WIDE_INT size;
13891
13892       array_die = new_die (DW_TAG_string_type, scope_die, type);
13893       add_name_attribute (array_die, type_tag (type));
13894       equate_type_number_to_die (type, array_die);
13895       size = int_size_in_bytes (type);
13896       if (size >= 0)
13897         add_AT_unsigned (array_die, DW_AT_byte_size, size);
13898       else if (TYPE_DOMAIN (type) != NULL_TREE
13899                && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
13900                && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
13901         {
13902           tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
13903           dw_loc_descr_ref loc = loc_descriptor_from_tree (szdecl);
13904
13905           size = int_size_in_bytes (TREE_TYPE (szdecl));
13906           if (loc && size > 0)
13907             {
13908               add_AT_loc (array_die, DW_AT_string_length, loc);
13909               if (size != DWARF2_ADDR_SIZE)
13910                 add_AT_unsigned (array_die, DW_AT_byte_size, size);
13911             }
13912         }
13913       return;
13914     }
13915
13916   /* ??? The SGI dwarf reader fails for array of array of enum types
13917      (e.g. const enum machine_mode insn_operand_mode[2][10]) unless the inner
13918      array type comes before the outer array type.  We thus call gen_type_die
13919      before we new_die and must prevent nested array types collapsing for this
13920      target.  */
13921
13922 #ifdef MIPS_DEBUGGING_INFO
13923   gen_type_die (TREE_TYPE (type), context_die);
13924   collapse_nested_arrays = false;
13925 #endif
13926
13927   array_die = new_die (DW_TAG_array_type, scope_die, type);
13928   add_name_attribute (array_die, type_tag (type));
13929   equate_type_number_to_die (type, array_die);
13930
13931   if (TREE_CODE (type) == VECTOR_TYPE)
13932     {
13933       /* The frontend feeds us a representation for the vector as a struct
13934          containing an array.  Pull out the array type.  */
13935       type = TREE_TYPE (TYPE_FIELDS (TYPE_DEBUG_REPRESENTATION_TYPE (type)));
13936       add_AT_flag (array_die, DW_AT_GNU_vector, 1);
13937     }
13938
13939   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
13940   if (is_fortran ()
13941       && TREE_CODE (type) == ARRAY_TYPE
13942       && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
13943       && !TYPE_STRING_FLAG (TREE_TYPE (type)))
13944     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
13945
13946 #if 0
13947   /* We default the array ordering.  SDB will probably do
13948      the right things even if DW_AT_ordering is not present.  It's not even
13949      an issue until we start to get into multidimensional arrays anyway.  If
13950      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
13951      then we'll have to put the DW_AT_ordering attribute back in.  (But if
13952      and when we find out that we need to put these in, we will only do so
13953      for multidimensional arrays.  */
13954   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
13955 #endif
13956
13957 #ifdef MIPS_DEBUGGING_INFO
13958   /* The SGI compilers handle arrays of unknown bound by setting
13959      AT_declaration and not emitting any subrange DIEs.  */
13960   if (! TYPE_DOMAIN (type))
13961     add_AT_flag (array_die, DW_AT_declaration, 1);
13962   else
13963 #endif
13964     add_subscript_info (array_die, type, collapse_nested_arrays);
13965
13966   /* Add representation of the type of the elements of this array type and
13967      emit the corresponding DIE if we haven't done it already.  */  
13968   element_type = TREE_TYPE (type);
13969   if (collapse_nested_arrays)
13970     while (TREE_CODE (element_type) == ARRAY_TYPE)
13971       {
13972         if (TYPE_STRING_FLAG (element_type) && is_fortran ())
13973           break;
13974         element_type = TREE_TYPE (element_type);
13975       }
13976
13977 #ifndef MIPS_DEBUGGING_INFO
13978   gen_type_die (element_type, context_die);
13979 #endif
13980
13981   add_type_attribute (array_die, element_type, 0, 0, context_die);
13982
13983   if (get_AT (array_die, DW_AT_name))
13984     add_pubtype (type, array_die);
13985 }
13986
13987 static dw_loc_descr_ref
13988 descr_info_loc (tree val, tree base_decl)
13989 {
13990   HOST_WIDE_INT size;
13991   dw_loc_descr_ref loc, loc2;
13992   enum dwarf_location_atom op;
13993
13994   if (val == base_decl)
13995     return new_loc_descr (DW_OP_push_object_address, 0, 0);
13996
13997   switch (TREE_CODE (val))
13998     {
13999     CASE_CONVERT:
14000       return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
14001     case VAR_DECL:
14002       return loc_descriptor_from_tree_1 (val, 0);
14003     case INTEGER_CST:
14004       if (host_integerp (val, 0))
14005         return int_loc_descriptor (tree_low_cst (val, 0));
14006       break;
14007     case INDIRECT_REF:
14008       size = int_size_in_bytes (TREE_TYPE (val));
14009       if (size < 0)
14010         break;
14011       loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
14012       if (!loc)
14013         break;
14014       if (size == DWARF2_ADDR_SIZE)
14015         add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
14016       else
14017         add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
14018       return loc;
14019     case POINTER_PLUS_EXPR:
14020     case PLUS_EXPR:
14021       if (host_integerp (TREE_OPERAND (val, 1), 1)
14022           && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
14023              < 16384)
14024         {
14025           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
14026           if (!loc)
14027             break;
14028           loc_descr_plus_const (&loc, tree_low_cst (TREE_OPERAND (val, 1), 0));
14029         }
14030       else
14031         {
14032           op = DW_OP_plus;
14033         do_binop:
14034           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
14035           if (!loc)
14036             break;
14037           loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
14038           if (!loc2)
14039             break;
14040           add_loc_descr (&loc, loc2);
14041           add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
14042         }
14043       return loc;
14044     case MINUS_EXPR:
14045       op = DW_OP_minus;
14046       goto do_binop;
14047     case MULT_EXPR:
14048       op = DW_OP_mul;
14049       goto do_binop;
14050     case EQ_EXPR:
14051       op = DW_OP_eq;
14052       goto do_binop;
14053     case NE_EXPR:
14054       op = DW_OP_ne;
14055       goto do_binop;
14056     default:
14057       break;
14058     }
14059   return NULL;
14060 }
14061
14062 static void
14063 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
14064                       tree val, tree base_decl)
14065 {
14066   dw_loc_descr_ref loc;
14067
14068   if (host_integerp (val, 0))
14069     {
14070       add_AT_unsigned (die, attr, tree_low_cst (val, 0));
14071       return;
14072     }
14073
14074   loc = descr_info_loc (val, base_decl);
14075   if (!loc)
14076     return;
14077
14078   add_AT_loc (die, attr, loc);
14079 }
14080
14081 /* This routine generates DIE for array with hidden descriptor, details
14082    are filled into *info by a langhook.  */
14083
14084 static void
14085 gen_descr_array_type_die (tree type, struct array_descr_info *info,
14086                           dw_die_ref context_die)
14087 {
14088   dw_die_ref scope_die = scope_die_for (type, context_die);
14089   dw_die_ref array_die;
14090   int dim;
14091
14092   array_die = new_die (DW_TAG_array_type, scope_die, type);
14093   add_name_attribute (array_die, type_tag (type));
14094   equate_type_number_to_die (type, array_die);
14095
14096   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
14097   if (is_fortran ()
14098       && info->ndimensions >= 2)
14099     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
14100
14101   if (info->data_location)
14102     add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
14103                           info->base_decl);
14104   if (info->associated)
14105     add_descr_info_field (array_die, DW_AT_associated, info->associated,
14106                           info->base_decl);
14107   if (info->allocated)
14108     add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
14109                           info->base_decl);
14110
14111   for (dim = 0; dim < info->ndimensions; dim++)
14112     {
14113       dw_die_ref subrange_die
14114         = new_die (DW_TAG_subrange_type, array_die, NULL);
14115
14116       if (info->dimen[dim].lower_bound)
14117         {
14118           /* If it is the default value, omit it.  */
14119           if ((is_c_family () || is_java ())
14120               && integer_zerop (info->dimen[dim].lower_bound))
14121             ;
14122           else if (is_fortran ()
14123                    && integer_onep (info->dimen[dim].lower_bound))
14124             ;
14125           else
14126             add_descr_info_field (subrange_die, DW_AT_lower_bound,
14127                                   info->dimen[dim].lower_bound,
14128                                   info->base_decl);
14129         }
14130       if (info->dimen[dim].upper_bound)
14131         add_descr_info_field (subrange_die, DW_AT_upper_bound,
14132                               info->dimen[dim].upper_bound,
14133                               info->base_decl);
14134       if (info->dimen[dim].stride)
14135         add_descr_info_field (subrange_die, DW_AT_byte_stride,
14136                               info->dimen[dim].stride,
14137                               info->base_decl);
14138     }
14139
14140   gen_type_die (info->element_type, context_die);
14141   add_type_attribute (array_die, info->element_type, 0, 0, context_die);
14142
14143   if (get_AT (array_die, DW_AT_name))
14144     add_pubtype (type, array_die);
14145 }
14146
14147 #if 0
14148 static void
14149 gen_entry_point_die (tree decl, dw_die_ref context_die)
14150 {
14151   tree origin = decl_ultimate_origin (decl);
14152   dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
14153
14154   if (origin != NULL)
14155     add_abstract_origin_attribute (decl_die, origin);
14156   else
14157     {
14158       add_name_and_src_coords_attributes (decl_die, decl);
14159       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
14160                           0, 0, context_die);
14161     }
14162
14163   if (DECL_ABSTRACT (decl))
14164     equate_decl_number_to_die (decl, decl_die);
14165   else
14166     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
14167 }
14168 #endif
14169
14170 /* Walk through the list of incomplete types again, trying once more to
14171    emit full debugging info for them.  */
14172
14173 static void
14174 retry_incomplete_types (void)
14175 {
14176   int i;
14177
14178   for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
14179     gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die);
14180 }
14181
14182 /* Determine what tag to use for a record type.  */
14183
14184 static enum dwarf_tag
14185 record_type_tag (tree type)
14186 {
14187   if (! lang_hooks.types.classify_record)
14188     return DW_TAG_structure_type;
14189
14190   switch (lang_hooks.types.classify_record (type))
14191     {
14192     case RECORD_IS_STRUCT:
14193       return DW_TAG_structure_type;
14194
14195     case RECORD_IS_CLASS:
14196       return DW_TAG_class_type;
14197
14198     case RECORD_IS_INTERFACE:
14199       return DW_TAG_interface_type;
14200
14201     default:
14202       gcc_unreachable ();
14203     }
14204 }
14205
14206 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
14207    include all of the information about the enumeration values also. Each
14208    enumerated type name/value is listed as a child of the enumerated type
14209    DIE.  */
14210
14211 static dw_die_ref
14212 gen_enumeration_type_die (tree type, dw_die_ref context_die)
14213 {
14214   dw_die_ref type_die = lookup_type_die (type);
14215
14216   if (type_die == NULL)
14217     {
14218       type_die = new_die (DW_TAG_enumeration_type,
14219                           scope_die_for (type, context_die), type);
14220       equate_type_number_to_die (type, type_die);
14221       add_name_attribute (type_die, type_tag (type));
14222     }
14223   else if (! TYPE_SIZE (type))
14224     return type_die;
14225   else
14226     remove_AT (type_die, DW_AT_declaration);
14227
14228   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
14229      given enum type is incomplete, do not generate the DW_AT_byte_size
14230      attribute or the DW_AT_element_list attribute.  */
14231   if (TYPE_SIZE (type))
14232     {
14233       tree link;
14234
14235       TREE_ASM_WRITTEN (type) = 1;
14236       add_byte_size_attribute (type_die, type);
14237       if (TYPE_STUB_DECL (type) != NULL_TREE)
14238         add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
14239
14240       /* If the first reference to this type was as the return type of an
14241          inline function, then it may not have a parent.  Fix this now.  */
14242       if (type_die->die_parent == NULL)
14243         add_child_die (scope_die_for (type, context_die), type_die);
14244
14245       for (link = TYPE_VALUES (type);
14246            link != NULL; link = TREE_CHAIN (link))
14247         {
14248           dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
14249           tree value = TREE_VALUE (link);
14250
14251           add_name_attribute (enum_die,
14252                               IDENTIFIER_POINTER (TREE_PURPOSE (link)));
14253
14254           if (TREE_CODE (value) == CONST_DECL)
14255             value = DECL_INITIAL (value);
14256
14257           if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
14258             /* DWARF2 does not provide a way of indicating whether or
14259                not enumeration constants are signed or unsigned.  GDB
14260                always assumes the values are signed, so we output all
14261                values as if they were signed.  That means that
14262                enumeration constants with very large unsigned values
14263                will appear to have negative values in the debugger.  */
14264             add_AT_int (enum_die, DW_AT_const_value,
14265                         tree_low_cst (value, tree_int_cst_sgn (value) > 0));
14266         }
14267     }
14268   else
14269     add_AT_flag (type_die, DW_AT_declaration, 1);
14270
14271   if (get_AT (type_die, DW_AT_name))
14272     add_pubtype (type, type_die);
14273
14274   return type_die;
14275 }
14276
14277 /* Generate a DIE to represent either a real live formal parameter decl or to
14278    represent just the type of some formal parameter position in some function
14279    type.
14280
14281    Note that this routine is a bit unusual because its argument may be a
14282    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
14283    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
14284    node.  If it's the former then this function is being called to output a
14285    DIE to represent a formal parameter object (or some inlining thereof).  If
14286    it's the latter, then this function is only being called to output a
14287    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
14288    argument type of some subprogram type.  */
14289
14290 static dw_die_ref
14291 gen_formal_parameter_die (tree node, tree origin, dw_die_ref context_die)
14292 {
14293   tree node_or_origin = node ? node : origin;
14294   dw_die_ref parm_die
14295     = new_die (DW_TAG_formal_parameter, context_die, node);
14296
14297   switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
14298     {
14299     case tcc_declaration:
14300       if (!origin)
14301         origin = decl_ultimate_origin (node);
14302       if (origin != NULL)
14303         add_abstract_origin_attribute (parm_die, origin);
14304       else
14305         {
14306           tree type = TREE_TYPE (node);
14307           add_name_and_src_coords_attributes (parm_die, node);
14308           if (DECL_BY_REFERENCE (node))
14309             add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
14310                                 context_die);
14311           else
14312             add_type_attribute (parm_die, type,
14313                                 TREE_READONLY (node),
14314                                 TREE_THIS_VOLATILE (node),
14315                                 context_die);
14316           if (DECL_ARTIFICIAL (node))
14317             add_AT_flag (parm_die, DW_AT_artificial, 1);
14318         }
14319
14320       if (node && node != origin)
14321         equate_decl_number_to_die (node, parm_die);
14322       if (! DECL_ABSTRACT (node_or_origin))
14323         add_location_or_const_value_attribute (parm_die, node_or_origin,
14324                                                DW_AT_location);
14325
14326       break;
14327
14328     case tcc_type:
14329       /* We were called with some kind of a ..._TYPE node.  */
14330       add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
14331       break;
14332
14333     default:
14334       gcc_unreachable ();
14335     }
14336
14337   return parm_die;
14338 }
14339
14340 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
14341    at the end of an (ANSI prototyped) formal parameters list.  */
14342
14343 static void
14344 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
14345 {
14346   new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
14347 }
14348
14349 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
14350    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
14351    parameters as specified in some function type specification (except for
14352    those which appear as part of a function *definition*).  */
14353
14354 static void
14355 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
14356 {
14357   tree link;
14358   tree formal_type = NULL;
14359   tree first_parm_type;
14360   tree arg;
14361
14362   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
14363     {
14364       arg = DECL_ARGUMENTS (function_or_method_type);
14365       function_or_method_type = TREE_TYPE (function_or_method_type);
14366     }
14367   else
14368     arg = NULL_TREE;
14369
14370   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
14371
14372   /* Make our first pass over the list of formal parameter types and output a
14373      DW_TAG_formal_parameter DIE for each one.  */
14374   for (link = first_parm_type; link; )
14375     {
14376       dw_die_ref parm_die;
14377
14378       formal_type = TREE_VALUE (link);
14379       if (formal_type == void_type_node)
14380         break;
14381
14382       /* Output a (nameless) DIE to represent the formal parameter itself.  */
14383       parm_die = gen_formal_parameter_die (formal_type, NULL, context_die);
14384       if ((TREE_CODE (function_or_method_type) == METHOD_TYPE
14385            && link == first_parm_type)
14386           || (arg && DECL_ARTIFICIAL (arg)))
14387         add_AT_flag (parm_die, DW_AT_artificial, 1);
14388
14389       link = TREE_CHAIN (link);
14390       if (arg)
14391         arg = TREE_CHAIN (arg);
14392     }
14393
14394   /* If this function type has an ellipsis, add a
14395      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
14396   if (formal_type != void_type_node)
14397     gen_unspecified_parameters_die (function_or_method_type, context_die);
14398
14399   /* Make our second (and final) pass over the list of formal parameter types
14400      and output DIEs to represent those types (as necessary).  */
14401   for (link = TYPE_ARG_TYPES (function_or_method_type);
14402        link && TREE_VALUE (link);
14403        link = TREE_CHAIN (link))
14404     gen_type_die (TREE_VALUE (link), context_die);
14405 }
14406
14407 /* We want to generate the DIE for TYPE so that we can generate the
14408    die for MEMBER, which has been defined; we will need to refer back
14409    to the member declaration nested within TYPE.  If we're trying to
14410    generate minimal debug info for TYPE, processing TYPE won't do the
14411    trick; we need to attach the member declaration by hand.  */
14412
14413 static void
14414 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
14415 {
14416   gen_type_die (type, context_die);
14417
14418   /* If we're trying to avoid duplicate debug info, we may not have
14419      emitted the member decl for this function.  Emit it now.  */
14420   if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
14421       && ! lookup_decl_die (member))
14422     {
14423       dw_die_ref type_die;
14424       gcc_assert (!decl_ultimate_origin (member));
14425
14426       push_decl_scope (type);
14427       type_die = lookup_type_die (type);
14428       if (TREE_CODE (member) == FUNCTION_DECL)
14429         gen_subprogram_die (member, type_die);
14430       else if (TREE_CODE (member) == FIELD_DECL)
14431         {
14432           /* Ignore the nameless fields that are used to skip bits but handle
14433              C++ anonymous unions and structs.  */
14434           if (DECL_NAME (member) != NULL_TREE
14435               || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
14436               || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
14437             {
14438               gen_type_die (member_declared_type (member), type_die);
14439               gen_field_die (member, type_die);
14440             }
14441         }
14442       else
14443         gen_variable_die (member, NULL_TREE, type_die);
14444
14445       pop_decl_scope ();
14446     }
14447 }
14448
14449 /* Generate the DWARF2 info for the "abstract" instance of a function which we
14450    may later generate inlined and/or out-of-line instances of.  */
14451
14452 static void
14453 dwarf2out_abstract_function (tree decl)
14454 {
14455   dw_die_ref old_die;
14456   tree save_fn;
14457   tree context;
14458   int was_abstract = DECL_ABSTRACT (decl);
14459
14460   /* Make sure we have the actual abstract inline, not a clone.  */
14461   decl = DECL_ORIGIN (decl);
14462   htab_empty (decl_loc_table);
14463
14464   old_die = lookup_decl_die (decl);
14465   if (old_die && get_AT (old_die, DW_AT_inline))
14466     /* We've already generated the abstract instance.  */
14467     return;
14468
14469   /* Be sure we've emitted the in-class declaration DIE (if any) first, so
14470      we don't get confused by DECL_ABSTRACT.  */
14471   if (debug_info_level > DINFO_LEVEL_TERSE)
14472     {
14473       context = decl_class_context (decl);
14474       if (context)
14475         gen_type_die_for_member
14476           (context, decl, decl_function_context (decl) ? NULL : comp_unit_die);
14477     }
14478
14479   /* Pretend we've just finished compiling this function.  */
14480   save_fn = current_function_decl;
14481   current_function_decl = decl;
14482   push_cfun (DECL_STRUCT_FUNCTION (decl));
14483
14484   set_decl_abstract_flags (decl, 1);
14485   dwarf2out_decl (decl);
14486   if (! was_abstract)
14487     set_decl_abstract_flags (decl, 0);
14488
14489   current_function_decl = save_fn;
14490   pop_cfun ();
14491 }
14492
14493 /* Helper function of premark_used_types() which gets called through
14494    htab_traverse_resize().
14495
14496    Marks the DIE of a given type in *SLOT as perennial, so it never gets
14497    marked as unused by prune_unused_types.  */
14498 static int
14499 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
14500 {
14501   tree type;
14502   dw_die_ref die;
14503
14504   type = (tree) *slot;
14505   die = lookup_type_die (type);
14506   if (die != NULL)
14507     die->die_perennial_p = 1;
14508   return 1;
14509 }
14510
14511 /* Mark all members of used_types_hash as perennial.  */
14512 static void
14513 premark_used_types (void)
14514 {
14515   if (cfun && cfun->used_types_hash)
14516     htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
14517 }
14518
14519 /* Generate a DIE to represent a declared function (either file-scope or
14520    block-local).  */
14521
14522 static void
14523 gen_subprogram_die (tree decl, dw_die_ref context_die)
14524 {
14525   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
14526   tree origin = decl_ultimate_origin (decl);
14527   dw_die_ref subr_die;
14528   tree fn_arg_types;
14529   tree outer_scope;
14530   dw_die_ref old_die = lookup_decl_die (decl);
14531   int declaration = (current_function_decl != decl
14532                      || class_or_namespace_scope_p (context_die));
14533
14534   premark_used_types ();
14535
14536   /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
14537      started to generate the abstract instance of an inline, decided to output
14538      its containing class, and proceeded to emit the declaration of the inline
14539      from the member list for the class.  If so, DECLARATION takes priority;
14540      we'll get back to the abstract instance when done with the class.  */
14541
14542   /* The class-scope declaration DIE must be the primary DIE.  */
14543   if (origin && declaration && class_or_namespace_scope_p (context_die))
14544     {
14545       origin = NULL;
14546       gcc_assert (!old_die);
14547     }
14548
14549   /* Now that the C++ front end lazily declares artificial member fns, we
14550      might need to retrofit the declaration into its class.  */
14551   if (!declaration && !origin && !old_die
14552       && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
14553       && !class_or_namespace_scope_p (context_die)
14554       && debug_info_level > DINFO_LEVEL_TERSE)
14555     old_die = force_decl_die (decl);
14556
14557   if (origin != NULL)
14558     {
14559       gcc_assert (!declaration || local_scope_p (context_die));
14560
14561       /* Fixup die_parent for the abstract instance of a nested
14562          inline function.  */
14563       if (old_die && old_die->die_parent == NULL)
14564         add_child_die (context_die, old_die);
14565
14566       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
14567       add_abstract_origin_attribute (subr_die, origin);
14568     }
14569   else if (old_die)
14570     {
14571       expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
14572       struct dwarf_file_data * file_index = lookup_filename (s.file);
14573
14574       if (!get_AT_flag (old_die, DW_AT_declaration)
14575           /* We can have a normal definition following an inline one in the
14576              case of redefinition of GNU C extern inlines.
14577              It seems reasonable to use AT_specification in this case.  */
14578           && !get_AT (old_die, DW_AT_inline))
14579         {
14580           /* Detect and ignore this case, where we are trying to output
14581              something we have already output.  */
14582           return;
14583         }
14584
14585       /* If the definition comes from the same place as the declaration,
14586          maybe use the old DIE.  We always want the DIE for this function
14587          that has the *_pc attributes to be under comp_unit_die so the
14588          debugger can find it.  We also need to do this for abstract
14589          instances of inlines, since the spec requires the out-of-line copy
14590          to have the same parent.  For local class methods, this doesn't
14591          apply; we just use the old DIE.  */
14592       if ((old_die->die_parent == comp_unit_die || context_die == NULL)
14593           && (DECL_ARTIFICIAL (decl)
14594               || (get_AT_file (old_die, DW_AT_decl_file) == file_index
14595                   && (get_AT_unsigned (old_die, DW_AT_decl_line)
14596                       == (unsigned) s.line))))
14597         {
14598           subr_die = old_die;
14599
14600           /* Clear out the declaration attribute and the formal parameters.
14601              Do not remove all children, because it is possible that this
14602              declaration die was forced using force_decl_die(). In such
14603              cases die that forced declaration die (e.g. TAG_imported_module)
14604              is one of the children that we do not want to remove.  */
14605           remove_AT (subr_die, DW_AT_declaration);
14606           remove_child_TAG (subr_die, DW_TAG_formal_parameter);
14607         }
14608       else
14609         {
14610           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
14611           add_AT_specification (subr_die, old_die);
14612           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
14613             add_AT_file (subr_die, DW_AT_decl_file, file_index);
14614           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
14615             add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
14616         }
14617     }
14618   else
14619     {
14620       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
14621
14622       if (TREE_PUBLIC (decl))
14623         add_AT_flag (subr_die, DW_AT_external, 1);
14624
14625       add_name_and_src_coords_attributes (subr_die, decl);
14626       if (debug_info_level > DINFO_LEVEL_TERSE)
14627         {
14628           add_prototyped_attribute (subr_die, TREE_TYPE (decl));
14629           add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
14630                               0, 0, context_die);
14631         }
14632
14633       add_pure_or_virtual_attribute (subr_die, decl);
14634       if (DECL_ARTIFICIAL (decl))
14635         add_AT_flag (subr_die, DW_AT_artificial, 1);
14636
14637       if (TREE_PROTECTED (decl))
14638         add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_protected);
14639       else if (TREE_PRIVATE (decl))
14640         add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_private);
14641     }
14642
14643   if (declaration)
14644     {
14645       if (!old_die || !get_AT (old_die, DW_AT_inline))
14646         {
14647           add_AT_flag (subr_die, DW_AT_declaration, 1);
14648
14649           /* If this is an explicit function declaration then generate
14650              a DW_AT_explicit attribute.  */
14651           if (lang_hooks.decls.function_decl_explicit_p (decl))
14652             add_AT_flag (subr_die, DW_AT_explicit, 1);
14653
14654           /* The first time we see a member function, it is in the context of
14655              the class to which it belongs.  We make sure of this by emitting
14656              the class first.  The next time is the definition, which is
14657              handled above.  The two may come from the same source text.
14658
14659              Note that force_decl_die() forces function declaration die. It is
14660              later reused to represent definition.  */
14661           equate_decl_number_to_die (decl, subr_die);
14662         }
14663     }
14664   else if (DECL_ABSTRACT (decl))
14665     {
14666       if (DECL_DECLARED_INLINE_P (decl))
14667         {
14668           if (cgraph_function_possibly_inlined_p (decl))
14669             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
14670           else
14671             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
14672         }
14673       else
14674         {
14675           if (cgraph_function_possibly_inlined_p (decl))
14676             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
14677           else
14678             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
14679         }
14680
14681       if (DECL_DECLARED_INLINE_P (decl)
14682           && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
14683         add_AT_flag (subr_die, DW_AT_artificial, 1);
14684
14685       equate_decl_number_to_die (decl, subr_die);
14686     }
14687   else if (!DECL_EXTERNAL (decl))
14688     {
14689       HOST_WIDE_INT cfa_fb_offset;
14690
14691       if (!old_die || !get_AT (old_die, DW_AT_inline))
14692         equate_decl_number_to_die (decl, subr_die);
14693
14694       if (!flag_reorder_blocks_and_partition)
14695         {
14696           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
14697                                        current_function_funcdef_no);
14698           add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
14699           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
14700                                        current_function_funcdef_no);
14701           add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
14702
14703           add_pubname (decl, subr_die);
14704           add_arange (decl, subr_die);
14705         }
14706       else
14707         {  /* Do nothing for now; maybe need to duplicate die, one for
14708               hot section and one for cold section, then use the hot/cold
14709               section begin/end labels to generate the aranges...  */
14710           /*
14711             add_AT_lbl_id (subr_die, DW_AT_low_pc, hot_section_label);
14712             add_AT_lbl_id (subr_die, DW_AT_high_pc, hot_section_end_label);
14713             add_AT_lbl_id (subr_die, DW_AT_lo_user, unlikely_section_label);
14714             add_AT_lbl_id (subr_die, DW_AT_hi_user, cold_section_end_label);
14715
14716             add_pubname (decl, subr_die);
14717             add_arange (decl, subr_die);
14718             add_arange (decl, subr_die);
14719            */
14720         }
14721
14722 #ifdef MIPS_DEBUGGING_INFO
14723       /* Add a reference to the FDE for this routine.  */
14724       add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
14725 #endif
14726
14727       cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
14728
14729       /* We define the "frame base" as the function's CFA.  This is more
14730          convenient for several reasons: (1) It's stable across the prologue
14731          and epilogue, which makes it better than just a frame pointer,
14732          (2) With dwarf3, there exists a one-byte encoding that allows us
14733          to reference the .debug_frame data by proxy, but failing that,
14734          (3) We can at least reuse the code inspection and interpretation
14735          code that determines the CFA position at various points in the
14736          function.  */
14737       if (dwarf_version >= 3)
14738         {
14739           dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
14740           add_AT_loc (subr_die, DW_AT_frame_base, op);
14741         }
14742       else
14743         {
14744           dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
14745           if (list->dw_loc_next)
14746             add_AT_loc_list (subr_die, DW_AT_frame_base, list);
14747           else
14748             add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
14749         }
14750
14751       /* Compute a displacement from the "steady-state frame pointer" to
14752          the CFA.  The former is what all stack slots and argument slots
14753          will reference in the rtl; the later is what we've told the
14754          debugger about.  We'll need to adjust all frame_base references
14755          by this displacement.  */
14756       compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
14757
14758       if (cfun->static_chain_decl)
14759         add_AT_location_description (subr_die, DW_AT_static_link,
14760                  loc_descriptor_from_tree (cfun->static_chain_decl));
14761     }
14762
14763   /* Generate child dies for template paramaters.  */
14764   if (debug_info_level > DINFO_LEVEL_TERSE)
14765     gen_generic_params_dies (decl);
14766
14767   /* Now output descriptions of the arguments for this function. This gets
14768      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
14769      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
14770      `...' at the end of the formal parameter list.  In order to find out if
14771      there was a trailing ellipsis or not, we must instead look at the type
14772      associated with the FUNCTION_DECL.  This will be a node of type
14773      FUNCTION_TYPE. If the chain of type nodes hanging off of this
14774      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
14775      an ellipsis at the end.  */
14776
14777   /* In the case where we are describing a mere function declaration, all we
14778      need to do here (and all we *can* do here) is to describe the *types* of
14779      its formal parameters.  */
14780   if (debug_info_level <= DINFO_LEVEL_TERSE)
14781     ;
14782   else if (declaration)
14783     gen_formal_types_die (decl, subr_die);
14784   else
14785     {
14786       /* Generate DIEs to represent all known formal parameters.  */
14787       tree arg_decls = DECL_ARGUMENTS (decl);
14788       tree parm;
14789
14790       /* When generating DIEs, generate the unspecified_parameters DIE
14791          instead if we come across the arg "__builtin_va_alist" */
14792       for (parm = arg_decls; parm; parm = TREE_CHAIN (parm))
14793         if (TREE_CODE (parm) == PARM_DECL)
14794           {
14795             if (DECL_NAME (parm)
14796                 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (parm)),
14797                             "__builtin_va_alist"))
14798               gen_unspecified_parameters_die (parm, subr_die);
14799             else
14800               gen_decl_die (parm, NULL, subr_die);
14801           }
14802
14803       /* Decide whether we need an unspecified_parameters DIE at the end.
14804          There are 2 more cases to do this for: 1) the ansi ... declaration -
14805          this is detectable when the end of the arg list is not a
14806          void_type_node 2) an unprototyped function declaration (not a
14807          definition).  This just means that we have no info about the
14808          parameters at all.  */
14809       fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
14810       if (fn_arg_types != NULL)
14811         {
14812           /* This is the prototyped case, check for....  */
14813           if (TREE_VALUE (tree_last (fn_arg_types)) != void_type_node)
14814             gen_unspecified_parameters_die (decl, subr_die);
14815         }
14816       else if (DECL_INITIAL (decl) == NULL_TREE)
14817         gen_unspecified_parameters_die (decl, subr_die);
14818     }
14819
14820   /* Output Dwarf info for all of the stuff within the body of the function
14821      (if it has one - it may be just a declaration).  */
14822   outer_scope = DECL_INITIAL (decl);
14823
14824   /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
14825      a function.  This BLOCK actually represents the outermost binding contour
14826      for the function, i.e. the contour in which the function's formal
14827      parameters and labels get declared. Curiously, it appears that the front
14828      end doesn't actually put the PARM_DECL nodes for the current function onto
14829      the BLOCK_VARS list for this outer scope, but are strung off of the
14830      DECL_ARGUMENTS list for the function instead.
14831
14832      The BLOCK_VARS list for the `outer_scope' does provide us with a list of
14833      the LABEL_DECL nodes for the function however, and we output DWARF info
14834      for those in decls_for_scope.  Just within the `outer_scope' there will be
14835      a BLOCK node representing the function's outermost pair of curly braces,
14836      and any blocks used for the base and member initializers of a C++
14837      constructor function.  */
14838   if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
14839     {
14840       /* Emit a DW_TAG_variable DIE for a named return value.  */
14841       if (DECL_NAME (DECL_RESULT (decl)))
14842         gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
14843
14844       current_function_has_inlines = 0;
14845       decls_for_scope (outer_scope, subr_die, 0);
14846
14847 #if 0 && defined (MIPS_DEBUGGING_INFO)
14848       if (current_function_has_inlines)
14849         {
14850           add_AT_flag (subr_die, DW_AT_MIPS_has_inlines, 1);
14851           if (! comp_unit_has_inlines)
14852             {
14853               add_AT_flag (comp_unit_die, DW_AT_MIPS_has_inlines, 1);
14854               comp_unit_has_inlines = 1;
14855             }
14856         }
14857 #endif
14858     }
14859   /* Add the calling convention attribute if requested.  */
14860   add_calling_convention_attribute (subr_die, decl);
14861
14862 }
14863
14864 /* Returns a hash value for X (which really is a die_struct).  */
14865
14866 static hashval_t
14867 common_block_die_table_hash (const void *x)
14868 {
14869   const_dw_die_ref d = (const_dw_die_ref) x;
14870   return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
14871 }
14872
14873 /* Return nonzero if decl_id and die_parent of die_struct X is the same
14874    as decl_id and die_parent of die_struct Y.  */
14875
14876 static int
14877 common_block_die_table_eq (const void *x, const void *y)
14878 {
14879   const_dw_die_ref d = (const_dw_die_ref) x;
14880   const_dw_die_ref e = (const_dw_die_ref) y;
14881   return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
14882 }
14883
14884 /* Generate a DIE to represent a declared data object.
14885    Either DECL or ORIGIN must be non-null.  */
14886
14887 static void
14888 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
14889 {
14890   HOST_WIDE_INT off;
14891   tree com_decl;
14892   tree decl_or_origin = decl ? decl : origin;
14893   dw_die_ref var_die;
14894   dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
14895   dw_die_ref origin_die;
14896   int declaration = (DECL_EXTERNAL (decl_or_origin)
14897                      /* If DECL is COMDAT and has not actually been
14898                         emitted, we cannot take its address; there
14899                         might end up being no definition anywhere in
14900                         the program.  For example, consider the C++
14901                         test case:
14902
14903                           template <class T>
14904                           struct S { static const int i = 7; };
14905
14906                           template <class T>
14907                           const int S<T>::i;
14908
14909                           int f() { return S<int>::i; }
14910
14911                         Here, S<int>::i is not DECL_EXTERNAL, but no
14912                         definition is required, so the compiler will
14913                         not emit a definition.  */
14914                      || (TREE_CODE (decl_or_origin) == VAR_DECL
14915                          && DECL_COMDAT (decl_or_origin)
14916                          && !TREE_ASM_WRITTEN (decl_or_origin))
14917                      || class_or_namespace_scope_p (context_die));
14918
14919   if (!origin)
14920     origin = decl_ultimate_origin (decl);
14921
14922   com_decl = fortran_common (decl_or_origin, &off);
14923
14924   /* Symbol in common gets emitted as a child of the common block, in the form
14925      of a data member.  */
14926   if (com_decl)
14927     {
14928       tree field;
14929       dw_die_ref com_die;
14930       dw_loc_descr_ref loc;
14931       die_node com_die_arg;
14932
14933       var_die = lookup_decl_die (decl_or_origin);
14934       if (var_die)
14935         {
14936           if (get_AT (var_die, DW_AT_location) == NULL)
14937             {
14938               loc = loc_descriptor_from_tree (com_decl);
14939               if (loc)
14940                 {
14941                   if (off)
14942                     {
14943                       /* Optimize the common case.  */
14944                       if (loc->dw_loc_opc == DW_OP_addr
14945                           && loc->dw_loc_next == NULL
14946                           && GET_CODE (loc->dw_loc_oprnd1.v.val_addr)
14947                              == SYMBOL_REF)
14948                         loc->dw_loc_oprnd1.v.val_addr
14949                           = plus_constant (loc->dw_loc_oprnd1.v.val_addr, off);
14950                         else
14951                           loc_descr_plus_const (&loc, off);
14952                     }
14953                   add_AT_loc (var_die, DW_AT_location, loc);
14954                   remove_AT (var_die, DW_AT_declaration);
14955                 }
14956             }
14957           return;
14958         }
14959
14960       if (common_block_die_table == NULL)
14961         common_block_die_table
14962           = htab_create_ggc (10, common_block_die_table_hash,
14963                              common_block_die_table_eq, NULL);
14964
14965       field = TREE_OPERAND (DECL_VALUE_EXPR (decl), 0);
14966       com_die_arg.decl_id = DECL_UID (com_decl);
14967       com_die_arg.die_parent = context_die;
14968       com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
14969       loc = loc_descriptor_from_tree (com_decl);
14970       if (com_die == NULL)
14971         {
14972           const char *cnam
14973             = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
14974           void **slot;
14975
14976           com_die = new_die (DW_TAG_common_block, context_die, decl);
14977           add_name_and_src_coords_attributes (com_die, com_decl);
14978           if (loc)
14979             {
14980               add_AT_loc (com_die, DW_AT_location, loc);
14981               /* Avoid sharing the same loc descriptor between
14982                  DW_TAG_common_block and DW_TAG_variable.  */
14983               loc = loc_descriptor_from_tree (com_decl);
14984             }
14985           else if (DECL_EXTERNAL (decl))
14986             add_AT_flag (com_die, DW_AT_declaration, 1);
14987           add_pubname_string (cnam, com_die); /* ??? needed? */
14988           com_die->decl_id = DECL_UID (com_decl);
14989           slot = htab_find_slot (common_block_die_table, com_die, INSERT);
14990           *slot = (void *) com_die;
14991         }
14992       else if (get_AT (com_die, DW_AT_location) == NULL && loc)
14993         {
14994           add_AT_loc (com_die, DW_AT_location, loc);
14995           loc = loc_descriptor_from_tree (com_decl);
14996           remove_AT (com_die, DW_AT_declaration);
14997         }
14998       var_die = new_die (DW_TAG_variable, com_die, decl);
14999       add_name_and_src_coords_attributes (var_die, decl);
15000       add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
15001                           TREE_THIS_VOLATILE (decl), context_die);
15002       add_AT_flag (var_die, DW_AT_external, 1);
15003       if (loc)
15004         {
15005           if (off)
15006             {
15007               /* Optimize the common case.  */
15008               if (loc->dw_loc_opc == DW_OP_addr
15009                   && loc->dw_loc_next == NULL
15010                   && GET_CODE (loc->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
15011                 loc->dw_loc_oprnd1.v.val_addr
15012                   = plus_constant (loc->dw_loc_oprnd1.v.val_addr, off);
15013               else
15014                 loc_descr_plus_const (&loc, off);
15015             }
15016           add_AT_loc (var_die, DW_AT_location, loc);
15017         }
15018       else if (DECL_EXTERNAL (decl))
15019         add_AT_flag (var_die, DW_AT_declaration, 1);
15020       equate_decl_number_to_die (decl, var_die);
15021       return;
15022     }
15023
15024   /* If the compiler emitted a definition for the DECL declaration
15025      and if we already emitted a DIE for it, don't emit a second
15026      DIE for it again.  */
15027   if (old_die
15028       && declaration
15029       && old_die->die_parent == context_die)
15030     return;
15031
15032   /* For static data members, the declaration in the class is supposed
15033      to have DW_TAG_member tag; the specification should still be
15034      DW_TAG_variable referencing the DW_TAG_member DIE.  */
15035   if (declaration && class_scope_p (context_die))
15036     var_die = new_die (DW_TAG_member, context_die, decl);
15037   else
15038     var_die = new_die (DW_TAG_variable, context_die, decl);
15039
15040   origin_die = NULL;
15041   if (origin != NULL)
15042     origin_die = add_abstract_origin_attribute (var_die, origin);
15043
15044   /* Loop unrolling can create multiple blocks that refer to the same
15045      static variable, so we must test for the DW_AT_declaration flag.
15046
15047      ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
15048      copy decls and set the DECL_ABSTRACT flag on them instead of
15049      sharing them.
15050
15051      ??? Duplicated blocks have been rewritten to use .debug_ranges.
15052
15053      ??? The declare_in_namespace support causes us to get two DIEs for one
15054      variable, both of which are declarations.  We want to avoid considering
15055      one to be a specification, so we must test that this DIE is not a
15056      declaration.  */
15057   else if (old_die && TREE_STATIC (decl) && ! declaration
15058            && get_AT_flag (old_die, DW_AT_declaration) == 1)
15059     {
15060       /* This is a definition of a C++ class level static.  */
15061       add_AT_specification (var_die, old_die);
15062       if (DECL_NAME (decl))
15063         {
15064           expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
15065           struct dwarf_file_data * file_index = lookup_filename (s.file);
15066
15067           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
15068             add_AT_file (var_die, DW_AT_decl_file, file_index);
15069
15070           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
15071             add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
15072         }
15073     }
15074   else
15075     {
15076       tree type = TREE_TYPE (decl);
15077
15078       add_name_and_src_coords_attributes (var_die, decl);
15079       if ((TREE_CODE (decl) == PARM_DECL
15080            || TREE_CODE (decl) == RESULT_DECL
15081            || TREE_CODE (decl) == VAR_DECL)
15082           && DECL_BY_REFERENCE (decl))
15083         add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
15084       else
15085         add_type_attribute (var_die, type, TREE_READONLY (decl),
15086                             TREE_THIS_VOLATILE (decl), context_die);
15087
15088       if (TREE_PUBLIC (decl))
15089         add_AT_flag (var_die, DW_AT_external, 1);
15090
15091       if (DECL_ARTIFICIAL (decl))
15092         add_AT_flag (var_die, DW_AT_artificial, 1);
15093
15094       if (TREE_PROTECTED (decl))
15095         add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_protected);
15096       else if (TREE_PRIVATE (decl))
15097         add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_private);
15098     }
15099
15100   if (declaration)
15101     add_AT_flag (var_die, DW_AT_declaration, 1);
15102
15103   if (decl && (DECL_ABSTRACT (decl) || declaration))
15104     equate_decl_number_to_die (decl, var_die);
15105
15106   if (! declaration
15107       && (! DECL_ABSTRACT (decl_or_origin)
15108           /* Local static vars are shared between all clones/inlines,
15109              so emit DW_AT_location on the abstract DIE if DECL_RTL is
15110              already set.  */
15111           || (TREE_CODE (decl_or_origin) == VAR_DECL
15112               && TREE_STATIC (decl_or_origin)
15113               && DECL_RTL_SET_P (decl_or_origin)))
15114       /* When abstract origin already has DW_AT_location attribute, no need
15115          to add it again.  */
15116       && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
15117     {
15118       if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
15119           && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
15120         defer_location (decl_or_origin, var_die);
15121       else
15122         add_location_or_const_value_attribute (var_die,
15123                                                decl_or_origin,
15124                                                DW_AT_location);
15125       add_pubname (decl_or_origin, var_die);
15126     }
15127   else
15128     tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
15129 }
15130
15131 /* Generate a DIE to represent a named constant.  */
15132
15133 static void
15134 gen_const_die (tree decl, dw_die_ref context_die)
15135 {
15136   dw_die_ref const_die;
15137   tree type = TREE_TYPE (decl);
15138
15139   const_die = new_die (DW_TAG_constant, context_die, decl);
15140   add_name_and_src_coords_attributes (const_die, decl);
15141   add_type_attribute (const_die, type, 1, 0, context_die);
15142   if (TREE_PUBLIC (decl))
15143     add_AT_flag (const_die, DW_AT_external, 1);
15144   if (DECL_ARTIFICIAL (decl))
15145     add_AT_flag (const_die, DW_AT_artificial, 1);
15146   tree_add_const_value_attribute_for_decl (const_die, decl);
15147 }
15148
15149 /* Generate a DIE to represent a label identifier.  */
15150
15151 static void
15152 gen_label_die (tree decl, dw_die_ref context_die)
15153 {
15154   tree origin = decl_ultimate_origin (decl);
15155   dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
15156   rtx insn;
15157   char label[MAX_ARTIFICIAL_LABEL_BYTES];
15158
15159   if (origin != NULL)
15160     add_abstract_origin_attribute (lbl_die, origin);
15161   else
15162     add_name_and_src_coords_attributes (lbl_die, decl);
15163
15164   if (DECL_ABSTRACT (decl))
15165     equate_decl_number_to_die (decl, lbl_die);
15166   else
15167     {
15168       insn = DECL_RTL_IF_SET (decl);
15169
15170       /* Deleted labels are programmer specified labels which have been
15171          eliminated because of various optimizations.  We still emit them
15172          here so that it is possible to put breakpoints on them.  */
15173       if (insn
15174           && (LABEL_P (insn)
15175               || ((NOTE_P (insn)
15176                    && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
15177         {
15178           /* When optimization is enabled (via -O) some parts of the compiler
15179              (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
15180              represent source-level labels which were explicitly declared by
15181              the user.  This really shouldn't be happening though, so catch
15182              it if it ever does happen.  */
15183           gcc_assert (!INSN_DELETED_P (insn));
15184
15185           ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
15186           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
15187         }
15188     }
15189 }
15190
15191 /* A helper function for gen_inlined_subroutine_die.  Add source coordinate
15192    attributes to the DIE for a block STMT, to describe where the inlined
15193    function was called from.  This is similar to add_src_coords_attributes.  */
15194
15195 static inline void
15196 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
15197 {
15198   expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
15199
15200   add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
15201   add_AT_unsigned (die, DW_AT_call_line, s.line);
15202 }
15203
15204
15205 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
15206    Add low_pc and high_pc attributes to the DIE for a block STMT.  */
15207
15208 static inline void
15209 add_high_low_attributes (tree stmt, dw_die_ref die)
15210 {
15211   char label[MAX_ARTIFICIAL_LABEL_BYTES];
15212
15213   if (BLOCK_FRAGMENT_CHAIN (stmt))
15214     {
15215       tree chain;
15216
15217       if (inlined_function_outer_scope_p (stmt))
15218         {
15219           ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
15220                                        BLOCK_NUMBER (stmt));
15221           add_AT_lbl_id (die, DW_AT_entry_pc, label);
15222         }
15223
15224       add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
15225
15226       chain = BLOCK_FRAGMENT_CHAIN (stmt);
15227       do
15228         {
15229           add_ranges (chain);
15230           chain = BLOCK_FRAGMENT_CHAIN (chain);
15231         }
15232       while (chain);
15233       add_ranges (NULL);
15234     }
15235   else
15236     {
15237       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
15238                                    BLOCK_NUMBER (stmt));
15239       add_AT_lbl_id (die, DW_AT_low_pc, label);
15240       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
15241                                    BLOCK_NUMBER (stmt));
15242       add_AT_lbl_id (die, DW_AT_high_pc, label);
15243     }
15244 }
15245
15246 /* Generate a DIE for a lexical block.  */
15247
15248 static void
15249 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
15250 {
15251   dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
15252
15253   if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
15254     add_high_low_attributes (stmt, stmt_die);
15255
15256   decls_for_scope (stmt, stmt_die, depth);
15257 }
15258
15259 /* Generate a DIE for an inlined subprogram.  */
15260
15261 static void
15262 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
15263 {
15264   tree decl;
15265
15266   /* The instance of function that is effectively being inlined shall not
15267      be abstract.  */
15268   gcc_assert (! BLOCK_ABSTRACT (stmt));
15269
15270   decl = block_ultimate_origin (stmt);
15271
15272   /* Emit info for the abstract instance first, if we haven't yet.  We
15273      must emit this even if the block is abstract, otherwise when we
15274      emit the block below (or elsewhere), we may end up trying to emit
15275      a die whose origin die hasn't been emitted, and crashing.  */
15276   dwarf2out_abstract_function (decl);
15277
15278   if (! BLOCK_ABSTRACT (stmt))
15279     {
15280       dw_die_ref subr_die
15281         = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
15282
15283       add_abstract_origin_attribute (subr_die, decl);
15284       if (TREE_ASM_WRITTEN (stmt))
15285         add_high_low_attributes (stmt, subr_die);
15286       add_call_src_coords_attributes (stmt, subr_die);
15287
15288       decls_for_scope (stmt, subr_die, depth);
15289       current_function_has_inlines = 1;
15290     }
15291 }
15292
15293 /* Generate a DIE for a field in a record, or structure.  */
15294
15295 static void
15296 gen_field_die (tree decl, dw_die_ref context_die)
15297 {
15298   dw_die_ref decl_die;
15299
15300   if (TREE_TYPE (decl) == error_mark_node)
15301     return;
15302
15303   decl_die = new_die (DW_TAG_member, context_die, decl);
15304   add_name_and_src_coords_attributes (decl_die, decl);
15305   add_type_attribute (decl_die, member_declared_type (decl),
15306                       TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
15307                       context_die);
15308
15309   if (DECL_BIT_FIELD_TYPE (decl))
15310     {
15311       add_byte_size_attribute (decl_die, decl);
15312       add_bit_size_attribute (decl_die, decl);
15313       add_bit_offset_attribute (decl_die, decl);
15314     }
15315
15316   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
15317     add_data_member_location_attribute (decl_die, decl);
15318
15319   if (DECL_ARTIFICIAL (decl))
15320     add_AT_flag (decl_die, DW_AT_artificial, 1);
15321
15322   if (TREE_PROTECTED (decl))
15323     add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_protected);
15324   else if (TREE_PRIVATE (decl))
15325     add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_private);
15326
15327   /* Equate decl number to die, so that we can look up this decl later on.  */
15328   equate_decl_number_to_die (decl, decl_die);
15329 }
15330
15331 #if 0
15332 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
15333    Use modified_type_die instead.
15334    We keep this code here just in case these types of DIEs may be needed to
15335    represent certain things in other languages (e.g. Pascal) someday.  */
15336
15337 static void
15338 gen_pointer_type_die (tree type, dw_die_ref context_die)
15339 {
15340   dw_die_ref ptr_die
15341     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
15342
15343   equate_type_number_to_die (type, ptr_die);
15344   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
15345   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
15346 }
15347
15348 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
15349    Use modified_type_die instead.
15350    We keep this code here just in case these types of DIEs may be needed to
15351    represent certain things in other languages (e.g. Pascal) someday.  */
15352
15353 static void
15354 gen_reference_type_die (tree type, dw_die_ref context_die)
15355 {
15356   dw_die_ref ref_die
15357     = new_die (DW_TAG_reference_type, scope_die_for (type, context_die), type);
15358
15359   equate_type_number_to_die (type, ref_die);
15360   add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
15361   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
15362 }
15363 #endif
15364
15365 /* Generate a DIE for a pointer to a member type.  */
15366
15367 static void
15368 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
15369 {
15370   dw_die_ref ptr_die
15371     = new_die (DW_TAG_ptr_to_member_type,
15372                scope_die_for (type, context_die), type);
15373
15374   equate_type_number_to_die (type, ptr_die);
15375   add_AT_die_ref (ptr_die, DW_AT_containing_type,
15376                   lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
15377   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
15378 }
15379
15380 /* Generate the DIE for the compilation unit.  */
15381
15382 static dw_die_ref
15383 gen_compile_unit_die (const char *filename)
15384 {
15385   dw_die_ref die;
15386   char producer[250];
15387   const char *language_string = lang_hooks.name;
15388   int language;
15389
15390   die = new_die (DW_TAG_compile_unit, NULL, NULL);
15391
15392   if (filename)
15393     {
15394       add_name_attribute (die, filename);
15395       /* Don't add cwd for <built-in>.  */
15396       if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
15397         add_comp_dir_attribute (die);
15398     }
15399
15400   sprintf (producer, "%s %s", language_string, version_string);
15401
15402 #ifdef MIPS_DEBUGGING_INFO
15403   /* The MIPS/SGI compilers place the 'cc' command line options in the producer
15404      string.  The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
15405      not appear in the producer string, the debugger reaches the conclusion
15406      that the object file is stripped and has no debugging information.
15407      To get the MIPS/SGI debugger to believe that there is debugging
15408      information in the object file, we add a -g to the producer string.  */
15409   if (debug_info_level > DINFO_LEVEL_TERSE)
15410     strcat (producer, " -g");
15411 #endif
15412
15413   add_AT_string (die, DW_AT_producer, producer);
15414
15415   if (strcmp (language_string, "GNU C++") == 0)
15416     language = DW_LANG_C_plus_plus;
15417   else if (strcmp (language_string, "GNU Ada") == 0)
15418     language = DW_LANG_Ada95;
15419   else if (strcmp (language_string, "GNU F77") == 0)
15420     language = DW_LANG_Fortran77;
15421   else if (strcmp (language_string, "GNU Fortran") == 0)
15422     language = DW_LANG_Fortran95;
15423   else if (strcmp (language_string, "GNU Pascal") == 0)
15424     language = DW_LANG_Pascal83;
15425   else if (strcmp (language_string, "GNU Java") == 0)
15426     language = DW_LANG_Java;
15427   else if (strcmp (language_string, "GNU Objective-C") == 0)
15428     language = DW_LANG_ObjC;
15429   else if (strcmp (language_string, "GNU Objective-C++") == 0)
15430     language = DW_LANG_ObjC_plus_plus;
15431   else
15432     language = DW_LANG_C89;
15433
15434   add_AT_unsigned (die, DW_AT_language, language);
15435   return die;
15436 }
15437
15438 /* Generate the DIE for a base class.  */
15439
15440 static void
15441 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
15442 {
15443   dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
15444
15445   add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
15446   add_data_member_location_attribute (die, binfo);
15447
15448   if (BINFO_VIRTUAL_P (binfo))
15449     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
15450
15451   if (access == access_public_node)
15452     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
15453   else if (access == access_protected_node)
15454     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
15455 }
15456
15457 /* Generate a DIE for a class member.  */
15458
15459 static void
15460 gen_member_die (tree type, dw_die_ref context_die)
15461 {
15462   tree member;
15463   tree binfo = TYPE_BINFO (type);
15464   dw_die_ref child;
15465
15466   /* If this is not an incomplete type, output descriptions of each of its
15467      members. Note that as we output the DIEs necessary to represent the
15468      members of this record or union type, we will also be trying to output
15469      DIEs to represent the *types* of those members. However the `type'
15470      function (above) will specifically avoid generating type DIEs for member
15471      types *within* the list of member DIEs for this (containing) type except
15472      for those types (of members) which are explicitly marked as also being
15473      members of this (containing) type themselves.  The g++ front- end can
15474      force any given type to be treated as a member of some other (containing)
15475      type by setting the TYPE_CONTEXT of the given (member) type to point to
15476      the TREE node representing the appropriate (containing) type.  */
15477
15478   /* First output info about the base classes.  */
15479   if (binfo)
15480     {
15481       VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
15482       int i;
15483       tree base;
15484
15485       for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
15486         gen_inheritance_die (base,
15487                              (accesses ? VEC_index (tree, accesses, i)
15488                               : access_public_node), context_die);
15489     }
15490
15491   /* Now output info about the data members and type members.  */
15492   for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member))
15493     {
15494       /* If we thought we were generating minimal debug info for TYPE
15495          and then changed our minds, some of the member declarations
15496          may have already been defined.  Don't define them again, but
15497          do put them in the right order.  */
15498
15499       child = lookup_decl_die (member);
15500       if (child)
15501         splice_child_die (context_die, child);
15502       else
15503         gen_decl_die (member, NULL, context_die);
15504     }
15505
15506   /* Now output info about the function members (if any).  */
15507   for (member = TYPE_METHODS (type); member; member = TREE_CHAIN (member))
15508     {
15509       /* Don't include clones in the member list.  */
15510       if (DECL_ABSTRACT_ORIGIN (member))
15511         continue;
15512
15513       child = lookup_decl_die (member);
15514       if (child)
15515         splice_child_die (context_die, child);
15516       else
15517         gen_decl_die (member, NULL, context_die);
15518     }
15519 }
15520
15521 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
15522    is set, we pretend that the type was never defined, so we only get the
15523    member DIEs needed by later specification DIEs.  */
15524
15525 static void
15526 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
15527                                 enum debug_info_usage usage)
15528 {
15529   dw_die_ref type_die = lookup_type_die (type);
15530   dw_die_ref scope_die = 0;
15531   int nested = 0;
15532   int complete = (TYPE_SIZE (type)
15533                   && (! TYPE_STUB_DECL (type)
15534                       || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
15535   int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
15536   complete = complete && should_emit_struct_debug (type, usage);
15537
15538   if (type_die && ! complete)
15539     return;
15540
15541   if (TYPE_CONTEXT (type) != NULL_TREE
15542       && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
15543           || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
15544     nested = 1;
15545
15546   scope_die = scope_die_for (type, context_die);
15547
15548   if (! type_die || (nested && scope_die == comp_unit_die))
15549     /* First occurrence of type or toplevel definition of nested class.  */
15550     {
15551       dw_die_ref old_die = type_die;
15552
15553       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
15554                           ? record_type_tag (type) : DW_TAG_union_type,
15555                           scope_die, type);
15556       equate_type_number_to_die (type, type_die);
15557       if (old_die)
15558         add_AT_specification (type_die, old_die);
15559       else
15560         add_name_attribute (type_die, type_tag (type));
15561     }
15562   else
15563     remove_AT (type_die, DW_AT_declaration);
15564
15565   /* Generate child dies for template paramaters.  */
15566   if (debug_info_level > DINFO_LEVEL_TERSE
15567       && COMPLETE_TYPE_P (type))
15568     gen_generic_params_dies (type);
15569
15570   /* If this type has been completed, then give it a byte_size attribute and
15571      then give a list of members.  */
15572   if (complete && !ns_decl)
15573     {
15574       /* Prevent infinite recursion in cases where the type of some member of
15575          this type is expressed in terms of this type itself.  */
15576       TREE_ASM_WRITTEN (type) = 1;
15577       add_byte_size_attribute (type_die, type);
15578       if (TYPE_STUB_DECL (type) != NULL_TREE)
15579         add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
15580
15581       /* If the first reference to this type was as the return type of an
15582          inline function, then it may not have a parent.  Fix this now.  */
15583       if (type_die->die_parent == NULL)
15584         add_child_die (scope_die, type_die);
15585
15586       push_decl_scope (type);
15587       gen_member_die (type, type_die);
15588       pop_decl_scope ();
15589
15590       /* GNU extension: Record what type our vtable lives in.  */
15591       if (TYPE_VFIELD (type))
15592         {
15593           tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
15594
15595           gen_type_die (vtype, context_die);
15596           add_AT_die_ref (type_die, DW_AT_containing_type,
15597                           lookup_type_die (vtype));
15598         }
15599     }
15600   else
15601     {
15602       add_AT_flag (type_die, DW_AT_declaration, 1);
15603
15604       /* We don't need to do this for function-local types.  */
15605       if (TYPE_STUB_DECL (type)
15606           && ! decl_function_context (TYPE_STUB_DECL (type)))
15607         VEC_safe_push (tree, gc, incomplete_types, type);
15608     }
15609
15610   if (get_AT (type_die, DW_AT_name))
15611     add_pubtype (type, type_die);
15612 }
15613
15614 /* Generate a DIE for a subroutine _type_.  */
15615
15616 static void
15617 gen_subroutine_type_die (tree type, dw_die_ref context_die)
15618 {
15619   tree return_type = TREE_TYPE (type);
15620   dw_die_ref subr_die
15621     = new_die (DW_TAG_subroutine_type,
15622                scope_die_for (type, context_die), type);
15623
15624   equate_type_number_to_die (type, subr_die);
15625   add_prototyped_attribute (subr_die, type);
15626   add_type_attribute (subr_die, return_type, 0, 0, context_die);
15627   gen_formal_types_die (type, subr_die);
15628
15629   if (get_AT (subr_die, DW_AT_name))
15630     add_pubtype (type, subr_die);
15631 }
15632
15633 /* Generate a DIE for a type definition.  */
15634
15635 static void
15636 gen_typedef_die (tree decl, dw_die_ref context_die)
15637 {
15638   dw_die_ref type_die;
15639   tree origin;
15640
15641   if (TREE_ASM_WRITTEN (decl))
15642     return;
15643
15644   TREE_ASM_WRITTEN (decl) = 1;
15645   type_die = new_die (DW_TAG_typedef, context_die, decl);
15646   origin = decl_ultimate_origin (decl);
15647   if (origin != NULL)
15648     add_abstract_origin_attribute (type_die, origin);
15649   else
15650     {
15651       tree type;
15652
15653       add_name_and_src_coords_attributes (type_die, decl);
15654       if (DECL_ORIGINAL_TYPE (decl))
15655         {
15656           type = DECL_ORIGINAL_TYPE (decl);
15657
15658           gcc_assert (type != TREE_TYPE (decl));
15659           equate_type_number_to_die (TREE_TYPE (decl), type_die);
15660         }
15661       else
15662         type = TREE_TYPE (decl);
15663
15664       add_type_attribute (type_die, type, TREE_READONLY (decl),
15665                           TREE_THIS_VOLATILE (decl), context_die);
15666     }
15667
15668   if (DECL_ABSTRACT (decl))
15669     equate_decl_number_to_die (decl, type_die);
15670
15671   if (get_AT (type_die, DW_AT_name))
15672     add_pubtype (decl, type_die);
15673 }
15674
15675 /* Generate a type description DIE.  */
15676
15677 static void
15678 gen_type_die_with_usage (tree type, dw_die_ref context_die,
15679                                 enum debug_info_usage usage)
15680 {
15681   int need_pop;
15682   struct array_descr_info info;
15683
15684   if (type == NULL_TREE || type == error_mark_node)
15685     return;
15686
15687   /* If TYPE is a typedef type variant, let's generate debug info
15688      for the parent typedef which TYPE is a type of.  */
15689   if (TYPE_NAME (type) && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
15690       && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
15691     {
15692       if (TREE_ASM_WRITTEN (type))
15693         return;
15694
15695       /* Prevent broken recursion; we can't hand off to the same type.  */
15696       gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
15697
15698       /* Use the DIE of the containing namespace as the parent DIE of
15699          the type description DIE we want to generate.  */
15700       if (DECL_CONTEXT (TYPE_NAME (type))
15701           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
15702         context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
15703
15704       TREE_ASM_WRITTEN (type) = 1;
15705       gen_decl_die (TYPE_NAME (type), NULL, context_die);
15706       return;
15707     }
15708
15709   /* If this is an array type with hidden descriptor, handle it first.  */
15710   if (!TREE_ASM_WRITTEN (type)
15711       && lang_hooks.types.get_array_descr_info
15712       && lang_hooks.types.get_array_descr_info (type, &info))
15713     {
15714       gen_descr_array_type_die (type, &info, context_die);
15715       TREE_ASM_WRITTEN (type) = 1;
15716       return;
15717     }
15718
15719   /* We are going to output a DIE to represent the unqualified version
15720      of this type (i.e. without any const or volatile qualifiers) so
15721      get the main variant (i.e. the unqualified version) of this type
15722      now.  (Vectors are special because the debugging info is in the
15723      cloned type itself).  */
15724   if (TREE_CODE (type) != VECTOR_TYPE)
15725     type = type_main_variant (type);
15726
15727   if (TREE_ASM_WRITTEN (type))
15728     return;
15729
15730   switch (TREE_CODE (type))
15731     {
15732     case ERROR_MARK:
15733       break;
15734
15735     case POINTER_TYPE:
15736     case REFERENCE_TYPE:
15737       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
15738          ensures that the gen_type_die recursion will terminate even if the
15739          type is recursive.  Recursive types are possible in Ada.  */
15740       /* ??? We could perhaps do this for all types before the switch
15741          statement.  */
15742       TREE_ASM_WRITTEN (type) = 1;
15743
15744       /* For these types, all that is required is that we output a DIE (or a
15745          set of DIEs) to represent the "basis" type.  */
15746       gen_type_die_with_usage (TREE_TYPE (type), context_die,
15747                                 DINFO_USAGE_IND_USE);
15748       break;
15749
15750     case OFFSET_TYPE:
15751       /* This code is used for C++ pointer-to-data-member types.
15752          Output a description of the relevant class type.  */
15753       gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
15754                                         DINFO_USAGE_IND_USE);
15755
15756       /* Output a description of the type of the object pointed to.  */
15757       gen_type_die_with_usage (TREE_TYPE (type), context_die,
15758                                         DINFO_USAGE_IND_USE);
15759
15760       /* Now output a DIE to represent this pointer-to-data-member type
15761          itself.  */
15762       gen_ptr_to_mbr_type_die (type, context_die);
15763       break;
15764
15765     case FUNCTION_TYPE:
15766       /* Force out return type (in case it wasn't forced out already).  */
15767       gen_type_die_with_usage (TREE_TYPE (type), context_die,
15768                                         DINFO_USAGE_DIR_USE);
15769       gen_subroutine_type_die (type, context_die);
15770       break;
15771
15772     case METHOD_TYPE:
15773       /* Force out return type (in case it wasn't forced out already).  */
15774       gen_type_die_with_usage (TREE_TYPE (type), context_die,
15775                                         DINFO_USAGE_DIR_USE);
15776       gen_subroutine_type_die (type, context_die);
15777       break;
15778
15779     case ARRAY_TYPE:
15780       gen_array_type_die (type, context_die);
15781       break;
15782
15783     case VECTOR_TYPE:
15784       gen_array_type_die (type, context_die);
15785       break;
15786
15787     case ENUMERAL_TYPE:
15788     case RECORD_TYPE:
15789     case UNION_TYPE:
15790     case QUAL_UNION_TYPE:
15791       /* If this is a nested type whose containing class hasn't been written
15792          out yet, writing it out will cover this one, too.  This does not apply
15793          to instantiations of member class templates; they need to be added to
15794          the containing class as they are generated.  FIXME: This hurts the
15795          idea of combining type decls from multiple TUs, since we can't predict
15796          what set of template instantiations we'll get.  */
15797       if (TYPE_CONTEXT (type)
15798           && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
15799           && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
15800         {
15801           gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
15802
15803           if (TREE_ASM_WRITTEN (type))
15804             return;
15805
15806           /* If that failed, attach ourselves to the stub.  */
15807           push_decl_scope (TYPE_CONTEXT (type));
15808           context_die = lookup_type_die (TYPE_CONTEXT (type));
15809           need_pop = 1;
15810         }
15811       else if (TYPE_CONTEXT (type) != NULL_TREE
15812                && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
15813         {
15814           /* If this type is local to a function that hasn't been written
15815              out yet, use a NULL context for now; it will be fixed up in
15816              decls_for_scope.  */
15817           context_die = lookup_decl_die (TYPE_CONTEXT (type));
15818           need_pop = 0;
15819         }
15820       else
15821         {
15822           context_die = declare_in_namespace (type, context_die);
15823           need_pop = 0;
15824         }
15825
15826       if (TREE_CODE (type) == ENUMERAL_TYPE)
15827         {
15828           /* This might have been written out by the call to
15829              declare_in_namespace.  */
15830           if (!TREE_ASM_WRITTEN (type))
15831             gen_enumeration_type_die (type, context_die);
15832         }
15833       else
15834         gen_struct_or_union_type_die (type, context_die, usage);
15835
15836       if (need_pop)
15837         pop_decl_scope ();
15838
15839       /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
15840          it up if it is ever completed.  gen_*_type_die will set it for us
15841          when appropriate.  */
15842       return;
15843
15844     case VOID_TYPE:
15845     case INTEGER_TYPE:
15846     case REAL_TYPE:
15847     case FIXED_POINT_TYPE:
15848     case COMPLEX_TYPE:
15849     case BOOLEAN_TYPE:
15850       /* No DIEs needed for fundamental types.  */
15851       break;
15852
15853     case LANG_TYPE:
15854       /* No Dwarf representation currently defined.  */
15855       break;
15856
15857     default:
15858       gcc_unreachable ();
15859     }
15860
15861   TREE_ASM_WRITTEN (type) = 1;
15862 }
15863
15864 static void
15865 gen_type_die (tree type, dw_die_ref context_die)
15866 {
15867   gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
15868 }
15869
15870 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
15871    things which are local to the given block.  */
15872
15873 static void
15874 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
15875 {
15876   int must_output_die = 0;
15877   bool inlined_func;
15878
15879   /* Ignore blocks that are NULL.  */
15880   if (stmt == NULL_TREE)
15881     return;
15882
15883   inlined_func = inlined_function_outer_scope_p (stmt);
15884
15885   /* If the block is one fragment of a non-contiguous block, do not
15886      process the variables, since they will have been done by the
15887      origin block.  Do process subblocks.  */
15888   if (BLOCK_FRAGMENT_ORIGIN (stmt))
15889     {
15890       tree sub;
15891
15892       for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
15893         gen_block_die (sub, context_die, depth + 1);
15894
15895       return;
15896     }
15897
15898   /* Determine if we need to output any Dwarf DIEs at all to represent this
15899      block.  */
15900   if (inlined_func)
15901     /* The outer scopes for inlinings *must* always be represented.  We
15902        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
15903     must_output_die = 1;
15904   else
15905     {
15906       /* Determine if this block directly contains any "significant"
15907          local declarations which we will need to output DIEs for.  */
15908       if (debug_info_level > DINFO_LEVEL_TERSE)
15909         /* We are not in terse mode so *any* local declaration counts
15910            as being a "significant" one.  */
15911         must_output_die = ((BLOCK_VARS (stmt) != NULL
15912                             || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
15913                            && (TREE_USED (stmt)
15914                                || TREE_ASM_WRITTEN (stmt)
15915                                || BLOCK_ABSTRACT (stmt)));
15916       else if ((TREE_USED (stmt)
15917                 || TREE_ASM_WRITTEN (stmt)
15918                 || BLOCK_ABSTRACT (stmt))
15919                && !dwarf2out_ignore_block (stmt))
15920         must_output_die = 1;
15921     }
15922
15923   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
15924      DIE for any block which contains no significant local declarations at
15925      all.  Rather, in such cases we just call `decls_for_scope' so that any
15926      needed Dwarf info for any sub-blocks will get properly generated. Note
15927      that in terse mode, our definition of what constitutes a "significant"
15928      local declaration gets restricted to include only inlined function
15929      instances and local (nested) function definitions.  */
15930   if (must_output_die)
15931     {
15932       if (inlined_func)
15933         {
15934           /* If STMT block is abstract, that means we have been called
15935              indirectly from dwarf2out_abstract_function.
15936              That function rightfully marks the descendent blocks (of
15937              the abstract function it is dealing with) as being abstract,
15938              precisely to prevent us from emitting any
15939              DW_TAG_inlined_subroutine DIE as a descendent
15940              of an abstract function instance. So in that case, we should
15941              not call gen_inlined_subroutine_die.
15942
15943              Later though, when cgraph asks dwarf2out to emit info
15944              for the concrete instance of the function decl into which
15945              the concrete instance of STMT got inlined, the later will lead
15946              to the generation of a DW_TAG_inlined_subroutine DIE.  */
15947           if (! BLOCK_ABSTRACT (stmt))
15948             gen_inlined_subroutine_die (stmt, context_die, depth);
15949         }
15950       else
15951         gen_lexical_block_die (stmt, context_die, depth);
15952     }
15953   else
15954     decls_for_scope (stmt, context_die, depth);
15955 }
15956
15957 /* Process variable DECL (or variable with origin ORIGIN) within
15958    block STMT and add it to CONTEXT_DIE.  */
15959 static void
15960 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
15961 {
15962   dw_die_ref die;
15963   tree decl_or_origin = decl ? decl : origin;
15964   tree ultimate_origin = origin ? decl_ultimate_origin (origin) : NULL;
15965
15966   if (ultimate_origin)
15967     origin = ultimate_origin;
15968
15969   if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
15970     die = lookup_decl_die (decl_or_origin);
15971   else if (TREE_CODE (decl_or_origin) == TYPE_DECL
15972            && TYPE_DECL_IS_STUB (decl_or_origin))
15973     die = lookup_type_die (TREE_TYPE (decl_or_origin));
15974   else
15975     die = NULL;
15976
15977   if (die != NULL && die->die_parent == NULL)
15978     add_child_die (context_die, die);
15979   else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
15980     dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
15981                                          stmt, context_die);
15982   else
15983     gen_decl_die (decl, origin, context_die);
15984 }
15985
15986 /* Generate all of the decls declared within a given scope and (recursively)
15987    all of its sub-blocks.  */
15988
15989 static void
15990 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
15991 {
15992   tree decl;
15993   unsigned int i;
15994   tree subblocks;
15995
15996   /* Ignore NULL blocks.  */
15997   if (stmt == NULL_TREE)
15998     return;
15999
16000   /* Output the DIEs to represent all of the data objects and typedefs
16001      declared directly within this block but not within any nested
16002      sub-blocks.  Also, nested function and tag DIEs have been
16003      generated with a parent of NULL; fix that up now.  */
16004   for (decl = BLOCK_VARS (stmt); decl != NULL; decl = TREE_CHAIN (decl))
16005     process_scope_var (stmt, decl, NULL_TREE, context_die);
16006   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
16007     process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
16008                        context_die);
16009
16010   /* If we're at -g1, we're not interested in subblocks.  */
16011   if (debug_info_level <= DINFO_LEVEL_TERSE)
16012     return;
16013
16014   /* Output the DIEs to represent all sub-blocks (and the items declared
16015      therein) of this block.  */
16016   for (subblocks = BLOCK_SUBBLOCKS (stmt);
16017        subblocks != NULL;
16018        subblocks = BLOCK_CHAIN (subblocks))
16019     gen_block_die (subblocks, context_die, depth + 1);
16020 }
16021
16022 /* Is this a typedef we can avoid emitting?  */
16023
16024 static inline int
16025 is_redundant_typedef (const_tree decl)
16026 {
16027   if (TYPE_DECL_IS_STUB (decl))
16028     return 1;
16029
16030   if (DECL_ARTIFICIAL (decl)
16031       && DECL_CONTEXT (decl)
16032       && is_tagged_type (DECL_CONTEXT (decl))
16033       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
16034       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
16035     /* Also ignore the artificial member typedef for the class name.  */
16036     return 1;
16037
16038   return 0;
16039 }
16040
16041 /* Returns the DIE for a context.  */
16042
16043 static inline dw_die_ref
16044 get_context_die (tree context)
16045 {
16046   if (context)
16047     {
16048       /* Find die that represents this context.  */
16049       if (TYPE_P (context))
16050         return force_type_die (context);
16051       else
16052         return force_decl_die (context);
16053     }
16054   return comp_unit_die;
16055 }
16056
16057 /* Returns the DIE for decl.  A DIE will always be returned.  */
16058
16059 static dw_die_ref
16060 force_decl_die (tree decl)
16061 {
16062   dw_die_ref decl_die;
16063   unsigned saved_external_flag;
16064   tree save_fn = NULL_TREE;
16065   decl_die = lookup_decl_die (decl);
16066   if (!decl_die)
16067     {
16068       dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
16069
16070       decl_die = lookup_decl_die (decl);
16071       if (decl_die)
16072         return decl_die;
16073
16074       switch (TREE_CODE (decl))
16075         {
16076         case FUNCTION_DECL:
16077           /* Clear current_function_decl, so that gen_subprogram_die thinks
16078              that this is a declaration. At this point, we just want to force
16079              declaration die.  */
16080           save_fn = current_function_decl;
16081           current_function_decl = NULL_TREE;
16082           gen_subprogram_die (decl, context_die);
16083           current_function_decl = save_fn;
16084           break;
16085
16086         case VAR_DECL:
16087           /* Set external flag to force declaration die. Restore it after
16088            gen_decl_die() call.  */
16089           saved_external_flag = DECL_EXTERNAL (decl);
16090           DECL_EXTERNAL (decl) = 1;
16091           gen_decl_die (decl, NULL, context_die);
16092           DECL_EXTERNAL (decl) = saved_external_flag;
16093           break;
16094
16095         case NAMESPACE_DECL:
16096           dwarf2out_decl (decl);
16097           break;
16098
16099         default:
16100           gcc_unreachable ();
16101         }
16102
16103       /* We should be able to find the DIE now.  */
16104       if (!decl_die)
16105         decl_die = lookup_decl_die (decl);
16106       gcc_assert (decl_die);
16107     }
16108
16109   return decl_die;
16110 }
16111
16112 /* Returns the DIE for TYPE, that must not be a base type.  A DIE is
16113    always returned.  */
16114
16115 static dw_die_ref
16116 force_type_die (tree type)
16117 {
16118   dw_die_ref type_die;
16119
16120   type_die = lookup_type_die (type);
16121   if (!type_die)
16122     {
16123       dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
16124
16125       type_die = modified_type_die (type, TYPE_READONLY (type),
16126                                     TYPE_VOLATILE (type), context_die);
16127       gcc_assert (type_die);
16128     }
16129   return type_die;
16130 }
16131
16132 /* Force out any required namespaces to be able to output DECL,
16133    and return the new context_die for it, if it's changed.  */
16134
16135 static dw_die_ref
16136 setup_namespace_context (tree thing, dw_die_ref context_die)
16137 {
16138   tree context = (DECL_P (thing)
16139                   ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
16140   if (context && TREE_CODE (context) == NAMESPACE_DECL)
16141     /* Force out the namespace.  */
16142     context_die = force_decl_die (context);
16143
16144   return context_die;
16145 }
16146
16147 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
16148    type) within its namespace, if appropriate.
16149
16150    For compatibility with older debuggers, namespace DIEs only contain
16151    declarations; all definitions are emitted at CU scope.  */
16152
16153 static dw_die_ref
16154 declare_in_namespace (tree thing, dw_die_ref context_die)
16155 {
16156   dw_die_ref ns_context;
16157
16158   if (debug_info_level <= DINFO_LEVEL_TERSE)
16159     return context_die;
16160
16161   /* If this decl is from an inlined function, then don't try to emit it in its
16162      namespace, as we will get confused.  It would have already been emitted
16163      when the abstract instance of the inline function was emitted anyways.  */
16164   if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
16165     return context_die;
16166
16167   ns_context = setup_namespace_context (thing, context_die);
16168
16169   if (ns_context != context_die)
16170     {
16171       if (is_fortran ())
16172         return ns_context;
16173       if (DECL_P (thing))
16174         gen_decl_die (thing, NULL, ns_context);
16175       else
16176         gen_type_die (thing, ns_context);
16177     }
16178   return context_die;
16179 }
16180
16181 /* Generate a DIE for a namespace or namespace alias.  */
16182
16183 static void
16184 gen_namespace_die (tree decl, dw_die_ref context_die)
16185 {
16186   dw_die_ref namespace_die;
16187
16188   /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
16189      they are an alias of.  */
16190   if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
16191     {
16192       /* Output a real namespace or module.  */
16193       context_die = setup_namespace_context (decl, comp_unit_die);
16194       namespace_die = new_die (is_fortran ()
16195                                ? DW_TAG_module : DW_TAG_namespace,
16196                                context_die, decl);
16197       /* For Fortran modules defined in different CU don't add src coords.  */
16198       if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
16199         add_name_attribute (namespace_die, dwarf2_name (decl, 0));
16200       else
16201         add_name_and_src_coords_attributes (namespace_die, decl);
16202       if (DECL_EXTERNAL (decl))
16203         add_AT_flag (namespace_die, DW_AT_declaration, 1);
16204       equate_decl_number_to_die (decl, namespace_die);
16205     }
16206   else
16207     {
16208       /* Output a namespace alias.  */
16209
16210       /* Force out the namespace we are an alias of, if necessary.  */
16211       dw_die_ref origin_die
16212         = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
16213
16214       if (DECL_CONTEXT (decl) == NULL_TREE
16215           || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
16216         context_die = setup_namespace_context (decl, comp_unit_die);
16217       /* Now create the namespace alias DIE.  */
16218       namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
16219       add_name_and_src_coords_attributes (namespace_die, decl);
16220       add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
16221       equate_decl_number_to_die (decl, namespace_die);
16222     }
16223 }
16224
16225 /* Generate Dwarf debug information for a decl described by DECL.  */
16226
16227 static void
16228 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
16229 {
16230   tree decl_or_origin = decl ? decl : origin;
16231   tree class_origin = NULL;
16232
16233   if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
16234     return;
16235
16236   switch (TREE_CODE (decl_or_origin))
16237     {
16238     case ERROR_MARK:
16239       break;
16240
16241     case CONST_DECL:
16242       if (!is_fortran ())
16243         {
16244           /* The individual enumerators of an enum type get output when we output
16245              the Dwarf representation of the relevant enum type itself.  */
16246           break;
16247         }
16248
16249       /* Emit its type.  */
16250       gen_type_die (TREE_TYPE (decl), context_die);
16251
16252       /* And its containing namespace.  */
16253       context_die = declare_in_namespace (decl, context_die);
16254
16255       gen_const_die (decl, context_die);
16256       break;
16257
16258     case FUNCTION_DECL:
16259       /* Don't output any DIEs to represent mere function declarations,
16260          unless they are class members or explicit block externs.  */
16261       if (DECL_INITIAL (decl_or_origin) == NULL_TREE
16262           && DECL_CONTEXT (decl_or_origin) == NULL_TREE
16263           && (current_function_decl == NULL_TREE
16264               || DECL_ARTIFICIAL (decl_or_origin)))
16265         break;
16266
16267 #if 0
16268       /* FIXME */
16269       /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
16270          on local redeclarations of global functions.  That seems broken.  */
16271       if (current_function_decl != decl)
16272         /* This is only a declaration.  */;
16273 #endif
16274
16275       /* If we're emitting a clone, emit info for the abstract instance.  */
16276       if (origin || DECL_ORIGIN (decl) != decl)
16277         dwarf2out_abstract_function (origin ? origin : DECL_ABSTRACT_ORIGIN (decl));
16278
16279       /* If we're emitting an out-of-line copy of an inline function,
16280          emit info for the abstract instance and set up to refer to it.  */
16281       else if (cgraph_function_possibly_inlined_p (decl)
16282                && ! DECL_ABSTRACT (decl)
16283                && ! class_or_namespace_scope_p (context_die)
16284                /* dwarf2out_abstract_function won't emit a die if this is just
16285                   a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
16286                   that case, because that works only if we have a die.  */
16287                && DECL_INITIAL (decl) != NULL_TREE)
16288         {
16289           dwarf2out_abstract_function (decl);
16290           set_decl_origin_self (decl);
16291         }
16292
16293       /* Otherwise we're emitting the primary DIE for this decl.  */
16294       else if (debug_info_level > DINFO_LEVEL_TERSE)
16295         {
16296           /* Before we describe the FUNCTION_DECL itself, make sure that we
16297              have described its return type.  */
16298           gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
16299
16300           /* And its virtual context.  */
16301           if (DECL_VINDEX (decl) != NULL_TREE)
16302             gen_type_die (DECL_CONTEXT (decl), context_die);
16303
16304           /* And its containing type.  */
16305           if (!origin)
16306             origin = decl_class_context (decl);
16307           if (origin != NULL_TREE)
16308             gen_type_die_for_member (origin, decl, context_die);
16309
16310           /* And its containing namespace.  */
16311           context_die = declare_in_namespace (decl, context_die);
16312         }
16313
16314       /* Now output a DIE to represent the function itself.  */
16315       if (decl)
16316         gen_subprogram_die (decl, context_die);
16317       break;
16318
16319     case TYPE_DECL:
16320       /* If we are in terse mode, don't generate any DIEs to represent any
16321          actual typedefs.  */
16322       if (debug_info_level <= DINFO_LEVEL_TERSE)
16323         break;
16324
16325       /* In the special case of a TYPE_DECL node representing the declaration
16326          of some type tag, if the given TYPE_DECL is marked as having been
16327          instantiated from some other (original) TYPE_DECL node (e.g. one which
16328          was generated within the original definition of an inline function) we
16329          used to generate a special (abbreviated) DW_TAG_structure_type,
16330          DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  But nothing
16331          should be actually referencing those DIEs, as variable DIEs with that
16332          type would be emitted already in the abstract origin, so it was always
16333          removed during unused type prunning.  Don't add anything in this
16334          case.  */
16335       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
16336         break;
16337
16338       if (is_redundant_typedef (decl))
16339         gen_type_die (TREE_TYPE (decl), context_die);
16340       else
16341         /* Output a DIE to represent the typedef itself.  */
16342         gen_typedef_die (decl, context_die);
16343       break;
16344
16345     case LABEL_DECL:
16346       if (debug_info_level >= DINFO_LEVEL_NORMAL)
16347         gen_label_die (decl, context_die);
16348       break;
16349
16350     case VAR_DECL:
16351     case RESULT_DECL:
16352       /* If we are in terse mode, don't generate any DIEs to represent any
16353          variable declarations or definitions.  */
16354       if (debug_info_level <= DINFO_LEVEL_TERSE)
16355         break;
16356
16357       /* Output any DIEs that are needed to specify the type of this data
16358          object.  */
16359       if ((TREE_CODE (decl_or_origin) == RESULT_DECL
16360            || TREE_CODE (decl_or_origin) == VAR_DECL)
16361           && DECL_BY_REFERENCE (decl_or_origin))
16362         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
16363       else
16364         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
16365
16366       /* And its containing type.  */
16367       class_origin = decl_class_context (decl_or_origin);
16368       if (class_origin != NULL_TREE)
16369         gen_type_die_for_member (class_origin, decl_or_origin, context_die);
16370
16371       /* And its containing namespace.  */
16372       context_die = declare_in_namespace (decl_or_origin, context_die);
16373
16374       /* Now output the DIE to represent the data object itself.  This gets
16375          complicated because of the possibility that the VAR_DECL really
16376          represents an inlined instance of a formal parameter for an inline
16377          function.  */
16378       if (!origin)
16379         origin = decl_ultimate_origin (decl);
16380       if (origin != NULL_TREE && TREE_CODE (origin) == PARM_DECL)
16381         gen_formal_parameter_die (decl, origin, context_die);
16382       else
16383         gen_variable_die (decl, origin, context_die);
16384       break;
16385
16386     case FIELD_DECL:
16387       /* Ignore the nameless fields that are used to skip bits but handle C++
16388          anonymous unions and structs.  */
16389       if (DECL_NAME (decl) != NULL_TREE
16390           || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
16391           || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
16392         {
16393           gen_type_die (member_declared_type (decl), context_die);
16394           gen_field_die (decl, context_die);
16395         }
16396       break;
16397
16398     case PARM_DECL:
16399       if (DECL_BY_REFERENCE (decl_or_origin))
16400         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
16401       else
16402         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
16403       gen_formal_parameter_die (decl, origin, context_die);
16404       break;
16405
16406     case NAMESPACE_DECL:
16407     case IMPORTED_DECL:
16408       gen_namespace_die (decl, context_die);
16409       break;
16410
16411     default:
16412       /* Probably some frontend-internal decl.  Assume we don't care.  */
16413       gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
16414       break;
16415     }
16416 }
16417 \f
16418 /* Output debug information for global decl DECL.  Called from toplev.c after
16419    compilation proper has finished.  */
16420
16421 static void
16422 dwarf2out_global_decl (tree decl)
16423 {
16424   /* Output DWARF2 information for file-scope tentative data object
16425      declarations, file-scope (extern) function declarations (which
16426      had no corresponding body) and file-scope tagged type declarations
16427      and definitions which have not yet been forced out.  */
16428   if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
16429     dwarf2out_decl (decl);
16430 }
16431
16432 /* Output debug information for type decl DECL.  Called from toplev.c
16433    and from language front ends (to record built-in types).  */
16434 static void
16435 dwarf2out_type_decl (tree decl, int local)
16436 {
16437   if (!local)
16438     dwarf2out_decl (decl);
16439 }
16440
16441 /* Output debug information for imported module or decl DECL.
16442    NAME is non-NULL name in the lexical block if the decl has been renamed.
16443    LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
16444    that DECL belongs to.
16445    LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK.  */
16446 static void
16447 dwarf2out_imported_module_or_decl_1 (tree decl,
16448                                      tree name,
16449                                      tree lexical_block,
16450                                      dw_die_ref lexical_block_die)
16451 {
16452   expanded_location xloc;
16453   dw_die_ref imported_die = NULL;
16454   dw_die_ref at_import_die;
16455
16456   if (TREE_CODE (decl) == IMPORTED_DECL)
16457     {
16458       xloc = expand_location (DECL_SOURCE_LOCATION (decl));
16459       decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
16460       gcc_assert (decl);
16461     }
16462   else
16463     xloc = expand_location (input_location);
16464
16465   if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
16466     {
16467       if (is_base_type (TREE_TYPE (decl)))
16468         at_import_die = base_type_die (TREE_TYPE (decl));
16469       else
16470         at_import_die = force_type_die (TREE_TYPE (decl));
16471       /* For namespace N { typedef void T; } using N::T; base_type_die
16472          returns NULL, but DW_TAG_imported_declaration requires
16473          the DW_AT_import tag.  Force creation of DW_TAG_typedef.  */
16474       if (!at_import_die)
16475         {
16476           gcc_assert (TREE_CODE (decl) == TYPE_DECL);
16477           gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
16478           at_import_die = lookup_type_die (TREE_TYPE (decl));
16479           gcc_assert (at_import_die);
16480         }
16481     }
16482   else
16483     {
16484       at_import_die = lookup_decl_die (decl);
16485       if (!at_import_die)
16486         {
16487           /* If we're trying to avoid duplicate debug info, we may not have
16488              emitted the member decl for this field.  Emit it now.  */
16489           if (TREE_CODE (decl) == FIELD_DECL)
16490             {
16491               tree type = DECL_CONTEXT (decl);
16492
16493               if (TYPE_CONTEXT (type)
16494                   && TYPE_P (TYPE_CONTEXT (type))
16495                   && !should_emit_struct_debug (TYPE_CONTEXT (type),
16496                                                 DINFO_USAGE_DIR_USE))
16497                 return;
16498               gen_type_die_for_member (type, decl,
16499                                        get_context_die (TYPE_CONTEXT (type)));
16500             }
16501           at_import_die = force_decl_die (decl);
16502         }
16503     }
16504
16505   if (TREE_CODE (decl) == NAMESPACE_DECL)
16506     imported_die = new_die (DW_TAG_imported_module,
16507                             lexical_block_die,
16508                             lexical_block);
16509   else
16510     imported_die = new_die (DW_TAG_imported_declaration,
16511                             lexical_block_die,
16512                             lexical_block);
16513
16514   add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
16515   add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
16516   if (name)
16517     add_AT_string (imported_die, DW_AT_name,
16518                    IDENTIFIER_POINTER (name));
16519   add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
16520 }
16521
16522 /* Output debug information for imported module or decl DECL.
16523    NAME is non-NULL name in context if the decl has been renamed.
16524    CHILD is true if decl is one of the renamed decls as part of
16525    importing whole module.  */
16526
16527 static void
16528 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
16529                                    bool child)
16530 {
16531   /* dw_die_ref at_import_die;  */
16532   dw_die_ref scope_die;
16533
16534   if (debug_info_level <= DINFO_LEVEL_TERSE)
16535     return;
16536
16537   gcc_assert (decl);
16538
16539   /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
16540      We need decl DIE for reference and scope die. First, get DIE for the decl
16541      itself.  */
16542
16543   /* Get the scope die for decl context. Use comp_unit_die for global module
16544      or decl. If die is not found for non globals, force new die.  */
16545   if (context
16546       && TYPE_P (context)
16547       && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
16548     return;
16549   scope_die = get_context_die (context);
16550
16551   if (child)
16552     {
16553       gcc_assert (scope_die->die_child);
16554       gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
16555       gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
16556       scope_die = scope_die->die_child;
16557     }
16558
16559   /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
16560   dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
16561
16562 }
16563
16564 /* Write the debugging output for DECL.  */
16565
16566 void
16567 dwarf2out_decl (tree decl)
16568 {
16569   dw_die_ref context_die = comp_unit_die;
16570
16571   switch (TREE_CODE (decl))
16572     {
16573     case ERROR_MARK:
16574       return;
16575
16576     case FUNCTION_DECL:
16577       /* What we would really like to do here is to filter out all mere
16578          file-scope declarations of file-scope functions which are never
16579          referenced later within this translation unit (and keep all of ones
16580          that *are* referenced later on) but we aren't clairvoyant, so we have
16581          no idea which functions will be referenced in the future (i.e. later
16582          on within the current translation unit). So here we just ignore all
16583          file-scope function declarations which are not also definitions.  If
16584          and when the debugger needs to know something about these functions,
16585          it will have to hunt around and find the DWARF information associated
16586          with the definition of the function.
16587
16588          We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
16589          nodes represent definitions and which ones represent mere
16590          declarations.  We have to check DECL_INITIAL instead. That's because
16591          the C front-end supports some weird semantics for "extern inline"
16592          function definitions.  These can get inlined within the current
16593          translation unit (and thus, we need to generate Dwarf info for their
16594          abstract instances so that the Dwarf info for the concrete inlined
16595          instances can have something to refer to) but the compiler never
16596          generates any out-of-lines instances of such things (despite the fact
16597          that they *are* definitions).
16598
16599          The important point is that the C front-end marks these "extern
16600          inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
16601          them anyway. Note that the C++ front-end also plays some similar games
16602          for inline function definitions appearing within include files which
16603          also contain `#pragma interface' pragmas.  */
16604       if (DECL_INITIAL (decl) == NULL_TREE)
16605         return;
16606
16607       /* If we're a nested function, initially use a parent of NULL; if we're
16608          a plain function, this will be fixed up in decls_for_scope.  If
16609          we're a method, it will be ignored, since we already have a DIE.  */
16610       if (decl_function_context (decl)
16611           /* But if we're in terse mode, we don't care about scope.  */
16612           && debug_info_level > DINFO_LEVEL_TERSE)
16613         context_die = NULL;
16614       break;
16615
16616     case VAR_DECL:
16617       /* Ignore this VAR_DECL if it refers to a file-scope extern data object
16618          declaration and if the declaration was never even referenced from
16619          within this entire compilation unit.  We suppress these DIEs in
16620          order to save space in the .debug section (by eliminating entries
16621          which are probably useless).  Note that we must not suppress
16622          block-local extern declarations (whether used or not) because that
16623          would screw-up the debugger's name lookup mechanism and cause it to
16624          miss things which really ought to be in scope at a given point.  */
16625       if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
16626         return;
16627
16628       /* For local statics lookup proper context die.  */
16629       if (TREE_STATIC (decl) && decl_function_context (decl))
16630         context_die = lookup_decl_die (DECL_CONTEXT (decl));
16631
16632       /* If we are in terse mode, don't generate any DIEs to represent any
16633          variable declarations or definitions.  */
16634       if (debug_info_level <= DINFO_LEVEL_TERSE)
16635         return;
16636       break;
16637
16638     case CONST_DECL:
16639       if (debug_info_level <= DINFO_LEVEL_TERSE)
16640         return;
16641       if (!is_fortran ())
16642         return;
16643       if (TREE_STATIC (decl) && decl_function_context (decl))
16644         context_die = lookup_decl_die (DECL_CONTEXT (decl));
16645       break;
16646
16647     case NAMESPACE_DECL:
16648     case IMPORTED_DECL:
16649       if (debug_info_level <= DINFO_LEVEL_TERSE)
16650         return;
16651       if (lookup_decl_die (decl) != NULL)
16652         return;
16653       break;
16654
16655     case TYPE_DECL:
16656       /* Don't emit stubs for types unless they are needed by other DIEs.  */
16657       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
16658         return;
16659
16660       /* Don't bother trying to generate any DIEs to represent any of the
16661          normal built-in types for the language we are compiling.  */
16662       if (DECL_IS_BUILTIN (decl))
16663         {
16664           /* OK, we need to generate one for `bool' so GDB knows what type
16665              comparisons have.  */
16666           if (is_cxx ()
16667               && TREE_CODE (TREE_TYPE (decl)) == BOOLEAN_TYPE
16668               && ! DECL_IGNORED_P (decl))
16669             modified_type_die (TREE_TYPE (decl), 0, 0, NULL);
16670
16671           return;
16672         }
16673
16674       /* If we are in terse mode, don't generate any DIEs for types.  */
16675       if (debug_info_level <= DINFO_LEVEL_TERSE)
16676         return;
16677
16678       /* If we're a function-scope tag, initially use a parent of NULL;
16679          this will be fixed up in decls_for_scope.  */
16680       if (decl_function_context (decl))
16681         context_die = NULL;
16682
16683       break;
16684
16685     default:
16686       return;
16687     }
16688
16689   gen_decl_die (decl, NULL, context_die);
16690 }
16691
16692 /* Output a marker (i.e. a label) for the beginning of the generated code for
16693    a lexical block.  */
16694
16695 static void
16696 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
16697                        unsigned int blocknum)
16698 {
16699   switch_to_section (current_function_section ());
16700   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
16701 }
16702
16703 /* Output a marker (i.e. a label) for the end of the generated code for a
16704    lexical block.  */
16705
16706 static void
16707 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
16708 {
16709   switch_to_section (current_function_section ());
16710   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
16711 }
16712
16713 /* Returns nonzero if it is appropriate not to emit any debugging
16714    information for BLOCK, because it doesn't contain any instructions.
16715
16716    Don't allow this for blocks with nested functions or local classes
16717    as we would end up with orphans, and in the presence of scheduling
16718    we may end up calling them anyway.  */
16719
16720 static bool
16721 dwarf2out_ignore_block (const_tree block)
16722 {
16723   tree decl;
16724   unsigned int i;
16725
16726   for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
16727     if (TREE_CODE (decl) == FUNCTION_DECL
16728         || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
16729       return 0;
16730   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
16731     {
16732       decl = BLOCK_NONLOCALIZED_VAR (block, i);
16733       if (TREE_CODE (decl) == FUNCTION_DECL
16734           || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
16735       return 0;
16736     }
16737
16738   return 1;
16739 }
16740
16741 /* Hash table routines for file_hash.  */
16742
16743 static int
16744 file_table_eq (const void *p1_p, const void *p2_p)
16745 {
16746   const struct dwarf_file_data *const p1 =
16747     (const struct dwarf_file_data *) p1_p;
16748   const char *const p2 = (const char *) p2_p;
16749   return strcmp (p1->filename, p2) == 0;
16750 }
16751
16752 static hashval_t
16753 file_table_hash (const void *p_p)
16754 {
16755   const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
16756   return htab_hash_string (p->filename);
16757 }
16758
16759 /* Lookup FILE_NAME (in the list of filenames that we know about here in
16760    dwarf2out.c) and return its "index".  The index of each (known) filename is
16761    just a unique number which is associated with only that one filename.  We
16762    need such numbers for the sake of generating labels (in the .debug_sfnames
16763    section) and references to those files numbers (in the .debug_srcinfo
16764    and.debug_macinfo sections).  If the filename given as an argument is not
16765    found in our current list, add it to the list and assign it the next
16766    available unique index number.  In order to speed up searches, we remember
16767    the index of the filename was looked up last.  This handles the majority of
16768    all searches.  */
16769
16770 static struct dwarf_file_data *
16771 lookup_filename (const char *file_name)
16772 {
16773   void ** slot;
16774   struct dwarf_file_data * created;
16775
16776   /* Check to see if the file name that was searched on the previous
16777      call matches this file name.  If so, return the index.  */
16778   if (file_table_last_lookup
16779       && (file_name == file_table_last_lookup->filename
16780           || strcmp (file_table_last_lookup->filename, file_name) == 0))
16781     return file_table_last_lookup;
16782
16783   /* Didn't match the previous lookup, search the table.  */
16784   slot = htab_find_slot_with_hash (file_table, file_name,
16785                                    htab_hash_string (file_name), INSERT);
16786   if (*slot)
16787     return (struct dwarf_file_data *) *slot;
16788
16789   created = GGC_NEW (struct dwarf_file_data);
16790   created->filename = file_name;
16791   created->emitted_number = 0;
16792   *slot = created;
16793   return created;
16794 }
16795
16796 /* If the assembler will construct the file table, then translate the compiler
16797    internal file table number into the assembler file table number, and emit
16798    a .file directive if we haven't already emitted one yet.  The file table
16799    numbers are different because we prune debug info for unused variables and
16800    types, which may include filenames.  */
16801
16802 static int
16803 maybe_emit_file (struct dwarf_file_data * fd)
16804 {
16805   if (! fd->emitted_number)
16806     {
16807       if (last_emitted_file)
16808         fd->emitted_number = last_emitted_file->emitted_number + 1;
16809       else
16810         fd->emitted_number = 1;
16811       last_emitted_file = fd;
16812
16813       if (DWARF2_ASM_LINE_DEBUG_INFO)
16814         {
16815           fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
16816           output_quoted_string (asm_out_file,
16817                                 remap_debug_filename (fd->filename));
16818           fputc ('\n', asm_out_file);
16819         }
16820     }
16821
16822   return fd->emitted_number;
16823 }
16824
16825 /* Schedule generation of a DW_AT_const_value attribute to DIE.
16826    That generation should happen after function debug info has been
16827    generated. The value of the attribute is the constant value of ARG.  */
16828
16829 static void
16830 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
16831 {
16832   die_arg_entry entry;
16833
16834   if (!die || !arg)
16835     return;
16836
16837   if (!tmpl_value_parm_die_table)
16838     tmpl_value_parm_die_table
16839       = VEC_alloc (die_arg_entry, gc, 32);
16840
16841   entry.die = die;
16842   entry.arg = arg;
16843   VEC_safe_push (die_arg_entry, gc,
16844                  tmpl_value_parm_die_table,
16845                  &entry);
16846 }
16847
16848 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
16849    by append_entry_to_tmpl_value_parm_die_table. This function must
16850    be called after function DIEs have been generated.  */
16851
16852 static void
16853 gen_remaining_tmpl_value_param_die_attribute (void)
16854 {
16855   if (tmpl_value_parm_die_table)
16856     {
16857       unsigned i;
16858       die_arg_entry *e;
16859
16860       for (i = 0;
16861            VEC_iterate (die_arg_entry, tmpl_value_parm_die_table, i, e);
16862            i++)
16863         tree_add_const_value_attribute (e->die, e->arg);
16864     }
16865 }
16866
16867
16868 /* Replace DW_AT_name for the decl with name.  */
16869  
16870 static void
16871 dwarf2out_set_name (tree decl, tree name)
16872 {
16873   dw_die_ref die;
16874   dw_attr_ref attr;
16875
16876   die = TYPE_SYMTAB_DIE (decl);
16877   if (!die)
16878     return;
16879
16880   attr = get_AT (die, DW_AT_name);
16881   if (attr)
16882     {
16883       struct indirect_string_node *node;
16884
16885       node = find_AT_string (dwarf2_name (name, 0));
16886       /* replace the string.  */
16887       attr->dw_attr_val.v.val_str = node;
16888     }
16889
16890   else
16891     add_name_attribute (die, dwarf2_name (name, 0));
16892 }
16893
16894 /* Called by the final INSN scan whenever we see a var location.  We
16895    use it to drop labels in the right places, and throw the location in
16896    our lookup table.  */
16897
16898 static void
16899 dwarf2out_var_location (rtx loc_note)
16900 {
16901   char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
16902   struct var_loc_node *newloc;
16903   rtx next_real;
16904   static const char *last_label;
16905   static bool last_in_cold_section_p;
16906   tree decl;
16907
16908   if (!DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
16909     return;
16910
16911   next_real = next_real_insn (loc_note);
16912   /* If there are no instructions which would be affected by this note,
16913      don't do anything.  */
16914   if (next_real == NULL_RTX)
16915     return;
16916
16917   newloc = GGC_CNEW (struct var_loc_node);
16918   /* If there were no real insns between note we processed last time
16919      and this note, use the label we emitted last time.  */
16920   if (last_var_location_insn != NULL_RTX
16921       && last_var_location_insn == next_real
16922       && last_in_cold_section_p == in_cold_section_p)
16923     newloc->label = last_label;
16924   else
16925     {
16926       ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
16927       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
16928       loclabel_num++;
16929       newloc->label = ggc_strdup (loclabel);
16930     }
16931   newloc->var_loc_note = loc_note;
16932   newloc->next = NULL;
16933
16934   if (cfun && in_cold_section_p)
16935     newloc->section_label = crtl->subsections.cold_section_label;
16936   else
16937     newloc->section_label = text_section_label;
16938
16939   last_var_location_insn = next_real;
16940   last_label = newloc->label;
16941   last_in_cold_section_p = in_cold_section_p;
16942   decl = NOTE_VAR_LOCATION_DECL (loc_note);
16943   add_var_loc_to_decl (decl, newloc);
16944 }
16945
16946 /* We need to reset the locations at the beginning of each
16947    function. We can't do this in the end_function hook, because the
16948    declarations that use the locations won't have been output when
16949    that hook is called.  Also compute have_multiple_function_sections here.  */
16950
16951 static void
16952 dwarf2out_begin_function (tree fun)
16953 {
16954   htab_empty (decl_loc_table);
16955
16956   if (function_section (fun) != text_section)
16957     have_multiple_function_sections = true;
16958
16959   dwarf2out_note_section_used ();
16960 }
16961
16962 /* Output a label to mark the beginning of a source code line entry
16963    and record information relating to this source line, in
16964    'line_info_table' for later output of the .debug_line section.  */
16965
16966 static void
16967 dwarf2out_source_line (unsigned int line, const char *filename,
16968                        int discriminator, bool is_stmt)
16969 {
16970   static bool last_is_stmt = true;
16971
16972   if (debug_info_level >= DINFO_LEVEL_NORMAL
16973       && line != 0)
16974     {
16975       int file_num = maybe_emit_file (lookup_filename (filename));
16976
16977       switch_to_section (current_function_section ());
16978
16979       /* If requested, emit something human-readable.  */
16980       if (flag_debug_asm)
16981         fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
16982                  filename, line);
16983
16984       if (DWARF2_ASM_LINE_DEBUG_INFO)
16985         {
16986           /* Emit the .loc directive understood by GNU as.  */
16987           fprintf (asm_out_file, "\t.loc %d %d 0", file_num, line);
16988           if (is_stmt != last_is_stmt)
16989             {
16990               fprintf (asm_out_file, " is_stmt %d", is_stmt ? 1 : 0);
16991               last_is_stmt = is_stmt;
16992             }
16993           if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
16994             fprintf (asm_out_file, " discriminator %d", discriminator);
16995           fputc ('\n', asm_out_file);
16996
16997           /* Indicate that line number info exists.  */
16998           line_info_table_in_use++;
16999         }
17000       else if (function_section (current_function_decl) != text_section)
17001         {
17002           dw_separate_line_info_ref line_info;
17003           targetm.asm_out.internal_label (asm_out_file,
17004                                           SEPARATE_LINE_CODE_LABEL,
17005                                           separate_line_info_table_in_use);
17006
17007           /* Expand the line info table if necessary.  */
17008           if (separate_line_info_table_in_use
17009               == separate_line_info_table_allocated)
17010             {
17011               separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
17012               separate_line_info_table
17013                 = GGC_RESIZEVEC (dw_separate_line_info_entry,
17014                                  separate_line_info_table,
17015                                  separate_line_info_table_allocated);
17016               memset (separate_line_info_table
17017                        + separate_line_info_table_in_use,
17018                       0,
17019                       (LINE_INFO_TABLE_INCREMENT
17020                        * sizeof (dw_separate_line_info_entry)));
17021             }
17022
17023           /* Add the new entry at the end of the line_info_table.  */
17024           line_info
17025             = &separate_line_info_table[separate_line_info_table_in_use++];
17026           line_info->dw_file_num = file_num;
17027           line_info->dw_line_num = line;
17028           line_info->function = current_function_funcdef_no;
17029         }
17030       else
17031         {
17032           dw_line_info_ref line_info;
17033
17034           targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL,
17035                                      line_info_table_in_use);
17036
17037           /* Expand the line info table if necessary.  */
17038           if (line_info_table_in_use == line_info_table_allocated)
17039             {
17040               line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
17041               line_info_table
17042                 = GGC_RESIZEVEC (dw_line_info_entry, line_info_table,
17043                                  line_info_table_allocated);
17044               memset (line_info_table + line_info_table_in_use, 0,
17045                       LINE_INFO_TABLE_INCREMENT * sizeof (dw_line_info_entry));
17046             }
17047
17048           /* Add the new entry at the end of the line_info_table.  */
17049           line_info = &line_info_table[line_info_table_in_use++];
17050           line_info->dw_file_num = file_num;
17051           line_info->dw_line_num = line;
17052         }
17053     }
17054 }
17055
17056 /* Record the beginning of a new source file.  */
17057
17058 static void
17059 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
17060 {
17061   if (flag_eliminate_dwarf2_dups)
17062     {
17063       /* Record the beginning of the file for break_out_includes.  */
17064       dw_die_ref bincl_die;
17065
17066       bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die, NULL);
17067       add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
17068     }
17069
17070   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
17071     {
17072       int file_num = maybe_emit_file (lookup_filename (filename));
17073
17074       switch_to_section (debug_macinfo_section);
17075       dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
17076       dw2_asm_output_data_uleb128 (lineno, "Included from line number %d",
17077                                    lineno);
17078
17079       dw2_asm_output_data_uleb128 (file_num, "file %s", filename);
17080     }
17081 }
17082
17083 /* Record the end of a source file.  */
17084
17085 static void
17086 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
17087 {
17088   if (flag_eliminate_dwarf2_dups)
17089     /* Record the end of the file for break_out_includes.  */
17090     new_die (DW_TAG_GNU_EINCL, comp_unit_die, NULL);
17091
17092   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
17093     {
17094       switch_to_section (debug_macinfo_section);
17095       dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
17096     }
17097 }
17098
17099 /* Called from debug_define in toplev.c.  The `buffer' parameter contains
17100    the tail part of the directive line, i.e. the part which is past the
17101    initial whitespace, #, whitespace, directive-name, whitespace part.  */
17102
17103 static void
17104 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
17105                   const char *buffer ATTRIBUTE_UNUSED)
17106 {
17107   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
17108     {
17109       switch_to_section (debug_macinfo_section);
17110       dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
17111       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
17112       dw2_asm_output_nstring (buffer, -1, "The macro");
17113     }
17114 }
17115
17116 /* Called from debug_undef in toplev.c.  The `buffer' parameter contains
17117    the tail part of the directive line, i.e. the part which is past the
17118    initial whitespace, #, whitespace, directive-name, whitespace part.  */
17119
17120 static void
17121 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
17122                  const char *buffer ATTRIBUTE_UNUSED)
17123 {
17124   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
17125     {
17126       switch_to_section (debug_macinfo_section);
17127       dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
17128       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
17129       dw2_asm_output_nstring (buffer, -1, "The macro");
17130     }
17131 }
17132
17133 /* Set up for Dwarf output at the start of compilation.  */
17134
17135 static void
17136 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
17137 {
17138   /* Allocate the file_table.  */
17139   file_table = htab_create_ggc (50, file_table_hash,
17140                                 file_table_eq, NULL);
17141
17142   /* Allocate the decl_die_table.  */
17143   decl_die_table = htab_create_ggc (10, decl_die_table_hash,
17144                                     decl_die_table_eq, NULL);
17145
17146   /* Allocate the decl_loc_table.  */
17147   decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
17148                                     decl_loc_table_eq, NULL);
17149
17150   /* Allocate the initial hunk of the decl_scope_table.  */
17151   decl_scope_table = VEC_alloc (tree, gc, 256);
17152
17153   /* Allocate the initial hunk of the abbrev_die_table.  */
17154   abbrev_die_table = GGC_CNEWVEC (dw_die_ref, ABBREV_DIE_TABLE_INCREMENT);
17155   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
17156   /* Zero-th entry is allocated, but unused.  */
17157   abbrev_die_table_in_use = 1;
17158
17159   /* Allocate the initial hunk of the line_info_table.  */
17160   line_info_table = GGC_CNEWVEC (dw_line_info_entry, LINE_INFO_TABLE_INCREMENT);
17161   line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
17162
17163   /* Zero-th entry is allocated, but unused.  */
17164   line_info_table_in_use = 1;
17165
17166   /* Allocate the pubtypes and pubnames vectors.  */
17167   pubname_table = VEC_alloc (pubname_entry, gc, 32);
17168   pubtype_table = VEC_alloc (pubname_entry, gc, 32);
17169
17170   /* Generate the initial DIE for the .debug section.  Note that the (string)
17171      value given in the DW_AT_name attribute of the DW_TAG_compile_unit DIE
17172      will (typically) be a relative pathname and that this pathname should be
17173      taken as being relative to the directory from which the compiler was
17174      invoked when the given (base) source file was compiled.  We will fill
17175      in this value in dwarf2out_finish.  */
17176   comp_unit_die = gen_compile_unit_die (NULL);
17177
17178   incomplete_types = VEC_alloc (tree, gc, 64);
17179
17180   used_rtx_array = VEC_alloc (rtx, gc, 32);
17181
17182   debug_info_section = get_section (DEBUG_INFO_SECTION,
17183                                     SECTION_DEBUG, NULL);
17184   debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
17185                                       SECTION_DEBUG, NULL);
17186   debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
17187                                        SECTION_DEBUG, NULL);
17188   debug_macinfo_section = get_section (DEBUG_MACINFO_SECTION,
17189                                        SECTION_DEBUG, NULL);
17190   debug_line_section = get_section (DEBUG_LINE_SECTION,
17191                                     SECTION_DEBUG, NULL);
17192   debug_loc_section = get_section (DEBUG_LOC_SECTION,
17193                                    SECTION_DEBUG, NULL);
17194   debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
17195                                         SECTION_DEBUG, NULL);
17196   debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
17197                                         SECTION_DEBUG, NULL);
17198   debug_str_section = get_section (DEBUG_STR_SECTION,
17199                                    DEBUG_STR_SECTION_FLAGS, NULL);
17200   debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
17201                                       SECTION_DEBUG, NULL);
17202   debug_frame_section = get_section (DEBUG_FRAME_SECTION,
17203                                      SECTION_DEBUG, NULL);
17204
17205   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
17206   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
17207                                DEBUG_ABBREV_SECTION_LABEL, 0);
17208   ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
17209   ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
17210                                COLD_TEXT_SECTION_LABEL, 0);
17211   ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
17212
17213   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
17214                                DEBUG_INFO_SECTION_LABEL, 0);
17215   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
17216                                DEBUG_LINE_SECTION_LABEL, 0);
17217   ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
17218                                DEBUG_RANGES_SECTION_LABEL, 0);
17219   switch_to_section (debug_abbrev_section);
17220   ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
17221   switch_to_section (debug_info_section);
17222   ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
17223   switch_to_section (debug_line_section);
17224   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
17225
17226   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
17227     {
17228       switch_to_section (debug_macinfo_section);
17229       ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
17230                                    DEBUG_MACINFO_SECTION_LABEL, 0);
17231       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
17232     }
17233
17234   switch_to_section (text_section);
17235   ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
17236   if (flag_reorder_blocks_and_partition)
17237     {
17238       cold_text_section = unlikely_text_section ();
17239       switch_to_section (cold_text_section);
17240       ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
17241     }
17242 }
17243
17244 /* A helper function for dwarf2out_finish called through
17245    ht_forall.  Emit one queued .debug_str string.  */
17246
17247 static int
17248 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
17249 {
17250   struct indirect_string_node *node = (struct indirect_string_node *) *h;
17251
17252   if (node->form == DW_FORM_strp)
17253     {
17254       switch_to_section (debug_str_section);
17255       ASM_OUTPUT_LABEL (asm_out_file, node->label);
17256       assemble_string (node->str, strlen (node->str) + 1);
17257     }
17258
17259   return 1;
17260 }
17261
17262 #if ENABLE_ASSERT_CHECKING
17263 /* Verify that all marks are clear.  */
17264
17265 static void
17266 verify_marks_clear (dw_die_ref die)
17267 {
17268   dw_die_ref c;
17269
17270   gcc_assert (! die->die_mark);
17271   FOR_EACH_CHILD (die, c, verify_marks_clear (c));
17272 }
17273 #endif /* ENABLE_ASSERT_CHECKING */
17274
17275 /* Clear the marks for a die and its children.
17276    Be cool if the mark isn't set.  */
17277
17278 static void
17279 prune_unmark_dies (dw_die_ref die)
17280 {
17281   dw_die_ref c;
17282
17283   if (die->die_mark)
17284     die->die_mark = 0;
17285   FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
17286 }
17287
17288 /* Given DIE that we're marking as used, find any other dies
17289    it references as attributes and mark them as used.  */
17290
17291 static void
17292 prune_unused_types_walk_attribs (dw_die_ref die)
17293 {
17294   dw_attr_ref a;
17295   unsigned ix;
17296
17297   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
17298     {
17299       if (a->dw_attr_val.val_class == dw_val_class_die_ref)
17300         {
17301           /* A reference to another DIE.
17302              Make sure that it will get emitted.  */
17303           prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
17304         }
17305       /* Set the string's refcount to 0 so that prune_unused_types_mark
17306          accounts properly for it.  */
17307       if (AT_class (a) == dw_val_class_str)
17308         a->dw_attr_val.v.val_str->refcount = 0;
17309     }
17310 }
17311
17312
17313 /* Mark DIE as being used.  If DOKIDS is true, then walk down
17314    to DIE's children.  */
17315
17316 static void
17317 prune_unused_types_mark (dw_die_ref die, int dokids)
17318 {
17319   dw_die_ref c;
17320
17321   if (die->die_mark == 0)
17322     {
17323       /* We haven't done this node yet.  Mark it as used.  */
17324       die->die_mark = 1;
17325
17326       /* We also have to mark its parents as used.
17327          (But we don't want to mark our parents' kids due to this.)  */
17328       if (die->die_parent)
17329         prune_unused_types_mark (die->die_parent, 0);
17330
17331       /* Mark any referenced nodes.  */
17332       prune_unused_types_walk_attribs (die);
17333
17334       /* If this node is a specification,
17335          also mark the definition, if it exists.  */
17336       if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
17337         prune_unused_types_mark (die->die_definition, 1);
17338     }
17339
17340   if (dokids && die->die_mark != 2)
17341     {
17342       /* We need to walk the children, but haven't done so yet.
17343          Remember that we've walked the kids.  */
17344       die->die_mark = 2;
17345
17346       /* If this is an array type, we need to make sure our
17347          kids get marked, even if they're types.  */
17348       if (die->die_tag == DW_TAG_array_type)
17349         FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
17350       else
17351         FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
17352     }
17353 }
17354
17355 /* For local classes, look if any static member functions were emitted
17356    and if so, mark them.  */
17357
17358 static void
17359 prune_unused_types_walk_local_classes (dw_die_ref die)
17360 {
17361   dw_die_ref c;
17362
17363   if (die->die_mark == 2)
17364     return;
17365
17366   switch (die->die_tag)
17367     {
17368     case DW_TAG_structure_type:
17369     case DW_TAG_union_type:
17370     case DW_TAG_class_type:
17371       break;
17372
17373     case DW_TAG_subprogram:
17374       if (!get_AT_flag (die, DW_AT_declaration)
17375           || die->die_definition != NULL)
17376         prune_unused_types_mark (die, 1);
17377       return;
17378
17379     default:
17380       return;
17381     }
17382
17383   /* Mark children.  */
17384   FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
17385 }
17386
17387 /* Walk the tree DIE and mark types that we actually use.  */
17388
17389 static void
17390 prune_unused_types_walk (dw_die_ref die)
17391 {
17392   dw_die_ref c;
17393
17394   /* Don't do anything if this node is already marked and
17395      children have been marked as well.  */
17396   if (die->die_mark == 2)
17397     return;
17398
17399   switch (die->die_tag)
17400     {
17401     case DW_TAG_structure_type:
17402     case DW_TAG_union_type:
17403     case DW_TAG_class_type:
17404       if (die->die_perennial_p)
17405         break;
17406
17407       for (c = die->die_parent; c; c = c->die_parent)
17408         if (c->die_tag == DW_TAG_subprogram)
17409           break;
17410
17411       /* Finding used static member functions inside of classes
17412          is needed just for local classes, because for other classes
17413          static member function DIEs with DW_AT_specification
17414          are emitted outside of the DW_TAG_*_type.  If we ever change
17415          it, we'd need to call this even for non-local classes.  */
17416       if (c)
17417         prune_unused_types_walk_local_classes (die);
17418
17419       /* It's a type node --- don't mark it.  */
17420       return;
17421
17422     case DW_TAG_const_type:
17423     case DW_TAG_packed_type:
17424     case DW_TAG_pointer_type:
17425     case DW_TAG_reference_type:
17426     case DW_TAG_volatile_type:
17427     case DW_TAG_typedef:
17428     case DW_TAG_array_type:
17429     case DW_TAG_interface_type:
17430     case DW_TAG_friend:
17431     case DW_TAG_variant_part:
17432     case DW_TAG_enumeration_type:
17433     case DW_TAG_subroutine_type:
17434     case DW_TAG_string_type:
17435     case DW_TAG_set_type:
17436     case DW_TAG_subrange_type:
17437     case DW_TAG_ptr_to_member_type:
17438     case DW_TAG_file_type:
17439       if (die->die_perennial_p)
17440         break;
17441
17442       /* It's a type node --- don't mark it.  */
17443       return;
17444
17445     default:
17446       /* Mark everything else.  */
17447       break;
17448   }
17449
17450   if (die->die_mark == 0)
17451     {
17452       die->die_mark = 1;
17453
17454       /* Now, mark any dies referenced from here.  */
17455       prune_unused_types_walk_attribs (die);
17456     }
17457
17458   die->die_mark = 2;
17459
17460   /* Mark children.  */
17461   FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
17462 }
17463
17464 /* Increment the string counts on strings referred to from DIE's
17465    attributes.  */
17466
17467 static void
17468 prune_unused_types_update_strings (dw_die_ref die)
17469 {
17470   dw_attr_ref a;
17471   unsigned ix;
17472
17473   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
17474     if (AT_class (a) == dw_val_class_str)
17475       {
17476         struct indirect_string_node *s = a->dw_attr_val.v.val_str;
17477         s->refcount++;
17478         /* Avoid unnecessarily putting strings that are used less than
17479            twice in the hash table.  */
17480         if (s->refcount
17481             == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
17482           {
17483             void ** slot;
17484             slot = htab_find_slot_with_hash (debug_str_hash, s->str,
17485                                              htab_hash_string (s->str),
17486                                              INSERT);
17487             gcc_assert (*slot == NULL);
17488             *slot = s;
17489           }
17490       }
17491 }
17492
17493 /* Remove from the tree DIE any dies that aren't marked.  */
17494
17495 static void
17496 prune_unused_types_prune (dw_die_ref die)
17497 {
17498   dw_die_ref c;
17499
17500   gcc_assert (die->die_mark);
17501   prune_unused_types_update_strings (die);
17502
17503   if (! die->die_child)
17504     return;
17505
17506   c = die->die_child;
17507   do {
17508     dw_die_ref prev = c;
17509     for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
17510       if (c == die->die_child)
17511         {
17512           /* No marked children between 'prev' and the end of the list.  */
17513           if (prev == c)
17514             /* No marked children at all.  */
17515             die->die_child = NULL;
17516           else
17517             {
17518               prev->die_sib = c->die_sib;
17519               die->die_child = prev;
17520             }
17521           return;
17522         }
17523
17524     if (c != prev->die_sib)
17525       prev->die_sib = c;
17526     prune_unused_types_prune (c);
17527   } while (c != die->die_child);
17528 }
17529
17530
17531 /* Remove dies representing declarations that we never use.  */
17532
17533 static void
17534 prune_unused_types (void)
17535 {
17536   unsigned int i;
17537   limbo_die_node *node;
17538   pubname_ref pub;
17539
17540 #if ENABLE_ASSERT_CHECKING
17541   /* All the marks should already be clear.  */
17542   verify_marks_clear (comp_unit_die);
17543   for (node = limbo_die_list; node; node = node->next)
17544     verify_marks_clear (node->die);
17545 #endif /* ENABLE_ASSERT_CHECKING */
17546
17547   /* Set the mark on nodes that are actually used.  */
17548   prune_unused_types_walk (comp_unit_die);
17549   for (node = limbo_die_list; node; node = node->next)
17550     prune_unused_types_walk (node->die);
17551
17552   /* Also set the mark on nodes referenced from the
17553      pubname_table or arange_table.  */
17554   for (i = 0; VEC_iterate (pubname_entry, pubname_table, i, pub); i++)
17555     prune_unused_types_mark (pub->die, 1);
17556   for (i = 0; i < arange_table_in_use; i++)
17557     prune_unused_types_mark (arange_table[i], 1);
17558
17559   /* Get rid of nodes that aren't marked; and update the string counts.  */
17560   if (debug_str_hash)
17561     htab_empty (debug_str_hash);
17562   prune_unused_types_prune (comp_unit_die);
17563   for (node = limbo_die_list; node; node = node->next)
17564     prune_unused_types_prune (node->die);
17565
17566   /* Leave the marks clear.  */
17567   prune_unmark_dies (comp_unit_die);
17568   for (node = limbo_die_list; node; node = node->next)
17569     prune_unmark_dies (node->die);
17570 }
17571
17572 /* Set the parameter to true if there are any relative pathnames in
17573    the file table.  */
17574 static int
17575 file_table_relative_p (void ** slot, void *param)
17576 {
17577   bool *p = (bool *) param;
17578   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
17579   if (!IS_ABSOLUTE_PATH (d->filename))
17580     {
17581       *p = true;
17582       return 0;
17583     }
17584   return 1;
17585 }
17586
17587 /* Move a DW_AT_MIPS_linkage_name attribute just added to dw_die_ref
17588    to the location it would have been added, should we know its
17589    DECL_ASSEMBLER_NAME when we added other attributes.  This will
17590    probably improve compactness of debug info, removing equivalent
17591    abbrevs, and hide any differences caused by deferring the
17592    computation of the assembler name, triggered by e.g. PCH.  */
17593
17594 static inline void
17595 move_linkage_attr (dw_die_ref die)
17596 {
17597   unsigned ix = VEC_length (dw_attr_node, die->die_attr);
17598   dw_attr_node linkage = *VEC_index (dw_attr_node, die->die_attr, ix - 1);
17599
17600   gcc_assert (linkage.dw_attr == DW_AT_MIPS_linkage_name);
17601
17602   while (--ix > 0)
17603     {
17604       dw_attr_node *prev = VEC_index (dw_attr_node, die->die_attr, ix - 1);
17605
17606       if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
17607         break;
17608     }
17609
17610   if (ix != VEC_length (dw_attr_node, die->die_attr) - 1)
17611     {
17612       VEC_pop (dw_attr_node, die->die_attr);
17613       VEC_quick_insert (dw_attr_node, die->die_attr, ix, &linkage);
17614     }
17615 }
17616
17617 /* Output stuff that dwarf requires at the end of every file,
17618    and generate the DWARF-2 debugging info.  */
17619
17620 static void
17621 dwarf2out_finish (const char *filename)
17622 {
17623   limbo_die_node *node, *next_node;
17624   dw_die_ref die = 0;
17625   unsigned int i;
17626
17627   gen_remaining_tmpl_value_param_die_attribute ();
17628
17629   /* Add the name for the main input file now.  We delayed this from
17630      dwarf2out_init to avoid complications with PCH.  */
17631   add_name_attribute (comp_unit_die, remap_debug_filename (filename));
17632   if (!IS_ABSOLUTE_PATH (filename))
17633     add_comp_dir_attribute (comp_unit_die);
17634   else if (get_AT (comp_unit_die, DW_AT_comp_dir) == NULL)
17635     {
17636       bool p = false;
17637       htab_traverse (file_table, file_table_relative_p, &p);
17638       if (p)
17639         add_comp_dir_attribute (comp_unit_die);
17640     }
17641
17642   for (i = 0; i < VEC_length (deferred_locations, deferred_locations_list); i++)
17643     {
17644       add_location_or_const_value_attribute (
17645         VEC_index (deferred_locations, deferred_locations_list, i)->die,
17646         VEC_index (deferred_locations, deferred_locations_list, i)->variable,
17647         DW_AT_location);
17648     }
17649
17650   /* Traverse the limbo die list, and add parent/child links.  The only
17651      dies without parents that should be here are concrete instances of
17652      inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
17653      For concrete instances, we can get the parent die from the abstract
17654      instance.  */
17655   for (node = limbo_die_list; node; node = next_node)
17656     {
17657       next_node = node->next;
17658       die = node->die;
17659
17660       if (die->die_parent == NULL)
17661         {
17662           dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
17663
17664           if (origin)
17665             add_child_die (origin->die_parent, die);
17666           else if (die == comp_unit_die)
17667             ;
17668           else if (errorcount > 0 || sorrycount > 0)
17669             /* It's OK to be confused by errors in the input.  */
17670             add_child_die (comp_unit_die, die);
17671           else
17672             {
17673               /* In certain situations, the lexical block containing a
17674                  nested function can be optimized away, which results
17675                  in the nested function die being orphaned.  Likewise
17676                  with the return type of that nested function.  Force
17677                  this to be a child of the containing function.
17678
17679                  It may happen that even the containing function got fully
17680                  inlined and optimized out.  In that case we are lost and
17681                  assign the empty child.  This should not be big issue as
17682                  the function is likely unreachable too.  */
17683               tree context = NULL_TREE;
17684
17685               gcc_assert (node->created_for);
17686
17687               if (DECL_P (node->created_for))
17688                 context = DECL_CONTEXT (node->created_for);
17689               else if (TYPE_P (node->created_for))
17690                 context = TYPE_CONTEXT (node->created_for);
17691
17692               gcc_assert (context
17693                           && (TREE_CODE (context) == FUNCTION_DECL
17694                               || TREE_CODE (context) == NAMESPACE_DECL));
17695
17696               origin = lookup_decl_die (context);
17697               if (origin)
17698                 add_child_die (origin, die);
17699               else
17700                 add_child_die (comp_unit_die, die);
17701             }
17702         }
17703     }
17704
17705   limbo_die_list = NULL;
17706
17707   for (node = deferred_asm_name; node; node = node->next)
17708     {
17709       tree decl = node->created_for;
17710       if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
17711         {
17712           add_AT_string (node->die, DW_AT_MIPS_linkage_name,
17713                          IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
17714           move_linkage_attr (node->die);
17715         }
17716     }
17717
17718   deferred_asm_name = NULL;
17719
17720   /* Walk through the list of incomplete types again, trying once more to
17721      emit full debugging info for them.  */
17722   retry_incomplete_types ();
17723
17724   if (flag_eliminate_unused_debug_types)
17725     prune_unused_types ();
17726
17727   /* Generate separate CUs for each of the include files we've seen.
17728      They will go into limbo_die_list.  */
17729   if (flag_eliminate_dwarf2_dups)
17730     break_out_includes (comp_unit_die);
17731
17732   /* Traverse the DIE's and add add sibling attributes to those DIE's
17733      that have children.  */
17734   add_sibling_attributes (comp_unit_die);
17735   for (node = limbo_die_list; node; node = node->next)
17736     add_sibling_attributes (node->die);
17737
17738   /* Output a terminator label for the .text section.  */
17739   switch_to_section (text_section);
17740   targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
17741   if (flag_reorder_blocks_and_partition)
17742     {
17743       switch_to_section (unlikely_text_section ());
17744       targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
17745     }
17746
17747   /* We can only use the low/high_pc attributes if all of the code was
17748      in .text.  */
17749   if (!have_multiple_function_sections)
17750     {
17751       add_AT_lbl_id (comp_unit_die, DW_AT_low_pc, text_section_label);
17752       add_AT_lbl_id (comp_unit_die, DW_AT_high_pc, text_end_label);
17753     }
17754
17755   else
17756     {
17757       unsigned fde_idx = 0;
17758
17759       /* We need to give .debug_loc and .debug_ranges an appropriate
17760          "base address".  Use zero so that these addresses become
17761          absolute.  Historically, we've emitted the unexpected
17762          DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
17763          Emit both to give time for other tools to adapt.  */
17764       add_AT_addr (comp_unit_die, DW_AT_low_pc, const0_rtx);
17765       add_AT_addr (comp_unit_die, DW_AT_entry_pc, const0_rtx);
17766
17767       add_AT_range_list (comp_unit_die, DW_AT_ranges,
17768                          add_ranges_by_labels (text_section_label,
17769                                                text_end_label));
17770       if (flag_reorder_blocks_and_partition)
17771         add_ranges_by_labels (cold_text_section_label,
17772                               cold_end_label);
17773
17774       for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
17775         {
17776           dw_fde_ref fde = &fde_table[fde_idx];
17777
17778           if (fde->dw_fde_switched_sections)
17779             {
17780               if (!fde->in_std_section)
17781                 add_ranges_by_labels (fde->dw_fde_hot_section_label,
17782                                       fde->dw_fde_hot_section_end_label);
17783               if (!fde->cold_in_std_section)
17784                 add_ranges_by_labels (fde->dw_fde_unlikely_section_label,
17785                                       fde->dw_fde_unlikely_section_end_label);
17786             }
17787           else if (!fde->in_std_section)
17788             add_ranges_by_labels (fde->dw_fde_begin,
17789                                   fde->dw_fde_end);
17790         }
17791
17792       add_ranges (NULL);
17793     }
17794
17795   /* Output location list section if necessary.  */
17796   if (have_location_lists)
17797     {
17798       /* Output the location lists info.  */
17799       switch_to_section (debug_loc_section);
17800       ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
17801                                    DEBUG_LOC_SECTION_LABEL, 0);
17802       ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
17803       output_location_lists (die);
17804     }
17805
17806   if (debug_info_level >= DINFO_LEVEL_NORMAL)
17807     add_AT_lineptr (comp_unit_die, DW_AT_stmt_list,
17808                     debug_line_section_label);
17809
17810   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
17811     add_AT_macptr (comp_unit_die, DW_AT_macro_info, macinfo_section_label);
17812
17813   /* Output all of the compilation units.  We put the main one last so that
17814      the offsets are available to output_pubnames.  */
17815   for (node = limbo_die_list; node; node = node->next)
17816     output_comp_unit (node->die, 0);
17817
17818   /* Output the main compilation unit if non-empty or if .debug_macinfo
17819      has been emitted.  */
17820   output_comp_unit (comp_unit_die, debug_info_level >= DINFO_LEVEL_VERBOSE);
17821
17822   /* Output the abbreviation table.  */
17823   switch_to_section (debug_abbrev_section);
17824   output_abbrev_section ();
17825
17826   /* Output public names table if necessary.  */
17827   if (!VEC_empty (pubname_entry, pubname_table))
17828     {
17829       switch_to_section (debug_pubnames_section);
17830       output_pubnames (pubname_table);
17831     }
17832
17833   /* Output public types table if necessary.  */
17834   /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
17835      It shouldn't hurt to emit it always, since pure DWARF2 consumers
17836      simply won't look for the section.  */
17837   if (!VEC_empty (pubname_entry, pubtype_table))
17838     {
17839       switch_to_section (debug_pubtypes_section);
17840       output_pubnames (pubtype_table);
17841     }
17842
17843   /* Output the address range information.  We only put functions in the arange
17844      table, so don't write it out if we don't have any.  */
17845   if (fde_table_in_use)
17846     {
17847       switch_to_section (debug_aranges_section);
17848       output_aranges ();
17849     }
17850
17851   /* Output ranges section if necessary.  */
17852   if (ranges_table_in_use)
17853     {
17854       switch_to_section (debug_ranges_section);
17855       ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
17856       output_ranges ();
17857     }
17858
17859   /* Output the source line correspondence table.  We must do this
17860      even if there is no line information.  Otherwise, on an empty
17861      translation unit, we will generate a present, but empty,
17862      .debug_info section.  IRIX 6.5 `nm' will then complain when
17863      examining the file.  This is done late so that any filenames
17864      used by the debug_info section are marked as 'used'.  */
17865   if (! DWARF2_ASM_LINE_DEBUG_INFO)
17866     {
17867       switch_to_section (debug_line_section);
17868       output_line_info ();
17869     }
17870
17871   /* Have to end the macro section.  */
17872   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
17873     {
17874       switch_to_section (debug_macinfo_section);
17875       dw2_asm_output_data (1, 0, "End compilation unit");
17876     }
17877
17878   /* If we emitted any DW_FORM_strp form attribute, output the string
17879      table too.  */
17880   if (debug_str_hash)
17881     htab_traverse (debug_str_hash, output_indirect_string, NULL);
17882 }
17883 #else
17884
17885 /* This should never be used, but its address is needed for comparisons.  */
17886 const struct gcc_debug_hooks dwarf2_debug_hooks =
17887 {
17888   0,            /* init */
17889   0,            /* finish */
17890   0,            /* define */
17891   0,            /* undef */
17892   0,            /* start_source_file */
17893   0,            /* end_source_file */
17894   0,            /* begin_block */
17895   0,            /* end_block */
17896   0,            /* ignore_block */
17897   0,            /* source_line */
17898   0,            /* begin_prologue */
17899   0,            /* end_prologue */
17900   0,            /* end_epilogue */
17901   0,            /* begin_function */
17902   0,            /* end_function */
17903   0,            /* function_decl */
17904   0,            /* global_decl */
17905   0,            /* type_decl */
17906   0,            /* imported_module_or_decl */
17907   0,            /* deferred_inline_function */
17908   0,            /* outlining_inline_function */
17909   0,            /* label */
17910   0,            /* handle_pch */
17911   0,            /* var_location */
17912   0,            /* switch_text_section */
17913   0,            /* set_name */
17914   0             /* start_end_main_source_file */
17915 };
17916
17917 #endif /* DWARF2_DEBUGGING_INFO */
17918
17919 #include "gt-dwarf2out.h"