OSDN Git Service

2009-07-14 Dodji Seketeli <dodji@redhat.com>
[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 #ifndef DWARF2_FRAME_INFO
100 # ifdef DWARF2_DEBUGGING_INFO
101 #  define DWARF2_FRAME_INFO \
102   (write_symbols == DWARF2_DEBUG || write_symbols == VMS_AND_DWARF2_DEBUG)
103 # else
104 #  define DWARF2_FRAME_INFO 0
105 # endif
106 #endif
107
108 /* Map register numbers held in the call frame info that gcc has
109    collected using DWARF_FRAME_REGNUM to those that should be output in
110    .debug_frame and .eh_frame.  */
111 #ifndef DWARF2_FRAME_REG_OUT
112 #define DWARF2_FRAME_REG_OUT(REGNO, FOR_EH) (REGNO)
113 #endif
114
115 /* Save the result of dwarf2out_do_frame across PCH.  */
116 static GTY(()) bool saved_do_cfi_asm = 0;
117
118 /* Decide whether we want to emit frame unwind information for the current
119    translation unit.  */
120
121 int
122 dwarf2out_do_frame (void)
123 {
124   /* We want to emit correct CFA location expressions or lists, so we
125      have to return true if we're going to output debug info, even if
126      we're not going to output frame or unwind info.  */
127   return (write_symbols == DWARF2_DEBUG
128           || write_symbols == VMS_AND_DWARF2_DEBUG
129           || DWARF2_FRAME_INFO || saved_do_cfi_asm
130 #ifdef DWARF2_UNWIND_INFO
131           || (DWARF2_UNWIND_INFO
132               && (flag_unwind_tables
133                   || (flag_exceptions && ! USING_SJLJ_EXCEPTIONS)))
134 #endif
135           );
136 }
137
138 /* Decide whether to emit frame unwind via assembler directives.  */
139
140 int
141 dwarf2out_do_cfi_asm (void)
142 {
143   int enc;
144
145 #ifdef MIPS_DEBUGGING_INFO
146   return false;
147 #endif
148   if (!flag_dwarf2_cfi_asm || !dwarf2out_do_frame ())
149     return false;
150   if (saved_do_cfi_asm || !eh_personality_libfunc)
151     return true;
152   if (!HAVE_GAS_CFI_PERSONALITY_DIRECTIVE)
153     return false;
154
155   /* Make sure the personality encoding is one the assembler can support.
156      In particular, aligned addresses can't be handled.  */
157   enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,/*global=*/1);
158   if ((enc & 0x70) != 0 && (enc & 0x70) != DW_EH_PE_pcrel)
159     return false;
160   enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,/*global=*/0);
161   if ((enc & 0x70) != 0 && (enc & 0x70) != DW_EH_PE_pcrel)
162     return false;
163
164   saved_do_cfi_asm = true;
165   return true;
166 }
167
168 /* The size of the target's pointer type.  */
169 #ifndef PTR_SIZE
170 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
171 #endif
172
173 /* Array of RTXes referenced by the debugging information, which therefore
174    must be kept around forever.  */
175 static GTY(()) VEC(rtx,gc) *used_rtx_array;
176
177 /* A pointer to the base of a list of incomplete types which might be
178    completed at some later time.  incomplete_types_list needs to be a
179    VEC(tree,gc) because we want to tell the garbage collector about
180    it.  */
181 static GTY(()) VEC(tree,gc) *incomplete_types;
182
183 /* A pointer to the base of a table of references to declaration
184    scopes.  This table is a display which tracks the nesting
185    of declaration scopes at the current scope and containing
186    scopes.  This table is used to find the proper place to
187    define type declaration DIE's.  */
188 static GTY(()) VEC(tree,gc) *decl_scope_table;
189
190 /* Pointers to various DWARF2 sections.  */
191 static GTY(()) section *debug_info_section;
192 static GTY(()) section *debug_abbrev_section;
193 static GTY(()) section *debug_aranges_section;
194 static GTY(()) section *debug_macinfo_section;
195 static GTY(()) section *debug_line_section;
196 static GTY(()) section *debug_loc_section;
197 static GTY(()) section *debug_pubnames_section;
198 static GTY(()) section *debug_pubtypes_section;
199 static GTY(()) section *debug_str_section;
200 static GTY(()) section *debug_ranges_section;
201 static GTY(()) section *debug_frame_section;
202
203 /* How to start an assembler comment.  */
204 #ifndef ASM_COMMENT_START
205 #define ASM_COMMENT_START ";#"
206 #endif
207
208 typedef struct dw_cfi_struct *dw_cfi_ref;
209 typedef struct dw_fde_struct *dw_fde_ref;
210 typedef union  dw_cfi_oprnd_struct *dw_cfi_oprnd_ref;
211
212 /* Call frames are described using a sequence of Call Frame
213    Information instructions.  The register number, offset
214    and address fields are provided as possible operands;
215    their use is selected by the opcode field.  */
216
217 enum dw_cfi_oprnd_type {
218   dw_cfi_oprnd_unused,
219   dw_cfi_oprnd_reg_num,
220   dw_cfi_oprnd_offset,
221   dw_cfi_oprnd_addr,
222   dw_cfi_oprnd_loc
223 };
224
225 typedef union GTY(()) dw_cfi_oprnd_struct {
226   unsigned int GTY ((tag ("dw_cfi_oprnd_reg_num"))) dw_cfi_reg_num;
227   HOST_WIDE_INT GTY ((tag ("dw_cfi_oprnd_offset"))) dw_cfi_offset;
228   const char * GTY ((tag ("dw_cfi_oprnd_addr"))) dw_cfi_addr;
229   struct dw_loc_descr_struct * GTY ((tag ("dw_cfi_oprnd_loc"))) dw_cfi_loc;
230 }
231 dw_cfi_oprnd;
232
233 typedef struct GTY(()) dw_cfi_struct {
234   dw_cfi_ref dw_cfi_next;
235   enum dwarf_call_frame_info dw_cfi_opc;
236   dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd1_desc (%1.dw_cfi_opc)")))
237     dw_cfi_oprnd1;
238   dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd2_desc (%1.dw_cfi_opc)")))
239     dw_cfi_oprnd2;
240 }
241 dw_cfi_node;
242
243 /* This is how we define the location of the CFA. We use to handle it
244    as REG + OFFSET all the time,  but now it can be more complex.
245    It can now be either REG + CFA_OFFSET or *(REG + BASE_OFFSET) + CFA_OFFSET.
246    Instead of passing around REG and OFFSET, we pass a copy
247    of this structure.  */
248 typedef struct GTY(()) cfa_loc {
249   HOST_WIDE_INT offset;
250   HOST_WIDE_INT base_offset;
251   unsigned int reg;
252   BOOL_BITFIELD indirect : 1;  /* 1 if CFA is accessed via a dereference.  */
253   BOOL_BITFIELD in_use : 1;    /* 1 if a saved cfa is stored here.  */
254 } dw_cfa_location;
255
256 /* All call frame descriptions (FDE's) in the GCC generated DWARF
257    refer to a single Common Information Entry (CIE), defined at
258    the beginning of the .debug_frame section.  This use of a single
259    CIE obviates the need to keep track of multiple CIE's
260    in the DWARF generation routines below.  */
261
262 typedef struct GTY(()) dw_fde_struct {
263   tree decl;
264   const char *dw_fde_begin;
265   const char *dw_fde_current_label;
266   const char *dw_fde_end;
267   const char *dw_fde_hot_section_label;
268   const char *dw_fde_hot_section_end_label;
269   const char *dw_fde_unlikely_section_label;
270   const char *dw_fde_unlikely_section_end_label;
271   bool dw_fde_switched_sections;
272   dw_cfi_ref dw_fde_cfi;
273   unsigned funcdef_number;
274   HOST_WIDE_INT stack_realignment;
275   /* Dynamic realign argument pointer register.  */
276   unsigned int drap_reg;
277   /* Virtual dynamic realign argument pointer register.  */
278   unsigned int vdrap_reg;
279   unsigned all_throwers_are_sibcalls : 1;
280   unsigned nothrow : 1;
281   unsigned uses_eh_lsda : 1;
282   /* Whether we did stack realign in this call frame.  */
283   unsigned stack_realign : 1;
284   /* Whether dynamic realign argument pointer register has been saved.  */
285   unsigned drap_reg_saved: 1;
286   /* True iff dw_fde_begin label is in text_section or cold_text_section.  */
287   unsigned in_std_section : 1;
288   /* True iff dw_fde_unlikely_section_label is in text_section or
289      cold_text_section.  */
290   unsigned cold_in_std_section : 1;
291 }
292 dw_fde_node;
293
294 /* Maximum size (in bytes) of an artificially generated label.  */
295 #define MAX_ARTIFICIAL_LABEL_BYTES      30
296
297 /* The size of addresses as they appear in the Dwarf 2 data.
298    Some architectures use word addresses to refer to code locations,
299    but Dwarf 2 info always uses byte addresses.  On such machines,
300    Dwarf 2 addresses need to be larger than the architecture's
301    pointers.  */
302 #ifndef DWARF2_ADDR_SIZE
303 #define DWARF2_ADDR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
304 #endif
305
306 /* The size in bytes of a DWARF field indicating an offset or length
307    relative to a debug info section, specified to be 4 bytes in the
308    DWARF-2 specification.  The SGI/MIPS ABI defines it to be the same
309    as PTR_SIZE.  */
310
311 #ifndef DWARF_OFFSET_SIZE
312 #define DWARF_OFFSET_SIZE 4
313 #endif
314
315 /* According to the (draft) DWARF 3 specification, the initial length
316    should either be 4 or 12 bytes.  When it's 12 bytes, the first 4
317    bytes are 0xffffffff, followed by the length stored in the next 8
318    bytes.
319
320    However, the SGI/MIPS ABI uses an initial length which is equal to
321    DWARF_OFFSET_SIZE.  It is defined (elsewhere) accordingly.  */
322
323 #ifndef DWARF_INITIAL_LENGTH_SIZE
324 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
325 #endif
326
327 /* Round SIZE up to the nearest BOUNDARY.  */
328 #define DWARF_ROUND(SIZE,BOUNDARY) \
329   ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
330
331 /* Offsets recorded in opcodes are a multiple of this alignment factor.  */
332 #ifndef DWARF_CIE_DATA_ALIGNMENT
333 #ifdef STACK_GROWS_DOWNWARD
334 #define DWARF_CIE_DATA_ALIGNMENT (-((int) UNITS_PER_WORD))
335 #else
336 #define DWARF_CIE_DATA_ALIGNMENT ((int) UNITS_PER_WORD)
337 #endif
338 #endif
339
340 /* CIE identifier.  */
341 #if HOST_BITS_PER_WIDE_INT >= 64
342 #define DWARF_CIE_ID \
343   (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
344 #else
345 #define DWARF_CIE_ID DW_CIE_ID
346 #endif
347
348 /* A pointer to the base of a table that contains frame description
349    information for each routine.  */
350 static GTY((length ("fde_table_allocated"))) dw_fde_ref fde_table;
351
352 /* Number of elements currently allocated for fde_table.  */
353 static GTY(()) unsigned fde_table_allocated;
354
355 /* Number of elements in fde_table currently in use.  */
356 static GTY(()) unsigned fde_table_in_use;
357
358 /* Size (in elements) of increments by which we may expand the
359    fde_table.  */
360 #define FDE_TABLE_INCREMENT 256
361
362 /* Get the current fde_table entry we should use.  */
363
364 static inline dw_fde_ref
365 current_fde (void)
366 {
367   return fde_table_in_use ? &fde_table[fde_table_in_use - 1] : NULL;
368 }
369
370 /* A list of call frame insns for the CIE.  */
371 static GTY(()) dw_cfi_ref cie_cfi_head;
372
373 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
374 /* Some DWARF extensions (e.g., MIPS/SGI) implement a subprogram
375    attribute that accelerates the lookup of the FDE associated
376    with the subprogram.  This variable holds the table index of the FDE
377    associated with the current function (body) definition.  */
378 static unsigned current_funcdef_fde;
379 #endif
380
381 struct GTY(()) indirect_string_node {
382   const char *str;
383   unsigned int refcount;
384   enum dwarf_form form;
385   char *label;
386 };
387
388 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
389
390 static GTY(()) int dw2_string_counter;
391 static GTY(()) unsigned long dwarf2out_cfi_label_num;
392
393 /* True if the compilation unit places functions in more than one section.  */
394 static GTY(()) bool have_multiple_function_sections = false;
395
396 /* Whether the default text and cold text sections have been used at all.  */
397
398 static GTY(()) bool text_section_used = false;
399 static GTY(()) bool cold_text_section_used = false;
400
401 /* The default cold text section.  */
402 static GTY(()) section *cold_text_section;
403
404 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
405
406 /* Forward declarations for functions defined in this file.  */
407
408 static char *stripattributes (const char *);
409 static const char *dwarf_cfi_name (unsigned);
410 static dw_cfi_ref new_cfi (void);
411 static void add_cfi (dw_cfi_ref *, dw_cfi_ref);
412 static void add_fde_cfi (const char *, dw_cfi_ref);
413 static void lookup_cfa_1 (dw_cfi_ref, dw_cfa_location *, dw_cfa_location *);
414 static void lookup_cfa (dw_cfa_location *);
415 static void reg_save (const char *, unsigned, unsigned, HOST_WIDE_INT);
416 #ifdef DWARF2_UNWIND_INFO
417 static void initial_return_save (rtx);
418 #endif
419 static HOST_WIDE_INT stack_adjust_offset (const_rtx, HOST_WIDE_INT,
420                                           HOST_WIDE_INT);
421 static void output_cfi (dw_cfi_ref, dw_fde_ref, int);
422 static void output_cfi_directive (dw_cfi_ref);
423 static void output_call_frame_info (int);
424 static void dwarf2out_note_section_used (void);
425 static void dwarf2out_stack_adjust (rtx, bool);
426 static void dwarf2out_args_size_adjust (HOST_WIDE_INT, const char *);
427 static void flush_queued_reg_saves (void);
428 static bool clobbers_queued_reg_save (const_rtx);
429 static void dwarf2out_frame_debug_expr (rtx, const char *);
430
431 /* Support for complex CFA locations.  */
432 static void output_cfa_loc (dw_cfi_ref);
433 static void output_cfa_loc_raw (dw_cfi_ref);
434 static void get_cfa_from_loc_descr (dw_cfa_location *,
435                                     struct dw_loc_descr_struct *);
436 static struct dw_loc_descr_struct *build_cfa_loc
437   (dw_cfa_location *, HOST_WIDE_INT);
438 static struct dw_loc_descr_struct *build_cfa_aligned_loc
439   (HOST_WIDE_INT, HOST_WIDE_INT);
440 static void def_cfa_1 (const char *, dw_cfa_location *);
441
442 /* How to start an assembler comment.  */
443 #ifndef ASM_COMMENT_START
444 #define ASM_COMMENT_START ";#"
445 #endif
446
447 /* Data and reference forms for relocatable data.  */
448 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
449 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
450
451 #ifndef DEBUG_FRAME_SECTION
452 #define DEBUG_FRAME_SECTION     ".debug_frame"
453 #endif
454
455 #ifndef FUNC_BEGIN_LABEL
456 #define FUNC_BEGIN_LABEL        "LFB"
457 #endif
458
459 #ifndef FUNC_END_LABEL
460 #define FUNC_END_LABEL          "LFE"
461 #endif
462
463 #ifndef FRAME_BEGIN_LABEL
464 #define FRAME_BEGIN_LABEL       "Lframe"
465 #endif
466 #define CIE_AFTER_SIZE_LABEL    "LSCIE"
467 #define CIE_END_LABEL           "LECIE"
468 #define FDE_LABEL               "LSFDE"
469 #define FDE_AFTER_SIZE_LABEL    "LASFDE"
470 #define FDE_END_LABEL           "LEFDE"
471 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
472 #define LINE_NUMBER_END_LABEL   "LELT"
473 #define LN_PROLOG_AS_LABEL      "LASLTP"
474 #define LN_PROLOG_END_LABEL     "LELTP"
475 #define DIE_LABEL_PREFIX        "DW"
476
477 /* The DWARF 2 CFA column which tracks the return address.  Normally this
478    is the column for PC, or the first column after all of the hard
479    registers.  */
480 #ifndef DWARF_FRAME_RETURN_COLUMN
481 #ifdef PC_REGNUM
482 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGNUM (PC_REGNUM)
483 #else
484 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGISTERS
485 #endif
486 #endif
487
488 /* The mapping from gcc register number to DWARF 2 CFA column number.  By
489    default, we just provide columns for all registers.  */
490 #ifndef DWARF_FRAME_REGNUM
491 #define DWARF_FRAME_REGNUM(REG) DBX_REGISTER_NUMBER (REG)
492 #endif
493 \f
494 /* Hook used by __throw.  */
495
496 rtx
497 expand_builtin_dwarf_sp_column (void)
498 {
499   unsigned int dwarf_regnum = DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM);
500   return GEN_INT (DWARF2_FRAME_REG_OUT (dwarf_regnum, 1));
501 }
502
503 /* Return a pointer to a copy of the section string name S with all
504    attributes stripped off, and an asterisk prepended (for assemble_name).  */
505
506 static inline char *
507 stripattributes (const char *s)
508 {
509   char *stripped = XNEWVEC (char, strlen (s) + 2);
510   char *p = stripped;
511
512   *p++ = '*';
513
514   while (*s && *s != ',')
515     *p++ = *s++;
516
517   *p = '\0';
518   return stripped;
519 }
520
521 /* MEM is a memory reference for the register size table, each element of
522    which has mode MODE.  Initialize column C as a return address column.  */
523
524 static void
525 init_return_column_size (enum machine_mode mode, rtx mem, unsigned int c)
526 {
527   HOST_WIDE_INT offset = c * GET_MODE_SIZE (mode);
528   HOST_WIDE_INT size = GET_MODE_SIZE (Pmode);
529   emit_move_insn (adjust_address (mem, mode, offset), GEN_INT (size));
530 }
531
532 /* Divide OFF by DWARF_CIE_DATA_ALIGNMENT, asserting no remainder.  */
533
534 static inline HOST_WIDE_INT
535 div_data_align (HOST_WIDE_INT off)
536 {
537   HOST_WIDE_INT r = off / DWARF_CIE_DATA_ALIGNMENT;
538   gcc_assert (r * DWARF_CIE_DATA_ALIGNMENT == off);
539   return r;
540 }
541
542 /* Return true if we need a signed version of a given opcode
543    (e.g. DW_CFA_offset_extended_sf vs DW_CFA_offset_extended).  */
544
545 static inline bool
546 need_data_align_sf_opcode (HOST_WIDE_INT off)
547 {
548   return DWARF_CIE_DATA_ALIGNMENT < 0 ? off > 0 : off < 0;
549 }
550
551 /* Generate code to initialize the register size table.  */
552
553 void
554 expand_builtin_init_dwarf_reg_sizes (tree address)
555 {
556   unsigned int i;
557   enum machine_mode mode = TYPE_MODE (char_type_node);
558   rtx addr = expand_normal (address);
559   rtx mem = gen_rtx_MEM (BLKmode, addr);
560   bool wrote_return_column = false;
561
562   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
563     {
564       int rnum = DWARF2_FRAME_REG_OUT (DWARF_FRAME_REGNUM (i), 1);
565
566       if (rnum < DWARF_FRAME_REGISTERS)
567         {
568           HOST_WIDE_INT offset = rnum * GET_MODE_SIZE (mode);
569           enum machine_mode save_mode = reg_raw_mode[i];
570           HOST_WIDE_INT size;
571
572           if (HARD_REGNO_CALL_PART_CLOBBERED (i, save_mode))
573             save_mode = choose_hard_reg_mode (i, 1, true);
574           if (DWARF_FRAME_REGNUM (i) == DWARF_FRAME_RETURN_COLUMN)
575             {
576               if (save_mode == VOIDmode)
577                 continue;
578               wrote_return_column = true;
579             }
580           size = GET_MODE_SIZE (save_mode);
581           if (offset < 0)
582             continue;
583
584           emit_move_insn (adjust_address (mem, mode, offset),
585                           gen_int_mode (size, mode));
586         }
587     }
588
589   if (!wrote_return_column)
590     init_return_column_size (mode, mem, DWARF_FRAME_RETURN_COLUMN);
591
592 #ifdef DWARF_ALT_FRAME_RETURN_COLUMN
593   init_return_column_size (mode, mem, DWARF_ALT_FRAME_RETURN_COLUMN);
594 #endif
595
596   targetm.init_dwarf_reg_sizes_extra (address);
597 }
598
599 /* Convert a DWARF call frame info. operation to its string name */
600
601 static const char *
602 dwarf_cfi_name (unsigned int cfi_opc)
603 {
604   switch (cfi_opc)
605     {
606     case DW_CFA_advance_loc:
607       return "DW_CFA_advance_loc";
608     case DW_CFA_offset:
609       return "DW_CFA_offset";
610     case DW_CFA_restore:
611       return "DW_CFA_restore";
612     case DW_CFA_nop:
613       return "DW_CFA_nop";
614     case DW_CFA_set_loc:
615       return "DW_CFA_set_loc";
616     case DW_CFA_advance_loc1:
617       return "DW_CFA_advance_loc1";
618     case DW_CFA_advance_loc2:
619       return "DW_CFA_advance_loc2";
620     case DW_CFA_advance_loc4:
621       return "DW_CFA_advance_loc4";
622     case DW_CFA_offset_extended:
623       return "DW_CFA_offset_extended";
624     case DW_CFA_restore_extended:
625       return "DW_CFA_restore_extended";
626     case DW_CFA_undefined:
627       return "DW_CFA_undefined";
628     case DW_CFA_same_value:
629       return "DW_CFA_same_value";
630     case DW_CFA_register:
631       return "DW_CFA_register";
632     case DW_CFA_remember_state:
633       return "DW_CFA_remember_state";
634     case DW_CFA_restore_state:
635       return "DW_CFA_restore_state";
636     case DW_CFA_def_cfa:
637       return "DW_CFA_def_cfa";
638     case DW_CFA_def_cfa_register:
639       return "DW_CFA_def_cfa_register";
640     case DW_CFA_def_cfa_offset:
641       return "DW_CFA_def_cfa_offset";
642
643     /* DWARF 3 */
644     case DW_CFA_def_cfa_expression:
645       return "DW_CFA_def_cfa_expression";
646     case DW_CFA_expression:
647       return "DW_CFA_expression";
648     case DW_CFA_offset_extended_sf:
649       return "DW_CFA_offset_extended_sf";
650     case DW_CFA_def_cfa_sf:
651       return "DW_CFA_def_cfa_sf";
652     case DW_CFA_def_cfa_offset_sf:
653       return "DW_CFA_def_cfa_offset_sf";
654
655     /* SGI/MIPS specific */
656     case DW_CFA_MIPS_advance_loc8:
657       return "DW_CFA_MIPS_advance_loc8";
658
659     /* GNU extensions */
660     case DW_CFA_GNU_window_save:
661       return "DW_CFA_GNU_window_save";
662     case DW_CFA_GNU_args_size:
663       return "DW_CFA_GNU_args_size";
664     case DW_CFA_GNU_negative_offset_extended:
665       return "DW_CFA_GNU_negative_offset_extended";
666
667     default:
668       return "DW_CFA_<unknown>";
669     }
670 }
671
672 /* Return a pointer to a newly allocated Call Frame Instruction.  */
673
674 static inline dw_cfi_ref
675 new_cfi (void)
676 {
677   dw_cfi_ref cfi = GGC_NEW (dw_cfi_node);
678
679   cfi->dw_cfi_next = NULL;
680   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = 0;
681   cfi->dw_cfi_oprnd2.dw_cfi_reg_num = 0;
682
683   return cfi;
684 }
685
686 /* Add a Call Frame Instruction to list of instructions.  */
687
688 static inline void
689 add_cfi (dw_cfi_ref *list_head, dw_cfi_ref cfi)
690 {
691   dw_cfi_ref *p;
692   dw_fde_ref fde = current_fde ();
693
694   /* When DRAP is used, CFA is defined with an expression.  Redefine
695      CFA may lead to a different CFA value.   */
696   /* ??? Of course, this heuristic fails when we're annotating epilogues,
697      because of course we'll always want to redefine the CFA back to the
698      stack pointer on the way out.  Where should we move this check?  */
699   if (0 && fde && fde->drap_reg != INVALID_REGNUM)
700     switch (cfi->dw_cfi_opc)
701       {
702         case DW_CFA_def_cfa_register:
703         case DW_CFA_def_cfa_offset:
704         case DW_CFA_def_cfa_offset_sf:
705         case DW_CFA_def_cfa:
706         case DW_CFA_def_cfa_sf:
707           gcc_unreachable ();
708
709         default:
710           break;
711       }
712
713   /* Find the end of the chain.  */
714   for (p = list_head; (*p) != NULL; p = &(*p)->dw_cfi_next)
715     ;
716
717   *p = cfi;
718 }
719
720 /* Generate a new label for the CFI info to refer to.  FORCE is true
721    if a label needs to be output even when using .cfi_* directives.  */
722
723 char *
724 dwarf2out_cfi_label (bool force)
725 {
726   static char label[20];
727
728   if (!force && dwarf2out_do_cfi_asm ())
729     {
730       /* In this case, we will be emitting the asm directive instead of
731          the label, so just return a placeholder to keep the rest of the
732          interfaces happy.  */
733       strcpy (label, "<do not output>");
734     }
735   else
736     {
737       ASM_GENERATE_INTERNAL_LABEL (label, "LCFI", dwarf2out_cfi_label_num++);
738       ASM_OUTPUT_LABEL (asm_out_file, label);
739     }
740
741   return label;
742 }
743
744 /* True if remember_state should be emitted before following CFI directive.  */
745 static bool emit_cfa_remember;
746
747 /* Add CFI to the current fde at the PC value indicated by LABEL if specified,
748    or to the CIE if LABEL is NULL.  */
749
750 static void
751 add_fde_cfi (const char *label, dw_cfi_ref cfi)
752 {
753   dw_cfi_ref *list_head;
754
755   if (emit_cfa_remember)
756     {
757       dw_cfi_ref cfi_remember;
758
759       /* Emit the state save.  */
760       emit_cfa_remember = false;
761       cfi_remember = new_cfi (); 
762       cfi_remember->dw_cfi_opc = DW_CFA_remember_state;
763       add_fde_cfi (label, cfi_remember);
764     }
765
766   list_head = &cie_cfi_head;
767
768   if (dwarf2out_do_cfi_asm ())
769     {
770       if (label)
771         {
772           dw_fde_ref fde = current_fde ();
773
774           gcc_assert (fde != NULL);
775
776           /* We still have to add the cfi to the list so that lookup_cfa
777              works later on.  When -g2 and above we even need to force
778              emitting of CFI labels and add to list a DW_CFA_set_loc for
779              convert_cfa_to_fb_loc_list purposes.  If we're generating
780              DWARF3 output we use DW_OP_call_frame_cfa and so don't use
781              convert_cfa_to_fb_loc_list.  */
782           if (dwarf_version == 2
783               && debug_info_level > DINFO_LEVEL_TERSE
784               && (write_symbols == DWARF2_DEBUG
785                   || write_symbols == VMS_AND_DWARF2_DEBUG))
786             {
787               switch (cfi->dw_cfi_opc)
788                 {
789                 case DW_CFA_def_cfa_offset:
790                 case DW_CFA_def_cfa_offset_sf:
791                 case DW_CFA_def_cfa_register:
792                 case DW_CFA_def_cfa:
793                 case DW_CFA_def_cfa_sf:
794                 case DW_CFA_def_cfa_expression:
795                 case DW_CFA_restore_state:
796                   if (*label == 0 || strcmp (label, "<do not output>") == 0)
797                     label = dwarf2out_cfi_label (true);
798
799                   if (fde->dw_fde_current_label == NULL
800                       || strcmp (label, fde->dw_fde_current_label) != 0)
801                     {
802                       dw_cfi_ref xcfi;
803
804                       label = xstrdup (label);
805
806                       /* Set the location counter to the new label.  */
807                       xcfi = new_cfi ();
808                       /* It doesn't metter whether DW_CFA_set_loc
809                          or DW_CFA_advance_loc4 is added here, those aren't
810                          emitted into assembly, only looked up by
811                          convert_cfa_to_fb_loc_list.  */
812                       xcfi->dw_cfi_opc = DW_CFA_set_loc;
813                       xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
814                       add_cfi (&fde->dw_fde_cfi, xcfi);
815                       fde->dw_fde_current_label = label;
816                     }
817                   break;
818                 default:
819                   break;
820                 }
821             }
822
823           output_cfi_directive (cfi);
824
825           list_head = &fde->dw_fde_cfi;
826         }
827       /* ??? If this is a CFI for the CIE, we don't emit.  This
828          assumes that the standard CIE contents that the assembler
829          uses matches the standard CIE contents that the compiler
830          uses.  This is probably a bad assumption.  I'm not quite
831          sure how to address this for now.  */
832     }
833   else if (label)
834     {
835       dw_fde_ref fde = current_fde ();
836
837       gcc_assert (fde != NULL);
838
839       if (*label == 0)
840         label = dwarf2out_cfi_label (false);
841
842       if (fde->dw_fde_current_label == NULL
843           || strcmp (label, fde->dw_fde_current_label) != 0)
844         {
845           dw_cfi_ref xcfi;
846
847           label = xstrdup (label);
848
849           /* Set the location counter to the new label.  */
850           xcfi = new_cfi ();
851           /* If we have a current label, advance from there, otherwise
852              set the location directly using set_loc.  */
853           xcfi->dw_cfi_opc = fde->dw_fde_current_label
854                              ? DW_CFA_advance_loc4
855                              : DW_CFA_set_loc;
856           xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
857           add_cfi (&fde->dw_fde_cfi, xcfi);
858
859           fde->dw_fde_current_label = label;
860         }
861
862       list_head = &fde->dw_fde_cfi;
863     }
864
865   add_cfi (list_head, cfi);
866 }
867
868 /* Subroutine of lookup_cfa.  */
869
870 static void
871 lookup_cfa_1 (dw_cfi_ref cfi, dw_cfa_location *loc, dw_cfa_location *remember)
872 {
873   switch (cfi->dw_cfi_opc)
874     {
875     case DW_CFA_def_cfa_offset:
876     case DW_CFA_def_cfa_offset_sf:
877       loc->offset = cfi->dw_cfi_oprnd1.dw_cfi_offset;
878       break;
879     case DW_CFA_def_cfa_register:
880       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
881       break;
882     case DW_CFA_def_cfa:
883     case DW_CFA_def_cfa_sf:
884       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
885       loc->offset = cfi->dw_cfi_oprnd2.dw_cfi_offset;
886       break;
887     case DW_CFA_def_cfa_expression:
888       get_cfa_from_loc_descr (loc, cfi->dw_cfi_oprnd1.dw_cfi_loc);
889       break;
890
891     case DW_CFA_remember_state:
892       gcc_assert (!remember->in_use);
893       *remember = *loc;
894       remember->in_use = 1;
895       break;
896     case DW_CFA_restore_state:
897       gcc_assert (remember->in_use);
898       *loc = *remember;
899       remember->in_use = 0;
900       break;
901
902     default:
903       break;
904     }
905 }
906
907 /* Find the previous value for the CFA.  */
908
909 static void
910 lookup_cfa (dw_cfa_location *loc)
911 {
912   dw_cfi_ref cfi;
913   dw_fde_ref fde;
914   dw_cfa_location remember;
915
916   memset (loc, 0, sizeof (*loc));
917   loc->reg = INVALID_REGNUM;
918   remember = *loc;
919
920   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
921     lookup_cfa_1 (cfi, loc, &remember);
922
923   fde = current_fde ();
924   if (fde)
925     for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
926       lookup_cfa_1 (cfi, loc, &remember);
927 }
928
929 /* The current rule for calculating the DWARF2 canonical frame address.  */
930 static dw_cfa_location cfa;
931
932 /* The register used for saving registers to the stack, and its offset
933    from the CFA.  */
934 static dw_cfa_location cfa_store;
935
936 /* The current save location around an epilogue.  */
937 static dw_cfa_location cfa_remember;
938
939 /* The running total of the size of arguments pushed onto the stack.  */
940 static HOST_WIDE_INT args_size;
941
942 /* The last args_size we actually output.  */
943 static HOST_WIDE_INT old_args_size;
944
945 /* Entry point to update the canonical frame address (CFA).
946    LABEL is passed to add_fde_cfi.  The value of CFA is now to be
947    calculated from REG+OFFSET.  */
948
949 void
950 dwarf2out_def_cfa (const char *label, unsigned int reg, HOST_WIDE_INT offset)
951 {
952   dw_cfa_location loc;
953   loc.indirect = 0;
954   loc.base_offset = 0;
955   loc.reg = reg;
956   loc.offset = offset;
957   def_cfa_1 (label, &loc);
958 }
959
960 /* Determine if two dw_cfa_location structures define the same data.  */
961
962 static bool
963 cfa_equal_p (const dw_cfa_location *loc1, const dw_cfa_location *loc2)
964 {
965   return (loc1->reg == loc2->reg
966           && loc1->offset == loc2->offset
967           && loc1->indirect == loc2->indirect
968           && (loc1->indirect == 0
969               || loc1->base_offset == loc2->base_offset));
970 }
971
972 /* This routine does the actual work.  The CFA is now calculated from
973    the dw_cfa_location structure.  */
974
975 static void
976 def_cfa_1 (const char *label, dw_cfa_location *loc_p)
977 {
978   dw_cfi_ref cfi;
979   dw_cfa_location old_cfa, loc;
980
981   cfa = *loc_p;
982   loc = *loc_p;
983
984   if (cfa_store.reg == loc.reg && loc.indirect == 0)
985     cfa_store.offset = loc.offset;
986
987   loc.reg = DWARF_FRAME_REGNUM (loc.reg);
988   lookup_cfa (&old_cfa);
989
990   /* If nothing changed, no need to issue any call frame instructions.  */
991   if (cfa_equal_p (&loc, &old_cfa))
992     return;
993
994   cfi = new_cfi ();
995
996   if (loc.reg == old_cfa.reg && !loc.indirect)
997     {
998       /* Construct a "DW_CFA_def_cfa_offset <offset>" instruction, indicating
999          the CFA register did not change but the offset did.  The data 
1000          factoring for DW_CFA_def_cfa_offset_sf happens in output_cfi, or
1001          in the assembler via the .cfi_def_cfa_offset directive.  */
1002       if (loc.offset < 0)
1003         cfi->dw_cfi_opc = DW_CFA_def_cfa_offset_sf;
1004       else
1005         cfi->dw_cfi_opc = DW_CFA_def_cfa_offset;
1006       cfi->dw_cfi_oprnd1.dw_cfi_offset = loc.offset;
1007     }
1008
1009 #ifndef MIPS_DEBUGGING_INFO  /* SGI dbx thinks this means no offset.  */
1010   else if (loc.offset == old_cfa.offset
1011            && old_cfa.reg != INVALID_REGNUM
1012            && !loc.indirect)
1013     {
1014       /* Construct a "DW_CFA_def_cfa_register <register>" instruction,
1015          indicating the CFA register has changed to <register> but the
1016          offset has not changed.  */
1017       cfi->dw_cfi_opc = DW_CFA_def_cfa_register;
1018       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
1019     }
1020 #endif
1021
1022   else if (loc.indirect == 0)
1023     {
1024       /* Construct a "DW_CFA_def_cfa <register> <offset>" instruction,
1025          indicating the CFA register has changed to <register> with
1026          the specified offset.  The data factoring for DW_CFA_def_cfa_sf
1027          happens in output_cfi, or in the assembler via the .cfi_def_cfa
1028          directive.  */
1029       if (loc.offset < 0)
1030         cfi->dw_cfi_opc = DW_CFA_def_cfa_sf;
1031       else
1032         cfi->dw_cfi_opc = DW_CFA_def_cfa;
1033       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
1034       cfi->dw_cfi_oprnd2.dw_cfi_offset = loc.offset;
1035     }
1036   else
1037     {
1038       /* Construct a DW_CFA_def_cfa_expression instruction to
1039          calculate the CFA using a full location expression since no
1040          register-offset pair is available.  */
1041       struct dw_loc_descr_struct *loc_list;
1042
1043       cfi->dw_cfi_opc = DW_CFA_def_cfa_expression;
1044       loc_list = build_cfa_loc (&loc, 0);
1045       cfi->dw_cfi_oprnd1.dw_cfi_loc = loc_list;
1046     }
1047
1048   add_fde_cfi (label, cfi);
1049 }
1050
1051 /* Add the CFI for saving a register.  REG is the CFA column number.
1052    LABEL is passed to add_fde_cfi.
1053    If SREG is -1, the register is saved at OFFSET from the CFA;
1054    otherwise it is saved in SREG.  */
1055
1056 static void
1057 reg_save (const char *label, unsigned int reg, unsigned int sreg, HOST_WIDE_INT offset)
1058 {
1059   dw_cfi_ref cfi = new_cfi ();
1060   dw_fde_ref fde = current_fde ();
1061
1062   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
1063
1064   /* When stack is aligned, store REG using DW_CFA_expression with
1065      FP.  */
1066   if (fde
1067       && fde->stack_realign
1068       && sreg == INVALID_REGNUM)
1069     {
1070       cfi->dw_cfi_opc = DW_CFA_expression;
1071       cfi->dw_cfi_oprnd2.dw_cfi_reg_num = reg;
1072       cfi->dw_cfi_oprnd1.dw_cfi_loc
1073         = build_cfa_aligned_loc (offset, fde->stack_realignment);
1074     }
1075   else if (sreg == INVALID_REGNUM)
1076     {
1077       if (need_data_align_sf_opcode (offset))
1078         cfi->dw_cfi_opc = DW_CFA_offset_extended_sf;
1079       else if (reg & ~0x3f)
1080         cfi->dw_cfi_opc = DW_CFA_offset_extended;
1081       else
1082         cfi->dw_cfi_opc = DW_CFA_offset;
1083       cfi->dw_cfi_oprnd2.dw_cfi_offset = offset;
1084     }
1085   else if (sreg == reg)
1086     cfi->dw_cfi_opc = DW_CFA_same_value;
1087   else
1088     {
1089       cfi->dw_cfi_opc = DW_CFA_register;
1090       cfi->dw_cfi_oprnd2.dw_cfi_reg_num = sreg;
1091     }
1092
1093   add_fde_cfi (label, cfi);
1094 }
1095
1096 /* Add the CFI for saving a register window.  LABEL is passed to reg_save.
1097    This CFI tells the unwinder that it needs to restore the window registers
1098    from the previous frame's window save area.
1099
1100    ??? Perhaps we should note in the CIE where windows are saved (instead of
1101    assuming 0(cfa)) and what registers are in the window.  */
1102
1103 void
1104 dwarf2out_window_save (const char *label)
1105 {
1106   dw_cfi_ref cfi = new_cfi ();
1107
1108   cfi->dw_cfi_opc = DW_CFA_GNU_window_save;
1109   add_fde_cfi (label, cfi);
1110 }
1111
1112 /* Add a CFI to update the running total of the size of arguments
1113    pushed onto the stack.  */
1114
1115 void
1116 dwarf2out_args_size (const char *label, HOST_WIDE_INT size)
1117 {
1118   dw_cfi_ref cfi;
1119
1120   if (size == old_args_size)
1121     return;
1122
1123   old_args_size = size;
1124
1125   cfi = new_cfi ();
1126   cfi->dw_cfi_opc = DW_CFA_GNU_args_size;
1127   cfi->dw_cfi_oprnd1.dw_cfi_offset = size;
1128   add_fde_cfi (label, cfi);
1129 }
1130
1131 /* Entry point for saving a register to the stack.  REG is the GCC register
1132    number.  LABEL and OFFSET are passed to reg_save.  */
1133
1134 void
1135 dwarf2out_reg_save (const char *label, unsigned int reg, HOST_WIDE_INT offset)
1136 {
1137   reg_save (label, DWARF_FRAME_REGNUM (reg), INVALID_REGNUM, offset);
1138 }
1139
1140 /* Entry point for saving the return address in the stack.
1141    LABEL and OFFSET are passed to reg_save.  */
1142
1143 void
1144 dwarf2out_return_save (const char *label, HOST_WIDE_INT offset)
1145 {
1146   reg_save (label, DWARF_FRAME_RETURN_COLUMN, INVALID_REGNUM, offset);
1147 }
1148
1149 /* Entry point for saving the return address in a register.
1150    LABEL and SREG are passed to reg_save.  */
1151
1152 void
1153 dwarf2out_return_reg (const char *label, unsigned int sreg)
1154 {
1155   reg_save (label, DWARF_FRAME_RETURN_COLUMN, DWARF_FRAME_REGNUM (sreg), 0);
1156 }
1157
1158 #ifdef DWARF2_UNWIND_INFO
1159 /* Record the initial position of the return address.  RTL is
1160    INCOMING_RETURN_ADDR_RTX.  */
1161
1162 static void
1163 initial_return_save (rtx rtl)
1164 {
1165   unsigned int reg = INVALID_REGNUM;
1166   HOST_WIDE_INT offset = 0;
1167
1168   switch (GET_CODE (rtl))
1169     {
1170     case REG:
1171       /* RA is in a register.  */
1172       reg = DWARF_FRAME_REGNUM (REGNO (rtl));
1173       break;
1174
1175     case MEM:
1176       /* RA is on the stack.  */
1177       rtl = XEXP (rtl, 0);
1178       switch (GET_CODE (rtl))
1179         {
1180         case REG:
1181           gcc_assert (REGNO (rtl) == STACK_POINTER_REGNUM);
1182           offset = 0;
1183           break;
1184
1185         case PLUS:
1186           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1187           offset = INTVAL (XEXP (rtl, 1));
1188           break;
1189
1190         case MINUS:
1191           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1192           offset = -INTVAL (XEXP (rtl, 1));
1193           break;
1194
1195         default:
1196           gcc_unreachable ();
1197         }
1198
1199       break;
1200
1201     case PLUS:
1202       /* The return address is at some offset from any value we can
1203          actually load.  For instance, on the SPARC it is in %i7+8. Just
1204          ignore the offset for now; it doesn't matter for unwinding frames.  */
1205       gcc_assert (CONST_INT_P (XEXP (rtl, 1)));
1206       initial_return_save (XEXP (rtl, 0));
1207       return;
1208
1209     default:
1210       gcc_unreachable ();
1211     }
1212
1213   if (reg != DWARF_FRAME_RETURN_COLUMN)
1214     reg_save (NULL, DWARF_FRAME_RETURN_COLUMN, reg, offset - cfa.offset);
1215 }
1216 #endif
1217
1218 /* Given a SET, calculate the amount of stack adjustment it
1219    contains.  */
1220
1221 static HOST_WIDE_INT
1222 stack_adjust_offset (const_rtx pattern, HOST_WIDE_INT cur_args_size,
1223                      HOST_WIDE_INT cur_offset)
1224 {
1225   const_rtx src = SET_SRC (pattern);
1226   const_rtx dest = SET_DEST (pattern);
1227   HOST_WIDE_INT offset = 0;
1228   enum rtx_code code;
1229
1230   if (dest == stack_pointer_rtx)
1231     {
1232       code = GET_CODE (src);
1233
1234       /* Assume (set (reg sp) (reg whatever)) sets args_size
1235          level to 0.  */
1236       if (code == REG && src != stack_pointer_rtx)
1237         {
1238           offset = -cur_args_size;
1239 #ifndef STACK_GROWS_DOWNWARD
1240           offset = -offset;
1241 #endif
1242           return offset - cur_offset;
1243         }
1244
1245       if (! (code == PLUS || code == MINUS)
1246           || XEXP (src, 0) != stack_pointer_rtx
1247           || !CONST_INT_P (XEXP (src, 1)))
1248         return 0;
1249
1250       /* (set (reg sp) (plus (reg sp) (const_int))) */
1251       offset = INTVAL (XEXP (src, 1));
1252       if (code == PLUS)
1253         offset = -offset;
1254       return offset;
1255     }
1256
1257   if (MEM_P (src) && !MEM_P (dest))
1258     dest = src;
1259   if (MEM_P (dest))
1260     {
1261       /* (set (mem (pre_dec (reg sp))) (foo)) */
1262       src = XEXP (dest, 0);
1263       code = GET_CODE (src);
1264
1265       switch (code)
1266         {
1267         case PRE_MODIFY:
1268         case POST_MODIFY:
1269           if (XEXP (src, 0) == stack_pointer_rtx)
1270             {
1271               rtx val = XEXP (XEXP (src, 1), 1);
1272               /* We handle only adjustments by constant amount.  */
1273               gcc_assert (GET_CODE (XEXP (src, 1)) == PLUS
1274                           && CONST_INT_P (val));
1275               offset = -INTVAL (val);
1276               break;
1277             }
1278           return 0;
1279
1280         case PRE_DEC:
1281         case POST_DEC:
1282           if (XEXP (src, 0) == stack_pointer_rtx)
1283             {
1284               offset = GET_MODE_SIZE (GET_MODE (dest));
1285               break;
1286             }
1287           return 0;
1288
1289         case PRE_INC:
1290         case POST_INC:
1291           if (XEXP (src, 0) == stack_pointer_rtx)
1292             {
1293               offset = -GET_MODE_SIZE (GET_MODE (dest));
1294               break;
1295             }
1296           return 0;
1297
1298         default:
1299           return 0;
1300         }
1301     }
1302   else
1303     return 0;
1304
1305   return offset;
1306 }
1307
1308 /* Precomputed args_size for CODE_LABELs and BARRIERs preceeding them,
1309    indexed by INSN_UID.  */
1310
1311 static HOST_WIDE_INT *barrier_args_size;
1312
1313 /* Helper function for compute_barrier_args_size.  Handle one insn.  */
1314
1315 static HOST_WIDE_INT
1316 compute_barrier_args_size_1 (rtx insn, HOST_WIDE_INT cur_args_size,
1317                              VEC (rtx, heap) **next)
1318 {
1319   HOST_WIDE_INT offset = 0;
1320   int i;
1321
1322   if (! RTX_FRAME_RELATED_P (insn))
1323     {
1324       if (prologue_epilogue_contains (insn))
1325         /* Nothing */;
1326       else if (GET_CODE (PATTERN (insn)) == SET)
1327         offset = stack_adjust_offset (PATTERN (insn), cur_args_size, 0);
1328       else if (GET_CODE (PATTERN (insn)) == PARALLEL
1329                || GET_CODE (PATTERN (insn)) == SEQUENCE)
1330         {
1331           /* There may be stack adjustments inside compound insns.  Search
1332              for them.  */
1333           for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1334             if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1335               offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i),
1336                                              cur_args_size, offset);
1337         }
1338     }
1339   else
1340     {
1341       rtx expr = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
1342
1343       if (expr)
1344         {
1345           expr = XEXP (expr, 0);
1346           if (GET_CODE (expr) == PARALLEL
1347               || GET_CODE (expr) == SEQUENCE)
1348             for (i = 1; i < XVECLEN (expr, 0); i++)
1349               {
1350                 rtx elem = XVECEXP (expr, 0, i);
1351
1352                 if (GET_CODE (elem) == SET && !RTX_FRAME_RELATED_P (elem))
1353                   offset += stack_adjust_offset (elem, cur_args_size, offset);
1354               }
1355         }
1356     }
1357
1358 #ifndef STACK_GROWS_DOWNWARD
1359   offset = -offset;
1360 #endif
1361
1362   cur_args_size += offset;
1363   if (cur_args_size < 0)
1364     cur_args_size = 0;
1365
1366   if (JUMP_P (insn))
1367     {
1368       rtx dest = JUMP_LABEL (insn);
1369
1370       if (dest)
1371         {
1372           if (barrier_args_size [INSN_UID (dest)] < 0)
1373             {
1374               barrier_args_size [INSN_UID (dest)] = cur_args_size;
1375               VEC_safe_push (rtx, heap, *next, dest);
1376             }
1377         }
1378     }
1379
1380   return cur_args_size;
1381 }
1382
1383 /* Walk the whole function and compute args_size on BARRIERs.  */
1384
1385 static void
1386 compute_barrier_args_size (void)
1387 {
1388   int max_uid = get_max_uid (), i;
1389   rtx insn;
1390   VEC (rtx, heap) *worklist, *next, *tmp;
1391
1392   barrier_args_size = XNEWVEC (HOST_WIDE_INT, max_uid);
1393   for (i = 0; i < max_uid; i++)
1394     barrier_args_size[i] = -1;
1395
1396   worklist = VEC_alloc (rtx, heap, 20);
1397   next = VEC_alloc (rtx, heap, 20);
1398   insn = get_insns ();
1399   barrier_args_size[INSN_UID (insn)] = 0;
1400   VEC_quick_push (rtx, worklist, insn);
1401   for (;;)
1402     {
1403       while (!VEC_empty (rtx, worklist))
1404         {
1405           rtx prev, body, first_insn;
1406           HOST_WIDE_INT cur_args_size;
1407
1408           first_insn = insn = VEC_pop (rtx, worklist);
1409           cur_args_size = barrier_args_size[INSN_UID (insn)];
1410           prev = prev_nonnote_insn (insn);
1411           if (prev && BARRIER_P (prev))
1412             barrier_args_size[INSN_UID (prev)] = cur_args_size;
1413
1414           for (; insn; insn = NEXT_INSN (insn))
1415             {
1416               if (INSN_DELETED_P (insn) || NOTE_P (insn))
1417                 continue;
1418               if (BARRIER_P (insn))
1419                 break;
1420
1421               if (LABEL_P (insn))
1422                 {
1423                   if (insn == first_insn)
1424                     continue;
1425                   else if (barrier_args_size[INSN_UID (insn)] < 0)
1426                     {
1427                       barrier_args_size[INSN_UID (insn)] = cur_args_size;
1428                       continue;
1429                     }
1430                   else
1431                     {
1432                       /* The insns starting with this label have been
1433                          already scanned or are in the worklist.  */
1434                       break;
1435                     }
1436                 }
1437
1438               body = PATTERN (insn);
1439               if (GET_CODE (body) == SEQUENCE)
1440                 {
1441                   HOST_WIDE_INT dest_args_size = cur_args_size;
1442                   for (i = 1; i < XVECLEN (body, 0); i++)
1443                     if (INSN_ANNULLED_BRANCH_P (XVECEXP (body, 0, 0))
1444                         && INSN_FROM_TARGET_P (XVECEXP (body, 0, i)))
1445                       dest_args_size
1446                         = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1447                                                        dest_args_size, &next);
1448                     else
1449                       cur_args_size
1450                         = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1451                                                        cur_args_size, &next);
1452
1453                   if (INSN_ANNULLED_BRANCH_P (XVECEXP (body, 0, 0)))
1454                     compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1455                                                  dest_args_size, &next);
1456                   else
1457                     cur_args_size
1458                       = compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1459                                                      cur_args_size, &next);
1460                 }
1461               else
1462                 cur_args_size
1463                   = compute_barrier_args_size_1 (insn, cur_args_size, &next);
1464             }
1465         }
1466
1467       if (VEC_empty (rtx, next))
1468         break;
1469
1470       /* Swap WORKLIST with NEXT and truncate NEXT for next iteration.  */
1471       tmp = next;
1472       next = worklist;
1473       worklist = tmp;
1474       VEC_truncate (rtx, next, 0);
1475     }
1476
1477   VEC_free (rtx, heap, worklist);
1478   VEC_free (rtx, heap, next);
1479 }
1480
1481
1482 /* Check INSN to see if it looks like a push or a stack adjustment, and
1483    make a note of it if it does.  EH uses this information to find out how
1484    much extra space it needs to pop off the stack.  */
1485
1486 static void
1487 dwarf2out_stack_adjust (rtx insn, bool after_p)
1488 {
1489   HOST_WIDE_INT offset;
1490   const char *label;
1491   int i;
1492
1493   /* Don't handle epilogues at all.  Certainly it would be wrong to do so
1494      with this function.  Proper support would require all frame-related
1495      insns to be marked, and to be able to handle saving state around
1496      epilogues textually in the middle of the function.  */
1497   if (prologue_epilogue_contains (insn))
1498     return;
1499
1500   /* If INSN is an instruction from target of an annulled branch, the
1501      effects are for the target only and so current argument size
1502      shouldn't change at all.  */
1503   if (final_sequence
1504       && INSN_ANNULLED_BRANCH_P (XVECEXP (final_sequence, 0, 0))
1505       && INSN_FROM_TARGET_P (insn))
1506     return;
1507
1508   /* If only calls can throw, and we have a frame pointer,
1509      save up adjustments until we see the CALL_INSN.  */
1510   if (!flag_asynchronous_unwind_tables && cfa.reg != STACK_POINTER_REGNUM)
1511     {
1512       if (CALL_P (insn) && !after_p)
1513         {
1514           /* Extract the size of the args from the CALL rtx itself.  */
1515           insn = PATTERN (insn);
1516           if (GET_CODE (insn) == PARALLEL)
1517             insn = XVECEXP (insn, 0, 0);
1518           if (GET_CODE (insn) == SET)
1519             insn = SET_SRC (insn);
1520           gcc_assert (GET_CODE (insn) == CALL);
1521           dwarf2out_args_size ("", INTVAL (XEXP (insn, 1)));
1522         }
1523       return;
1524     }
1525
1526   if (CALL_P (insn) && !after_p)
1527     {
1528       if (!flag_asynchronous_unwind_tables)
1529         dwarf2out_args_size ("", args_size);
1530       return;
1531     }
1532   else if (BARRIER_P (insn))
1533     {
1534       /* Don't call compute_barrier_args_size () if the only
1535          BARRIER is at the end of function.  */
1536       if (barrier_args_size == NULL && next_nonnote_insn (insn))
1537         compute_barrier_args_size ();
1538       if (barrier_args_size == NULL)
1539         offset = 0;
1540       else
1541         {
1542           offset = barrier_args_size[INSN_UID (insn)];
1543           if (offset < 0)
1544             offset = 0;
1545         }
1546
1547       offset -= args_size;
1548 #ifndef STACK_GROWS_DOWNWARD
1549       offset = -offset;
1550 #endif
1551     }
1552   else if (GET_CODE (PATTERN (insn)) == SET)
1553     offset = stack_adjust_offset (PATTERN (insn), args_size, 0);
1554   else if (GET_CODE (PATTERN (insn)) == PARALLEL
1555            || GET_CODE (PATTERN (insn)) == SEQUENCE)
1556     {
1557       /* There may be stack adjustments inside compound insns.  Search
1558          for them.  */
1559       for (offset = 0, i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1560         if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1561           offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i),
1562                                          args_size, offset);
1563     }
1564   else
1565     return;
1566
1567   if (offset == 0)
1568     return;
1569
1570   label = dwarf2out_cfi_label (false);
1571   dwarf2out_args_size_adjust (offset, label);
1572 }
1573
1574 /* Adjust args_size based on stack adjustment OFFSET.  */
1575
1576 static void
1577 dwarf2out_args_size_adjust (HOST_WIDE_INT offset, const char *label)
1578 {
1579   if (cfa.reg == STACK_POINTER_REGNUM)
1580     cfa.offset += offset;
1581
1582   if (cfa_store.reg == STACK_POINTER_REGNUM)
1583     cfa_store.offset += offset;
1584
1585 #ifndef STACK_GROWS_DOWNWARD
1586   offset = -offset;
1587 #endif
1588
1589   args_size += offset;
1590   if (args_size < 0)
1591     args_size = 0;
1592
1593   def_cfa_1 (label, &cfa);
1594   if (flag_asynchronous_unwind_tables)
1595     dwarf2out_args_size (label, args_size);
1596 }
1597
1598 #endif
1599
1600 /* We delay emitting a register save until either (a) we reach the end
1601    of the prologue or (b) the register is clobbered.  This clusters
1602    register saves so that there are fewer pc advances.  */
1603
1604 struct GTY(()) queued_reg_save {
1605   struct queued_reg_save *next;
1606   rtx reg;
1607   HOST_WIDE_INT cfa_offset;
1608   rtx saved_reg;
1609 };
1610
1611 static GTY(()) struct queued_reg_save *queued_reg_saves;
1612
1613 /* The caller's ORIG_REG is saved in SAVED_IN_REG.  */
1614 struct GTY(()) reg_saved_in_data {
1615   rtx orig_reg;
1616   rtx saved_in_reg;
1617 };
1618
1619 /* A list of registers saved in other registers.
1620    The list intentionally has a small maximum capacity of 4; if your
1621    port needs more than that, you might consider implementing a
1622    more efficient data structure.  */
1623 static GTY(()) struct reg_saved_in_data regs_saved_in_regs[4];
1624 static GTY(()) size_t num_regs_saved_in_regs;
1625
1626 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
1627 static const char *last_reg_save_label;
1628
1629 /* Add an entry to QUEUED_REG_SAVES saying that REG is now saved at
1630    SREG, or if SREG is NULL then it is saved at OFFSET to the CFA.  */
1631
1632 static void
1633 queue_reg_save (const char *label, rtx reg, rtx sreg, HOST_WIDE_INT offset)
1634 {
1635   struct queued_reg_save *q;
1636
1637   /* Duplicates waste space, but it's also necessary to remove them
1638      for correctness, since the queue gets output in reverse
1639      order.  */
1640   for (q = queued_reg_saves; q != NULL; q = q->next)
1641     if (REGNO (q->reg) == REGNO (reg))
1642       break;
1643
1644   if (q == NULL)
1645     {
1646       q = GGC_NEW (struct queued_reg_save);
1647       q->next = queued_reg_saves;
1648       queued_reg_saves = q;
1649     }
1650
1651   q->reg = reg;
1652   q->cfa_offset = offset;
1653   q->saved_reg = sreg;
1654
1655   last_reg_save_label = label;
1656 }
1657
1658 /* Output all the entries in QUEUED_REG_SAVES.  */
1659
1660 static void
1661 flush_queued_reg_saves (void)
1662 {
1663   struct queued_reg_save *q;
1664
1665   for (q = queued_reg_saves; q; q = q->next)
1666     {
1667       size_t i;
1668       unsigned int reg, sreg;
1669
1670       for (i = 0; i < num_regs_saved_in_regs; i++)
1671         if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (q->reg))
1672           break;
1673       if (q->saved_reg && i == num_regs_saved_in_regs)
1674         {
1675           gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1676           num_regs_saved_in_regs++;
1677         }
1678       if (i != num_regs_saved_in_regs)
1679         {
1680           regs_saved_in_regs[i].orig_reg = q->reg;
1681           regs_saved_in_regs[i].saved_in_reg = q->saved_reg;
1682         }
1683
1684       reg = DWARF_FRAME_REGNUM (REGNO (q->reg));
1685       if (q->saved_reg)
1686         sreg = DWARF_FRAME_REGNUM (REGNO (q->saved_reg));
1687       else
1688         sreg = INVALID_REGNUM;
1689       reg_save (last_reg_save_label, reg, sreg, q->cfa_offset);
1690     }
1691
1692   queued_reg_saves = NULL;
1693   last_reg_save_label = NULL;
1694 }
1695
1696 /* Does INSN clobber any register which QUEUED_REG_SAVES lists a saved
1697    location for?  Or, does it clobber a register which we've previously
1698    said that some other register is saved in, and for which we now
1699    have a new location for?  */
1700
1701 static bool
1702 clobbers_queued_reg_save (const_rtx insn)
1703 {
1704   struct queued_reg_save *q;
1705
1706   for (q = queued_reg_saves; q; q = q->next)
1707     {
1708       size_t i;
1709       if (modified_in_p (q->reg, insn))
1710         return true;
1711       for (i = 0; i < num_regs_saved_in_regs; i++)
1712         if (REGNO (q->reg) == REGNO (regs_saved_in_regs[i].orig_reg)
1713             && modified_in_p (regs_saved_in_regs[i].saved_in_reg, insn))
1714           return true;
1715     }
1716
1717   return false;
1718 }
1719
1720 /* Entry point for saving the first register into the second.  */
1721
1722 void
1723 dwarf2out_reg_save_reg (const char *label, rtx reg, rtx sreg)
1724 {
1725   size_t i;
1726   unsigned int regno, sregno;
1727
1728   for (i = 0; i < num_regs_saved_in_regs; i++)
1729     if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (reg))
1730       break;
1731   if (i == num_regs_saved_in_regs)
1732     {
1733       gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1734       num_regs_saved_in_regs++;
1735     }
1736   regs_saved_in_regs[i].orig_reg = reg;
1737   regs_saved_in_regs[i].saved_in_reg = sreg;
1738
1739   regno = DWARF_FRAME_REGNUM (REGNO (reg));
1740   sregno = DWARF_FRAME_REGNUM (REGNO (sreg));
1741   reg_save (label, regno, sregno, 0);
1742 }
1743
1744 /* What register, if any, is currently saved in REG?  */
1745
1746 static rtx
1747 reg_saved_in (rtx reg)
1748 {
1749   unsigned int regn = REGNO (reg);
1750   size_t i;
1751   struct queued_reg_save *q;
1752
1753   for (q = queued_reg_saves; q; q = q->next)
1754     if (q->saved_reg && regn == REGNO (q->saved_reg))
1755       return q->reg;
1756
1757   for (i = 0; i < num_regs_saved_in_regs; i++)
1758     if (regs_saved_in_regs[i].saved_in_reg
1759         && regn == REGNO (regs_saved_in_regs[i].saved_in_reg))
1760       return regs_saved_in_regs[i].orig_reg;
1761
1762   return NULL_RTX;
1763 }
1764
1765
1766 /* A temporary register holding an integral value used in adjusting SP
1767    or setting up the store_reg.  The "offset" field holds the integer
1768    value, not an offset.  */
1769 static dw_cfa_location cfa_temp;
1770
1771 /* A subroutine of dwarf2out_frame_debug, process a REG_DEF_CFA note.  */
1772
1773 static void
1774 dwarf2out_frame_debug_def_cfa (rtx pat, const char *label)
1775 {
1776   memset (&cfa, 0, sizeof (cfa));
1777
1778   switch (GET_CODE (pat))
1779     {
1780     case PLUS:
1781       cfa.reg = REGNO (XEXP (pat, 0));
1782       cfa.offset = INTVAL (XEXP (pat, 1));
1783       break;
1784
1785     case REG:
1786       cfa.reg = REGNO (pat);
1787       break;
1788
1789     default:
1790       /* Recurse and define an expression.  */
1791       gcc_unreachable ();
1792     }
1793
1794   def_cfa_1 (label, &cfa);
1795 }
1796
1797 /* A subroutine of dwarf2out_frame_debug, process a REG_ADJUST_CFA note.  */
1798
1799 static void
1800 dwarf2out_frame_debug_adjust_cfa (rtx pat, const char *label)
1801 {
1802   rtx src, dest;
1803
1804   gcc_assert (GET_CODE (pat) == SET);
1805   dest = XEXP (pat, 0);
1806   src = XEXP (pat, 1);
1807
1808   switch (GET_CODE (src))
1809     {
1810     case PLUS:
1811       gcc_assert (REGNO (XEXP (src, 0)) == cfa.reg);
1812       cfa.offset -= INTVAL (XEXP (src, 1));
1813       break;
1814
1815     case REG:
1816         break;
1817
1818     default:
1819         gcc_unreachable ();
1820     }
1821
1822   cfa.reg = REGNO (dest);
1823   gcc_assert (cfa.indirect == 0);
1824
1825   def_cfa_1 (label, &cfa);
1826 }
1827
1828 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_OFFSET note.  */
1829
1830 static void
1831 dwarf2out_frame_debug_cfa_offset (rtx set, const char *label)
1832 {
1833   HOST_WIDE_INT offset;
1834   rtx src, addr, span;
1835
1836   src = XEXP (set, 1);
1837   addr = XEXP (set, 0);
1838   gcc_assert (MEM_P (addr));
1839   addr = XEXP (addr, 0);
1840   
1841   /* As documented, only consider extremely simple addresses.  */
1842   switch (GET_CODE (addr))
1843     {
1844     case REG:
1845       gcc_assert (REGNO (addr) == cfa.reg);
1846       offset = -cfa.offset;
1847       break;
1848     case PLUS:
1849       gcc_assert (REGNO (XEXP (addr, 0)) == cfa.reg);
1850       offset = INTVAL (XEXP (addr, 1)) - cfa.offset;
1851       break;
1852     default:
1853       gcc_unreachable ();
1854     }
1855
1856   span = targetm.dwarf_register_span (src);
1857
1858   /* ??? We'd like to use queue_reg_save, but we need to come up with
1859      a different flushing heuristic for epilogues.  */
1860   if (!span)
1861     reg_save (label, DWARF_FRAME_REGNUM (REGNO (src)), INVALID_REGNUM, offset);
1862   else
1863     {
1864       /* We have a PARALLEL describing where the contents of SRC live.
1865          Queue register saves for each piece of the PARALLEL.  */
1866       int par_index;
1867       int limit;
1868       HOST_WIDE_INT span_offset = offset;
1869
1870       gcc_assert (GET_CODE (span) == PARALLEL);
1871
1872       limit = XVECLEN (span, 0);
1873       for (par_index = 0; par_index < limit; par_index++)
1874         {
1875           rtx elem = XVECEXP (span, 0, par_index);
1876
1877           reg_save (label, DWARF_FRAME_REGNUM (REGNO (elem)),
1878                     INVALID_REGNUM, span_offset);
1879           span_offset += GET_MODE_SIZE (GET_MODE (elem));
1880         }
1881     }
1882 }
1883
1884 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_REGISTER note.  */
1885
1886 static void
1887 dwarf2out_frame_debug_cfa_register (rtx set, const char *label)
1888 {
1889   rtx src, dest;
1890   unsigned sregno, dregno;
1891
1892   src = XEXP (set, 1);
1893   dest = XEXP (set, 0);
1894
1895   if (src == pc_rtx)
1896     sregno = DWARF_FRAME_RETURN_COLUMN;
1897   else
1898     sregno = DWARF_FRAME_REGNUM (REGNO (src));
1899
1900   dregno = DWARF_FRAME_REGNUM (REGNO (dest));
1901
1902   /* ??? We'd like to use queue_reg_save, but we need to come up with
1903      a different flushing heuristic for epilogues.  */
1904   reg_save (label, sregno, dregno, 0);
1905 }
1906
1907 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_RESTORE note.  */
1908
1909 static void
1910 dwarf2out_frame_debug_cfa_restore (rtx reg, const char *label)
1911 {
1912   dw_cfi_ref cfi = new_cfi ();
1913   unsigned int regno = DWARF_FRAME_REGNUM (REGNO (reg));
1914
1915   cfi->dw_cfi_opc = (regno & ~0x3f ? DW_CFA_restore_extended : DW_CFA_restore);
1916   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = regno;
1917
1918   add_fde_cfi (label, cfi);
1919 }
1920
1921 /* Record call frame debugging information for an expression EXPR,
1922    which either sets SP or FP (adjusting how we calculate the frame
1923    address) or saves a register to the stack or another register.
1924    LABEL indicates the address of EXPR.
1925
1926    This function encodes a state machine mapping rtxes to actions on
1927    cfa, cfa_store, and cfa_temp.reg.  We describe these rules so
1928    users need not read the source code.
1929
1930   The High-Level Picture
1931
1932   Changes in the register we use to calculate the CFA: Currently we
1933   assume that if you copy the CFA register into another register, we
1934   should take the other one as the new CFA register; this seems to
1935   work pretty well.  If it's wrong for some target, it's simple
1936   enough not to set RTX_FRAME_RELATED_P on the insn in question.
1937
1938   Changes in the register we use for saving registers to the stack:
1939   This is usually SP, but not always.  Again, we deduce that if you
1940   copy SP into another register (and SP is not the CFA register),
1941   then the new register is the one we will be using for register
1942   saves.  This also seems to work.
1943
1944   Register saves: There's not much guesswork about this one; if
1945   RTX_FRAME_RELATED_P is set on an insn which modifies memory, it's a
1946   register save, and the register used to calculate the destination
1947   had better be the one we think we're using for this purpose.
1948   It's also assumed that a copy from a call-saved register to another
1949   register is saving that register if RTX_FRAME_RELATED_P is set on
1950   that instruction.  If the copy is from a call-saved register to
1951   the *same* register, that means that the register is now the same
1952   value as in the caller.
1953
1954   Except: If the register being saved is the CFA register, and the
1955   offset is nonzero, we are saving the CFA, so we assume we have to
1956   use DW_CFA_def_cfa_expression.  If the offset is 0, we assume that
1957   the intent is to save the value of SP from the previous frame.
1958
1959   In addition, if a register has previously been saved to a different
1960   register,
1961
1962   Invariants / Summaries of Rules
1963
1964   cfa          current rule for calculating the CFA.  It usually
1965                consists of a register and an offset.
1966   cfa_store    register used by prologue code to save things to the stack
1967                cfa_store.offset is the offset from the value of
1968                cfa_store.reg to the actual CFA
1969   cfa_temp     register holding an integral value.  cfa_temp.offset
1970                stores the value, which will be used to adjust the
1971                stack pointer.  cfa_temp is also used like cfa_store,
1972                to track stores to the stack via fp or a temp reg.
1973
1974   Rules  1- 4: Setting a register's value to cfa.reg or an expression
1975                with cfa.reg as the first operand changes the cfa.reg and its
1976                cfa.offset.  Rule 1 and 4 also set cfa_temp.reg and
1977                cfa_temp.offset.
1978
1979   Rules  6- 9: Set a non-cfa.reg register value to a constant or an
1980                expression yielding a constant.  This sets cfa_temp.reg
1981                and cfa_temp.offset.
1982
1983   Rule 5:      Create a new register cfa_store used to save items to the
1984                stack.
1985
1986   Rules 10-14: Save a register to the stack.  Define offset as the
1987                difference of the original location and cfa_store's
1988                location (or cfa_temp's location if cfa_temp is used).
1989
1990   Rules 16-20: If AND operation happens on sp in prologue, we assume
1991                stack is realigned.  We will use a group of DW_OP_XXX
1992                expressions to represent the location of the stored
1993                register instead of CFA+offset.
1994
1995   The Rules
1996
1997   "{a,b}" indicates a choice of a xor b.
1998   "<reg>:cfa.reg" indicates that <reg> must equal cfa.reg.
1999
2000   Rule 1:
2001   (set <reg1> <reg2>:cfa.reg)
2002   effects: cfa.reg = <reg1>
2003            cfa.offset unchanged
2004            cfa_temp.reg = <reg1>
2005            cfa_temp.offset = cfa.offset
2006
2007   Rule 2:
2008   (set sp ({minus,plus,losum} {sp,fp}:cfa.reg
2009                               {<const_int>,<reg>:cfa_temp.reg}))
2010   effects: cfa.reg = sp if fp used
2011            cfa.offset += {+/- <const_int>, cfa_temp.offset} if cfa.reg==sp
2012            cfa_store.offset += {+/- <const_int>, cfa_temp.offset}
2013              if cfa_store.reg==sp
2014
2015   Rule 3:
2016   (set fp ({minus,plus,losum} <reg>:cfa.reg <const_int>))
2017   effects: cfa.reg = fp
2018            cfa_offset += +/- <const_int>
2019
2020   Rule 4:
2021   (set <reg1> ({plus,losum} <reg2>:cfa.reg <const_int>))
2022   constraints: <reg1> != fp
2023                <reg1> != sp
2024   effects: cfa.reg = <reg1>
2025            cfa_temp.reg = <reg1>
2026            cfa_temp.offset = cfa.offset
2027
2028   Rule 5:
2029   (set <reg1> (plus <reg2>:cfa_temp.reg sp:cfa.reg))
2030   constraints: <reg1> != fp
2031                <reg1> != sp
2032   effects: cfa_store.reg = <reg1>
2033            cfa_store.offset = cfa.offset - cfa_temp.offset
2034
2035   Rule 6:
2036   (set <reg> <const_int>)
2037   effects: cfa_temp.reg = <reg>
2038            cfa_temp.offset = <const_int>
2039
2040   Rule 7:
2041   (set <reg1>:cfa_temp.reg (ior <reg2>:cfa_temp.reg <const_int>))
2042   effects: cfa_temp.reg = <reg1>
2043            cfa_temp.offset |= <const_int>
2044
2045   Rule 8:
2046   (set <reg> (high <exp>))
2047   effects: none
2048
2049   Rule 9:
2050   (set <reg> (lo_sum <exp> <const_int>))
2051   effects: cfa_temp.reg = <reg>
2052            cfa_temp.offset = <const_int>
2053
2054   Rule 10:
2055   (set (mem (pre_modify sp:cfa_store (???? <reg1> <const_int>))) <reg2>)
2056   effects: cfa_store.offset -= <const_int>
2057            cfa.offset = cfa_store.offset if cfa.reg == sp
2058            cfa.reg = sp
2059            cfa.base_offset = -cfa_store.offset
2060
2061   Rule 11:
2062   (set (mem ({pre_inc,pre_dec} sp:cfa_store.reg)) <reg>)
2063   effects: cfa_store.offset += -/+ mode_size(mem)
2064            cfa.offset = cfa_store.offset if cfa.reg == sp
2065            cfa.reg = sp
2066            cfa.base_offset = -cfa_store.offset
2067
2068   Rule 12:
2069   (set (mem ({minus,plus,losum} <reg1>:{cfa_store,cfa_temp} <const_int>))
2070
2071        <reg2>)
2072   effects: cfa.reg = <reg1>
2073            cfa.base_offset = -/+ <const_int> - {cfa_store,cfa_temp}.offset
2074
2075   Rule 13:
2076   (set (mem <reg1>:{cfa_store,cfa_temp}) <reg2>)
2077   effects: cfa.reg = <reg1>
2078            cfa.base_offset = -{cfa_store,cfa_temp}.offset
2079
2080   Rule 14:
2081   (set (mem (postinc <reg1>:cfa_temp <const_int>)) <reg2>)
2082   effects: cfa.reg = <reg1>
2083            cfa.base_offset = -cfa_temp.offset
2084            cfa_temp.offset -= mode_size(mem)
2085
2086   Rule 15:
2087   (set <reg> {unspec, unspec_volatile})
2088   effects: target-dependent
2089
2090   Rule 16:
2091   (set sp (and: sp <const_int>))
2092   constraints: cfa_store.reg == sp
2093   effects: current_fde.stack_realign = 1
2094            cfa_store.offset = 0
2095            fde->drap_reg = cfa.reg if cfa.reg != sp and cfa.reg != fp
2096
2097   Rule 17:
2098   (set (mem ({pre_inc, pre_dec} sp)) (mem (plus (cfa.reg) (const_int))))
2099   effects: cfa_store.offset += -/+ mode_size(mem)
2100
2101   Rule 18:
2102   (set (mem ({pre_inc, pre_dec} sp)) fp)
2103   constraints: fde->stack_realign == 1
2104   effects: cfa_store.offset = 0
2105            cfa.reg != HARD_FRAME_POINTER_REGNUM
2106
2107   Rule 19:
2108   (set (mem ({pre_inc, pre_dec} sp)) cfa.reg)
2109   constraints: fde->stack_realign == 1
2110                && cfa.offset == 0
2111                && cfa.indirect == 0
2112                && cfa.reg != HARD_FRAME_POINTER_REGNUM
2113   effects: Use DW_CFA_def_cfa_expression to define cfa
2114            cfa.reg == fde->drap_reg
2115
2116   Rule 20:
2117   (set reg fde->drap_reg)
2118   constraints: fde->vdrap_reg == INVALID_REGNUM
2119   effects: fde->vdrap_reg = reg.
2120   (set mem fde->drap_reg)
2121   constraints: fde->drap_reg_saved == 1
2122   effects: none.  */
2123
2124 static void
2125 dwarf2out_frame_debug_expr (rtx expr, const char *label)
2126 {
2127   rtx src, dest, span;
2128   HOST_WIDE_INT offset;
2129   dw_fde_ref fde;
2130
2131   /* If RTX_FRAME_RELATED_P is set on a PARALLEL, process each member of
2132      the PARALLEL independently. The first element is always processed if
2133      it is a SET. This is for backward compatibility.   Other elements
2134      are processed only if they are SETs and the RTX_FRAME_RELATED_P
2135      flag is set in them.  */
2136   if (GET_CODE (expr) == PARALLEL || GET_CODE (expr) == SEQUENCE)
2137     {
2138       int par_index;
2139       int limit = XVECLEN (expr, 0);
2140       rtx elem;
2141
2142       /* PARALLELs have strict read-modify-write semantics, so we
2143          ought to evaluate every rvalue before changing any lvalue.
2144          It's cumbersome to do that in general, but there's an
2145          easy approximation that is enough for all current users:
2146          handle register saves before register assignments.  */
2147       if (GET_CODE (expr) == PARALLEL)
2148         for (par_index = 0; par_index < limit; par_index++)
2149           {
2150             elem = XVECEXP (expr, 0, par_index);
2151             if (GET_CODE (elem) == SET
2152                 && MEM_P (SET_DEST (elem))
2153                 && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
2154               dwarf2out_frame_debug_expr (elem, label);
2155           }
2156
2157       for (par_index = 0; par_index < limit; par_index++)
2158         {
2159           elem = XVECEXP (expr, 0, par_index);
2160           if (GET_CODE (elem) == SET
2161               && (!MEM_P (SET_DEST (elem)) || GET_CODE (expr) == SEQUENCE)
2162               && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
2163             dwarf2out_frame_debug_expr (elem, label);
2164           else if (GET_CODE (elem) == SET
2165                    && par_index != 0
2166                    && !RTX_FRAME_RELATED_P (elem))
2167             {
2168               /* Stack adjustment combining might combine some post-prologue
2169                  stack adjustment into a prologue stack adjustment.  */
2170               HOST_WIDE_INT offset = stack_adjust_offset (elem, args_size, 0);
2171
2172               if (offset != 0)
2173                 dwarf2out_args_size_adjust (offset, label);
2174             }
2175         }
2176       return;
2177     }
2178
2179   gcc_assert (GET_CODE (expr) == SET);
2180
2181   src = SET_SRC (expr);
2182   dest = SET_DEST (expr);
2183
2184   if (REG_P (src))
2185     {
2186       rtx rsi = reg_saved_in (src);
2187       if (rsi)
2188         src = rsi;
2189     }
2190
2191   fde = current_fde ();
2192
2193   if (REG_P (src)
2194       && fde
2195       && fde->drap_reg == REGNO (src)
2196       && (fde->drap_reg_saved
2197           || REG_P (dest)))
2198     {
2199       /* Rule 20 */
2200       /* If we are saving dynamic realign argument pointer to a
2201          register, the destination is virtual dynamic realign
2202          argument pointer.  It may be used to access argument.  */
2203       if (REG_P (dest))
2204         {
2205           gcc_assert (fde->vdrap_reg == INVALID_REGNUM);
2206           fde->vdrap_reg = REGNO (dest);
2207         }
2208       return;
2209     }
2210
2211   switch (GET_CODE (dest))
2212     {
2213     case REG:
2214       switch (GET_CODE (src))
2215         {
2216           /* Setting FP from SP.  */
2217         case REG:
2218           if (cfa.reg == (unsigned) REGNO (src))
2219             {
2220               /* Rule 1 */
2221               /* Update the CFA rule wrt SP or FP.  Make sure src is
2222                  relative to the current CFA register.
2223
2224                  We used to require that dest be either SP or FP, but the
2225                  ARM copies SP to a temporary register, and from there to
2226                  FP.  So we just rely on the backends to only set
2227                  RTX_FRAME_RELATED_P on appropriate insns.  */
2228               cfa.reg = REGNO (dest);
2229               cfa_temp.reg = cfa.reg;
2230               cfa_temp.offset = cfa.offset;
2231             }
2232           else
2233             {
2234               /* Saving a register in a register.  */
2235               gcc_assert (!fixed_regs [REGNO (dest)]
2236                           /* For the SPARC and its register window.  */
2237                           || (DWARF_FRAME_REGNUM (REGNO (src))
2238                               == DWARF_FRAME_RETURN_COLUMN));
2239
2240               /* After stack is aligned, we can only save SP in FP
2241                  if drap register is used.  In this case, we have
2242                  to restore stack pointer with the CFA value and we
2243                  don't generate this DWARF information.  */
2244               if (fde
2245                   && fde->stack_realign
2246                   && REGNO (src) == STACK_POINTER_REGNUM)
2247                 gcc_assert (REGNO (dest) == HARD_FRAME_POINTER_REGNUM
2248                             && fde->drap_reg != INVALID_REGNUM
2249                             && cfa.reg != REGNO (src));
2250               else
2251                 queue_reg_save (label, src, dest, 0);
2252             }
2253           break;
2254
2255         case PLUS:
2256         case MINUS:
2257         case LO_SUM:
2258           if (dest == stack_pointer_rtx)
2259             {
2260               /* Rule 2 */
2261               /* Adjusting SP.  */
2262               switch (GET_CODE (XEXP (src, 1)))
2263                 {
2264                 case CONST_INT:
2265                   offset = INTVAL (XEXP (src, 1));
2266                   break;
2267                 case REG:
2268                   gcc_assert ((unsigned) REGNO (XEXP (src, 1))
2269                               == cfa_temp.reg);
2270                   offset = cfa_temp.offset;
2271                   break;
2272                 default:
2273                   gcc_unreachable ();
2274                 }
2275
2276               if (XEXP (src, 0) == hard_frame_pointer_rtx)
2277                 {
2278                   /* Restoring SP from FP in the epilogue.  */
2279                   gcc_assert (cfa.reg == (unsigned) HARD_FRAME_POINTER_REGNUM);
2280                   cfa.reg = STACK_POINTER_REGNUM;
2281                 }
2282               else if (GET_CODE (src) == LO_SUM)
2283                 /* Assume we've set the source reg of the LO_SUM from sp.  */
2284                 ;
2285               else
2286                 gcc_assert (XEXP (src, 0) == stack_pointer_rtx);
2287
2288               if (GET_CODE (src) != MINUS)
2289                 offset = -offset;
2290               if (cfa.reg == STACK_POINTER_REGNUM)
2291                 cfa.offset += offset;
2292               if (cfa_store.reg == STACK_POINTER_REGNUM)
2293                 cfa_store.offset += offset;
2294             }
2295           else if (dest == hard_frame_pointer_rtx)
2296             {
2297               /* Rule 3 */
2298               /* Either setting the FP from an offset of the SP,
2299                  or adjusting the FP */
2300               gcc_assert (frame_pointer_needed);
2301
2302               gcc_assert (REG_P (XEXP (src, 0))
2303                           && (unsigned) REGNO (XEXP (src, 0)) == cfa.reg
2304                           && CONST_INT_P (XEXP (src, 1)));
2305               offset = INTVAL (XEXP (src, 1));
2306               if (GET_CODE (src) != MINUS)
2307                 offset = -offset;
2308               cfa.offset += offset;
2309               cfa.reg = HARD_FRAME_POINTER_REGNUM;
2310             }
2311           else
2312             {
2313               gcc_assert (GET_CODE (src) != MINUS);
2314
2315               /* Rule 4 */
2316               if (REG_P (XEXP (src, 0))
2317                   && REGNO (XEXP (src, 0)) == cfa.reg
2318                   && CONST_INT_P (XEXP (src, 1)))
2319                 {
2320                   /* Setting a temporary CFA register that will be copied
2321                      into the FP later on.  */
2322                   offset = - INTVAL (XEXP (src, 1));
2323                   cfa.offset += offset;
2324                   cfa.reg = REGNO (dest);
2325                   /* Or used to save regs to the stack.  */
2326                   cfa_temp.reg = cfa.reg;
2327                   cfa_temp.offset = cfa.offset;
2328                 }
2329
2330               /* Rule 5 */
2331               else if (REG_P (XEXP (src, 0))
2332                        && REGNO (XEXP (src, 0)) == cfa_temp.reg
2333                        && XEXP (src, 1) == stack_pointer_rtx)
2334                 {
2335                   /* Setting a scratch register that we will use instead
2336                      of SP for saving registers to the stack.  */
2337                   gcc_assert (cfa.reg == STACK_POINTER_REGNUM);
2338                   cfa_store.reg = REGNO (dest);
2339                   cfa_store.offset = cfa.offset - cfa_temp.offset;
2340                 }
2341
2342               /* Rule 9 */
2343               else if (GET_CODE (src) == LO_SUM
2344                        && CONST_INT_P (XEXP (src, 1)))
2345                 {
2346                   cfa_temp.reg = REGNO (dest);
2347                   cfa_temp.offset = INTVAL (XEXP (src, 1));
2348                 }
2349               else
2350                 gcc_unreachable ();
2351             }
2352           break;
2353
2354           /* Rule 6 */
2355         case CONST_INT:
2356           cfa_temp.reg = REGNO (dest);
2357           cfa_temp.offset = INTVAL (src);
2358           break;
2359
2360           /* Rule 7 */
2361         case IOR:
2362           gcc_assert (REG_P (XEXP (src, 0))
2363                       && (unsigned) REGNO (XEXP (src, 0)) == cfa_temp.reg
2364                       && CONST_INT_P (XEXP (src, 1)));
2365
2366           if ((unsigned) REGNO (dest) != cfa_temp.reg)
2367             cfa_temp.reg = REGNO (dest);
2368           cfa_temp.offset |= INTVAL (XEXP (src, 1));
2369           break;
2370
2371           /* Skip over HIGH, assuming it will be followed by a LO_SUM,
2372              which will fill in all of the bits.  */
2373           /* Rule 8 */
2374         case HIGH:
2375           break;
2376
2377           /* Rule 15 */
2378         case UNSPEC:
2379         case UNSPEC_VOLATILE:
2380           gcc_assert (targetm.dwarf_handle_frame_unspec);
2381           targetm.dwarf_handle_frame_unspec (label, expr, XINT (src, 1));
2382           return;
2383
2384           /* Rule 16 */
2385         case AND:
2386           /* If this AND operation happens on stack pointer in prologue,
2387              we assume the stack is realigned and we extract the
2388              alignment.  */
2389           if (fde && XEXP (src, 0) == stack_pointer_rtx)
2390             {
2391               gcc_assert (cfa_store.reg == REGNO (XEXP (src, 0)));
2392               fde->stack_realign = 1;
2393               fde->stack_realignment = INTVAL (XEXP (src, 1));
2394               cfa_store.offset = 0;
2395
2396               if (cfa.reg != STACK_POINTER_REGNUM
2397                   && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2398                 fde->drap_reg = cfa.reg;
2399             }
2400           return;
2401
2402         default:
2403           gcc_unreachable ();
2404         }
2405
2406       def_cfa_1 (label, &cfa);
2407       break;
2408
2409     case MEM:
2410
2411       /* Saving a register to the stack.  Make sure dest is relative to the
2412          CFA register.  */
2413       switch (GET_CODE (XEXP (dest, 0)))
2414         {
2415           /* Rule 10 */
2416           /* With a push.  */
2417         case PRE_MODIFY:
2418           /* We can't handle variable size modifications.  */
2419           gcc_assert (GET_CODE (XEXP (XEXP (XEXP (dest, 0), 1), 1))
2420                       == CONST_INT);
2421           offset = -INTVAL (XEXP (XEXP (XEXP (dest, 0), 1), 1));
2422
2423           gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
2424                       && cfa_store.reg == STACK_POINTER_REGNUM);
2425
2426           cfa_store.offset += offset;
2427           if (cfa.reg == STACK_POINTER_REGNUM)
2428             cfa.offset = cfa_store.offset;
2429
2430           offset = -cfa_store.offset;
2431           break;
2432
2433           /* Rule 11 */
2434         case PRE_INC:
2435         case PRE_DEC:
2436           offset = GET_MODE_SIZE (GET_MODE (dest));
2437           if (GET_CODE (XEXP (dest, 0)) == PRE_INC)
2438             offset = -offset;
2439
2440           gcc_assert ((REGNO (XEXP (XEXP (dest, 0), 0))
2441                        == STACK_POINTER_REGNUM)
2442                       && cfa_store.reg == STACK_POINTER_REGNUM);
2443
2444           cfa_store.offset += offset;
2445
2446           /* Rule 18: If stack is aligned, we will use FP as a
2447              reference to represent the address of the stored
2448              regiser.  */
2449           if (fde
2450               && fde->stack_realign
2451               && src == hard_frame_pointer_rtx)
2452             {
2453               gcc_assert (cfa.reg != HARD_FRAME_POINTER_REGNUM);
2454               cfa_store.offset = 0;
2455             }
2456
2457           if (cfa.reg == STACK_POINTER_REGNUM)
2458             cfa.offset = cfa_store.offset;
2459
2460           offset = -cfa_store.offset;
2461           break;
2462
2463           /* Rule 12 */
2464           /* With an offset.  */
2465         case PLUS:
2466         case MINUS:
2467         case LO_SUM:
2468           {
2469             int regno;
2470
2471             gcc_assert (CONST_INT_P (XEXP (XEXP (dest, 0), 1))
2472                         && REG_P (XEXP (XEXP (dest, 0), 0)));
2473             offset = INTVAL (XEXP (XEXP (dest, 0), 1));
2474             if (GET_CODE (XEXP (dest, 0)) == MINUS)
2475               offset = -offset;
2476
2477             regno = REGNO (XEXP (XEXP (dest, 0), 0));
2478
2479             if (cfa_store.reg == (unsigned) regno)
2480               offset -= cfa_store.offset;
2481             else
2482               {
2483                 gcc_assert (cfa_temp.reg == (unsigned) regno);
2484                 offset -= cfa_temp.offset;
2485               }
2486           }
2487           break;
2488
2489           /* Rule 13 */
2490           /* Without an offset.  */
2491         case REG:
2492           {
2493             int regno = REGNO (XEXP (dest, 0));
2494
2495             if (cfa_store.reg == (unsigned) regno)
2496               offset = -cfa_store.offset;
2497             else
2498               {
2499                 gcc_assert (cfa_temp.reg == (unsigned) regno);
2500                 offset = -cfa_temp.offset;
2501               }
2502           }
2503           break;
2504
2505           /* Rule 14 */
2506         case POST_INC:
2507           gcc_assert (cfa_temp.reg
2508                       == (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)));
2509           offset = -cfa_temp.offset;
2510           cfa_temp.offset -= GET_MODE_SIZE (GET_MODE (dest));
2511           break;
2512
2513         default:
2514           gcc_unreachable ();
2515         }
2516
2517         /* Rule 17 */
2518         /* If the source operand of this MEM operation is not a
2519            register, basically the source is return address.  Here
2520            we only care how much stack grew and we don't save it.  */
2521       if (!REG_P (src))
2522         break;
2523
2524       if (REGNO (src) != STACK_POINTER_REGNUM
2525           && REGNO (src) != HARD_FRAME_POINTER_REGNUM
2526           && (unsigned) REGNO (src) == cfa.reg)
2527         {
2528           /* We're storing the current CFA reg into the stack.  */
2529
2530           if (cfa.offset == 0)
2531             {
2532               /* Rule 19 */
2533               /* If stack is aligned, putting CFA reg into stack means
2534                  we can no longer use reg + offset to represent CFA.
2535                  Here we use DW_CFA_def_cfa_expression instead.  The
2536                  result of this expression equals to the original CFA
2537                  value.  */
2538               if (fde
2539                   && fde->stack_realign
2540                   && cfa.indirect == 0
2541                   && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2542                 {
2543                   dw_cfa_location cfa_exp;
2544
2545                   gcc_assert (fde->drap_reg == cfa.reg);
2546
2547                   cfa_exp.indirect = 1;
2548                   cfa_exp.reg = HARD_FRAME_POINTER_REGNUM;
2549                   cfa_exp.base_offset = offset;
2550                   cfa_exp.offset = 0;
2551
2552                   fde->drap_reg_saved = 1;
2553
2554                   def_cfa_1 (label, &cfa_exp);
2555                   break;
2556                 }
2557
2558               /* If the source register is exactly the CFA, assume
2559                  we're saving SP like any other register; this happens
2560                  on the ARM.  */
2561               def_cfa_1 (label, &cfa);
2562               queue_reg_save (label, stack_pointer_rtx, NULL_RTX, offset);
2563               break;
2564             }
2565           else
2566             {
2567               /* Otherwise, we'll need to look in the stack to
2568                  calculate the CFA.  */
2569               rtx x = XEXP (dest, 0);
2570
2571               if (!REG_P (x))
2572                 x = XEXP (x, 0);
2573               gcc_assert (REG_P (x));
2574
2575               cfa.reg = REGNO (x);
2576               cfa.base_offset = offset;
2577               cfa.indirect = 1;
2578               def_cfa_1 (label, &cfa);
2579               break;
2580             }
2581         }
2582
2583       def_cfa_1 (label, &cfa);
2584       {
2585         span = targetm.dwarf_register_span (src);
2586
2587         if (!span)
2588           queue_reg_save (label, src, NULL_RTX, offset);
2589         else
2590           {
2591             /* We have a PARALLEL describing where the contents of SRC
2592                live.  Queue register saves for each piece of the
2593                PARALLEL.  */
2594             int par_index;
2595             int limit;
2596             HOST_WIDE_INT span_offset = offset;
2597
2598             gcc_assert (GET_CODE (span) == PARALLEL);
2599
2600             limit = XVECLEN (span, 0);
2601             for (par_index = 0; par_index < limit; par_index++)
2602               {
2603                 rtx elem = XVECEXP (span, 0, par_index);
2604
2605                 queue_reg_save (label, elem, NULL_RTX, span_offset);
2606                 span_offset += GET_MODE_SIZE (GET_MODE (elem));
2607               }
2608           }
2609       }
2610       break;
2611
2612     default:
2613       gcc_unreachable ();
2614     }
2615 }
2616
2617 /* Record call frame debugging information for INSN, which either
2618    sets SP or FP (adjusting how we calculate the frame address) or saves a
2619    register to the stack.  If INSN is NULL_RTX, initialize our state.
2620
2621    If AFTER_P is false, we're being called before the insn is emitted,
2622    otherwise after.  Call instructions get invoked twice.  */
2623
2624 void
2625 dwarf2out_frame_debug (rtx insn, bool after_p)
2626 {
2627   const char *label;
2628   rtx note, n;
2629   bool handled_one = false;
2630
2631   if (insn == NULL_RTX)
2632     {
2633       size_t i;
2634
2635       /* Flush any queued register saves.  */
2636       flush_queued_reg_saves ();
2637
2638       /* Set up state for generating call frame debug info.  */
2639       lookup_cfa (&cfa);
2640       gcc_assert (cfa.reg
2641                   == (unsigned long)DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM));
2642
2643       cfa.reg = STACK_POINTER_REGNUM;
2644       cfa_store = cfa;
2645       cfa_temp.reg = -1;
2646       cfa_temp.offset = 0;
2647
2648       for (i = 0; i < num_regs_saved_in_regs; i++)
2649         {
2650           regs_saved_in_regs[i].orig_reg = NULL_RTX;
2651           regs_saved_in_regs[i].saved_in_reg = NULL_RTX;
2652         }
2653       num_regs_saved_in_regs = 0;
2654
2655       if (barrier_args_size)
2656         {
2657           XDELETEVEC (barrier_args_size);
2658           barrier_args_size = NULL;
2659         }
2660       return;
2661     }
2662
2663   if (!NONJUMP_INSN_P (insn) || clobbers_queued_reg_save (insn))
2664     flush_queued_reg_saves ();
2665
2666   if (! RTX_FRAME_RELATED_P (insn))
2667     {
2668       if (!ACCUMULATE_OUTGOING_ARGS)
2669         dwarf2out_stack_adjust (insn, after_p);
2670       return;
2671     }
2672
2673   label = dwarf2out_cfi_label (false);
2674
2675   for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
2676     switch (REG_NOTE_KIND (note))
2677       {
2678       case REG_FRAME_RELATED_EXPR:
2679         insn = XEXP (note, 0);
2680         goto found;
2681
2682       case REG_CFA_DEF_CFA:
2683         dwarf2out_frame_debug_def_cfa (XEXP (note, 0), label);
2684         handled_one = true;
2685         break;
2686
2687       case REG_CFA_ADJUST_CFA:
2688         n = XEXP (note, 0);
2689         if (n == NULL)
2690           {
2691             n = PATTERN (insn);
2692             if (GET_CODE (n) == PARALLEL)
2693               n = XVECEXP (n, 0, 0);
2694           }
2695         dwarf2out_frame_debug_adjust_cfa (n, label);
2696         handled_one = true;
2697         break;
2698
2699       case REG_CFA_OFFSET:
2700         n = XEXP (note, 0);
2701         if (n == NULL)
2702           n = single_set (insn);
2703         dwarf2out_frame_debug_cfa_offset (n, label);
2704         handled_one = true;
2705         break;
2706
2707       case REG_CFA_REGISTER:
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_cfa_register (n, label);
2716         handled_one = true;
2717         break;
2718
2719       case REG_CFA_RESTORE:
2720         n = XEXP (note, 0);
2721         if (n == NULL)
2722           {
2723             n = PATTERN (insn);
2724             if (GET_CODE (n) == PARALLEL)
2725               n = XVECEXP (n, 0, 0);
2726             n = XEXP (n, 0);
2727           }
2728         dwarf2out_frame_debug_cfa_restore (n, label);
2729         handled_one = true;
2730         break;
2731
2732       default:
2733         break;
2734       }
2735   if (handled_one)
2736     return;
2737
2738   insn = PATTERN (insn);
2739  found:
2740   dwarf2out_frame_debug_expr (insn, label);
2741 }
2742
2743 /* Determine if we need to save and restore CFI information around this
2744    epilogue.  If SIBCALL is true, then this is a sibcall epilogue.  If
2745    we do need to save/restore, then emit the save now, and insert a
2746    NOTE_INSN_CFA_RESTORE_STATE at the appropriate place in the stream.  */
2747
2748 void
2749 dwarf2out_begin_epilogue (rtx insn)
2750 {
2751   bool saw_frp = false;
2752   rtx i;
2753
2754   /* Scan forward to the return insn, noticing if there are possible
2755      frame related insns.  */
2756   for (i = NEXT_INSN (insn); i ; i = NEXT_INSN (i))
2757     {
2758       if (!INSN_P (i))
2759         continue;
2760
2761       /* Look for both regular and sibcalls to end the block.  */
2762       if (returnjump_p (i))
2763         break;
2764       if (CALL_P (i) && SIBLING_CALL_P (i))
2765         break;
2766
2767       if (GET_CODE (PATTERN (i)) == SEQUENCE)
2768         {
2769           int idx;
2770           rtx seq = PATTERN (i);
2771
2772           if (returnjump_p (XVECEXP (seq, 0, 0)))
2773             break;
2774           if (CALL_P (XVECEXP (seq, 0, 0))
2775               && SIBLING_CALL_P (XVECEXP (seq, 0, 0)))
2776             break;
2777
2778           for (idx = 0; idx < XVECLEN (seq, 0); idx++)
2779             if (RTX_FRAME_RELATED_P (XVECEXP (seq, 0, idx)))
2780               saw_frp = true;
2781         }
2782
2783       if (RTX_FRAME_RELATED_P (i))
2784         saw_frp = true;
2785     }
2786
2787   /* If the port doesn't emit epilogue unwind info, we don't need a
2788      save/restore pair.  */
2789   if (!saw_frp)
2790     return;
2791
2792   /* Otherwise, search forward to see if the return insn was the last
2793      basic block of the function.  If so, we don't need save/restore.  */
2794   gcc_assert (i != NULL);
2795   i = next_real_insn (i);
2796   if (i == NULL)
2797     return;
2798
2799   /* Insert the restore before that next real insn in the stream, and before
2800      a potential NOTE_INSN_EPILOGUE_BEG -- we do need these notes to be
2801      properly nested.  This should be after any label or alignment.  This
2802      will be pushed into the CFI stream by the function below.  */
2803   while (1)
2804     {
2805       rtx p = PREV_INSN (i);
2806       if (!NOTE_P (p))
2807         break;
2808       if (NOTE_KIND (p) == NOTE_INSN_BASIC_BLOCK)
2809         break;
2810       i = p;
2811     }
2812   emit_note_before (NOTE_INSN_CFA_RESTORE_STATE, i);
2813
2814   emit_cfa_remember = true;
2815
2816   /* And emulate the state save.  */
2817   gcc_assert (!cfa_remember.in_use);
2818   cfa_remember = cfa;
2819   cfa_remember.in_use = 1;
2820 }
2821
2822 /* A "subroutine" of dwarf2out_begin_epilogue.  Emit the restore required.  */
2823
2824 void
2825 dwarf2out_frame_debug_restore_state (void)
2826 {
2827   dw_cfi_ref cfi = new_cfi (); 
2828   const char *label = dwarf2out_cfi_label (false);
2829
2830   cfi->dw_cfi_opc = DW_CFA_restore_state;
2831   add_fde_cfi (label, cfi);
2832
2833   gcc_assert (cfa_remember.in_use);
2834   cfa = cfa_remember;
2835   cfa_remember.in_use = 0;
2836 }
2837
2838 #endif
2839
2840 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used.  */
2841 static enum dw_cfi_oprnd_type dw_cfi_oprnd1_desc
2842  (enum dwarf_call_frame_info cfi);
2843
2844 static enum dw_cfi_oprnd_type
2845 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
2846 {
2847   switch (cfi)
2848     {
2849     case DW_CFA_nop:
2850     case DW_CFA_GNU_window_save:
2851     case DW_CFA_remember_state:
2852     case DW_CFA_restore_state:
2853       return dw_cfi_oprnd_unused;
2854
2855     case DW_CFA_set_loc:
2856     case DW_CFA_advance_loc1:
2857     case DW_CFA_advance_loc2:
2858     case DW_CFA_advance_loc4:
2859     case DW_CFA_MIPS_advance_loc8:
2860       return dw_cfi_oprnd_addr;
2861
2862     case DW_CFA_offset:
2863     case DW_CFA_offset_extended:
2864     case DW_CFA_def_cfa:
2865     case DW_CFA_offset_extended_sf:
2866     case DW_CFA_def_cfa_sf:
2867     case DW_CFA_restore:
2868     case DW_CFA_restore_extended:
2869     case DW_CFA_undefined:
2870     case DW_CFA_same_value:
2871     case DW_CFA_def_cfa_register:
2872     case DW_CFA_register:
2873       return dw_cfi_oprnd_reg_num;
2874
2875     case DW_CFA_def_cfa_offset:
2876     case DW_CFA_GNU_args_size:
2877     case DW_CFA_def_cfa_offset_sf:
2878       return dw_cfi_oprnd_offset;
2879
2880     case DW_CFA_def_cfa_expression:
2881     case DW_CFA_expression:
2882       return dw_cfi_oprnd_loc;
2883
2884     default:
2885       gcc_unreachable ();
2886     }
2887 }
2888
2889 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used.  */
2890 static enum dw_cfi_oprnd_type dw_cfi_oprnd2_desc
2891  (enum dwarf_call_frame_info cfi);
2892
2893 static enum dw_cfi_oprnd_type
2894 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
2895 {
2896   switch (cfi)
2897     {
2898     case DW_CFA_def_cfa:
2899     case DW_CFA_def_cfa_sf:
2900     case DW_CFA_offset:
2901     case DW_CFA_offset_extended_sf:
2902     case DW_CFA_offset_extended:
2903       return dw_cfi_oprnd_offset;
2904
2905     case DW_CFA_register:
2906       return dw_cfi_oprnd_reg_num;
2907
2908     default:
2909       return dw_cfi_oprnd_unused;
2910     }
2911 }
2912
2913 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
2914
2915 /* Switch to eh_frame_section.  If we don't have an eh_frame_section,
2916    switch to the data section instead, and write out a synthetic label
2917    for collect2.  */
2918
2919 static void
2920 switch_to_eh_frame_section (void)
2921 {
2922   tree label;
2923
2924 #ifdef EH_FRAME_SECTION_NAME
2925   if (eh_frame_section == 0)
2926     {
2927       int flags;
2928
2929       if (EH_TABLES_CAN_BE_READ_ONLY)
2930         {
2931           int fde_encoding;
2932           int per_encoding;
2933           int lsda_encoding;
2934
2935           fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
2936                                                        /*global=*/0);
2937           per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
2938                                                        /*global=*/1);
2939           lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
2940                                                         /*global=*/0);
2941           flags = ((! flag_pic
2942                     || ((fde_encoding & 0x70) != DW_EH_PE_absptr
2943                         && (fde_encoding & 0x70) != DW_EH_PE_aligned
2944                         && (per_encoding & 0x70) != DW_EH_PE_absptr
2945                         && (per_encoding & 0x70) != DW_EH_PE_aligned
2946                         && (lsda_encoding & 0x70) != DW_EH_PE_absptr
2947                         && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
2948                    ? 0 : SECTION_WRITE);
2949         }
2950       else
2951         flags = SECTION_WRITE;
2952       eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
2953     }
2954 #endif
2955
2956   if (eh_frame_section)
2957     switch_to_section (eh_frame_section);
2958   else
2959     {
2960       /* We have no special eh_frame section.  Put the information in
2961          the data section and emit special labels to guide collect2.  */
2962       switch_to_section (data_section);
2963       label = get_file_function_name ("F");
2964       ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
2965       targetm.asm_out.globalize_label (asm_out_file,
2966                                        IDENTIFIER_POINTER (label));
2967       ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
2968     }
2969 }
2970
2971 /* Output a Call Frame Information opcode and its operand(s).  */
2972
2973 static void
2974 output_cfi (dw_cfi_ref cfi, dw_fde_ref fde, int for_eh)
2975 {
2976   unsigned long r;
2977   HOST_WIDE_INT off;
2978
2979   if (cfi->dw_cfi_opc == DW_CFA_advance_loc)
2980     dw2_asm_output_data (1, (cfi->dw_cfi_opc
2981                              | (cfi->dw_cfi_oprnd1.dw_cfi_offset & 0x3f)),
2982                          "DW_CFA_advance_loc " HOST_WIDE_INT_PRINT_HEX,
2983                          ((unsigned HOST_WIDE_INT)
2984                           cfi->dw_cfi_oprnd1.dw_cfi_offset));
2985   else if (cfi->dw_cfi_opc == DW_CFA_offset)
2986     {
2987       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2988       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
2989                            "DW_CFA_offset, column 0x%lx", r);
2990       off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
2991       dw2_asm_output_data_uleb128 (off, NULL);
2992     }
2993   else if (cfi->dw_cfi_opc == DW_CFA_restore)
2994     {
2995       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2996       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
2997                            "DW_CFA_restore, column 0x%lx", r);
2998     }
2999   else
3000     {
3001       dw2_asm_output_data (1, cfi->dw_cfi_opc,
3002                            "%s", dwarf_cfi_name (cfi->dw_cfi_opc));
3003
3004       switch (cfi->dw_cfi_opc)
3005         {
3006         case DW_CFA_set_loc:
3007           if (for_eh)
3008             dw2_asm_output_encoded_addr_rtx (
3009                 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0),
3010                 gen_rtx_SYMBOL_REF (Pmode, cfi->dw_cfi_oprnd1.dw_cfi_addr),
3011                 false, NULL);
3012           else
3013             dw2_asm_output_addr (DWARF2_ADDR_SIZE,
3014                                  cfi->dw_cfi_oprnd1.dw_cfi_addr, NULL);
3015           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3016           break;
3017
3018         case DW_CFA_advance_loc1:
3019           dw2_asm_output_delta (1, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3020                                 fde->dw_fde_current_label, NULL);
3021           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3022           break;
3023
3024         case DW_CFA_advance_loc2:
3025           dw2_asm_output_delta (2, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3026                                 fde->dw_fde_current_label, NULL);
3027           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3028           break;
3029
3030         case DW_CFA_advance_loc4:
3031           dw2_asm_output_delta (4, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3032                                 fde->dw_fde_current_label, NULL);
3033           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3034           break;
3035
3036         case DW_CFA_MIPS_advance_loc8:
3037           dw2_asm_output_delta (8, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3038                                 fde->dw_fde_current_label, NULL);
3039           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3040           break;
3041
3042         case DW_CFA_offset_extended:
3043           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3044           dw2_asm_output_data_uleb128 (r, NULL);
3045           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3046           dw2_asm_output_data_uleb128 (off, NULL);
3047           break;
3048
3049         case DW_CFA_def_cfa:
3050           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3051           dw2_asm_output_data_uleb128 (r, NULL);
3052           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
3053           break;
3054
3055         case DW_CFA_offset_extended_sf:
3056           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3057           dw2_asm_output_data_uleb128 (r, NULL);
3058           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3059           dw2_asm_output_data_sleb128 (off, NULL);
3060           break;
3061
3062         case DW_CFA_def_cfa_sf:
3063           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3064           dw2_asm_output_data_uleb128 (r, NULL);
3065           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3066           dw2_asm_output_data_sleb128 (off, NULL);
3067           break;
3068
3069         case DW_CFA_restore_extended:
3070         case DW_CFA_undefined:
3071         case DW_CFA_same_value:
3072         case DW_CFA_def_cfa_register:
3073           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3074           dw2_asm_output_data_uleb128 (r, NULL);
3075           break;
3076
3077         case DW_CFA_register:
3078           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3079           dw2_asm_output_data_uleb128 (r, NULL);
3080           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, for_eh);
3081           dw2_asm_output_data_uleb128 (r, NULL);
3082           break;
3083
3084         case DW_CFA_def_cfa_offset:
3085         case DW_CFA_GNU_args_size:
3086           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
3087           break;
3088
3089         case DW_CFA_def_cfa_offset_sf:
3090           off = div_data_align (cfi->dw_cfi_oprnd1.dw_cfi_offset);
3091           dw2_asm_output_data_sleb128 (off, NULL);
3092           break;
3093
3094         case DW_CFA_GNU_window_save:
3095           break;
3096
3097         case DW_CFA_def_cfa_expression:
3098         case DW_CFA_expression:
3099           output_cfa_loc (cfi);
3100           break;
3101
3102         case DW_CFA_GNU_negative_offset_extended:
3103           /* Obsoleted by DW_CFA_offset_extended_sf.  */
3104           gcc_unreachable ();
3105
3106         default:
3107           break;
3108         }
3109     }
3110 }
3111
3112 /* Similar, but do it via assembler directives instead.  */
3113
3114 static void
3115 output_cfi_directive (dw_cfi_ref cfi)
3116 {
3117   unsigned long r, r2;
3118
3119   switch (cfi->dw_cfi_opc)
3120     {
3121     case DW_CFA_advance_loc:
3122     case DW_CFA_advance_loc1:
3123     case DW_CFA_advance_loc2:
3124     case DW_CFA_advance_loc4:
3125     case DW_CFA_MIPS_advance_loc8:
3126     case DW_CFA_set_loc:
3127       /* Should only be created by add_fde_cfi in a code path not
3128          followed when emitting via directives.  The assembler is
3129          going to take care of this for us.  */
3130       gcc_unreachable ();
3131
3132     case DW_CFA_offset:
3133     case DW_CFA_offset_extended:
3134     case DW_CFA_offset_extended_sf:
3135       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3136       fprintf (asm_out_file, "\t.cfi_offset %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
3137                r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
3138       break;
3139
3140     case DW_CFA_restore:
3141     case DW_CFA_restore_extended:
3142       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3143       fprintf (asm_out_file, "\t.cfi_restore %lu\n", r);
3144       break;
3145
3146     case DW_CFA_undefined:
3147       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3148       fprintf (asm_out_file, "\t.cfi_undefined %lu\n", r);
3149       break;
3150
3151     case DW_CFA_same_value:
3152       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3153       fprintf (asm_out_file, "\t.cfi_same_value %lu\n", r);
3154       break;
3155
3156     case DW_CFA_def_cfa:
3157     case DW_CFA_def_cfa_sf:
3158       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3159       fprintf (asm_out_file, "\t.cfi_def_cfa %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
3160                r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
3161       break;
3162
3163     case DW_CFA_def_cfa_register:
3164       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3165       fprintf (asm_out_file, "\t.cfi_def_cfa_register %lu\n", r);
3166       break;
3167
3168     case DW_CFA_register:
3169       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3170       r2 = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, 1);
3171       fprintf (asm_out_file, "\t.cfi_register %lu, %lu\n", r, r2);
3172       break;
3173
3174     case DW_CFA_def_cfa_offset:
3175     case DW_CFA_def_cfa_offset_sf:
3176       fprintf (asm_out_file, "\t.cfi_def_cfa_offset "
3177                HOST_WIDE_INT_PRINT_DEC"\n",
3178                cfi->dw_cfi_oprnd1.dw_cfi_offset);
3179       break;
3180
3181     case DW_CFA_remember_state:
3182       fprintf (asm_out_file, "\t.cfi_remember_state\n");
3183       break;
3184     case DW_CFA_restore_state:
3185       fprintf (asm_out_file, "\t.cfi_restore_state\n");
3186       break;
3187
3188     case DW_CFA_GNU_args_size:
3189       fprintf (asm_out_file, "\t.cfi_escape 0x%x,", DW_CFA_GNU_args_size);
3190       dw2_asm_output_data_uleb128_raw (cfi->dw_cfi_oprnd1.dw_cfi_offset);
3191       if (flag_debug_asm)
3192         fprintf (asm_out_file, "\t%s args_size "HOST_WIDE_INT_PRINT_DEC,
3193                  ASM_COMMENT_START, cfi->dw_cfi_oprnd1.dw_cfi_offset);
3194       fputc ('\n', asm_out_file);
3195       break;
3196
3197     case DW_CFA_GNU_window_save:
3198       fprintf (asm_out_file, "\t.cfi_window_save\n");
3199       break;
3200
3201     case DW_CFA_def_cfa_expression:
3202     case DW_CFA_expression:
3203       fprintf (asm_out_file, "\t.cfi_escape 0x%x,", cfi->dw_cfi_opc);
3204       output_cfa_loc_raw (cfi);
3205       fputc ('\n', asm_out_file);
3206       break;
3207
3208     default:
3209       gcc_unreachable ();
3210     }
3211 }
3212
3213 /* Output the call frame information used to record information
3214    that relates to calculating the frame pointer, and records the
3215    location of saved registers.  */
3216
3217 static void
3218 output_call_frame_info (int for_eh)
3219 {
3220   unsigned int i;
3221   dw_fde_ref fde;
3222   dw_cfi_ref cfi;
3223   char l1[20], l2[20], section_start_label[20];
3224   bool any_lsda_needed = false;
3225   char augmentation[6];
3226   int augmentation_size;
3227   int fde_encoding = DW_EH_PE_absptr;
3228   int per_encoding = DW_EH_PE_absptr;
3229   int lsda_encoding = DW_EH_PE_absptr;
3230   int return_reg;
3231   int dw_cie_version;
3232
3233   /* Don't emit a CIE if there won't be any FDEs.  */
3234   if (fde_table_in_use == 0)
3235     return;
3236
3237   /* Nothing to do if the assembler's doing it all.  */
3238   if (dwarf2out_do_cfi_asm ())
3239     return;
3240
3241   /* If we make FDEs linkonce, we may have to emit an empty label for
3242      an FDE that wouldn't otherwise be emitted.  We want to avoid
3243      having an FDE kept around when the function it refers to is
3244      discarded.  Example where this matters: a primary function
3245      template in C++ requires EH information, but an explicit
3246      specialization doesn't.  */
3247   if (TARGET_USES_WEAK_UNWIND_INFO
3248       && ! flag_asynchronous_unwind_tables
3249       && flag_exceptions
3250       && for_eh)
3251     for (i = 0; i < fde_table_in_use; i++)
3252       if ((fde_table[i].nothrow || fde_table[i].all_throwers_are_sibcalls)
3253           && !fde_table[i].uses_eh_lsda
3254           && ! DECL_WEAK (fde_table[i].decl))
3255         targetm.asm_out.unwind_label (asm_out_file, fde_table[i].decl,
3256                                       for_eh, /* empty */ 1);
3257
3258   /* If we don't have any functions we'll want to unwind out of, don't
3259      emit any EH unwind information.  Note that if exceptions aren't
3260      enabled, we won't have collected nothrow information, and if we
3261      asked for asynchronous tables, we always want this info.  */
3262   if (for_eh)
3263     {
3264       bool any_eh_needed = !flag_exceptions || flag_asynchronous_unwind_tables;
3265
3266       for (i = 0; i < fde_table_in_use; i++)
3267         if (fde_table[i].uses_eh_lsda)
3268           any_eh_needed = any_lsda_needed = true;
3269         else if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
3270           any_eh_needed = true;
3271         else if (! fde_table[i].nothrow
3272                  && ! fde_table[i].all_throwers_are_sibcalls)
3273           any_eh_needed = true;
3274
3275       if (! any_eh_needed)
3276         return;
3277     }
3278
3279   /* We're going to be generating comments, so turn on app.  */
3280   if (flag_debug_asm)
3281     app_enable ();
3282
3283   if (for_eh)
3284     switch_to_eh_frame_section ();
3285   else
3286     {
3287       if (!debug_frame_section)
3288         debug_frame_section = get_section (DEBUG_FRAME_SECTION,
3289                                            SECTION_DEBUG, NULL);
3290       switch_to_section (debug_frame_section);
3291     }
3292
3293   ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
3294   ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
3295
3296   /* Output the CIE.  */
3297   ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
3298   ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
3299   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
3300     dw2_asm_output_data (4, 0xffffffff,
3301       "Initial length escape value indicating 64-bit DWARF extension");
3302   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
3303                         "Length of Common Information Entry");
3304   ASM_OUTPUT_LABEL (asm_out_file, l1);
3305
3306   /* Now that the CIE pointer is PC-relative for EH,
3307      use 0 to identify the CIE.  */
3308   dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
3309                        (for_eh ? 0 : DWARF_CIE_ID),
3310                        "CIE Identifier Tag");
3311
3312   /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
3313      use CIE version 1, unless that would produce incorrect results
3314      due to overflowing the return register column.  */
3315   return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
3316   dw_cie_version = 1;
3317   if (return_reg >= 256 || dwarf_version > 2)
3318     dw_cie_version = 3;
3319   dw2_asm_output_data (1, dw_cie_version, "CIE Version");
3320
3321   augmentation[0] = 0;
3322   augmentation_size = 0;
3323   if (for_eh)
3324     {
3325       char *p;
3326
3327       /* Augmentation:
3328          z      Indicates that a uleb128 is present to size the
3329                 augmentation section.
3330          L      Indicates the encoding (and thus presence) of
3331                 an LSDA pointer in the FDE augmentation.
3332          R      Indicates a non-default pointer encoding for
3333                 FDE code pointers.
3334          P      Indicates the presence of an encoding + language
3335                 personality routine in the CIE augmentation.  */
3336
3337       fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
3338       per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
3339       lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
3340
3341       p = augmentation + 1;
3342       if (eh_personality_libfunc)
3343         {
3344           *p++ = 'P';
3345           augmentation_size += 1 + size_of_encoded_value (per_encoding);
3346           assemble_external_libcall (eh_personality_libfunc);
3347         }
3348       if (any_lsda_needed)
3349         {
3350           *p++ = 'L';
3351           augmentation_size += 1;
3352         }
3353       if (fde_encoding != DW_EH_PE_absptr)
3354         {
3355           *p++ = 'R';
3356           augmentation_size += 1;
3357         }
3358       if (p > augmentation + 1)
3359         {
3360           augmentation[0] = 'z';
3361           *p = '\0';
3362         }
3363
3364       /* Ug.  Some platforms can't do unaligned dynamic relocations at all.  */
3365       if (eh_personality_libfunc && per_encoding == DW_EH_PE_aligned)
3366         {
3367           int offset = (  4             /* Length */
3368                         + 4             /* CIE Id */
3369                         + 1             /* CIE version */
3370                         + strlen (augmentation) + 1     /* Augmentation */
3371                         + size_of_uleb128 (1)           /* Code alignment */
3372                         + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
3373                         + 1             /* RA column */
3374                         + 1             /* Augmentation size */
3375                         + 1             /* Personality encoding */ );
3376           int pad = -offset & (PTR_SIZE - 1);
3377
3378           augmentation_size += pad;
3379
3380           /* Augmentations should be small, so there's scarce need to
3381              iterate for a solution.  Die if we exceed one uleb128 byte.  */
3382           gcc_assert (size_of_uleb128 (augmentation_size) == 1);
3383         }
3384     }
3385
3386   dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
3387   dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
3388   dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
3389                                "CIE Data Alignment Factor");
3390
3391   if (dw_cie_version == 1)
3392     dw2_asm_output_data (1, return_reg, "CIE RA Column");
3393   else
3394     dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
3395
3396   if (augmentation[0])
3397     {
3398       dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
3399       if (eh_personality_libfunc)
3400         {
3401           dw2_asm_output_data (1, per_encoding, "Personality (%s)",
3402                                eh_data_format_name (per_encoding));
3403           dw2_asm_output_encoded_addr_rtx (per_encoding,
3404                                            eh_personality_libfunc,
3405                                            true, NULL);
3406         }
3407
3408       if (any_lsda_needed)
3409         dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
3410                              eh_data_format_name (lsda_encoding));
3411
3412       if (fde_encoding != DW_EH_PE_absptr)
3413         dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
3414                              eh_data_format_name (fde_encoding));
3415     }
3416
3417   for (cfi = cie_cfi_head; cfi != NULL; cfi = cfi->dw_cfi_next)
3418     output_cfi (cfi, NULL, for_eh);
3419
3420   /* Pad the CIE out to an address sized boundary.  */
3421   ASM_OUTPUT_ALIGN (asm_out_file,
3422                     floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
3423   ASM_OUTPUT_LABEL (asm_out_file, l2);
3424
3425   /* Loop through all of the FDE's.  */
3426   for (i = 0; i < fde_table_in_use; i++)
3427     {
3428       fde = &fde_table[i];
3429
3430       /* Don't emit EH unwind info for leaf functions that don't need it.  */
3431       if (for_eh && !flag_asynchronous_unwind_tables && flag_exceptions
3432           && (fde->nothrow || fde->all_throwers_are_sibcalls)
3433           && ! (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
3434           && !fde->uses_eh_lsda)
3435         continue;
3436
3437       targetm.asm_out.unwind_label (asm_out_file, fde->decl, for_eh, /* empty */ 0);
3438       targetm.asm_out.internal_label (asm_out_file, FDE_LABEL, for_eh + i * 2);
3439       ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + i * 2);
3440       ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + i * 2);
3441       if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
3442         dw2_asm_output_data (4, 0xffffffff,
3443                              "Initial length escape value indicating 64-bit DWARF extension");
3444       dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
3445                             "FDE Length");
3446       ASM_OUTPUT_LABEL (asm_out_file, l1);
3447
3448       if (for_eh)
3449         dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
3450       else
3451         dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
3452                                debug_frame_section, "FDE CIE offset");
3453
3454       if (for_eh)
3455         {
3456           if (fde->dw_fde_switched_sections)
3457             {
3458               rtx sym_ref2 = gen_rtx_SYMBOL_REF (Pmode,
3459                                       fde->dw_fde_unlikely_section_label);
3460               rtx sym_ref3= gen_rtx_SYMBOL_REF (Pmode,
3461                                       fde->dw_fde_hot_section_label);
3462               SYMBOL_REF_FLAGS (sym_ref2) |= SYMBOL_FLAG_LOCAL;
3463               SYMBOL_REF_FLAGS (sym_ref3) |= SYMBOL_FLAG_LOCAL;
3464               dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref3, false,
3465                                                "FDE initial location");
3466               dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
3467                                     fde->dw_fde_hot_section_end_label,
3468                                     fde->dw_fde_hot_section_label,
3469                                     "FDE address range");
3470               dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref2, false,
3471                                                "FDE initial location");
3472               dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
3473                                     fde->dw_fde_unlikely_section_end_label,
3474                                     fde->dw_fde_unlikely_section_label,
3475                                     "FDE address range");
3476             }
3477           else
3478             {
3479               rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, fde->dw_fde_begin);
3480               SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
3481               dw2_asm_output_encoded_addr_rtx (fde_encoding,
3482                                                sym_ref,
3483                                                false,
3484                                                "FDE initial location");
3485               dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
3486                                     fde->dw_fde_end, fde->dw_fde_begin,
3487                                     "FDE address range");
3488             }
3489         }
3490       else
3491         {
3492           if (fde->dw_fde_switched_sections)
3493             {
3494               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
3495                                    fde->dw_fde_hot_section_label,
3496                                    "FDE initial location");
3497               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
3498                                     fde->dw_fde_hot_section_end_label,
3499                                     fde->dw_fde_hot_section_label,
3500                                     "FDE address range");
3501               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
3502                                    fde->dw_fde_unlikely_section_label,
3503                                    "FDE initial location");
3504               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
3505                                     fde->dw_fde_unlikely_section_end_label,
3506                                     fde->dw_fde_unlikely_section_label,
3507                                     "FDE address range");
3508             }
3509           else
3510             {
3511               dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
3512                                    "FDE initial location");
3513               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
3514                                     fde->dw_fde_end, fde->dw_fde_begin,
3515                                     "FDE address range");
3516             }
3517         }
3518
3519       if (augmentation[0])
3520         {
3521           if (any_lsda_needed)
3522             {
3523               int size = size_of_encoded_value (lsda_encoding);
3524
3525               if (lsda_encoding == DW_EH_PE_aligned)
3526                 {
3527                   int offset = (  4             /* Length */
3528                                 + 4             /* CIE offset */
3529                                 + 2 * size_of_encoded_value (fde_encoding)
3530                                 + 1             /* Augmentation size */ );
3531                   int pad = -offset & (PTR_SIZE - 1);
3532
3533                   size += pad;
3534                   gcc_assert (size_of_uleb128 (size) == 1);
3535                 }
3536
3537               dw2_asm_output_data_uleb128 (size, "Augmentation size");
3538
3539               if (fde->uses_eh_lsda)
3540                 {
3541                   ASM_GENERATE_INTERNAL_LABEL (l1, "LLSDA",
3542                                                fde->funcdef_number);
3543                   dw2_asm_output_encoded_addr_rtx (
3544                         lsda_encoding, gen_rtx_SYMBOL_REF (Pmode, l1),
3545                         false, "Language Specific Data Area");
3546                 }
3547               else
3548                 {
3549                   if (lsda_encoding == DW_EH_PE_aligned)
3550                     ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
3551                   dw2_asm_output_data
3552                     (size_of_encoded_value (lsda_encoding), 0,
3553                      "Language Specific Data Area (none)");
3554                 }
3555             }
3556           else
3557             dw2_asm_output_data_uleb128 (0, "Augmentation size");
3558         }
3559
3560       /* Loop through the Call Frame Instructions associated with
3561          this FDE.  */
3562       fde->dw_fde_current_label = fde->dw_fde_begin;
3563       for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
3564         output_cfi (cfi, fde, for_eh);
3565
3566       /* Pad the FDE out to an address sized boundary.  */
3567       ASM_OUTPUT_ALIGN (asm_out_file,
3568                         floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
3569       ASM_OUTPUT_LABEL (asm_out_file, l2);
3570     }
3571
3572   if (for_eh && targetm.terminate_dw2_eh_frame_info)
3573     dw2_asm_output_data (4, 0, "End of Table");
3574 #ifdef MIPS_DEBUGGING_INFO
3575   /* Work around Irix 6 assembler bug whereby labels at the end of a section
3576      get a value of 0.  Putting .align 0 after the label fixes it.  */
3577   ASM_OUTPUT_ALIGN (asm_out_file, 0);
3578 #endif
3579
3580   /* Turn off app to make assembly quicker.  */
3581   if (flag_debug_asm)
3582     app_disable ();
3583 }
3584
3585 /* Output a marker (i.e. a label) for the beginning of a function, before
3586    the prologue.  */
3587
3588 void
3589 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
3590                           const char *file ATTRIBUTE_UNUSED)
3591 {
3592   char label[MAX_ARTIFICIAL_LABEL_BYTES];
3593   char * dup_label;
3594   dw_fde_ref fde;
3595   section *fnsec;
3596
3597   current_function_func_begin_label = NULL;
3598
3599 #ifdef TARGET_UNWIND_INFO
3600   /* ??? current_function_func_begin_label is also used by except.c
3601      for call-site information.  We must emit this label if it might
3602      be used.  */
3603   if ((! flag_exceptions || USING_SJLJ_EXCEPTIONS)
3604       && ! dwarf2out_do_frame ())
3605     return;
3606 #else
3607   if (! dwarf2out_do_frame ())
3608     return;
3609 #endif
3610
3611   fnsec = function_section (current_function_decl);
3612   switch_to_section (fnsec);
3613   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
3614                                current_function_funcdef_no);
3615   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
3616                           current_function_funcdef_no);
3617   dup_label = xstrdup (label);
3618   current_function_func_begin_label = dup_label;
3619
3620 #ifdef TARGET_UNWIND_INFO
3621   /* We can elide the fde allocation if we're not emitting debug info.  */
3622   if (! dwarf2out_do_frame ())
3623     return;
3624 #endif
3625
3626   /* Expand the fde table if necessary.  */
3627   if (fde_table_in_use == fde_table_allocated)
3628     {
3629       fde_table_allocated += FDE_TABLE_INCREMENT;
3630       fde_table = GGC_RESIZEVEC (dw_fde_node, fde_table, fde_table_allocated);
3631       memset (fde_table + fde_table_in_use, 0,
3632               FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
3633     }
3634
3635   /* Record the FDE associated with this function.  */
3636   current_funcdef_fde = fde_table_in_use;
3637
3638   /* Add the new FDE at the end of the fde_table.  */
3639   fde = &fde_table[fde_table_in_use++];
3640   fde->decl = current_function_decl;
3641   fde->dw_fde_begin = dup_label;
3642   fde->dw_fde_current_label = dup_label;
3643   fde->dw_fde_hot_section_label = NULL;
3644   fde->dw_fde_hot_section_end_label = NULL;
3645   fde->dw_fde_unlikely_section_label = NULL;
3646   fde->dw_fde_unlikely_section_end_label = NULL;
3647   fde->dw_fde_switched_sections = false;
3648   fde->dw_fde_end = NULL;
3649   fde->dw_fde_cfi = NULL;
3650   fde->funcdef_number = current_function_funcdef_no;
3651   fde->nothrow = crtl->nothrow;
3652   fde->uses_eh_lsda = crtl->uses_eh_lsda;
3653   fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
3654   fde->drap_reg = INVALID_REGNUM;
3655   fde->vdrap_reg = INVALID_REGNUM;
3656   if (flag_reorder_blocks_and_partition)
3657     {
3658       section *unlikelysec;
3659       if (first_function_block_is_cold)
3660         fde->in_std_section = 1;
3661       else
3662         fde->in_std_section
3663           = (fnsec == text_section
3664              || (cold_text_section && fnsec == cold_text_section));
3665       unlikelysec = unlikely_text_section ();
3666       fde->cold_in_std_section
3667         = (unlikelysec == text_section
3668            || (cold_text_section && unlikelysec == cold_text_section));
3669     }
3670   else
3671     {
3672       fde->in_std_section
3673         = (fnsec == text_section
3674            || (cold_text_section && fnsec == cold_text_section));
3675       fde->cold_in_std_section = 0;
3676     }
3677
3678   args_size = old_args_size = 0;
3679
3680   /* We only want to output line number information for the genuine dwarf2
3681      prologue case, not the eh frame case.  */
3682 #ifdef DWARF2_DEBUGGING_INFO
3683   if (file)
3684     dwarf2out_source_line (line, file, 0, true);
3685 #endif
3686
3687   if (dwarf2out_do_cfi_asm ())
3688     {
3689       int enc;
3690       rtx ref;
3691
3692       fprintf (asm_out_file, "\t.cfi_startproc\n");
3693
3694       if (eh_personality_libfunc)
3695         {
3696           enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1); 
3697           ref = eh_personality_libfunc;
3698
3699           /* ??? The GAS support isn't entirely consistent.  We have to
3700              handle indirect support ourselves, but PC-relative is done
3701              in the assembler.  Further, the assembler can't handle any
3702              of the weirder relocation types.  */
3703           if (enc & DW_EH_PE_indirect)
3704             ref = dw2_force_const_mem (ref, true);
3705
3706           fprintf (asm_out_file, "\t.cfi_personality 0x%x,", enc);
3707           output_addr_const (asm_out_file, ref);
3708           fputc ('\n', asm_out_file);
3709         }
3710
3711       if (crtl->uses_eh_lsda)
3712         {
3713           char lab[20];
3714
3715           enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
3716           ASM_GENERATE_INTERNAL_LABEL (lab, "LLSDA",
3717                                        current_function_funcdef_no);
3718           ref = gen_rtx_SYMBOL_REF (Pmode, lab);
3719           SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
3720
3721           if (enc & DW_EH_PE_indirect)
3722             ref = dw2_force_const_mem (ref, true);
3723
3724           fprintf (asm_out_file, "\t.cfi_lsda 0x%x,", enc);
3725           output_addr_const (asm_out_file, ref);
3726           fputc ('\n', asm_out_file);
3727         }
3728     }
3729 }
3730
3731 /* Output a marker (i.e. a label) for the absolute end of the generated code
3732    for a function definition.  This gets called *after* the epilogue code has
3733    been generated.  */
3734
3735 void
3736 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
3737                         const char *file ATTRIBUTE_UNUSED)
3738 {
3739   dw_fde_ref fde;
3740   char label[MAX_ARTIFICIAL_LABEL_BYTES];
3741
3742 #ifdef DWARF2_DEBUGGING_INFO
3743   last_var_location_insn = NULL_RTX;
3744 #endif
3745
3746   if (dwarf2out_do_cfi_asm ())
3747     fprintf (asm_out_file, "\t.cfi_endproc\n");
3748
3749   /* Output a label to mark the endpoint of the code generated for this
3750      function.  */
3751   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
3752                                current_function_funcdef_no);
3753   ASM_OUTPUT_LABEL (asm_out_file, label);
3754   fde = current_fde ();
3755   gcc_assert (fde != NULL);
3756   fde->dw_fde_end = xstrdup (label);
3757 }
3758
3759 void
3760 dwarf2out_frame_init (void)
3761 {
3762   /* Allocate the initial hunk of the fde_table.  */
3763   fde_table = GGC_CNEWVEC (dw_fde_node, FDE_TABLE_INCREMENT);
3764   fde_table_allocated = FDE_TABLE_INCREMENT;
3765   fde_table_in_use = 0;
3766
3767   /* Generate the CFA instructions common to all FDE's.  Do it now for the
3768      sake of lookup_cfa.  */
3769
3770   /* On entry, the Canonical Frame Address is at SP.  */
3771   dwarf2out_def_cfa (NULL, STACK_POINTER_REGNUM, INCOMING_FRAME_SP_OFFSET);
3772
3773 #ifdef DWARF2_UNWIND_INFO
3774   if (DWARF2_UNWIND_INFO || DWARF2_FRAME_INFO)
3775     initial_return_save (INCOMING_RETURN_ADDR_RTX);
3776 #endif
3777 }
3778
3779 void
3780 dwarf2out_frame_finish (void)
3781 {
3782   /* Output call frame information.  */
3783   if (DWARF2_FRAME_INFO)
3784     output_call_frame_info (0);
3785
3786 #ifndef TARGET_UNWIND_INFO
3787   /* Output another copy for the unwinder.  */
3788   if (! USING_SJLJ_EXCEPTIONS && (flag_unwind_tables || flag_exceptions))
3789     output_call_frame_info (1);
3790 #endif
3791 }
3792
3793 /* Note that the current function section is being used for code.  */
3794
3795 static void
3796 dwarf2out_note_section_used (void)
3797 {
3798   section *sec = current_function_section ();
3799   if (sec == text_section)
3800     text_section_used = true;
3801   else if (sec == cold_text_section)
3802     cold_text_section_used = true;
3803 }
3804
3805 void
3806 dwarf2out_switch_text_section (void)
3807 {
3808   dw_fde_ref fde = current_fde ();
3809
3810   gcc_assert (cfun && fde);
3811
3812   fde->dw_fde_switched_sections = true;
3813   fde->dw_fde_hot_section_label = crtl->subsections.hot_section_label;
3814   fde->dw_fde_hot_section_end_label = crtl->subsections.hot_section_end_label;
3815   fde->dw_fde_unlikely_section_label = crtl->subsections.cold_section_label;
3816   fde->dw_fde_unlikely_section_end_label = crtl->subsections.cold_section_end_label;
3817   have_multiple_function_sections = true;
3818
3819   /* Reset the current label on switching text sections, so that we
3820      don't attempt to advance_loc4 between labels in different sections.  */
3821   fde->dw_fde_current_label = NULL;
3822
3823   /* There is no need to mark used sections when not debugging.  */
3824   if (cold_text_section != NULL)
3825     dwarf2out_note_section_used ();
3826 }
3827 #endif
3828 \f
3829 /* And now, the subset of the debugging information support code necessary
3830    for emitting location expressions.  */
3831
3832 /* Data about a single source file.  */
3833 struct GTY(()) dwarf_file_data {
3834   const char * filename;
3835   int emitted_number;
3836 };
3837
3838 typedef struct dw_val_struct *dw_val_ref;
3839 typedef struct die_struct *dw_die_ref;
3840 typedef const struct die_struct *const_dw_die_ref;
3841 typedef struct dw_loc_descr_struct *dw_loc_descr_ref;
3842 typedef struct dw_loc_list_struct *dw_loc_list_ref;
3843
3844 typedef struct GTY(()) deferred_locations_struct
3845 {
3846   tree variable;
3847   dw_die_ref die;
3848 } deferred_locations;
3849
3850 DEF_VEC_O(deferred_locations);
3851 DEF_VEC_ALLOC_O(deferred_locations,gc);
3852
3853 static GTY(()) VEC(deferred_locations, gc) *deferred_locations_list;
3854
3855 /* Each DIE may have a series of attribute/value pairs.  Values
3856    can take on several forms.  The forms that are used in this
3857    implementation are listed below.  */
3858
3859 enum dw_val_class
3860 {
3861   dw_val_class_addr,
3862   dw_val_class_offset,
3863   dw_val_class_loc,
3864   dw_val_class_loc_list,
3865   dw_val_class_range_list,
3866   dw_val_class_const,
3867   dw_val_class_unsigned_const,
3868   dw_val_class_long_long,
3869   dw_val_class_vec,
3870   dw_val_class_flag,
3871   dw_val_class_die_ref,
3872   dw_val_class_fde_ref,
3873   dw_val_class_lbl_id,
3874   dw_val_class_lineptr,
3875   dw_val_class_str,
3876   dw_val_class_macptr,
3877   dw_val_class_file
3878 };
3879
3880 /* Describe a double word constant value.  */
3881 /* ??? Every instance of long_long in the code really means CONST_DOUBLE.  */
3882
3883 typedef struct GTY(()) dw_long_long_struct {
3884   unsigned long hi;
3885   unsigned long low;
3886 }
3887 dw_long_long_const;
3888
3889 /* Describe a floating point constant value, or a vector constant value.  */
3890
3891 typedef struct GTY(()) dw_vec_struct {
3892   unsigned char * GTY((length ("%h.length"))) array;
3893   unsigned length;
3894   unsigned elt_size;
3895 }
3896 dw_vec_const;
3897
3898 /* The dw_val_node describes an attribute's value, as it is
3899    represented internally.  */
3900
3901 typedef struct GTY(()) dw_val_struct {
3902   enum dw_val_class val_class;
3903   union dw_val_struct_union
3904     {
3905       rtx GTY ((tag ("dw_val_class_addr"))) val_addr;
3906       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_offset"))) val_offset;
3907       dw_loc_list_ref GTY ((tag ("dw_val_class_loc_list"))) val_loc_list;
3908       dw_loc_descr_ref GTY ((tag ("dw_val_class_loc"))) val_loc;
3909       HOST_WIDE_INT GTY ((default)) val_int;
3910       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_unsigned_const"))) val_unsigned;
3911       dw_long_long_const GTY ((tag ("dw_val_class_long_long"))) val_long_long;
3912       dw_vec_const GTY ((tag ("dw_val_class_vec"))) val_vec;
3913       struct dw_val_die_union
3914         {
3915           dw_die_ref die;
3916           int external;
3917         } GTY ((tag ("dw_val_class_die_ref"))) val_die_ref;
3918       unsigned GTY ((tag ("dw_val_class_fde_ref"))) val_fde_index;
3919       struct indirect_string_node * GTY ((tag ("dw_val_class_str"))) val_str;
3920       char * GTY ((tag ("dw_val_class_lbl_id"))) val_lbl_id;
3921       unsigned char GTY ((tag ("dw_val_class_flag"))) val_flag;
3922       struct dwarf_file_data * GTY ((tag ("dw_val_class_file"))) val_file;
3923     }
3924   GTY ((desc ("%1.val_class"))) v;
3925 }
3926 dw_val_node;
3927
3928 /* Locations in memory are described using a sequence of stack machine
3929    operations.  */
3930
3931 typedef struct GTY(()) dw_loc_descr_struct {
3932   dw_loc_descr_ref dw_loc_next;
3933   ENUM_BITFIELD (dwarf_location_atom) dw_loc_opc : 8;
3934   /* Used to distinguish DW_OP_addr with a direct symbol relocation
3935      from DW_OP_addr with a dtp-relative symbol relocation.  */
3936   unsigned int dtprel : 1;
3937   int dw_loc_addr;
3938   dw_val_node dw_loc_oprnd1;
3939   dw_val_node dw_loc_oprnd2;
3940 }
3941 dw_loc_descr_node;
3942
3943 /* Location lists are ranges + location descriptions for that range,
3944    so you can track variables that are in different places over
3945    their entire life.  */
3946 typedef struct GTY(()) dw_loc_list_struct {
3947   dw_loc_list_ref dw_loc_next;
3948   const char *begin; /* Label for begin address of range */
3949   const char *end;  /* Label for end address of range */
3950   char *ll_symbol; /* Label for beginning of location list.
3951                       Only on head of list */
3952   const char *section; /* Section this loclist is relative to */
3953   dw_loc_descr_ref expr;
3954 } dw_loc_list_node;
3955
3956 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
3957
3958 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
3959
3960 /* Convert a DWARF stack opcode into its string name.  */
3961
3962 static const char *
3963 dwarf_stack_op_name (unsigned int op)
3964 {
3965   switch (op)
3966     {
3967     case DW_OP_addr:
3968       return "DW_OP_addr";
3969     case DW_OP_deref:
3970       return "DW_OP_deref";
3971     case DW_OP_const1u:
3972       return "DW_OP_const1u";
3973     case DW_OP_const1s:
3974       return "DW_OP_const1s";
3975     case DW_OP_const2u:
3976       return "DW_OP_const2u";
3977     case DW_OP_const2s:
3978       return "DW_OP_const2s";
3979     case DW_OP_const4u:
3980       return "DW_OP_const4u";
3981     case DW_OP_const4s:
3982       return "DW_OP_const4s";
3983     case DW_OP_const8u:
3984       return "DW_OP_const8u";
3985     case DW_OP_const8s:
3986       return "DW_OP_const8s";
3987     case DW_OP_constu:
3988       return "DW_OP_constu";
3989     case DW_OP_consts:
3990       return "DW_OP_consts";
3991     case DW_OP_dup:
3992       return "DW_OP_dup";
3993     case DW_OP_drop:
3994       return "DW_OP_drop";
3995     case DW_OP_over:
3996       return "DW_OP_over";
3997     case DW_OP_pick:
3998       return "DW_OP_pick";
3999     case DW_OP_swap:
4000       return "DW_OP_swap";
4001     case DW_OP_rot:
4002       return "DW_OP_rot";
4003     case DW_OP_xderef:
4004       return "DW_OP_xderef";
4005     case DW_OP_abs:
4006       return "DW_OP_abs";
4007     case DW_OP_and:
4008       return "DW_OP_and";
4009     case DW_OP_div:
4010       return "DW_OP_div";
4011     case DW_OP_minus:
4012       return "DW_OP_minus";
4013     case DW_OP_mod:
4014       return "DW_OP_mod";
4015     case DW_OP_mul:
4016       return "DW_OP_mul";
4017     case DW_OP_neg:
4018       return "DW_OP_neg";
4019     case DW_OP_not:
4020       return "DW_OP_not";
4021     case DW_OP_or:
4022       return "DW_OP_or";
4023     case DW_OP_plus:
4024       return "DW_OP_plus";
4025     case DW_OP_plus_uconst:
4026       return "DW_OP_plus_uconst";
4027     case DW_OP_shl:
4028       return "DW_OP_shl";
4029     case DW_OP_shr:
4030       return "DW_OP_shr";
4031     case DW_OP_shra:
4032       return "DW_OP_shra";
4033     case DW_OP_xor:
4034       return "DW_OP_xor";
4035     case DW_OP_bra:
4036       return "DW_OP_bra";
4037     case DW_OP_eq:
4038       return "DW_OP_eq";
4039     case DW_OP_ge:
4040       return "DW_OP_ge";
4041     case DW_OP_gt:
4042       return "DW_OP_gt";
4043     case DW_OP_le:
4044       return "DW_OP_le";
4045     case DW_OP_lt:
4046       return "DW_OP_lt";
4047     case DW_OP_ne:
4048       return "DW_OP_ne";
4049     case DW_OP_skip:
4050       return "DW_OP_skip";
4051     case DW_OP_lit0:
4052       return "DW_OP_lit0";
4053     case DW_OP_lit1:
4054       return "DW_OP_lit1";
4055     case DW_OP_lit2:
4056       return "DW_OP_lit2";
4057     case DW_OP_lit3:
4058       return "DW_OP_lit3";
4059     case DW_OP_lit4:
4060       return "DW_OP_lit4";
4061     case DW_OP_lit5:
4062       return "DW_OP_lit5";
4063     case DW_OP_lit6:
4064       return "DW_OP_lit6";
4065     case DW_OP_lit7:
4066       return "DW_OP_lit7";
4067     case DW_OP_lit8:
4068       return "DW_OP_lit8";
4069     case DW_OP_lit9:
4070       return "DW_OP_lit9";
4071     case DW_OP_lit10:
4072       return "DW_OP_lit10";
4073     case DW_OP_lit11:
4074       return "DW_OP_lit11";
4075     case DW_OP_lit12:
4076       return "DW_OP_lit12";
4077     case DW_OP_lit13:
4078       return "DW_OP_lit13";
4079     case DW_OP_lit14:
4080       return "DW_OP_lit14";
4081     case DW_OP_lit15:
4082       return "DW_OP_lit15";
4083     case DW_OP_lit16:
4084       return "DW_OP_lit16";
4085     case DW_OP_lit17:
4086       return "DW_OP_lit17";
4087     case DW_OP_lit18:
4088       return "DW_OP_lit18";
4089     case DW_OP_lit19:
4090       return "DW_OP_lit19";
4091     case DW_OP_lit20:
4092       return "DW_OP_lit20";
4093     case DW_OP_lit21:
4094       return "DW_OP_lit21";
4095     case DW_OP_lit22:
4096       return "DW_OP_lit22";
4097     case DW_OP_lit23:
4098       return "DW_OP_lit23";
4099     case DW_OP_lit24:
4100       return "DW_OP_lit24";
4101     case DW_OP_lit25:
4102       return "DW_OP_lit25";
4103     case DW_OP_lit26:
4104       return "DW_OP_lit26";
4105     case DW_OP_lit27:
4106       return "DW_OP_lit27";
4107     case DW_OP_lit28:
4108       return "DW_OP_lit28";
4109     case DW_OP_lit29:
4110       return "DW_OP_lit29";
4111     case DW_OP_lit30:
4112       return "DW_OP_lit30";
4113     case DW_OP_lit31:
4114       return "DW_OP_lit31";
4115     case DW_OP_reg0:
4116       return "DW_OP_reg0";
4117     case DW_OP_reg1:
4118       return "DW_OP_reg1";
4119     case DW_OP_reg2:
4120       return "DW_OP_reg2";
4121     case DW_OP_reg3:
4122       return "DW_OP_reg3";
4123     case DW_OP_reg4:
4124       return "DW_OP_reg4";
4125     case DW_OP_reg5:
4126       return "DW_OP_reg5";
4127     case DW_OP_reg6:
4128       return "DW_OP_reg6";
4129     case DW_OP_reg7:
4130       return "DW_OP_reg7";
4131     case DW_OP_reg8:
4132       return "DW_OP_reg8";
4133     case DW_OP_reg9:
4134       return "DW_OP_reg9";
4135     case DW_OP_reg10:
4136       return "DW_OP_reg10";
4137     case DW_OP_reg11:
4138       return "DW_OP_reg11";
4139     case DW_OP_reg12:
4140       return "DW_OP_reg12";
4141     case DW_OP_reg13:
4142       return "DW_OP_reg13";
4143     case DW_OP_reg14:
4144       return "DW_OP_reg14";
4145     case DW_OP_reg15:
4146       return "DW_OP_reg15";
4147     case DW_OP_reg16:
4148       return "DW_OP_reg16";
4149     case DW_OP_reg17:
4150       return "DW_OP_reg17";
4151     case DW_OP_reg18:
4152       return "DW_OP_reg18";
4153     case DW_OP_reg19:
4154       return "DW_OP_reg19";
4155     case DW_OP_reg20:
4156       return "DW_OP_reg20";
4157     case DW_OP_reg21:
4158       return "DW_OP_reg21";
4159     case DW_OP_reg22:
4160       return "DW_OP_reg22";
4161     case DW_OP_reg23:
4162       return "DW_OP_reg23";
4163     case DW_OP_reg24:
4164       return "DW_OP_reg24";
4165     case DW_OP_reg25:
4166       return "DW_OP_reg25";
4167     case DW_OP_reg26:
4168       return "DW_OP_reg26";
4169     case DW_OP_reg27:
4170       return "DW_OP_reg27";
4171     case DW_OP_reg28:
4172       return "DW_OP_reg28";
4173     case DW_OP_reg29:
4174       return "DW_OP_reg29";
4175     case DW_OP_reg30:
4176       return "DW_OP_reg30";
4177     case DW_OP_reg31:
4178       return "DW_OP_reg31";
4179     case DW_OP_breg0:
4180       return "DW_OP_breg0";
4181     case DW_OP_breg1:
4182       return "DW_OP_breg1";
4183     case DW_OP_breg2:
4184       return "DW_OP_breg2";
4185     case DW_OP_breg3:
4186       return "DW_OP_breg3";
4187     case DW_OP_breg4:
4188       return "DW_OP_breg4";
4189     case DW_OP_breg5:
4190       return "DW_OP_breg5";
4191     case DW_OP_breg6:
4192       return "DW_OP_breg6";
4193     case DW_OP_breg7:
4194       return "DW_OP_breg7";
4195     case DW_OP_breg8:
4196       return "DW_OP_breg8";
4197     case DW_OP_breg9:
4198       return "DW_OP_breg9";
4199     case DW_OP_breg10:
4200       return "DW_OP_breg10";
4201     case DW_OP_breg11:
4202       return "DW_OP_breg11";
4203     case DW_OP_breg12:
4204       return "DW_OP_breg12";
4205     case DW_OP_breg13:
4206       return "DW_OP_breg13";
4207     case DW_OP_breg14:
4208       return "DW_OP_breg14";
4209     case DW_OP_breg15:
4210       return "DW_OP_breg15";
4211     case DW_OP_breg16:
4212       return "DW_OP_breg16";
4213     case DW_OP_breg17:
4214       return "DW_OP_breg17";
4215     case DW_OP_breg18:
4216       return "DW_OP_breg18";
4217     case DW_OP_breg19:
4218       return "DW_OP_breg19";
4219     case DW_OP_breg20:
4220       return "DW_OP_breg20";
4221     case DW_OP_breg21:
4222       return "DW_OP_breg21";
4223     case DW_OP_breg22:
4224       return "DW_OP_breg22";
4225     case DW_OP_breg23:
4226       return "DW_OP_breg23";
4227     case DW_OP_breg24:
4228       return "DW_OP_breg24";
4229     case DW_OP_breg25:
4230       return "DW_OP_breg25";
4231     case DW_OP_breg26:
4232       return "DW_OP_breg26";
4233     case DW_OP_breg27:
4234       return "DW_OP_breg27";
4235     case DW_OP_breg28:
4236       return "DW_OP_breg28";
4237     case DW_OP_breg29:
4238       return "DW_OP_breg29";
4239     case DW_OP_breg30:
4240       return "DW_OP_breg30";
4241     case DW_OP_breg31:
4242       return "DW_OP_breg31";
4243     case DW_OP_regx:
4244       return "DW_OP_regx";
4245     case DW_OP_fbreg:
4246       return "DW_OP_fbreg";
4247     case DW_OP_bregx:
4248       return "DW_OP_bregx";
4249     case DW_OP_piece:
4250       return "DW_OP_piece";
4251     case DW_OP_deref_size:
4252       return "DW_OP_deref_size";
4253     case DW_OP_xderef_size:
4254       return "DW_OP_xderef_size";
4255     case DW_OP_nop:
4256       return "DW_OP_nop";
4257
4258     case DW_OP_push_object_address:
4259       return "DW_OP_push_object_address";
4260     case DW_OP_call2:
4261       return "DW_OP_call2";
4262     case DW_OP_call4:
4263       return "DW_OP_call4";
4264     case DW_OP_call_ref:
4265       return "DW_OP_call_ref";
4266     case DW_OP_form_tls_address:
4267       return "DW_OP_form_tls_address";
4268     case DW_OP_call_frame_cfa:
4269       return "DW_OP_call_frame_cfa";
4270     case DW_OP_bit_piece:
4271       return "DW_OP_bit_piece";
4272
4273     case DW_OP_GNU_push_tls_address:
4274       return "DW_OP_GNU_push_tls_address";
4275     case DW_OP_GNU_uninit:
4276       return "DW_OP_GNU_uninit";
4277     case DW_OP_GNU_encoded_addr:
4278       return "DW_OP_GNU_encoded_addr";
4279
4280     default:
4281       return "OP_<unknown>";
4282     }
4283 }
4284
4285 /* Return a pointer to a newly allocated location description.  Location
4286    descriptions are simple expression terms that can be strung
4287    together to form more complicated location (address) descriptions.  */
4288
4289 static inline dw_loc_descr_ref
4290 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
4291                unsigned HOST_WIDE_INT oprnd2)
4292 {
4293   dw_loc_descr_ref descr = GGC_CNEW (dw_loc_descr_node);
4294
4295   descr->dw_loc_opc = op;
4296   descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
4297   descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
4298   descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
4299   descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
4300
4301   return descr;
4302 }
4303
4304 /* Return a pointer to a newly allocated location description for
4305    REG and OFFSET.  */
4306
4307 static inline dw_loc_descr_ref
4308 new_reg_loc_descr (unsigned int reg,  unsigned HOST_WIDE_INT offset)
4309 {
4310   if (reg <= 31)
4311     return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
4312                           offset, 0);
4313   else
4314     return new_loc_descr (DW_OP_bregx, reg, offset);
4315 }
4316
4317 /* Add a location description term to a location description expression.  */
4318
4319 static inline void
4320 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
4321 {
4322   dw_loc_descr_ref *d;
4323
4324   /* Find the end of the chain.  */
4325   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
4326     ;
4327
4328   *d = descr;
4329 }
4330
4331 /* Add a constant OFFSET to a location expression.  */
4332
4333 static void
4334 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
4335 {
4336   dw_loc_descr_ref loc;
4337   HOST_WIDE_INT *p;
4338
4339   gcc_assert (*list_head != NULL);
4340
4341   if (!offset)
4342     return;
4343
4344   /* Find the end of the chain.  */
4345   for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
4346     ;
4347
4348   p = NULL;
4349   if (loc->dw_loc_opc == DW_OP_fbreg
4350       || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
4351     p = &loc->dw_loc_oprnd1.v.val_int;
4352   else if (loc->dw_loc_opc == DW_OP_bregx)
4353     p = &loc->dw_loc_oprnd2.v.val_int;
4354
4355   /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
4356      offset.  Don't optimize if an signed integer overflow would happen.  */
4357   if (p != NULL
4358       && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
4359           || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
4360     *p += offset;
4361
4362   else if (offset > 0)
4363     loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
4364
4365   else
4366     {
4367       loc->dw_loc_next = int_loc_descriptor (offset);
4368       add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_plus, 0, 0));
4369     }
4370 }
4371
4372 /* Return the size of a location descriptor.  */
4373
4374 static unsigned long
4375 size_of_loc_descr (dw_loc_descr_ref loc)
4376 {
4377   unsigned long size = 1;
4378
4379   switch (loc->dw_loc_opc)
4380     {
4381     case DW_OP_addr:
4382       size += DWARF2_ADDR_SIZE;
4383       break;
4384     case DW_OP_const1u:
4385     case DW_OP_const1s:
4386       size += 1;
4387       break;
4388     case DW_OP_const2u:
4389     case DW_OP_const2s:
4390       size += 2;
4391       break;
4392     case DW_OP_const4u:
4393     case DW_OP_const4s:
4394       size += 4;
4395       break;
4396     case DW_OP_const8u:
4397     case DW_OP_const8s:
4398       size += 8;
4399       break;
4400     case DW_OP_constu:
4401       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4402       break;
4403     case DW_OP_consts:
4404       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4405       break;
4406     case DW_OP_pick:
4407       size += 1;
4408       break;
4409     case DW_OP_plus_uconst:
4410       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4411       break;
4412     case DW_OP_skip:
4413     case DW_OP_bra:
4414       size += 2;
4415       break;
4416     case DW_OP_breg0:
4417     case DW_OP_breg1:
4418     case DW_OP_breg2:
4419     case DW_OP_breg3:
4420     case DW_OP_breg4:
4421     case DW_OP_breg5:
4422     case DW_OP_breg6:
4423     case DW_OP_breg7:
4424     case DW_OP_breg8:
4425     case DW_OP_breg9:
4426     case DW_OP_breg10:
4427     case DW_OP_breg11:
4428     case DW_OP_breg12:
4429     case DW_OP_breg13:
4430     case DW_OP_breg14:
4431     case DW_OP_breg15:
4432     case DW_OP_breg16:
4433     case DW_OP_breg17:
4434     case DW_OP_breg18:
4435     case DW_OP_breg19:
4436     case DW_OP_breg20:
4437     case DW_OP_breg21:
4438     case DW_OP_breg22:
4439     case DW_OP_breg23:
4440     case DW_OP_breg24:
4441     case DW_OP_breg25:
4442     case DW_OP_breg26:
4443     case DW_OP_breg27:
4444     case DW_OP_breg28:
4445     case DW_OP_breg29:
4446     case DW_OP_breg30:
4447     case DW_OP_breg31:
4448       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4449       break;
4450     case DW_OP_regx:
4451       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4452       break;
4453     case DW_OP_fbreg:
4454       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4455       break;
4456     case DW_OP_bregx:
4457       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4458       size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
4459       break;
4460     case DW_OP_piece:
4461       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4462       break;
4463     case DW_OP_deref_size:
4464     case DW_OP_xderef_size:
4465       size += 1;
4466       break;
4467     case DW_OP_call2:
4468       size += 2;
4469       break;
4470     case DW_OP_call4:
4471       size += 4;
4472       break;
4473     case DW_OP_call_ref:
4474       size += DWARF2_ADDR_SIZE;
4475       break;
4476     default:
4477       break;
4478     }
4479
4480   return size;
4481 }
4482
4483 /* Return the size of a series of location descriptors.  */
4484
4485 static unsigned long
4486 size_of_locs (dw_loc_descr_ref loc)
4487 {
4488   dw_loc_descr_ref l;
4489   unsigned long size;
4490
4491   /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
4492      field, to avoid writing to a PCH file.  */
4493   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
4494     {
4495       if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
4496         break;
4497       size += size_of_loc_descr (l);
4498     }
4499   if (! l)
4500     return size;
4501
4502   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
4503     {
4504       l->dw_loc_addr = size;
4505       size += size_of_loc_descr (l);
4506     }
4507
4508   return size;
4509 }
4510
4511 /* Output location description stack opcode's operands (if any).  */
4512
4513 static void
4514 output_loc_operands (dw_loc_descr_ref loc)
4515 {
4516   dw_val_ref val1 = &loc->dw_loc_oprnd1;
4517   dw_val_ref val2 = &loc->dw_loc_oprnd2;
4518
4519   switch (loc->dw_loc_opc)
4520     {
4521 #ifdef DWARF2_DEBUGGING_INFO
4522     case DW_OP_const2u:
4523     case DW_OP_const2s:
4524       dw2_asm_output_data (2, val1->v.val_int, NULL);
4525       break;
4526     case DW_OP_const4u:
4527     case DW_OP_const4s:
4528       dw2_asm_output_data (4, val1->v.val_int, NULL);
4529       break;
4530     case DW_OP_const8u:
4531     case DW_OP_const8s:
4532       gcc_assert (HOST_BITS_PER_LONG >= 64);
4533       dw2_asm_output_data (8, val1->v.val_int, NULL);
4534       break;
4535     case DW_OP_skip:
4536     case DW_OP_bra:
4537       {
4538         int offset;
4539
4540         gcc_assert (val1->val_class == dw_val_class_loc);
4541         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
4542
4543         dw2_asm_output_data (2, offset, NULL);
4544       }
4545       break;
4546 #else
4547     case DW_OP_const2u:
4548     case DW_OP_const2s:
4549     case DW_OP_const4u:
4550     case DW_OP_const4s:
4551     case DW_OP_const8u:
4552     case DW_OP_const8s:
4553     case DW_OP_skip:
4554     case DW_OP_bra:
4555       /* We currently don't make any attempt to make sure these are
4556          aligned properly like we do for the main unwind info, so
4557          don't support emitting things larger than a byte if we're
4558          only doing unwinding.  */
4559       gcc_unreachable ();
4560 #endif
4561     case DW_OP_const1u:
4562     case DW_OP_const1s:
4563       dw2_asm_output_data (1, val1->v.val_int, NULL);
4564       break;
4565     case DW_OP_constu:
4566       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4567       break;
4568     case DW_OP_consts:
4569       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
4570       break;
4571     case DW_OP_pick:
4572       dw2_asm_output_data (1, val1->v.val_int, NULL);
4573       break;
4574     case DW_OP_plus_uconst:
4575       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4576       break;
4577     case DW_OP_breg0:
4578     case DW_OP_breg1:
4579     case DW_OP_breg2:
4580     case DW_OP_breg3:
4581     case DW_OP_breg4:
4582     case DW_OP_breg5:
4583     case DW_OP_breg6:
4584     case DW_OP_breg7:
4585     case DW_OP_breg8:
4586     case DW_OP_breg9:
4587     case DW_OP_breg10:
4588     case DW_OP_breg11:
4589     case DW_OP_breg12:
4590     case DW_OP_breg13:
4591     case DW_OP_breg14:
4592     case DW_OP_breg15:
4593     case DW_OP_breg16:
4594     case DW_OP_breg17:
4595     case DW_OP_breg18:
4596     case DW_OP_breg19:
4597     case DW_OP_breg20:
4598     case DW_OP_breg21:
4599     case DW_OP_breg22:
4600     case DW_OP_breg23:
4601     case DW_OP_breg24:
4602     case DW_OP_breg25:
4603     case DW_OP_breg26:
4604     case DW_OP_breg27:
4605     case DW_OP_breg28:
4606     case DW_OP_breg29:
4607     case DW_OP_breg30:
4608     case DW_OP_breg31:
4609       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
4610       break;
4611     case DW_OP_regx:
4612       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4613       break;
4614     case DW_OP_fbreg:
4615       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
4616       break;
4617     case DW_OP_bregx:
4618       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4619       dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
4620       break;
4621     case DW_OP_piece:
4622       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4623       break;
4624     case DW_OP_deref_size:
4625     case DW_OP_xderef_size:
4626       dw2_asm_output_data (1, val1->v.val_int, NULL);
4627       break;
4628
4629     case DW_OP_addr:
4630       if (loc->dtprel)
4631         {
4632           if (targetm.asm_out.output_dwarf_dtprel)
4633             {
4634               targetm.asm_out.output_dwarf_dtprel (asm_out_file,
4635                                                    DWARF2_ADDR_SIZE,
4636                                                    val1->v.val_addr);
4637               fputc ('\n', asm_out_file);
4638             }
4639           else
4640             gcc_unreachable ();
4641         }
4642       else
4643         {
4644 #ifdef DWARF2_DEBUGGING_INFO
4645           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
4646 #else
4647           gcc_unreachable ();
4648 #endif
4649         }
4650       break;
4651
4652     default:
4653       /* Other codes have no operands.  */
4654       break;
4655     }
4656 }
4657
4658 /* Output a sequence of location operations.  */
4659
4660 static void
4661 output_loc_sequence (dw_loc_descr_ref loc)
4662 {
4663   for (; loc != NULL; loc = loc->dw_loc_next)
4664     {
4665       /* Output the opcode.  */
4666       dw2_asm_output_data (1, loc->dw_loc_opc,
4667                            "%s", dwarf_stack_op_name (loc->dw_loc_opc));
4668
4669       /* Output the operand(s) (if any).  */
4670       output_loc_operands (loc);
4671     }
4672 }
4673
4674 /* Output location description stack opcode's operands (if any).
4675    The output is single bytes on a line, suitable for .cfi_escape.  */
4676
4677 static void
4678 output_loc_operands_raw (dw_loc_descr_ref loc)
4679 {
4680   dw_val_ref val1 = &loc->dw_loc_oprnd1;
4681   dw_val_ref val2 = &loc->dw_loc_oprnd2;
4682
4683   switch (loc->dw_loc_opc)
4684     {
4685     case DW_OP_addr:
4686       /* We cannot output addresses in .cfi_escape, only bytes.  */
4687       gcc_unreachable ();
4688
4689     case DW_OP_const1u:
4690     case DW_OP_const1s:
4691     case DW_OP_pick:
4692     case DW_OP_deref_size:
4693     case DW_OP_xderef_size:
4694       fputc (',', asm_out_file);
4695       dw2_asm_output_data_raw (1, val1->v.val_int);
4696       break;
4697
4698     case DW_OP_const2u:
4699     case DW_OP_const2s:
4700       fputc (',', asm_out_file);
4701       dw2_asm_output_data_raw (2, val1->v.val_int);
4702       break;
4703
4704     case DW_OP_const4u:
4705     case DW_OP_const4s:
4706       fputc (',', asm_out_file);
4707       dw2_asm_output_data_raw (4, val1->v.val_int);
4708       break;
4709
4710     case DW_OP_const8u:
4711     case DW_OP_const8s:
4712       gcc_assert (HOST_BITS_PER_LONG >= 64);
4713       fputc (',', asm_out_file);
4714       dw2_asm_output_data_raw (8, val1->v.val_int);
4715       break;
4716
4717     case DW_OP_skip:
4718     case DW_OP_bra:
4719       {
4720         int offset;
4721
4722         gcc_assert (val1->val_class == dw_val_class_loc);
4723         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
4724
4725         fputc (',', asm_out_file);
4726         dw2_asm_output_data_raw (2, offset);
4727       }
4728       break;
4729
4730     case DW_OP_constu:
4731     case DW_OP_plus_uconst:
4732     case DW_OP_regx:
4733     case DW_OP_piece:
4734       fputc (',', asm_out_file);
4735       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
4736       break;
4737
4738     case DW_OP_consts:
4739     case DW_OP_breg0:
4740     case DW_OP_breg1:
4741     case DW_OP_breg2:
4742     case DW_OP_breg3:
4743     case DW_OP_breg4:
4744     case DW_OP_breg5:
4745     case DW_OP_breg6:
4746     case DW_OP_breg7:
4747     case DW_OP_breg8:
4748     case DW_OP_breg9:
4749     case DW_OP_breg10:
4750     case DW_OP_breg11:
4751     case DW_OP_breg12:
4752     case DW_OP_breg13:
4753     case DW_OP_breg14:
4754     case DW_OP_breg15:
4755     case DW_OP_breg16:
4756     case DW_OP_breg17:
4757     case DW_OP_breg18:
4758     case DW_OP_breg19:
4759     case DW_OP_breg20:
4760     case DW_OP_breg21:
4761     case DW_OP_breg22:
4762     case DW_OP_breg23:
4763     case DW_OP_breg24:
4764     case DW_OP_breg25:
4765     case DW_OP_breg26:
4766     case DW_OP_breg27:
4767     case DW_OP_breg28:
4768     case DW_OP_breg29:
4769     case DW_OP_breg30:
4770     case DW_OP_breg31:
4771     case DW_OP_fbreg:
4772       fputc (',', asm_out_file);
4773       dw2_asm_output_data_sleb128_raw (val1->v.val_int);
4774       break;
4775
4776     case DW_OP_bregx:
4777       fputc (',', asm_out_file);
4778       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
4779       fputc (',', asm_out_file);
4780       dw2_asm_output_data_sleb128_raw (val2->v.val_int);
4781       break;
4782
4783     default:
4784       /* Other codes have no operands.  */
4785       break;
4786     }
4787 }
4788
4789 static void
4790 output_loc_sequence_raw (dw_loc_descr_ref loc)
4791 {
4792   while (1)
4793     {
4794       /* Output the opcode.  */
4795       fprintf (asm_out_file, "0x%x", loc->dw_loc_opc);
4796       output_loc_operands_raw (loc);
4797
4798       if (!loc->dw_loc_next)
4799         break;
4800       loc = loc->dw_loc_next;
4801
4802       fputc (',', asm_out_file);
4803     }
4804 }
4805
4806 /* This routine will generate the correct assembly data for a location
4807    description based on a cfi entry with a complex address.  */
4808
4809 static void
4810 output_cfa_loc (dw_cfi_ref cfi)
4811 {
4812   dw_loc_descr_ref loc;
4813   unsigned long size;
4814
4815   if (cfi->dw_cfi_opc == DW_CFA_expression)
4816     dw2_asm_output_data (1, cfi->dw_cfi_oprnd2.dw_cfi_reg_num, NULL);
4817
4818   /* Output the size of the block.  */
4819   loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
4820   size = size_of_locs (loc);
4821   dw2_asm_output_data_uleb128 (size, NULL);
4822
4823   /* Now output the operations themselves.  */
4824   output_loc_sequence (loc);
4825 }
4826
4827 /* Similar, but used for .cfi_escape.  */
4828
4829 static void
4830 output_cfa_loc_raw (dw_cfi_ref cfi)
4831 {
4832   dw_loc_descr_ref loc;
4833   unsigned long size;
4834
4835   if (cfi->dw_cfi_opc == DW_CFA_expression)
4836     fprintf (asm_out_file, "0x%x,", cfi->dw_cfi_oprnd2.dw_cfi_reg_num);
4837
4838   /* Output the size of the block.  */
4839   loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
4840   size = size_of_locs (loc);
4841   dw2_asm_output_data_uleb128_raw (size);
4842   fputc (',', asm_out_file);
4843
4844   /* Now output the operations themselves.  */
4845   output_loc_sequence_raw (loc);
4846 }
4847
4848 /* This function builds a dwarf location descriptor sequence from a
4849    dw_cfa_location, adding the given OFFSET to the result of the
4850    expression.  */
4851
4852 static struct dw_loc_descr_struct *
4853 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
4854 {
4855   struct dw_loc_descr_struct *head, *tmp;
4856
4857   offset += cfa->offset;
4858
4859   if (cfa->indirect)
4860     {
4861       head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
4862       head->dw_loc_oprnd1.val_class = dw_val_class_const;
4863       tmp = new_loc_descr (DW_OP_deref, 0, 0);
4864       add_loc_descr (&head, tmp);
4865       if (offset != 0)
4866         {
4867           tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
4868           add_loc_descr (&head, tmp);
4869         }
4870     }
4871   else
4872     head = new_reg_loc_descr (cfa->reg, offset);
4873
4874   return head;
4875 }
4876
4877 /* This function builds a dwarf location descriptor sequence for
4878    the address at OFFSET from the CFA when stack is aligned to
4879    ALIGNMENT byte.  */
4880
4881 static struct dw_loc_descr_struct *
4882 build_cfa_aligned_loc (HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
4883 {
4884   struct dw_loc_descr_struct *head;
4885   unsigned int dwarf_fp
4886     = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
4887
4888  /* When CFA is defined as FP+OFFSET, emulate stack alignment.  */
4889   if (cfa.reg == HARD_FRAME_POINTER_REGNUM && cfa.indirect == 0)
4890     {
4891       head = new_reg_loc_descr (dwarf_fp, 0);
4892       add_loc_descr (&head, int_loc_descriptor (alignment));
4893       add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
4894       loc_descr_plus_const (&head, offset);
4895     }
4896   else
4897     head = new_reg_loc_descr (dwarf_fp, offset);
4898   return head;
4899 }
4900
4901 /* This function fills in aa dw_cfa_location structure from a dwarf location
4902    descriptor sequence.  */
4903
4904 static void
4905 get_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_struct *loc)
4906 {
4907   struct dw_loc_descr_struct *ptr;
4908   cfa->offset = 0;
4909   cfa->base_offset = 0;
4910   cfa->indirect = 0;
4911   cfa->reg = -1;
4912
4913   for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
4914     {
4915       enum dwarf_location_atom op = ptr->dw_loc_opc;
4916
4917       switch (op)
4918         {
4919         case DW_OP_reg0:
4920         case DW_OP_reg1:
4921         case DW_OP_reg2:
4922         case DW_OP_reg3:
4923         case DW_OP_reg4:
4924         case DW_OP_reg5:
4925         case DW_OP_reg6:
4926         case DW_OP_reg7:
4927         case DW_OP_reg8:
4928         case DW_OP_reg9:
4929         case DW_OP_reg10:
4930         case DW_OP_reg11:
4931         case DW_OP_reg12:
4932         case DW_OP_reg13:
4933         case DW_OP_reg14:
4934         case DW_OP_reg15:
4935         case DW_OP_reg16:
4936         case DW_OP_reg17:
4937         case DW_OP_reg18:
4938         case DW_OP_reg19:
4939         case DW_OP_reg20:
4940         case DW_OP_reg21:
4941         case DW_OP_reg22:
4942         case DW_OP_reg23:
4943         case DW_OP_reg24:
4944         case DW_OP_reg25:
4945         case DW_OP_reg26:
4946         case DW_OP_reg27:
4947         case DW_OP_reg28:
4948         case DW_OP_reg29:
4949         case DW_OP_reg30:
4950         case DW_OP_reg31:
4951           cfa->reg = op - DW_OP_reg0;
4952           break;
4953         case DW_OP_regx:
4954           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
4955           break;
4956         case DW_OP_breg0:
4957         case DW_OP_breg1:
4958         case DW_OP_breg2:
4959         case DW_OP_breg3:
4960         case DW_OP_breg4:
4961         case DW_OP_breg5:
4962         case DW_OP_breg6:
4963         case DW_OP_breg7:
4964         case DW_OP_breg8:
4965         case DW_OP_breg9:
4966         case DW_OP_breg10:
4967         case DW_OP_breg11:
4968         case DW_OP_breg12:
4969         case DW_OP_breg13:
4970         case DW_OP_breg14:
4971         case DW_OP_breg15:
4972         case DW_OP_breg16:
4973         case DW_OP_breg17:
4974         case DW_OP_breg18:
4975         case DW_OP_breg19:
4976         case DW_OP_breg20:
4977         case DW_OP_breg21:
4978         case DW_OP_breg22:
4979         case DW_OP_breg23:
4980         case DW_OP_breg24:
4981         case DW_OP_breg25:
4982         case DW_OP_breg26:
4983         case DW_OP_breg27:
4984         case DW_OP_breg28:
4985         case DW_OP_breg29:
4986         case DW_OP_breg30:
4987         case DW_OP_breg31:
4988           cfa->reg = op - DW_OP_breg0;
4989           cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
4990           break;
4991         case DW_OP_bregx:
4992           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
4993           cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
4994           break;
4995         case DW_OP_deref:
4996           cfa->indirect = 1;
4997           break;
4998         case DW_OP_plus_uconst:
4999           cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
5000           break;
5001         default:
5002           internal_error ("DW_LOC_OP %s not implemented",
5003                           dwarf_stack_op_name (ptr->dw_loc_opc));
5004         }
5005     }
5006 }
5007 #endif /* .debug_frame support */
5008 \f
5009 /* And now, the support for symbolic debugging information.  */
5010 #ifdef DWARF2_DEBUGGING_INFO
5011
5012 /* .debug_str support.  */
5013 static int output_indirect_string (void **, void *);
5014
5015 static void dwarf2out_init (const char *);
5016 static void dwarf2out_finish (const char *);
5017 static void dwarf2out_define (unsigned int, const char *);
5018 static void dwarf2out_undef (unsigned int, const char *);
5019 static void dwarf2out_start_source_file (unsigned, const char *);
5020 static void dwarf2out_end_source_file (unsigned);
5021 static void dwarf2out_begin_block (unsigned, unsigned);
5022 static void dwarf2out_end_block (unsigned, unsigned);
5023 static bool dwarf2out_ignore_block (const_tree);
5024 static void dwarf2out_global_decl (tree);
5025 static void dwarf2out_type_decl (tree, int);
5026 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
5027 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
5028                                                  dw_die_ref);
5029 static void dwarf2out_abstract_function (tree);
5030 static void dwarf2out_var_location (rtx);
5031 static void dwarf2out_begin_function (tree);
5032 static void dwarf2out_set_name (tree, tree);
5033
5034 /* The debug hooks structure.  */
5035
5036 const struct gcc_debug_hooks dwarf2_debug_hooks =
5037 {
5038   dwarf2out_init,
5039   dwarf2out_finish,
5040   dwarf2out_define,
5041   dwarf2out_undef,
5042   dwarf2out_start_source_file,
5043   dwarf2out_end_source_file,
5044   dwarf2out_begin_block,
5045   dwarf2out_end_block,
5046   dwarf2out_ignore_block,
5047   dwarf2out_source_line,
5048   dwarf2out_begin_prologue,
5049   debug_nothing_int_charstar,   /* end_prologue */
5050   dwarf2out_end_epilogue,
5051   dwarf2out_begin_function,
5052   debug_nothing_int,            /* end_function */
5053   dwarf2out_decl,               /* function_decl */
5054   dwarf2out_global_decl,
5055   dwarf2out_type_decl,          /* type_decl */
5056   dwarf2out_imported_module_or_decl,
5057   debug_nothing_tree,           /* deferred_inline_function */
5058   /* The DWARF 2 backend tries to reduce debugging bloat by not
5059      emitting the abstract description of inline functions until
5060      something tries to reference them.  */
5061   dwarf2out_abstract_function,  /* outlining_inline_function */
5062   debug_nothing_rtx,            /* label */
5063   debug_nothing_int,            /* handle_pch */
5064   dwarf2out_var_location,
5065   dwarf2out_switch_text_section,
5066   dwarf2out_set_name,
5067   1                             /* start_end_main_source_file */
5068 };
5069 #endif
5070 \f
5071 /* NOTE: In the comments in this file, many references are made to
5072    "Debugging Information Entries".  This term is abbreviated as `DIE'
5073    throughout the remainder of this file.  */
5074
5075 /* An internal representation of the DWARF output is built, and then
5076    walked to generate the DWARF debugging info.  The walk of the internal
5077    representation is done after the entire program has been compiled.
5078    The types below are used to describe the internal representation.  */
5079
5080 /* Various DIE's use offsets relative to the beginning of the
5081    .debug_info section to refer to each other.  */
5082
5083 typedef long int dw_offset;
5084
5085 /* Define typedefs here to avoid circular dependencies.  */
5086
5087 typedef struct dw_attr_struct *dw_attr_ref;
5088 typedef struct dw_line_info_struct *dw_line_info_ref;
5089 typedef struct dw_separate_line_info_struct *dw_separate_line_info_ref;
5090 typedef struct pubname_struct *pubname_ref;
5091 typedef struct dw_ranges_struct *dw_ranges_ref;
5092 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
5093
5094 /* Each entry in the line_info_table maintains the file and
5095    line number associated with the label generated for that
5096    entry.  The label gives the PC value associated with
5097    the line number entry.  */
5098
5099 typedef struct GTY(()) dw_line_info_struct {
5100   unsigned long dw_file_num;
5101   unsigned long dw_line_num;
5102 }
5103 dw_line_info_entry;
5104
5105 /* Line information for functions in separate sections; each one gets its
5106    own sequence.  */
5107 typedef struct GTY(()) dw_separate_line_info_struct {
5108   unsigned long dw_file_num;
5109   unsigned long dw_line_num;
5110   unsigned long function;
5111 }
5112 dw_separate_line_info_entry;
5113
5114 /* Each DIE attribute has a field specifying the attribute kind,
5115    a link to the next attribute in the chain, and an attribute value.
5116    Attributes are typically linked below the DIE they modify.  */
5117
5118 typedef struct GTY(()) dw_attr_struct {
5119   enum dwarf_attribute dw_attr;
5120   dw_val_node dw_attr_val;
5121 }
5122 dw_attr_node;
5123
5124 DEF_VEC_O(dw_attr_node);
5125 DEF_VEC_ALLOC_O(dw_attr_node,gc);
5126
5127 /* The Debugging Information Entry (DIE) structure.  DIEs form a tree.
5128    The children of each node form a circular list linked by
5129    die_sib.  die_child points to the node *before* the "first" child node.  */
5130
5131 typedef struct GTY((chain_circular ("%h.die_sib"))) die_struct {
5132   enum dwarf_tag die_tag;
5133   char *die_symbol;
5134   VEC(dw_attr_node,gc) * die_attr;
5135   dw_die_ref die_parent;
5136   dw_die_ref die_child;
5137   dw_die_ref die_sib;
5138   dw_die_ref die_definition; /* ref from a specification to its definition */
5139   dw_offset die_offset;
5140   unsigned long die_abbrev;
5141   int die_mark;
5142   /* Die is used and must not be pruned as unused.  */
5143   int die_perennial_p;
5144   unsigned int decl_id;
5145 }
5146 die_node;
5147
5148 /* Evaluate 'expr' while 'c' is set to each child of DIE in order.  */
5149 #define FOR_EACH_CHILD(die, c, expr) do {       \
5150   c = die->die_child;                           \
5151   if (c) do {                                   \
5152     c = c->die_sib;                             \
5153     expr;                                       \
5154   } while (c != die->die_child);                \
5155 } while (0)
5156
5157 /* The pubname structure */
5158
5159 typedef struct GTY(()) pubname_struct {
5160   dw_die_ref die;
5161   const char *name;
5162 }
5163 pubname_entry;
5164
5165 DEF_VEC_O(pubname_entry);
5166 DEF_VEC_ALLOC_O(pubname_entry, gc);
5167
5168 struct GTY(()) dw_ranges_struct {
5169   /* If this is positive, it's a block number, otherwise it's a
5170      bitwise-negated index into dw_ranges_by_label.  */
5171   int num;
5172 };
5173
5174 struct GTY(()) dw_ranges_by_label_struct {
5175   const char *begin;
5176   const char *end;
5177 };
5178
5179 /* The limbo die list structure.  */
5180 typedef struct GTY(()) limbo_die_struct {
5181   dw_die_ref die;
5182   tree created_for;
5183   struct limbo_die_struct *next;
5184 }
5185 limbo_die_node;
5186
5187 /* How to start an assembler comment.  */
5188 #ifndef ASM_COMMENT_START
5189 #define ASM_COMMENT_START ";#"
5190 #endif
5191
5192 /* Define a macro which returns nonzero for a TYPE_DECL which was
5193    implicitly generated for a tagged type.
5194
5195    Note that unlike the gcc front end (which generates a NULL named
5196    TYPE_DECL node for each complete tagged type, each array type, and
5197    each function type node created) the g++ front end generates a
5198    _named_ TYPE_DECL node for each tagged type node created.
5199    These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
5200    generate a DW_TAG_typedef DIE for them.  */
5201
5202 #define TYPE_DECL_IS_STUB(decl)                         \
5203   (DECL_NAME (decl) == NULL_TREE                        \
5204    || (DECL_ARTIFICIAL (decl)                           \
5205        && is_tagged_type (TREE_TYPE (decl))             \
5206        && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl)))  \
5207            /* This is necessary for stub decls that     \
5208               appear in nested inline functions.  */    \
5209            || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
5210                && (decl_ultimate_origin (decl)          \
5211                    == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
5212
5213 /* Information concerning the compilation unit's programming
5214    language, and compiler version.  */
5215
5216 /* Fixed size portion of the DWARF compilation unit header.  */
5217 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
5218   (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
5219
5220 /* Fixed size portion of public names info.  */
5221 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
5222
5223 /* Fixed size portion of the address range info.  */
5224 #define DWARF_ARANGES_HEADER_SIZE                                       \
5225   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4,      \
5226                 DWARF2_ADDR_SIZE * 2)                                   \
5227    - DWARF_INITIAL_LENGTH_SIZE)
5228
5229 /* Size of padding portion in the address range info.  It must be
5230    aligned to twice the pointer size.  */
5231 #define DWARF_ARANGES_PAD_SIZE \
5232   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
5233                 DWARF2_ADDR_SIZE * 2)                              \
5234    - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
5235
5236 /* Use assembler line directives if available.  */
5237 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
5238 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
5239 #define DWARF2_ASM_LINE_DEBUG_INFO 1
5240 #else
5241 #define DWARF2_ASM_LINE_DEBUG_INFO 0
5242 #endif
5243 #endif
5244
5245 /* Minimum line offset in a special line info. opcode.
5246    This value was chosen to give a reasonable range of values.  */
5247 #define DWARF_LINE_BASE  -10
5248
5249 /* First special line opcode - leave room for the standard opcodes.  */
5250 #define DWARF_LINE_OPCODE_BASE  10
5251
5252 /* Range of line offsets in a special line info. opcode.  */
5253 #define DWARF_LINE_RANGE  (254-DWARF_LINE_OPCODE_BASE+1)
5254
5255 /* Flag that indicates the initial value of the is_stmt_start flag.
5256    In the present implementation, we do not mark any lines as
5257    the beginning of a source statement, because that information
5258    is not made available by the GCC front-end.  */
5259 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
5260
5261 #ifdef DWARF2_DEBUGGING_INFO
5262 /* This location is used by calc_die_sizes() to keep track
5263    the offset of each DIE within the .debug_info section.  */
5264 static unsigned long next_die_offset;
5265 #endif
5266
5267 /* Record the root of the DIE's built for the current compilation unit.  */
5268 static GTY(()) dw_die_ref comp_unit_die;
5269
5270 /* A list of DIEs with a NULL parent waiting to be relocated.  */
5271 static GTY(()) limbo_die_node *limbo_die_list;
5272
5273 /* A list of DIEs for which we may have to generate
5274    DW_AT_MIPS_linkage_name once their DECL_ASSEMBLER_NAMEs are
5275    set.  */
5276 static GTY(()) limbo_die_node *deferred_asm_name;
5277
5278 /* Filenames referenced by this compilation unit.  */
5279 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
5280
5281 /* A hash table of references to DIE's that describe declarations.
5282    The key is a DECL_UID() which is a unique number identifying each decl.  */
5283 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
5284
5285 /* A hash table of references to DIE's that describe COMMON blocks.
5286    The key is DECL_UID() ^ die_parent.  */
5287 static GTY ((param_is (struct die_struct))) htab_t common_block_die_table;
5288
5289 /* Node of the variable location list.  */
5290 struct GTY ((chain_next ("%h.next"))) var_loc_node {
5291   rtx GTY (()) var_loc_note;
5292   const char * GTY (()) label;
5293   const char * GTY (()) section_label;
5294   struct var_loc_node * GTY (()) next;
5295 };
5296
5297 /* Variable location list.  */
5298 struct GTY (()) var_loc_list_def {
5299   struct var_loc_node * GTY (()) first;
5300
5301   /* Do not mark the last element of the chained list because
5302      it is marked through the chain.  */
5303   struct var_loc_node * GTY ((skip ("%h"))) last;
5304
5305   /* DECL_UID of the variable decl.  */
5306   unsigned int decl_id;
5307 };
5308 typedef struct var_loc_list_def var_loc_list;
5309
5310
5311 /* Table of decl location linked lists.  */
5312 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
5313
5314 /* A pointer to the base of a list of references to DIE's that
5315    are uniquely identified by their tag, presence/absence of
5316    children DIE's, and list of attribute/value pairs.  */
5317 static GTY((length ("abbrev_die_table_allocated")))
5318   dw_die_ref *abbrev_die_table;
5319
5320 /* Number of elements currently allocated for abbrev_die_table.  */
5321 static GTY(()) unsigned abbrev_die_table_allocated;
5322
5323 /* Number of elements in type_die_table currently in use.  */
5324 static GTY(()) unsigned abbrev_die_table_in_use;
5325
5326 /* Size (in elements) of increments by which we may expand the
5327    abbrev_die_table.  */
5328 #define ABBREV_DIE_TABLE_INCREMENT 256
5329
5330 /* A pointer to the base of a table that contains line information
5331    for each source code line in .text in the compilation unit.  */
5332 static GTY((length ("line_info_table_allocated")))
5333      dw_line_info_ref line_info_table;
5334
5335 /* Number of elements currently allocated for line_info_table.  */
5336 static GTY(()) unsigned line_info_table_allocated;
5337
5338 /* Number of elements in line_info_table currently in use.  */
5339 static GTY(()) unsigned line_info_table_in_use;
5340
5341 /* A pointer to the base of a table that contains line information
5342    for each source code line outside of .text in the compilation unit.  */
5343 static GTY ((length ("separate_line_info_table_allocated")))
5344      dw_separate_line_info_ref separate_line_info_table;
5345
5346 /* Number of elements currently allocated for separate_line_info_table.  */
5347 static GTY(()) unsigned separate_line_info_table_allocated;
5348
5349 /* Number of elements in separate_line_info_table currently in use.  */
5350 static GTY(()) unsigned separate_line_info_table_in_use;
5351
5352 /* Size (in elements) of increments by which we may expand the
5353    line_info_table.  */
5354 #define LINE_INFO_TABLE_INCREMENT 1024
5355
5356 /* A pointer to the base of a table that contains a list of publicly
5357    accessible names.  */
5358 static GTY (()) VEC (pubname_entry, gc) *  pubname_table;
5359
5360 /* A pointer to the base of a table that contains a list of publicly
5361    accessible types.  */
5362 static GTY (()) VEC (pubname_entry, gc) * pubtype_table;
5363
5364 /* Array of dies for which we should generate .debug_arange info.  */
5365 static GTY((length ("arange_table_allocated"))) dw_die_ref *arange_table;
5366
5367 /* Number of elements currently allocated for arange_table.  */
5368 static GTY(()) unsigned arange_table_allocated;
5369
5370 /* Number of elements in arange_table currently in use.  */
5371 static GTY(()) unsigned arange_table_in_use;
5372
5373 /* Size (in elements) of increments by which we may expand the
5374    arange_table.  */
5375 #define ARANGE_TABLE_INCREMENT 64
5376
5377 /* Array of dies for which we should generate .debug_ranges info.  */
5378 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
5379
5380 /* Number of elements currently allocated for ranges_table.  */
5381 static GTY(()) unsigned ranges_table_allocated;
5382
5383 /* Number of elements in ranges_table currently in use.  */
5384 static GTY(()) unsigned ranges_table_in_use;
5385
5386 /* Array of pairs of labels referenced in ranges_table.  */
5387 static GTY ((length ("ranges_by_label_allocated")))
5388      dw_ranges_by_label_ref ranges_by_label;
5389
5390 /* Number of elements currently allocated for ranges_by_label.  */
5391 static GTY(()) unsigned ranges_by_label_allocated;
5392
5393 /* Number of elements in ranges_by_label currently in use.  */
5394 static GTY(()) unsigned ranges_by_label_in_use;
5395
5396 /* Size (in elements) of increments by which we may expand the
5397    ranges_table.  */
5398 #define RANGES_TABLE_INCREMENT 64
5399
5400 /* Whether we have location lists that need outputting */
5401 static GTY(()) bool have_location_lists;
5402
5403 /* Unique label counter.  */
5404 static GTY(()) unsigned int loclabel_num;
5405
5406 #ifdef DWARF2_DEBUGGING_INFO
5407 /* Record whether the function being analyzed contains inlined functions.  */
5408 static int current_function_has_inlines;
5409 #endif
5410 #if 0 && defined (MIPS_DEBUGGING_INFO)
5411 static int comp_unit_has_inlines;
5412 #endif
5413
5414 /* The last file entry emitted by maybe_emit_file().  */
5415 static GTY(()) struct dwarf_file_data * last_emitted_file;
5416
5417 /* Number of internal labels generated by gen_internal_sym().  */
5418 static GTY(()) int label_num;
5419
5420 /* Cached result of previous call to lookup_filename.  */
5421 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
5422
5423 #ifdef DWARF2_DEBUGGING_INFO
5424
5425 /* Offset from the "steady-state frame pointer" to the frame base,
5426    within the current function.  */
5427 static HOST_WIDE_INT frame_pointer_fb_offset;
5428
5429 /* Forward declarations for functions defined in this file.  */
5430
5431 static int is_pseudo_reg (const_rtx);
5432 static tree type_main_variant (tree);
5433 static int is_tagged_type (const_tree);
5434 static const char *dwarf_tag_name (unsigned);
5435 static const char *dwarf_attr_name (unsigned);
5436 static const char *dwarf_form_name (unsigned);
5437 static tree decl_ultimate_origin (const_tree);
5438 static tree decl_class_context (tree);
5439 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
5440 static inline enum dw_val_class AT_class (dw_attr_ref);
5441 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
5442 static inline unsigned AT_flag (dw_attr_ref);
5443 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
5444 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
5445 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
5446 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
5447 static void add_AT_long_long (dw_die_ref, enum dwarf_attribute, unsigned long,
5448                               unsigned long);
5449 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
5450                                unsigned int, unsigned char *);
5451 static hashval_t debug_str_do_hash (const void *);
5452 static int debug_str_eq (const void *, const void *);
5453 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
5454 static inline const char *AT_string (dw_attr_ref);
5455 static enum dwarf_form AT_string_form (dw_attr_ref);
5456 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
5457 static void add_AT_specification (dw_die_ref, dw_die_ref);
5458 static inline dw_die_ref AT_ref (dw_attr_ref);
5459 static inline int AT_ref_external (dw_attr_ref);
5460 static inline void set_AT_ref_external (dw_attr_ref, int);
5461 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
5462 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
5463 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
5464 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
5465                              dw_loc_list_ref);
5466 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
5467 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
5468 static inline rtx AT_addr (dw_attr_ref);
5469 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
5470 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
5471 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
5472 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
5473                            unsigned HOST_WIDE_INT);
5474 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
5475                                unsigned long);
5476 static inline const char *AT_lbl (dw_attr_ref);
5477 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
5478 static const char *get_AT_low_pc (dw_die_ref);
5479 static const char *get_AT_hi_pc (dw_die_ref);
5480 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
5481 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
5482 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
5483 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
5484 static bool is_c_family (void);
5485 static bool is_cxx (void);
5486 static bool is_java (void);
5487 static bool is_fortran (void);
5488 static bool is_ada (void);
5489 static void remove_AT (dw_die_ref, enum dwarf_attribute);
5490 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
5491 static void add_child_die (dw_die_ref, dw_die_ref);
5492 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
5493 static dw_die_ref lookup_type_die (tree);
5494 static void equate_type_number_to_die (tree, dw_die_ref);
5495 static hashval_t decl_die_table_hash (const void *);
5496 static int decl_die_table_eq (const void *, const void *);
5497 static dw_die_ref lookup_decl_die (tree);
5498 static hashval_t common_block_die_table_hash (const void *);
5499 static int common_block_die_table_eq (const void *, const void *);
5500 static hashval_t decl_loc_table_hash (const void *);
5501 static int decl_loc_table_eq (const void *, const void *);
5502 static var_loc_list *lookup_decl_loc (const_tree);
5503 static void equate_decl_number_to_die (tree, dw_die_ref);
5504 static void add_var_loc_to_decl (tree, struct var_loc_node *);
5505 static void print_spaces (FILE *);
5506 static void print_die (dw_die_ref, FILE *);
5507 static void print_dwarf_line_table (FILE *);
5508 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
5509 static dw_die_ref pop_compile_unit (dw_die_ref);
5510 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
5511 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
5512 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
5513 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
5514 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
5515 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
5516 static int same_die_p (dw_die_ref, dw_die_ref, int *);
5517 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
5518 static void compute_section_prefix (dw_die_ref);
5519 static int is_type_die (dw_die_ref);
5520 static int is_comdat_die (dw_die_ref);
5521 static int is_symbol_die (dw_die_ref);
5522 static void assign_symbol_names (dw_die_ref);
5523 static void break_out_includes (dw_die_ref);
5524 static hashval_t htab_cu_hash (const void *);
5525 static int htab_cu_eq (const void *, const void *);
5526 static void htab_cu_del (void *);
5527 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
5528 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
5529 static void add_sibling_attributes (dw_die_ref);
5530 static void build_abbrev_table (dw_die_ref);
5531 static void output_location_lists (dw_die_ref);
5532 static int constant_size (unsigned HOST_WIDE_INT);
5533 static unsigned long size_of_die (dw_die_ref);
5534 static void calc_die_sizes (dw_die_ref);
5535 static void mark_dies (dw_die_ref);
5536 static void unmark_dies (dw_die_ref);
5537 static void unmark_all_dies (dw_die_ref);
5538 static unsigned long size_of_pubnames (VEC (pubname_entry,gc) *);
5539 static unsigned long size_of_aranges (void);
5540 static enum dwarf_form value_format (dw_attr_ref);
5541 static void output_value_format (dw_attr_ref);
5542 static void output_abbrev_section (void);
5543 static void output_die_symbol (dw_die_ref);
5544 static void output_die (dw_die_ref);
5545 static void output_compilation_unit_header (void);
5546 static void output_comp_unit (dw_die_ref, int);
5547 static const char *dwarf2_name (tree, int);
5548 static void add_pubname (tree, dw_die_ref);
5549 static void add_pubname_string (const char *, dw_die_ref);
5550 static void add_pubtype (tree, dw_die_ref);
5551 static void output_pubnames (VEC (pubname_entry,gc) *);
5552 static void add_arange (tree, dw_die_ref);
5553 static void output_aranges (void);
5554 static unsigned int add_ranges_num (int);
5555 static unsigned int add_ranges (const_tree);
5556 static unsigned int add_ranges_by_labels (const char *, const char *);
5557 static void output_ranges (void);
5558 static void output_line_info (void);
5559 static void output_file_names (void);
5560 static dw_die_ref base_type_die (tree);
5561 static int is_base_type (tree);
5562 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
5563 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
5564 static int type_is_enum (const_tree);
5565 static unsigned int dbx_reg_number (const_rtx);
5566 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
5567 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
5568 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
5569                                                 enum var_init_status);
5570 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
5571                                                      enum var_init_status);
5572 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
5573                                          enum var_init_status);
5574 static int is_based_loc (const_rtx);
5575 static dw_loc_descr_ref mem_loc_descriptor (rtx, enum machine_mode mode,
5576                                             enum var_init_status);
5577 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
5578                                                enum var_init_status);
5579 static dw_loc_descr_ref loc_descriptor (rtx, enum var_init_status);
5580 static dw_loc_descr_ref loc_descriptor_from_tree_1 (tree, int);
5581 static dw_loc_descr_ref loc_descriptor_from_tree (tree);
5582 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
5583 static tree field_type (const_tree);
5584 static unsigned int simple_type_align_in_bits (const_tree);
5585 static unsigned int simple_decl_align_in_bits (const_tree);
5586 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
5587 static HOST_WIDE_INT field_byte_offset (const_tree);
5588 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
5589                                          dw_loc_descr_ref);
5590 static void add_data_member_location_attribute (dw_die_ref, tree);
5591 static void add_const_value_attribute (dw_die_ref, rtx);
5592 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
5593 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
5594 static void insert_float (const_rtx, unsigned char *);
5595 static rtx rtl_for_decl_location (tree);
5596 static void add_location_or_const_value_attribute (dw_die_ref, tree,
5597                                                    enum dwarf_attribute);
5598 static void tree_add_const_value_attribute (dw_die_ref, tree);
5599 static void add_name_attribute (dw_die_ref, const char *);
5600 static void add_comp_dir_attribute (dw_die_ref);
5601 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
5602 static void add_subscript_info (dw_die_ref, tree, bool);
5603 static void add_byte_size_attribute (dw_die_ref, tree);
5604 static void add_bit_offset_attribute (dw_die_ref, tree);
5605 static void add_bit_size_attribute (dw_die_ref, tree);
5606 static void add_prototyped_attribute (dw_die_ref, tree);
5607 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
5608 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
5609 static void add_src_coords_attributes (dw_die_ref, tree);
5610 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
5611 static void push_decl_scope (tree);
5612 static void pop_decl_scope (void);
5613 static dw_die_ref scope_die_for (tree, dw_die_ref);
5614 static inline int local_scope_p (dw_die_ref);
5615 static inline int class_scope_p (dw_die_ref);
5616 static inline int class_or_namespace_scope_p (dw_die_ref);
5617 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
5618 static void add_calling_convention_attribute (dw_die_ref, tree);
5619 static const char *type_tag (const_tree);
5620 static tree member_declared_type (const_tree);
5621 #if 0
5622 static const char *decl_start_label (tree);
5623 #endif
5624 static void gen_array_type_die (tree, dw_die_ref);
5625 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
5626 #if 0
5627 static void gen_entry_point_die (tree, dw_die_ref);
5628 #endif
5629 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
5630 static dw_die_ref gen_formal_parameter_die (tree, tree, dw_die_ref);
5631 static void gen_unspecified_parameters_die (tree, dw_die_ref);
5632 static void gen_formal_types_die (tree, dw_die_ref);
5633 static void gen_subprogram_die (tree, dw_die_ref);
5634 static void gen_variable_die (tree, tree, dw_die_ref);
5635 static void gen_const_die (tree, dw_die_ref);
5636 static void gen_label_die (tree, dw_die_ref);
5637 static void gen_lexical_block_die (tree, dw_die_ref, int);
5638 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
5639 static void gen_field_die (tree, dw_die_ref);
5640 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
5641 static dw_die_ref gen_compile_unit_die (const char *);
5642 static void gen_inheritance_die (tree, tree, dw_die_ref);
5643 static void gen_member_die (tree, dw_die_ref);
5644 static void gen_struct_or_union_type_die (tree, dw_die_ref,
5645                                                 enum debug_info_usage);
5646 static void gen_subroutine_type_die (tree, dw_die_ref);
5647 static void gen_typedef_die (tree, dw_die_ref);
5648 static void gen_type_die (tree, dw_die_ref);
5649 static void gen_block_die (tree, dw_die_ref, int);
5650 static void decls_for_scope (tree, dw_die_ref, int);
5651 static int is_redundant_typedef (const_tree);
5652 static void gen_namespace_die (tree, dw_die_ref);
5653 static void gen_decl_die (tree, tree, dw_die_ref);
5654 static dw_die_ref force_decl_die (tree);
5655 static dw_die_ref force_type_die (tree);
5656 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
5657 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
5658 static struct dwarf_file_data * lookup_filename (const char *);
5659 static void retry_incomplete_types (void);
5660 static void gen_type_die_for_member (tree, tree, dw_die_ref);
5661 static void splice_child_die (dw_die_ref, dw_die_ref);
5662 static int file_info_cmp (const void *, const void *);
5663 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
5664                                      const char *, const char *, unsigned);
5665 static void add_loc_descr_to_loc_list (dw_loc_list_ref *, dw_loc_descr_ref,
5666                                        const char *, const char *,
5667                                        const char *);
5668 static void output_loc_list (dw_loc_list_ref);
5669 static char *gen_internal_sym (const char *);
5670
5671 static void prune_unmark_dies (dw_die_ref);
5672 static void prune_unused_types_mark (dw_die_ref, int);
5673 static void prune_unused_types_walk (dw_die_ref);
5674 static void prune_unused_types_walk_attribs (dw_die_ref);
5675 static void prune_unused_types_prune (dw_die_ref);
5676 static void prune_unused_types (void);
5677 static int maybe_emit_file (struct dwarf_file_data *fd);
5678
5679 /* Section names used to hold DWARF debugging information.  */
5680 #ifndef DEBUG_INFO_SECTION
5681 #define DEBUG_INFO_SECTION      ".debug_info"
5682 #endif
5683 #ifndef DEBUG_ABBREV_SECTION
5684 #define DEBUG_ABBREV_SECTION    ".debug_abbrev"
5685 #endif
5686 #ifndef DEBUG_ARANGES_SECTION
5687 #define DEBUG_ARANGES_SECTION   ".debug_aranges"
5688 #endif
5689 #ifndef DEBUG_MACINFO_SECTION
5690 #define DEBUG_MACINFO_SECTION   ".debug_macinfo"
5691 #endif
5692 #ifndef DEBUG_LINE_SECTION
5693 #define DEBUG_LINE_SECTION      ".debug_line"
5694 #endif
5695 #ifndef DEBUG_LOC_SECTION
5696 #define DEBUG_LOC_SECTION       ".debug_loc"
5697 #endif
5698 #ifndef DEBUG_PUBNAMES_SECTION
5699 #define DEBUG_PUBNAMES_SECTION  ".debug_pubnames"
5700 #endif
5701 #ifndef DEBUG_PUBTYPES_SECTION
5702 #define DEBUG_PUBTYPES_SECTION  ".debug_pubtypes"
5703 #endif
5704 #ifndef DEBUG_STR_SECTION
5705 #define DEBUG_STR_SECTION       ".debug_str"
5706 #endif
5707 #ifndef DEBUG_RANGES_SECTION
5708 #define DEBUG_RANGES_SECTION    ".debug_ranges"
5709 #endif
5710
5711 /* Standard ELF section names for compiled code and data.  */
5712 #ifndef TEXT_SECTION_NAME
5713 #define TEXT_SECTION_NAME       ".text"
5714 #endif
5715
5716 /* Section flags for .debug_str section.  */
5717 #define DEBUG_STR_SECTION_FLAGS \
5718   (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings               \
5719    ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1        \
5720    : SECTION_DEBUG)
5721
5722 /* Labels we insert at beginning sections we can reference instead of
5723    the section names themselves.  */
5724
5725 #ifndef TEXT_SECTION_LABEL
5726 #define TEXT_SECTION_LABEL              "Ltext"
5727 #endif
5728 #ifndef COLD_TEXT_SECTION_LABEL
5729 #define COLD_TEXT_SECTION_LABEL         "Ltext_cold"
5730 #endif
5731 #ifndef DEBUG_LINE_SECTION_LABEL
5732 #define DEBUG_LINE_SECTION_LABEL        "Ldebug_line"
5733 #endif
5734 #ifndef DEBUG_INFO_SECTION_LABEL
5735 #define DEBUG_INFO_SECTION_LABEL        "Ldebug_info"
5736 #endif
5737 #ifndef DEBUG_ABBREV_SECTION_LABEL
5738 #define DEBUG_ABBREV_SECTION_LABEL      "Ldebug_abbrev"
5739 #endif
5740 #ifndef DEBUG_LOC_SECTION_LABEL
5741 #define DEBUG_LOC_SECTION_LABEL         "Ldebug_loc"
5742 #endif
5743 #ifndef DEBUG_RANGES_SECTION_LABEL
5744 #define DEBUG_RANGES_SECTION_LABEL      "Ldebug_ranges"
5745 #endif
5746 #ifndef DEBUG_MACINFO_SECTION_LABEL
5747 #define DEBUG_MACINFO_SECTION_LABEL     "Ldebug_macinfo"
5748 #endif
5749
5750 /* Definitions of defaults for formats and names of various special
5751    (artificial) labels which may be generated within this file (when the -g
5752    options is used and DWARF2_DEBUGGING_INFO is in effect.
5753    If necessary, these may be overridden from within the tm.h file, but
5754    typically, overriding these defaults is unnecessary.  */
5755
5756 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
5757 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5758 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5759 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
5760 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5761 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5762 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5763 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5764 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5765 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
5766
5767 #ifndef TEXT_END_LABEL
5768 #define TEXT_END_LABEL          "Letext"
5769 #endif
5770 #ifndef COLD_END_LABEL
5771 #define COLD_END_LABEL          "Letext_cold"
5772 #endif
5773 #ifndef BLOCK_BEGIN_LABEL
5774 #define BLOCK_BEGIN_LABEL       "LBB"
5775 #endif
5776 #ifndef BLOCK_END_LABEL
5777 #define BLOCK_END_LABEL         "LBE"
5778 #endif
5779 #ifndef LINE_CODE_LABEL
5780 #define LINE_CODE_LABEL         "LM"
5781 #endif
5782 #ifndef SEPARATE_LINE_CODE_LABEL
5783 #define SEPARATE_LINE_CODE_LABEL        "LSM"
5784 #endif
5785
5786 \f
5787 /* We allow a language front-end to designate a function that is to be
5788    called to "demangle" any name before it is put into a DIE.  */
5789
5790 static const char *(*demangle_name_func) (const char *);
5791
5792 void
5793 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
5794 {
5795   demangle_name_func = func;
5796 }
5797
5798 /* Test if rtl node points to a pseudo register.  */
5799
5800 static inline int
5801 is_pseudo_reg (const_rtx rtl)
5802 {
5803   return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
5804           || (GET_CODE (rtl) == SUBREG
5805               && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
5806 }
5807
5808 /* Return a reference to a type, with its const and volatile qualifiers
5809    removed.  */
5810
5811 static inline tree
5812 type_main_variant (tree type)
5813 {
5814   type = TYPE_MAIN_VARIANT (type);
5815
5816   /* ??? There really should be only one main variant among any group of
5817      variants of a given type (and all of the MAIN_VARIANT values for all
5818      members of the group should point to that one type) but sometimes the C
5819      front-end messes this up for array types, so we work around that bug
5820      here.  */
5821   if (TREE_CODE (type) == ARRAY_TYPE)
5822     while (type != TYPE_MAIN_VARIANT (type))
5823       type = TYPE_MAIN_VARIANT (type);
5824
5825   return type;
5826 }
5827
5828 /* Return nonzero if the given type node represents a tagged type.  */
5829
5830 static inline int
5831 is_tagged_type (const_tree type)
5832 {
5833   enum tree_code code = TREE_CODE (type);
5834
5835   return (code == RECORD_TYPE || code == UNION_TYPE
5836           || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
5837 }
5838
5839 /* Convert a DIE tag into its string name.  */
5840
5841 static const char *
5842 dwarf_tag_name (unsigned int tag)
5843 {
5844   switch (tag)
5845     {
5846     case DW_TAG_padding:
5847       return "DW_TAG_padding";
5848     case DW_TAG_array_type:
5849       return "DW_TAG_array_type";
5850     case DW_TAG_class_type:
5851       return "DW_TAG_class_type";
5852     case DW_TAG_entry_point:
5853       return "DW_TAG_entry_point";
5854     case DW_TAG_enumeration_type:
5855       return "DW_TAG_enumeration_type";
5856     case DW_TAG_formal_parameter:
5857       return "DW_TAG_formal_parameter";
5858     case DW_TAG_imported_declaration:
5859       return "DW_TAG_imported_declaration";
5860     case DW_TAG_label:
5861       return "DW_TAG_label";
5862     case DW_TAG_lexical_block:
5863       return "DW_TAG_lexical_block";
5864     case DW_TAG_member:
5865       return "DW_TAG_member";
5866     case DW_TAG_pointer_type:
5867       return "DW_TAG_pointer_type";
5868     case DW_TAG_reference_type:
5869       return "DW_TAG_reference_type";
5870     case DW_TAG_compile_unit:
5871       return "DW_TAG_compile_unit";
5872     case DW_TAG_string_type:
5873       return "DW_TAG_string_type";
5874     case DW_TAG_structure_type:
5875       return "DW_TAG_structure_type";
5876     case DW_TAG_subroutine_type:
5877       return "DW_TAG_subroutine_type";
5878     case DW_TAG_typedef:
5879       return "DW_TAG_typedef";
5880     case DW_TAG_union_type:
5881       return "DW_TAG_union_type";
5882     case DW_TAG_unspecified_parameters:
5883       return "DW_TAG_unspecified_parameters";
5884     case DW_TAG_variant:
5885       return "DW_TAG_variant";
5886     case DW_TAG_common_block:
5887       return "DW_TAG_common_block";
5888     case DW_TAG_common_inclusion:
5889       return "DW_TAG_common_inclusion";
5890     case DW_TAG_inheritance:
5891       return "DW_TAG_inheritance";
5892     case DW_TAG_inlined_subroutine:
5893       return "DW_TAG_inlined_subroutine";
5894     case DW_TAG_module:
5895       return "DW_TAG_module";
5896     case DW_TAG_ptr_to_member_type:
5897       return "DW_TAG_ptr_to_member_type";
5898     case DW_TAG_set_type:
5899       return "DW_TAG_set_type";
5900     case DW_TAG_subrange_type:
5901       return "DW_TAG_subrange_type";
5902     case DW_TAG_with_stmt:
5903       return "DW_TAG_with_stmt";
5904     case DW_TAG_access_declaration:
5905       return "DW_TAG_access_declaration";
5906     case DW_TAG_base_type:
5907       return "DW_TAG_base_type";
5908     case DW_TAG_catch_block:
5909       return "DW_TAG_catch_block";
5910     case DW_TAG_const_type:
5911       return "DW_TAG_const_type";
5912     case DW_TAG_constant:
5913       return "DW_TAG_constant";
5914     case DW_TAG_enumerator:
5915       return "DW_TAG_enumerator";
5916     case DW_TAG_file_type:
5917       return "DW_TAG_file_type";
5918     case DW_TAG_friend:
5919       return "DW_TAG_friend";
5920     case DW_TAG_namelist:
5921       return "DW_TAG_namelist";
5922     case DW_TAG_namelist_item:
5923       return "DW_TAG_namelist_item";
5924     case DW_TAG_packed_type:
5925       return "DW_TAG_packed_type";
5926     case DW_TAG_subprogram:
5927       return "DW_TAG_subprogram";
5928     case DW_TAG_template_type_param:
5929       return "DW_TAG_template_type_param";
5930     case DW_TAG_template_value_param:
5931       return "DW_TAG_template_value_param";
5932     case DW_TAG_thrown_type:
5933       return "DW_TAG_thrown_type";
5934     case DW_TAG_try_block:
5935       return "DW_TAG_try_block";
5936     case DW_TAG_variant_part:
5937       return "DW_TAG_variant_part";
5938     case DW_TAG_variable:
5939       return "DW_TAG_variable";
5940     case DW_TAG_volatile_type:
5941       return "DW_TAG_volatile_type";
5942     case DW_TAG_dwarf_procedure:
5943       return "DW_TAG_dwarf_procedure";
5944     case DW_TAG_restrict_type:
5945       return "DW_TAG_restrict_type";
5946     case DW_TAG_interface_type:
5947       return "DW_TAG_interface_type";
5948     case DW_TAG_namespace:
5949       return "DW_TAG_namespace";
5950     case DW_TAG_imported_module:
5951       return "DW_TAG_imported_module";
5952     case DW_TAG_unspecified_type:
5953       return "DW_TAG_unspecified_type";
5954     case DW_TAG_partial_unit:
5955       return "DW_TAG_partial_unit";
5956     case DW_TAG_imported_unit:
5957       return "DW_TAG_imported_unit";
5958     case DW_TAG_condition:
5959       return "DW_TAG_condition";
5960     case DW_TAG_shared_type:
5961       return "DW_TAG_shared_type";
5962     case DW_TAG_MIPS_loop:
5963       return "DW_TAG_MIPS_loop";
5964     case DW_TAG_format_label:
5965       return "DW_TAG_format_label";
5966     case DW_TAG_function_template:
5967       return "DW_TAG_function_template";
5968     case DW_TAG_class_template:
5969       return "DW_TAG_class_template";
5970     case DW_TAG_GNU_BINCL:
5971       return "DW_TAG_GNU_BINCL";
5972     case DW_TAG_GNU_EINCL:
5973       return "DW_TAG_GNU_EINCL";
5974     default:
5975       return "DW_TAG_<unknown>";
5976     }
5977 }
5978
5979 /* Convert a DWARF attribute code into its string name.  */
5980
5981 static const char *
5982 dwarf_attr_name (unsigned int attr)
5983 {
5984   switch (attr)
5985     {
5986     case DW_AT_sibling:
5987       return "DW_AT_sibling";
5988     case DW_AT_location:
5989       return "DW_AT_location";
5990     case DW_AT_name:
5991       return "DW_AT_name";
5992     case DW_AT_ordering:
5993       return "DW_AT_ordering";
5994     case DW_AT_subscr_data:
5995       return "DW_AT_subscr_data";
5996     case DW_AT_byte_size:
5997       return "DW_AT_byte_size";
5998     case DW_AT_bit_offset:
5999       return "DW_AT_bit_offset";
6000     case DW_AT_bit_size:
6001       return "DW_AT_bit_size";
6002     case DW_AT_element_list:
6003       return "DW_AT_element_list";
6004     case DW_AT_stmt_list:
6005       return "DW_AT_stmt_list";
6006     case DW_AT_low_pc:
6007       return "DW_AT_low_pc";
6008     case DW_AT_high_pc:
6009       return "DW_AT_high_pc";
6010     case DW_AT_language:
6011       return "DW_AT_language";
6012     case DW_AT_member:
6013       return "DW_AT_member";
6014     case DW_AT_discr:
6015       return "DW_AT_discr";
6016     case DW_AT_discr_value:
6017       return "DW_AT_discr_value";
6018     case DW_AT_visibility:
6019       return "DW_AT_visibility";
6020     case DW_AT_import:
6021       return "DW_AT_import";
6022     case DW_AT_string_length:
6023       return "DW_AT_string_length";
6024     case DW_AT_common_reference:
6025       return "DW_AT_common_reference";
6026     case DW_AT_comp_dir:
6027       return "DW_AT_comp_dir";
6028     case DW_AT_const_value:
6029       return "DW_AT_const_value";
6030     case DW_AT_containing_type:
6031       return "DW_AT_containing_type";
6032     case DW_AT_default_value:
6033       return "DW_AT_default_value";
6034     case DW_AT_inline:
6035       return "DW_AT_inline";
6036     case DW_AT_is_optional:
6037       return "DW_AT_is_optional";
6038     case DW_AT_lower_bound:
6039       return "DW_AT_lower_bound";
6040     case DW_AT_producer:
6041       return "DW_AT_producer";
6042     case DW_AT_prototyped:
6043       return "DW_AT_prototyped";
6044     case DW_AT_return_addr:
6045       return "DW_AT_return_addr";
6046     case DW_AT_start_scope:
6047       return "DW_AT_start_scope";
6048     case DW_AT_bit_stride:
6049       return "DW_AT_bit_stride";
6050     case DW_AT_upper_bound:
6051       return "DW_AT_upper_bound";
6052     case DW_AT_abstract_origin:
6053       return "DW_AT_abstract_origin";
6054     case DW_AT_accessibility:
6055       return "DW_AT_accessibility";
6056     case DW_AT_address_class:
6057       return "DW_AT_address_class";
6058     case DW_AT_artificial:
6059       return "DW_AT_artificial";
6060     case DW_AT_base_types:
6061       return "DW_AT_base_types";
6062     case DW_AT_calling_convention:
6063       return "DW_AT_calling_convention";
6064     case DW_AT_count:
6065       return "DW_AT_count";
6066     case DW_AT_data_member_location:
6067       return "DW_AT_data_member_location";
6068     case DW_AT_decl_column:
6069       return "DW_AT_decl_column";
6070     case DW_AT_decl_file:
6071       return "DW_AT_decl_file";
6072     case DW_AT_decl_line:
6073       return "DW_AT_decl_line";
6074     case DW_AT_declaration:
6075       return "DW_AT_declaration";
6076     case DW_AT_discr_list:
6077       return "DW_AT_discr_list";
6078     case DW_AT_encoding:
6079       return "DW_AT_encoding";
6080     case DW_AT_external:
6081       return "DW_AT_external";
6082     case DW_AT_explicit:
6083       return "DW_AT_explicit";
6084     case DW_AT_frame_base:
6085       return "DW_AT_frame_base";
6086     case DW_AT_friend:
6087       return "DW_AT_friend";
6088     case DW_AT_identifier_case:
6089       return "DW_AT_identifier_case";
6090     case DW_AT_macro_info:
6091       return "DW_AT_macro_info";
6092     case DW_AT_namelist_items:
6093       return "DW_AT_namelist_items";
6094     case DW_AT_priority:
6095       return "DW_AT_priority";
6096     case DW_AT_segment:
6097       return "DW_AT_segment";
6098     case DW_AT_specification:
6099       return "DW_AT_specification";
6100     case DW_AT_static_link:
6101       return "DW_AT_static_link";
6102     case DW_AT_type:
6103       return "DW_AT_type";
6104     case DW_AT_use_location:
6105       return "DW_AT_use_location";
6106     case DW_AT_variable_parameter:
6107       return "DW_AT_variable_parameter";
6108     case DW_AT_virtuality:
6109       return "DW_AT_virtuality";
6110     case DW_AT_vtable_elem_location:
6111       return "DW_AT_vtable_elem_location";
6112
6113     case DW_AT_allocated:
6114       return "DW_AT_allocated";
6115     case DW_AT_associated:
6116       return "DW_AT_associated";
6117     case DW_AT_data_location:
6118       return "DW_AT_data_location";
6119     case DW_AT_byte_stride:
6120       return "DW_AT_byte_stride";
6121     case DW_AT_entry_pc:
6122       return "DW_AT_entry_pc";
6123     case DW_AT_use_UTF8:
6124       return "DW_AT_use_UTF8";
6125     case DW_AT_extension:
6126       return "DW_AT_extension";
6127     case DW_AT_ranges:
6128       return "DW_AT_ranges";
6129     case DW_AT_trampoline:
6130       return "DW_AT_trampoline";
6131     case DW_AT_call_column:
6132       return "DW_AT_call_column";
6133     case DW_AT_call_file:
6134       return "DW_AT_call_file";
6135     case DW_AT_call_line:
6136       return "DW_AT_call_line";
6137
6138     case DW_AT_MIPS_fde:
6139       return "DW_AT_MIPS_fde";
6140     case DW_AT_MIPS_loop_begin:
6141       return "DW_AT_MIPS_loop_begin";
6142     case DW_AT_MIPS_tail_loop_begin:
6143       return "DW_AT_MIPS_tail_loop_begin";
6144     case DW_AT_MIPS_epilog_begin:
6145       return "DW_AT_MIPS_epilog_begin";
6146     case DW_AT_MIPS_loop_unroll_factor:
6147       return "DW_AT_MIPS_loop_unroll_factor";
6148     case DW_AT_MIPS_software_pipeline_depth:
6149       return "DW_AT_MIPS_software_pipeline_depth";
6150     case DW_AT_MIPS_linkage_name:
6151       return "DW_AT_MIPS_linkage_name";
6152     case DW_AT_MIPS_stride:
6153       return "DW_AT_MIPS_stride";
6154     case DW_AT_MIPS_abstract_name:
6155       return "DW_AT_MIPS_abstract_name";
6156     case DW_AT_MIPS_clone_origin:
6157       return "DW_AT_MIPS_clone_origin";
6158     case DW_AT_MIPS_has_inlines:
6159       return "DW_AT_MIPS_has_inlines";
6160
6161     case DW_AT_sf_names:
6162       return "DW_AT_sf_names";
6163     case DW_AT_src_info:
6164       return "DW_AT_src_info";
6165     case DW_AT_mac_info:
6166       return "DW_AT_mac_info";
6167     case DW_AT_src_coords:
6168       return "DW_AT_src_coords";
6169     case DW_AT_body_begin:
6170       return "DW_AT_body_begin";
6171     case DW_AT_body_end:
6172       return "DW_AT_body_end";
6173     case DW_AT_GNU_vector:
6174       return "DW_AT_GNU_vector";
6175
6176     case DW_AT_VMS_rtnbeg_pd_address:
6177       return "DW_AT_VMS_rtnbeg_pd_address";
6178
6179     default:
6180       return "DW_AT_<unknown>";
6181     }
6182 }
6183
6184 /* Convert a DWARF value form code into its string name.  */
6185
6186 static const char *
6187 dwarf_form_name (unsigned int form)
6188 {
6189   switch (form)
6190     {
6191     case DW_FORM_addr:
6192       return "DW_FORM_addr";
6193     case DW_FORM_block2:
6194       return "DW_FORM_block2";
6195     case DW_FORM_block4:
6196       return "DW_FORM_block4";
6197     case DW_FORM_data2:
6198       return "DW_FORM_data2";
6199     case DW_FORM_data4:
6200       return "DW_FORM_data4";
6201     case DW_FORM_data8:
6202       return "DW_FORM_data8";
6203     case DW_FORM_string:
6204       return "DW_FORM_string";
6205     case DW_FORM_block:
6206       return "DW_FORM_block";
6207     case DW_FORM_block1:
6208       return "DW_FORM_block1";
6209     case DW_FORM_data1:
6210       return "DW_FORM_data1";
6211     case DW_FORM_flag:
6212       return "DW_FORM_flag";
6213     case DW_FORM_sdata:
6214       return "DW_FORM_sdata";
6215     case DW_FORM_strp:
6216       return "DW_FORM_strp";
6217     case DW_FORM_udata:
6218       return "DW_FORM_udata";
6219     case DW_FORM_ref_addr:
6220       return "DW_FORM_ref_addr";
6221     case DW_FORM_ref1:
6222       return "DW_FORM_ref1";
6223     case DW_FORM_ref2:
6224       return "DW_FORM_ref2";
6225     case DW_FORM_ref4:
6226       return "DW_FORM_ref4";
6227     case DW_FORM_ref8:
6228       return "DW_FORM_ref8";
6229     case DW_FORM_ref_udata:
6230       return "DW_FORM_ref_udata";
6231     case DW_FORM_indirect:
6232       return "DW_FORM_indirect";
6233     default:
6234       return "DW_FORM_<unknown>";
6235     }
6236 }
6237 \f
6238 /* Determine the "ultimate origin" of a decl.  The decl may be an inlined
6239    instance of an inlined instance of a decl which is local to an inline
6240    function, so we have to trace all of the way back through the origin chain
6241    to find out what sort of node actually served as the original seed for the
6242    given block.  */
6243
6244 static tree
6245 decl_ultimate_origin (const_tree decl)
6246 {
6247   if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
6248     return NULL_TREE;
6249
6250   /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
6251      nodes in the function to point to themselves; ignore that if
6252      we're trying to output the abstract instance of this function.  */
6253   if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
6254     return NULL_TREE;
6255
6256   /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
6257      most distant ancestor, this should never happen.  */
6258   gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
6259
6260   return DECL_ABSTRACT_ORIGIN (decl);
6261 }
6262
6263 /* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
6264    of a virtual function may refer to a base class, so we check the 'this'
6265    parameter.  */
6266
6267 static tree
6268 decl_class_context (tree decl)
6269 {
6270   tree context = NULL_TREE;
6271
6272   if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
6273     context = DECL_CONTEXT (decl);
6274   else
6275     context = TYPE_MAIN_VARIANT
6276       (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
6277
6278   if (context && !TYPE_P (context))
6279     context = NULL_TREE;
6280
6281   return context;
6282 }
6283 \f
6284 /* Add an attribute/value pair to a DIE.  */
6285
6286 static inline void
6287 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
6288 {
6289   /* Maybe this should be an assert?  */
6290   if (die == NULL)
6291     return;
6292
6293   if (die->die_attr == NULL)
6294     die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
6295   VEC_safe_push (dw_attr_node, gc, die->die_attr, attr);
6296 }
6297
6298 static inline enum dw_val_class
6299 AT_class (dw_attr_ref a)
6300 {
6301   return a->dw_attr_val.val_class;
6302 }
6303
6304 /* Add a flag value attribute to a DIE.  */
6305
6306 static inline void
6307 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
6308 {
6309   dw_attr_node attr;
6310
6311   attr.dw_attr = attr_kind;
6312   attr.dw_attr_val.val_class = dw_val_class_flag;
6313   attr.dw_attr_val.v.val_flag = flag;
6314   add_dwarf_attr (die, &attr);
6315 }
6316
6317 static inline unsigned
6318 AT_flag (dw_attr_ref a)
6319 {
6320   gcc_assert (a && AT_class (a) == dw_val_class_flag);
6321   return a->dw_attr_val.v.val_flag;
6322 }
6323
6324 /* Add a signed integer attribute value to a DIE.  */
6325
6326 static inline void
6327 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
6328 {
6329   dw_attr_node attr;
6330
6331   attr.dw_attr = attr_kind;
6332   attr.dw_attr_val.val_class = dw_val_class_const;
6333   attr.dw_attr_val.v.val_int = int_val;
6334   add_dwarf_attr (die, &attr);
6335 }
6336
6337 static inline HOST_WIDE_INT
6338 AT_int (dw_attr_ref a)
6339 {
6340   gcc_assert (a && AT_class (a) == dw_val_class_const);
6341   return a->dw_attr_val.v.val_int;
6342 }
6343
6344 /* Add an unsigned integer attribute value to a DIE.  */
6345
6346 static inline void
6347 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
6348                  unsigned HOST_WIDE_INT unsigned_val)
6349 {
6350   dw_attr_node attr;
6351
6352   attr.dw_attr = attr_kind;
6353   attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
6354   attr.dw_attr_val.v.val_unsigned = unsigned_val;
6355   add_dwarf_attr (die, &attr);
6356 }
6357
6358 static inline unsigned HOST_WIDE_INT
6359 AT_unsigned (dw_attr_ref a)
6360 {
6361   gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
6362   return a->dw_attr_val.v.val_unsigned;
6363 }
6364
6365 /* Add an unsigned double integer attribute value to a DIE.  */
6366
6367 static inline void
6368 add_AT_long_long (dw_die_ref die, enum dwarf_attribute attr_kind,
6369                   long unsigned int val_hi, long unsigned int val_low)
6370 {
6371   dw_attr_node attr;
6372
6373   attr.dw_attr = attr_kind;
6374   attr.dw_attr_val.val_class = dw_val_class_long_long;
6375   attr.dw_attr_val.v.val_long_long.hi = val_hi;
6376   attr.dw_attr_val.v.val_long_long.low = val_low;
6377   add_dwarf_attr (die, &attr);
6378 }
6379
6380 /* Add a floating point attribute value to a DIE and return it.  */
6381
6382 static inline void
6383 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
6384             unsigned int length, unsigned int elt_size, unsigned char *array)
6385 {
6386   dw_attr_node attr;
6387
6388   attr.dw_attr = attr_kind;
6389   attr.dw_attr_val.val_class = dw_val_class_vec;
6390   attr.dw_attr_val.v.val_vec.length = length;
6391   attr.dw_attr_val.v.val_vec.elt_size = elt_size;
6392   attr.dw_attr_val.v.val_vec.array = array;
6393   add_dwarf_attr (die, &attr);
6394 }
6395
6396 /* Hash and equality functions for debug_str_hash.  */
6397
6398 static hashval_t
6399 debug_str_do_hash (const void *x)
6400 {
6401   return htab_hash_string (((const struct indirect_string_node *)x)->str);
6402 }
6403
6404 static int
6405 debug_str_eq (const void *x1, const void *x2)
6406 {
6407   return strcmp ((((const struct indirect_string_node *)x1)->str),
6408                  (const char *)x2) == 0;
6409 }
6410
6411 static struct indirect_string_node *
6412 find_AT_string (const char *str)
6413 {
6414   struct indirect_string_node *node;
6415   void **slot;
6416
6417   if (! debug_str_hash)
6418     debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
6419                                       debug_str_eq, NULL);
6420
6421   slot = htab_find_slot_with_hash (debug_str_hash, str,
6422                                    htab_hash_string (str), INSERT);
6423   if (*slot == NULL)
6424     {
6425       node = (struct indirect_string_node *)
6426                ggc_alloc_cleared (sizeof (struct indirect_string_node));
6427       node->str = ggc_strdup (str);
6428       *slot = node;
6429     }
6430   else
6431     node = (struct indirect_string_node *) *slot;
6432
6433   node->refcount++;
6434   return node;
6435 }
6436
6437 /* Add a string attribute value to a DIE.  */
6438
6439 static inline void
6440 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
6441 {
6442   dw_attr_node attr;
6443   struct indirect_string_node *node;
6444
6445   node = find_AT_string (str);
6446
6447   attr.dw_attr = attr_kind;
6448   attr.dw_attr_val.val_class = dw_val_class_str;
6449   attr.dw_attr_val.v.val_str = node;
6450   add_dwarf_attr (die, &attr);
6451 }
6452
6453 static inline const char *
6454 AT_string (dw_attr_ref a)
6455 {
6456   gcc_assert (a && AT_class (a) == dw_val_class_str);
6457   return a->dw_attr_val.v.val_str->str;
6458 }
6459
6460 /* Find out whether a string should be output inline in DIE
6461    or out-of-line in .debug_str section.  */
6462
6463 static enum dwarf_form
6464 AT_string_form (dw_attr_ref a)
6465 {
6466   struct indirect_string_node *node;
6467   unsigned int len;
6468   char label[32];
6469
6470   gcc_assert (a && AT_class (a) == dw_val_class_str);
6471
6472   node = a->dw_attr_val.v.val_str;
6473   if (node->form)
6474     return node->form;
6475
6476   len = strlen (node->str) + 1;
6477
6478   /* If the string is shorter or equal to the size of the reference, it is
6479      always better to put it inline.  */
6480   if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
6481     return node->form = DW_FORM_string;
6482
6483   /* If we cannot expect the linker to merge strings in .debug_str
6484      section, only put it into .debug_str if it is worth even in this
6485      single module.  */
6486   if ((debug_str_section->common.flags & SECTION_MERGE) == 0
6487       && (len - DWARF_OFFSET_SIZE) * node->refcount <= len)
6488     return node->form = DW_FORM_string;
6489
6490   ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
6491   ++dw2_string_counter;
6492   node->label = xstrdup (label);
6493
6494   return node->form = DW_FORM_strp;
6495 }
6496
6497 /* Add a DIE reference attribute value to a DIE.  */
6498
6499 static inline void
6500 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
6501 {
6502   dw_attr_node attr;
6503
6504   attr.dw_attr = attr_kind;
6505   attr.dw_attr_val.val_class = dw_val_class_die_ref;
6506   attr.dw_attr_val.v.val_die_ref.die = targ_die;
6507   attr.dw_attr_val.v.val_die_ref.external = 0;
6508   add_dwarf_attr (die, &attr);
6509 }
6510
6511 /* Add an AT_specification attribute to a DIE, and also make the back
6512    pointer from the specification to the definition.  */
6513
6514 static inline void
6515 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
6516 {
6517   add_AT_die_ref (die, DW_AT_specification, targ_die);
6518   gcc_assert (!targ_die->die_definition);
6519   targ_die->die_definition = die;
6520 }
6521
6522 static inline dw_die_ref
6523 AT_ref (dw_attr_ref a)
6524 {
6525   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
6526   return a->dw_attr_val.v.val_die_ref.die;
6527 }
6528
6529 static inline int
6530 AT_ref_external (dw_attr_ref a)
6531 {
6532   if (a && AT_class (a) == dw_val_class_die_ref)
6533     return a->dw_attr_val.v.val_die_ref.external;
6534
6535   return 0;
6536 }
6537
6538 static inline void
6539 set_AT_ref_external (dw_attr_ref a, int i)
6540 {
6541   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
6542   a->dw_attr_val.v.val_die_ref.external = i;
6543 }
6544
6545 /* Add an FDE reference attribute value to a DIE.  */
6546
6547 static inline void
6548 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
6549 {
6550   dw_attr_node attr;
6551
6552   attr.dw_attr = attr_kind;
6553   attr.dw_attr_val.val_class = dw_val_class_fde_ref;
6554   attr.dw_attr_val.v.val_fde_index = targ_fde;
6555   add_dwarf_attr (die, &attr);
6556 }
6557
6558 /* Add a location description attribute value to a DIE.  */
6559
6560 static inline void
6561 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
6562 {
6563   dw_attr_node attr;
6564
6565   attr.dw_attr = attr_kind;
6566   attr.dw_attr_val.val_class = dw_val_class_loc;
6567   attr.dw_attr_val.v.val_loc = loc;
6568   add_dwarf_attr (die, &attr);
6569 }
6570
6571 static inline dw_loc_descr_ref
6572 AT_loc (dw_attr_ref a)
6573 {
6574   gcc_assert (a && AT_class (a) == dw_val_class_loc);
6575   return a->dw_attr_val.v.val_loc;
6576 }
6577
6578 static inline void
6579 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
6580 {
6581   dw_attr_node attr;
6582
6583   attr.dw_attr = attr_kind;
6584   attr.dw_attr_val.val_class = dw_val_class_loc_list;
6585   attr.dw_attr_val.v.val_loc_list = loc_list;
6586   add_dwarf_attr (die, &attr);
6587   have_location_lists = true;
6588 }
6589
6590 static inline dw_loc_list_ref
6591 AT_loc_list (dw_attr_ref a)
6592 {
6593   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
6594   return a->dw_attr_val.v.val_loc_list;
6595 }
6596
6597 /* Add an address constant attribute value to a DIE.  */
6598
6599 static inline void
6600 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
6601 {
6602   dw_attr_node attr;
6603
6604   attr.dw_attr = attr_kind;
6605   attr.dw_attr_val.val_class = dw_val_class_addr;
6606   attr.dw_attr_val.v.val_addr = addr;
6607   add_dwarf_attr (die, &attr);
6608 }
6609
6610 /* Get the RTX from to an address DIE attribute.  */
6611
6612 static inline rtx
6613 AT_addr (dw_attr_ref a)
6614 {
6615   gcc_assert (a && AT_class (a) == dw_val_class_addr);
6616   return a->dw_attr_val.v.val_addr;
6617 }
6618
6619 /* Add a file attribute value to a DIE.  */
6620
6621 static inline void
6622 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
6623              struct dwarf_file_data *fd)
6624 {
6625   dw_attr_node attr;
6626
6627   attr.dw_attr = attr_kind;
6628   attr.dw_attr_val.val_class = dw_val_class_file;
6629   attr.dw_attr_val.v.val_file = fd;
6630   add_dwarf_attr (die, &attr);
6631 }
6632
6633 /* Get the dwarf_file_data from a file DIE attribute.  */
6634
6635 static inline struct dwarf_file_data *
6636 AT_file (dw_attr_ref a)
6637 {
6638   gcc_assert (a && AT_class (a) == dw_val_class_file);
6639   return a->dw_attr_val.v.val_file;
6640 }
6641
6642 /* Add a label identifier attribute value to a DIE.  */
6643
6644 static inline void
6645 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
6646 {
6647   dw_attr_node attr;
6648
6649   attr.dw_attr = attr_kind;
6650   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
6651   attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
6652   add_dwarf_attr (die, &attr);
6653 }
6654
6655 /* Add a section offset attribute value to a DIE, an offset into the
6656    debug_line section.  */
6657
6658 static inline void
6659 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
6660                 const char *label)
6661 {
6662   dw_attr_node attr;
6663
6664   attr.dw_attr = attr_kind;
6665   attr.dw_attr_val.val_class = dw_val_class_lineptr;
6666   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
6667   add_dwarf_attr (die, &attr);
6668 }
6669
6670 /* Add a section offset attribute value to a DIE, an offset into the
6671    debug_macinfo section.  */
6672
6673 static inline void
6674 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
6675                const char *label)
6676 {
6677   dw_attr_node attr;
6678
6679   attr.dw_attr = attr_kind;
6680   attr.dw_attr_val.val_class = dw_val_class_macptr;
6681   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
6682   add_dwarf_attr (die, &attr);
6683 }
6684
6685 /* Add an offset attribute value to a DIE.  */
6686
6687 static inline void
6688 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
6689                unsigned HOST_WIDE_INT offset)
6690 {
6691   dw_attr_node attr;
6692
6693   attr.dw_attr = attr_kind;
6694   attr.dw_attr_val.val_class = dw_val_class_offset;
6695   attr.dw_attr_val.v.val_offset = offset;
6696   add_dwarf_attr (die, &attr);
6697 }
6698
6699 /* Add an range_list attribute value to a DIE.  */
6700
6701 static void
6702 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
6703                    long unsigned int offset)
6704 {
6705   dw_attr_node attr;
6706
6707   attr.dw_attr = attr_kind;
6708   attr.dw_attr_val.val_class = dw_val_class_range_list;
6709   attr.dw_attr_val.v.val_offset = offset;
6710   add_dwarf_attr (die, &attr);
6711 }
6712
6713 static inline const char *
6714 AT_lbl (dw_attr_ref a)
6715 {
6716   gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
6717                     || AT_class (a) == dw_val_class_lineptr
6718                     || AT_class (a) == dw_val_class_macptr));
6719   return a->dw_attr_val.v.val_lbl_id;
6720 }
6721
6722 /* Get the attribute of type attr_kind.  */
6723
6724 static dw_attr_ref
6725 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
6726 {
6727   dw_attr_ref a;
6728   unsigned ix;
6729   dw_die_ref spec = NULL;
6730
6731   if (! die)
6732     return NULL;
6733
6734   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6735     if (a->dw_attr == attr_kind)
6736       return a;
6737     else if (a->dw_attr == DW_AT_specification
6738              || a->dw_attr == DW_AT_abstract_origin)
6739       spec = AT_ref (a);
6740
6741   if (spec)
6742     return get_AT (spec, attr_kind);
6743
6744   return NULL;
6745 }
6746
6747 /* Return the "low pc" attribute value, typically associated with a subprogram
6748    DIE.  Return null if the "low pc" attribute is either not present, or if it
6749    cannot be represented as an assembler label identifier.  */
6750
6751 static inline const char *
6752 get_AT_low_pc (dw_die_ref die)
6753 {
6754   dw_attr_ref a = get_AT (die, DW_AT_low_pc);
6755
6756   return a ? AT_lbl (a) : NULL;
6757 }
6758
6759 /* Return the "high pc" attribute value, typically associated with a subprogram
6760    DIE.  Return null if the "high pc" attribute is either not present, or if it
6761    cannot be represented as an assembler label identifier.  */
6762
6763 static inline const char *
6764 get_AT_hi_pc (dw_die_ref die)
6765 {
6766   dw_attr_ref a = get_AT (die, DW_AT_high_pc);
6767
6768   return a ? AT_lbl (a) : NULL;
6769 }
6770
6771 /* Return the value of the string attribute designated by ATTR_KIND, or
6772    NULL if it is not present.  */
6773
6774 static inline const char *
6775 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
6776 {
6777   dw_attr_ref a = get_AT (die, attr_kind);
6778
6779   return a ? AT_string (a) : NULL;
6780 }
6781
6782 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
6783    if it is not present.  */
6784
6785 static inline int
6786 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
6787 {
6788   dw_attr_ref a = get_AT (die, attr_kind);
6789
6790   return a ? AT_flag (a) : 0;
6791 }
6792
6793 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
6794    if it is not present.  */
6795
6796 static inline unsigned
6797 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
6798 {
6799   dw_attr_ref a = get_AT (die, attr_kind);
6800
6801   return a ? AT_unsigned (a) : 0;
6802 }
6803
6804 static inline dw_die_ref
6805 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
6806 {
6807   dw_attr_ref a = get_AT (die, attr_kind);
6808
6809   return a ? AT_ref (a) : NULL;
6810 }
6811
6812 static inline struct dwarf_file_data *
6813 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
6814 {
6815   dw_attr_ref a = get_AT (die, attr_kind);
6816
6817   return a ? AT_file (a) : NULL;
6818 }
6819
6820 /* Return TRUE if the language is C or C++.  */
6821
6822 static inline bool
6823 is_c_family (void)
6824 {
6825   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
6826
6827   return (lang == DW_LANG_C || lang == DW_LANG_C89 || lang == DW_LANG_ObjC
6828           || lang == DW_LANG_C99
6829           || lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus);
6830 }
6831
6832 /* Return TRUE if the language is C++.  */
6833
6834 static inline bool
6835 is_cxx (void)
6836 {
6837   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
6838
6839   return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
6840 }
6841
6842 /* Return TRUE if the language is Fortran.  */
6843
6844 static inline bool
6845 is_fortran (void)
6846 {
6847   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
6848
6849   return (lang == DW_LANG_Fortran77
6850           || lang == DW_LANG_Fortran90
6851           || lang == DW_LANG_Fortran95);
6852 }
6853
6854 /* Return TRUE if the language is Java.  */
6855
6856 static inline bool
6857 is_java (void)
6858 {
6859   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
6860
6861   return lang == DW_LANG_Java;
6862 }
6863
6864 /* Return TRUE if the language is Ada.  */
6865
6866 static inline bool
6867 is_ada (void)
6868 {
6869   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
6870
6871   return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
6872 }
6873
6874 /* Remove the specified attribute if present.  */
6875
6876 static void
6877 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
6878 {
6879   dw_attr_ref a;
6880   unsigned ix;
6881
6882   if (! die)
6883     return;
6884
6885   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6886     if (a->dw_attr == attr_kind)
6887       {
6888         if (AT_class (a) == dw_val_class_str)
6889           if (a->dw_attr_val.v.val_str->refcount)
6890             a->dw_attr_val.v.val_str->refcount--;
6891
6892         /* VEC_ordered_remove should help reduce the number of abbrevs
6893            that are needed.  */
6894         VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
6895         return;
6896       }
6897 }
6898
6899 /* Remove CHILD from its parent.  PREV must have the property that
6900    PREV->DIE_SIB == CHILD.  Does not alter CHILD.  */
6901
6902 static void
6903 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
6904 {
6905   gcc_assert (child->die_parent == prev->die_parent);
6906   gcc_assert (prev->die_sib == child);
6907   if (prev == child)
6908     {
6909       gcc_assert (child->die_parent->die_child == child);
6910       prev = NULL;
6911     }
6912   else
6913     prev->die_sib = child->die_sib;
6914   if (child->die_parent->die_child == child)
6915     child->die_parent->die_child = prev;
6916 }
6917
6918 /* Remove child DIE whose die_tag is TAG.  Do nothing if no child
6919    matches TAG.  */
6920
6921 static void
6922 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
6923 {
6924   dw_die_ref c;
6925
6926   c = die->die_child;
6927   if (c) do {
6928     dw_die_ref prev = c;
6929     c = c->die_sib;
6930     while (c->die_tag == tag)
6931       {
6932         remove_child_with_prev (c, prev);
6933         /* Might have removed every child.  */
6934         if (c == c->die_sib)
6935           return;
6936         c = c->die_sib;
6937       }
6938   } while (c != die->die_child);
6939 }
6940
6941 /* Add a CHILD_DIE as the last child of DIE.  */
6942
6943 static void
6944 add_child_die (dw_die_ref die, dw_die_ref child_die)
6945 {
6946   /* FIXME this should probably be an assert.  */
6947   if (! die || ! child_die)
6948     return;
6949   gcc_assert (die != child_die);
6950
6951   child_die->die_parent = die;
6952   if (die->die_child)
6953     {
6954       child_die->die_sib = die->die_child->die_sib;
6955       die->die_child->die_sib = child_die;
6956     }
6957   else
6958     child_die->die_sib = child_die;
6959   die->die_child = child_die;
6960 }
6961
6962 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
6963    is the specification, to the end of PARENT's list of children.
6964    This is done by removing and re-adding it.  */
6965
6966 static void
6967 splice_child_die (dw_die_ref parent, dw_die_ref child)
6968 {
6969   dw_die_ref p;
6970
6971   /* We want the declaration DIE from inside the class, not the
6972      specification DIE at toplevel.  */
6973   if (child->die_parent != parent)
6974     {
6975       dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
6976
6977       if (tmp)
6978         child = tmp;
6979     }
6980
6981   gcc_assert (child->die_parent == parent
6982               || (child->die_parent
6983                   == get_AT_ref (parent, DW_AT_specification)));
6984
6985   for (p = child->die_parent->die_child; ; p = p->die_sib)
6986     if (p->die_sib == child)
6987       {
6988         remove_child_with_prev (child, p);
6989         break;
6990       }
6991
6992   add_child_die (parent, child);
6993 }
6994
6995 /* Return a pointer to a newly created DIE node.  */
6996
6997 static inline dw_die_ref
6998 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
6999 {
7000   dw_die_ref die = GGC_CNEW (die_node);
7001
7002   die->die_tag = tag_value;
7003
7004   if (parent_die != NULL)
7005     add_child_die (parent_die, die);
7006   else
7007     {
7008       limbo_die_node *limbo_node;
7009
7010       limbo_node = GGC_CNEW (limbo_die_node);
7011       limbo_node->die = die;
7012       limbo_node->created_for = t;
7013       limbo_node->next = limbo_die_list;
7014       limbo_die_list = limbo_node;
7015     }
7016
7017   return die;
7018 }
7019
7020 /* Return the DIE associated with the given type specifier.  */
7021
7022 static inline dw_die_ref
7023 lookup_type_die (tree type)
7024 {
7025   return TYPE_SYMTAB_DIE (type);
7026 }
7027
7028 /* Equate a DIE to a given type specifier.  */
7029
7030 static inline void
7031 equate_type_number_to_die (tree type, dw_die_ref type_die)
7032 {
7033   TYPE_SYMTAB_DIE (type) = type_die;
7034 }
7035
7036 /* Returns a hash value for X (which really is a die_struct).  */
7037
7038 static hashval_t
7039 decl_die_table_hash (const void *x)
7040 {
7041   return (hashval_t) ((const_dw_die_ref) x)->decl_id;
7042 }
7043
7044 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
7045
7046 static int
7047 decl_die_table_eq (const void *x, const void *y)
7048 {
7049   return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
7050 }
7051
7052 /* Return the DIE associated with a given declaration.  */
7053
7054 static inline dw_die_ref
7055 lookup_decl_die (tree decl)
7056 {
7057   return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
7058 }
7059
7060 /* Returns a hash value for X (which really is a var_loc_list).  */
7061
7062 static hashval_t
7063 decl_loc_table_hash (const void *x)
7064 {
7065   return (hashval_t) ((const var_loc_list *) x)->decl_id;
7066 }
7067
7068 /* Return nonzero if decl_id of var_loc_list X is the same as
7069    UID of decl *Y.  */
7070
7071 static int
7072 decl_loc_table_eq (const void *x, const void *y)
7073 {
7074   return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
7075 }
7076
7077 /* Return the var_loc list associated with a given declaration.  */
7078
7079 static inline var_loc_list *
7080 lookup_decl_loc (const_tree decl)
7081 {
7082   return (var_loc_list *)
7083     htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
7084 }
7085
7086 /* Equate a DIE to a particular declaration.  */
7087
7088 static void
7089 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
7090 {
7091   unsigned int decl_id = DECL_UID (decl);
7092   void **slot;
7093
7094   slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
7095   *slot = decl_die;
7096   decl_die->decl_id = decl_id;
7097 }
7098
7099 /* Add a variable location node to the linked list for DECL.  */
7100
7101 static void
7102 add_var_loc_to_decl (tree decl, struct var_loc_node *loc)
7103 {
7104   unsigned int decl_id = DECL_UID (decl);
7105   var_loc_list *temp;
7106   void **slot;
7107
7108   slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
7109   if (*slot == NULL)
7110     {
7111       temp = GGC_CNEW (var_loc_list);
7112       temp->decl_id = decl_id;
7113       *slot = temp;
7114     }
7115   else
7116     temp = (var_loc_list *) *slot;
7117
7118   if (temp->last)
7119     {
7120       /* If the current location is the same as the end of the list,
7121          and either both or neither of the locations is uninitialized,
7122          we have nothing to do.  */
7123       if ((!rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->last->var_loc_note),
7124                          NOTE_VAR_LOCATION_LOC (loc->var_loc_note)))
7125           || ((NOTE_VAR_LOCATION_STATUS (temp->last->var_loc_note)
7126                != NOTE_VAR_LOCATION_STATUS (loc->var_loc_note))
7127               && ((NOTE_VAR_LOCATION_STATUS (temp->last->var_loc_note)
7128                    == VAR_INIT_STATUS_UNINITIALIZED)
7129                   || (NOTE_VAR_LOCATION_STATUS (loc->var_loc_note)
7130                       == VAR_INIT_STATUS_UNINITIALIZED))))
7131         {
7132           /* Add LOC to the end of list and update LAST.  */
7133           temp->last->next = loc;
7134           temp->last = loc;
7135         }
7136     }
7137   /* Do not add empty location to the beginning of the list.  */
7138   else if (NOTE_VAR_LOCATION_LOC (loc->var_loc_note) != NULL_RTX)
7139     {
7140       temp->first = loc;
7141       temp->last = loc;
7142     }
7143 }
7144 \f
7145 /* Keep track of the number of spaces used to indent the
7146    output of the debugging routines that print the structure of
7147    the DIE internal representation.  */
7148 static int print_indent;
7149
7150 /* Indent the line the number of spaces given by print_indent.  */
7151
7152 static inline void
7153 print_spaces (FILE *outfile)
7154 {
7155   fprintf (outfile, "%*s", print_indent, "");
7156 }
7157
7158 /* Print the information associated with a given DIE, and its children.
7159    This routine is a debugging aid only.  */
7160
7161 static void
7162 print_die (dw_die_ref die, FILE *outfile)
7163 {
7164   dw_attr_ref a;
7165   dw_die_ref c;
7166   unsigned ix;
7167
7168   print_spaces (outfile);
7169   fprintf (outfile, "DIE %4ld: %s\n",
7170            die->die_offset, dwarf_tag_name (die->die_tag));
7171   print_spaces (outfile);
7172   fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
7173   fprintf (outfile, " offset: %ld\n", die->die_offset);
7174
7175   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7176     {
7177       print_spaces (outfile);
7178       fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
7179
7180       switch (AT_class (a))
7181         {
7182         case dw_val_class_addr:
7183           fprintf (outfile, "address");
7184           break;
7185         case dw_val_class_offset:
7186           fprintf (outfile, "offset");
7187           break;
7188         case dw_val_class_loc:
7189           fprintf (outfile, "location descriptor");
7190           break;
7191         case dw_val_class_loc_list:
7192           fprintf (outfile, "location list -> label:%s",
7193                    AT_loc_list (a)->ll_symbol);
7194           break;
7195         case dw_val_class_range_list:
7196           fprintf (outfile, "range list");
7197           break;
7198         case dw_val_class_const:
7199           fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
7200           break;
7201         case dw_val_class_unsigned_const:
7202           fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
7203           break;
7204         case dw_val_class_long_long:
7205           fprintf (outfile, "constant (%lu,%lu)",
7206                    a->dw_attr_val.v.val_long_long.hi,
7207                    a->dw_attr_val.v.val_long_long.low);
7208           break;
7209         case dw_val_class_vec:
7210           fprintf (outfile, "floating-point or vector constant");
7211           break;
7212         case dw_val_class_flag:
7213           fprintf (outfile, "%u", AT_flag (a));
7214           break;
7215         case dw_val_class_die_ref:
7216           if (AT_ref (a) != NULL)
7217             {
7218               if (AT_ref (a)->die_symbol)
7219                 fprintf (outfile, "die -> label: %s", AT_ref (a)->die_symbol);
7220               else
7221                 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
7222             }
7223           else
7224             fprintf (outfile, "die -> <null>");
7225           break;
7226         case dw_val_class_lbl_id:
7227         case dw_val_class_lineptr:
7228         case dw_val_class_macptr:
7229           fprintf (outfile, "label: %s", AT_lbl (a));
7230           break;
7231         case dw_val_class_str:
7232           if (AT_string (a) != NULL)
7233             fprintf (outfile, "\"%s\"", AT_string (a));
7234           else
7235             fprintf (outfile, "<null>");
7236           break;
7237         case dw_val_class_file:
7238           fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
7239                    AT_file (a)->emitted_number);
7240           break;
7241         default:
7242           break;
7243         }
7244
7245       fprintf (outfile, "\n");
7246     }
7247
7248   if (die->die_child != NULL)
7249     {
7250       print_indent += 4;
7251       FOR_EACH_CHILD (die, c, print_die (c, outfile));
7252       print_indent -= 4;
7253     }
7254   if (print_indent == 0)
7255     fprintf (outfile, "\n");
7256 }
7257
7258 /* Print the contents of the source code line number correspondence table.
7259    This routine is a debugging aid only.  */
7260
7261 static void
7262 print_dwarf_line_table (FILE *outfile)
7263 {
7264   unsigned i;
7265   dw_line_info_ref line_info;
7266
7267   fprintf (outfile, "\n\nDWARF source line information\n");
7268   for (i = 1; i < line_info_table_in_use; i++)
7269     {
7270       line_info = &line_info_table[i];
7271       fprintf (outfile, "%5d: %4ld %6ld\n", i,
7272                line_info->dw_file_num,
7273                line_info->dw_line_num);
7274     }
7275
7276   fprintf (outfile, "\n\n");
7277 }
7278
7279 /* Print the information collected for a given DIE.  */
7280
7281 void
7282 debug_dwarf_die (dw_die_ref die)
7283 {
7284   print_die (die, stderr);
7285 }
7286
7287 /* Print all DWARF information collected for the compilation unit.
7288    This routine is a debugging aid only.  */
7289
7290 void
7291 debug_dwarf (void)
7292 {
7293   print_indent = 0;
7294   print_die (comp_unit_die, stderr);
7295   if (! DWARF2_ASM_LINE_DEBUG_INFO)
7296     print_dwarf_line_table (stderr);
7297 }
7298 \f
7299 /* Start a new compilation unit DIE for an include file.  OLD_UNIT is the CU
7300    for the enclosing include file, if any.  BINCL_DIE is the DW_TAG_GNU_BINCL
7301    DIE that marks the start of the DIEs for this include file.  */
7302
7303 static dw_die_ref
7304 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
7305 {
7306   const char *filename = get_AT_string (bincl_die, DW_AT_name);
7307   dw_die_ref new_unit = gen_compile_unit_die (filename);
7308
7309   new_unit->die_sib = old_unit;
7310   return new_unit;
7311 }
7312
7313 /* Close an include-file CU and reopen the enclosing one.  */
7314
7315 static dw_die_ref
7316 pop_compile_unit (dw_die_ref old_unit)
7317 {
7318   dw_die_ref new_unit = old_unit->die_sib;
7319
7320   old_unit->die_sib = NULL;
7321   return new_unit;
7322 }
7323
7324 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
7325 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
7326
7327 /* Calculate the checksum of a location expression.  */
7328
7329 static inline void
7330 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
7331 {
7332   int tem;
7333
7334   tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
7335   CHECKSUM (tem);
7336   CHECKSUM (loc->dw_loc_oprnd1);
7337   CHECKSUM (loc->dw_loc_oprnd2);
7338 }
7339
7340 /* Calculate the checksum of an attribute.  */
7341
7342 static void
7343 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
7344 {
7345   dw_loc_descr_ref loc;
7346   rtx r;
7347
7348   CHECKSUM (at->dw_attr);
7349
7350   /* We don't care that this was compiled with a different compiler
7351      snapshot; if the output is the same, that's what matters.  */
7352   if (at->dw_attr == DW_AT_producer)
7353     return;
7354
7355   switch (AT_class (at))
7356     {
7357     case dw_val_class_const:
7358       CHECKSUM (at->dw_attr_val.v.val_int);
7359       break;
7360     case dw_val_class_unsigned_const:
7361       CHECKSUM (at->dw_attr_val.v.val_unsigned);
7362       break;
7363     case dw_val_class_long_long:
7364       CHECKSUM (at->dw_attr_val.v.val_long_long);
7365       break;
7366     case dw_val_class_vec:
7367       CHECKSUM (at->dw_attr_val.v.val_vec);
7368       break;
7369     case dw_val_class_flag:
7370       CHECKSUM (at->dw_attr_val.v.val_flag);
7371       break;
7372     case dw_val_class_str:
7373       CHECKSUM_STRING (AT_string (at));
7374       break;
7375
7376     case dw_val_class_addr:
7377       r = AT_addr (at);
7378       gcc_assert (GET_CODE (r) == SYMBOL_REF);
7379       CHECKSUM_STRING (XSTR (r, 0));
7380       break;
7381
7382     case dw_val_class_offset:
7383       CHECKSUM (at->dw_attr_val.v.val_offset);
7384       break;
7385
7386     case dw_val_class_loc:
7387       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
7388         loc_checksum (loc, ctx);
7389       break;
7390
7391     case dw_val_class_die_ref:
7392       die_checksum (AT_ref (at), ctx, mark);
7393       break;
7394
7395     case dw_val_class_fde_ref:
7396     case dw_val_class_lbl_id:
7397     case dw_val_class_lineptr:
7398     case dw_val_class_macptr:
7399       break;
7400
7401     case dw_val_class_file:
7402       CHECKSUM_STRING (AT_file (at)->filename);
7403       break;
7404
7405     default:
7406       break;
7407     }
7408 }
7409
7410 /* Calculate the checksum of a DIE.  */
7411
7412 static void
7413 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
7414 {
7415   dw_die_ref c;
7416   dw_attr_ref a;
7417   unsigned ix;
7418
7419   /* To avoid infinite recursion.  */
7420   if (die->die_mark)
7421     {
7422       CHECKSUM (die->die_mark);
7423       return;
7424     }
7425   die->die_mark = ++(*mark);
7426
7427   CHECKSUM (die->die_tag);
7428
7429   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7430     attr_checksum (a, ctx, mark);
7431
7432   FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
7433 }
7434
7435 #undef CHECKSUM
7436 #undef CHECKSUM_STRING
7437
7438 /* Do the location expressions look same?  */
7439 static inline int
7440 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
7441 {
7442   return loc1->dw_loc_opc == loc2->dw_loc_opc
7443          && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
7444          && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
7445 }
7446
7447 /* Do the values look the same?  */
7448 static int
7449 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
7450 {
7451   dw_loc_descr_ref loc1, loc2;
7452   rtx r1, r2;
7453
7454   if (v1->val_class != v2->val_class)
7455     return 0;
7456
7457   switch (v1->val_class)
7458     {
7459     case dw_val_class_const:
7460       return v1->v.val_int == v2->v.val_int;
7461     case dw_val_class_unsigned_const:
7462       return v1->v.val_unsigned == v2->v.val_unsigned;
7463     case dw_val_class_long_long:
7464       return v1->v.val_long_long.hi == v2->v.val_long_long.hi
7465              && v1->v.val_long_long.low == v2->v.val_long_long.low;
7466     case dw_val_class_vec:
7467       if (v1->v.val_vec.length != v2->v.val_vec.length
7468           || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
7469         return 0;
7470       if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
7471                   v1->v.val_vec.length * v1->v.val_vec.elt_size))
7472         return 0;
7473       return 1;
7474     case dw_val_class_flag:
7475       return v1->v.val_flag == v2->v.val_flag;
7476     case dw_val_class_str:
7477       return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
7478
7479     case dw_val_class_addr:
7480       r1 = v1->v.val_addr;
7481       r2 = v2->v.val_addr;
7482       if (GET_CODE (r1) != GET_CODE (r2))
7483         return 0;
7484       gcc_assert (GET_CODE (r1) == SYMBOL_REF);
7485       return !strcmp (XSTR (r1, 0), XSTR (r2, 0));
7486
7487     case dw_val_class_offset:
7488       return v1->v.val_offset == v2->v.val_offset;
7489
7490     case dw_val_class_loc:
7491       for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
7492            loc1 && loc2;
7493            loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
7494         if (!same_loc_p (loc1, loc2, mark))
7495           return 0;
7496       return !loc1 && !loc2;
7497
7498     case dw_val_class_die_ref:
7499       return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
7500
7501     case dw_val_class_fde_ref:
7502     case dw_val_class_lbl_id:
7503     case dw_val_class_lineptr:
7504     case dw_val_class_macptr:
7505       return 1;
7506
7507     case dw_val_class_file:
7508       return v1->v.val_file == v2->v.val_file;
7509
7510     default:
7511       return 1;
7512     }
7513 }
7514
7515 /* Do the attributes look the same?  */
7516
7517 static int
7518 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
7519 {
7520   if (at1->dw_attr != at2->dw_attr)
7521     return 0;
7522
7523   /* We don't care that this was compiled with a different compiler
7524      snapshot; if the output is the same, that's what matters. */
7525   if (at1->dw_attr == DW_AT_producer)
7526     return 1;
7527
7528   return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
7529 }
7530
7531 /* Do the dies look the same?  */
7532
7533 static int
7534 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
7535 {
7536   dw_die_ref c1, c2;
7537   dw_attr_ref a1;
7538   unsigned ix;
7539
7540   /* To avoid infinite recursion.  */
7541   if (die1->die_mark)
7542     return die1->die_mark == die2->die_mark;
7543   die1->die_mark = die2->die_mark = ++(*mark);
7544
7545   if (die1->die_tag != die2->die_tag)
7546     return 0;
7547
7548   if (VEC_length (dw_attr_node, die1->die_attr)
7549       != VEC_length (dw_attr_node, die2->die_attr))
7550     return 0;
7551
7552   for (ix = 0; VEC_iterate (dw_attr_node, die1->die_attr, ix, a1); ix++)
7553     if (!same_attr_p (a1, VEC_index (dw_attr_node, die2->die_attr, ix), mark))
7554       return 0;
7555
7556   c1 = die1->die_child;
7557   c2 = die2->die_child;
7558   if (! c1)
7559     {
7560       if (c2)
7561         return 0;
7562     }
7563   else
7564     for (;;)
7565       {
7566         if (!same_die_p (c1, c2, mark))
7567           return 0;
7568         c1 = c1->die_sib;
7569         c2 = c2->die_sib;
7570         if (c1 == die1->die_child)
7571           {
7572             if (c2 == die2->die_child)
7573               break;
7574             else
7575               return 0;
7576           }
7577     }
7578
7579   return 1;
7580 }
7581
7582 /* Do the dies look the same?  Wrapper around same_die_p.  */
7583
7584 static int
7585 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
7586 {
7587   int mark = 0;
7588   int ret = same_die_p (die1, die2, &mark);
7589
7590   unmark_all_dies (die1);
7591   unmark_all_dies (die2);
7592
7593   return ret;
7594 }
7595
7596 /* The prefix to attach to symbols on DIEs in the current comdat debug
7597    info section.  */
7598 static char *comdat_symbol_id;
7599
7600 /* The index of the current symbol within the current comdat CU.  */
7601 static unsigned int comdat_symbol_number;
7602
7603 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
7604    children, and set comdat_symbol_id accordingly.  */
7605
7606 static void
7607 compute_section_prefix (dw_die_ref unit_die)
7608 {
7609   const char *die_name = get_AT_string (unit_die, DW_AT_name);
7610   const char *base = die_name ? lbasename (die_name) : "anonymous";
7611   char *name = XALLOCAVEC (char, strlen (base) + 64);
7612   char *p;
7613   int i, mark;
7614   unsigned char checksum[16];
7615   struct md5_ctx ctx;
7616
7617   /* Compute the checksum of the DIE, then append part of it as hex digits to
7618      the name filename of the unit.  */
7619
7620   md5_init_ctx (&ctx);
7621   mark = 0;
7622   die_checksum (unit_die, &ctx, &mark);
7623   unmark_all_dies (unit_die);
7624   md5_finish_ctx (&ctx, checksum);
7625
7626   sprintf (name, "%s.", base);
7627   clean_symbol_name (name);
7628
7629   p = name + strlen (name);
7630   for (i = 0; i < 4; i++)
7631     {
7632       sprintf (p, "%.2x", checksum[i]);
7633       p += 2;
7634     }
7635
7636   comdat_symbol_id = unit_die->die_symbol = xstrdup (name);
7637   comdat_symbol_number = 0;
7638 }
7639
7640 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P.  */
7641
7642 static int
7643 is_type_die (dw_die_ref die)
7644 {
7645   switch (die->die_tag)
7646     {
7647     case DW_TAG_array_type:
7648     case DW_TAG_class_type:
7649     case DW_TAG_interface_type:
7650     case DW_TAG_enumeration_type:
7651     case DW_TAG_pointer_type:
7652     case DW_TAG_reference_type:
7653     case DW_TAG_string_type:
7654     case DW_TAG_structure_type:
7655     case DW_TAG_subroutine_type:
7656     case DW_TAG_union_type:
7657     case DW_TAG_ptr_to_member_type:
7658     case DW_TAG_set_type:
7659     case DW_TAG_subrange_type:
7660     case DW_TAG_base_type:
7661     case DW_TAG_const_type:
7662     case DW_TAG_file_type:
7663     case DW_TAG_packed_type:
7664     case DW_TAG_volatile_type:
7665     case DW_TAG_typedef:
7666       return 1;
7667     default:
7668       return 0;
7669     }
7670 }
7671
7672 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
7673    Basically, we want to choose the bits that are likely to be shared between
7674    compilations (types) and leave out the bits that are specific to individual
7675    compilations (functions).  */
7676
7677 static int
7678 is_comdat_die (dw_die_ref c)
7679 {
7680   /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
7681      we do for stabs.  The advantage is a greater likelihood of sharing between
7682      objects that don't include headers in the same order (and therefore would
7683      put the base types in a different comdat).  jason 8/28/00 */
7684
7685   if (c->die_tag == DW_TAG_base_type)
7686     return 0;
7687
7688   if (c->die_tag == DW_TAG_pointer_type
7689       || c->die_tag == DW_TAG_reference_type
7690       || c->die_tag == DW_TAG_const_type
7691       || c->die_tag == DW_TAG_volatile_type)
7692     {
7693       dw_die_ref t = get_AT_ref (c, DW_AT_type);
7694
7695       return t ? is_comdat_die (t) : 0;
7696     }
7697
7698   return is_type_die (c);
7699 }
7700
7701 /* Returns 1 iff C is the sort of DIE that might be referred to from another
7702    compilation unit.  */
7703
7704 static int
7705 is_symbol_die (dw_die_ref c)
7706 {
7707   return (is_type_die (c)
7708           || (get_AT (c, DW_AT_declaration)
7709               && !get_AT (c, DW_AT_specification))
7710           || c->die_tag == DW_TAG_namespace
7711           || c->die_tag == DW_TAG_module);
7712 }
7713
7714 static char *
7715 gen_internal_sym (const char *prefix)
7716 {
7717   char buf[256];
7718
7719   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
7720   return xstrdup (buf);
7721 }
7722
7723 /* Assign symbols to all worthy DIEs under DIE.  */
7724
7725 static void
7726 assign_symbol_names (dw_die_ref die)
7727 {
7728   dw_die_ref c;
7729
7730   if (is_symbol_die (die))
7731     {
7732       if (comdat_symbol_id)
7733         {
7734           char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
7735
7736           sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
7737                    comdat_symbol_id, comdat_symbol_number++);
7738           die->die_symbol = xstrdup (p);
7739         }
7740       else
7741         die->die_symbol = gen_internal_sym ("LDIE");
7742     }
7743
7744   FOR_EACH_CHILD (die, c, assign_symbol_names (c));
7745 }
7746
7747 struct cu_hash_table_entry
7748 {
7749   dw_die_ref cu;
7750   unsigned min_comdat_num, max_comdat_num;
7751   struct cu_hash_table_entry *next;
7752 };
7753
7754 /* Routines to manipulate hash table of CUs.  */
7755 static hashval_t
7756 htab_cu_hash (const void *of)
7757 {
7758   const struct cu_hash_table_entry *const entry =
7759     (const struct cu_hash_table_entry *) of;
7760
7761   return htab_hash_string (entry->cu->die_symbol);
7762 }
7763
7764 static int
7765 htab_cu_eq (const void *of1, const void *of2)
7766 {
7767   const struct cu_hash_table_entry *const entry1 =
7768     (const struct cu_hash_table_entry *) of1;
7769   const struct die_struct *const entry2 = (const struct die_struct *) of2;
7770
7771   return !strcmp (entry1->cu->die_symbol, entry2->die_symbol);
7772 }
7773
7774 static void
7775 htab_cu_del (void *what)
7776 {
7777   struct cu_hash_table_entry *next,
7778     *entry = (struct cu_hash_table_entry *) what;
7779
7780   while (entry)
7781     {
7782       next = entry->next;
7783       free (entry);
7784       entry = next;
7785     }
7786 }
7787
7788 /* Check whether we have already seen this CU and set up SYM_NUM
7789    accordingly.  */
7790 static int
7791 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
7792 {
7793   struct cu_hash_table_entry dummy;
7794   struct cu_hash_table_entry **slot, *entry, *last = &dummy;
7795
7796   dummy.max_comdat_num = 0;
7797
7798   slot = (struct cu_hash_table_entry **)
7799     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
7800         INSERT);
7801   entry = *slot;
7802
7803   for (; entry; last = entry, entry = entry->next)
7804     {
7805       if (same_die_p_wrap (cu, entry->cu))
7806         break;
7807     }
7808
7809   if (entry)
7810     {
7811       *sym_num = entry->min_comdat_num;
7812       return 1;
7813     }
7814
7815   entry = XCNEW (struct cu_hash_table_entry);
7816   entry->cu = cu;
7817   entry->min_comdat_num = *sym_num = last->max_comdat_num;
7818   entry->next = *slot;
7819   *slot = entry;
7820
7821   return 0;
7822 }
7823
7824 /* Record SYM_NUM to record of CU in HTABLE.  */
7825 static void
7826 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
7827 {
7828   struct cu_hash_table_entry **slot, *entry;
7829
7830   slot = (struct cu_hash_table_entry **)
7831     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
7832         NO_INSERT);
7833   entry = *slot;
7834
7835   entry->max_comdat_num = sym_num;
7836 }
7837
7838 /* Traverse the DIE (which is always comp_unit_die), and set up
7839    additional compilation units for each of the include files we see
7840    bracketed by BINCL/EINCL.  */
7841
7842 static void
7843 break_out_includes (dw_die_ref die)
7844 {
7845   dw_die_ref c;
7846   dw_die_ref unit = NULL;
7847   limbo_die_node *node, **pnode;
7848   htab_t cu_hash_table;
7849
7850   c = die->die_child;
7851   if (c) do {
7852     dw_die_ref prev = c;
7853     c = c->die_sib;
7854     while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
7855            || (unit && is_comdat_die (c)))
7856       {
7857         dw_die_ref next = c->die_sib;
7858
7859         /* This DIE is for a secondary CU; remove it from the main one.  */
7860         remove_child_with_prev (c, prev);
7861
7862         if (c->die_tag == DW_TAG_GNU_BINCL)
7863           unit = push_new_compile_unit (unit, c);
7864         else if (c->die_tag == DW_TAG_GNU_EINCL)
7865           unit = pop_compile_unit (unit);
7866         else
7867           add_child_die (unit, c);
7868         c = next;
7869         if (c == die->die_child)
7870           break;
7871       }
7872   } while (c != die->die_child);
7873
7874 #if 0
7875   /* We can only use this in debugging, since the frontend doesn't check
7876      to make sure that we leave every include file we enter.  */
7877   gcc_assert (!unit);
7878 #endif
7879
7880   assign_symbol_names (die);
7881   cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
7882   for (node = limbo_die_list, pnode = &limbo_die_list;
7883        node;
7884        node = node->next)
7885     {
7886       int is_dupl;
7887
7888       compute_section_prefix (node->die);
7889       is_dupl = check_duplicate_cu (node->die, cu_hash_table,
7890                         &comdat_symbol_number);
7891       assign_symbol_names (node->die);
7892       if (is_dupl)
7893         *pnode = node->next;
7894       else
7895         {
7896           pnode = &node->next;
7897           record_comdat_symbol_number (node->die, cu_hash_table,
7898                 comdat_symbol_number);
7899         }
7900     }
7901   htab_delete (cu_hash_table);
7902 }
7903
7904 /* Traverse the DIE and add a sibling attribute if it may have the
7905    effect of speeding up access to siblings.  To save some space,
7906    avoid generating sibling attributes for DIE's without children.  */
7907
7908 static void
7909 add_sibling_attributes (dw_die_ref die)
7910 {
7911   dw_die_ref c;
7912
7913   if (! die->die_child)
7914     return;
7915
7916   if (die->die_parent && die != die->die_parent->die_child)
7917     add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
7918
7919   FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
7920 }
7921
7922 /* Output all location lists for the DIE and its children.  */
7923
7924 static void
7925 output_location_lists (dw_die_ref die)
7926 {
7927   dw_die_ref c;
7928   dw_attr_ref a;
7929   unsigned ix;
7930
7931   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7932     if (AT_class (a) == dw_val_class_loc_list)
7933       output_loc_list (AT_loc_list (a));
7934
7935   FOR_EACH_CHILD (die, c, output_location_lists (c));
7936 }
7937
7938 /* The format of each DIE (and its attribute value pairs) is encoded in an
7939    abbreviation table.  This routine builds the abbreviation table and assigns
7940    a unique abbreviation id for each abbreviation entry.  The children of each
7941    die are visited recursively.  */
7942
7943 static void
7944 build_abbrev_table (dw_die_ref die)
7945 {
7946   unsigned long abbrev_id;
7947   unsigned int n_alloc;
7948   dw_die_ref c;
7949   dw_attr_ref a;
7950   unsigned ix;
7951
7952   /* Scan the DIE references, and mark as external any that refer to
7953      DIEs from other CUs (i.e. those which are not marked).  */
7954   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7955     if (AT_class (a) == dw_val_class_die_ref
7956         && AT_ref (a)->die_mark == 0)
7957       {
7958         gcc_assert (AT_ref (a)->die_symbol);
7959         set_AT_ref_external (a, 1);
7960       }
7961
7962   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7963     {
7964       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7965       dw_attr_ref die_a, abbrev_a;
7966       unsigned ix;
7967       bool ok = true;
7968
7969       if (abbrev->die_tag != die->die_tag)
7970         continue;
7971       if ((abbrev->die_child != NULL) != (die->die_child != NULL))
7972         continue;
7973
7974       if (VEC_length (dw_attr_node, abbrev->die_attr)
7975           != VEC_length (dw_attr_node, die->die_attr))
7976         continue;
7977
7978       for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, die_a); ix++)
7979         {
7980           abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
7981           if ((abbrev_a->dw_attr != die_a->dw_attr)
7982               || (value_format (abbrev_a) != value_format (die_a)))
7983             {
7984               ok = false;
7985               break;
7986             }
7987         }
7988       if (ok)
7989         break;
7990     }
7991
7992   if (abbrev_id >= abbrev_die_table_in_use)
7993     {
7994       if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
7995         {
7996           n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
7997           abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
7998                                             n_alloc);
7999
8000           memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
8001                  (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
8002           abbrev_die_table_allocated = n_alloc;
8003         }
8004
8005       ++abbrev_die_table_in_use;
8006       abbrev_die_table[abbrev_id] = die;
8007     }
8008
8009   die->die_abbrev = abbrev_id;
8010   FOR_EACH_CHILD (die, c, build_abbrev_table (c));
8011 }
8012 \f
8013 /* Return the power-of-two number of bytes necessary to represent VALUE.  */
8014
8015 static int
8016 constant_size (unsigned HOST_WIDE_INT value)
8017 {
8018   int log;
8019
8020   if (value == 0)
8021     log = 0;
8022   else
8023     log = floor_log2 (value);
8024
8025   log = log / 8;
8026   log = 1 << (floor_log2 (log) + 1);
8027
8028   return log;
8029 }
8030
8031 /* Return the size of a DIE as it is represented in the
8032    .debug_info section.  */
8033
8034 static unsigned long
8035 size_of_die (dw_die_ref die)
8036 {
8037   unsigned long size = 0;
8038   dw_attr_ref a;
8039   unsigned ix;
8040
8041   size += size_of_uleb128 (die->die_abbrev);
8042   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
8043     {
8044       switch (AT_class (a))
8045         {
8046         case dw_val_class_addr:
8047           size += DWARF2_ADDR_SIZE;
8048           break;
8049         case dw_val_class_offset:
8050           size += DWARF_OFFSET_SIZE;
8051           break;
8052         case dw_val_class_loc:
8053           {
8054             unsigned long lsize = size_of_locs (AT_loc (a));
8055
8056             /* Block length.  */
8057             size += constant_size (lsize);
8058             size += lsize;
8059           }
8060           break;
8061         case dw_val_class_loc_list:
8062           size += DWARF_OFFSET_SIZE;
8063           break;
8064         case dw_val_class_range_list:
8065           size += DWARF_OFFSET_SIZE;
8066           break;
8067         case dw_val_class_const:
8068           size += size_of_sleb128 (AT_int (a));
8069           break;
8070         case dw_val_class_unsigned_const:
8071           size += constant_size (AT_unsigned (a));
8072           break;
8073         case dw_val_class_long_long:
8074           size += 1 + 2*HOST_BITS_PER_LONG/HOST_BITS_PER_CHAR; /* block */
8075           break;
8076         case dw_val_class_vec:
8077           size += constant_size (a->dw_attr_val.v.val_vec.length
8078                                  * a->dw_attr_val.v.val_vec.elt_size)
8079                   + a->dw_attr_val.v.val_vec.length
8080                     * a->dw_attr_val.v.val_vec.elt_size; /* block */
8081           break;
8082         case dw_val_class_flag:
8083           size += 1;
8084           break;
8085         case dw_val_class_die_ref:
8086           /* In DWARF2, DW_FORM_ref_addr is sized by target address length,
8087              whereas in DWARF3 it's always sized as an offset.  */
8088           if (AT_ref_external (a) && dwarf_version == 2)
8089             size += DWARF2_ADDR_SIZE;
8090           else
8091             size += DWARF_OFFSET_SIZE;
8092           break;
8093         case dw_val_class_fde_ref:
8094           size += DWARF_OFFSET_SIZE;
8095           break;
8096         case dw_val_class_lbl_id:
8097           size += DWARF2_ADDR_SIZE;
8098           break;
8099         case dw_val_class_lineptr:
8100         case dw_val_class_macptr:
8101           size += DWARF_OFFSET_SIZE;
8102           break;
8103         case dw_val_class_str:
8104           if (AT_string_form (a) == DW_FORM_strp)
8105             size += DWARF_OFFSET_SIZE;
8106           else
8107             size += strlen (a->dw_attr_val.v.val_str->str) + 1;
8108           break;
8109         case dw_val_class_file:
8110           size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
8111           break;
8112         default:
8113           gcc_unreachable ();
8114         }
8115     }
8116
8117   return size;
8118 }
8119
8120 /* Size the debugging information associated with a given DIE.  Visits the
8121    DIE's children recursively.  Updates the global variable next_die_offset, on
8122    each time through.  Uses the current value of next_die_offset to update the
8123    die_offset field in each DIE.  */
8124
8125 static void
8126 calc_die_sizes (dw_die_ref die)
8127 {
8128   dw_die_ref c;
8129
8130   die->die_offset = next_die_offset;
8131   next_die_offset += size_of_die (die);
8132
8133   FOR_EACH_CHILD (die, c, calc_die_sizes (c));
8134
8135   if (die->die_child != NULL)
8136     /* Count the null byte used to terminate sibling lists.  */
8137     next_die_offset += 1;
8138 }
8139
8140 /* Set the marks for a die and its children.  We do this so
8141    that we know whether or not a reference needs to use FORM_ref_addr; only
8142    DIEs in the same CU will be marked.  We used to clear out the offset
8143    and use that as the flag, but ran into ordering problems.  */
8144
8145 static void
8146 mark_dies (dw_die_ref die)
8147 {
8148   dw_die_ref c;
8149
8150   gcc_assert (!die->die_mark);
8151
8152   die->die_mark = 1;
8153   FOR_EACH_CHILD (die, c, mark_dies (c));
8154 }
8155
8156 /* Clear the marks for a die and its children.  */
8157
8158 static void
8159 unmark_dies (dw_die_ref die)
8160 {
8161   dw_die_ref c;
8162
8163   gcc_assert (die->die_mark);
8164
8165   die->die_mark = 0;
8166   FOR_EACH_CHILD (die, c, unmark_dies (c));
8167 }
8168
8169 /* Clear the marks for a die, its children and referred dies.  */
8170
8171 static void
8172 unmark_all_dies (dw_die_ref die)
8173 {
8174   dw_die_ref c;
8175   dw_attr_ref a;
8176   unsigned ix;
8177
8178   if (!die->die_mark)
8179     return;
8180   die->die_mark = 0;
8181
8182   FOR_EACH_CHILD (die, c, unmark_all_dies (c));
8183
8184   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
8185     if (AT_class (a) == dw_val_class_die_ref)
8186       unmark_all_dies (AT_ref (a));
8187 }
8188
8189 /* Return the size of the .debug_pubnames or .debug_pubtypes table
8190    generated for the compilation unit.  */
8191
8192 static unsigned long
8193 size_of_pubnames (VEC (pubname_entry, gc) * names)
8194 {
8195   unsigned long size;
8196   unsigned i;
8197   pubname_ref p;
8198
8199   size = DWARF_PUBNAMES_HEADER_SIZE;
8200   for (i = 0; VEC_iterate (pubname_entry, names, i, p); i++)
8201     if (names != pubtype_table
8202         || p->die->die_offset != 0
8203         || !flag_eliminate_unused_debug_types)
8204       size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
8205
8206   size += DWARF_OFFSET_SIZE;
8207   return size;
8208 }
8209
8210 /* Return the size of the information in the .debug_aranges section.  */
8211
8212 static unsigned long
8213 size_of_aranges (void)
8214 {
8215   unsigned long size;
8216
8217   size = DWARF_ARANGES_HEADER_SIZE;
8218
8219   /* Count the address/length pair for this compilation unit.  */
8220   if (text_section_used)
8221     size += 2 * DWARF2_ADDR_SIZE;
8222   if (cold_text_section_used)
8223     size += 2 * DWARF2_ADDR_SIZE;
8224   size += 2 * DWARF2_ADDR_SIZE * arange_table_in_use;
8225
8226   /* Count the two zero words used to terminated the address range table.  */
8227   size += 2 * DWARF2_ADDR_SIZE;
8228   return size;
8229 }
8230 \f
8231 /* Select the encoding of an attribute value.  */
8232
8233 static enum dwarf_form
8234 value_format (dw_attr_ref a)
8235 {
8236   switch (a->dw_attr_val.val_class)
8237     {
8238     case dw_val_class_addr:
8239       return DW_FORM_addr;
8240     case dw_val_class_range_list:
8241     case dw_val_class_offset:
8242     case dw_val_class_loc_list:
8243       switch (DWARF_OFFSET_SIZE)
8244         {
8245         case 4:
8246           return DW_FORM_data4;
8247         case 8:
8248           return DW_FORM_data8;
8249         default:
8250           gcc_unreachable ();
8251         }
8252     case dw_val_class_loc:
8253       switch (constant_size (size_of_locs (AT_loc (a))))
8254         {
8255         case 1:
8256           return DW_FORM_block1;
8257         case 2:
8258           return DW_FORM_block2;
8259         default:
8260           gcc_unreachable ();
8261         }
8262     case dw_val_class_const:
8263       return DW_FORM_sdata;
8264     case dw_val_class_unsigned_const:
8265       switch (constant_size (AT_unsigned (a)))
8266         {
8267         case 1:
8268           return DW_FORM_data1;
8269         case 2:
8270           return DW_FORM_data2;
8271         case 4:
8272           return DW_FORM_data4;
8273         case 8:
8274           return DW_FORM_data8;
8275         default:
8276           gcc_unreachable ();
8277         }
8278     case dw_val_class_long_long:
8279       return DW_FORM_block1;
8280     case dw_val_class_vec:
8281       switch (constant_size (a->dw_attr_val.v.val_vec.length
8282                              * a->dw_attr_val.v.val_vec.elt_size))
8283         {
8284         case 1:
8285           return DW_FORM_block1;
8286         case 2:
8287           return DW_FORM_block2;
8288         case 4:
8289           return DW_FORM_block4;
8290         default:
8291           gcc_unreachable ();
8292         }
8293     case dw_val_class_flag:
8294       return DW_FORM_flag;
8295     case dw_val_class_die_ref:
8296       if (AT_ref_external (a))
8297         return DW_FORM_ref_addr;
8298       else
8299         return DW_FORM_ref;
8300     case dw_val_class_fde_ref:
8301       return DW_FORM_data;
8302     case dw_val_class_lbl_id:
8303       return DW_FORM_addr;
8304     case dw_val_class_lineptr:
8305     case dw_val_class_macptr:
8306       return DW_FORM_data;
8307     case dw_val_class_str:
8308       return AT_string_form (a);
8309     case dw_val_class_file:
8310       switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
8311         {
8312         case 1:
8313           return DW_FORM_data1;
8314         case 2:
8315           return DW_FORM_data2;
8316         case 4:
8317           return DW_FORM_data4;
8318         default:
8319           gcc_unreachable ();
8320         }
8321
8322     default:
8323       gcc_unreachable ();
8324     }
8325 }
8326
8327 /* Output the encoding of an attribute value.  */
8328
8329 static void
8330 output_value_format (dw_attr_ref a)
8331 {
8332   enum dwarf_form form = value_format (a);
8333
8334   dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
8335 }
8336
8337 /* Output the .debug_abbrev section which defines the DIE abbreviation
8338    table.  */
8339
8340 static void
8341 output_abbrev_section (void)
8342 {
8343   unsigned long abbrev_id;
8344
8345   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8346     {
8347       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
8348       unsigned ix;
8349       dw_attr_ref a_attr;
8350
8351       dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
8352       dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
8353                                    dwarf_tag_name (abbrev->die_tag));
8354
8355       if (abbrev->die_child != NULL)
8356         dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
8357       else
8358         dw2_asm_output_data (1, DW_children_no, "DW_children_no");
8359
8360       for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
8361            ix++)
8362         {
8363           dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
8364                                        dwarf_attr_name (a_attr->dw_attr));
8365           output_value_format (a_attr);
8366         }
8367
8368       dw2_asm_output_data (1, 0, NULL);
8369       dw2_asm_output_data (1, 0, NULL);
8370     }
8371
8372   /* Terminate the table.  */
8373   dw2_asm_output_data (1, 0, NULL);
8374 }
8375
8376 /* Output a symbol we can use to refer to this DIE from another CU.  */
8377
8378 static inline void
8379 output_die_symbol (dw_die_ref die)
8380 {
8381   char *sym = die->die_symbol;
8382
8383   if (sym == 0)
8384     return;
8385
8386   if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
8387     /* We make these global, not weak; if the target doesn't support
8388        .linkonce, it doesn't support combining the sections, so debugging
8389        will break.  */
8390     targetm.asm_out.globalize_label (asm_out_file, sym);
8391
8392   ASM_OUTPUT_LABEL (asm_out_file, sym);
8393 }
8394
8395 /* Return a new location list, given the begin and end range, and the
8396    expression. gensym tells us whether to generate a new internal symbol for
8397    this location list node, which is done for the head of the list only.  */
8398
8399 static inline dw_loc_list_ref
8400 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
8401               const char *section, unsigned int gensym)
8402 {
8403   dw_loc_list_ref retlist = GGC_CNEW (dw_loc_list_node);
8404
8405   retlist->begin = begin;
8406   retlist->end = end;
8407   retlist->expr = expr;
8408   retlist->section = section;
8409   if (gensym)
8410     retlist->ll_symbol = gen_internal_sym ("LLST");
8411
8412   return retlist;
8413 }
8414
8415 /* Add a location description expression to a location list.  */
8416
8417 static inline void
8418 add_loc_descr_to_loc_list (dw_loc_list_ref *list_head, dw_loc_descr_ref descr,
8419                            const char *begin, const char *end,
8420                            const char *section)
8421 {
8422   dw_loc_list_ref *d;
8423
8424   /* Find the end of the chain.  */
8425   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
8426     ;
8427
8428   /* Add a new location list node to the list.  */
8429   *d = new_loc_list (descr, begin, end, section, 0);
8430 }
8431
8432 /* Output the location list given to us.  */
8433
8434 static void
8435 output_loc_list (dw_loc_list_ref list_head)
8436 {
8437   dw_loc_list_ref curr = list_head;
8438
8439   ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
8440
8441   /* Walk the location list, and output each range + expression.  */
8442   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
8443     {
8444       unsigned long size;
8445       /* Don't output an entry that starts and ends at the same address.  */
8446       if (strcmp (curr->begin, curr->end) == 0)
8447         continue;
8448       if (!have_multiple_function_sections)
8449         {
8450           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
8451                                 "Location list begin address (%s)",
8452                                 list_head->ll_symbol);
8453           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
8454                                 "Location list end address (%s)",
8455                                 list_head->ll_symbol);
8456         }
8457       else
8458         {
8459           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
8460                                "Location list begin address (%s)",
8461                                list_head->ll_symbol);
8462           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
8463                                "Location list end address (%s)",
8464                                list_head->ll_symbol);
8465         }
8466       size = size_of_locs (curr->expr);
8467
8468       /* Output the block length for this list of location operations.  */
8469       gcc_assert (size <= 0xffff);
8470       dw2_asm_output_data (2, size, "%s", "Location expression size");
8471
8472       output_loc_sequence (curr->expr);
8473     }
8474
8475   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8476                        "Location list terminator begin (%s)",
8477                        list_head->ll_symbol);
8478   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8479                        "Location list terminator end (%s)",
8480                        list_head->ll_symbol);
8481 }
8482
8483 /* Output the DIE and its attributes.  Called recursively to generate
8484    the definitions of each child DIE.  */
8485
8486 static void
8487 output_die (dw_die_ref die)
8488 {
8489   dw_attr_ref a;
8490   dw_die_ref c;
8491   unsigned long size;
8492   unsigned ix;
8493
8494   /* If someone in another CU might refer to us, set up a symbol for
8495      them to point to.  */
8496   if (die->die_symbol)
8497     output_die_symbol (die);
8498
8499   dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (0x%lx) %s)",
8500                                (unsigned long)die->die_offset,
8501                                dwarf_tag_name (die->die_tag));
8502
8503   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
8504     {
8505       const char *name = dwarf_attr_name (a->dw_attr);
8506
8507       switch (AT_class (a))
8508         {
8509         case dw_val_class_addr:
8510           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8511           break;
8512
8513         case dw_val_class_offset:
8514           dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8515                                "%s", name);
8516           break;
8517
8518         case dw_val_class_range_list:
8519           {
8520             char *p = strchr (ranges_section_label, '\0');
8521
8522             sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
8523                      a->dw_attr_val.v.val_offset);
8524             dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8525                                    debug_ranges_section, "%s", name);
8526             *p = '\0';
8527           }
8528           break;
8529
8530         case dw_val_class_loc:
8531           size = size_of_locs (AT_loc (a));
8532
8533           /* Output the block length for this list of location operations.  */
8534           dw2_asm_output_data (constant_size (size), size, "%s", name);
8535
8536           output_loc_sequence (AT_loc (a));
8537           break;
8538
8539         case dw_val_class_const:
8540           /* ??? It would be slightly more efficient to use a scheme like is
8541              used for unsigned constants below, but gdb 4.x does not sign
8542              extend.  Gdb 5.x does sign extend.  */
8543           dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8544           break;
8545
8546         case dw_val_class_unsigned_const:
8547           dw2_asm_output_data (constant_size (AT_unsigned (a)),
8548                                AT_unsigned (a), "%s", name);
8549           break;
8550
8551         case dw_val_class_long_long:
8552           {
8553             unsigned HOST_WIDE_INT first, second;
8554
8555             dw2_asm_output_data (1,
8556                                  2 * HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
8557                                  "%s", name);
8558
8559             if (WORDS_BIG_ENDIAN)
8560               {
8561                 first = a->dw_attr_val.v.val_long_long.hi;
8562                 second = a->dw_attr_val.v.val_long_long.low;
8563               }
8564             else
8565               {
8566                 first = a->dw_attr_val.v.val_long_long.low;
8567                 second = a->dw_attr_val.v.val_long_long.hi;
8568               }
8569
8570             dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
8571                                  first, "long long constant");
8572             dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
8573                                  second, NULL);
8574           }
8575           break;
8576
8577         case dw_val_class_vec:
8578           {
8579             unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
8580             unsigned int len = a->dw_attr_val.v.val_vec.length;
8581             unsigned int i;
8582             unsigned char *p;
8583
8584             dw2_asm_output_data (constant_size (len * elt_size),
8585                                  len * elt_size, "%s", name);
8586             if (elt_size > sizeof (HOST_WIDE_INT))
8587               {
8588                 elt_size /= 2;
8589                 len *= 2;
8590               }
8591             for (i = 0, p = a->dw_attr_val.v.val_vec.array;
8592                  i < len;
8593                  i++, p += elt_size)
8594               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
8595                                    "fp or vector constant word %u", i);
8596             break;
8597           }
8598
8599         case dw_val_class_flag:
8600           dw2_asm_output_data (1, AT_flag (a), "%s", name);
8601           break;
8602
8603         case dw_val_class_loc_list:
8604           {
8605             char *sym = AT_loc_list (a)->ll_symbol;
8606
8607             gcc_assert (sym);
8608             dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8609                                    "%s", name);
8610           }
8611           break;
8612
8613         case dw_val_class_die_ref:
8614           if (AT_ref_external (a))
8615             {
8616               char *sym = AT_ref (a)->die_symbol;
8617               int size;
8618
8619               gcc_assert (sym);
8620
8621               /* In DWARF2, DW_FORM_ref_addr is sized by target address
8622                  length, whereas in DWARF3 it's always sized as an offset.  */
8623               if (dwarf_version == 2)
8624                 size = DWARF2_ADDR_SIZE;
8625               else
8626                 size = DWARF_OFFSET_SIZE;
8627               dw2_asm_output_offset (size, sym, debug_info_section, "%s", name);
8628             }
8629           else
8630             {
8631               gcc_assert (AT_ref (a)->die_offset);
8632               dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
8633                                    "%s", name);
8634             }
8635           break;
8636
8637         case dw_val_class_fde_ref:
8638           {
8639             char l1[20];
8640
8641             ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
8642                                          a->dw_attr_val.v.val_fde_index * 2);
8643             dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
8644                                    "%s", name);
8645           }
8646           break;
8647
8648         case dw_val_class_lbl_id:
8649           dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8650           break;
8651
8652         case dw_val_class_lineptr:
8653           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8654                                  debug_line_section, "%s", name);
8655           break;
8656
8657         case dw_val_class_macptr:
8658           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8659                                  debug_macinfo_section, "%s", name);
8660           break;
8661
8662         case dw_val_class_str:
8663           if (AT_string_form (a) == DW_FORM_strp)
8664             dw2_asm_output_offset (DWARF_OFFSET_SIZE,
8665                                    a->dw_attr_val.v.val_str->label,
8666                                    debug_str_section,
8667                                    "%s: \"%s\"", name, AT_string (a));
8668           else
8669             dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
8670           break;
8671
8672         case dw_val_class_file:
8673           {
8674             int f = maybe_emit_file (a->dw_attr_val.v.val_file);
8675
8676             dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
8677                                  a->dw_attr_val.v.val_file->filename);
8678             break;
8679           }
8680
8681         default:
8682           gcc_unreachable ();
8683         }
8684     }
8685
8686   FOR_EACH_CHILD (die, c, output_die (c));
8687
8688   /* Add null byte to terminate sibling list.  */
8689   if (die->die_child != NULL)
8690     dw2_asm_output_data (1, 0, "end of children of DIE 0x%lx",
8691                          (unsigned long) die->die_offset);
8692 }
8693
8694 /* Output the compilation unit that appears at the beginning of the
8695    .debug_info section, and precedes the DIE descriptions.  */
8696
8697 static void
8698 output_compilation_unit_header (void)
8699 {
8700   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8701     dw2_asm_output_data (4, 0xffffffff,
8702       "Initial length escape value indicating 64-bit DWARF extension");
8703   dw2_asm_output_data (DWARF_OFFSET_SIZE,
8704                        next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
8705                        "Length of Compilation Unit Info");
8706   dw2_asm_output_data (2, dwarf_version, "DWARF version number");
8707   dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
8708                          debug_abbrev_section,
8709                          "Offset Into Abbrev. Section");
8710   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8711 }
8712
8713 /* Output the compilation unit DIE and its children.  */
8714
8715 static void
8716 output_comp_unit (dw_die_ref die, int output_if_empty)
8717 {
8718   const char *secname;
8719   char *oldsym, *tmp;
8720
8721   /* Unless we are outputting main CU, we may throw away empty ones.  */
8722   if (!output_if_empty && die->die_child == NULL)
8723     return;
8724
8725   /* Even if there are no children of this DIE, we must output the information
8726      about the compilation unit.  Otherwise, on an empty translation unit, we
8727      will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
8728      will then complain when examining the file.  First mark all the DIEs in
8729      this CU so we know which get local refs.  */
8730   mark_dies (die);
8731
8732   build_abbrev_table (die);
8733
8734   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
8735   next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8736   calc_die_sizes (die);
8737
8738   oldsym = die->die_symbol;
8739   if (oldsym)
8740     {
8741       tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
8742
8743       sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
8744       secname = tmp;
8745       die->die_symbol = NULL;
8746       switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
8747     }
8748   else
8749     switch_to_section (debug_info_section);
8750
8751   /* Output debugging information.  */
8752   output_compilation_unit_header ();
8753   output_die (die);
8754
8755   /* Leave the marks on the main CU, so we can check them in
8756      output_pubnames.  */
8757   if (oldsym)
8758     {
8759       unmark_dies (die);
8760       die->die_symbol = oldsym;
8761     }
8762 }
8763
8764 /* Return the DWARF2/3 pubname associated with a decl.  */
8765
8766 static const char *
8767 dwarf2_name (tree decl, int scope)
8768 {
8769   return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
8770 }
8771
8772 /* Add a new entry to .debug_pubnames if appropriate.  */
8773
8774 static void
8775 add_pubname_string (const char *str, dw_die_ref die)
8776 {
8777   pubname_entry e;
8778
8779   e.die = die;
8780   e.name = xstrdup (str);
8781   VEC_safe_push (pubname_entry, gc, pubname_table, &e);
8782 }
8783
8784 static void
8785 add_pubname (tree decl, dw_die_ref die)
8786 {
8787   if (TREE_PUBLIC (decl))
8788     add_pubname_string (dwarf2_name (decl, 1), die);
8789 }
8790
8791 /* Add a new entry to .debug_pubtypes if appropriate.  */
8792
8793 static void
8794 add_pubtype (tree decl, dw_die_ref die)
8795 {
8796   pubname_entry e;
8797
8798   e.name = NULL;
8799   if ((TREE_PUBLIC (decl)
8800        || die->die_parent == comp_unit_die)
8801       && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
8802     {
8803       e.die = die;
8804       if (TYPE_P (decl))
8805         {
8806           if (TYPE_NAME (decl))
8807             {
8808               if (TREE_CODE (TYPE_NAME (decl)) == IDENTIFIER_NODE)
8809                 e.name = IDENTIFIER_POINTER (TYPE_NAME (decl));
8810               else if (TREE_CODE (TYPE_NAME (decl)) == TYPE_DECL
8811                        && DECL_NAME (TYPE_NAME (decl)))
8812                 e.name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (decl)));
8813               else
8814                e.name = xstrdup ((const char *) get_AT_string (die, DW_AT_name));
8815             }
8816         }
8817       else
8818         e.name = xstrdup (dwarf2_name (decl, 1));
8819
8820       /* If we don't have a name for the type, there's no point in adding
8821          it to the table.  */
8822       if (e.name && e.name[0] != '\0')
8823         VEC_safe_push (pubname_entry, gc, pubtype_table, &e);
8824     }
8825 }
8826
8827 /* Output the public names table used to speed up access to externally
8828    visible names; or the public types table used to find type definitions.  */
8829
8830 static void
8831 output_pubnames (VEC (pubname_entry, gc) * names)
8832 {
8833   unsigned i;
8834   unsigned long pubnames_length = size_of_pubnames (names);
8835   pubname_ref pub;
8836
8837   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8838     dw2_asm_output_data (4, 0xffffffff,
8839       "Initial length escape value indicating 64-bit DWARF extension");
8840   if (names == pubname_table)
8841     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
8842                          "Length of Public Names Info");
8843   else
8844     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
8845                          "Length of Public Type Names Info");
8846   /* Version number for pubnames/pubtypes is still 2, even in DWARF3.  */
8847   dw2_asm_output_data (2, 2, "DWARF Version");
8848   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
8849                          debug_info_section,
8850                          "Offset of Compilation Unit Info");
8851   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
8852                        "Compilation Unit Length");
8853
8854   for (i = 0; VEC_iterate (pubname_entry, names, i, pub); i++)
8855     {
8856       /* We shouldn't see pubnames for DIEs outside of the main CU.  */
8857       if (names == pubname_table)
8858         gcc_assert (pub->die->die_mark);
8859
8860       if (names != pubtype_table
8861           || pub->die->die_offset != 0
8862           || !flag_eliminate_unused_debug_types)
8863         {
8864           dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
8865                                "DIE offset");
8866
8867           dw2_asm_output_nstring (pub->name, -1, "external name");
8868         }
8869     }
8870
8871   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
8872 }
8873
8874 /* Add a new entry to .debug_aranges if appropriate.  */
8875
8876 static void
8877 add_arange (tree decl, dw_die_ref die)
8878 {
8879   if (! DECL_SECTION_NAME (decl))
8880     return;
8881
8882   if (arange_table_in_use == arange_table_allocated)
8883     {
8884       arange_table_allocated += ARANGE_TABLE_INCREMENT;
8885       arange_table = GGC_RESIZEVEC (dw_die_ref, arange_table,
8886                                     arange_table_allocated);
8887       memset (arange_table + arange_table_in_use, 0,
8888               ARANGE_TABLE_INCREMENT * sizeof (dw_die_ref));
8889     }
8890
8891   arange_table[arange_table_in_use++] = die;
8892 }
8893
8894 /* Output the information that goes into the .debug_aranges table.
8895    Namely, define the beginning and ending address range of the
8896    text section generated for this compilation unit.  */
8897
8898 static void
8899 output_aranges (void)
8900 {
8901   unsigned i;
8902   unsigned long aranges_length = size_of_aranges ();
8903
8904   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8905     dw2_asm_output_data (4, 0xffffffff,
8906       "Initial length escape value indicating 64-bit DWARF extension");
8907   dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
8908                        "Length of Address Ranges Info");
8909   /* Version number for aranges is still 2, even in DWARF3.  */
8910   dw2_asm_output_data (2, 2, "DWARF Version");
8911   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
8912                          debug_info_section,
8913                          "Offset of Compilation Unit Info");
8914   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
8915   dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
8916
8917   /* We need to align to twice the pointer size here.  */
8918   if (DWARF_ARANGES_PAD_SIZE)
8919     {
8920       /* Pad using a 2 byte words so that padding is correct for any
8921          pointer size.  */
8922       dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
8923                            2 * DWARF2_ADDR_SIZE);
8924       for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
8925         dw2_asm_output_data (2, 0, NULL);
8926     }
8927
8928   /* It is necessary not to output these entries if the sections were
8929      not used; if the sections were not used, the length will be 0 and
8930      the address may end up as 0 if the section is discarded by ld
8931      --gc-sections, leaving an invalid (0, 0) entry that can be
8932      confused with the terminator.  */
8933   if (text_section_used)
8934     {
8935       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
8936       dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
8937                             text_section_label, "Length");
8938     }
8939   if (cold_text_section_used)
8940     {
8941       dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
8942                            "Address");
8943       dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
8944                             cold_text_section_label, "Length");
8945     }
8946
8947   for (i = 0; i < arange_table_in_use; i++)
8948     {
8949       dw_die_ref die = arange_table[i];
8950
8951       /* We shouldn't see aranges for DIEs outside of the main CU.  */
8952       gcc_assert (die->die_mark);
8953
8954       if (die->die_tag == DW_TAG_subprogram)
8955         {
8956           dw2_asm_output_addr (DWARF2_ADDR_SIZE, get_AT_low_pc (die),
8957                                "Address");
8958           dw2_asm_output_delta (DWARF2_ADDR_SIZE, get_AT_hi_pc (die),
8959                                 get_AT_low_pc (die), "Length");
8960         }
8961       else
8962         {
8963           /* A static variable; extract the symbol from DW_AT_location.
8964              Note that this code isn't currently hit, as we only emit
8965              aranges for functions (jason 9/23/99).  */
8966           dw_attr_ref a = get_AT (die, DW_AT_location);
8967           dw_loc_descr_ref loc;
8968
8969           gcc_assert (a && AT_class (a) == dw_val_class_loc);
8970
8971           loc = AT_loc (a);
8972           gcc_assert (loc->dw_loc_opc == DW_OP_addr);
8973
8974           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE,
8975                                    loc->dw_loc_oprnd1.v.val_addr, "Address");
8976           dw2_asm_output_data (DWARF2_ADDR_SIZE,
8977                                get_AT_unsigned (die, DW_AT_byte_size),
8978                                "Length");
8979         }
8980     }
8981
8982   /* Output the terminator words.  */
8983   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8984   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8985 }
8986
8987 /* Add a new entry to .debug_ranges.  Return the offset at which it
8988    was placed.  */
8989
8990 static unsigned int
8991 add_ranges_num (int num)
8992 {
8993   unsigned int in_use = ranges_table_in_use;
8994
8995   if (in_use == ranges_table_allocated)
8996     {
8997       ranges_table_allocated += RANGES_TABLE_INCREMENT;
8998       ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
8999                                     ranges_table_allocated);
9000       memset (ranges_table + ranges_table_in_use, 0,
9001               RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
9002     }
9003
9004   ranges_table[in_use].num = num;
9005   ranges_table_in_use = in_use + 1;
9006
9007   return in_use * 2 * DWARF2_ADDR_SIZE;
9008 }
9009
9010 /* Add a new entry to .debug_ranges corresponding to a block, or a
9011    range terminator if BLOCK is NULL.  */
9012
9013 static unsigned int
9014 add_ranges (const_tree block)
9015 {
9016   return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
9017 }
9018
9019 /* Add a new entry to .debug_ranges corresponding to a pair of
9020    labels.  */
9021
9022 static unsigned int
9023 add_ranges_by_labels (const char *begin, const char *end)
9024 {
9025   unsigned int in_use = ranges_by_label_in_use;
9026
9027   if (in_use == ranges_by_label_allocated)
9028     {
9029       ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
9030       ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
9031                                        ranges_by_label,
9032                                        ranges_by_label_allocated);
9033       memset (ranges_by_label + ranges_by_label_in_use, 0,
9034               RANGES_TABLE_INCREMENT
9035               * sizeof (struct dw_ranges_by_label_struct));
9036     }
9037
9038   ranges_by_label[in_use].begin = begin;
9039   ranges_by_label[in_use].end = end;
9040   ranges_by_label_in_use = in_use + 1;
9041
9042   return add_ranges_num (-(int)in_use - 1);
9043 }
9044
9045 static void
9046 output_ranges (void)
9047 {
9048   unsigned i;
9049   static const char *const start_fmt = "Offset 0x%x";
9050   const char *fmt = start_fmt;
9051
9052   for (i = 0; i < ranges_table_in_use; i++)
9053     {
9054       int block_num = ranges_table[i].num;
9055
9056       if (block_num > 0)
9057         {
9058           char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
9059           char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
9060
9061           ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
9062           ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
9063
9064           /* If all code is in the text section, then the compilation
9065              unit base address defaults to DW_AT_low_pc, which is the
9066              base of the text section.  */
9067           if (!have_multiple_function_sections)
9068             {
9069               dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
9070                                     text_section_label,
9071                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
9072               dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
9073                                     text_section_label, NULL);
9074             }
9075
9076           /* Otherwise, the compilation unit base address is zero,
9077              which allows us to use absolute addresses, and not worry
9078              about whether the target supports cross-section
9079              arithmetic.  */
9080           else
9081             {
9082               dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
9083                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
9084               dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
9085             }
9086
9087           fmt = NULL;
9088         }
9089
9090       /* Negative block_num stands for an index into ranges_by_label.  */
9091       else if (block_num < 0)
9092         {
9093           int lab_idx = - block_num - 1;
9094
9095           if (!have_multiple_function_sections)
9096             {
9097               gcc_unreachable ();
9098 #if 0
9099               /* If we ever use add_ranges_by_labels () for a single
9100                  function section, all we have to do is to take out
9101                  the #if 0 above.  */
9102               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9103                                     ranges_by_label[lab_idx].begin,
9104                                     text_section_label,
9105                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
9106               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9107                                     ranges_by_label[lab_idx].end,
9108                                     text_section_label, NULL);
9109 #endif
9110             }
9111           else
9112             {
9113               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9114                                    ranges_by_label[lab_idx].begin,
9115                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
9116               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9117                                    ranges_by_label[lab_idx].end,
9118                                    NULL);
9119             }
9120         }
9121       else
9122         {
9123           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9124           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9125           fmt = start_fmt;
9126         }
9127     }
9128 }
9129
9130 /* Data structure containing information about input files.  */
9131 struct file_info
9132 {
9133   const char *path;     /* Complete file name.  */
9134   const char *fname;    /* File name part.  */
9135   int length;           /* Length of entire string.  */
9136   struct dwarf_file_data * file_idx;    /* Index in input file table.  */
9137   int dir_idx;          /* Index in directory table.  */
9138 };
9139
9140 /* Data structure containing information about directories with source
9141    files.  */
9142 struct dir_info
9143 {
9144   const char *path;     /* Path including directory name.  */
9145   int length;           /* Path length.  */
9146   int prefix;           /* Index of directory entry which is a prefix.  */
9147   int count;            /* Number of files in this directory.  */
9148   int dir_idx;          /* Index of directory used as base.  */
9149 };
9150
9151 /* Callback function for file_info comparison.  We sort by looking at
9152    the directories in the path.  */
9153
9154 static int
9155 file_info_cmp (const void *p1, const void *p2)
9156 {
9157   const struct file_info *const s1 = (const struct file_info *) p1;
9158   const struct file_info *const s2 = (const struct file_info *) p2;
9159   const unsigned char *cp1;
9160   const unsigned char *cp2;
9161
9162   /* Take care of file names without directories.  We need to make sure that
9163      we return consistent values to qsort since some will get confused if
9164      we return the same value when identical operands are passed in opposite
9165      orders.  So if neither has a directory, return 0 and otherwise return
9166      1 or -1 depending on which one has the directory.  */
9167   if ((s1->path == s1->fname || s2->path == s2->fname))
9168     return (s2->path == s2->fname) - (s1->path == s1->fname);
9169
9170   cp1 = (const unsigned char *) s1->path;
9171   cp2 = (const unsigned char *) s2->path;
9172
9173   while (1)
9174     {
9175       ++cp1;
9176       ++cp2;
9177       /* Reached the end of the first path?  If so, handle like above.  */
9178       if ((cp1 == (const unsigned char *) s1->fname)
9179           || (cp2 == (const unsigned char *) s2->fname))
9180         return ((cp2 == (const unsigned char *) s2->fname)
9181                 - (cp1 == (const unsigned char *) s1->fname));
9182
9183       /* Character of current path component the same?  */
9184       else if (*cp1 != *cp2)
9185         return *cp1 - *cp2;
9186     }
9187 }
9188
9189 struct file_name_acquire_data
9190 {
9191   struct file_info *files;
9192   int used_files;
9193   int max_files;
9194 };
9195
9196 /* Traversal function for the hash table.  */
9197
9198 static int
9199 file_name_acquire (void ** slot, void *data)
9200 {
9201   struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
9202   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
9203   struct file_info *fi;
9204   const char *f;
9205
9206   gcc_assert (fnad->max_files >= d->emitted_number);
9207
9208   if (! d->emitted_number)
9209     return 1;
9210
9211   gcc_assert (fnad->max_files != fnad->used_files);
9212
9213   fi = fnad->files + fnad->used_files++;
9214
9215   /* Skip all leading "./".  */
9216   f = d->filename;
9217   while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
9218     f += 2;
9219
9220   /* Create a new array entry.  */
9221   fi->path = f;
9222   fi->length = strlen (f);
9223   fi->file_idx = d;
9224
9225   /* Search for the file name part.  */
9226   f = strrchr (f, DIR_SEPARATOR);
9227 #if defined (DIR_SEPARATOR_2)
9228   {
9229     char *g = strrchr (fi->path, DIR_SEPARATOR_2);
9230
9231     if (g != NULL)
9232       {
9233         if (f == NULL || f < g)
9234           f = g;
9235       }
9236   }
9237 #endif
9238
9239   fi->fname = f == NULL ? fi->path : f + 1;
9240   return 1;
9241 }
9242
9243 /* Output the directory table and the file name table.  We try to minimize
9244    the total amount of memory needed.  A heuristic is used to avoid large
9245    slowdowns with many input files.  */
9246
9247 static void
9248 output_file_names (void)
9249 {
9250   struct file_name_acquire_data fnad;
9251   int numfiles;
9252   struct file_info *files;
9253   struct dir_info *dirs;
9254   int *saved;
9255   int *savehere;
9256   int *backmap;
9257   int ndirs;
9258   int idx_offset;
9259   int i;
9260   int idx;
9261
9262   if (!last_emitted_file)
9263     {
9264       dw2_asm_output_data (1, 0, "End directory table");
9265       dw2_asm_output_data (1, 0, "End file name table");
9266       return;
9267     }
9268
9269   numfiles = last_emitted_file->emitted_number;
9270
9271   /* Allocate the various arrays we need.  */
9272   files = XALLOCAVEC (struct file_info, numfiles);
9273   dirs = XALLOCAVEC (struct dir_info, numfiles);
9274
9275   fnad.files = files;
9276   fnad.used_files = 0;
9277   fnad.max_files = numfiles;
9278   htab_traverse (file_table, file_name_acquire, &fnad);
9279   gcc_assert (fnad.used_files == fnad.max_files);
9280
9281   qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
9282
9283   /* Find all the different directories used.  */
9284   dirs[0].path = files[0].path;
9285   dirs[0].length = files[0].fname - files[0].path;
9286   dirs[0].prefix = -1;
9287   dirs[0].count = 1;
9288   dirs[0].dir_idx = 0;
9289   files[0].dir_idx = 0;
9290   ndirs = 1;
9291
9292   for (i = 1; i < numfiles; i++)
9293     if (files[i].fname - files[i].path == dirs[ndirs - 1].length
9294         && memcmp (dirs[ndirs - 1].path, files[i].path,
9295                    dirs[ndirs - 1].length) == 0)
9296       {
9297         /* Same directory as last entry.  */
9298         files[i].dir_idx = ndirs - 1;
9299         ++dirs[ndirs - 1].count;
9300       }
9301     else
9302       {
9303         int j;
9304
9305         /* This is a new directory.  */
9306         dirs[ndirs].path = files[i].path;
9307         dirs[ndirs].length = files[i].fname - files[i].path;
9308         dirs[ndirs].count = 1;
9309         dirs[ndirs].dir_idx = ndirs;
9310         files[i].dir_idx = ndirs;
9311
9312         /* Search for a prefix.  */
9313         dirs[ndirs].prefix = -1;
9314         for (j = 0; j < ndirs; j++)
9315           if (dirs[j].length < dirs[ndirs].length
9316               && dirs[j].length > 1
9317               && (dirs[ndirs].prefix == -1
9318                   || dirs[j].length > dirs[dirs[ndirs].prefix].length)
9319               && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
9320             dirs[ndirs].prefix = j;
9321
9322         ++ndirs;
9323       }
9324
9325   /* Now to the actual work.  We have to find a subset of the directories which
9326      allow expressing the file name using references to the directory table
9327      with the least amount of characters.  We do not do an exhaustive search
9328      where we would have to check out every combination of every single
9329      possible prefix.  Instead we use a heuristic which provides nearly optimal
9330      results in most cases and never is much off.  */
9331   saved = XALLOCAVEC (int, ndirs);
9332   savehere = XALLOCAVEC (int, ndirs);
9333
9334   memset (saved, '\0', ndirs * sizeof (saved[0]));
9335   for (i = 0; i < ndirs; i++)
9336     {
9337       int j;
9338       int total;
9339
9340       /* We can always save some space for the current directory.  But this
9341          does not mean it will be enough to justify adding the directory.  */
9342       savehere[i] = dirs[i].length;
9343       total = (savehere[i] - saved[i]) * dirs[i].count;
9344
9345       for (j = i + 1; j < ndirs; j++)
9346         {
9347           savehere[j] = 0;
9348           if (saved[j] < dirs[i].length)
9349             {
9350               /* Determine whether the dirs[i] path is a prefix of the
9351                  dirs[j] path.  */
9352               int k;
9353
9354               k = dirs[j].prefix;
9355               while (k != -1 && k != (int) i)
9356                 k = dirs[k].prefix;
9357
9358               if (k == (int) i)
9359                 {
9360                   /* Yes it is.  We can possibly save some memory by
9361                      writing the filenames in dirs[j] relative to
9362                      dirs[i].  */
9363                   savehere[j] = dirs[i].length;
9364                   total += (savehere[j] - saved[j]) * dirs[j].count;
9365                 }
9366             }
9367         }
9368
9369       /* Check whether we can save enough to justify adding the dirs[i]
9370          directory.  */
9371       if (total > dirs[i].length + 1)
9372         {
9373           /* It's worthwhile adding.  */
9374           for (j = i; j < ndirs; j++)
9375             if (savehere[j] > 0)
9376               {
9377                 /* Remember how much we saved for this directory so far.  */
9378                 saved[j] = savehere[j];
9379
9380                 /* Remember the prefix directory.  */
9381                 dirs[j].dir_idx = i;
9382               }
9383         }
9384     }
9385
9386   /* Emit the directory name table.  */
9387   idx = 1;
9388   idx_offset = dirs[0].length > 0 ? 1 : 0;
9389   for (i = 1 - idx_offset; i < ndirs; i++)
9390     dw2_asm_output_nstring (dirs[i].path, dirs[i].length - 1,
9391                             "Directory Entry: 0x%x", i + idx_offset);
9392
9393   dw2_asm_output_data (1, 0, "End directory table");
9394
9395   /* We have to emit them in the order of emitted_number since that's
9396      used in the debug info generation.  To do this efficiently we
9397      generate a back-mapping of the indices first.  */
9398   backmap = XALLOCAVEC (int, numfiles);
9399   for (i = 0; i < numfiles; i++)
9400     backmap[files[i].file_idx->emitted_number - 1] = i;
9401
9402   /* Now write all the file names.  */
9403   for (i = 0; i < numfiles; i++)
9404     {
9405       int file_idx = backmap[i];
9406       int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
9407
9408       dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
9409                               "File Entry: 0x%x", (unsigned) i + 1);
9410
9411       /* Include directory index.  */
9412       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9413
9414       /* Modification time.  */
9415       dw2_asm_output_data_uleb128 (0, NULL);
9416
9417       /* File length in bytes.  */
9418       dw2_asm_output_data_uleb128 (0, NULL);
9419     }
9420
9421   dw2_asm_output_data (1, 0, "End file name table");
9422 }
9423
9424
9425 /* Output the source line number correspondence information.  This
9426    information goes into the .debug_line section.  */
9427
9428 static void
9429 output_line_info (void)
9430 {
9431   char l1[20], l2[20], p1[20], p2[20];
9432   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
9433   char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
9434   unsigned opc;
9435   unsigned n_op_args;
9436   unsigned long lt_index;
9437   unsigned long current_line;
9438   long line_offset;
9439   long line_delta;
9440   unsigned long current_file;
9441   unsigned long function;
9442
9443   ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
9444   ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
9445   ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
9446   ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
9447
9448   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9449     dw2_asm_output_data (4, 0xffffffff,
9450       "Initial length escape value indicating 64-bit DWARF extension");
9451   dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
9452                         "Length of Source Line Info");
9453   ASM_OUTPUT_LABEL (asm_out_file, l1);
9454
9455   dw2_asm_output_data (2, dwarf_version, "DWARF Version");
9456   dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
9457   ASM_OUTPUT_LABEL (asm_out_file, p1);
9458
9459   /* Define the architecture-dependent minimum instruction length (in
9460    bytes).  In this implementation of DWARF, this field is used for
9461    information purposes only.  Since GCC generates assembly language,
9462    we have no a priori knowledge of how many instruction bytes are
9463    generated for each source line, and therefore can use only the
9464    DW_LNE_set_address and DW_LNS_fixed_advance_pc line information
9465    commands.  Accordingly, we fix this as `1', which is "correct
9466    enough" for all architectures, and don't let the target override.  */
9467   dw2_asm_output_data (1, 1,
9468                        "Minimum Instruction Length");
9469
9470   dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
9471                        "Default is_stmt_start flag");
9472   dw2_asm_output_data (1, DWARF_LINE_BASE,
9473                        "Line Base Value (Special Opcodes)");
9474   dw2_asm_output_data (1, DWARF_LINE_RANGE,
9475                        "Line Range Value (Special Opcodes)");
9476   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
9477                        "Special Opcode Base");
9478
9479   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
9480     {
9481       switch (opc)
9482         {
9483         case DW_LNS_advance_pc:
9484         case DW_LNS_advance_line:
9485         case DW_LNS_set_file:
9486         case DW_LNS_set_column:
9487         case DW_LNS_fixed_advance_pc:
9488           n_op_args = 1;
9489           break;
9490         default:
9491           n_op_args = 0;
9492           break;
9493         }
9494
9495       dw2_asm_output_data (1, n_op_args, "opcode: 0x%x has %d args",
9496                            opc, n_op_args);
9497     }
9498
9499   /* Write out the information about the files we use.  */
9500   output_file_names ();
9501   ASM_OUTPUT_LABEL (asm_out_file, p2);
9502
9503   /* We used to set the address register to the first location in the text
9504      section here, but that didn't accomplish anything since we already
9505      have a line note for the opening brace of the first function.  */
9506
9507   /* Generate the line number to PC correspondence table, encoded as
9508      a series of state machine operations.  */
9509   current_file = 1;
9510   current_line = 1;
9511
9512   if (cfun && in_cold_section_p)
9513     strcpy (prev_line_label, crtl->subsections.cold_section_label);
9514   else
9515     strcpy (prev_line_label, text_section_label);
9516   for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
9517     {
9518       dw_line_info_ref line_info = &line_info_table[lt_index];
9519
9520 #if 0
9521       /* Disable this optimization for now; GDB wants to see two line notes
9522          at the beginning of a function so it can find the end of the
9523          prologue.  */
9524
9525       /* Don't emit anything for redundant notes.  Just updating the
9526          address doesn't accomplish anything, because we already assume
9527          that anything after the last address is this line.  */
9528       if (line_info->dw_line_num == current_line
9529           && line_info->dw_file_num == current_file)
9530         continue;
9531 #endif
9532
9533       /* Emit debug info for the address of the current line.
9534
9535          Unfortunately, we have little choice here currently, and must always
9536          use the most general form.  GCC does not know the address delta
9537          itself, so we can't use DW_LNS_advance_pc.  Many ports do have length
9538          attributes which will give an upper bound on the address range.  We
9539          could perhaps use length attributes to determine when it is safe to
9540          use DW_LNS_fixed_advance_pc.  */
9541
9542       ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
9543       if (0)
9544         {
9545           /* This can handle deltas up to 0xffff.  This takes 3 bytes.  */
9546           dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
9547                                "DW_LNS_fixed_advance_pc");
9548           dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
9549         }
9550       else
9551         {
9552           /* This can handle any delta.  This takes
9553              4+DWARF2_ADDR_SIZE bytes.  */
9554           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9555           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9556           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9557           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9558         }
9559
9560       strcpy (prev_line_label, line_label);
9561
9562       /* Emit debug info for the source file of the current line, if
9563          different from the previous line.  */
9564       if (line_info->dw_file_num != current_file)
9565         {
9566           current_file = line_info->dw_file_num;
9567           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
9568           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
9569         }
9570
9571       /* Emit debug info for the current line number, choosing the encoding
9572          that uses the least amount of space.  */
9573       if (line_info->dw_line_num != current_line)
9574         {
9575           line_offset = line_info->dw_line_num - current_line;
9576           line_delta = line_offset - DWARF_LINE_BASE;
9577           current_line = line_info->dw_line_num;
9578           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
9579             /* This can handle deltas from -10 to 234, using the current
9580                definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.  This
9581                takes 1 byte.  */
9582             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
9583                                  "line %lu", current_line);
9584           else
9585             {
9586               /* This can handle any delta.  This takes at least 4 bytes,
9587                  depending on the value being encoded.  */
9588               dw2_asm_output_data (1, DW_LNS_advance_line,
9589                                    "advance to line %lu", current_line);
9590               dw2_asm_output_data_sleb128 (line_offset, NULL);
9591               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
9592             }
9593         }
9594       else
9595         /* We still need to start a new row, so output a copy insn.  */
9596         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
9597     }
9598
9599   /* Emit debug info for the address of the end of the function.  */
9600   if (0)
9601     {
9602       dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
9603                            "DW_LNS_fixed_advance_pc");
9604       dw2_asm_output_delta (2, text_end_label, prev_line_label, NULL);
9605     }
9606   else
9607     {
9608       dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9609       dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9610       dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9611       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_end_label, NULL);
9612     }
9613
9614   dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
9615   dw2_asm_output_data_uleb128 (1, NULL);
9616   dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
9617
9618   function = 0;
9619   current_file = 1;
9620   current_line = 1;
9621   for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
9622     {
9623       dw_separate_line_info_ref line_info
9624         = &separate_line_info_table[lt_index];
9625
9626 #if 0
9627       /* Don't emit anything for redundant notes.  */
9628       if (line_info->dw_line_num == current_line
9629           && line_info->dw_file_num == current_file
9630           && line_info->function == function)
9631         goto cont;
9632 #endif
9633
9634       /* Emit debug info for the address of the current line.  If this is
9635          a new function, or the first line of a function, then we need
9636          to handle it differently.  */
9637       ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
9638                                    lt_index);
9639       if (function != line_info->function)
9640         {
9641           function = line_info->function;
9642
9643           /* Set the address register to the first line in the function.  */
9644           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9645           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9646           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9647           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9648         }
9649       else
9650         {
9651           /* ??? See the DW_LNS_advance_pc comment above.  */
9652           if (0)
9653             {
9654               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
9655                                    "DW_LNS_fixed_advance_pc");
9656               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
9657             }
9658           else
9659             {
9660               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9661               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9662               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9663               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9664             }
9665         }
9666
9667       strcpy (prev_line_label, line_label);
9668
9669       /* Emit debug info for the source file of the current line, if
9670          different from the previous line.  */
9671       if (line_info->dw_file_num != current_file)
9672         {
9673           current_file = line_info->dw_file_num;
9674           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
9675           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
9676         }
9677
9678       /* Emit debug info for the current line number, choosing the encoding
9679          that uses the least amount of space.  */
9680       if (line_info->dw_line_num != current_line)
9681         {
9682           line_offset = line_info->dw_line_num - current_line;
9683           line_delta = line_offset - DWARF_LINE_BASE;
9684           current_line = line_info->dw_line_num;
9685           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
9686             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
9687                                  "line %lu", current_line);
9688           else
9689             {
9690               dw2_asm_output_data (1, DW_LNS_advance_line,
9691                                    "advance to line %lu", current_line);
9692               dw2_asm_output_data_sleb128 (line_offset, NULL);
9693               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
9694             }
9695         }
9696       else
9697         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
9698
9699 #if 0
9700     cont:
9701 #endif
9702
9703       lt_index++;
9704
9705       /* If we're done with a function, end its sequence.  */
9706       if (lt_index == separate_line_info_table_in_use
9707           || separate_line_info_table[lt_index].function != function)
9708         {
9709           current_file = 1;
9710           current_line = 1;
9711
9712           /* Emit debug info for the address of the end of the function.  */
9713           ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
9714           if (0)
9715             {
9716               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
9717                                    "DW_LNS_fixed_advance_pc");
9718               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
9719             }
9720           else
9721             {
9722               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9723               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9724               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9725               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9726             }
9727
9728           /* Output the marker for the end of this sequence.  */
9729           dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
9730           dw2_asm_output_data_uleb128 (1, NULL);
9731           dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
9732         }
9733     }
9734
9735   /* Output the marker for the end of the line number info.  */
9736   ASM_OUTPUT_LABEL (asm_out_file, l2);
9737 }
9738 \f
9739 /* Given a pointer to a tree node for some base type, return a pointer to
9740    a DIE that describes the given type.
9741
9742    This routine must only be called for GCC type nodes that correspond to
9743    Dwarf base (fundamental) types.  */
9744
9745 static dw_die_ref
9746 base_type_die (tree type)
9747 {
9748   dw_die_ref base_type_result;
9749   enum dwarf_type encoding;
9750
9751   if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
9752     return 0;
9753
9754   /* If this is a subtype that should not be emitted as a subrange type,
9755      use the base type.  See subrange_type_for_debug_p.  */
9756   if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
9757     type = TREE_TYPE (type);
9758
9759   switch (TREE_CODE (type))
9760     {
9761     case INTEGER_TYPE:
9762       if (TYPE_STRING_FLAG (type))
9763         {
9764           if (TYPE_UNSIGNED (type))
9765             encoding = DW_ATE_unsigned_char;
9766           else
9767             encoding = DW_ATE_signed_char;
9768         }
9769       else if (TYPE_UNSIGNED (type))
9770         encoding = DW_ATE_unsigned;
9771       else
9772         encoding = DW_ATE_signed;
9773       break;
9774
9775     case REAL_TYPE:
9776       if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
9777         encoding = DW_ATE_decimal_float;
9778       else
9779         encoding = DW_ATE_float;
9780       break;
9781
9782     case FIXED_POINT_TYPE:
9783       if (TYPE_UNSIGNED (type))
9784         encoding = DW_ATE_unsigned_fixed;
9785       else
9786         encoding = DW_ATE_signed_fixed;
9787       break;
9788
9789       /* Dwarf2 doesn't know anything about complex ints, so use
9790          a user defined type for it.  */
9791     case COMPLEX_TYPE:
9792       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
9793         encoding = DW_ATE_complex_float;
9794       else
9795         encoding = DW_ATE_lo_user;
9796       break;
9797
9798     case BOOLEAN_TYPE:
9799       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
9800       encoding = DW_ATE_boolean;
9801       break;
9802
9803     default:
9804       /* No other TREE_CODEs are Dwarf fundamental types.  */
9805       gcc_unreachable ();
9806     }
9807
9808   base_type_result = new_die (DW_TAG_base_type, comp_unit_die, type);
9809
9810   /* This probably indicates a bug.  */
9811   if (! TYPE_NAME (type))
9812     add_name_attribute (base_type_result, "__unknown__");
9813
9814   add_AT_unsigned (base_type_result, DW_AT_byte_size,
9815                    int_size_in_bytes (type));
9816   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
9817
9818   return base_type_result;
9819 }
9820
9821 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
9822    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
9823
9824 static inline int
9825 is_base_type (tree type)
9826 {
9827   switch (TREE_CODE (type))
9828     {
9829     case ERROR_MARK:
9830     case VOID_TYPE:
9831     case INTEGER_TYPE:
9832     case REAL_TYPE:
9833     case FIXED_POINT_TYPE:
9834     case COMPLEX_TYPE:
9835     case BOOLEAN_TYPE:
9836       return 1;
9837
9838     case ARRAY_TYPE:
9839     case RECORD_TYPE:
9840     case UNION_TYPE:
9841     case QUAL_UNION_TYPE:
9842     case ENUMERAL_TYPE:
9843     case FUNCTION_TYPE:
9844     case METHOD_TYPE:
9845     case POINTER_TYPE:
9846     case REFERENCE_TYPE:
9847     case OFFSET_TYPE:
9848     case LANG_TYPE:
9849     case VECTOR_TYPE:
9850       return 0;
9851
9852     default:
9853       gcc_unreachable ();
9854     }
9855
9856   return 0;
9857 }
9858
9859 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
9860    node, return the size in bits for the type if it is a constant, or else
9861    return the alignment for the type if the type's size is not constant, or
9862    else return BITS_PER_WORD if the type actually turns out to be an
9863    ERROR_MARK node.  */
9864
9865 static inline unsigned HOST_WIDE_INT
9866 simple_type_size_in_bits (const_tree type)
9867 {
9868   if (TREE_CODE (type) == ERROR_MARK)
9869     return BITS_PER_WORD;
9870   else if (TYPE_SIZE (type) == NULL_TREE)
9871     return 0;
9872   else if (host_integerp (TYPE_SIZE (type), 1))
9873     return tree_low_cst (TYPE_SIZE (type), 1);
9874   else
9875     return TYPE_ALIGN (type);
9876 }
9877
9878 /*  Given a pointer to a tree node for a subrange type, return a pointer
9879     to a DIE that describes the given type.  */
9880
9881 static dw_die_ref
9882 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
9883 {
9884   dw_die_ref subrange_die;
9885   const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
9886
9887   if (context_die == NULL)
9888     context_die = comp_unit_die;
9889
9890   subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
9891
9892   if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
9893     {
9894       /* The size of the subrange type and its base type do not match,
9895          so we need to generate a size attribute for the subrange type.  */
9896       add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
9897     }
9898
9899   if (low)
9900     add_bound_info (subrange_die, DW_AT_lower_bound, low);
9901   if (high)
9902     add_bound_info (subrange_die, DW_AT_upper_bound, high);
9903
9904   return subrange_die;
9905 }
9906
9907 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
9908    entry that chains various modifiers in front of the given type.  */
9909
9910 static dw_die_ref
9911 modified_type_die (tree type, int is_const_type, int is_volatile_type,
9912                    dw_die_ref context_die)
9913 {
9914   enum tree_code code = TREE_CODE (type);
9915   dw_die_ref mod_type_die;
9916   dw_die_ref sub_die = NULL;
9917   tree item_type = NULL;
9918   tree qualified_type;
9919   tree name, low, high;
9920
9921   if (code == ERROR_MARK)
9922     return NULL;
9923
9924   /* See if we already have the appropriately qualified variant of
9925      this type.  */
9926   qualified_type
9927     = get_qualified_type (type,
9928                           ((is_const_type ? TYPE_QUAL_CONST : 0)
9929                            | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
9930
9931   /* If we do, then we can just use its DIE, if it exists.  */
9932   if (qualified_type)
9933     {
9934       mod_type_die = lookup_type_die (qualified_type);
9935       if (mod_type_die)
9936         return mod_type_die;
9937     }
9938
9939   name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
9940
9941   /* Handle C typedef types.  */
9942   if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name))
9943     {
9944       tree dtype = TREE_TYPE (name);
9945
9946       if (qualified_type == dtype)
9947         {
9948           /* For a named type, use the typedef.  */
9949           gen_type_die (qualified_type, context_die);
9950           return lookup_type_die (qualified_type);
9951         }
9952       else if (is_const_type < TYPE_READONLY (dtype)
9953                || is_volatile_type < TYPE_VOLATILE (dtype)
9954                || (is_const_type <= TYPE_READONLY (dtype)
9955                    && is_volatile_type <= TYPE_VOLATILE (dtype)
9956                    && DECL_ORIGINAL_TYPE (name) != type))
9957         /* cv-unqualified version of named type.  Just use the unnamed
9958            type to which it refers.  */
9959         return modified_type_die (DECL_ORIGINAL_TYPE (name),
9960                                   is_const_type, is_volatile_type,
9961                                   context_die);
9962       /* Else cv-qualified version of named type; fall through.  */
9963     }
9964
9965   if (is_const_type)
9966     {
9967       mod_type_die = new_die (DW_TAG_const_type, comp_unit_die, type);
9968       sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
9969     }
9970   else if (is_volatile_type)
9971     {
9972       mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die, type);
9973       sub_die = modified_type_die (type, 0, 0, context_die);
9974     }
9975   else if (code == POINTER_TYPE)
9976     {
9977       mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die, type);
9978       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
9979                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
9980       item_type = TREE_TYPE (type);
9981     }
9982   else if (code == REFERENCE_TYPE)
9983     {
9984       mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die, type);
9985       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
9986                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
9987       item_type = TREE_TYPE (type);
9988     }
9989   else if (code == INTEGER_TYPE
9990            && TREE_TYPE (type) != NULL_TREE
9991            && subrange_type_for_debug_p (type, &low, &high))
9992     {
9993       mod_type_die = subrange_type_die (type, low, high, context_die);
9994       item_type = TREE_TYPE (type);
9995     }
9996   else if (is_base_type (type))
9997     mod_type_die = base_type_die (type);
9998   else
9999     {
10000       gen_type_die (type, context_die);
10001
10002       /* We have to get the type_main_variant here (and pass that to the
10003          `lookup_type_die' routine) because the ..._TYPE node we have
10004          might simply be a *copy* of some original type node (where the
10005          copy was created to help us keep track of typedef names) and
10006          that copy might have a different TYPE_UID from the original
10007          ..._TYPE node.  */
10008       if (TREE_CODE (type) != VECTOR_TYPE)
10009         return lookup_type_die (type_main_variant (type));
10010       else
10011         /* Vectors have the debugging information in the type,
10012            not the main variant.  */
10013         return lookup_type_die (type);
10014     }
10015
10016   /* Builtin types don't have a DECL_ORIGINAL_TYPE.  For those,
10017      don't output a DW_TAG_typedef, since there isn't one in the
10018      user's program; just attach a DW_AT_name to the type.  */
10019   if (name
10020       && (TREE_CODE (name) != TYPE_DECL
10021           || (TREE_TYPE (name) == qualified_type && DECL_NAME (name))))
10022     {
10023       if (TREE_CODE (name) == TYPE_DECL)
10024         /* Could just call add_name_and_src_coords_attributes here,
10025            but since this is a builtin type it doesn't have any
10026            useful source coordinates anyway.  */
10027         name = DECL_NAME (name);
10028       add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
10029     }
10030
10031   if (qualified_type)
10032     equate_type_number_to_die (qualified_type, mod_type_die);
10033
10034   if (item_type)
10035     /* We must do this after the equate_type_number_to_die call, in case
10036        this is a recursive type.  This ensures that the modified_type_die
10037        recursion will terminate even if the type is recursive.  Recursive
10038        types are possible in Ada.  */
10039     sub_die = modified_type_die (item_type,
10040                                  TYPE_READONLY (item_type),
10041                                  TYPE_VOLATILE (item_type),
10042                                  context_die);
10043
10044   if (sub_die != NULL)
10045     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
10046
10047   return mod_type_die;
10048 }
10049
10050 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
10051    an enumerated type.  */
10052
10053 static inline int
10054 type_is_enum (const_tree type)
10055 {
10056   return TREE_CODE (type) == ENUMERAL_TYPE;
10057 }
10058
10059 /* Return the DBX register number described by a given RTL node.  */
10060
10061 static unsigned int
10062 dbx_reg_number (const_rtx rtl)
10063 {
10064   unsigned regno = REGNO (rtl);
10065
10066   gcc_assert (regno < FIRST_PSEUDO_REGISTER);
10067
10068 #ifdef LEAF_REG_REMAP
10069   if (current_function_uses_only_leaf_regs)
10070     {
10071       int leaf_reg = LEAF_REG_REMAP (regno);
10072       if (leaf_reg != -1)
10073         regno = (unsigned) leaf_reg;
10074     }
10075 #endif
10076
10077   return DBX_REGISTER_NUMBER (regno);
10078 }
10079
10080 /* Optionally add a DW_OP_piece term to a location description expression.
10081    DW_OP_piece is only added if the location description expression already
10082    doesn't end with DW_OP_piece.  */
10083
10084 static void
10085 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
10086 {
10087   dw_loc_descr_ref loc;
10088
10089   if (*list_head != NULL)
10090     {
10091       /* Find the end of the chain.  */
10092       for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
10093         ;
10094
10095       if (loc->dw_loc_opc != DW_OP_piece)
10096         loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
10097     }
10098 }
10099
10100 /* Return a location descriptor that designates a machine register or
10101    zero if there is none.  */
10102
10103 static dw_loc_descr_ref
10104 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
10105 {
10106   rtx regs;
10107
10108   if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
10109     return 0;
10110
10111   regs = targetm.dwarf_register_span (rtl);
10112
10113   if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
10114     return multiple_reg_loc_descriptor (rtl, regs, initialized);
10115   else
10116     return one_reg_loc_descriptor (dbx_reg_number (rtl), initialized);
10117 }
10118
10119 /* Return a location descriptor that designates a machine register for
10120    a given hard register number.  */
10121
10122 static dw_loc_descr_ref
10123 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
10124 {
10125   dw_loc_descr_ref reg_loc_descr;
10126
10127   if (regno <= 31)
10128     reg_loc_descr
10129       = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
10130   else
10131     reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
10132
10133   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10134     add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10135
10136   return reg_loc_descr;
10137 }
10138
10139 /* Given an RTL of a register, return a location descriptor that
10140    designates a value that spans more than one register.  */
10141
10142 static dw_loc_descr_ref
10143 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
10144                              enum var_init_status initialized)
10145 {
10146   int nregs, size, i;
10147   unsigned reg;
10148   dw_loc_descr_ref loc_result = NULL;
10149
10150   reg = REGNO (rtl);
10151 #ifdef LEAF_REG_REMAP
10152   if (current_function_uses_only_leaf_regs)
10153     {
10154       int leaf_reg = LEAF_REG_REMAP (reg);
10155       if (leaf_reg != -1)
10156         reg = (unsigned) leaf_reg;
10157     }
10158 #endif
10159   gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
10160   nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
10161
10162   /* Simple, contiguous registers.  */
10163   if (regs == NULL_RTX)
10164     {
10165       size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
10166
10167       loc_result = NULL;
10168       while (nregs--)
10169         {
10170           dw_loc_descr_ref t;
10171
10172           t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
10173                                       VAR_INIT_STATUS_INITIALIZED);
10174           add_loc_descr (&loc_result, t);
10175           add_loc_descr_op_piece (&loc_result, size);
10176           ++reg;
10177         }
10178       return loc_result;
10179     }
10180
10181   /* Now onto stupid register sets in non contiguous locations.  */
10182
10183   gcc_assert (GET_CODE (regs) == PARALLEL);
10184
10185   size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
10186   loc_result = NULL;
10187
10188   for (i = 0; i < XVECLEN (regs, 0); ++i)
10189     {
10190       dw_loc_descr_ref t;
10191
10192       t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
10193                                   VAR_INIT_STATUS_INITIALIZED);
10194       add_loc_descr (&loc_result, t);
10195       size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
10196       add_loc_descr_op_piece (&loc_result, size);
10197     }
10198
10199   if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10200     add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10201   return loc_result;
10202 }
10203
10204 #endif /* DWARF2_DEBUGGING_INFO */
10205
10206 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
10207
10208 /* Return a location descriptor that designates a constant.  */
10209
10210 static dw_loc_descr_ref
10211 int_loc_descriptor (HOST_WIDE_INT i)
10212 {
10213   enum dwarf_location_atom op;
10214
10215   /* Pick the smallest representation of a constant, rather than just
10216      defaulting to the LEB encoding.  */
10217   if (i >= 0)
10218     {
10219       if (i <= 31)
10220         op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
10221       else if (i <= 0xff)
10222         op = DW_OP_const1u;
10223       else if (i <= 0xffff)
10224         op = DW_OP_const2u;
10225       else if (HOST_BITS_PER_WIDE_INT == 32
10226                || i <= 0xffffffff)
10227         op = DW_OP_const4u;
10228       else
10229         op = DW_OP_constu;
10230     }
10231   else
10232     {
10233       if (i >= -0x80)
10234         op = DW_OP_const1s;
10235       else if (i >= -0x8000)
10236         op = DW_OP_const2s;
10237       else if (HOST_BITS_PER_WIDE_INT == 32
10238                || i >= -0x80000000)
10239         op = DW_OP_const4s;
10240       else
10241         op = DW_OP_consts;
10242     }
10243
10244   return new_loc_descr (op, i, 0);
10245 }
10246 #endif
10247
10248 #ifdef DWARF2_DEBUGGING_INFO
10249
10250 /* Return a location descriptor that designates a base+offset location.  */
10251
10252 static dw_loc_descr_ref
10253 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
10254                  enum var_init_status initialized)
10255 {
10256   unsigned int regno;
10257   dw_loc_descr_ref result;
10258   dw_fde_ref fde = current_fde ();
10259
10260   /* We only use "frame base" when we're sure we're talking about the
10261      post-prologue local stack frame.  We do this by *not* running
10262      register elimination until this point, and recognizing the special
10263      argument pointer and soft frame pointer rtx's.  */
10264   if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
10265     {
10266       rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
10267
10268       if (elim != reg)
10269         {
10270           if (GET_CODE (elim) == PLUS)
10271             {
10272               offset += INTVAL (XEXP (elim, 1));
10273               elim = XEXP (elim, 0);
10274             }
10275           gcc_assert ((SUPPORTS_STACK_ALIGNMENT
10276                        && (elim == hard_frame_pointer_rtx
10277                            || elim == stack_pointer_rtx))
10278                       || elim == (frame_pointer_needed
10279                                   ? hard_frame_pointer_rtx
10280                                   : stack_pointer_rtx));
10281
10282           /* If drap register is used to align stack, use frame
10283              pointer + offset to access stack variables.  If stack
10284              is aligned without drap, use stack pointer + offset to
10285              access stack variables.  */
10286           if (crtl->stack_realign_tried
10287               && reg == frame_pointer_rtx)
10288             {
10289               int base_reg
10290                 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
10291                                       ? HARD_FRAME_POINTER_REGNUM
10292                                       : STACK_POINTER_REGNUM);
10293               return new_reg_loc_descr (base_reg, offset);
10294             }
10295
10296           offset += frame_pointer_fb_offset;
10297           return new_loc_descr (DW_OP_fbreg, offset, 0);
10298         }
10299     }
10300   else if (fde
10301            && fde->drap_reg != INVALID_REGNUM
10302            && (fde->drap_reg == REGNO (reg)
10303                || fde->vdrap_reg == REGNO (reg)))
10304     {
10305       /* Use cfa+offset to represent the location of arguments passed
10306          on stack when drap is used to align stack.  */
10307       return new_loc_descr (DW_OP_fbreg, offset, 0);
10308     }
10309
10310   regno = dbx_reg_number (reg);
10311   if (regno <= 31)
10312     result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
10313                             offset, 0);
10314   else
10315     result = new_loc_descr (DW_OP_bregx, regno, offset);
10316
10317   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10318     add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10319
10320   return result;
10321 }
10322
10323 /* Return true if this RTL expression describes a base+offset calculation.  */
10324
10325 static inline int
10326 is_based_loc (const_rtx rtl)
10327 {
10328   return (GET_CODE (rtl) == PLUS
10329           && ((REG_P (XEXP (rtl, 0))
10330                && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
10331                && CONST_INT_P (XEXP (rtl, 1)))));
10332 }
10333
10334 /* Return a descriptor that describes the concatenation of N locations
10335    used to form the address of a memory location.  */
10336
10337 static dw_loc_descr_ref
10338 concatn_mem_loc_descriptor (rtx concatn, enum machine_mode mode,
10339                             enum var_init_status initialized)
10340 {
10341   unsigned int i;
10342   dw_loc_descr_ref cc_loc_result = NULL;
10343   unsigned int n = XVECLEN (concatn, 0);
10344
10345   for (i = 0; i < n; ++i)
10346     {
10347       dw_loc_descr_ref ref;
10348       rtx x = XVECEXP (concatn, 0, i);
10349
10350       ref = mem_loc_descriptor (x, mode, VAR_INIT_STATUS_INITIALIZED);
10351       if (ref == NULL)
10352         return NULL;
10353
10354       add_loc_descr (&cc_loc_result, ref);
10355       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
10356     }
10357
10358   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10359     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10360
10361   return cc_loc_result;
10362 }
10363
10364 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
10365    failed.  */
10366
10367 static dw_loc_descr_ref
10368 tls_mem_loc_descriptor (rtx mem)
10369 {
10370   tree base;
10371   dw_loc_descr_ref loc_result;
10372
10373   if (MEM_EXPR (mem) == NULL_TREE || MEM_OFFSET (mem) == NULL_RTX)
10374     return NULL;
10375
10376   base = get_base_address (MEM_EXPR (mem));
10377   if (base == NULL
10378       || TREE_CODE (base) != VAR_DECL
10379       || !DECL_THREAD_LOCAL_P (base))
10380     return NULL;
10381
10382   loc_result = loc_descriptor_from_tree_1 (MEM_EXPR (mem), 2);
10383   if (loc_result == NULL)
10384     return NULL;
10385
10386   if (INTVAL (MEM_OFFSET (mem)))
10387     loc_descr_plus_const (&loc_result, INTVAL (MEM_OFFSET (mem)));
10388
10389   return loc_result;
10390 }
10391
10392 /* The following routine converts the RTL for a variable or parameter
10393    (resident in memory) into an equivalent Dwarf representation of a
10394    mechanism for getting the address of that same variable onto the top of a
10395    hypothetical "address evaluation" stack.
10396
10397    When creating memory location descriptors, we are effectively transforming
10398    the RTL for a memory-resident object into its Dwarf postfix expression
10399    equivalent.  This routine recursively descends an RTL tree, turning
10400    it into Dwarf postfix code as it goes.
10401
10402    MODE is the mode of the memory reference, needed to handle some
10403    autoincrement addressing modes.
10404
10405    CAN_USE_FBREG is a flag whether we can use DW_AT_frame_base in the
10406    location list for RTL.
10407
10408    Return 0 if we can't represent the location.  */
10409
10410 static dw_loc_descr_ref
10411 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
10412                     enum var_init_status initialized)
10413 {
10414   dw_loc_descr_ref mem_loc_result = NULL;
10415   enum dwarf_location_atom op;
10416
10417   /* Note that for a dynamically sized array, the location we will generate a
10418      description of here will be the lowest numbered location which is
10419      actually within the array.  That's *not* necessarily the same as the
10420      zeroth element of the array.  */
10421
10422   rtl = targetm.delegitimize_address (rtl);
10423
10424   switch (GET_CODE (rtl))
10425     {
10426     case POST_INC:
10427     case POST_DEC:
10428     case POST_MODIFY:
10429       /* POST_INC and POST_DEC can be handled just like a SUBREG.  So we
10430          just fall into the SUBREG code.  */
10431
10432       /* ... fall through ...  */
10433
10434     case SUBREG:
10435       /* The case of a subreg may arise when we have a local (register)
10436          variable or a formal (register) parameter which doesn't quite fill
10437          up an entire register.  For now, just assume that it is
10438          legitimate to make the Dwarf info refer to the whole register which
10439          contains the given subreg.  */
10440       rtl = XEXP (rtl, 0);
10441
10442       /* ... fall through ...  */
10443
10444     case REG:
10445       /* Whenever a register number forms a part of the description of the
10446          method for calculating the (dynamic) address of a memory resident
10447          object, DWARF rules require the register number be referred to as
10448          a "base register".  This distinction is not based in any way upon
10449          what category of register the hardware believes the given register
10450          belongs to.  This is strictly DWARF terminology we're dealing with
10451          here. Note that in cases where the location of a memory-resident
10452          data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
10453          OP_CONST (0)) the actual DWARF location descriptor that we generate
10454          may just be OP_BASEREG (basereg).  This may look deceptively like
10455          the object in question was allocated to a register (rather than in
10456          memory) so DWARF consumers need to be aware of the subtle
10457          distinction between OP_REG and OP_BASEREG.  */
10458       if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
10459         mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
10460       else if (stack_realign_drap
10461                && crtl->drap_reg
10462                && crtl->args.internal_arg_pointer == rtl
10463                && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
10464         {
10465           /* If RTL is internal_arg_pointer, which has been optimized
10466              out, use DRAP instead.  */
10467           mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
10468                                             VAR_INIT_STATUS_INITIALIZED);
10469         }
10470       break;
10471
10472     case MEM:
10473       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
10474                                            VAR_INIT_STATUS_INITIALIZED);
10475       if (mem_loc_result == NULL)
10476         mem_loc_result = tls_mem_loc_descriptor (rtl);
10477       if (mem_loc_result != 0)
10478         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
10479       break;
10480
10481     case LO_SUM:
10482          rtl = XEXP (rtl, 1);
10483
10484       /* ... fall through ...  */
10485
10486     case LABEL_REF:
10487       /* Some ports can transform a symbol ref into a label ref, because
10488          the symbol ref is too far away and has to be dumped into a constant
10489          pool.  */
10490     case CONST:
10491     case SYMBOL_REF:
10492       /* Alternatively, the symbol in the constant pool might be referenced
10493          by a different symbol.  */
10494       if (GET_CODE (rtl) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (rtl))
10495         {
10496           bool marked;
10497           rtx tmp = get_pool_constant_mark (rtl, &marked);
10498
10499           if (GET_CODE (tmp) == SYMBOL_REF)
10500             {
10501               rtl = tmp;
10502               if (CONSTANT_POOL_ADDRESS_P (tmp))
10503                 get_pool_constant_mark (tmp, &marked);
10504               else
10505                 marked = true;
10506             }
10507
10508           /* If all references to this pool constant were optimized away,
10509              it was not output and thus we can't represent it.
10510              FIXME: might try to use DW_OP_const_value here, though
10511              DW_OP_piece complicates it.  */
10512           if (!marked)
10513             return 0;
10514         }
10515
10516       mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
10517       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
10518       mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
10519       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
10520       break;
10521
10522     case PRE_MODIFY:
10523       /* Extract the PLUS expression nested inside and fall into
10524          PLUS code below.  */
10525       rtl = XEXP (rtl, 1);
10526       goto plus;
10527
10528     case PRE_INC:
10529     case PRE_DEC:
10530       /* Turn these into a PLUS expression and fall into the PLUS code
10531          below.  */
10532       rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
10533                           GEN_INT (GET_CODE (rtl) == PRE_INC
10534                                    ? GET_MODE_UNIT_SIZE (mode)
10535                                    : -GET_MODE_UNIT_SIZE (mode)));
10536
10537       /* ... fall through ...  */
10538
10539     case PLUS:
10540     plus:
10541       if (is_based_loc (rtl))
10542         mem_loc_result = based_loc_descr (XEXP (rtl, 0),
10543                                           INTVAL (XEXP (rtl, 1)),
10544                                           VAR_INIT_STATUS_INITIALIZED);
10545       else
10546         {
10547           mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode,
10548                                                VAR_INIT_STATUS_INITIALIZED);
10549           if (mem_loc_result == 0)
10550             break;
10551
10552           if (CONST_INT_P (XEXP (rtl, 1)))
10553             loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
10554           else
10555             {
10556               dw_loc_descr_ref mem_loc_result2
10557                 = mem_loc_descriptor (XEXP (rtl, 1), mode,
10558                                       VAR_INIT_STATUS_INITIALIZED);
10559               if (mem_loc_result2 == 0)
10560                 break;
10561               add_loc_descr (&mem_loc_result, mem_loc_result2);
10562               add_loc_descr (&mem_loc_result,
10563                              new_loc_descr (DW_OP_plus, 0, 0));
10564             }
10565         }
10566       break;
10567
10568     /* If a pseudo-reg is optimized away, it is possible for it to
10569        be replaced with a MEM containing a multiply or shift.  */
10570     case MULT:
10571       op = DW_OP_mul;
10572       goto do_binop;
10573
10574     case ASHIFT:
10575       op = DW_OP_shl;
10576       goto do_binop;
10577
10578     case ASHIFTRT:
10579       op = DW_OP_shra;
10580       goto do_binop;
10581
10582     case LSHIFTRT:
10583       op = DW_OP_shr;
10584       goto do_binop;
10585
10586     do_binop:
10587       {
10588         dw_loc_descr_ref op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
10589                                                    VAR_INIT_STATUS_INITIALIZED);
10590         dw_loc_descr_ref op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
10591                                                    VAR_INIT_STATUS_INITIALIZED);
10592
10593         if (op0 == 0 || op1 == 0)
10594           break;
10595
10596         mem_loc_result = op0;
10597         add_loc_descr (&mem_loc_result, op1);
10598         add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
10599         break;
10600       }
10601
10602     case CONST_INT:
10603       mem_loc_result = int_loc_descriptor (INTVAL (rtl));
10604       break;
10605
10606     case CONCATN:
10607       mem_loc_result = concatn_mem_loc_descriptor (rtl, mode,
10608                                                    VAR_INIT_STATUS_INITIALIZED);
10609       break;
10610
10611     case UNSPEC:
10612       /* If delegitimize_address couldn't do anything with the UNSPEC, we
10613          can't express it in the debug info.  This can happen e.g. with some
10614          TLS UNSPECs.  */
10615       break;
10616
10617     default:
10618       gcc_unreachable ();
10619     }
10620
10621   if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10622     add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10623
10624   return mem_loc_result;
10625 }
10626
10627 /* Return a descriptor that describes the concatenation of two locations.
10628    This is typically a complex variable.  */
10629
10630 static dw_loc_descr_ref
10631 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
10632 {
10633   dw_loc_descr_ref cc_loc_result = NULL;
10634   dw_loc_descr_ref x0_ref = loc_descriptor (x0, VAR_INIT_STATUS_INITIALIZED);
10635   dw_loc_descr_ref x1_ref = loc_descriptor (x1, VAR_INIT_STATUS_INITIALIZED);
10636
10637   if (x0_ref == 0 || x1_ref == 0)
10638     return 0;
10639
10640   cc_loc_result = x0_ref;
10641   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
10642
10643   add_loc_descr (&cc_loc_result, x1_ref);
10644   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
10645
10646   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10647     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10648
10649   return cc_loc_result;
10650 }
10651
10652 /* Return a descriptor that describes the concatenation of N
10653    locations.  */
10654
10655 static dw_loc_descr_ref
10656 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
10657 {
10658   unsigned int i;
10659   dw_loc_descr_ref cc_loc_result = NULL;
10660   unsigned int n = XVECLEN (concatn, 0);
10661
10662   for (i = 0; i < n; ++i)
10663     {
10664       dw_loc_descr_ref ref;
10665       rtx x = XVECEXP (concatn, 0, i);
10666
10667       ref = loc_descriptor (x, VAR_INIT_STATUS_INITIALIZED);
10668       if (ref == NULL)
10669         return NULL;
10670
10671       add_loc_descr (&cc_loc_result, ref);
10672       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
10673     }
10674
10675   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10676     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10677
10678   return cc_loc_result;
10679 }
10680
10681 /* Output a proper Dwarf location descriptor for a variable or parameter
10682    which is either allocated in a register or in a memory location.  For a
10683    register, we just generate an OP_REG and the register number.  For a
10684    memory location we provide a Dwarf postfix expression describing how to
10685    generate the (dynamic) address of the object onto the address stack.
10686
10687    If we don't know how to describe it, return 0.  */
10688
10689 static dw_loc_descr_ref
10690 loc_descriptor (rtx rtl, enum var_init_status initialized)
10691 {
10692   dw_loc_descr_ref loc_result = NULL;
10693
10694   switch (GET_CODE (rtl))
10695     {
10696     case SUBREG:
10697       /* The case of a subreg may arise when we have a local (register)
10698          variable or a formal (register) parameter which doesn't quite fill
10699          up an entire register.  For now, just assume that it is
10700          legitimate to make the Dwarf info refer to the whole register which
10701          contains the given subreg.  */
10702       rtl = SUBREG_REG (rtl);
10703
10704       /* ... fall through ...  */
10705
10706     case REG:
10707       loc_result = reg_loc_descriptor (rtl, initialized);
10708       break;
10709
10710     case MEM:
10711       loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
10712                                        initialized);
10713       if (loc_result == NULL)
10714         loc_result = tls_mem_loc_descriptor (rtl);
10715       break;
10716
10717     case CONCAT:
10718       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
10719                                           initialized);
10720       break;
10721
10722     case CONCATN:
10723       loc_result = concatn_loc_descriptor (rtl, initialized);
10724       break;
10725
10726     case VAR_LOCATION:
10727       /* Single part.  */
10728       if (GET_CODE (XEXP (rtl, 1)) != PARALLEL)
10729         {
10730           loc_result = loc_descriptor (XEXP (XEXP (rtl, 1), 0), initialized);
10731           break;
10732         }
10733
10734       rtl = XEXP (rtl, 1);
10735       /* FALLTHRU */
10736
10737     case PARALLEL:
10738       {
10739         rtvec par_elems = XVEC (rtl, 0);
10740         int num_elem = GET_NUM_ELEM (par_elems);
10741         enum machine_mode mode;
10742         int i;
10743
10744         /* Create the first one, so we have something to add to.  */
10745         loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
10746                                      initialized);
10747         if (loc_result == NULL)
10748           return NULL;
10749         mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
10750         add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
10751         for (i = 1; i < num_elem; i++)
10752           {
10753             dw_loc_descr_ref temp;
10754
10755             temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
10756                                    initialized);
10757             if (temp == NULL)
10758               return NULL;
10759             add_loc_descr (&loc_result, temp);
10760             mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
10761             add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
10762           }
10763       }
10764       break;
10765
10766     default:
10767       gcc_unreachable ();
10768     }
10769
10770   return loc_result;
10771 }
10772
10773 /* Similar, but generate the descriptor from trees instead of rtl.  This comes
10774    up particularly with variable length arrays.  WANT_ADDRESS is 2 if this is
10775    a top-level invocation of loc_descriptor_from_tree; is 1 if this is not a
10776    top-level invocation, and we require the address of LOC; is 0 if we require
10777    the value of LOC.  */
10778
10779 static dw_loc_descr_ref
10780 loc_descriptor_from_tree_1 (tree loc, int want_address)
10781 {
10782   dw_loc_descr_ref ret, ret1;
10783   int have_address = 0;
10784   enum dwarf_location_atom op;
10785
10786   /* ??? Most of the time we do not take proper care for sign/zero
10787      extending the values properly.  Hopefully this won't be a real
10788      problem...  */
10789
10790   switch (TREE_CODE (loc))
10791     {
10792     case ERROR_MARK:
10793       return 0;
10794
10795     case PLACEHOLDER_EXPR:
10796       /* This case involves extracting fields from an object to determine the
10797          position of other fields.  We don't try to encode this here.  The
10798          only user of this is Ada, which encodes the needed information using
10799          the names of types.  */
10800       return 0;
10801
10802     case CALL_EXPR:
10803       return 0;
10804
10805     case PREINCREMENT_EXPR:
10806     case PREDECREMENT_EXPR:
10807     case POSTINCREMENT_EXPR:
10808     case POSTDECREMENT_EXPR:
10809       /* There are no opcodes for these operations.  */
10810       return 0;
10811
10812     case ADDR_EXPR:
10813       /* If we already want an address, there's nothing we can do.  */
10814       if (want_address)
10815         return 0;
10816
10817       /* Otherwise, process the argument and look for the address.  */
10818       return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 1);
10819
10820     case VAR_DECL:
10821       if (DECL_THREAD_LOCAL_P (loc))
10822         {
10823           rtx rtl;
10824           enum dwarf_location_atom first_op;
10825           enum dwarf_location_atom second_op;
10826           bool dtprel = false;
10827
10828           if (targetm.have_tls)
10829             {
10830               /* If this is not defined, we have no way to emit the
10831                  data.  */
10832               if (!targetm.asm_out.output_dwarf_dtprel)
10833                 return 0;
10834
10835                /* The way DW_OP_GNU_push_tls_address is specified, we
10836                   can only look up addresses of objects in the current
10837                   module.  */
10838               if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
10839                 return 0;
10840               first_op = DW_OP_addr;
10841               dtprel = true;
10842               second_op = DW_OP_GNU_push_tls_address;
10843             }
10844           else
10845             {
10846               if (!targetm.emutls.debug_form_tls_address)
10847                 return 0;
10848               loc = emutls_decl (loc);
10849               first_op = DW_OP_addr;
10850               second_op = DW_OP_form_tls_address;
10851             }
10852
10853           rtl = rtl_for_decl_location (loc);
10854           if (rtl == NULL_RTX)
10855             return 0;
10856
10857           if (!MEM_P (rtl))
10858             return 0;
10859           rtl = XEXP (rtl, 0);
10860           if (! CONSTANT_P (rtl))
10861             return 0;
10862
10863           ret = new_loc_descr (first_op, 0, 0);
10864           ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
10865           ret->dw_loc_oprnd1.v.val_addr = rtl;
10866           ret->dtprel = dtprel;
10867
10868           ret1 = new_loc_descr (second_op, 0, 0);
10869           add_loc_descr (&ret, ret1);
10870
10871           have_address = 1;
10872           break;
10873         }
10874       /* FALLTHRU */
10875
10876     case PARM_DECL:
10877       if (DECL_HAS_VALUE_EXPR_P (loc))
10878         return loc_descriptor_from_tree_1 (DECL_VALUE_EXPR (loc),
10879                                            want_address);
10880       /* FALLTHRU */
10881
10882     case RESULT_DECL:
10883     case FUNCTION_DECL:
10884       {
10885         rtx rtl = rtl_for_decl_location (loc);
10886
10887         if (rtl == NULL_RTX)
10888           return 0;
10889         else if (CONST_INT_P (rtl))
10890           {
10891             HOST_WIDE_INT val = INTVAL (rtl);
10892             if (TYPE_UNSIGNED (TREE_TYPE (loc)))
10893               val &= GET_MODE_MASK (DECL_MODE (loc));
10894             ret = int_loc_descriptor (val);
10895           }
10896         else if (GET_CODE (rtl) == CONST_STRING)
10897           return 0;
10898         else if (CONSTANT_P (rtl))
10899           {
10900             ret = new_loc_descr (DW_OP_addr, 0, 0);
10901             ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
10902             ret->dw_loc_oprnd1.v.val_addr = rtl;
10903           }
10904         else
10905           {
10906             enum machine_mode mode;
10907
10908             /* Certain constructs can only be represented at top-level.  */
10909             if (want_address == 2)
10910               return loc_descriptor (rtl, VAR_INIT_STATUS_INITIALIZED);
10911
10912             mode = GET_MODE (rtl);
10913             if (MEM_P (rtl))
10914               {
10915                 rtl = XEXP (rtl, 0);
10916                 have_address = 1;
10917               }
10918             ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
10919           }
10920       }
10921       break;
10922
10923     case INDIRECT_REF:
10924       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
10925       have_address = 1;
10926       break;
10927
10928     case COMPOUND_EXPR:
10929       return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), want_address);
10930
10931     CASE_CONVERT:
10932     case VIEW_CONVERT_EXPR:
10933     case SAVE_EXPR:
10934     case MODIFY_EXPR:
10935       return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), want_address);
10936
10937     case COMPONENT_REF:
10938     case BIT_FIELD_REF:
10939     case ARRAY_REF:
10940     case ARRAY_RANGE_REF:
10941       {
10942         tree obj, offset;
10943         HOST_WIDE_INT bitsize, bitpos, bytepos;
10944         enum machine_mode mode;
10945         int volatilep;
10946         int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
10947
10948         obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
10949                                    &unsignedp, &volatilep, false);
10950
10951         if (obj == loc)
10952           return 0;
10953
10954         ret = loc_descriptor_from_tree_1 (obj, 1);
10955         if (ret == 0
10956             || bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
10957           return 0;
10958
10959         if (offset != NULL_TREE)
10960           {
10961             /* Variable offset.  */
10962             ret1 = loc_descriptor_from_tree_1 (offset, 0);
10963             if (ret1 == 0)
10964               return 0;
10965             add_loc_descr (&ret, ret1);
10966             add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
10967           }
10968
10969         bytepos = bitpos / BITS_PER_UNIT;
10970         loc_descr_plus_const (&ret, bytepos);
10971
10972         have_address = 1;
10973         break;
10974       }
10975
10976     case INTEGER_CST:
10977       if (host_integerp (loc, 0))
10978         ret = int_loc_descriptor (tree_low_cst (loc, 0));
10979       else
10980         return 0;
10981       break;
10982
10983     case CONSTRUCTOR:
10984       {
10985         /* Get an RTL for this, if something has been emitted.  */
10986         rtx rtl = lookup_constant_def (loc);
10987         enum machine_mode mode;
10988
10989         if (!rtl || !MEM_P (rtl))
10990           return 0;
10991         mode = GET_MODE (rtl);
10992         rtl = XEXP (rtl, 0);
10993         ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
10994         have_address = 1;
10995         break;
10996       }
10997
10998     case TRUTH_AND_EXPR:
10999     case TRUTH_ANDIF_EXPR:
11000     case BIT_AND_EXPR:
11001       op = DW_OP_and;
11002       goto do_binop;
11003
11004     case TRUTH_XOR_EXPR:
11005     case BIT_XOR_EXPR:
11006       op = DW_OP_xor;
11007       goto do_binop;
11008
11009     case TRUTH_OR_EXPR:
11010     case TRUTH_ORIF_EXPR:
11011     case BIT_IOR_EXPR:
11012       op = DW_OP_or;
11013       goto do_binop;
11014
11015     case FLOOR_DIV_EXPR:
11016     case CEIL_DIV_EXPR:
11017     case ROUND_DIV_EXPR:
11018     case TRUNC_DIV_EXPR:
11019       op = DW_OP_div;
11020       goto do_binop;
11021
11022     case MINUS_EXPR:
11023       op = DW_OP_minus;
11024       goto do_binop;
11025
11026     case FLOOR_MOD_EXPR:
11027     case CEIL_MOD_EXPR:
11028     case ROUND_MOD_EXPR:
11029     case TRUNC_MOD_EXPR:
11030       op = DW_OP_mod;
11031       goto do_binop;
11032
11033     case MULT_EXPR:
11034       op = DW_OP_mul;
11035       goto do_binop;
11036
11037     case LSHIFT_EXPR:
11038       op = DW_OP_shl;
11039       goto do_binop;
11040
11041     case RSHIFT_EXPR:
11042       op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
11043       goto do_binop;
11044
11045     case POINTER_PLUS_EXPR:
11046     case PLUS_EXPR:
11047       if (TREE_CODE (TREE_OPERAND (loc, 1)) == INTEGER_CST
11048           && host_integerp (TREE_OPERAND (loc, 1), 0))
11049         {
11050           ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
11051           if (ret == 0)
11052             return 0;
11053
11054           loc_descr_plus_const (&ret, tree_low_cst (TREE_OPERAND (loc, 1), 0));
11055           break;
11056         }
11057
11058       op = DW_OP_plus;
11059       goto do_binop;
11060
11061     case LE_EXPR:
11062       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
11063         return 0;
11064
11065       op = DW_OP_le;
11066       goto do_binop;
11067
11068     case GE_EXPR:
11069       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
11070         return 0;
11071
11072       op = DW_OP_ge;
11073       goto do_binop;
11074
11075     case LT_EXPR:
11076       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
11077         return 0;
11078
11079       op = DW_OP_lt;
11080       goto do_binop;
11081
11082     case GT_EXPR:
11083       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
11084         return 0;
11085
11086       op = DW_OP_gt;
11087       goto do_binop;
11088
11089     case EQ_EXPR:
11090       op = DW_OP_eq;
11091       goto do_binop;
11092
11093     case NE_EXPR:
11094       op = DW_OP_ne;
11095       goto do_binop;
11096
11097     do_binop:
11098       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
11099       ret1 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
11100       if (ret == 0 || ret1 == 0)
11101         return 0;
11102
11103       add_loc_descr (&ret, ret1);
11104       add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11105       break;
11106
11107     case TRUTH_NOT_EXPR:
11108     case BIT_NOT_EXPR:
11109       op = DW_OP_not;
11110       goto do_unop;
11111
11112     case ABS_EXPR:
11113       op = DW_OP_abs;
11114       goto do_unop;
11115
11116     case NEGATE_EXPR:
11117       op = DW_OP_neg;
11118       goto do_unop;
11119
11120     do_unop:
11121       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
11122       if (ret == 0)
11123         return 0;
11124
11125       add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11126       break;
11127
11128     case MIN_EXPR:
11129     case MAX_EXPR:
11130       {
11131         const enum tree_code code =
11132           TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
11133
11134         loc = build3 (COND_EXPR, TREE_TYPE (loc),
11135                       build2 (code, integer_type_node,
11136                               TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
11137                       TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
11138       }
11139
11140       /* ... fall through ...  */
11141
11142     case COND_EXPR:
11143       {
11144         dw_loc_descr_ref lhs
11145           = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
11146         dw_loc_descr_ref rhs
11147           = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 2), 0);
11148         dw_loc_descr_ref bra_node, jump_node, tmp;
11149
11150         ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
11151         if (ret == 0 || lhs == 0 || rhs == 0)
11152           return 0;
11153
11154         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
11155         add_loc_descr (&ret, bra_node);
11156
11157         add_loc_descr (&ret, rhs);
11158         jump_node = new_loc_descr (DW_OP_skip, 0, 0);
11159         add_loc_descr (&ret, jump_node);
11160
11161         add_loc_descr (&ret, lhs);
11162         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
11163         bra_node->dw_loc_oprnd1.v.val_loc = lhs;
11164
11165         /* ??? Need a node to point the skip at.  Use a nop.  */
11166         tmp = new_loc_descr (DW_OP_nop, 0, 0);
11167         add_loc_descr (&ret, tmp);
11168         jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
11169         jump_node->dw_loc_oprnd1.v.val_loc = tmp;
11170       }
11171       break;
11172
11173     case FIX_TRUNC_EXPR:
11174       return 0;
11175
11176     default:
11177       /* Leave front-end specific codes as simply unknown.  This comes
11178          up, for instance, with the C STMT_EXPR.  */
11179       if ((unsigned int) TREE_CODE (loc)
11180           >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
11181         return 0;
11182
11183 #ifdef ENABLE_CHECKING
11184       /* Otherwise this is a generic code; we should just lists all of
11185          these explicitly.  We forgot one.  */
11186       gcc_unreachable ();
11187 #else
11188       /* In a release build, we want to degrade gracefully: better to
11189          generate incomplete debugging information than to crash.  */
11190       return NULL;
11191 #endif
11192     }
11193
11194   /* Show if we can't fill the request for an address.  */
11195   if (want_address && !have_address)
11196     return 0;
11197
11198   /* If we've got an address and don't want one, dereference.  */
11199   if (!want_address && have_address && ret)
11200     {
11201       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
11202
11203       if (size > DWARF2_ADDR_SIZE || size == -1)
11204         return 0;
11205       else if (size == DWARF2_ADDR_SIZE)
11206         op = DW_OP_deref;
11207       else
11208         op = DW_OP_deref_size;
11209
11210       add_loc_descr (&ret, new_loc_descr (op, size, 0));
11211     }
11212
11213   return ret;
11214 }
11215
11216 static inline dw_loc_descr_ref
11217 loc_descriptor_from_tree (tree loc)
11218 {
11219   return loc_descriptor_from_tree_1 (loc, 2);
11220 }
11221
11222 /* Given a value, round it up to the lowest multiple of `boundary'
11223    which is not less than the value itself.  */
11224
11225 static inline HOST_WIDE_INT
11226 ceiling (HOST_WIDE_INT value, unsigned int boundary)
11227 {
11228   return (((value + boundary - 1) / boundary) * boundary);
11229 }
11230
11231 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
11232    pointer to the declared type for the relevant field variable, or return
11233    `integer_type_node' if the given node turns out to be an
11234    ERROR_MARK node.  */
11235
11236 static inline tree
11237 field_type (const_tree decl)
11238 {
11239   tree type;
11240
11241   if (TREE_CODE (decl) == ERROR_MARK)
11242     return integer_type_node;
11243
11244   type = DECL_BIT_FIELD_TYPE (decl);
11245   if (type == NULL_TREE)
11246     type = TREE_TYPE (decl);
11247
11248   return type;
11249 }
11250
11251 /* Given a pointer to a tree node, return the alignment in bits for
11252    it, or else return BITS_PER_WORD if the node actually turns out to
11253    be an ERROR_MARK node.  */
11254
11255 static inline unsigned
11256 simple_type_align_in_bits (const_tree type)
11257 {
11258   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
11259 }
11260
11261 static inline unsigned
11262 simple_decl_align_in_bits (const_tree decl)
11263 {
11264   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
11265 }
11266
11267 /* Return the result of rounding T up to ALIGN.  */
11268
11269 static inline HOST_WIDE_INT
11270 round_up_to_align (HOST_WIDE_INT t, unsigned int align)
11271 {
11272   /* We must be careful if T is negative because HOST_WIDE_INT can be
11273      either "above" or "below" unsigned int as per the C promotion
11274      rules, depending on the host, thus making the signedness of the
11275      direct multiplication and division unpredictable.  */
11276   unsigned HOST_WIDE_INT u = (unsigned HOST_WIDE_INT) t;
11277
11278   u += align - 1;
11279   u /= align;
11280   u *= align;
11281
11282   return (HOST_WIDE_INT) u;
11283 }
11284
11285 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
11286    lowest addressed byte of the "containing object" for the given FIELD_DECL,
11287    or return 0 if we are unable to determine what that offset is, either
11288    because the argument turns out to be a pointer to an ERROR_MARK node, or
11289    because the offset is actually variable.  (We can't handle the latter case
11290    just yet).  */
11291
11292 static HOST_WIDE_INT
11293 field_byte_offset (const_tree decl)
11294 {
11295   HOST_WIDE_INT object_offset_in_bits;
11296   HOST_WIDE_INT bitpos_int;
11297
11298   if (TREE_CODE (decl) == ERROR_MARK)
11299     return 0;
11300
11301   gcc_assert (TREE_CODE (decl) == FIELD_DECL);
11302
11303   /* We cannot yet cope with fields whose positions are variable, so
11304      for now, when we see such things, we simply return 0.  Someday, we may
11305      be able to handle such cases, but it will be damn difficult.  */
11306   if (! host_integerp (bit_position (decl), 0))
11307     return 0;
11308
11309   bitpos_int = int_bit_position (decl);
11310
11311 #ifdef PCC_BITFIELD_TYPE_MATTERS
11312   if (PCC_BITFIELD_TYPE_MATTERS)
11313     {
11314       tree type;
11315       tree field_size_tree;
11316       HOST_WIDE_INT deepest_bitpos;
11317       unsigned HOST_WIDE_INT field_size_in_bits;
11318       unsigned int type_align_in_bits;
11319       unsigned int decl_align_in_bits;
11320       unsigned HOST_WIDE_INT type_size_in_bits;
11321
11322       type = field_type (decl);
11323       type_size_in_bits = simple_type_size_in_bits (type);
11324       type_align_in_bits = simple_type_align_in_bits (type);
11325
11326       field_size_tree = DECL_SIZE (decl);
11327
11328       /* The size could be unspecified if there was an error, or for
11329          a flexible array member.  */
11330       if (!field_size_tree)
11331         field_size_tree = bitsize_zero_node;
11332
11333       /* If the size of the field is not constant, use the type size.  */
11334       if (host_integerp (field_size_tree, 1))
11335         field_size_in_bits = tree_low_cst (field_size_tree, 1);
11336       else
11337         field_size_in_bits = type_size_in_bits;
11338
11339       decl_align_in_bits = simple_decl_align_in_bits (decl);
11340
11341       /* The GCC front-end doesn't make any attempt to keep track of the
11342          starting bit offset (relative to the start of the containing
11343          structure type) of the hypothetical "containing object" for a
11344          bit-field.  Thus, when computing the byte offset value for the
11345          start of the "containing object" of a bit-field, we must deduce
11346          this information on our own. This can be rather tricky to do in
11347          some cases.  For example, handling the following structure type
11348          definition when compiling for an i386/i486 target (which only
11349          aligns long long's to 32-bit boundaries) can be very tricky:
11350
11351          struct S { int field1; long long field2:31; };
11352
11353          Fortunately, there is a simple rule-of-thumb which can be used
11354          in such cases.  When compiling for an i386/i486, GCC will
11355          allocate 8 bytes for the structure shown above.  It decides to
11356          do this based upon one simple rule for bit-field allocation.
11357          GCC allocates each "containing object" for each bit-field at
11358          the first (i.e. lowest addressed) legitimate alignment boundary
11359          (based upon the required minimum alignment for the declared
11360          type of the field) which it can possibly use, subject to the
11361          condition that there is still enough available space remaining
11362          in the containing object (when allocated at the selected point)
11363          to fully accommodate all of the bits of the bit-field itself.
11364
11365          This simple rule makes it obvious why GCC allocates 8 bytes for
11366          each object of the structure type shown above.  When looking
11367          for a place to allocate the "containing object" for `field2',
11368          the compiler simply tries to allocate a 64-bit "containing
11369          object" at each successive 32-bit boundary (starting at zero)
11370          until it finds a place to allocate that 64- bit field such that
11371          at least 31 contiguous (and previously unallocated) bits remain
11372          within that selected 64 bit field.  (As it turns out, for the
11373          example above, the compiler finds it is OK to allocate the
11374          "containing object" 64-bit field at bit-offset zero within the
11375          structure type.)
11376
11377          Here we attempt to work backwards from the limited set of facts
11378          we're given, and we try to deduce from those facts, where GCC
11379          must have believed that the containing object started (within
11380          the structure type). The value we deduce is then used (by the
11381          callers of this routine) to generate DW_AT_location and
11382          DW_AT_bit_offset attributes for fields (both bit-fields and, in
11383          the case of DW_AT_location, regular fields as well).  */
11384
11385       /* Figure out the bit-distance from the start of the structure to
11386          the "deepest" bit of the bit-field.  */
11387       deepest_bitpos = bitpos_int + field_size_in_bits;
11388
11389       /* This is the tricky part.  Use some fancy footwork to deduce
11390          where the lowest addressed bit of the containing object must
11391          be.  */
11392       object_offset_in_bits = deepest_bitpos - type_size_in_bits;
11393
11394       /* Round up to type_align by default.  This works best for
11395          bitfields.  */
11396       object_offset_in_bits
11397         = round_up_to_align (object_offset_in_bits, type_align_in_bits);
11398
11399       if (object_offset_in_bits > bitpos_int)
11400         {
11401           object_offset_in_bits = deepest_bitpos - type_size_in_bits;
11402
11403           /* Round up to decl_align instead.  */
11404           object_offset_in_bits
11405             = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
11406         }
11407     }
11408   else
11409 #endif
11410     object_offset_in_bits = bitpos_int;
11411
11412   return object_offset_in_bits / BITS_PER_UNIT;
11413 }
11414 \f
11415 /* The following routines define various Dwarf attributes and any data
11416    associated with them.  */
11417
11418 /* Add a location description attribute value to a DIE.
11419
11420    This emits location attributes suitable for whole variables and
11421    whole parameters.  Note that the location attributes for struct fields are
11422    generated by the routine `data_member_location_attribute' below.  */
11423
11424 static inline void
11425 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
11426                              dw_loc_descr_ref descr)
11427 {
11428   if (descr != 0)
11429     add_AT_loc (die, attr_kind, descr);
11430 }
11431
11432 /* Attach the specialized form of location attribute used for data members of
11433    struct and union types.  In the special case of a FIELD_DECL node which
11434    represents a bit-field, the "offset" part of this special location
11435    descriptor must indicate the distance in bytes from the lowest-addressed
11436    byte of the containing struct or union type to the lowest-addressed byte of
11437    the "containing object" for the bit-field.  (See the `field_byte_offset'
11438    function above).
11439
11440    For any given bit-field, the "containing object" is a hypothetical object
11441    (of some integral or enum type) within which the given bit-field lives.  The
11442    type of this hypothetical "containing object" is always the same as the
11443    declared type of the individual bit-field itself (for GCC anyway... the
11444    DWARF spec doesn't actually mandate this).  Note that it is the size (in
11445    bytes) of the hypothetical "containing object" which will be given in the
11446    DW_AT_byte_size attribute for this bit-field.  (See the
11447    `byte_size_attribute' function below.)  It is also used when calculating the
11448    value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
11449    function below.)  */
11450
11451 static void
11452 add_data_member_location_attribute (dw_die_ref die, tree decl)
11453 {
11454   HOST_WIDE_INT offset;
11455   dw_loc_descr_ref loc_descr = 0;
11456
11457   if (TREE_CODE (decl) == TREE_BINFO)
11458     {
11459       /* We're working on the TAG_inheritance for a base class.  */
11460       if (BINFO_VIRTUAL_P (decl) && is_cxx ())
11461         {
11462           /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
11463              aren't at a fixed offset from all (sub)objects of the same
11464              type.  We need to extract the appropriate offset from our
11465              vtable.  The following dwarf expression means
11466
11467                BaseAddr = ObAddr + *((*ObAddr) - Offset)
11468
11469              This is specific to the V3 ABI, of course.  */
11470
11471           dw_loc_descr_ref tmp;
11472
11473           /* Make a copy of the object address.  */
11474           tmp = new_loc_descr (DW_OP_dup, 0, 0);
11475           add_loc_descr (&loc_descr, tmp);
11476
11477           /* Extract the vtable address.  */
11478           tmp = new_loc_descr (DW_OP_deref, 0, 0);
11479           add_loc_descr (&loc_descr, tmp);
11480
11481           /* Calculate the address of the offset.  */
11482           offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
11483           gcc_assert (offset < 0);
11484
11485           tmp = int_loc_descriptor (-offset);
11486           add_loc_descr (&loc_descr, tmp);
11487           tmp = new_loc_descr (DW_OP_minus, 0, 0);
11488           add_loc_descr (&loc_descr, tmp);
11489
11490           /* Extract the offset.  */
11491           tmp = new_loc_descr (DW_OP_deref, 0, 0);
11492           add_loc_descr (&loc_descr, tmp);
11493
11494           /* Add it to the object address.  */
11495           tmp = new_loc_descr (DW_OP_plus, 0, 0);
11496           add_loc_descr (&loc_descr, tmp);
11497         }
11498       else
11499         offset = tree_low_cst (BINFO_OFFSET (decl), 0);
11500     }
11501   else
11502     offset = field_byte_offset (decl);
11503
11504   if (! loc_descr)
11505     {
11506       if (dwarf_version > 2)
11507         {
11508           /* Don't need to output a location expression, just the constant. */
11509           add_AT_int (die, DW_AT_data_member_location, offset);
11510           return;
11511         }
11512       else
11513         {
11514           enum dwarf_location_atom op;
11515           
11516           /* The DWARF2 standard says that we should assume that the structure
11517              address is already on the stack, so we can specify a structure
11518              field address by using DW_OP_plus_uconst.  */
11519           
11520 #ifdef MIPS_DEBUGGING_INFO
11521           /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
11522              operator correctly.  It works only if we leave the offset on the
11523              stack.  */
11524           op = DW_OP_constu;
11525 #else
11526           op = DW_OP_plus_uconst;
11527 #endif
11528           
11529           loc_descr = new_loc_descr (op, offset, 0);
11530         }
11531     }
11532
11533   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
11534 }
11535
11536 /* Writes integer values to dw_vec_const array.  */
11537
11538 static void
11539 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
11540 {
11541   while (size != 0)
11542     {
11543       *dest++ = val & 0xff;
11544       val >>= 8;
11545       --size;
11546     }
11547 }
11548
11549 /* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
11550
11551 static HOST_WIDE_INT
11552 extract_int (const unsigned char *src, unsigned int size)
11553 {
11554   HOST_WIDE_INT val = 0;
11555
11556   src += size;
11557   while (size != 0)
11558     {
11559       val <<= 8;
11560       val |= *--src & 0xff;
11561       --size;
11562     }
11563   return val;
11564 }
11565
11566 /* Writes floating point values to dw_vec_const array.  */
11567
11568 static void
11569 insert_float (const_rtx rtl, unsigned char *array)
11570 {
11571   REAL_VALUE_TYPE rv;
11572   long val[4];
11573   int i;
11574
11575   REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
11576   real_to_target (val, &rv, GET_MODE (rtl));
11577
11578   /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
11579   for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
11580     {
11581       insert_int (val[i], 4, array);
11582       array += 4;
11583     }
11584 }
11585
11586 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
11587    does not have a "location" either in memory or in a register.  These
11588    things can arise in GNU C when a constant is passed as an actual parameter
11589    to an inlined function.  They can also arise in C++ where declared
11590    constants do not necessarily get memory "homes".  */
11591
11592 static void
11593 add_const_value_attribute (dw_die_ref die, rtx rtl)
11594 {
11595   switch (GET_CODE (rtl))
11596     {
11597     case CONST_INT:
11598       {
11599         HOST_WIDE_INT val = INTVAL (rtl);
11600
11601         if (val < 0)
11602           add_AT_int (die, DW_AT_const_value, val);
11603         else
11604           add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
11605       }
11606       break;
11607
11608     case CONST_DOUBLE:
11609       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
11610          floating-point constant.  A CONST_DOUBLE is used whenever the
11611          constant requires more than one word in order to be adequately
11612          represented.  We output CONST_DOUBLEs as blocks.  */
11613       {
11614         enum machine_mode mode = GET_MODE (rtl);
11615
11616         if (SCALAR_FLOAT_MODE_P (mode))
11617           {
11618             unsigned int length = GET_MODE_SIZE (mode);
11619             unsigned char *array = GGC_NEWVEC (unsigned char, length);
11620
11621             insert_float (rtl, array);
11622             add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
11623           }
11624         else
11625           {
11626             /* ??? We really should be using HOST_WIDE_INT throughout.  */
11627             gcc_assert (HOST_BITS_PER_LONG == HOST_BITS_PER_WIDE_INT);
11628
11629             add_AT_long_long (die, DW_AT_const_value,
11630                               CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
11631           }
11632       }
11633       break;
11634
11635     case CONST_VECTOR:
11636       {
11637         enum machine_mode mode = GET_MODE (rtl);
11638         unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
11639         unsigned int length = CONST_VECTOR_NUNITS (rtl);
11640         unsigned char *array = GGC_NEWVEC (unsigned char, length * elt_size);
11641         unsigned int i;
11642         unsigned char *p;
11643
11644         switch (GET_MODE_CLASS (mode))
11645           {
11646           case MODE_VECTOR_INT:
11647             for (i = 0, p = array; i < length; i++, p += elt_size)
11648               {
11649                 rtx elt = CONST_VECTOR_ELT (rtl, i);
11650                 HOST_WIDE_INT lo, hi;
11651
11652                 switch (GET_CODE (elt))
11653                   {
11654                   case CONST_INT:
11655                     lo = INTVAL (elt);
11656                     hi = -(lo < 0);
11657                     break;
11658
11659                   case CONST_DOUBLE:
11660                     lo = CONST_DOUBLE_LOW (elt);
11661                     hi = CONST_DOUBLE_HIGH (elt);
11662                     break;
11663
11664                   default:
11665                     gcc_unreachable ();
11666                   }
11667
11668                 if (elt_size <= sizeof (HOST_WIDE_INT))
11669                   insert_int (lo, elt_size, p);
11670                 else
11671                   {
11672                     unsigned char *p0 = p;
11673                     unsigned char *p1 = p + sizeof (HOST_WIDE_INT);
11674
11675                     gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
11676                     if (WORDS_BIG_ENDIAN)
11677                       {
11678                         p0 = p1;
11679                         p1 = p;
11680                       }
11681                     insert_int (lo, sizeof (HOST_WIDE_INT), p0);
11682                     insert_int (hi, sizeof (HOST_WIDE_INT), p1);
11683                   }
11684               }
11685             break;
11686
11687           case MODE_VECTOR_FLOAT:
11688             for (i = 0, p = array; i < length; i++, p += elt_size)
11689               {
11690                 rtx elt = CONST_VECTOR_ELT (rtl, i);
11691                 insert_float (elt, p);
11692               }
11693             break;
11694
11695           default:
11696             gcc_unreachable ();
11697           }
11698
11699         add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
11700       }
11701       break;
11702
11703     case CONST_STRING:
11704       add_AT_string (die, DW_AT_const_value, XSTR (rtl, 0));
11705       break;
11706
11707     case SYMBOL_REF:
11708     case LABEL_REF:
11709     case CONST:
11710       add_AT_addr (die, DW_AT_const_value, rtl);
11711       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
11712       break;
11713
11714     case PLUS:
11715       /* In cases where an inlined instance of an inline function is passed
11716          the address of an `auto' variable (which is local to the caller) we
11717          can get a situation where the DECL_RTL of the artificial local
11718          variable (for the inlining) which acts as a stand-in for the
11719          corresponding formal parameter (of the inline function) will look
11720          like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
11721          exactly a compile-time constant expression, but it isn't the address
11722          of the (artificial) local variable either.  Rather, it represents the
11723          *value* which the artificial local variable always has during its
11724          lifetime.  We currently have no way to represent such quasi-constant
11725          values in Dwarf, so for now we just punt and generate nothing.  */
11726       break;
11727
11728     default:
11729       /* No other kinds of rtx should be possible here.  */
11730       gcc_unreachable ();
11731     }
11732
11733 }
11734
11735 /* Determine whether the evaluation of EXPR references any variables
11736    or functions which aren't otherwise used (and therefore may not be
11737    output).  */
11738 static tree
11739 reference_to_unused (tree * tp, int * walk_subtrees,
11740                      void * data ATTRIBUTE_UNUSED)
11741 {
11742   if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
11743     *walk_subtrees = 0;
11744
11745   if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
11746       && ! TREE_ASM_WRITTEN (*tp))
11747     return *tp;
11748   /* ???  The C++ FE emits debug information for using decls, so
11749      putting gcc_unreachable here falls over.  See PR31899.  For now
11750      be conservative.  */
11751   else if (!cgraph_global_info_ready
11752            && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
11753     return *tp;
11754   else if (DECL_P (*tp) && TREE_CODE (*tp) == VAR_DECL)
11755     {
11756       struct varpool_node *node = varpool_node (*tp);
11757       if (!node->needed)
11758         return *tp;
11759     }
11760   else if (DECL_P (*tp) && TREE_CODE (*tp) == FUNCTION_DECL
11761            && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
11762     {
11763       struct cgraph_node *node = cgraph_node (*tp);
11764       if (node->process || TREE_ASM_WRITTEN (*tp))
11765         return *tp;
11766     }
11767   else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
11768     return *tp;
11769
11770   return NULL_TREE;
11771 }
11772
11773 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
11774    for use in a later add_const_value_attribute call.  */
11775
11776 static rtx
11777 rtl_for_decl_init (tree init, tree type)
11778 {
11779   rtx rtl = NULL_RTX;
11780
11781   /* If a variable is initialized with a string constant without embedded
11782      zeros, build CONST_STRING.  */
11783   if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
11784     {
11785       tree enttype = TREE_TYPE (type);
11786       tree domain = TYPE_DOMAIN (type);
11787       enum machine_mode mode = TYPE_MODE (enttype);
11788
11789       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
11790           && domain
11791           && integer_zerop (TYPE_MIN_VALUE (domain))
11792           && compare_tree_int (TYPE_MAX_VALUE (domain),
11793                                TREE_STRING_LENGTH (init) - 1) == 0
11794           && ((size_t) TREE_STRING_LENGTH (init)
11795               == strlen (TREE_STRING_POINTER (init)) + 1))
11796         rtl = gen_rtx_CONST_STRING (VOIDmode,
11797                                     ggc_strdup (TREE_STRING_POINTER (init)));
11798     }
11799   /* Other aggregates, and complex values, could be represented using
11800      CONCAT: FIXME!  */
11801   else if (AGGREGATE_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
11802     ;
11803   /* Vectors only work if their mode is supported by the target.
11804      FIXME: generic vectors ought to work too.  */
11805   else if (TREE_CODE (type) == VECTOR_TYPE && TYPE_MODE (type) == BLKmode)
11806     ;
11807   /* If the initializer is something that we know will expand into an
11808      immediate RTL constant, expand it now.  We must be careful not to
11809      reference variables which won't be output.  */
11810   else if (initializer_constant_valid_p (init, type)
11811            && ! walk_tree (&init, reference_to_unused, NULL, NULL))
11812     {
11813       /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
11814          possible.  */
11815       if (TREE_CODE (type) == VECTOR_TYPE)
11816         switch (TREE_CODE (init))
11817           {
11818           case VECTOR_CST:
11819             break;
11820           case CONSTRUCTOR:
11821             if (TREE_CONSTANT (init))
11822               {
11823                 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
11824                 bool constant_p = true;
11825                 tree value;
11826                 unsigned HOST_WIDE_INT ix;
11827
11828                 /* Even when ctor is constant, it might contain non-*_CST
11829                    elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
11830                    belong into VECTOR_CST nodes.  */
11831                 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
11832                   if (!CONSTANT_CLASS_P (value))
11833                     {
11834                       constant_p = false;
11835                       break;
11836                     }
11837
11838                 if (constant_p)
11839                   {
11840                     init = build_vector_from_ctor (type, elts);
11841                     break;
11842                   }
11843               }
11844             /* FALLTHRU */
11845
11846           default:
11847             return NULL;
11848           }
11849
11850       rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
11851
11852       /* If expand_expr returns a MEM, it wasn't immediate.  */
11853       gcc_assert (!rtl || !MEM_P (rtl));
11854     }
11855
11856   return rtl;
11857 }
11858
11859 /* Generate RTL for the variable DECL to represent its location.  */
11860
11861 static rtx
11862 rtl_for_decl_location (tree decl)
11863 {
11864   rtx rtl;
11865
11866   /* Here we have to decide where we are going to say the parameter "lives"
11867      (as far as the debugger is concerned).  We only have a couple of
11868      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
11869
11870      DECL_RTL normally indicates where the parameter lives during most of the
11871      activation of the function.  If optimization is enabled however, this
11872      could be either NULL or else a pseudo-reg.  Both of those cases indicate
11873      that the parameter doesn't really live anywhere (as far as the code
11874      generation parts of GCC are concerned) during most of the function's
11875      activation.  That will happen (for example) if the parameter is never
11876      referenced within the function.
11877
11878      We could just generate a location descriptor here for all non-NULL
11879      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
11880      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
11881      where DECL_RTL is NULL or is a pseudo-reg.
11882
11883      Note however that we can only get away with using DECL_INCOMING_RTL as
11884      a backup substitute for DECL_RTL in certain limited cases.  In cases
11885      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
11886      we can be sure that the parameter was passed using the same type as it is
11887      declared to have within the function, and that its DECL_INCOMING_RTL
11888      points us to a place where a value of that type is passed.
11889
11890      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
11891      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
11892      because in these cases DECL_INCOMING_RTL points us to a value of some
11893      type which is *different* from the type of the parameter itself.  Thus,
11894      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
11895      such cases, the debugger would end up (for example) trying to fetch a
11896      `float' from a place which actually contains the first part of a
11897      `double'.  That would lead to really incorrect and confusing
11898      output at debug-time.
11899
11900      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
11901      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
11902      are a couple of exceptions however.  On little-endian machines we can
11903      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
11904      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
11905      an integral type that is smaller than TREE_TYPE (decl). These cases arise
11906      when (on a little-endian machine) a non-prototyped function has a
11907      parameter declared to be of type `short' or `char'.  In such cases,
11908      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
11909      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
11910      passed `int' value.  If the debugger then uses that address to fetch
11911      a `short' or a `char' (on a little-endian machine) the result will be
11912      the correct data, so we allow for such exceptional cases below.
11913
11914      Note that our goal here is to describe the place where the given formal
11915      parameter lives during most of the function's activation (i.e. between the
11916      end of the prologue and the start of the epilogue).  We'll do that as best
11917      as we can. Note however that if the given formal parameter is modified
11918      sometime during the execution of the function, then a stack backtrace (at
11919      debug-time) will show the function as having been called with the *new*
11920      value rather than the value which was originally passed in.  This happens
11921      rarely enough that it is not a major problem, but it *is* a problem, and
11922      I'd like to fix it.
11923
11924      A future version of dwarf2out.c may generate two additional attributes for
11925      any given DW_TAG_formal_parameter DIE which will describe the "passed
11926      type" and the "passed location" for the given formal parameter in addition
11927      to the attributes we now generate to indicate the "declared type" and the
11928      "active location" for each parameter.  This additional set of attributes
11929      could be used by debuggers for stack backtraces. Separately, note that
11930      sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
11931      This happens (for example) for inlined-instances of inline function formal
11932      parameters which are never referenced.  This really shouldn't be
11933      happening.  All PARM_DECL nodes should get valid non-NULL
11934      DECL_INCOMING_RTL values.  FIXME.  */
11935
11936   /* Use DECL_RTL as the "location" unless we find something better.  */
11937   rtl = DECL_RTL_IF_SET (decl);
11938
11939   /* When generating abstract instances, ignore everything except
11940      constants, symbols living in memory, and symbols living in
11941      fixed registers.  */
11942   if (! reload_completed)
11943     {
11944       if (rtl
11945           && (CONSTANT_P (rtl)
11946               || (MEM_P (rtl)
11947                   && CONSTANT_P (XEXP (rtl, 0)))
11948               || (REG_P (rtl)
11949                   && TREE_CODE (decl) == VAR_DECL
11950                   && TREE_STATIC (decl))))
11951         {
11952           rtl = targetm.delegitimize_address (rtl);
11953           return rtl;
11954         }
11955       rtl = NULL_RTX;
11956     }
11957   else if (TREE_CODE (decl) == PARM_DECL)
11958     {
11959       if (rtl == NULL_RTX || is_pseudo_reg (rtl))
11960         {
11961           tree declared_type = TREE_TYPE (decl);
11962           tree passed_type = DECL_ARG_TYPE (decl);
11963           enum machine_mode dmode = TYPE_MODE (declared_type);
11964           enum machine_mode pmode = TYPE_MODE (passed_type);
11965
11966           /* This decl represents a formal parameter which was optimized out.
11967              Note that DECL_INCOMING_RTL may be NULL in here, but we handle
11968              all cases where (rtl == NULL_RTX) just below.  */
11969           if (dmode == pmode)
11970             rtl = DECL_INCOMING_RTL (decl);
11971           else if (SCALAR_INT_MODE_P (dmode)
11972                    && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
11973                    && DECL_INCOMING_RTL (decl))
11974             {
11975               rtx inc = DECL_INCOMING_RTL (decl);
11976               if (REG_P (inc))
11977                 rtl = inc;
11978               else if (MEM_P (inc))
11979                 {
11980                   if (BYTES_BIG_ENDIAN)
11981                     rtl = adjust_address_nv (inc, dmode,
11982                                              GET_MODE_SIZE (pmode)
11983                                              - GET_MODE_SIZE (dmode));
11984                   else
11985                     rtl = inc;
11986                 }
11987             }
11988         }
11989
11990       /* If the parm was passed in registers, but lives on the stack, then
11991          make a big endian correction if the mode of the type of the
11992          parameter is not the same as the mode of the rtl.  */
11993       /* ??? This is the same series of checks that are made in dbxout.c before
11994          we reach the big endian correction code there.  It isn't clear if all
11995          of these checks are necessary here, but keeping them all is the safe
11996          thing to do.  */
11997       else if (MEM_P (rtl)
11998                && XEXP (rtl, 0) != const0_rtx
11999                && ! CONSTANT_P (XEXP (rtl, 0))
12000                /* Not passed in memory.  */
12001                && !MEM_P (DECL_INCOMING_RTL (decl))
12002                /* Not passed by invisible reference.  */
12003                && (!REG_P (XEXP (rtl, 0))
12004                    || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
12005                    || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
12006 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
12007                    || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
12008 #endif
12009                      )
12010                /* Big endian correction check.  */
12011                && BYTES_BIG_ENDIAN
12012                && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
12013                && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
12014                    < UNITS_PER_WORD))
12015         {
12016           int offset = (UNITS_PER_WORD
12017                         - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
12018
12019           rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
12020                              plus_constant (XEXP (rtl, 0), offset));
12021         }
12022     }
12023   else if (TREE_CODE (decl) == VAR_DECL
12024            && rtl
12025            && MEM_P (rtl)
12026            && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
12027            && BYTES_BIG_ENDIAN)
12028     {
12029       int rsize = GET_MODE_SIZE (GET_MODE (rtl));
12030       int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
12031
12032       /* If a variable is declared "register" yet is smaller than
12033          a register, then if we store the variable to memory, it
12034          looks like we're storing a register-sized value, when in
12035          fact we are not.  We need to adjust the offset of the
12036          storage location to reflect the actual value's bytes,
12037          else gdb will not be able to display it.  */
12038       if (rsize > dsize)
12039         rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
12040                            plus_constant (XEXP (rtl, 0), rsize-dsize));
12041     }
12042
12043   /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
12044      and will have been substituted directly into all expressions that use it.
12045      C does not have such a concept, but C++ and other languages do.  */
12046   if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
12047     rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
12048
12049   if (rtl)
12050     rtl = targetm.delegitimize_address (rtl);
12051
12052   /* If we don't look past the constant pool, we risk emitting a
12053      reference to a constant pool entry that isn't referenced from
12054      code, and thus is not emitted.  */
12055   if (rtl)
12056     rtl = avoid_constant_pool_reference (rtl);
12057
12058   return rtl;
12059 }
12060
12061 /* We need to figure out what section we should use as the base for the
12062    address ranges where a given location is valid.
12063    1. If this particular DECL has a section associated with it, use that.
12064    2. If this function has a section associated with it, use that.
12065    3. Otherwise, use the text section.
12066    XXX: If you split a variable across multiple sections, we won't notice.  */
12067
12068 static const char *
12069 secname_for_decl (const_tree decl)
12070 {
12071   const char *secname;
12072
12073   if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
12074     {
12075       tree sectree = DECL_SECTION_NAME (decl);
12076       secname = TREE_STRING_POINTER (sectree);
12077     }
12078   else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
12079     {
12080       tree sectree = DECL_SECTION_NAME (current_function_decl);
12081       secname = TREE_STRING_POINTER (sectree);
12082     }
12083   else if (cfun && in_cold_section_p)
12084     secname = crtl->subsections.cold_section_label;
12085   else
12086     secname = text_section_label;
12087
12088   return secname;
12089 }
12090
12091 /* Check whether decl is a Fortran COMMON symbol.  If not, NULL_TREE is
12092    returned.  If so, the decl for the COMMON block is returned, and the
12093    value is the offset into the common block for the symbol.  */
12094
12095 static tree
12096 fortran_common (tree decl, HOST_WIDE_INT *value)
12097 {
12098   tree val_expr, cvar;
12099   enum machine_mode mode;
12100   HOST_WIDE_INT bitsize, bitpos;
12101   tree offset;
12102   int volatilep = 0, unsignedp = 0;
12103
12104   /* If the decl isn't a VAR_DECL, or if it isn't public or static, or if
12105      it does not have a value (the offset into the common area), or if it
12106      is thread local (as opposed to global) then it isn't common, and shouldn't
12107      be handled as such.  */
12108   if (TREE_CODE (decl) != VAR_DECL
12109       || !TREE_PUBLIC (decl)
12110       || !TREE_STATIC (decl)
12111       || !DECL_HAS_VALUE_EXPR_P (decl)
12112       || !is_fortran ())
12113     return NULL_TREE;
12114
12115   val_expr = DECL_VALUE_EXPR (decl);
12116   if (TREE_CODE (val_expr) != COMPONENT_REF)
12117     return NULL_TREE;
12118
12119   cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
12120                               &mode, &unsignedp, &volatilep, true);
12121
12122   if (cvar == NULL_TREE
12123       || TREE_CODE (cvar) != VAR_DECL
12124       || DECL_ARTIFICIAL (cvar)
12125       || !TREE_PUBLIC (cvar))
12126     return NULL_TREE;
12127
12128   *value = 0;
12129   if (offset != NULL)
12130     {
12131       if (!host_integerp (offset, 0))
12132         return NULL_TREE;
12133       *value = tree_low_cst (offset, 0);
12134     }
12135   if (bitpos != 0)
12136     *value += bitpos / BITS_PER_UNIT;
12137
12138   return cvar;
12139 }
12140
12141 /* Dereference a location expression LOC if DECL is passed by invisible
12142    reference.  */
12143
12144 static dw_loc_descr_ref
12145 loc_by_reference (dw_loc_descr_ref loc, tree decl)
12146 {
12147   HOST_WIDE_INT size;
12148   enum dwarf_location_atom op;
12149
12150   if (loc == NULL)
12151     return NULL;
12152
12153   if ((TREE_CODE (decl) != PARM_DECL
12154        && TREE_CODE (decl) != RESULT_DECL
12155        && TREE_CODE (decl) != VAR_DECL)
12156       || !DECL_BY_REFERENCE (decl))
12157     return loc;
12158
12159   /* If loc is DW_OP_reg{0...31,x}, don't add DW_OP_deref, instead
12160      change it into corresponding DW_OP_breg{0...31,x} 0.  Then the
12161      location expression is considered to be address of a memory location,
12162      rather than the register itself.  */
12163   if (((loc->dw_loc_opc >= DW_OP_reg0 && loc->dw_loc_opc <= DW_OP_reg31)
12164        || loc->dw_loc_opc == DW_OP_regx)
12165       && (loc->dw_loc_next == NULL
12166           || (loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_uninit
12167               && loc->dw_loc_next->dw_loc_next == NULL)))
12168     {
12169       if (loc->dw_loc_opc == DW_OP_regx)
12170         {
12171           loc->dw_loc_opc = DW_OP_bregx;
12172           loc->dw_loc_oprnd2.v.val_int = 0;
12173         }
12174       else
12175         {
12176           loc->dw_loc_opc
12177             = (enum dwarf_location_atom)
12178               (loc->dw_loc_opc + (DW_OP_breg0 - DW_OP_reg0));
12179           loc->dw_loc_oprnd1.v.val_int = 0;
12180         }
12181       return loc;
12182     }
12183
12184   size = int_size_in_bytes (TREE_TYPE (decl));
12185   if (size > DWARF2_ADDR_SIZE || size == -1)
12186     return 0;
12187   else if (size == DWARF2_ADDR_SIZE)
12188     op = DW_OP_deref;
12189   else
12190     op = DW_OP_deref_size;
12191   add_loc_descr (&loc, new_loc_descr (op, size, 0));
12192   return loc;
12193 }
12194
12195 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
12196    data attribute for a variable or a parameter.  We generate the
12197    DW_AT_const_value attribute only in those cases where the given variable
12198    or parameter does not have a true "location" either in memory or in a
12199    register.  This can happen (for example) when a constant is passed as an
12200    actual argument in a call to an inline function.  (It's possible that
12201    these things can crop up in other ways also.)  Note that one type of
12202    constant value which can be passed into an inlined function is a constant
12203    pointer.  This can happen for example if an actual argument in an inlined
12204    function call evaluates to a compile-time constant address.  */
12205
12206 static void
12207 add_location_or_const_value_attribute (dw_die_ref die, tree decl,
12208                                        enum dwarf_attribute attr)
12209 {
12210   rtx rtl;
12211   dw_loc_descr_ref descr;
12212   var_loc_list *loc_list;
12213   struct var_loc_node *node;
12214   if (TREE_CODE (decl) == ERROR_MARK)
12215     return;
12216
12217   gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
12218               || TREE_CODE (decl) == RESULT_DECL);
12219
12220   /* See if we possibly have multiple locations for this variable.  */
12221   loc_list = lookup_decl_loc (decl);
12222
12223   /* If it truly has multiple locations, the first and last node will
12224      differ.  */
12225   if (loc_list && loc_list->first != loc_list->last)
12226     {
12227       const char *endname, *secname;
12228       dw_loc_list_ref list;
12229       rtx varloc;
12230       enum var_init_status initialized;
12231
12232       /* Now that we know what section we are using for a base,
12233          actually construct the list of locations.
12234          The first location information is what is passed to the
12235          function that creates the location list, and the remaining
12236          locations just get added on to that list.
12237          Note that we only know the start address for a location
12238          (IE location changes), so to build the range, we use
12239          the range [current location start, next location start].
12240          This means we have to special case the last node, and generate
12241          a range of [last location start, end of function label].  */
12242
12243       node = loc_list->first;
12244       varloc = NOTE_VAR_LOCATION (node->var_loc_note);
12245       secname = secname_for_decl (decl);
12246
12247       if (NOTE_VAR_LOCATION_LOC (node->var_loc_note))
12248         initialized = NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
12249       else
12250         initialized = VAR_INIT_STATUS_INITIALIZED;
12251
12252       descr = loc_by_reference (loc_descriptor (varloc, initialized), decl);
12253       list = new_loc_list (descr, node->label, node->next->label, secname, 1);
12254       node = node->next;
12255
12256       for (; node->next; node = node->next)
12257         if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
12258           {
12259             /* The variable has a location between NODE->LABEL and
12260                NODE->NEXT->LABEL.  */
12261             enum var_init_status initialized =
12262               NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
12263             varloc = NOTE_VAR_LOCATION (node->var_loc_note);
12264             descr = loc_by_reference (loc_descriptor (varloc, initialized),
12265                                       decl);
12266             add_loc_descr_to_loc_list (&list, descr,
12267                                        node->label, node->next->label, secname);
12268           }
12269
12270       /* If the variable has a location at the last label
12271          it keeps its location until the end of function.  */
12272       if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
12273         {
12274           char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
12275           enum var_init_status initialized =
12276             NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
12277
12278           varloc = NOTE_VAR_LOCATION (node->var_loc_note);
12279           if (!current_function_decl)
12280             endname = text_end_label;
12281           else
12282             {
12283               ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
12284                                            current_function_funcdef_no);
12285               endname = ggc_strdup (label_id);
12286             }
12287           descr = loc_by_reference (loc_descriptor (varloc, initialized),
12288                                     decl);
12289           add_loc_descr_to_loc_list (&list, descr,
12290                                      node->label, endname, secname);
12291         }
12292
12293       /* Finally, add the location list to the DIE, and we are done.  */
12294       add_AT_loc_list (die, attr, list);
12295       return;
12296     }
12297
12298   /* Try to get some constant RTL for this decl, and use that as the value of
12299      the location.  */
12300
12301   rtl = rtl_for_decl_location (decl);
12302   if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING))
12303     {
12304       add_const_value_attribute (die, rtl);
12305       return;
12306     }
12307
12308   /* If we have tried to generate the location otherwise, and it
12309      didn't work out (we wouldn't be here if we did), and we have a one entry
12310      location list, try generating a location from that.  */
12311   if (loc_list && loc_list->first)
12312     {
12313       enum var_init_status status;
12314       node = loc_list->first;
12315       status = NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
12316       descr = loc_descriptor (NOTE_VAR_LOCATION (node->var_loc_note), status);
12317       if (descr)
12318         {
12319           descr = loc_by_reference (descr, decl);
12320           add_AT_location_description (die, attr, descr);
12321           return;
12322         }
12323     }
12324
12325   /* We couldn't get any rtl, so try directly generating the location
12326      description from the tree.  */
12327   descr = loc_descriptor_from_tree (decl);
12328   if (descr)
12329     {
12330       descr = loc_by_reference (descr, decl);
12331       add_AT_location_description (die, attr, descr);
12332       return;
12333     }
12334   /* None of that worked, so it must not really have a location;
12335      try adding a constant value attribute from the DECL_INITIAL.  */
12336   tree_add_const_value_attribute (die, decl);
12337 }
12338
12339 /* Add VARIABLE and DIE into deferred locations list.  */
12340
12341 static void
12342 defer_location (tree variable, dw_die_ref die)
12343 {
12344   deferred_locations entry;
12345   entry.variable = variable;
12346   entry.die = die;
12347   VEC_safe_push (deferred_locations, gc, deferred_locations_list, &entry);
12348 }
12349
12350 /* Helper function for tree_add_const_value_attribute.  Natively encode
12351    initializer INIT into an array.  Return true if successful.  */
12352
12353 static bool
12354 native_encode_initializer (tree init, unsigned char *array, int size)
12355 {
12356   tree type;
12357
12358   if (init == NULL_TREE)
12359     return false;
12360
12361   STRIP_NOPS (init);
12362   switch (TREE_CODE (init))
12363     {
12364     case STRING_CST:
12365       type = TREE_TYPE (init);
12366       if (TREE_CODE (type) == ARRAY_TYPE)
12367         {
12368           tree enttype = TREE_TYPE (type);
12369           enum machine_mode mode = TYPE_MODE (enttype);
12370
12371           if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
12372             return false;
12373           if (int_size_in_bytes (type) != size)
12374             return false;
12375           if (size > TREE_STRING_LENGTH (init))
12376             {
12377               memcpy (array, TREE_STRING_POINTER (init),
12378                       TREE_STRING_LENGTH (init));
12379               memset (array + TREE_STRING_LENGTH (init),
12380                       '\0', size - TREE_STRING_LENGTH (init));
12381             }
12382           else
12383             memcpy (array, TREE_STRING_POINTER (init), size);
12384           return true;
12385         }
12386       return false;
12387     case CONSTRUCTOR:
12388       type = TREE_TYPE (init);
12389       if (int_size_in_bytes (type) != size)
12390         return false;
12391       if (TREE_CODE (type) == ARRAY_TYPE)
12392         {
12393           HOST_WIDE_INT min_index;
12394           unsigned HOST_WIDE_INT cnt;
12395           int curpos = 0, fieldsize;
12396           constructor_elt *ce;
12397
12398           if (TYPE_DOMAIN (type) == NULL_TREE
12399               || !host_integerp (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0))
12400             return false;
12401
12402           fieldsize = int_size_in_bytes (TREE_TYPE (type));
12403           if (fieldsize <= 0)
12404             return false;
12405
12406           min_index = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0);
12407           memset (array, '\0', size);
12408           for (cnt = 0;
12409                VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce);
12410                cnt++)
12411             {
12412               tree val = ce->value;
12413               tree index = ce->index;
12414               int pos = curpos;
12415               if (index && TREE_CODE (index) == RANGE_EXPR)
12416                 pos = (tree_low_cst (TREE_OPERAND (index, 0), 0) - min_index)
12417                       * fieldsize;
12418               else if (index)
12419                 pos = (tree_low_cst (index, 0) - min_index) * fieldsize;
12420
12421               if (val)
12422                 {
12423                   STRIP_NOPS (val);
12424                   if (!native_encode_initializer (val, array + pos, fieldsize))
12425                     return false;
12426                 }
12427               curpos = pos + fieldsize;
12428               if (index && TREE_CODE (index) == RANGE_EXPR)
12429                 {
12430                   int count = tree_low_cst (TREE_OPERAND (index, 1), 0)
12431                               - tree_low_cst (TREE_OPERAND (index, 0), 0);
12432                   while (count > 0)
12433                     {
12434                       if (val)
12435                         memcpy (array + curpos, array + pos, fieldsize);
12436                       curpos += fieldsize;
12437                     }
12438                 }
12439               gcc_assert (curpos <= size);
12440             }
12441           return true;
12442         }
12443       else if (TREE_CODE (type) == RECORD_TYPE
12444                || TREE_CODE (type) == UNION_TYPE)
12445         {
12446           tree field = NULL_TREE;
12447           unsigned HOST_WIDE_INT cnt;
12448           constructor_elt *ce;
12449
12450           if (int_size_in_bytes (type) != size)
12451             return false;
12452
12453           if (TREE_CODE (type) == RECORD_TYPE)
12454             field = TYPE_FIELDS (type);
12455
12456           for (cnt = 0;
12457                VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce);
12458                cnt++, field = field ? TREE_CHAIN (field) : 0)
12459             {
12460               tree val = ce->value;
12461               int pos, fieldsize;
12462
12463               if (ce->index != 0)
12464                 field = ce->index;
12465
12466               if (val)
12467                 STRIP_NOPS (val);
12468
12469               if (field == NULL_TREE || DECL_BIT_FIELD (field))
12470                 return false;
12471
12472               if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
12473                   && TYPE_DOMAIN (TREE_TYPE (field))
12474                   && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
12475                 return false;
12476               else if (DECL_SIZE_UNIT (field) == NULL_TREE
12477                        || !host_integerp (DECL_SIZE_UNIT (field), 0))
12478                 return false;
12479               fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 0);
12480               pos = int_byte_position (field);
12481               gcc_assert (pos + fieldsize <= size);
12482               if (val
12483                   && !native_encode_initializer (val, array + pos, fieldsize))
12484                 return false;
12485             }
12486           return true;
12487         }
12488       return false;
12489     case VIEW_CONVERT_EXPR:
12490     case NON_LVALUE_EXPR:
12491       return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
12492     default:
12493       return native_encode_expr (init, array, size) == size;
12494     }
12495 }
12496
12497 /* If we don't have a copy of this variable in memory for some reason (such
12498    as a C++ member constant that doesn't have an out-of-line definition),
12499    we should tell the debugger about the constant value.  */
12500
12501 static void
12502 tree_add_const_value_attribute (dw_die_ref var_die, tree decl)
12503 {
12504   tree init;
12505   tree type = TREE_TYPE (decl);
12506   rtx rtl;
12507
12508   if (TREE_CODE (decl) != VAR_DECL && TREE_CODE (decl) != CONST_DECL)
12509     return;
12510
12511   init = DECL_INITIAL (decl);
12512   if (TREE_READONLY (decl) && ! TREE_THIS_VOLATILE (decl) && init)
12513     /* OK */;
12514   else
12515     return;
12516
12517   rtl = rtl_for_decl_init (init, type);
12518   if (rtl)
12519     add_const_value_attribute (var_die, rtl);
12520   /* If the host and target are sane, try harder.  */
12521   else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
12522            && initializer_constant_valid_p (init, type))
12523     {
12524       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
12525       if (size > 0 && (int) size == size)
12526         {
12527           unsigned char *array = GGC_CNEWVEC (unsigned char, size);
12528
12529           if (native_encode_initializer (init, array, size))
12530             add_AT_vec (var_die, DW_AT_const_value, size, 1, array);
12531         }
12532     }
12533 }
12534
12535 /* Convert the CFI instructions for the current function into a
12536    location list.  This is used for DW_AT_frame_base when we targeting
12537    a dwarf2 consumer that does not support the dwarf3
12538    DW_OP_call_frame_cfa.  OFFSET is a constant to be added to all CFA
12539    expressions.  */
12540
12541 static dw_loc_list_ref
12542 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
12543 {
12544   dw_fde_ref fde;
12545   dw_loc_list_ref list, *list_tail;
12546   dw_cfi_ref cfi;
12547   dw_cfa_location last_cfa, next_cfa;
12548   const char *start_label, *last_label, *section;
12549   dw_cfa_location remember;
12550
12551   fde = current_fde ();
12552   gcc_assert (fde != NULL);
12553
12554   section = secname_for_decl (current_function_decl);
12555   list_tail = &list;
12556   list = NULL;
12557
12558   memset (&next_cfa, 0, sizeof (next_cfa));
12559   next_cfa.reg = INVALID_REGNUM;
12560   remember = next_cfa;
12561
12562   start_label = fde->dw_fde_begin;
12563
12564   /* ??? Bald assumption that the CIE opcode list does not contain
12565      advance opcodes.  */
12566   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
12567     lookup_cfa_1 (cfi, &next_cfa, &remember);
12568
12569   last_cfa = next_cfa;
12570   last_label = start_label;
12571
12572   for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
12573     switch (cfi->dw_cfi_opc)
12574       {
12575       case DW_CFA_set_loc:
12576       case DW_CFA_advance_loc1:
12577       case DW_CFA_advance_loc2:
12578       case DW_CFA_advance_loc4:
12579         if (!cfa_equal_p (&last_cfa, &next_cfa))
12580           {
12581             *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
12582                                        start_label, last_label, section,
12583                                        list == NULL);
12584
12585             list_tail = &(*list_tail)->dw_loc_next;
12586             last_cfa = next_cfa;
12587             start_label = last_label;
12588           }
12589         last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
12590         break;
12591
12592       case DW_CFA_advance_loc:
12593         /* The encoding is complex enough that we should never emit this.  */
12594         gcc_unreachable ();
12595
12596       default:
12597         lookup_cfa_1 (cfi, &next_cfa, &remember);
12598         break;
12599       }
12600
12601   if (!cfa_equal_p (&last_cfa, &next_cfa))
12602     {
12603       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
12604                                  start_label, last_label, section,
12605                                  list == NULL);
12606       list_tail = &(*list_tail)->dw_loc_next;
12607       start_label = last_label;
12608     }
12609   *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
12610                              start_label, fde->dw_fde_end, section,
12611                              list == NULL);
12612
12613   return list;
12614 }
12615
12616 /* Compute a displacement from the "steady-state frame pointer" to the
12617    frame base (often the same as the CFA), and store it in
12618    frame_pointer_fb_offset.  OFFSET is added to the displacement
12619    before the latter is negated.  */
12620
12621 static void
12622 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
12623 {
12624   rtx reg, elim;
12625
12626 #ifdef FRAME_POINTER_CFA_OFFSET
12627   reg = frame_pointer_rtx;
12628   offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
12629 #else
12630   reg = arg_pointer_rtx;
12631   offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
12632 #endif
12633
12634   elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
12635   if (GET_CODE (elim) == PLUS)
12636     {
12637       offset += INTVAL (XEXP (elim, 1));
12638       elim = XEXP (elim, 0);
12639     }
12640
12641   gcc_assert ((SUPPORTS_STACK_ALIGNMENT
12642                && (elim == hard_frame_pointer_rtx
12643                    || elim == stack_pointer_rtx))
12644               || elim == (frame_pointer_needed
12645                           ? hard_frame_pointer_rtx
12646                           : stack_pointer_rtx));
12647
12648   frame_pointer_fb_offset = -offset;
12649 }
12650
12651 /* Generate a DW_AT_name attribute given some string value to be included as
12652    the value of the attribute.  */
12653
12654 static void
12655 add_name_attribute (dw_die_ref die, const char *name_string)
12656 {
12657   if (name_string != NULL && *name_string != 0)
12658     {
12659       if (demangle_name_func)
12660         name_string = (*demangle_name_func) (name_string);
12661
12662       add_AT_string (die, DW_AT_name, name_string);
12663     }
12664 }
12665
12666 /* Generate a DW_AT_comp_dir attribute for DIE.  */
12667
12668 static void
12669 add_comp_dir_attribute (dw_die_ref die)
12670 {
12671   const char *wd = get_src_pwd ();
12672   if (wd != NULL)
12673     add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
12674 }
12675
12676 /* Given a tree node describing an array bound (either lower or upper) output
12677    a representation for that bound.  */
12678
12679 static void
12680 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
12681 {
12682   switch (TREE_CODE (bound))
12683     {
12684     case ERROR_MARK:
12685       return;
12686
12687     /* All fixed-bounds are represented by INTEGER_CST nodes.  */
12688     case INTEGER_CST:
12689       if (! host_integerp (bound, 0)
12690           || (bound_attr == DW_AT_lower_bound
12691               && (((is_c_family () || is_java ()) &&  integer_zerop (bound))
12692                   || (is_fortran () && integer_onep (bound)))))
12693         /* Use the default.  */
12694         ;
12695       else
12696         add_AT_unsigned (subrange_die, bound_attr, tree_low_cst (bound, 0));
12697       break;
12698
12699     CASE_CONVERT:
12700     case VIEW_CONVERT_EXPR:
12701       add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
12702       break;
12703
12704     case SAVE_EXPR:
12705       break;
12706
12707     case VAR_DECL:
12708     case PARM_DECL:
12709     case RESULT_DECL:
12710       {
12711         dw_die_ref decl_die = lookup_decl_die (bound);
12712         dw_loc_descr_ref loc;
12713
12714         /* ??? Can this happen, or should the variable have been bound
12715            first?  Probably it can, since I imagine that we try to create
12716            the types of parameters in the order in which they exist in
12717            the list, and won't have created a forward reference to a
12718            later parameter.  */
12719         if (decl_die != NULL)
12720           add_AT_die_ref (subrange_die, bound_attr, decl_die);
12721         else
12722           {
12723             loc = loc_descriptor_from_tree_1 (bound, 0);
12724             add_AT_location_description (subrange_die, bound_attr, loc);
12725           }
12726         break;
12727       }
12728
12729     default:
12730       {
12731         /* Otherwise try to create a stack operation procedure to
12732            evaluate the value of the array bound.  */
12733
12734         dw_die_ref ctx, decl_die;
12735         dw_loc_descr_ref loc;
12736
12737         loc = loc_descriptor_from_tree (bound);
12738         if (loc == NULL)
12739           break;
12740
12741         if (current_function_decl == 0)
12742           ctx = comp_unit_die;
12743         else
12744           ctx = lookup_decl_die (current_function_decl);
12745
12746         decl_die = new_die (DW_TAG_variable, ctx, bound);
12747         add_AT_flag (decl_die, DW_AT_artificial, 1);
12748         add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
12749         add_AT_loc (decl_die, DW_AT_location, loc);
12750
12751         add_AT_die_ref (subrange_die, bound_attr, decl_die);
12752         break;
12753       }
12754     }
12755 }
12756
12757 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
12758    possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
12759    Note that the block of subscript information for an array type also
12760    includes information about the element type of the given array type.  */
12761
12762 static void
12763 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
12764 {
12765   unsigned dimension_number;
12766   tree lower, upper;
12767   dw_die_ref subrange_die;
12768
12769   for (dimension_number = 0;
12770        TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
12771        type = TREE_TYPE (type), dimension_number++)
12772     {
12773       tree domain = TYPE_DOMAIN (type);
12774
12775       if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
12776         break;
12777
12778       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
12779          and (in GNU C only) variable bounds.  Handle all three forms
12780          here.  */
12781       subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
12782       if (domain)
12783         {
12784           /* We have an array type with specified bounds.  */
12785           lower = TYPE_MIN_VALUE (domain);
12786           upper = TYPE_MAX_VALUE (domain);
12787
12788           /* Define the index type.  */
12789           if (TREE_TYPE (domain))
12790             {
12791               /* ??? This is probably an Ada unnamed subrange type.  Ignore the
12792                  TREE_TYPE field.  We can't emit debug info for this
12793                  because it is an unnamed integral type.  */
12794               if (TREE_CODE (domain) == INTEGER_TYPE
12795                   && TYPE_NAME (domain) == NULL_TREE
12796                   && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
12797                   && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
12798                 ;
12799               else
12800                 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
12801                                     type_die);
12802             }
12803
12804           /* ??? If upper is NULL, the array has unspecified length,
12805              but it does have a lower bound.  This happens with Fortran
12806                dimension arr(N:*)
12807              Since the debugger is definitely going to need to know N
12808              to produce useful results, go ahead and output the lower
12809              bound solo, and hope the debugger can cope.  */
12810
12811           add_bound_info (subrange_die, DW_AT_lower_bound, lower);
12812           if (upper)
12813             add_bound_info (subrange_die, DW_AT_upper_bound, upper);
12814         }
12815
12816       /* Otherwise we have an array type with an unspecified length.  The
12817          DWARF-2 spec does not say how to handle this; let's just leave out the
12818          bounds.  */
12819     }
12820 }
12821
12822 static void
12823 add_byte_size_attribute (dw_die_ref die, tree tree_node)
12824 {
12825   unsigned size;
12826
12827   switch (TREE_CODE (tree_node))
12828     {
12829     case ERROR_MARK:
12830       size = 0;
12831       break;
12832     case ENUMERAL_TYPE:
12833     case RECORD_TYPE:
12834     case UNION_TYPE:
12835     case QUAL_UNION_TYPE:
12836       size = int_size_in_bytes (tree_node);
12837       break;
12838     case FIELD_DECL:
12839       /* For a data member of a struct or union, the DW_AT_byte_size is
12840          generally given as the number of bytes normally allocated for an
12841          object of the *declared* type of the member itself.  This is true
12842          even for bit-fields.  */
12843       size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
12844       break;
12845     default:
12846       gcc_unreachable ();
12847     }
12848
12849   /* Note that `size' might be -1 when we get to this point.  If it is, that
12850      indicates that the byte size of the entity in question is variable.  We
12851      have no good way of expressing this fact in Dwarf at the present time,
12852      so just let the -1 pass on through.  */
12853   add_AT_unsigned (die, DW_AT_byte_size, size);
12854 }
12855
12856 /* For a FIELD_DECL node which represents a bit-field, output an attribute
12857    which specifies the distance in bits from the highest order bit of the
12858    "containing object" for the bit-field to the highest order bit of the
12859    bit-field itself.
12860
12861    For any given bit-field, the "containing object" is a hypothetical object
12862    (of some integral or enum type) within which the given bit-field lives.  The
12863    type of this hypothetical "containing object" is always the same as the
12864    declared type of the individual bit-field itself.  The determination of the
12865    exact location of the "containing object" for a bit-field is rather
12866    complicated.  It's handled by the `field_byte_offset' function (above).
12867
12868    Note that it is the size (in bytes) of the hypothetical "containing object"
12869    which will be given in the DW_AT_byte_size attribute for this bit-field.
12870    (See `byte_size_attribute' above).  */
12871
12872 static inline void
12873 add_bit_offset_attribute (dw_die_ref die, tree decl)
12874 {
12875   HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
12876   tree type = DECL_BIT_FIELD_TYPE (decl);
12877   HOST_WIDE_INT bitpos_int;
12878   HOST_WIDE_INT highest_order_object_bit_offset;
12879   HOST_WIDE_INT highest_order_field_bit_offset;
12880   HOST_WIDE_INT unsigned bit_offset;
12881
12882   /* Must be a field and a bit field.  */
12883   gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
12884
12885   /* We can't yet handle bit-fields whose offsets are variable, so if we
12886      encounter such things, just return without generating any attribute
12887      whatsoever.  Likewise for variable or too large size.  */
12888   if (! host_integerp (bit_position (decl), 0)
12889       || ! host_integerp (DECL_SIZE (decl), 1))
12890     return;
12891
12892   bitpos_int = int_bit_position (decl);
12893
12894   /* Note that the bit offset is always the distance (in bits) from the
12895      highest-order bit of the "containing object" to the highest-order bit of
12896      the bit-field itself.  Since the "high-order end" of any object or field
12897      is different on big-endian and little-endian machines, the computation
12898      below must take account of these differences.  */
12899   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
12900   highest_order_field_bit_offset = bitpos_int;
12901
12902   if (! BYTES_BIG_ENDIAN)
12903     {
12904       highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
12905       highest_order_object_bit_offset += simple_type_size_in_bits (type);
12906     }
12907
12908   bit_offset
12909     = (! BYTES_BIG_ENDIAN
12910        ? highest_order_object_bit_offset - highest_order_field_bit_offset
12911        : highest_order_field_bit_offset - highest_order_object_bit_offset);
12912
12913   add_AT_unsigned (die, DW_AT_bit_offset, bit_offset);
12914 }
12915
12916 /* For a FIELD_DECL node which represents a bit field, output an attribute
12917    which specifies the length in bits of the given field.  */
12918
12919 static inline void
12920 add_bit_size_attribute (dw_die_ref die, tree decl)
12921 {
12922   /* Must be a field and a bit field.  */
12923   gcc_assert (TREE_CODE (decl) == FIELD_DECL
12924               && DECL_BIT_FIELD_TYPE (decl));
12925
12926   if (host_integerp (DECL_SIZE (decl), 1))
12927     add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
12928 }
12929
12930 /* If the compiled language is ANSI C, then add a 'prototyped'
12931    attribute, if arg types are given for the parameters of a function.  */
12932
12933 static inline void
12934 add_prototyped_attribute (dw_die_ref die, tree func_type)
12935 {
12936   if (get_AT_unsigned (comp_unit_die, DW_AT_language) == DW_LANG_C89
12937       && TYPE_ARG_TYPES (func_type) != NULL)
12938     add_AT_flag (die, DW_AT_prototyped, 1);
12939 }
12940
12941 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
12942    by looking in either the type declaration or object declaration
12943    equate table.  */
12944
12945 static inline dw_die_ref
12946 add_abstract_origin_attribute (dw_die_ref die, tree origin)
12947 {
12948   dw_die_ref origin_die = NULL;
12949
12950   if (TREE_CODE (origin) != FUNCTION_DECL)
12951     {
12952       /* We may have gotten separated from the block for the inlined
12953          function, if we're in an exception handler or some such; make
12954          sure that the abstract function has been written out.
12955
12956          Doing this for nested functions is wrong, however; functions are
12957          distinct units, and our context might not even be inline.  */
12958       tree fn = origin;
12959
12960       if (TYPE_P (fn))
12961         fn = TYPE_STUB_DECL (fn);
12962
12963       fn = decl_function_context (fn);
12964       if (fn)
12965         dwarf2out_abstract_function (fn);
12966     }
12967
12968   if (DECL_P (origin))
12969     origin_die = lookup_decl_die (origin);
12970   else if (TYPE_P (origin))
12971     origin_die = lookup_type_die (origin);
12972
12973   /* XXX: Functions that are never lowered don't always have correct block
12974      trees (in the case of java, they simply have no block tree, in some other
12975      languages).  For these functions, there is nothing we can really do to
12976      output correct debug info for inlined functions in all cases.  Rather
12977      than die, we'll just produce deficient debug info now, in that we will
12978      have variables without a proper abstract origin.  In the future, when all
12979      functions are lowered, we should re-add a gcc_assert (origin_die)
12980      here.  */
12981
12982   if (origin_die)
12983     add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
12984   return origin_die;
12985 }
12986
12987 /* We do not currently support the pure_virtual attribute.  */
12988
12989 static inline void
12990 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
12991 {
12992   if (DECL_VINDEX (func_decl))
12993     {
12994       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
12995
12996       if (host_integerp (DECL_VINDEX (func_decl), 0))
12997         add_AT_loc (die, DW_AT_vtable_elem_location,
12998                     new_loc_descr (DW_OP_constu,
12999                                    tree_low_cst (DECL_VINDEX (func_decl), 0),
13000                                    0));
13001
13002       /* GNU extension: Record what type this method came from originally.  */
13003       if (debug_info_level > DINFO_LEVEL_TERSE)
13004         add_AT_die_ref (die, DW_AT_containing_type,
13005                         lookup_type_die (DECL_CONTEXT (func_decl)));
13006     }
13007 }
13008 \f
13009 /* Add source coordinate attributes for the given decl.  */
13010
13011 static void
13012 add_src_coords_attributes (dw_die_ref die, tree decl)
13013 {
13014   expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
13015
13016   add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
13017   add_AT_unsigned (die, DW_AT_decl_line, s.line);
13018 }
13019
13020 /* Add a DW_AT_name attribute and source coordinate attribute for the
13021    given decl, but only if it actually has a name.  */
13022
13023 static void
13024 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
13025 {
13026   tree decl_name;
13027
13028   decl_name = DECL_NAME (decl);
13029   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
13030     {
13031       add_name_attribute (die, dwarf2_name (decl, 0));
13032       if (! DECL_ARTIFICIAL (decl))
13033         add_src_coords_attributes (die, decl);
13034
13035       if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
13036           && TREE_PUBLIC (decl)
13037           && !DECL_ABSTRACT (decl)
13038           && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
13039           && !is_fortran ())
13040         {
13041           /* Defer until we have an assembler name set.  */
13042           if (!DECL_ASSEMBLER_NAME_SET_P (decl))
13043             {
13044               limbo_die_node *asm_name;
13045
13046               asm_name = GGC_CNEW (limbo_die_node);
13047               asm_name->die = die;
13048               asm_name->created_for = decl;
13049               asm_name->next = deferred_asm_name;
13050               deferred_asm_name = asm_name;
13051             }
13052           else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
13053             add_AT_string (die, DW_AT_MIPS_linkage_name,
13054                            IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
13055         }
13056     }
13057
13058 #ifdef VMS_DEBUGGING_INFO
13059   /* Get the function's name, as described by its RTL.  This may be different
13060      from the DECL_NAME name used in the source file.  */
13061   if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
13062     {
13063       add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
13064                    XEXP (DECL_RTL (decl), 0));
13065       VEC_safe_push (tree, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
13066     }
13067 #endif
13068 }
13069
13070 /* Push a new declaration scope.  */
13071
13072 static void
13073 push_decl_scope (tree scope)
13074 {
13075   VEC_safe_push (tree, gc, decl_scope_table, scope);
13076 }
13077
13078 /* Pop a declaration scope.  */
13079
13080 static inline void
13081 pop_decl_scope (void)
13082 {
13083   VEC_pop (tree, decl_scope_table);
13084 }
13085
13086 /* Return the DIE for the scope that immediately contains this type.
13087    Non-named types get global scope.  Named types nested in other
13088    types get their containing scope if it's open, or global scope
13089    otherwise.  All other types (i.e. function-local named types) get
13090    the current active scope.  */
13091
13092 static dw_die_ref
13093 scope_die_for (tree t, dw_die_ref context_die)
13094 {
13095   dw_die_ref scope_die = NULL;
13096   tree containing_scope;
13097   int i;
13098
13099   /* Non-types always go in the current scope.  */
13100   gcc_assert (TYPE_P (t));
13101
13102   containing_scope = TYPE_CONTEXT (t);
13103
13104   /* Use the containing namespace if it was passed in (for a declaration).  */
13105   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
13106     {
13107       if (context_die == lookup_decl_die (containing_scope))
13108         /* OK */;
13109       else
13110         containing_scope = NULL_TREE;
13111     }
13112
13113   /* Ignore function type "scopes" from the C frontend.  They mean that
13114      a tagged type is local to a parmlist of a function declarator, but
13115      that isn't useful to DWARF.  */
13116   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
13117     containing_scope = NULL_TREE;
13118
13119   if (containing_scope == NULL_TREE)
13120     scope_die = comp_unit_die;
13121   else if (TYPE_P (containing_scope))
13122     {
13123       /* For types, we can just look up the appropriate DIE.  But
13124          first we check to see if we're in the middle of emitting it
13125          so we know where the new DIE should go.  */
13126       for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
13127         if (VEC_index (tree, decl_scope_table, i) == containing_scope)
13128           break;
13129
13130       if (i < 0)
13131         {
13132           gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
13133                       || TREE_ASM_WRITTEN (containing_scope));
13134
13135           /* If none of the current dies are suitable, we get file scope.  */
13136           scope_die = comp_unit_die;
13137         }
13138       else
13139         scope_die = lookup_type_die (containing_scope);
13140     }
13141   else
13142     scope_die = context_die;
13143
13144   return scope_die;
13145 }
13146
13147 /* Returns nonzero if CONTEXT_DIE is internal to a function.  */
13148
13149 static inline int
13150 local_scope_p (dw_die_ref context_die)
13151 {
13152   for (; context_die; context_die = context_die->die_parent)
13153     if (context_die->die_tag == DW_TAG_inlined_subroutine
13154         || context_die->die_tag == DW_TAG_subprogram)
13155       return 1;
13156
13157   return 0;
13158 }
13159
13160 /* Returns nonzero if CONTEXT_DIE is a class.  */
13161
13162 static inline int
13163 class_scope_p (dw_die_ref context_die)
13164 {
13165   return (context_die
13166           && (context_die->die_tag == DW_TAG_structure_type
13167               || context_die->die_tag == DW_TAG_class_type
13168               || context_die->die_tag == DW_TAG_interface_type
13169               || context_die->die_tag == DW_TAG_union_type));
13170 }
13171
13172 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
13173    whether or not to treat a DIE in this context as a declaration.  */
13174
13175 static inline int
13176 class_or_namespace_scope_p (dw_die_ref context_die)
13177 {
13178   return (class_scope_p (context_die)
13179           || (context_die && context_die->die_tag == DW_TAG_namespace));
13180 }
13181
13182 /* Many forms of DIEs require a "type description" attribute.  This
13183    routine locates the proper "type descriptor" die for the type given
13184    by 'type', and adds a DW_AT_type attribute below the given die.  */
13185
13186 static void
13187 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
13188                     int decl_volatile, dw_die_ref context_die)
13189 {
13190   enum tree_code code  = TREE_CODE (type);
13191   dw_die_ref type_die  = NULL;
13192
13193   /* ??? If this type is an unnamed subrange type of an integral, floating-point
13194      or fixed-point type, use the inner type.  This is because we have no
13195      support for unnamed types in base_type_die.  This can happen if this is
13196      an Ada subrange type.  Correct solution is emit a subrange type die.  */
13197   if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
13198       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
13199     type = TREE_TYPE (type), code = TREE_CODE (type);
13200
13201   if (code == ERROR_MARK
13202       /* Handle a special case.  For functions whose return type is void, we
13203          generate *no* type attribute.  (Note that no object may have type
13204          `void', so this only applies to function return types).  */
13205       || code == VOID_TYPE)
13206     return;
13207
13208   type_die = modified_type_die (type,
13209                                 decl_const || TYPE_READONLY (type),
13210                                 decl_volatile || TYPE_VOLATILE (type),
13211                                 context_die);
13212
13213   if (type_die != NULL)
13214     add_AT_die_ref (object_die, DW_AT_type, type_die);
13215 }
13216
13217 /* Given an object die, add the calling convention attribute for the
13218    function call type.  */
13219 static void
13220 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
13221 {
13222   enum dwarf_calling_convention value = DW_CC_normal;
13223
13224   value = ((enum dwarf_calling_convention)
13225            targetm.dwarf_calling_convention (TREE_TYPE (decl)));
13226
13227   /* DWARF doesn't provide a way to identify a program's source-level
13228      entry point.  DW_AT_calling_convention attributes are only meant
13229      to describe functions' calling conventions.  However, lacking a
13230      better way to signal the Fortran main program, we use this for the
13231      time being, following existing custom.  */
13232   if (is_fortran ()
13233       && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
13234     value = DW_CC_program;
13235
13236   /* Only add the attribute if the backend requests it, and
13237      is not DW_CC_normal.  */
13238   if (value && (value != DW_CC_normal))
13239     add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
13240 }
13241
13242 /* Given a tree pointer to a struct, class, union, or enum type node, return
13243    a pointer to the (string) tag name for the given type, or zero if the type
13244    was declared without a tag.  */
13245
13246 static const char *
13247 type_tag (const_tree type)
13248 {
13249   const char *name = 0;
13250
13251   if (TYPE_NAME (type) != 0)
13252     {
13253       tree t = 0;
13254
13255       /* Find the IDENTIFIER_NODE for the type name.  */
13256       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
13257         t = TYPE_NAME (type);
13258
13259       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
13260          a TYPE_DECL node, regardless of whether or not a `typedef' was
13261          involved.  */
13262       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
13263                && ! DECL_IGNORED_P (TYPE_NAME (type)))
13264         {
13265           /* We want to be extra verbose.  Don't call dwarf_name if
13266              DECL_NAME isn't set.  The default hook for decl_printable_name
13267              doesn't like that, and in this context it's correct to return
13268              0, instead of "<anonymous>" or the like.  */
13269           if (DECL_NAME (TYPE_NAME (type)))
13270             name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
13271         }
13272
13273       /* Now get the name as a string, or invent one.  */
13274       if (!name && t != 0)
13275         name = IDENTIFIER_POINTER (t);
13276     }
13277
13278   return (name == 0 || *name == '\0') ? 0 : name;
13279 }
13280
13281 /* Return the type associated with a data member, make a special check
13282    for bit field types.  */
13283
13284 static inline tree
13285 member_declared_type (const_tree member)
13286 {
13287   return (DECL_BIT_FIELD_TYPE (member)
13288           ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
13289 }
13290
13291 /* Get the decl's label, as described by its RTL. This may be different
13292    from the DECL_NAME name used in the source file.  */
13293
13294 #if 0
13295 static const char *
13296 decl_start_label (tree decl)
13297 {
13298   rtx x;
13299   const char *fnname;
13300
13301   x = DECL_RTL (decl);
13302   gcc_assert (MEM_P (x));
13303
13304   x = XEXP (x, 0);
13305   gcc_assert (GET_CODE (x) == SYMBOL_REF);
13306
13307   fnname = XSTR (x, 0);
13308   return fnname;
13309 }
13310 #endif
13311 \f
13312 /* These routines generate the internal representation of the DIE's for
13313    the compilation unit.  Debugging information is collected by walking
13314    the declaration trees passed in from dwarf2out_decl().  */
13315
13316 static void
13317 gen_array_type_die (tree type, dw_die_ref context_die)
13318 {
13319   dw_die_ref scope_die = scope_die_for (type, context_die);
13320   dw_die_ref array_die;
13321
13322   /* GNU compilers represent multidimensional array types as sequences of one
13323      dimensional array types whose element types are themselves array types.
13324      We sometimes squish that down to a single array_type DIE with multiple
13325      subscripts in the Dwarf debugging info.  The draft Dwarf specification
13326      say that we are allowed to do this kind of compression in C, because
13327      there is no difference between an array of arrays and a multidimensional
13328      array.  We don't do this for Ada to remain as close as possible to the
13329      actual representation, which is especially important against the language
13330      flexibilty wrt arrays of variable size.  */
13331
13332   bool collapse_nested_arrays = !is_ada ();
13333   tree element_type;
13334
13335   /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
13336      DW_TAG_string_type doesn't have DW_AT_type attribute).  */
13337   if (TYPE_STRING_FLAG (type)
13338       && TREE_CODE (type) == ARRAY_TYPE
13339       && is_fortran ()
13340       && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
13341     {
13342       HOST_WIDE_INT size;
13343
13344       array_die = new_die (DW_TAG_string_type, scope_die, type);
13345       add_name_attribute (array_die, type_tag (type));
13346       equate_type_number_to_die (type, array_die);
13347       size = int_size_in_bytes (type);
13348       if (size >= 0)
13349         add_AT_unsigned (array_die, DW_AT_byte_size, size);
13350       else if (TYPE_DOMAIN (type) != NULL_TREE
13351                && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
13352                && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
13353         {
13354           tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
13355           dw_loc_descr_ref loc = loc_descriptor_from_tree (szdecl);
13356
13357           size = int_size_in_bytes (TREE_TYPE (szdecl));
13358           if (loc && size > 0)
13359             {
13360               add_AT_loc (array_die, DW_AT_string_length, loc);
13361               if (size != DWARF2_ADDR_SIZE)
13362                 add_AT_unsigned (array_die, DW_AT_byte_size, size);
13363             }
13364         }
13365       return;
13366     }
13367
13368   /* ??? The SGI dwarf reader fails for array of array of enum types
13369      (e.g. const enum machine_mode insn_operand_mode[2][10]) unless the inner
13370      array type comes before the outer array type.  We thus call gen_type_die
13371      before we new_die and must prevent nested array types collapsing for this
13372      target.  */
13373
13374 #ifdef MIPS_DEBUGGING_INFO
13375   gen_type_die (TREE_TYPE (type), context_die);
13376   collapse_nested_arrays = false;
13377 #endif
13378
13379   array_die = new_die (DW_TAG_array_type, scope_die, type);
13380   add_name_attribute (array_die, type_tag (type));
13381   equate_type_number_to_die (type, array_die);
13382
13383   if (TREE_CODE (type) == VECTOR_TYPE)
13384     {
13385       /* The frontend feeds us a representation for the vector as a struct
13386          containing an array.  Pull out the array type.  */
13387       type = TREE_TYPE (TYPE_FIELDS (TYPE_DEBUG_REPRESENTATION_TYPE (type)));
13388       add_AT_flag (array_die, DW_AT_GNU_vector, 1);
13389     }
13390
13391   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
13392   if (is_fortran ()
13393       && TREE_CODE (type) == ARRAY_TYPE
13394       && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
13395       && !TYPE_STRING_FLAG (TREE_TYPE (type)))
13396     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
13397
13398 #if 0
13399   /* We default the array ordering.  SDB will probably do
13400      the right things even if DW_AT_ordering is not present.  It's not even
13401      an issue until we start to get into multidimensional arrays anyway.  If
13402      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
13403      then we'll have to put the DW_AT_ordering attribute back in.  (But if
13404      and when we find out that we need to put these in, we will only do so
13405      for multidimensional arrays.  */
13406   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
13407 #endif
13408
13409 #ifdef MIPS_DEBUGGING_INFO
13410   /* The SGI compilers handle arrays of unknown bound by setting
13411      AT_declaration and not emitting any subrange DIEs.  */
13412   if (! TYPE_DOMAIN (type))
13413     add_AT_flag (array_die, DW_AT_declaration, 1);
13414   else
13415 #endif
13416     add_subscript_info (array_die, type, collapse_nested_arrays);
13417
13418   /* Add representation of the type of the elements of this array type and
13419      emit the corresponding DIE if we haven't done it already.  */  
13420   element_type = TREE_TYPE (type);
13421   if (collapse_nested_arrays)
13422     while (TREE_CODE (element_type) == ARRAY_TYPE)
13423       {
13424         if (TYPE_STRING_FLAG (element_type) && is_fortran ())
13425           break;
13426         element_type = TREE_TYPE (element_type);
13427       }
13428
13429 #ifndef MIPS_DEBUGGING_INFO
13430   gen_type_die (element_type, context_die);
13431 #endif
13432
13433   add_type_attribute (array_die, element_type, 0, 0, context_die);
13434
13435   if (get_AT (array_die, DW_AT_name))
13436     add_pubtype (type, array_die);
13437 }
13438
13439 static dw_loc_descr_ref
13440 descr_info_loc (tree val, tree base_decl)
13441 {
13442   HOST_WIDE_INT size;
13443   dw_loc_descr_ref loc, loc2;
13444   enum dwarf_location_atom op;
13445
13446   if (val == base_decl)
13447     return new_loc_descr (DW_OP_push_object_address, 0, 0);
13448
13449   switch (TREE_CODE (val))
13450     {
13451     CASE_CONVERT:
13452       return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
13453     case VAR_DECL:
13454       return loc_descriptor_from_tree_1 (val, 0);
13455     case INTEGER_CST:
13456       if (host_integerp (val, 0))
13457         return int_loc_descriptor (tree_low_cst (val, 0));
13458       break;
13459     case INDIRECT_REF:
13460       size = int_size_in_bytes (TREE_TYPE (val));
13461       if (size < 0)
13462         break;
13463       loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
13464       if (!loc)
13465         break;
13466       if (size == DWARF2_ADDR_SIZE)
13467         add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
13468       else
13469         add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
13470       return loc;
13471     case POINTER_PLUS_EXPR:
13472     case PLUS_EXPR:
13473       if (host_integerp (TREE_OPERAND (val, 1), 1)
13474           && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
13475              < 16384)
13476         {
13477           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
13478           if (!loc)
13479             break;
13480           loc_descr_plus_const (&loc, tree_low_cst (TREE_OPERAND (val, 1), 0));
13481         }
13482       else
13483         {
13484           op = DW_OP_plus;
13485         do_binop:
13486           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
13487           if (!loc)
13488             break;
13489           loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
13490           if (!loc2)
13491             break;
13492           add_loc_descr (&loc, loc2);
13493           add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
13494         }
13495       return loc;
13496     case MINUS_EXPR:
13497       op = DW_OP_minus;
13498       goto do_binop;
13499     case MULT_EXPR:
13500       op = DW_OP_mul;
13501       goto do_binop;
13502     case EQ_EXPR:
13503       op = DW_OP_eq;
13504       goto do_binop;
13505     case NE_EXPR:
13506       op = DW_OP_ne;
13507       goto do_binop;
13508     default:
13509       break;
13510     }
13511   return NULL;
13512 }
13513
13514 static void
13515 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
13516                       tree val, tree base_decl)
13517 {
13518   dw_loc_descr_ref loc;
13519
13520   if (host_integerp (val, 0))
13521     {
13522       add_AT_unsigned (die, attr, tree_low_cst (val, 0));
13523       return;
13524     }
13525
13526   loc = descr_info_loc (val, base_decl);
13527   if (!loc)
13528     return;
13529
13530   add_AT_loc (die, attr, loc);
13531 }
13532
13533 /* This routine generates DIE for array with hidden descriptor, details
13534    are filled into *info by a langhook.  */
13535
13536 static void
13537 gen_descr_array_type_die (tree type, struct array_descr_info *info,
13538                           dw_die_ref context_die)
13539 {
13540   dw_die_ref scope_die = scope_die_for (type, context_die);
13541   dw_die_ref array_die;
13542   int dim;
13543
13544   array_die = new_die (DW_TAG_array_type, scope_die, type);
13545   add_name_attribute (array_die, type_tag (type));
13546   equate_type_number_to_die (type, array_die);
13547
13548   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
13549   if (is_fortran ()
13550       && info->ndimensions >= 2)
13551     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
13552
13553   if (info->data_location)
13554     add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
13555                           info->base_decl);
13556   if (info->associated)
13557     add_descr_info_field (array_die, DW_AT_associated, info->associated,
13558                           info->base_decl);
13559   if (info->allocated)
13560     add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
13561                           info->base_decl);
13562
13563   for (dim = 0; dim < info->ndimensions; dim++)
13564     {
13565       dw_die_ref subrange_die
13566         = new_die (DW_TAG_subrange_type, array_die, NULL);
13567
13568       if (info->dimen[dim].lower_bound)
13569         {
13570           /* If it is the default value, omit it.  */
13571           if ((is_c_family () || is_java ())
13572               && integer_zerop (info->dimen[dim].lower_bound))
13573             ;
13574           else if (is_fortran ()
13575                    && integer_onep (info->dimen[dim].lower_bound))
13576             ;
13577           else
13578             add_descr_info_field (subrange_die, DW_AT_lower_bound,
13579                                   info->dimen[dim].lower_bound,
13580                                   info->base_decl);
13581         }
13582       if (info->dimen[dim].upper_bound)
13583         add_descr_info_field (subrange_die, DW_AT_upper_bound,
13584                               info->dimen[dim].upper_bound,
13585                               info->base_decl);
13586       if (info->dimen[dim].stride)
13587         add_descr_info_field (subrange_die, DW_AT_byte_stride,
13588                               info->dimen[dim].stride,
13589                               info->base_decl);
13590     }
13591
13592   gen_type_die (info->element_type, context_die);
13593   add_type_attribute (array_die, info->element_type, 0, 0, context_die);
13594
13595   if (get_AT (array_die, DW_AT_name))
13596     add_pubtype (type, array_die);
13597 }
13598
13599 #if 0
13600 static void
13601 gen_entry_point_die (tree decl, dw_die_ref context_die)
13602 {
13603   tree origin = decl_ultimate_origin (decl);
13604   dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
13605
13606   if (origin != NULL)
13607     add_abstract_origin_attribute (decl_die, origin);
13608   else
13609     {
13610       add_name_and_src_coords_attributes (decl_die, decl);
13611       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
13612                           0, 0, context_die);
13613     }
13614
13615   if (DECL_ABSTRACT (decl))
13616     equate_decl_number_to_die (decl, decl_die);
13617   else
13618     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
13619 }
13620 #endif
13621
13622 /* Walk through the list of incomplete types again, trying once more to
13623    emit full debugging info for them.  */
13624
13625 static void
13626 retry_incomplete_types (void)
13627 {
13628   int i;
13629
13630   for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
13631     gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die);
13632 }
13633
13634 /* Determine what tag to use for a record type.  */
13635
13636 static enum dwarf_tag
13637 record_type_tag (tree type)
13638 {
13639   if (! lang_hooks.types.classify_record)
13640     return DW_TAG_structure_type;
13641
13642   switch (lang_hooks.types.classify_record (type))
13643     {
13644     case RECORD_IS_STRUCT:
13645       return DW_TAG_structure_type;
13646
13647     case RECORD_IS_CLASS:
13648       return DW_TAG_class_type;
13649
13650     case RECORD_IS_INTERFACE:
13651       return DW_TAG_interface_type;
13652
13653     default:
13654       gcc_unreachable ();
13655     }
13656 }
13657
13658 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
13659    include all of the information about the enumeration values also. Each
13660    enumerated type name/value is listed as a child of the enumerated type
13661    DIE.  */
13662
13663 static dw_die_ref
13664 gen_enumeration_type_die (tree type, dw_die_ref context_die)
13665 {
13666   dw_die_ref type_die = lookup_type_die (type);
13667
13668   if (type_die == NULL)
13669     {
13670       type_die = new_die (DW_TAG_enumeration_type,
13671                           scope_die_for (type, context_die), type);
13672       equate_type_number_to_die (type, type_die);
13673       add_name_attribute (type_die, type_tag (type));
13674     }
13675   else if (! TYPE_SIZE (type))
13676     return type_die;
13677   else
13678     remove_AT (type_die, DW_AT_declaration);
13679
13680   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
13681      given enum type is incomplete, do not generate the DW_AT_byte_size
13682      attribute or the DW_AT_element_list attribute.  */
13683   if (TYPE_SIZE (type))
13684     {
13685       tree link;
13686
13687       TREE_ASM_WRITTEN (type) = 1;
13688       add_byte_size_attribute (type_die, type);
13689       if (TYPE_STUB_DECL (type) != NULL_TREE)
13690         add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
13691
13692       /* If the first reference to this type was as the return type of an
13693          inline function, then it may not have a parent.  Fix this now.  */
13694       if (type_die->die_parent == NULL)
13695         add_child_die (scope_die_for (type, context_die), type_die);
13696
13697       for (link = TYPE_VALUES (type);
13698            link != NULL; link = TREE_CHAIN (link))
13699         {
13700           dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
13701           tree value = TREE_VALUE (link);
13702
13703           add_name_attribute (enum_die,
13704                               IDENTIFIER_POINTER (TREE_PURPOSE (link)));
13705
13706           if (TREE_CODE (value) == CONST_DECL)
13707             value = DECL_INITIAL (value);
13708
13709           if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
13710             /* DWARF2 does not provide a way of indicating whether or
13711                not enumeration constants are signed or unsigned.  GDB
13712                always assumes the values are signed, so we output all
13713                values as if they were signed.  That means that
13714                enumeration constants with very large unsigned values
13715                will appear to have negative values in the debugger.  */
13716             add_AT_int (enum_die, DW_AT_const_value,
13717                         tree_low_cst (value, tree_int_cst_sgn (value) > 0));
13718         }
13719     }
13720   else
13721     add_AT_flag (type_die, DW_AT_declaration, 1);
13722
13723   if (get_AT (type_die, DW_AT_name))
13724     add_pubtype (type, type_die);
13725
13726   return type_die;
13727 }
13728
13729 /* Generate a DIE to represent either a real live formal parameter decl or to
13730    represent just the type of some formal parameter position in some function
13731    type.
13732
13733    Note that this routine is a bit unusual because its argument may be a
13734    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
13735    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
13736    node.  If it's the former then this function is being called to output a
13737    DIE to represent a formal parameter object (or some inlining thereof).  If
13738    it's the latter, then this function is only being called to output a
13739    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
13740    argument type of some subprogram type.  */
13741
13742 static dw_die_ref
13743 gen_formal_parameter_die (tree node, tree origin, dw_die_ref context_die)
13744 {
13745   tree node_or_origin = node ? node : origin;
13746   dw_die_ref parm_die
13747     = new_die (DW_TAG_formal_parameter, context_die, node);
13748
13749   switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
13750     {
13751     case tcc_declaration:
13752       if (!origin)
13753         origin = decl_ultimate_origin (node);
13754       if (origin != NULL)
13755         add_abstract_origin_attribute (parm_die, origin);
13756       else
13757         {
13758           tree type = TREE_TYPE (node);
13759           add_name_and_src_coords_attributes (parm_die, node);
13760           if (DECL_BY_REFERENCE (node))
13761             add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
13762                                 context_die);
13763           else
13764             add_type_attribute (parm_die, type,
13765                                 TREE_READONLY (node),
13766                                 TREE_THIS_VOLATILE (node),
13767                                 context_die);
13768           if (DECL_ARTIFICIAL (node))
13769             add_AT_flag (parm_die, DW_AT_artificial, 1);
13770         }
13771
13772       if (node && node != origin)
13773         equate_decl_number_to_die (node, parm_die);
13774       if (! DECL_ABSTRACT (node_or_origin))
13775         add_location_or_const_value_attribute (parm_die, node_or_origin,
13776                                                DW_AT_location);
13777
13778       break;
13779
13780     case tcc_type:
13781       /* We were called with some kind of a ..._TYPE node.  */
13782       add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
13783       break;
13784
13785     default:
13786       gcc_unreachable ();
13787     }
13788
13789   return parm_die;
13790 }
13791
13792 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
13793    at the end of an (ANSI prototyped) formal parameters list.  */
13794
13795 static void
13796 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
13797 {
13798   new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
13799 }
13800
13801 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
13802    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
13803    parameters as specified in some function type specification (except for
13804    those which appear as part of a function *definition*).  */
13805
13806 static void
13807 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
13808 {
13809   tree link;
13810   tree formal_type = NULL;
13811   tree first_parm_type;
13812   tree arg;
13813
13814   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
13815     {
13816       arg = DECL_ARGUMENTS (function_or_method_type);
13817       function_or_method_type = TREE_TYPE (function_or_method_type);
13818     }
13819   else
13820     arg = NULL_TREE;
13821
13822   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
13823
13824   /* Make our first pass over the list of formal parameter types and output a
13825      DW_TAG_formal_parameter DIE for each one.  */
13826   for (link = first_parm_type; link; )
13827     {
13828       dw_die_ref parm_die;
13829
13830       formal_type = TREE_VALUE (link);
13831       if (formal_type == void_type_node)
13832         break;
13833
13834       /* Output a (nameless) DIE to represent the formal parameter itself.  */
13835       parm_die = gen_formal_parameter_die (formal_type, NULL, context_die);
13836       if ((TREE_CODE (function_or_method_type) == METHOD_TYPE
13837            && link == first_parm_type)
13838           || (arg && DECL_ARTIFICIAL (arg)))
13839         add_AT_flag (parm_die, DW_AT_artificial, 1);
13840
13841       link = TREE_CHAIN (link);
13842       if (arg)
13843         arg = TREE_CHAIN (arg);
13844     }
13845
13846   /* If this function type has an ellipsis, add a
13847      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
13848   if (formal_type != void_type_node)
13849     gen_unspecified_parameters_die (function_or_method_type, context_die);
13850
13851   /* Make our second (and final) pass over the list of formal parameter types
13852      and output DIEs to represent those types (as necessary).  */
13853   for (link = TYPE_ARG_TYPES (function_or_method_type);
13854        link && TREE_VALUE (link);
13855        link = TREE_CHAIN (link))
13856     gen_type_die (TREE_VALUE (link), context_die);
13857 }
13858
13859 /* We want to generate the DIE for TYPE so that we can generate the
13860    die for MEMBER, which has been defined; we will need to refer back
13861    to the member declaration nested within TYPE.  If we're trying to
13862    generate minimal debug info for TYPE, processing TYPE won't do the
13863    trick; we need to attach the member declaration by hand.  */
13864
13865 static void
13866 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
13867 {
13868   gen_type_die (type, context_die);
13869
13870   /* If we're trying to avoid duplicate debug info, we may not have
13871      emitted the member decl for this function.  Emit it now.  */
13872   if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
13873       && ! lookup_decl_die (member))
13874     {
13875       dw_die_ref type_die;
13876       gcc_assert (!decl_ultimate_origin (member));
13877
13878       push_decl_scope (type);
13879       type_die = lookup_type_die (type);
13880       if (TREE_CODE (member) == FUNCTION_DECL)
13881         gen_subprogram_die (member, type_die);
13882       else if (TREE_CODE (member) == FIELD_DECL)
13883         {
13884           /* Ignore the nameless fields that are used to skip bits but handle
13885              C++ anonymous unions and structs.  */
13886           if (DECL_NAME (member) != NULL_TREE
13887               || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
13888               || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
13889             {
13890               gen_type_die (member_declared_type (member), type_die);
13891               gen_field_die (member, type_die);
13892             }
13893         }
13894       else
13895         gen_variable_die (member, NULL_TREE, type_die);
13896
13897       pop_decl_scope ();
13898     }
13899 }
13900
13901 /* Generate the DWARF2 info for the "abstract" instance of a function which we
13902    may later generate inlined and/or out-of-line instances of.  */
13903
13904 static void
13905 dwarf2out_abstract_function (tree decl)
13906 {
13907   dw_die_ref old_die;
13908   tree save_fn;
13909   tree context;
13910   int was_abstract = DECL_ABSTRACT (decl);
13911
13912   /* Make sure we have the actual abstract inline, not a clone.  */
13913   decl = DECL_ORIGIN (decl);
13914   htab_empty (decl_loc_table);
13915
13916   old_die = lookup_decl_die (decl);
13917   if (old_die && get_AT (old_die, DW_AT_inline))
13918     /* We've already generated the abstract instance.  */
13919     return;
13920
13921   /* Be sure we've emitted the in-class declaration DIE (if any) first, so
13922      we don't get confused by DECL_ABSTRACT.  */
13923   if (debug_info_level > DINFO_LEVEL_TERSE)
13924     {
13925       context = decl_class_context (decl);
13926       if (context)
13927         gen_type_die_for_member
13928           (context, decl, decl_function_context (decl) ? NULL : comp_unit_die);
13929     }
13930
13931   /* Pretend we've just finished compiling this function.  */
13932   save_fn = current_function_decl;
13933   current_function_decl = decl;
13934   push_cfun (DECL_STRUCT_FUNCTION (decl));
13935
13936   set_decl_abstract_flags (decl, 1);
13937   dwarf2out_decl (decl);
13938   if (! was_abstract)
13939     set_decl_abstract_flags (decl, 0);
13940
13941   current_function_decl = save_fn;
13942   pop_cfun ();
13943 }
13944
13945 /* Helper function of premark_used_types() which gets called through
13946    htab_traverse_resize().
13947
13948    Marks the DIE of a given type in *SLOT as perennial, so it never gets
13949    marked as unused by prune_unused_types.  */
13950 static int
13951 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
13952 {
13953   tree type;
13954   dw_die_ref die;
13955
13956   type = (tree) *slot;
13957   die = lookup_type_die (type);
13958   if (die != NULL)
13959     die->die_perennial_p = 1;
13960   return 1;
13961 }
13962
13963 /* Mark all members of used_types_hash as perennial.  */
13964 static void
13965 premark_used_types (void)
13966 {
13967   if (cfun && cfun->used_types_hash)
13968     htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
13969 }
13970
13971 /* Generate a DIE to represent a declared function (either file-scope or
13972    block-local).  */
13973
13974 static void
13975 gen_subprogram_die (tree decl, dw_die_ref context_die)
13976 {
13977   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
13978   tree origin = decl_ultimate_origin (decl);
13979   dw_die_ref subr_die;
13980   tree fn_arg_types;
13981   tree outer_scope;
13982   dw_die_ref old_die = lookup_decl_die (decl);
13983   int declaration = (current_function_decl != decl
13984                      || class_or_namespace_scope_p (context_die));
13985
13986   premark_used_types ();
13987
13988   /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
13989      started to generate the abstract instance of an inline, decided to output
13990      its containing class, and proceeded to emit the declaration of the inline
13991      from the member list for the class.  If so, DECLARATION takes priority;
13992      we'll get back to the abstract instance when done with the class.  */
13993
13994   /* The class-scope declaration DIE must be the primary DIE.  */
13995   if (origin && declaration && class_or_namespace_scope_p (context_die))
13996     {
13997       origin = NULL;
13998       gcc_assert (!old_die);
13999     }
14000
14001   /* Now that the C++ front end lazily declares artificial member fns, we
14002      might need to retrofit the declaration into its class.  */
14003   if (!declaration && !origin && !old_die
14004       && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
14005       && !class_or_namespace_scope_p (context_die)
14006       && debug_info_level > DINFO_LEVEL_TERSE)
14007     old_die = force_decl_die (decl);
14008
14009   if (origin != NULL)
14010     {
14011       gcc_assert (!declaration || local_scope_p (context_die));
14012
14013       /* Fixup die_parent for the abstract instance of a nested
14014          inline function.  */
14015       if (old_die && old_die->die_parent == NULL)
14016         add_child_die (context_die, old_die);
14017
14018       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
14019       add_abstract_origin_attribute (subr_die, origin);
14020     }
14021   else if (old_die)
14022     {
14023       expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
14024       struct dwarf_file_data * file_index = lookup_filename (s.file);
14025
14026       if (!get_AT_flag (old_die, DW_AT_declaration)
14027           /* We can have a normal definition following an inline one in the
14028              case of redefinition of GNU C extern inlines.
14029              It seems reasonable to use AT_specification in this case.  */
14030           && !get_AT (old_die, DW_AT_inline))
14031         {
14032           /* Detect and ignore this case, where we are trying to output
14033              something we have already output.  */
14034           return;
14035         }
14036
14037       /* If the definition comes from the same place as the declaration,
14038          maybe use the old DIE.  We always want the DIE for this function
14039          that has the *_pc attributes to be under comp_unit_die so the
14040          debugger can find it.  We also need to do this for abstract
14041          instances of inlines, since the spec requires the out-of-line copy
14042          to have the same parent.  For local class methods, this doesn't
14043          apply; we just use the old DIE.  */
14044       if ((old_die->die_parent == comp_unit_die || context_die == NULL)
14045           && (DECL_ARTIFICIAL (decl)
14046               || (get_AT_file (old_die, DW_AT_decl_file) == file_index
14047                   && (get_AT_unsigned (old_die, DW_AT_decl_line)
14048                       == (unsigned) s.line))))
14049         {
14050           subr_die = old_die;
14051
14052           /* Clear out the declaration attribute and the formal parameters.
14053              Do not remove all children, because it is possible that this
14054              declaration die was forced using force_decl_die(). In such
14055              cases die that forced declaration die (e.g. TAG_imported_module)
14056              is one of the children that we do not want to remove.  */
14057           remove_AT (subr_die, DW_AT_declaration);
14058           remove_child_TAG (subr_die, DW_TAG_formal_parameter);
14059         }
14060       else
14061         {
14062           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
14063           add_AT_specification (subr_die, old_die);
14064           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
14065             add_AT_file (subr_die, DW_AT_decl_file, file_index);
14066           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
14067             add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
14068         }
14069     }
14070   else
14071     {
14072       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
14073
14074       if (TREE_PUBLIC (decl))
14075         add_AT_flag (subr_die, DW_AT_external, 1);
14076
14077       add_name_and_src_coords_attributes (subr_die, decl);
14078       if (debug_info_level > DINFO_LEVEL_TERSE)
14079         {
14080           add_prototyped_attribute (subr_die, TREE_TYPE (decl));
14081           add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
14082                               0, 0, context_die);
14083         }
14084
14085       add_pure_or_virtual_attribute (subr_die, decl);
14086       if (DECL_ARTIFICIAL (decl))
14087         add_AT_flag (subr_die, DW_AT_artificial, 1);
14088
14089       if (TREE_PROTECTED (decl))
14090         add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_protected);
14091       else if (TREE_PRIVATE (decl))
14092         add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_private);
14093     }
14094
14095   if (declaration)
14096     {
14097       if (!old_die || !get_AT (old_die, DW_AT_inline))
14098         {
14099           add_AT_flag (subr_die, DW_AT_declaration, 1);
14100
14101           /* If this is an explicit function declaration then generate
14102              a DW_AT_explicit attribute.  */
14103           if (lang_hooks.decls.function_decl_explicit_p (decl))
14104             add_AT_flag (subr_die, DW_AT_explicit, 1);
14105
14106           /* The first time we see a member function, it is in the context of
14107              the class to which it belongs.  We make sure of this by emitting
14108              the class first.  The next time is the definition, which is
14109              handled above.  The two may come from the same source text.
14110
14111              Note that force_decl_die() forces function declaration die. It is
14112              later reused to represent definition.  */
14113           equate_decl_number_to_die (decl, subr_die);
14114         }
14115     }
14116   else if (DECL_ABSTRACT (decl))
14117     {
14118       if (DECL_DECLARED_INLINE_P (decl))
14119         {
14120           if (cgraph_function_possibly_inlined_p (decl))
14121             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
14122           else
14123             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
14124         }
14125       else
14126         {
14127           if (cgraph_function_possibly_inlined_p (decl))
14128             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
14129           else
14130             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
14131         }
14132
14133       if (DECL_DECLARED_INLINE_P (decl)
14134           && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
14135         add_AT_flag (subr_die, DW_AT_artificial, 1);
14136
14137       equate_decl_number_to_die (decl, subr_die);
14138     }
14139   else if (!DECL_EXTERNAL (decl))
14140     {
14141       HOST_WIDE_INT cfa_fb_offset;
14142
14143       if (!old_die || !get_AT (old_die, DW_AT_inline))
14144         equate_decl_number_to_die (decl, subr_die);
14145
14146       if (!flag_reorder_blocks_and_partition)
14147         {
14148           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
14149                                        current_function_funcdef_no);
14150           add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
14151           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
14152                                        current_function_funcdef_no);
14153           add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
14154
14155           add_pubname (decl, subr_die);
14156           add_arange (decl, subr_die);
14157         }
14158       else
14159         {  /* Do nothing for now; maybe need to duplicate die, one for
14160               hot section and one for cold section, then use the hot/cold
14161               section begin/end labels to generate the aranges...  */
14162           /*
14163             add_AT_lbl_id (subr_die, DW_AT_low_pc, hot_section_label);
14164             add_AT_lbl_id (subr_die, DW_AT_high_pc, hot_section_end_label);
14165             add_AT_lbl_id (subr_die, DW_AT_lo_user, unlikely_section_label);
14166             add_AT_lbl_id (subr_die, DW_AT_hi_user, cold_section_end_label);
14167
14168             add_pubname (decl, subr_die);
14169             add_arange (decl, subr_die);
14170             add_arange (decl, subr_die);
14171            */
14172         }
14173
14174 #ifdef MIPS_DEBUGGING_INFO
14175       /* Add a reference to the FDE for this routine.  */
14176       add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
14177 #endif
14178
14179       cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
14180
14181       /* We define the "frame base" as the function's CFA.  This is more
14182          convenient for several reasons: (1) It's stable across the prologue
14183          and epilogue, which makes it better than just a frame pointer,
14184          (2) With dwarf3, there exists a one-byte encoding that allows us
14185          to reference the .debug_frame data by proxy, but failing that,
14186          (3) We can at least reuse the code inspection and interpretation
14187          code that determines the CFA position at various points in the
14188          function.  */
14189       if (dwarf_version >= 3)
14190         {
14191           dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
14192           add_AT_loc (subr_die, DW_AT_frame_base, op);
14193         }
14194       else
14195         {
14196           dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
14197           if (list->dw_loc_next)
14198             add_AT_loc_list (subr_die, DW_AT_frame_base, list);
14199           else
14200             add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
14201         }
14202
14203       /* Compute a displacement from the "steady-state frame pointer" to
14204          the CFA.  The former is what all stack slots and argument slots
14205          will reference in the rtl; the later is what we've told the
14206          debugger about.  We'll need to adjust all frame_base references
14207          by this displacement.  */
14208       compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
14209
14210       if (cfun->static_chain_decl)
14211         add_AT_location_description (subr_die, DW_AT_static_link,
14212                  loc_descriptor_from_tree (cfun->static_chain_decl));
14213     }
14214
14215   /* Now output descriptions of the arguments for this function. This gets
14216      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
14217      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
14218      `...' at the end of the formal parameter list.  In order to find out if
14219      there was a trailing ellipsis or not, we must instead look at the type
14220      associated with the FUNCTION_DECL.  This will be a node of type
14221      FUNCTION_TYPE. If the chain of type nodes hanging off of this
14222      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
14223      an ellipsis at the end.  */
14224
14225   /* In the case where we are describing a mere function declaration, all we
14226      need to do here (and all we *can* do here) is to describe the *types* of
14227      its formal parameters.  */
14228   if (debug_info_level <= DINFO_LEVEL_TERSE)
14229     ;
14230   else if (declaration)
14231     gen_formal_types_die (decl, subr_die);
14232   else
14233     {
14234       /* Generate DIEs to represent all known formal parameters.  */
14235       tree arg_decls = DECL_ARGUMENTS (decl);
14236       tree parm;
14237
14238       /* When generating DIEs, generate the unspecified_parameters DIE
14239          instead if we come across the arg "__builtin_va_alist" */
14240       for (parm = arg_decls; parm; parm = TREE_CHAIN (parm))
14241         if (TREE_CODE (parm) == PARM_DECL)
14242           {
14243             if (DECL_NAME (parm)
14244                 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (parm)),
14245                             "__builtin_va_alist"))
14246               gen_unspecified_parameters_die (parm, subr_die);
14247             else
14248               gen_decl_die (parm, NULL, subr_die);
14249           }
14250
14251       /* Decide whether we need an unspecified_parameters DIE at the end.
14252          There are 2 more cases to do this for: 1) the ansi ... declaration -
14253          this is detectable when the end of the arg list is not a
14254          void_type_node 2) an unprototyped function declaration (not a
14255          definition).  This just means that we have no info about the
14256          parameters at all.  */
14257       fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
14258       if (fn_arg_types != NULL)
14259         {
14260           /* This is the prototyped case, check for....  */
14261           if (TREE_VALUE (tree_last (fn_arg_types)) != void_type_node)
14262             gen_unspecified_parameters_die (decl, subr_die);
14263         }
14264       else if (DECL_INITIAL (decl) == NULL_TREE)
14265         gen_unspecified_parameters_die (decl, subr_die);
14266     }
14267
14268   /* Output Dwarf info for all of the stuff within the body of the function
14269      (if it has one - it may be just a declaration).  */
14270   outer_scope = DECL_INITIAL (decl);
14271
14272   /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
14273      a function.  This BLOCK actually represents the outermost binding contour
14274      for the function, i.e. the contour in which the function's formal
14275      parameters and labels get declared. Curiously, it appears that the front
14276      end doesn't actually put the PARM_DECL nodes for the current function onto
14277      the BLOCK_VARS list for this outer scope, but are strung off of the
14278      DECL_ARGUMENTS list for the function instead.
14279
14280      The BLOCK_VARS list for the `outer_scope' does provide us with a list of
14281      the LABEL_DECL nodes for the function however, and we output DWARF info
14282      for those in decls_for_scope.  Just within the `outer_scope' there will be
14283      a BLOCK node representing the function's outermost pair of curly braces,
14284      and any blocks used for the base and member initializers of a C++
14285      constructor function.  */
14286   if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
14287     {
14288       /* Emit a DW_TAG_variable DIE for a named return value.  */
14289       if (DECL_NAME (DECL_RESULT (decl)))
14290         gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
14291
14292       current_function_has_inlines = 0;
14293       decls_for_scope (outer_scope, subr_die, 0);
14294
14295 #if 0 && defined (MIPS_DEBUGGING_INFO)
14296       if (current_function_has_inlines)
14297         {
14298           add_AT_flag (subr_die, DW_AT_MIPS_has_inlines, 1);
14299           if (! comp_unit_has_inlines)
14300             {
14301               add_AT_flag (comp_unit_die, DW_AT_MIPS_has_inlines, 1);
14302               comp_unit_has_inlines = 1;
14303             }
14304         }
14305 #endif
14306     }
14307   /* Add the calling convention attribute if requested.  */
14308   add_calling_convention_attribute (subr_die, decl);
14309
14310 }
14311
14312 /* Returns a hash value for X (which really is a die_struct).  */
14313
14314 static hashval_t
14315 common_block_die_table_hash (const void *x)
14316 {
14317   const_dw_die_ref d = (const_dw_die_ref) x;
14318   return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
14319 }
14320
14321 /* Return nonzero if decl_id and die_parent of die_struct X is the same
14322    as decl_id and die_parent of die_struct Y.  */
14323
14324 static int
14325 common_block_die_table_eq (const void *x, const void *y)
14326 {
14327   const_dw_die_ref d = (const_dw_die_ref) x;
14328   const_dw_die_ref e = (const_dw_die_ref) y;
14329   return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
14330 }
14331
14332 /* Generate a DIE to represent a declared data object.
14333    Either DECL or ORIGIN must be non-null.  */
14334
14335 static void
14336 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
14337 {
14338   HOST_WIDE_INT off;
14339   tree com_decl;
14340   tree decl_or_origin = decl ? decl : origin;
14341   dw_die_ref var_die;
14342   dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
14343   dw_die_ref origin_die;
14344   int declaration = (DECL_EXTERNAL (decl_or_origin)
14345                      /* If DECL is COMDAT and has not actually been
14346                         emitted, we cannot take its address; there
14347                         might end up being no definition anywhere in
14348                         the program.  For example, consider the C++
14349                         test case:
14350
14351                           template <class T>
14352                           struct S { static const int i = 7; };
14353
14354                           template <class T>
14355                           const int S<T>::i;
14356
14357                           int f() { return S<int>::i; }
14358
14359                         Here, S<int>::i is not DECL_EXTERNAL, but no
14360                         definition is required, so the compiler will
14361                         not emit a definition.  */
14362                      || (TREE_CODE (decl_or_origin) == VAR_DECL
14363                          && DECL_COMDAT (decl_or_origin)
14364                          && !TREE_ASM_WRITTEN (decl_or_origin))
14365                      || class_or_namespace_scope_p (context_die));
14366
14367   if (!origin)
14368     origin = decl_ultimate_origin (decl);
14369
14370   com_decl = fortran_common (decl_or_origin, &off);
14371
14372   /* Symbol in common gets emitted as a child of the common block, in the form
14373      of a data member.  */
14374   if (com_decl)
14375     {
14376       tree field;
14377       dw_die_ref com_die;
14378       dw_loc_descr_ref loc;
14379       die_node com_die_arg;
14380
14381       var_die = lookup_decl_die (decl_or_origin);
14382       if (var_die)
14383         {
14384           if (get_AT (var_die, DW_AT_location) == NULL)
14385             {
14386               loc = loc_descriptor_from_tree (com_decl);
14387               if (loc)
14388                 {
14389                   if (off)
14390                     {
14391                       /* Optimize the common case.  */
14392                       if (loc->dw_loc_opc == DW_OP_addr
14393                           && loc->dw_loc_next == NULL
14394                           && GET_CODE (loc->dw_loc_oprnd1.v.val_addr)
14395                              == SYMBOL_REF)
14396                         loc->dw_loc_oprnd1.v.val_addr
14397                           = plus_constant (loc->dw_loc_oprnd1.v.val_addr, off);
14398                         else
14399                           loc_descr_plus_const (&loc, off);
14400                     }
14401                   add_AT_loc (var_die, DW_AT_location, loc);
14402                   remove_AT (var_die, DW_AT_declaration);
14403                 }
14404             }
14405           return;
14406         }
14407
14408       if (common_block_die_table == NULL)
14409         common_block_die_table
14410           = htab_create_ggc (10, common_block_die_table_hash,
14411                              common_block_die_table_eq, NULL);
14412
14413       field = TREE_OPERAND (DECL_VALUE_EXPR (decl), 0);
14414       com_die_arg.decl_id = DECL_UID (com_decl);
14415       com_die_arg.die_parent = context_die;
14416       com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
14417       loc = loc_descriptor_from_tree (com_decl);
14418       if (com_die == NULL)
14419         {
14420           const char *cnam
14421             = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
14422           void **slot;
14423
14424           com_die = new_die (DW_TAG_common_block, context_die, decl);
14425           add_name_and_src_coords_attributes (com_die, com_decl);
14426           if (loc)
14427             {
14428               add_AT_loc (com_die, DW_AT_location, loc);
14429               /* Avoid sharing the same loc descriptor between
14430                  DW_TAG_common_block and DW_TAG_variable.  */
14431               loc = loc_descriptor_from_tree (com_decl);
14432             }
14433           else if (DECL_EXTERNAL (decl))
14434             add_AT_flag (com_die, DW_AT_declaration, 1);
14435           add_pubname_string (cnam, com_die); /* ??? needed? */
14436           com_die->decl_id = DECL_UID (com_decl);
14437           slot = htab_find_slot (common_block_die_table, com_die, INSERT);
14438           *slot = (void *) com_die;
14439         }
14440       else if (get_AT (com_die, DW_AT_location) == NULL && loc)
14441         {
14442           add_AT_loc (com_die, DW_AT_location, loc);
14443           loc = loc_descriptor_from_tree (com_decl);
14444           remove_AT (com_die, DW_AT_declaration);
14445         }
14446       var_die = new_die (DW_TAG_variable, com_die, decl);
14447       add_name_and_src_coords_attributes (var_die, decl);
14448       add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
14449                           TREE_THIS_VOLATILE (decl), context_die);
14450       add_AT_flag (var_die, DW_AT_external, 1);
14451       if (loc)
14452         {
14453           if (off)
14454             {
14455               /* Optimize the common case.  */
14456               if (loc->dw_loc_opc == DW_OP_addr
14457                   && loc->dw_loc_next == NULL
14458                   && GET_CODE (loc->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
14459                 loc->dw_loc_oprnd1.v.val_addr
14460                   = plus_constant (loc->dw_loc_oprnd1.v.val_addr, off);
14461               else
14462                 loc_descr_plus_const (&loc, off);
14463             }
14464           add_AT_loc (var_die, DW_AT_location, loc);
14465         }
14466       else if (DECL_EXTERNAL (decl))
14467         add_AT_flag (var_die, DW_AT_declaration, 1);
14468       equate_decl_number_to_die (decl, var_die);
14469       return;
14470     }
14471
14472   /* If the compiler emitted a definition for the DECL declaration
14473      and if we already emitted a DIE for it, don't emit a second
14474      DIE for it again.  */
14475   if (old_die
14476       && declaration
14477       && old_die->die_parent == context_die)
14478     return;
14479
14480   /* For static data members, the declaration in the class is supposed
14481      to have DW_TAG_member tag; the specification should still be
14482      DW_TAG_variable referencing the DW_TAG_member DIE.  */
14483   if (declaration && class_scope_p (context_die))
14484     var_die = new_die (DW_TAG_member, context_die, decl);
14485   else
14486     var_die = new_die (DW_TAG_variable, context_die, decl);
14487
14488   origin_die = NULL;
14489   if (origin != NULL)
14490     origin_die = add_abstract_origin_attribute (var_die, origin);
14491
14492   /* Loop unrolling can create multiple blocks that refer to the same
14493      static variable, so we must test for the DW_AT_declaration flag.
14494
14495      ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
14496      copy decls and set the DECL_ABSTRACT flag on them instead of
14497      sharing them.
14498
14499      ??? Duplicated blocks have been rewritten to use .debug_ranges.
14500
14501      ??? The declare_in_namespace support causes us to get two DIEs for one
14502      variable, both of which are declarations.  We want to avoid considering
14503      one to be a specification, so we must test that this DIE is not a
14504      declaration.  */
14505   else if (old_die && TREE_STATIC (decl) && ! declaration
14506            && get_AT_flag (old_die, DW_AT_declaration) == 1)
14507     {
14508       /* This is a definition of a C++ class level static.  */
14509       add_AT_specification (var_die, old_die);
14510       if (DECL_NAME (decl))
14511         {
14512           expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
14513           struct dwarf_file_data * file_index = lookup_filename (s.file);
14514
14515           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
14516             add_AT_file (var_die, DW_AT_decl_file, file_index);
14517
14518           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
14519             add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
14520         }
14521     }
14522   else
14523     {
14524       tree type = TREE_TYPE (decl);
14525
14526       add_name_and_src_coords_attributes (var_die, decl);
14527       if ((TREE_CODE (decl) == PARM_DECL
14528            || TREE_CODE (decl) == RESULT_DECL
14529            || TREE_CODE (decl) == VAR_DECL)
14530           && DECL_BY_REFERENCE (decl))
14531         add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
14532       else
14533         add_type_attribute (var_die, type, TREE_READONLY (decl),
14534                             TREE_THIS_VOLATILE (decl), context_die);
14535
14536       if (TREE_PUBLIC (decl))
14537         add_AT_flag (var_die, DW_AT_external, 1);
14538
14539       if (DECL_ARTIFICIAL (decl))
14540         add_AT_flag (var_die, DW_AT_artificial, 1);
14541
14542       if (TREE_PROTECTED (decl))
14543         add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_protected);
14544       else if (TREE_PRIVATE (decl))
14545         add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_private);
14546     }
14547
14548   if (declaration)
14549     add_AT_flag (var_die, DW_AT_declaration, 1);
14550
14551   if (decl && (DECL_ABSTRACT (decl) || declaration))
14552     equate_decl_number_to_die (decl, var_die);
14553
14554   if (! declaration
14555       && (! DECL_ABSTRACT (decl_or_origin)
14556           /* Local static vars are shared between all clones/inlines,
14557              so emit DW_AT_location on the abstract DIE if DECL_RTL is
14558              already set.  */
14559           || (TREE_CODE (decl_or_origin) == VAR_DECL
14560               && TREE_STATIC (decl_or_origin)
14561               && DECL_RTL_SET_P (decl_or_origin)))
14562       /* When abstract origin already has DW_AT_location attribute, no need
14563          to add it again.  */
14564       && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
14565     {
14566       if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
14567           && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
14568         defer_location (decl_or_origin, var_die);
14569       else
14570         add_location_or_const_value_attribute (var_die,
14571                                                decl_or_origin,
14572                                                DW_AT_location);
14573       add_pubname (decl_or_origin, var_die);
14574     }
14575   else
14576     tree_add_const_value_attribute (var_die, decl_or_origin);
14577 }
14578
14579 /* Generate a DIE to represent a named constant.  */
14580
14581 static void
14582 gen_const_die (tree decl, dw_die_ref context_die)
14583 {
14584   dw_die_ref const_die;
14585   tree type = TREE_TYPE (decl);
14586
14587   const_die = new_die (DW_TAG_constant, context_die, decl);
14588   add_name_and_src_coords_attributes (const_die, decl);
14589   add_type_attribute (const_die, type, 1, 0, context_die);
14590   if (TREE_PUBLIC (decl))
14591     add_AT_flag (const_die, DW_AT_external, 1);
14592   if (DECL_ARTIFICIAL (decl))
14593     add_AT_flag (const_die, DW_AT_artificial, 1);
14594   tree_add_const_value_attribute (const_die, decl);
14595 }
14596
14597 /* Generate a DIE to represent a label identifier.  */
14598
14599 static void
14600 gen_label_die (tree decl, dw_die_ref context_die)
14601 {
14602   tree origin = decl_ultimate_origin (decl);
14603   dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
14604   rtx insn;
14605   char label[MAX_ARTIFICIAL_LABEL_BYTES];
14606
14607   if (origin != NULL)
14608     add_abstract_origin_attribute (lbl_die, origin);
14609   else
14610     add_name_and_src_coords_attributes (lbl_die, decl);
14611
14612   if (DECL_ABSTRACT (decl))
14613     equate_decl_number_to_die (decl, lbl_die);
14614   else
14615     {
14616       insn = DECL_RTL_IF_SET (decl);
14617
14618       /* Deleted labels are programmer specified labels which have been
14619          eliminated because of various optimizations.  We still emit them
14620          here so that it is possible to put breakpoints on them.  */
14621       if (insn
14622           && (LABEL_P (insn)
14623               || ((NOTE_P (insn)
14624                    && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
14625         {
14626           /* When optimization is enabled (via -O) some parts of the compiler
14627              (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
14628              represent source-level labels which were explicitly declared by
14629              the user.  This really shouldn't be happening though, so catch
14630              it if it ever does happen.  */
14631           gcc_assert (!INSN_DELETED_P (insn));
14632
14633           ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
14634           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
14635         }
14636     }
14637 }
14638
14639 /* A helper function for gen_inlined_subroutine_die.  Add source coordinate
14640    attributes to the DIE for a block STMT, to describe where the inlined
14641    function was called from.  This is similar to add_src_coords_attributes.  */
14642
14643 static inline void
14644 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
14645 {
14646   expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
14647
14648   add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
14649   add_AT_unsigned (die, DW_AT_call_line, s.line);
14650 }
14651
14652
14653 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
14654    Add low_pc and high_pc attributes to the DIE for a block STMT.  */
14655
14656 static inline void
14657 add_high_low_attributes (tree stmt, dw_die_ref die)
14658 {
14659   char label[MAX_ARTIFICIAL_LABEL_BYTES];
14660
14661   if (BLOCK_FRAGMENT_CHAIN (stmt))
14662     {
14663       tree chain;
14664
14665       if (inlined_function_outer_scope_p (stmt))
14666         {
14667           ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
14668                                        BLOCK_NUMBER (stmt));
14669           add_AT_lbl_id (die, DW_AT_entry_pc, label);
14670         }
14671
14672       add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
14673
14674       chain = BLOCK_FRAGMENT_CHAIN (stmt);
14675       do
14676         {
14677           add_ranges (chain);
14678           chain = BLOCK_FRAGMENT_CHAIN (chain);
14679         }
14680       while (chain);
14681       add_ranges (NULL);
14682     }
14683   else
14684     {
14685       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
14686                                    BLOCK_NUMBER (stmt));
14687       add_AT_lbl_id (die, DW_AT_low_pc, label);
14688       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
14689                                    BLOCK_NUMBER (stmt));
14690       add_AT_lbl_id (die, DW_AT_high_pc, label);
14691     }
14692 }
14693
14694 /* Generate a DIE for a lexical block.  */
14695
14696 static void
14697 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
14698 {
14699   dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
14700
14701   if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
14702     add_high_low_attributes (stmt, stmt_die);
14703
14704   decls_for_scope (stmt, stmt_die, depth);
14705 }
14706
14707 /* Generate a DIE for an inlined subprogram.  */
14708
14709 static void
14710 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
14711 {
14712   tree decl = block_ultimate_origin (stmt);
14713
14714   /* Emit info for the abstract instance first, if we haven't yet.  We
14715      must emit this even if the block is abstract, otherwise when we
14716      emit the block below (or elsewhere), we may end up trying to emit
14717      a die whose origin die hasn't been emitted, and crashing.  */
14718   dwarf2out_abstract_function (decl);
14719
14720   if (! BLOCK_ABSTRACT (stmt))
14721     {
14722       dw_die_ref subr_die
14723         = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
14724
14725       add_abstract_origin_attribute (subr_die, decl);
14726       if (TREE_ASM_WRITTEN (stmt))
14727         add_high_low_attributes (stmt, subr_die);
14728       add_call_src_coords_attributes (stmt, subr_die);
14729
14730       decls_for_scope (stmt, subr_die, depth);
14731       current_function_has_inlines = 1;
14732     }
14733   else
14734     /* We may get here if we're the outer block of function A that was
14735        inlined into function B that was inlined into function C.  When
14736        generating debugging info for C, dwarf2out_abstract_function(B)
14737        would mark all inlined blocks as abstract, including this one.
14738        So, we wouldn't (and shouldn't) expect labels to be generated
14739        for this one.  Instead, just emit debugging info for
14740        declarations within the block.  This is particularly important
14741        in the case of initializers of arguments passed from B to us:
14742        if they're statement expressions containing declarations, we
14743        wouldn't generate dies for their abstract variables, and then,
14744        when generating dies for the real variables, we'd die (pun
14745        intended :-)  */
14746     gen_lexical_block_die (stmt, context_die, depth);
14747 }
14748
14749 /* Generate a DIE for a field in a record, or structure.  */
14750
14751 static void
14752 gen_field_die (tree decl, dw_die_ref context_die)
14753 {
14754   dw_die_ref decl_die;
14755
14756   if (TREE_TYPE (decl) == error_mark_node)
14757     return;
14758
14759   decl_die = new_die (DW_TAG_member, context_die, decl);
14760   add_name_and_src_coords_attributes (decl_die, decl);
14761   add_type_attribute (decl_die, member_declared_type (decl),
14762                       TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
14763                       context_die);
14764
14765   if (DECL_BIT_FIELD_TYPE (decl))
14766     {
14767       add_byte_size_attribute (decl_die, decl);
14768       add_bit_size_attribute (decl_die, decl);
14769       add_bit_offset_attribute (decl_die, decl);
14770     }
14771
14772   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
14773     add_data_member_location_attribute (decl_die, decl);
14774
14775   if (DECL_ARTIFICIAL (decl))
14776     add_AT_flag (decl_die, DW_AT_artificial, 1);
14777
14778   if (TREE_PROTECTED (decl))
14779     add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_protected);
14780   else if (TREE_PRIVATE (decl))
14781     add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_private);
14782
14783   /* Equate decl number to die, so that we can look up this decl later on.  */
14784   equate_decl_number_to_die (decl, decl_die);
14785 }
14786
14787 #if 0
14788 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
14789    Use modified_type_die instead.
14790    We keep this code here just in case these types of DIEs may be needed to
14791    represent certain things in other languages (e.g. Pascal) someday.  */
14792
14793 static void
14794 gen_pointer_type_die (tree type, dw_die_ref context_die)
14795 {
14796   dw_die_ref ptr_die
14797     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
14798
14799   equate_type_number_to_die (type, ptr_die);
14800   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
14801   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
14802 }
14803
14804 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
14805    Use modified_type_die instead.
14806    We keep this code here just in case these types of DIEs may be needed to
14807    represent certain things in other languages (e.g. Pascal) someday.  */
14808
14809 static void
14810 gen_reference_type_die (tree type, dw_die_ref context_die)
14811 {
14812   dw_die_ref ref_die
14813     = new_die (DW_TAG_reference_type, scope_die_for (type, context_die), type);
14814
14815   equate_type_number_to_die (type, ref_die);
14816   add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
14817   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
14818 }
14819 #endif
14820
14821 /* Generate a DIE for a pointer to a member type.  */
14822
14823 static void
14824 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
14825 {
14826   dw_die_ref ptr_die
14827     = new_die (DW_TAG_ptr_to_member_type,
14828                scope_die_for (type, context_die), type);
14829
14830   equate_type_number_to_die (type, ptr_die);
14831   add_AT_die_ref (ptr_die, DW_AT_containing_type,
14832                   lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
14833   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
14834 }
14835
14836 /* Generate the DIE for the compilation unit.  */
14837
14838 static dw_die_ref
14839 gen_compile_unit_die (const char *filename)
14840 {
14841   dw_die_ref die;
14842   char producer[250];
14843   const char *language_string = lang_hooks.name;
14844   int language;
14845
14846   die = new_die (DW_TAG_compile_unit, NULL, NULL);
14847
14848   if (filename)
14849     {
14850       add_name_attribute (die, filename);
14851       /* Don't add cwd for <built-in>.  */
14852       if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
14853         add_comp_dir_attribute (die);
14854     }
14855
14856   sprintf (producer, "%s %s", language_string, version_string);
14857
14858 #ifdef MIPS_DEBUGGING_INFO
14859   /* The MIPS/SGI compilers place the 'cc' command line options in the producer
14860      string.  The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
14861      not appear in the producer string, the debugger reaches the conclusion
14862      that the object file is stripped and has no debugging information.
14863      To get the MIPS/SGI debugger to believe that there is debugging
14864      information in the object file, we add a -g to the producer string.  */
14865   if (debug_info_level > DINFO_LEVEL_TERSE)
14866     strcat (producer, " -g");
14867 #endif
14868
14869   add_AT_string (die, DW_AT_producer, producer);
14870
14871   if (strcmp (language_string, "GNU C++") == 0)
14872     language = DW_LANG_C_plus_plus;
14873   else if (strcmp (language_string, "GNU Ada") == 0)
14874     language = DW_LANG_Ada95;
14875   else if (strcmp (language_string, "GNU F77") == 0)
14876     language = DW_LANG_Fortran77;
14877   else if (strcmp (language_string, "GNU Fortran") == 0)
14878     language = DW_LANG_Fortran95;
14879   else if (strcmp (language_string, "GNU Pascal") == 0)
14880     language = DW_LANG_Pascal83;
14881   else if (strcmp (language_string, "GNU Java") == 0)
14882     language = DW_LANG_Java;
14883   else if (strcmp (language_string, "GNU Objective-C") == 0)
14884     language = DW_LANG_ObjC;
14885   else if (strcmp (language_string, "GNU Objective-C++") == 0)
14886     language = DW_LANG_ObjC_plus_plus;
14887   else
14888     language = DW_LANG_C89;
14889
14890   add_AT_unsigned (die, DW_AT_language, language);
14891   return die;
14892 }
14893
14894 /* Generate the DIE for a base class.  */
14895
14896 static void
14897 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
14898 {
14899   dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
14900
14901   add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
14902   add_data_member_location_attribute (die, binfo);
14903
14904   if (BINFO_VIRTUAL_P (binfo))
14905     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
14906
14907   if (access == access_public_node)
14908     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
14909   else if (access == access_protected_node)
14910     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
14911 }
14912
14913 /* Generate a DIE for a class member.  */
14914
14915 static void
14916 gen_member_die (tree type, dw_die_ref context_die)
14917 {
14918   tree member;
14919   tree binfo = TYPE_BINFO (type);
14920   dw_die_ref child;
14921
14922   /* If this is not an incomplete type, output descriptions of each of its
14923      members. Note that as we output the DIEs necessary to represent the
14924      members of this record or union type, we will also be trying to output
14925      DIEs to represent the *types* of those members. However the `type'
14926      function (above) will specifically avoid generating type DIEs for member
14927      types *within* the list of member DIEs for this (containing) type except
14928      for those types (of members) which are explicitly marked as also being
14929      members of this (containing) type themselves.  The g++ front- end can
14930      force any given type to be treated as a member of some other (containing)
14931      type by setting the TYPE_CONTEXT of the given (member) type to point to
14932      the TREE node representing the appropriate (containing) type.  */
14933
14934   /* First output info about the base classes.  */
14935   if (binfo)
14936     {
14937       VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
14938       int i;
14939       tree base;
14940
14941       for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
14942         gen_inheritance_die (base,
14943                              (accesses ? VEC_index (tree, accesses, i)
14944                               : access_public_node), context_die);
14945     }
14946
14947   /* Now output info about the data members and type members.  */
14948   for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member))
14949     {
14950       /* If we thought we were generating minimal debug info for TYPE
14951          and then changed our minds, some of the member declarations
14952          may have already been defined.  Don't define them again, but
14953          do put them in the right order.  */
14954
14955       child = lookup_decl_die (member);
14956       if (child)
14957         splice_child_die (context_die, child);
14958       else
14959         gen_decl_die (member, NULL, context_die);
14960     }
14961
14962   /* Now output info about the function members (if any).  */
14963   for (member = TYPE_METHODS (type); member; member = TREE_CHAIN (member))
14964     {
14965       /* Don't include clones in the member list.  */
14966       if (DECL_ABSTRACT_ORIGIN (member))
14967         continue;
14968
14969       child = lookup_decl_die (member);
14970       if (child)
14971         splice_child_die (context_die, child);
14972       else
14973         gen_decl_die (member, NULL, context_die);
14974     }
14975 }
14976
14977 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
14978    is set, we pretend that the type was never defined, so we only get the
14979    member DIEs needed by later specification DIEs.  */
14980
14981 static void
14982 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
14983                                 enum debug_info_usage usage)
14984 {
14985   dw_die_ref type_die = lookup_type_die (type);
14986   dw_die_ref scope_die = 0;
14987   int nested = 0;
14988   int complete = (TYPE_SIZE (type)
14989                   && (! TYPE_STUB_DECL (type)
14990                       || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
14991   int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
14992   complete = complete && should_emit_struct_debug (type, usage);
14993
14994   if (type_die && ! complete)
14995     return;
14996
14997   if (TYPE_CONTEXT (type) != NULL_TREE
14998       && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
14999           || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
15000     nested = 1;
15001
15002   scope_die = scope_die_for (type, context_die);
15003
15004   if (! type_die || (nested && scope_die == comp_unit_die))
15005     /* First occurrence of type or toplevel definition of nested class.  */
15006     {
15007       dw_die_ref old_die = type_die;
15008
15009       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
15010                           ? record_type_tag (type) : DW_TAG_union_type,
15011                           scope_die, type);
15012       equate_type_number_to_die (type, type_die);
15013       if (old_die)
15014         add_AT_specification (type_die, old_die);
15015       else
15016         add_name_attribute (type_die, type_tag (type));
15017     }
15018   else
15019     remove_AT (type_die, DW_AT_declaration);
15020
15021   /* If this type has been completed, then give it a byte_size attribute and
15022      then give a list of members.  */
15023   if (complete && !ns_decl)
15024     {
15025       /* Prevent infinite recursion in cases where the type of some member of
15026          this type is expressed in terms of this type itself.  */
15027       TREE_ASM_WRITTEN (type) = 1;
15028       add_byte_size_attribute (type_die, type);
15029       if (TYPE_STUB_DECL (type) != NULL_TREE)
15030         add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
15031
15032       /* If the first reference to this type was as the return type of an
15033          inline function, then it may not have a parent.  Fix this now.  */
15034       if (type_die->die_parent == NULL)
15035         add_child_die (scope_die, type_die);
15036
15037       push_decl_scope (type);
15038       gen_member_die (type, type_die);
15039       pop_decl_scope ();
15040
15041       /* GNU extension: Record what type our vtable lives in.  */
15042       if (TYPE_VFIELD (type))
15043         {
15044           tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
15045
15046           gen_type_die (vtype, context_die);
15047           add_AT_die_ref (type_die, DW_AT_containing_type,
15048                           lookup_type_die (vtype));
15049         }
15050     }
15051   else
15052     {
15053       add_AT_flag (type_die, DW_AT_declaration, 1);
15054
15055       /* We don't need to do this for function-local types.  */
15056       if (TYPE_STUB_DECL (type)
15057           && ! decl_function_context (TYPE_STUB_DECL (type)))
15058         VEC_safe_push (tree, gc, incomplete_types, type);
15059     }
15060
15061   if (get_AT (type_die, DW_AT_name))
15062     add_pubtype (type, type_die);
15063 }
15064
15065 /* Generate a DIE for a subroutine _type_.  */
15066
15067 static void
15068 gen_subroutine_type_die (tree type, dw_die_ref context_die)
15069 {
15070   tree return_type = TREE_TYPE (type);
15071   dw_die_ref subr_die
15072     = new_die (DW_TAG_subroutine_type,
15073                scope_die_for (type, context_die), type);
15074
15075   equate_type_number_to_die (type, subr_die);
15076   add_prototyped_attribute (subr_die, type);
15077   add_type_attribute (subr_die, return_type, 0, 0, context_die);
15078   gen_formal_types_die (type, subr_die);
15079
15080   if (get_AT (subr_die, DW_AT_name))
15081     add_pubtype (type, subr_die);
15082 }
15083
15084 /* Generate a DIE for a type definition.  */
15085
15086 static void
15087 gen_typedef_die (tree decl, dw_die_ref context_die)
15088 {
15089   dw_die_ref type_die;
15090   tree origin;
15091
15092   if (TREE_ASM_WRITTEN (decl))
15093     return;
15094
15095   TREE_ASM_WRITTEN (decl) = 1;
15096   type_die = new_die (DW_TAG_typedef, context_die, decl);
15097   origin = decl_ultimate_origin (decl);
15098   if (origin != NULL)
15099     add_abstract_origin_attribute (type_die, origin);
15100   else
15101     {
15102       tree type;
15103
15104       add_name_and_src_coords_attributes (type_die, decl);
15105       if (DECL_ORIGINAL_TYPE (decl))
15106         {
15107           type = DECL_ORIGINAL_TYPE (decl);
15108
15109           gcc_assert (type != TREE_TYPE (decl));
15110           equate_type_number_to_die (TREE_TYPE (decl), type_die);
15111         }
15112       else
15113         type = TREE_TYPE (decl);
15114
15115       add_type_attribute (type_die, type, TREE_READONLY (decl),
15116                           TREE_THIS_VOLATILE (decl), context_die);
15117     }
15118
15119   if (DECL_ABSTRACT (decl))
15120     equate_decl_number_to_die (decl, type_die);
15121
15122   if (get_AT (type_die, DW_AT_name))
15123     add_pubtype (decl, type_die);
15124 }
15125
15126 /* Generate a type description DIE.  */
15127
15128 static void
15129 gen_type_die_with_usage (tree type, dw_die_ref context_die,
15130                                 enum debug_info_usage usage)
15131 {
15132   int need_pop;
15133   struct array_descr_info info;
15134
15135   if (type == NULL_TREE || type == error_mark_node)
15136     return;
15137
15138   /* If TYPE is a typedef type variant, let's generate debug info
15139      for the parent typedef which TYPE is a type of.  */
15140   if (TYPE_NAME (type) && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
15141       && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
15142     {
15143       if (TREE_ASM_WRITTEN (type))
15144         return;
15145
15146       /* Prevent broken recursion; we can't hand off to the same type.  */
15147       gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
15148
15149       /* Use the DIE of the containing namespace as the parent DIE of
15150          the type description DIE we want to generate.  */
15151       if (DECL_CONTEXT (TYPE_NAME (type))
15152           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
15153         context_die = lookup_decl_die (DECL_CONTEXT (TYPE_NAME (type)));
15154
15155       TREE_ASM_WRITTEN (type) = 1;
15156       gen_decl_die (TYPE_NAME (type), NULL, context_die);
15157       return;
15158     }
15159
15160   /* If this is an array type with hidden descriptor, handle it first.  */
15161   if (!TREE_ASM_WRITTEN (type)
15162       && lang_hooks.types.get_array_descr_info
15163       && lang_hooks.types.get_array_descr_info (type, &info))
15164     {
15165       gen_descr_array_type_die (type, &info, context_die);
15166       TREE_ASM_WRITTEN (type) = 1;
15167       return;
15168     }
15169
15170   /* We are going to output a DIE to represent the unqualified version
15171      of this type (i.e. without any const or volatile qualifiers) so
15172      get the main variant (i.e. the unqualified version) of this type
15173      now.  (Vectors are special because the debugging info is in the
15174      cloned type itself).  */
15175   if (TREE_CODE (type) != VECTOR_TYPE)
15176     type = type_main_variant (type);
15177
15178   if (TREE_ASM_WRITTEN (type))
15179     return;
15180
15181   switch (TREE_CODE (type))
15182     {
15183     case ERROR_MARK:
15184       break;
15185
15186     case POINTER_TYPE:
15187     case REFERENCE_TYPE:
15188       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
15189          ensures that the gen_type_die recursion will terminate even if the
15190          type is recursive.  Recursive types are possible in Ada.  */
15191       /* ??? We could perhaps do this for all types before the switch
15192          statement.  */
15193       TREE_ASM_WRITTEN (type) = 1;
15194
15195       /* For these types, all that is required is that we output a DIE (or a
15196          set of DIEs) to represent the "basis" type.  */
15197       gen_type_die_with_usage (TREE_TYPE (type), context_die,
15198                                 DINFO_USAGE_IND_USE);
15199       break;
15200
15201     case OFFSET_TYPE:
15202       /* This code is used for C++ pointer-to-data-member types.
15203          Output a description of the relevant class type.  */
15204       gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
15205                                         DINFO_USAGE_IND_USE);
15206
15207       /* Output a description of the type of the object pointed to.  */
15208       gen_type_die_with_usage (TREE_TYPE (type), context_die,
15209                                         DINFO_USAGE_IND_USE);
15210
15211       /* Now output a DIE to represent this pointer-to-data-member type
15212          itself.  */
15213       gen_ptr_to_mbr_type_die (type, context_die);
15214       break;
15215
15216     case FUNCTION_TYPE:
15217       /* Force out return type (in case it wasn't forced out already).  */
15218       gen_type_die_with_usage (TREE_TYPE (type), context_die,
15219                                         DINFO_USAGE_DIR_USE);
15220       gen_subroutine_type_die (type, context_die);
15221       break;
15222
15223     case METHOD_TYPE:
15224       /* Force out return type (in case it wasn't forced out already).  */
15225       gen_type_die_with_usage (TREE_TYPE (type), context_die,
15226                                         DINFO_USAGE_DIR_USE);
15227       gen_subroutine_type_die (type, context_die);
15228       break;
15229
15230     case ARRAY_TYPE:
15231       gen_array_type_die (type, context_die);
15232       break;
15233
15234     case VECTOR_TYPE:
15235       gen_array_type_die (type, context_die);
15236       break;
15237
15238     case ENUMERAL_TYPE:
15239     case RECORD_TYPE:
15240     case UNION_TYPE:
15241     case QUAL_UNION_TYPE:
15242       /* If this is a nested type whose containing class hasn't been written
15243          out yet, writing it out will cover this one, too.  This does not apply
15244          to instantiations of member class templates; they need to be added to
15245          the containing class as they are generated.  FIXME: This hurts the
15246          idea of combining type decls from multiple TUs, since we can't predict
15247          what set of template instantiations we'll get.  */
15248       if (TYPE_CONTEXT (type)
15249           && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
15250           && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
15251         {
15252           gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
15253
15254           if (TREE_ASM_WRITTEN (type))
15255             return;
15256
15257           /* If that failed, attach ourselves to the stub.  */
15258           push_decl_scope (TYPE_CONTEXT (type));
15259           context_die = lookup_type_die (TYPE_CONTEXT (type));
15260           need_pop = 1;
15261         }
15262       else
15263         {
15264           context_die = declare_in_namespace (type, context_die);
15265           need_pop = 0;
15266         }
15267
15268       if (TREE_CODE (type) == ENUMERAL_TYPE)
15269         {
15270           /* This might have been written out by the call to
15271              declare_in_namespace.  */
15272           if (!TREE_ASM_WRITTEN (type))
15273             gen_enumeration_type_die (type, context_die);
15274         }
15275       else
15276         gen_struct_or_union_type_die (type, context_die, usage);
15277
15278       if (need_pop)
15279         pop_decl_scope ();
15280
15281       /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
15282          it up if it is ever completed.  gen_*_type_die will set it for us
15283          when appropriate.  */
15284       return;
15285
15286     case VOID_TYPE:
15287     case INTEGER_TYPE:
15288     case REAL_TYPE:
15289     case FIXED_POINT_TYPE:
15290     case COMPLEX_TYPE:
15291     case BOOLEAN_TYPE:
15292       /* No DIEs needed for fundamental types.  */
15293       break;
15294
15295     case LANG_TYPE:
15296       /* No Dwarf representation currently defined.  */
15297       break;
15298
15299     default:
15300       gcc_unreachable ();
15301     }
15302
15303   TREE_ASM_WRITTEN (type) = 1;
15304 }
15305
15306 static void
15307 gen_type_die (tree type, dw_die_ref context_die)
15308 {
15309   gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
15310 }
15311
15312 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
15313    things which are local to the given block.  */
15314
15315 static void
15316 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
15317 {
15318   int must_output_die = 0;
15319   bool inlined_func;
15320
15321   /* Ignore blocks that are NULL.  */
15322   if (stmt == NULL_TREE)
15323     return;
15324
15325   inlined_func = inlined_function_outer_scope_p (stmt);
15326
15327   /* If the block is one fragment of a non-contiguous block, do not
15328      process the variables, since they will have been done by the
15329      origin block.  Do process subblocks.  */
15330   if (BLOCK_FRAGMENT_ORIGIN (stmt))
15331     {
15332       tree sub;
15333
15334       for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
15335         gen_block_die (sub, context_die, depth + 1);
15336
15337       return;
15338     }
15339
15340   /* Determine if we need to output any Dwarf DIEs at all to represent this
15341      block.  */
15342   if (inlined_func)
15343     /* The outer scopes for inlinings *must* always be represented.  We
15344        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
15345     must_output_die = 1;
15346   else
15347     {
15348       /* Determine if this block directly contains any "significant"
15349          local declarations which we will need to output DIEs for.  */
15350       if (debug_info_level > DINFO_LEVEL_TERSE)
15351         /* We are not in terse mode so *any* local declaration counts
15352            as being a "significant" one.  */
15353         must_output_die = ((BLOCK_VARS (stmt) != NULL
15354                             || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
15355                            && (TREE_USED (stmt)
15356                                || TREE_ASM_WRITTEN (stmt)
15357                                || BLOCK_ABSTRACT (stmt)));
15358       else if ((TREE_USED (stmt)
15359                 || TREE_ASM_WRITTEN (stmt)
15360                 || BLOCK_ABSTRACT (stmt))
15361                && !dwarf2out_ignore_block (stmt))
15362         must_output_die = 1;
15363     }
15364
15365   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
15366      DIE for any block which contains no significant local declarations at
15367      all.  Rather, in such cases we just call `decls_for_scope' so that any
15368      needed Dwarf info for any sub-blocks will get properly generated. Note
15369      that in terse mode, our definition of what constitutes a "significant"
15370      local declaration gets restricted to include only inlined function
15371      instances and local (nested) function definitions.  */
15372   if (must_output_die)
15373     {
15374       if (inlined_func)
15375         gen_inlined_subroutine_die (stmt, context_die, depth);
15376       else
15377         gen_lexical_block_die (stmt, context_die, depth);
15378     }
15379   else
15380     decls_for_scope (stmt, context_die, depth);
15381 }
15382
15383 /* Process variable DECL (or variable with origin ORIGIN) within
15384    block STMT and add it to CONTEXT_DIE.  */
15385 static void
15386 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
15387 {
15388   dw_die_ref die;
15389   tree decl_or_origin = decl ? decl : origin;
15390   tree ultimate_origin = origin ? decl_ultimate_origin (origin) : NULL;
15391
15392   if (ultimate_origin)
15393     origin = ultimate_origin;
15394
15395   if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
15396     die = lookup_decl_die (decl_or_origin);
15397   else if (TREE_CODE (decl_or_origin) == TYPE_DECL
15398            && TYPE_DECL_IS_STUB (decl_or_origin))
15399     die = lookup_type_die (TREE_TYPE (decl_or_origin));
15400   else
15401     die = NULL;
15402
15403   if (die != NULL && die->die_parent == NULL)
15404     add_child_die (context_die, die);
15405   else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
15406     dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
15407                                          stmt, context_die);
15408   else
15409     gen_decl_die (decl, origin, context_die);
15410 }
15411
15412 /* Generate all of the decls declared within a given scope and (recursively)
15413    all of its sub-blocks.  */
15414
15415 static void
15416 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
15417 {
15418   tree decl;
15419   unsigned int i;
15420   tree subblocks;
15421
15422   /* Ignore NULL blocks.  */
15423   if (stmt == NULL_TREE)
15424     return;
15425
15426   /* Output the DIEs to represent all of the data objects and typedefs
15427      declared directly within this block but not within any nested
15428      sub-blocks.  Also, nested function and tag DIEs have been
15429      generated with a parent of NULL; fix that up now.  */
15430   for (decl = BLOCK_VARS (stmt); decl != NULL; decl = TREE_CHAIN (decl))
15431     process_scope_var (stmt, decl, NULL_TREE, context_die);
15432   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
15433     process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
15434                        context_die);
15435
15436   /* If we're at -g1, we're not interested in subblocks.  */
15437   if (debug_info_level <= DINFO_LEVEL_TERSE)
15438     return;
15439
15440   /* Output the DIEs to represent all sub-blocks (and the items declared
15441      therein) of this block.  */
15442   for (subblocks = BLOCK_SUBBLOCKS (stmt);
15443        subblocks != NULL;
15444        subblocks = BLOCK_CHAIN (subblocks))
15445     gen_block_die (subblocks, context_die, depth + 1);
15446 }
15447
15448 /* Is this a typedef we can avoid emitting?  */
15449
15450 static inline int
15451 is_redundant_typedef (const_tree decl)
15452 {
15453   if (TYPE_DECL_IS_STUB (decl))
15454     return 1;
15455
15456   if (DECL_ARTIFICIAL (decl)
15457       && DECL_CONTEXT (decl)
15458       && is_tagged_type (DECL_CONTEXT (decl))
15459       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
15460       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
15461     /* Also ignore the artificial member typedef for the class name.  */
15462     return 1;
15463
15464   return 0;
15465 }
15466
15467 /* Returns the DIE for a context.  */
15468
15469 static inline dw_die_ref
15470 get_context_die (tree context)
15471 {
15472   if (context)
15473     {
15474       /* Find die that represents this context.  */
15475       if (TYPE_P (context))
15476         return force_type_die (context);
15477       else
15478         return force_decl_die (context);
15479     }
15480   return comp_unit_die;
15481 }
15482
15483 /* Returns the DIE for decl.  A DIE will always be returned.  */
15484
15485 static dw_die_ref
15486 force_decl_die (tree decl)
15487 {
15488   dw_die_ref decl_die;
15489   unsigned saved_external_flag;
15490   tree save_fn = NULL_TREE;
15491   decl_die = lookup_decl_die (decl);
15492   if (!decl_die)
15493     {
15494       dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
15495
15496       decl_die = lookup_decl_die (decl);
15497       if (decl_die)
15498         return decl_die;
15499
15500       switch (TREE_CODE (decl))
15501         {
15502         case FUNCTION_DECL:
15503           /* Clear current_function_decl, so that gen_subprogram_die thinks
15504              that this is a declaration. At this point, we just want to force
15505              declaration die.  */
15506           save_fn = current_function_decl;
15507           current_function_decl = NULL_TREE;
15508           gen_subprogram_die (decl, context_die);
15509           current_function_decl = save_fn;
15510           break;
15511
15512         case VAR_DECL:
15513           /* Set external flag to force declaration die. Restore it after
15514            gen_decl_die() call.  */
15515           saved_external_flag = DECL_EXTERNAL (decl);
15516           DECL_EXTERNAL (decl) = 1;
15517           gen_decl_die (decl, NULL, context_die);
15518           DECL_EXTERNAL (decl) = saved_external_flag;
15519           break;
15520
15521         case NAMESPACE_DECL:
15522           dwarf2out_decl (decl);
15523           break;
15524
15525         default:
15526           gcc_unreachable ();
15527         }
15528
15529       /* We should be able to find the DIE now.  */
15530       if (!decl_die)
15531         decl_die = lookup_decl_die (decl);
15532       gcc_assert (decl_die);
15533     }
15534
15535   return decl_die;
15536 }
15537
15538 /* Returns the DIE for TYPE, that must not be a base type.  A DIE is
15539    always returned.  */
15540
15541 static dw_die_ref
15542 force_type_die (tree type)
15543 {
15544   dw_die_ref type_die;
15545
15546   type_die = lookup_type_die (type);
15547   if (!type_die)
15548     {
15549       dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
15550
15551       type_die = modified_type_die (type, TYPE_READONLY (type),
15552                                     TYPE_VOLATILE (type), context_die);
15553       gcc_assert (type_die);
15554     }
15555   return type_die;
15556 }
15557
15558 /* Force out any required namespaces to be able to output DECL,
15559    and return the new context_die for it, if it's changed.  */
15560
15561 static dw_die_ref
15562 setup_namespace_context (tree thing, dw_die_ref context_die)
15563 {
15564   tree context = (DECL_P (thing)
15565                   ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
15566   if (context && TREE_CODE (context) == NAMESPACE_DECL)
15567     /* Force out the namespace.  */
15568     context_die = force_decl_die (context);
15569
15570   return context_die;
15571 }
15572
15573 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
15574    type) within its namespace, if appropriate.
15575
15576    For compatibility with older debuggers, namespace DIEs only contain
15577    declarations; all definitions are emitted at CU scope.  */
15578
15579 static dw_die_ref
15580 declare_in_namespace (tree thing, dw_die_ref context_die)
15581 {
15582   dw_die_ref ns_context;
15583
15584   if (debug_info_level <= DINFO_LEVEL_TERSE)
15585     return context_die;
15586
15587   /* If this decl is from an inlined function, then don't try to emit it in its
15588      namespace, as we will get confused.  It would have already been emitted
15589      when the abstract instance of the inline function was emitted anyways.  */
15590   if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
15591     return context_die;
15592
15593   ns_context = setup_namespace_context (thing, context_die);
15594
15595   if (ns_context != context_die)
15596     {
15597       if (is_fortran ())
15598         return ns_context;
15599       if (DECL_P (thing))
15600         gen_decl_die (thing, NULL, ns_context);
15601       else
15602         gen_type_die (thing, ns_context);
15603     }
15604   return context_die;
15605 }
15606
15607 /* Generate a DIE for a namespace or namespace alias.  */
15608
15609 static void
15610 gen_namespace_die (tree decl, dw_die_ref context_die)
15611 {
15612   dw_die_ref namespace_die;
15613
15614   /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
15615      they are an alias of.  */
15616   if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
15617     {
15618       /* Output a real namespace or module.  */
15619       context_die = setup_namespace_context (decl, comp_unit_die);
15620       namespace_die = new_die (is_fortran ()
15621                                ? DW_TAG_module : DW_TAG_namespace,
15622                                context_die, decl);
15623       /* For Fortran modules defined in different CU don't add src coords.  */
15624       if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
15625         add_name_attribute (namespace_die, dwarf2_name (decl, 0));
15626       else
15627         add_name_and_src_coords_attributes (namespace_die, decl);
15628       if (DECL_EXTERNAL (decl))
15629         add_AT_flag (namespace_die, DW_AT_declaration, 1);
15630       equate_decl_number_to_die (decl, namespace_die);
15631     }
15632   else
15633     {
15634       /* Output a namespace alias.  */
15635
15636       /* Force out the namespace we are an alias of, if necessary.  */
15637       dw_die_ref origin_die
15638         = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
15639
15640       if (DECL_CONTEXT (decl) == NULL_TREE
15641           || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
15642         context_die = setup_namespace_context (decl, comp_unit_die);
15643       /* Now create the namespace alias DIE.  */
15644       namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
15645       add_name_and_src_coords_attributes (namespace_die, decl);
15646       add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
15647       equate_decl_number_to_die (decl, namespace_die);
15648     }
15649 }
15650
15651 /* Generate Dwarf debug information for a decl described by DECL.  */
15652
15653 static void
15654 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
15655 {
15656   tree decl_or_origin = decl ? decl : origin;
15657   tree class_origin = NULL;
15658
15659   if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
15660     return;
15661
15662   switch (TREE_CODE (decl_or_origin))
15663     {
15664     case ERROR_MARK:
15665       break;
15666
15667     case CONST_DECL:
15668       if (!is_fortran ())
15669         {
15670           /* The individual enumerators of an enum type get output when we output
15671              the Dwarf representation of the relevant enum type itself.  */
15672           break;
15673         }
15674
15675       /* Emit its type.  */
15676       gen_type_die (TREE_TYPE (decl), context_die);
15677
15678       /* And its containing namespace.  */
15679       context_die = declare_in_namespace (decl, context_die);
15680
15681       gen_const_die (decl, context_die);
15682       break;
15683
15684     case FUNCTION_DECL:
15685       /* Don't output any DIEs to represent mere function declarations,
15686          unless they are class members or explicit block externs.  */
15687       if (DECL_INITIAL (decl_or_origin) == NULL_TREE
15688           && DECL_CONTEXT (decl_or_origin) == NULL_TREE
15689           && (current_function_decl == NULL_TREE
15690               || DECL_ARTIFICIAL (decl_or_origin)))
15691         break;
15692
15693 #if 0
15694       /* FIXME */
15695       /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
15696          on local redeclarations of global functions.  That seems broken.  */
15697       if (current_function_decl != decl)
15698         /* This is only a declaration.  */;
15699 #endif
15700
15701       /* If we're emitting a clone, emit info for the abstract instance.  */
15702       if (origin || DECL_ORIGIN (decl) != decl)
15703         dwarf2out_abstract_function (origin ? origin : DECL_ABSTRACT_ORIGIN (decl));
15704
15705       /* If we're emitting an out-of-line copy of an inline function,
15706          emit info for the abstract instance and set up to refer to it.  */
15707       else if (cgraph_function_possibly_inlined_p (decl)
15708                && ! DECL_ABSTRACT (decl)
15709                && ! class_or_namespace_scope_p (context_die)
15710                /* dwarf2out_abstract_function won't emit a die if this is just
15711                   a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
15712                   that case, because that works only if we have a die.  */
15713                && DECL_INITIAL (decl) != NULL_TREE)
15714         {
15715           dwarf2out_abstract_function (decl);
15716           set_decl_origin_self (decl);
15717         }
15718
15719       /* Otherwise we're emitting the primary DIE for this decl.  */
15720       else if (debug_info_level > DINFO_LEVEL_TERSE)
15721         {
15722           /* Before we describe the FUNCTION_DECL itself, make sure that we
15723              have described its return type.  */
15724           gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
15725
15726           /* And its virtual context.  */
15727           if (DECL_VINDEX (decl) != NULL_TREE)
15728             gen_type_die (DECL_CONTEXT (decl), context_die);
15729
15730           /* And its containing type.  */
15731           if (!origin)
15732             origin = decl_class_context (decl);
15733           if (origin != NULL_TREE)
15734             gen_type_die_for_member (origin, decl, context_die);
15735
15736           /* And its containing namespace.  */
15737           context_die = declare_in_namespace (decl, context_die);
15738         }
15739
15740       /* Now output a DIE to represent the function itself.  */
15741       if (decl)
15742         gen_subprogram_die (decl, context_die);
15743       break;
15744
15745     case TYPE_DECL:
15746       /* If we are in terse mode, don't generate any DIEs to represent any
15747          actual typedefs.  */
15748       if (debug_info_level <= DINFO_LEVEL_TERSE)
15749         break;
15750
15751       /* In the special case of a TYPE_DECL node representing the declaration
15752          of some type tag, if the given TYPE_DECL is marked as having been
15753          instantiated from some other (original) TYPE_DECL node (e.g. one which
15754          was generated within the original definition of an inline function) we
15755          used to generate a special (abbreviated) DW_TAG_structure_type,
15756          DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  But nothing
15757          should be actually referencing those DIEs, as variable DIEs with that
15758          type would be emitted already in the abstract origin, so it was always
15759          removed during unused type prunning.  Don't add anything in this
15760          case.  */
15761       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
15762         break;
15763
15764       if (is_redundant_typedef (decl))
15765         gen_type_die (TREE_TYPE (decl), context_die);
15766       else
15767         /* Output a DIE to represent the typedef itself.  */
15768         gen_typedef_die (decl, context_die);
15769       break;
15770
15771     case LABEL_DECL:
15772       if (debug_info_level >= DINFO_LEVEL_NORMAL)
15773         gen_label_die (decl, context_die);
15774       break;
15775
15776     case VAR_DECL:
15777     case RESULT_DECL:
15778       /* If we are in terse mode, don't generate any DIEs to represent any
15779          variable declarations or definitions.  */
15780       if (debug_info_level <= DINFO_LEVEL_TERSE)
15781         break;
15782
15783       /* Output any DIEs that are needed to specify the type of this data
15784          object.  */
15785       if ((TREE_CODE (decl_or_origin) == RESULT_DECL
15786            || TREE_CODE (decl_or_origin) == VAR_DECL)
15787           && DECL_BY_REFERENCE (decl_or_origin))
15788         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
15789       else
15790         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
15791
15792       /* And its containing type.  */
15793       class_origin = decl_class_context (decl_or_origin);
15794       if (class_origin != NULL_TREE)
15795         gen_type_die_for_member (class_origin, decl_or_origin, context_die);
15796
15797       /* And its containing namespace.  */
15798       context_die = declare_in_namespace (decl_or_origin, context_die);
15799
15800       /* Now output the DIE to represent the data object itself.  This gets
15801          complicated because of the possibility that the VAR_DECL really
15802          represents an inlined instance of a formal parameter for an inline
15803          function.  */
15804       if (!origin)
15805         origin = decl_ultimate_origin (decl);
15806       if (origin != NULL_TREE && TREE_CODE (origin) == PARM_DECL)
15807         gen_formal_parameter_die (decl, origin, context_die);
15808       else
15809         gen_variable_die (decl, origin, context_die);
15810       break;
15811
15812     case FIELD_DECL:
15813       /* Ignore the nameless fields that are used to skip bits but handle C++
15814          anonymous unions and structs.  */
15815       if (DECL_NAME (decl) != NULL_TREE
15816           || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
15817           || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
15818         {
15819           gen_type_die (member_declared_type (decl), context_die);
15820           gen_field_die (decl, context_die);
15821         }
15822       break;
15823
15824     case PARM_DECL:
15825       if (DECL_BY_REFERENCE (decl_or_origin))
15826         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
15827       else
15828         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
15829       gen_formal_parameter_die (decl, origin, context_die);
15830       break;
15831
15832     case NAMESPACE_DECL:
15833     case IMPORTED_DECL:
15834       gen_namespace_die (decl, context_die);
15835       break;
15836
15837     default:
15838       /* Probably some frontend-internal decl.  Assume we don't care.  */
15839       gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
15840       break;
15841     }
15842 }
15843 \f
15844 /* Output debug information for global decl DECL.  Called from toplev.c after
15845    compilation proper has finished.  */
15846
15847 static void
15848 dwarf2out_global_decl (tree decl)
15849 {
15850   /* Output DWARF2 information for file-scope tentative data object
15851      declarations, file-scope (extern) function declarations (which
15852      had no corresponding body) and file-scope tagged type declarations
15853      and definitions which have not yet been forced out.  */
15854   if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
15855     dwarf2out_decl (decl);
15856 }
15857
15858 /* Output debug information for type decl DECL.  Called from toplev.c
15859    and from language front ends (to record built-in types).  */
15860 static void
15861 dwarf2out_type_decl (tree decl, int local)
15862 {
15863   if (!local)
15864     dwarf2out_decl (decl);
15865 }
15866
15867 /* Output debug information for imported module or decl DECL.
15868    NAME is non-NULL name in the lexical block if the decl has been renamed.
15869    LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
15870    that DECL belongs to.
15871    LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK.  */
15872 static void
15873 dwarf2out_imported_module_or_decl_1 (tree decl,
15874                                      tree name,
15875                                      tree lexical_block,
15876                                      dw_die_ref lexical_block_die)
15877 {
15878   expanded_location xloc;
15879   dw_die_ref imported_die = NULL;
15880   dw_die_ref at_import_die;
15881
15882   if (TREE_CODE (decl) == IMPORTED_DECL)
15883     {
15884       xloc = expand_location (DECL_SOURCE_LOCATION (decl));
15885       decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
15886       gcc_assert (decl);
15887     }
15888   else
15889     xloc = expand_location (input_location);
15890
15891   if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
15892     {
15893       if (is_base_type (TREE_TYPE (decl)))
15894         at_import_die = base_type_die (TREE_TYPE (decl));
15895       else
15896         at_import_die = force_type_die (TREE_TYPE (decl));
15897       /* For namespace N { typedef void T; } using N::T; base_type_die
15898          returns NULL, but DW_TAG_imported_declaration requires
15899          the DW_AT_import tag.  Force creation of DW_TAG_typedef.  */
15900       if (!at_import_die)
15901         {
15902           gcc_assert (TREE_CODE (decl) == TYPE_DECL);
15903           gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
15904           at_import_die = lookup_type_die (TREE_TYPE (decl));
15905           gcc_assert (at_import_die);
15906         }
15907     }
15908   else
15909     {
15910       at_import_die = lookup_decl_die (decl);
15911       if (!at_import_die)
15912         {
15913           /* If we're trying to avoid duplicate debug info, we may not have
15914              emitted the member decl for this field.  Emit it now.  */
15915           if (TREE_CODE (decl) == FIELD_DECL)
15916             {
15917               tree type = DECL_CONTEXT (decl);
15918
15919               if (TYPE_CONTEXT (type)
15920                   && TYPE_P (TYPE_CONTEXT (type))
15921                   && !should_emit_struct_debug (TYPE_CONTEXT (type),
15922                                                 DINFO_USAGE_DIR_USE))
15923                 return;
15924               gen_type_die_for_member (type, decl,
15925                                        get_context_die (TYPE_CONTEXT (type)));
15926             }
15927           at_import_die = force_decl_die (decl);
15928         }
15929     }
15930
15931   if (TREE_CODE (decl) == NAMESPACE_DECL)
15932     imported_die = new_die (DW_TAG_imported_module,
15933                             lexical_block_die,
15934                             lexical_block);
15935   else
15936     imported_die = new_die (DW_TAG_imported_declaration,
15937                             lexical_block_die,
15938                             lexical_block);
15939
15940   add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
15941   add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
15942   if (name)
15943     add_AT_string (imported_die, DW_AT_name,
15944                    IDENTIFIER_POINTER (name));
15945   add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
15946 }
15947
15948 /* Output debug information for imported module or decl DECL.
15949    NAME is non-NULL name in context if the decl has been renamed.
15950    CHILD is true if decl is one of the renamed decls as part of
15951    importing whole module.  */
15952
15953 static void
15954 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
15955                                    bool child)
15956 {
15957   /* dw_die_ref at_import_die;  */
15958   dw_die_ref scope_die;
15959
15960   if (debug_info_level <= DINFO_LEVEL_TERSE)
15961     return;
15962
15963   gcc_assert (decl);
15964
15965   /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
15966      We need decl DIE for reference and scope die. First, get DIE for the decl
15967      itself.  */
15968
15969   /* Get the scope die for decl context. Use comp_unit_die for global module
15970      or decl. If die is not found for non globals, force new die.  */
15971   if (context
15972       && TYPE_P (context)
15973       && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
15974     return;
15975   scope_die = get_context_die (context);
15976
15977   if (child)
15978     {
15979       gcc_assert (scope_die->die_child);
15980       gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
15981       gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
15982       scope_die = scope_die->die_child;
15983     }
15984
15985   /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
15986   dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
15987
15988 }
15989
15990 /* Write the debugging output for DECL.  */
15991
15992 void
15993 dwarf2out_decl (tree decl)
15994 {
15995   dw_die_ref context_die = comp_unit_die;
15996
15997   switch (TREE_CODE (decl))
15998     {
15999     case ERROR_MARK:
16000       return;
16001
16002     case FUNCTION_DECL:
16003       /* What we would really like to do here is to filter out all mere
16004          file-scope declarations of file-scope functions which are never
16005          referenced later within this translation unit (and keep all of ones
16006          that *are* referenced later on) but we aren't clairvoyant, so we have
16007          no idea which functions will be referenced in the future (i.e. later
16008          on within the current translation unit). So here we just ignore all
16009          file-scope function declarations which are not also definitions.  If
16010          and when the debugger needs to know something about these functions,
16011          it will have to hunt around and find the DWARF information associated
16012          with the definition of the function.
16013
16014          We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
16015          nodes represent definitions and which ones represent mere
16016          declarations.  We have to check DECL_INITIAL instead. That's because
16017          the C front-end supports some weird semantics for "extern inline"
16018          function definitions.  These can get inlined within the current
16019          translation unit (and thus, we need to generate Dwarf info for their
16020          abstract instances so that the Dwarf info for the concrete inlined
16021          instances can have something to refer to) but the compiler never
16022          generates any out-of-lines instances of such things (despite the fact
16023          that they *are* definitions).
16024
16025          The important point is that the C front-end marks these "extern
16026          inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
16027          them anyway. Note that the C++ front-end also plays some similar games
16028          for inline function definitions appearing within include files which
16029          also contain `#pragma interface' pragmas.  */
16030       if (DECL_INITIAL (decl) == NULL_TREE)
16031         return;
16032
16033       /* If we're a nested function, initially use a parent of NULL; if we're
16034          a plain function, this will be fixed up in decls_for_scope.  If
16035          we're a method, it will be ignored, since we already have a DIE.  */
16036       if (decl_function_context (decl)
16037           /* But if we're in terse mode, we don't care about scope.  */
16038           && debug_info_level > DINFO_LEVEL_TERSE)
16039         context_die = NULL;
16040       break;
16041
16042     case VAR_DECL:
16043       /* Ignore this VAR_DECL if it refers to a file-scope extern data object
16044          declaration and if the declaration was never even referenced from
16045          within this entire compilation unit.  We suppress these DIEs in
16046          order to save space in the .debug section (by eliminating entries
16047          which are probably useless).  Note that we must not suppress
16048          block-local extern declarations (whether used or not) because that
16049          would screw-up the debugger's name lookup mechanism and cause it to
16050          miss things which really ought to be in scope at a given point.  */
16051       if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
16052         return;
16053
16054       /* For local statics lookup proper context die.  */
16055       if (TREE_STATIC (decl) && decl_function_context (decl))
16056         context_die = lookup_decl_die (DECL_CONTEXT (decl));
16057
16058       /* If we are in terse mode, don't generate any DIEs to represent any
16059          variable declarations or definitions.  */
16060       if (debug_info_level <= DINFO_LEVEL_TERSE)
16061         return;
16062       break;
16063
16064     case CONST_DECL:
16065       if (debug_info_level <= DINFO_LEVEL_TERSE)
16066         return;
16067       if (!is_fortran ())
16068         return;
16069       if (TREE_STATIC (decl) && decl_function_context (decl))
16070         context_die = lookup_decl_die (DECL_CONTEXT (decl));
16071       break;
16072
16073     case NAMESPACE_DECL:
16074     case IMPORTED_DECL:
16075       if (debug_info_level <= DINFO_LEVEL_TERSE)
16076         return;
16077       if (lookup_decl_die (decl) != NULL)
16078         return;
16079       break;
16080
16081     case TYPE_DECL:
16082       /* Don't emit stubs for types unless they are needed by other DIEs.  */
16083       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
16084         return;
16085
16086       /* Don't bother trying to generate any DIEs to represent any of the
16087          normal built-in types for the language we are compiling.  */
16088       if (DECL_IS_BUILTIN (decl))
16089         {
16090           /* OK, we need to generate one for `bool' so GDB knows what type
16091              comparisons have.  */
16092           if (is_cxx ()
16093               && TREE_CODE (TREE_TYPE (decl)) == BOOLEAN_TYPE
16094               && ! DECL_IGNORED_P (decl))
16095             modified_type_die (TREE_TYPE (decl), 0, 0, NULL);
16096
16097           return;
16098         }
16099
16100       /* If we are in terse mode, don't generate any DIEs for types.  */
16101       if (debug_info_level <= DINFO_LEVEL_TERSE)
16102         return;
16103
16104       /* If we're a function-scope tag, initially use a parent of NULL;
16105          this will be fixed up in decls_for_scope.  */
16106       if (decl_function_context (decl))
16107         context_die = NULL;
16108
16109       break;
16110
16111     default:
16112       return;
16113     }
16114
16115   gen_decl_die (decl, NULL, context_die);
16116 }
16117
16118 /* Output a marker (i.e. a label) for the beginning of the generated code for
16119    a lexical block.  */
16120
16121 static void
16122 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
16123                        unsigned int blocknum)
16124 {
16125   switch_to_section (current_function_section ());
16126   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
16127 }
16128
16129 /* Output a marker (i.e. a label) for the end of the generated code for a
16130    lexical block.  */
16131
16132 static void
16133 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
16134 {
16135   switch_to_section (current_function_section ());
16136   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
16137 }
16138
16139 /* Returns nonzero if it is appropriate not to emit any debugging
16140    information for BLOCK, because it doesn't contain any instructions.
16141
16142    Don't allow this for blocks with nested functions or local classes
16143    as we would end up with orphans, and in the presence of scheduling
16144    we may end up calling them anyway.  */
16145
16146 static bool
16147 dwarf2out_ignore_block (const_tree block)
16148 {
16149   tree decl;
16150   unsigned int i;
16151
16152   for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
16153     if (TREE_CODE (decl) == FUNCTION_DECL
16154         || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
16155       return 0;
16156   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
16157     {
16158       decl = BLOCK_NONLOCALIZED_VAR (block, i);
16159       if (TREE_CODE (decl) == FUNCTION_DECL
16160           || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
16161       return 0;
16162     }
16163
16164   return 1;
16165 }
16166
16167 /* Hash table routines for file_hash.  */
16168
16169 static int
16170 file_table_eq (const void *p1_p, const void *p2_p)
16171 {
16172   const struct dwarf_file_data *const p1 =
16173     (const struct dwarf_file_data *) p1_p;
16174   const char *const p2 = (const char *) p2_p;
16175   return strcmp (p1->filename, p2) == 0;
16176 }
16177
16178 static hashval_t
16179 file_table_hash (const void *p_p)
16180 {
16181   const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
16182   return htab_hash_string (p->filename);
16183 }
16184
16185 /* Lookup FILE_NAME (in the list of filenames that we know about here in
16186    dwarf2out.c) and return its "index".  The index of each (known) filename is
16187    just a unique number which is associated with only that one filename.  We
16188    need such numbers for the sake of generating labels (in the .debug_sfnames
16189    section) and references to those files numbers (in the .debug_srcinfo
16190    and.debug_macinfo sections).  If the filename given as an argument is not
16191    found in our current list, add it to the list and assign it the next
16192    available unique index number.  In order to speed up searches, we remember
16193    the index of the filename was looked up last.  This handles the majority of
16194    all searches.  */
16195
16196 static struct dwarf_file_data *
16197 lookup_filename (const char *file_name)
16198 {
16199   void ** slot;
16200   struct dwarf_file_data * created;
16201
16202   /* Check to see if the file name that was searched on the previous
16203      call matches this file name.  If so, return the index.  */
16204   if (file_table_last_lookup
16205       && (file_name == file_table_last_lookup->filename
16206           || strcmp (file_table_last_lookup->filename, file_name) == 0))
16207     return file_table_last_lookup;
16208
16209   /* Didn't match the previous lookup, search the table.  */
16210   slot = htab_find_slot_with_hash (file_table, file_name,
16211                                    htab_hash_string (file_name), INSERT);
16212   if (*slot)
16213     return (struct dwarf_file_data *) *slot;
16214
16215   created = GGC_NEW (struct dwarf_file_data);
16216   created->filename = file_name;
16217   created->emitted_number = 0;
16218   *slot = created;
16219   return created;
16220 }
16221
16222 /* If the assembler will construct the file table, then translate the compiler
16223    internal file table number into the assembler file table number, and emit
16224    a .file directive if we haven't already emitted one yet.  The file table
16225    numbers are different because we prune debug info for unused variables and
16226    types, which may include filenames.  */
16227
16228 static int
16229 maybe_emit_file (struct dwarf_file_data * fd)
16230 {
16231   if (! fd->emitted_number)
16232     {
16233       if (last_emitted_file)
16234         fd->emitted_number = last_emitted_file->emitted_number + 1;
16235       else
16236         fd->emitted_number = 1;
16237       last_emitted_file = fd;
16238
16239       if (DWARF2_ASM_LINE_DEBUG_INFO)
16240         {
16241           fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
16242           output_quoted_string (asm_out_file,
16243                                 remap_debug_filename (fd->filename));
16244           fputc ('\n', asm_out_file);
16245         }
16246     }
16247
16248   return fd->emitted_number;
16249 }
16250
16251 /* Replace DW_AT_name for the decl with name.  */
16252  
16253 static void
16254 dwarf2out_set_name (tree decl, tree name)
16255 {
16256   dw_die_ref die;
16257   dw_attr_ref attr;
16258
16259   die = TYPE_SYMTAB_DIE (decl);
16260   if (!die)
16261     return;
16262
16263   attr = get_AT (die, DW_AT_name);
16264   if (attr)
16265     {
16266       struct indirect_string_node *node;
16267
16268       node = find_AT_string (dwarf2_name (name, 0));
16269       /* replace the string.  */
16270       attr->dw_attr_val.v.val_str = node;
16271     }
16272
16273   else
16274     add_name_attribute (die, dwarf2_name (name, 0));
16275 }
16276
16277 /* Called by the final INSN scan whenever we see a var location.  We
16278    use it to drop labels in the right places, and throw the location in
16279    our lookup table.  */
16280
16281 static void
16282 dwarf2out_var_location (rtx loc_note)
16283 {
16284   char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
16285   struct var_loc_node *newloc;
16286   rtx next_real;
16287   static const char *last_label;
16288   static bool last_in_cold_section_p;
16289   tree decl;
16290
16291   if (!DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
16292     return;
16293
16294   next_real = next_real_insn (loc_note);
16295   /* If there are no instructions which would be affected by this note,
16296      don't do anything.  */
16297   if (next_real == NULL_RTX)
16298     return;
16299
16300   newloc = GGC_CNEW (struct var_loc_node);
16301   /* If there were no real insns between note we processed last time
16302      and this note, use the label we emitted last time.  */
16303   if (last_var_location_insn != NULL_RTX
16304       && last_var_location_insn == next_real
16305       && last_in_cold_section_p == in_cold_section_p)
16306     newloc->label = last_label;
16307   else
16308     {
16309       ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
16310       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
16311       loclabel_num++;
16312       newloc->label = ggc_strdup (loclabel);
16313     }
16314   newloc->var_loc_note = loc_note;
16315   newloc->next = NULL;
16316
16317   if (cfun && in_cold_section_p)
16318     newloc->section_label = crtl->subsections.cold_section_label;
16319   else
16320     newloc->section_label = text_section_label;
16321
16322   last_var_location_insn = next_real;
16323   last_label = newloc->label;
16324   last_in_cold_section_p = in_cold_section_p;
16325   decl = NOTE_VAR_LOCATION_DECL (loc_note);
16326   add_var_loc_to_decl (decl, newloc);
16327 }
16328
16329 /* We need to reset the locations at the beginning of each
16330    function. We can't do this in the end_function hook, because the
16331    declarations that use the locations won't have been output when
16332    that hook is called.  Also compute have_multiple_function_sections here.  */
16333
16334 static void
16335 dwarf2out_begin_function (tree fun)
16336 {
16337   htab_empty (decl_loc_table);
16338
16339   if (function_section (fun) != text_section)
16340     have_multiple_function_sections = true;
16341
16342   dwarf2out_note_section_used ();
16343 }
16344
16345 /* Output a label to mark the beginning of a source code line entry
16346    and record information relating to this source line, in
16347    'line_info_table' for later output of the .debug_line section.  */
16348
16349 static void
16350 dwarf2out_source_line (unsigned int line, const char *filename,
16351                        int discriminator, bool is_stmt)
16352 {
16353   static bool last_is_stmt = true;
16354
16355   if (debug_info_level >= DINFO_LEVEL_NORMAL
16356       && line != 0)
16357     {
16358       int file_num = maybe_emit_file (lookup_filename (filename));
16359
16360       switch_to_section (current_function_section ());
16361
16362       /* If requested, emit something human-readable.  */
16363       if (flag_debug_asm)
16364         fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
16365                  filename, line);
16366
16367       if (DWARF2_ASM_LINE_DEBUG_INFO)
16368         {
16369           /* Emit the .loc directive understood by GNU as.  */
16370           fprintf (asm_out_file, "\t.loc %d %d 0", file_num, line);
16371           if (is_stmt != last_is_stmt)
16372             {
16373               fprintf (asm_out_file, " is_stmt %d", is_stmt ? 1 : 0);
16374               last_is_stmt = is_stmt;
16375             }
16376           if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
16377             fprintf (asm_out_file, " discriminator %d", discriminator);
16378           fputc ('\n', asm_out_file);
16379
16380           /* Indicate that line number info exists.  */
16381           line_info_table_in_use++;
16382         }
16383       else if (function_section (current_function_decl) != text_section)
16384         {
16385           dw_separate_line_info_ref line_info;
16386           targetm.asm_out.internal_label (asm_out_file,
16387                                           SEPARATE_LINE_CODE_LABEL,
16388                                           separate_line_info_table_in_use);
16389
16390           /* Expand the line info table if necessary.  */
16391           if (separate_line_info_table_in_use
16392               == separate_line_info_table_allocated)
16393             {
16394               separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
16395               separate_line_info_table
16396                 = GGC_RESIZEVEC (dw_separate_line_info_entry,
16397                                  separate_line_info_table,
16398                                  separate_line_info_table_allocated);
16399               memset (separate_line_info_table
16400                        + separate_line_info_table_in_use,
16401                       0,
16402                       (LINE_INFO_TABLE_INCREMENT
16403                        * sizeof (dw_separate_line_info_entry)));
16404             }
16405
16406           /* Add the new entry at the end of the line_info_table.  */
16407           line_info
16408             = &separate_line_info_table[separate_line_info_table_in_use++];
16409           line_info->dw_file_num = file_num;
16410           line_info->dw_line_num = line;
16411           line_info->function = current_function_funcdef_no;
16412         }
16413       else
16414         {
16415           dw_line_info_ref line_info;
16416
16417           targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL,
16418                                      line_info_table_in_use);
16419
16420           /* Expand the line info table if necessary.  */
16421           if (line_info_table_in_use == line_info_table_allocated)
16422             {
16423               line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
16424               line_info_table
16425                 = GGC_RESIZEVEC (dw_line_info_entry, line_info_table,
16426                                  line_info_table_allocated);
16427               memset (line_info_table + line_info_table_in_use, 0,
16428                       LINE_INFO_TABLE_INCREMENT * sizeof (dw_line_info_entry));
16429             }
16430
16431           /* Add the new entry at the end of the line_info_table.  */
16432           line_info = &line_info_table[line_info_table_in_use++];
16433           line_info->dw_file_num = file_num;
16434           line_info->dw_line_num = line;
16435         }
16436     }
16437 }
16438
16439 /* Record the beginning of a new source file.  */
16440
16441 static void
16442 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
16443 {
16444   if (flag_eliminate_dwarf2_dups)
16445     {
16446       /* Record the beginning of the file for break_out_includes.  */
16447       dw_die_ref bincl_die;
16448
16449       bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die, NULL);
16450       add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
16451     }
16452
16453   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
16454     {
16455       int file_num = maybe_emit_file (lookup_filename (filename));
16456
16457       switch_to_section (debug_macinfo_section);
16458       dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
16459       dw2_asm_output_data_uleb128 (lineno, "Included from line number %d",
16460                                    lineno);
16461
16462       dw2_asm_output_data_uleb128 (file_num, "file %s", filename);
16463     }
16464 }
16465
16466 /* Record the end of a source file.  */
16467
16468 static void
16469 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
16470 {
16471   if (flag_eliminate_dwarf2_dups)
16472     /* Record the end of the file for break_out_includes.  */
16473     new_die (DW_TAG_GNU_EINCL, comp_unit_die, NULL);
16474
16475   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
16476     {
16477       switch_to_section (debug_macinfo_section);
16478       dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
16479     }
16480 }
16481
16482 /* Called from debug_define in toplev.c.  The `buffer' parameter contains
16483    the tail part of the directive line, i.e. the part which is past the
16484    initial whitespace, #, whitespace, directive-name, whitespace part.  */
16485
16486 static void
16487 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
16488                   const char *buffer ATTRIBUTE_UNUSED)
16489 {
16490   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
16491     {
16492       switch_to_section (debug_macinfo_section);
16493       dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
16494       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
16495       dw2_asm_output_nstring (buffer, -1, "The macro");
16496     }
16497 }
16498
16499 /* Called from debug_undef in toplev.c.  The `buffer' parameter contains
16500    the tail part of the directive line, i.e. the part which is past the
16501    initial whitespace, #, whitespace, directive-name, whitespace part.  */
16502
16503 static void
16504 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
16505                  const char *buffer ATTRIBUTE_UNUSED)
16506 {
16507   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
16508     {
16509       switch_to_section (debug_macinfo_section);
16510       dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
16511       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
16512       dw2_asm_output_nstring (buffer, -1, "The macro");
16513     }
16514 }
16515
16516 /* Set up for Dwarf output at the start of compilation.  */
16517
16518 static void
16519 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
16520 {
16521   /* Allocate the file_table.  */
16522   file_table = htab_create_ggc (50, file_table_hash,
16523                                 file_table_eq, NULL);
16524
16525   /* Allocate the decl_die_table.  */
16526   decl_die_table = htab_create_ggc (10, decl_die_table_hash,
16527                                     decl_die_table_eq, NULL);
16528
16529   /* Allocate the decl_loc_table.  */
16530   decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
16531                                     decl_loc_table_eq, NULL);
16532
16533   /* Allocate the initial hunk of the decl_scope_table.  */
16534   decl_scope_table = VEC_alloc (tree, gc, 256);
16535
16536   /* Allocate the initial hunk of the abbrev_die_table.  */
16537   abbrev_die_table = GGC_CNEWVEC (dw_die_ref, ABBREV_DIE_TABLE_INCREMENT);
16538   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
16539   /* Zero-th entry is allocated, but unused.  */
16540   abbrev_die_table_in_use = 1;
16541
16542   /* Allocate the initial hunk of the line_info_table.  */
16543   line_info_table = GGC_CNEWVEC (dw_line_info_entry, LINE_INFO_TABLE_INCREMENT);
16544   line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
16545
16546   /* Zero-th entry is allocated, but unused.  */
16547   line_info_table_in_use = 1;
16548
16549   /* Allocate the pubtypes and pubnames vectors.  */
16550   pubname_table = VEC_alloc (pubname_entry, gc, 32);
16551   pubtype_table = VEC_alloc (pubname_entry, gc, 32);
16552
16553   /* Generate the initial DIE for the .debug section.  Note that the (string)
16554      value given in the DW_AT_name attribute of the DW_TAG_compile_unit DIE
16555      will (typically) be a relative pathname and that this pathname should be
16556      taken as being relative to the directory from which the compiler was
16557      invoked when the given (base) source file was compiled.  We will fill
16558      in this value in dwarf2out_finish.  */
16559   comp_unit_die = gen_compile_unit_die (NULL);
16560
16561   incomplete_types = VEC_alloc (tree, gc, 64);
16562
16563   used_rtx_array = VEC_alloc (rtx, gc, 32);
16564
16565   debug_info_section = get_section (DEBUG_INFO_SECTION,
16566                                     SECTION_DEBUG, NULL);
16567   debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
16568                                       SECTION_DEBUG, NULL);
16569   debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
16570                                        SECTION_DEBUG, NULL);
16571   debug_macinfo_section = get_section (DEBUG_MACINFO_SECTION,
16572                                        SECTION_DEBUG, NULL);
16573   debug_line_section = get_section (DEBUG_LINE_SECTION,
16574                                     SECTION_DEBUG, NULL);
16575   debug_loc_section = get_section (DEBUG_LOC_SECTION,
16576                                    SECTION_DEBUG, NULL);
16577   debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
16578                                         SECTION_DEBUG, NULL);
16579   debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
16580                                         SECTION_DEBUG, NULL);
16581   debug_str_section = get_section (DEBUG_STR_SECTION,
16582                                    DEBUG_STR_SECTION_FLAGS, NULL);
16583   debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
16584                                       SECTION_DEBUG, NULL);
16585   debug_frame_section = get_section (DEBUG_FRAME_SECTION,
16586                                      SECTION_DEBUG, NULL);
16587
16588   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
16589   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
16590                                DEBUG_ABBREV_SECTION_LABEL, 0);
16591   ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
16592   ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
16593                                COLD_TEXT_SECTION_LABEL, 0);
16594   ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
16595
16596   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
16597                                DEBUG_INFO_SECTION_LABEL, 0);
16598   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
16599                                DEBUG_LINE_SECTION_LABEL, 0);
16600   ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
16601                                DEBUG_RANGES_SECTION_LABEL, 0);
16602   switch_to_section (debug_abbrev_section);
16603   ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
16604   switch_to_section (debug_info_section);
16605   ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
16606   switch_to_section (debug_line_section);
16607   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
16608
16609   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
16610     {
16611       switch_to_section (debug_macinfo_section);
16612       ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
16613                                    DEBUG_MACINFO_SECTION_LABEL, 0);
16614       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
16615     }
16616
16617   switch_to_section (text_section);
16618   ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
16619   if (flag_reorder_blocks_and_partition)
16620     {
16621       cold_text_section = unlikely_text_section ();
16622       switch_to_section (cold_text_section);
16623       ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
16624     }
16625 }
16626
16627 /* A helper function for dwarf2out_finish called through
16628    ht_forall.  Emit one queued .debug_str string.  */
16629
16630 static int
16631 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
16632 {
16633   struct indirect_string_node *node = (struct indirect_string_node *) *h;
16634
16635   if (node->form == DW_FORM_strp)
16636     {
16637       switch_to_section (debug_str_section);
16638       ASM_OUTPUT_LABEL (asm_out_file, node->label);
16639       assemble_string (node->str, strlen (node->str) + 1);
16640     }
16641
16642   return 1;
16643 }
16644
16645 #if ENABLE_ASSERT_CHECKING
16646 /* Verify that all marks are clear.  */
16647
16648 static void
16649 verify_marks_clear (dw_die_ref die)
16650 {
16651   dw_die_ref c;
16652
16653   gcc_assert (! die->die_mark);
16654   FOR_EACH_CHILD (die, c, verify_marks_clear (c));
16655 }
16656 #endif /* ENABLE_ASSERT_CHECKING */
16657
16658 /* Clear the marks for a die and its children.
16659    Be cool if the mark isn't set.  */
16660
16661 static void
16662 prune_unmark_dies (dw_die_ref die)
16663 {
16664   dw_die_ref c;
16665
16666   if (die->die_mark)
16667     die->die_mark = 0;
16668   FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
16669 }
16670
16671 /* Given DIE that we're marking as used, find any other dies
16672    it references as attributes and mark them as used.  */
16673
16674 static void
16675 prune_unused_types_walk_attribs (dw_die_ref die)
16676 {
16677   dw_attr_ref a;
16678   unsigned ix;
16679
16680   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
16681     {
16682       if (a->dw_attr_val.val_class == dw_val_class_die_ref)
16683         {
16684           /* A reference to another DIE.
16685              Make sure that it will get emitted.  */
16686           prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
16687         }
16688       /* Set the string's refcount to 0 so that prune_unused_types_mark
16689          accounts properly for it.  */
16690       if (AT_class (a) == dw_val_class_str)
16691         a->dw_attr_val.v.val_str->refcount = 0;
16692     }
16693 }
16694
16695
16696 /* Mark DIE as being used.  If DOKIDS is true, then walk down
16697    to DIE's children.  */
16698
16699 static void
16700 prune_unused_types_mark (dw_die_ref die, int dokids)
16701 {
16702   dw_die_ref c;
16703
16704   if (die->die_mark == 0)
16705     {
16706       /* We haven't done this node yet.  Mark it as used.  */
16707       die->die_mark = 1;
16708
16709       /* We also have to mark its parents as used.
16710          (But we don't want to mark our parents' kids due to this.)  */
16711       if (die->die_parent)
16712         prune_unused_types_mark (die->die_parent, 0);
16713
16714       /* Mark any referenced nodes.  */
16715       prune_unused_types_walk_attribs (die);
16716
16717       /* If this node is a specification,
16718          also mark the definition, if it exists.  */
16719       if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
16720         prune_unused_types_mark (die->die_definition, 1);
16721     }
16722
16723   if (dokids && die->die_mark != 2)
16724     {
16725       /* We need to walk the children, but haven't done so yet.
16726          Remember that we've walked the kids.  */
16727       die->die_mark = 2;
16728
16729       /* If this is an array type, we need to make sure our
16730          kids get marked, even if they're types.  */
16731       if (die->die_tag == DW_TAG_array_type)
16732         FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
16733       else
16734         FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
16735     }
16736 }
16737
16738 /* For local classes, look if any static member functions were emitted
16739    and if so, mark them.  */
16740
16741 static void
16742 prune_unused_types_walk_local_classes (dw_die_ref die)
16743 {
16744   dw_die_ref c;
16745
16746   if (die->die_mark == 2)
16747     return;
16748
16749   switch (die->die_tag)
16750     {
16751     case DW_TAG_structure_type:
16752     case DW_TAG_union_type:
16753     case DW_TAG_class_type:
16754       break;
16755
16756     case DW_TAG_subprogram:
16757       if (!get_AT_flag (die, DW_AT_declaration)
16758           || die->die_definition != NULL)
16759         prune_unused_types_mark (die, 1);
16760       return;
16761
16762     default:
16763       return;
16764     }
16765
16766   /* Mark children.  */
16767   FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
16768 }
16769
16770 /* Walk the tree DIE and mark types that we actually use.  */
16771
16772 static void
16773 prune_unused_types_walk (dw_die_ref die)
16774 {
16775   dw_die_ref c;
16776
16777   /* Don't do anything if this node is already marked and
16778      children have been marked as well.  */
16779   if (die->die_mark == 2)
16780     return;
16781
16782   switch (die->die_tag)
16783     {
16784     case DW_TAG_structure_type:
16785     case DW_TAG_union_type:
16786     case DW_TAG_class_type:
16787       if (die->die_perennial_p)
16788         break;
16789
16790       for (c = die->die_parent; c; c = c->die_parent)
16791         if (c->die_tag == DW_TAG_subprogram)
16792           break;
16793
16794       /* Finding used static member functions inside of classes
16795          is needed just for local classes, because for other classes
16796          static member function DIEs with DW_AT_specification
16797          are emitted outside of the DW_TAG_*_type.  If we ever change
16798          it, we'd need to call this even for non-local classes.  */
16799       if (c)
16800         prune_unused_types_walk_local_classes (die);
16801
16802       /* It's a type node --- don't mark it.  */
16803       return;
16804
16805     case DW_TAG_const_type:
16806     case DW_TAG_packed_type:
16807     case DW_TAG_pointer_type:
16808     case DW_TAG_reference_type:
16809     case DW_TAG_volatile_type:
16810     case DW_TAG_typedef:
16811     case DW_TAG_array_type:
16812     case DW_TAG_interface_type:
16813     case DW_TAG_friend:
16814     case DW_TAG_variant_part:
16815     case DW_TAG_enumeration_type:
16816     case DW_TAG_subroutine_type:
16817     case DW_TAG_string_type:
16818     case DW_TAG_set_type:
16819     case DW_TAG_subrange_type:
16820     case DW_TAG_ptr_to_member_type:
16821     case DW_TAG_file_type:
16822       if (die->die_perennial_p)
16823         break;
16824
16825       /* It's a type node --- don't mark it.  */
16826       return;
16827
16828     default:
16829       /* Mark everything else.  */
16830       break;
16831   }
16832
16833   if (die->die_mark == 0)
16834     {
16835       die->die_mark = 1;
16836
16837       /* Now, mark any dies referenced from here.  */
16838       prune_unused_types_walk_attribs (die);
16839     }
16840
16841   die->die_mark = 2;
16842
16843   /* Mark children.  */
16844   FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
16845 }
16846
16847 /* Increment the string counts on strings referred to from DIE's
16848    attributes.  */
16849
16850 static void
16851 prune_unused_types_update_strings (dw_die_ref die)
16852 {
16853   dw_attr_ref a;
16854   unsigned ix;
16855
16856   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
16857     if (AT_class (a) == dw_val_class_str)
16858       {
16859         struct indirect_string_node *s = a->dw_attr_val.v.val_str;
16860         s->refcount++;
16861         /* Avoid unnecessarily putting strings that are used less than
16862            twice in the hash table.  */
16863         if (s->refcount
16864             == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
16865           {
16866             void ** slot;
16867             slot = htab_find_slot_with_hash (debug_str_hash, s->str,
16868                                              htab_hash_string (s->str),
16869                                              INSERT);
16870             gcc_assert (*slot == NULL);
16871             *slot = s;
16872           }
16873       }
16874 }
16875
16876 /* Remove from the tree DIE any dies that aren't marked.  */
16877
16878 static void
16879 prune_unused_types_prune (dw_die_ref die)
16880 {
16881   dw_die_ref c;
16882
16883   gcc_assert (die->die_mark);
16884   prune_unused_types_update_strings (die);
16885
16886   if (! die->die_child)
16887     return;
16888
16889   c = die->die_child;
16890   do {
16891     dw_die_ref prev = c;
16892     for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
16893       if (c == die->die_child)
16894         {
16895           /* No marked children between 'prev' and the end of the list.  */
16896           if (prev == c)
16897             /* No marked children at all.  */
16898             die->die_child = NULL;
16899           else
16900             {
16901               prev->die_sib = c->die_sib;
16902               die->die_child = prev;
16903             }
16904           return;
16905         }
16906
16907     if (c != prev->die_sib)
16908       prev->die_sib = c;
16909     prune_unused_types_prune (c);
16910   } while (c != die->die_child);
16911 }
16912
16913
16914 /* Remove dies representing declarations that we never use.  */
16915
16916 static void
16917 prune_unused_types (void)
16918 {
16919   unsigned int i;
16920   limbo_die_node *node;
16921   pubname_ref pub;
16922
16923 #if ENABLE_ASSERT_CHECKING
16924   /* All the marks should already be clear.  */
16925   verify_marks_clear (comp_unit_die);
16926   for (node = limbo_die_list; node; node = node->next)
16927     verify_marks_clear (node->die);
16928 #endif /* ENABLE_ASSERT_CHECKING */
16929
16930   /* Set the mark on nodes that are actually used.  */
16931   prune_unused_types_walk (comp_unit_die);
16932   for (node = limbo_die_list; node; node = node->next)
16933     prune_unused_types_walk (node->die);
16934
16935   /* Also set the mark on nodes referenced from the
16936      pubname_table or arange_table.  */
16937   for (i = 0; VEC_iterate (pubname_entry, pubname_table, i, pub); i++)
16938     prune_unused_types_mark (pub->die, 1);
16939   for (i = 0; i < arange_table_in_use; i++)
16940     prune_unused_types_mark (arange_table[i], 1);
16941
16942   /* Get rid of nodes that aren't marked; and update the string counts.  */
16943   if (debug_str_hash)
16944     htab_empty (debug_str_hash);
16945   prune_unused_types_prune (comp_unit_die);
16946   for (node = limbo_die_list; node; node = node->next)
16947     prune_unused_types_prune (node->die);
16948
16949   /* Leave the marks clear.  */
16950   prune_unmark_dies (comp_unit_die);
16951   for (node = limbo_die_list; node; node = node->next)
16952     prune_unmark_dies (node->die);
16953 }
16954
16955 /* Set the parameter to true if there are any relative pathnames in
16956    the file table.  */
16957 static int
16958 file_table_relative_p (void ** slot, void *param)
16959 {
16960   bool *p = (bool *) param;
16961   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
16962   if (!IS_ABSOLUTE_PATH (d->filename))
16963     {
16964       *p = true;
16965       return 0;
16966     }
16967   return 1;
16968 }
16969
16970 /* Move a DW_AT_MIPS_linkage_name attribute just added to dw_die_ref
16971    to the location it would have been added, should we know its
16972    DECL_ASSEMBLER_NAME when we added other attributes.  This will
16973    probably improve compactness of debug info, removing equivalent
16974    abbrevs, and hide any differences caused by deferring the
16975    computation of the assembler name, triggered by e.g. PCH.  */
16976
16977 static inline void
16978 move_linkage_attr (dw_die_ref die)
16979 {
16980   unsigned ix = VEC_length (dw_attr_node, die->die_attr);
16981   dw_attr_node linkage = *VEC_index (dw_attr_node, die->die_attr, ix - 1);
16982
16983   gcc_assert (linkage.dw_attr == DW_AT_MIPS_linkage_name);
16984
16985   while (--ix > 0)
16986     {
16987       dw_attr_node *prev = VEC_index (dw_attr_node, die->die_attr, ix - 1);
16988
16989       if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
16990         break;
16991     }
16992
16993   if (ix != VEC_length (dw_attr_node, die->die_attr) - 1)
16994     {
16995       VEC_pop (dw_attr_node, die->die_attr);
16996       VEC_quick_insert (dw_attr_node, die->die_attr, ix, &linkage);
16997     }
16998 }
16999
17000 /* Output stuff that dwarf requires at the end of every file,
17001    and generate the DWARF-2 debugging info.  */
17002
17003 static void
17004 dwarf2out_finish (const char *filename)
17005 {
17006   limbo_die_node *node, *next_node;
17007   dw_die_ref die = 0;
17008   unsigned int i;
17009
17010   /* Add the name for the main input file now.  We delayed this from
17011      dwarf2out_init to avoid complications with PCH.  */
17012   add_name_attribute (comp_unit_die, remap_debug_filename (filename));
17013   if (!IS_ABSOLUTE_PATH (filename))
17014     add_comp_dir_attribute (comp_unit_die);
17015   else if (get_AT (comp_unit_die, DW_AT_comp_dir) == NULL)
17016     {
17017       bool p = false;
17018       htab_traverse (file_table, file_table_relative_p, &p);
17019       if (p)
17020         add_comp_dir_attribute (comp_unit_die);
17021     }
17022
17023   for (i = 0; i < VEC_length (deferred_locations, deferred_locations_list); i++)
17024     {
17025       add_location_or_const_value_attribute (
17026         VEC_index (deferred_locations, deferred_locations_list, i)->die,
17027         VEC_index (deferred_locations, deferred_locations_list, i)->variable,
17028         DW_AT_location);
17029     }
17030
17031   /* Traverse the limbo die list, and add parent/child links.  The only
17032      dies without parents that should be here are concrete instances of
17033      inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
17034      For concrete instances, we can get the parent die from the abstract
17035      instance.  */
17036   for (node = limbo_die_list; node; node = next_node)
17037     {
17038       next_node = node->next;
17039       die = node->die;
17040
17041       if (die->die_parent == NULL)
17042         {
17043           dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
17044
17045           if (origin)
17046             add_child_die (origin->die_parent, die);
17047           else if (die == comp_unit_die)
17048             ;
17049           else if (errorcount > 0 || sorrycount > 0)
17050             /* It's OK to be confused by errors in the input.  */
17051             add_child_die (comp_unit_die, die);
17052           else
17053             {
17054               /* In certain situations, the lexical block containing a
17055                  nested function can be optimized away, which results
17056                  in the nested function die being orphaned.  Likewise
17057                  with the return type of that nested function.  Force
17058                  this to be a child of the containing function.
17059
17060                  It may happen that even the containing function got fully
17061                  inlined and optimized out.  In that case we are lost and
17062                  assign the empty child.  This should not be big issue as
17063                  the function is likely unreachable too.  */
17064               tree context = NULL_TREE;
17065
17066               gcc_assert (node->created_for);
17067
17068               if (DECL_P (node->created_for))
17069                 context = DECL_CONTEXT (node->created_for);
17070               else if (TYPE_P (node->created_for))
17071                 context = TYPE_CONTEXT (node->created_for);
17072
17073               gcc_assert (context
17074                           && (TREE_CODE (context) == FUNCTION_DECL
17075                               || TREE_CODE (context) == NAMESPACE_DECL));
17076
17077               origin = lookup_decl_die (context);
17078               if (origin)
17079                 add_child_die (origin, die);
17080               else
17081                 add_child_die (comp_unit_die, die);
17082             }
17083         }
17084     }
17085
17086   limbo_die_list = NULL;
17087
17088   for (node = deferred_asm_name; node; node = node->next)
17089     {
17090       tree decl = node->created_for;
17091       if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
17092         {
17093           add_AT_string (node->die, DW_AT_MIPS_linkage_name,
17094                          IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
17095           move_linkage_attr (node->die);
17096         }
17097     }
17098
17099   deferred_asm_name = NULL;
17100
17101   /* Walk through the list of incomplete types again, trying once more to
17102      emit full debugging info for them.  */
17103   retry_incomplete_types ();
17104
17105   if (flag_eliminate_unused_debug_types)
17106     prune_unused_types ();
17107
17108   /* Generate separate CUs for each of the include files we've seen.
17109      They will go into limbo_die_list.  */
17110   if (flag_eliminate_dwarf2_dups)
17111     break_out_includes (comp_unit_die);
17112
17113   /* Traverse the DIE's and add add sibling attributes to those DIE's
17114      that have children.  */
17115   add_sibling_attributes (comp_unit_die);
17116   for (node = limbo_die_list; node; node = node->next)
17117     add_sibling_attributes (node->die);
17118
17119   /* Output a terminator label for the .text section.  */
17120   switch_to_section (text_section);
17121   targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
17122   if (flag_reorder_blocks_and_partition)
17123     {
17124       switch_to_section (unlikely_text_section ());
17125       targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
17126     }
17127
17128   /* We can only use the low/high_pc attributes if all of the code was
17129      in .text.  */
17130   if (!have_multiple_function_sections)
17131     {
17132       add_AT_lbl_id (comp_unit_die, DW_AT_low_pc, text_section_label);
17133       add_AT_lbl_id (comp_unit_die, DW_AT_high_pc, text_end_label);
17134     }
17135
17136   else
17137     {
17138       unsigned fde_idx = 0;
17139
17140       /* We need to give .debug_loc and .debug_ranges an appropriate
17141          "base address".  Use zero so that these addresses become
17142          absolute.  Historically, we've emitted the unexpected
17143          DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
17144          Emit both to give time for other tools to adapt.  */
17145       add_AT_addr (comp_unit_die, DW_AT_low_pc, const0_rtx);
17146       add_AT_addr (comp_unit_die, DW_AT_entry_pc, const0_rtx);
17147
17148       add_AT_range_list (comp_unit_die, DW_AT_ranges,
17149                          add_ranges_by_labels (text_section_label,
17150                                                text_end_label));
17151       if (flag_reorder_blocks_and_partition)
17152         add_ranges_by_labels (cold_text_section_label,
17153                               cold_end_label);
17154
17155       for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
17156         {
17157           dw_fde_ref fde = &fde_table[fde_idx];
17158
17159           if (fde->dw_fde_switched_sections)
17160             {
17161               if (!fde->in_std_section)
17162                 add_ranges_by_labels (fde->dw_fde_hot_section_label,
17163                                       fde->dw_fde_hot_section_end_label);
17164               if (!fde->cold_in_std_section)
17165                 add_ranges_by_labels (fde->dw_fde_unlikely_section_label,
17166                                       fde->dw_fde_unlikely_section_end_label);
17167             }
17168           else if (!fde->in_std_section)
17169             add_ranges_by_labels (fde->dw_fde_begin,
17170                                   fde->dw_fde_end);
17171         }
17172
17173       add_ranges (NULL);
17174     }
17175
17176   /* Output location list section if necessary.  */
17177   if (have_location_lists)
17178     {
17179       /* Output the location lists info.  */
17180       switch_to_section (debug_loc_section);
17181       ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
17182                                    DEBUG_LOC_SECTION_LABEL, 0);
17183       ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
17184       output_location_lists (die);
17185     }
17186
17187   if (debug_info_level >= DINFO_LEVEL_NORMAL)
17188     add_AT_lineptr (comp_unit_die, DW_AT_stmt_list,
17189                     debug_line_section_label);
17190
17191   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
17192     add_AT_macptr (comp_unit_die, DW_AT_macro_info, macinfo_section_label);
17193
17194   /* Output all of the compilation units.  We put the main one last so that
17195      the offsets are available to output_pubnames.  */
17196   for (node = limbo_die_list; node; node = node->next)
17197     output_comp_unit (node->die, 0);
17198
17199   /* Output the main compilation unit if non-empty or if .debug_macinfo
17200      has been emitted.  */
17201   output_comp_unit (comp_unit_die, debug_info_level >= DINFO_LEVEL_VERBOSE);
17202
17203   /* Output the abbreviation table.  */
17204   switch_to_section (debug_abbrev_section);
17205   output_abbrev_section ();
17206
17207   /* Output public names table if necessary.  */
17208   if (!VEC_empty (pubname_entry, pubname_table))
17209     {
17210       switch_to_section (debug_pubnames_section);
17211       output_pubnames (pubname_table);
17212     }
17213
17214   /* Output public types table if necessary.  */
17215   /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
17216      It shouldn't hurt to emit it always, since pure DWARF2 consumers
17217      simply won't look for the section.  */
17218   if (!VEC_empty (pubname_entry, pubtype_table))
17219     {
17220       switch_to_section (debug_pubtypes_section);
17221       output_pubnames (pubtype_table);
17222     }
17223
17224   /* Output the address range information.  We only put functions in the arange
17225      table, so don't write it out if we don't have any.  */
17226   if (fde_table_in_use)
17227     {
17228       switch_to_section (debug_aranges_section);
17229       output_aranges ();
17230     }
17231
17232   /* Output ranges section if necessary.  */
17233   if (ranges_table_in_use)
17234     {
17235       switch_to_section (debug_ranges_section);
17236       ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
17237       output_ranges ();
17238     }
17239
17240   /* Output the source line correspondence table.  We must do this
17241      even if there is no line information.  Otherwise, on an empty
17242      translation unit, we will generate a present, but empty,
17243      .debug_info section.  IRIX 6.5 `nm' will then complain when
17244      examining the file.  This is done late so that any filenames
17245      used by the debug_info section are marked as 'used'.  */
17246   if (! DWARF2_ASM_LINE_DEBUG_INFO)
17247     {
17248       switch_to_section (debug_line_section);
17249       output_line_info ();
17250     }
17251
17252   /* Have to end the macro section.  */
17253   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
17254     {
17255       switch_to_section (debug_macinfo_section);
17256       dw2_asm_output_data (1, 0, "End compilation unit");
17257     }
17258
17259   /* If we emitted any DW_FORM_strp form attribute, output the string
17260      table too.  */
17261   if (debug_str_hash)
17262     htab_traverse (debug_str_hash, output_indirect_string, NULL);
17263 }
17264 #else
17265
17266 /* This should never be used, but its address is needed for comparisons.  */
17267 const struct gcc_debug_hooks dwarf2_debug_hooks =
17268 {
17269   0,            /* init */
17270   0,            /* finish */
17271   0,            /* define */
17272   0,            /* undef */
17273   0,            /* start_source_file */
17274   0,            /* end_source_file */
17275   0,            /* begin_block */
17276   0,            /* end_block */
17277   0,            /* ignore_block */
17278   0,            /* source_line */
17279   0,            /* begin_prologue */
17280   0,            /* end_prologue */
17281   0,            /* end_epilogue */
17282   0,            /* begin_function */
17283   0,            /* end_function */
17284   0,            /* function_decl */
17285   0,            /* global_decl */
17286   0,            /* type_decl */
17287   0,            /* imported_module_or_decl */
17288   0,            /* deferred_inline_function */
17289   0,            /* outlining_inline_function */
17290   0,            /* label */
17291   0,            /* handle_pch */
17292   0,            /* var_location */
17293   0,            /* switch_text_section */
17294   0,            /* set_name */
17295   0             /* start_end_main_source_file */
17296 };
17297
17298 #endif /* DWARF2_DEBUGGING_INFO */
17299
17300 #include "gt-dwarf2out.h"