OSDN Git Service

PR testsuite/40532
[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 }
287 dw_fde_node;
288
289 /* Maximum size (in bytes) of an artificially generated label.  */
290 #define MAX_ARTIFICIAL_LABEL_BYTES      30
291
292 /* The size of addresses as they appear in the Dwarf 2 data.
293    Some architectures use word addresses to refer to code locations,
294    but Dwarf 2 info always uses byte addresses.  On such machines,
295    Dwarf 2 addresses need to be larger than the architecture's
296    pointers.  */
297 #ifndef DWARF2_ADDR_SIZE
298 #define DWARF2_ADDR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
299 #endif
300
301 /* The size in bytes of a DWARF field indicating an offset or length
302    relative to a debug info section, specified to be 4 bytes in the
303    DWARF-2 specification.  The SGI/MIPS ABI defines it to be the same
304    as PTR_SIZE.  */
305
306 #ifndef DWARF_OFFSET_SIZE
307 #define DWARF_OFFSET_SIZE 4
308 #endif
309
310 /* According to the (draft) DWARF 3 specification, the initial length
311    should either be 4 or 12 bytes.  When it's 12 bytes, the first 4
312    bytes are 0xffffffff, followed by the length stored in the next 8
313    bytes.
314
315    However, the SGI/MIPS ABI uses an initial length which is equal to
316    DWARF_OFFSET_SIZE.  It is defined (elsewhere) accordingly.  */
317
318 #ifndef DWARF_INITIAL_LENGTH_SIZE
319 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
320 #endif
321
322 /* Round SIZE up to the nearest BOUNDARY.  */
323 #define DWARF_ROUND(SIZE,BOUNDARY) \
324   ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
325
326 /* Offsets recorded in opcodes are a multiple of this alignment factor.  */
327 #ifndef DWARF_CIE_DATA_ALIGNMENT
328 #ifdef STACK_GROWS_DOWNWARD
329 #define DWARF_CIE_DATA_ALIGNMENT (-((int) UNITS_PER_WORD))
330 #else
331 #define DWARF_CIE_DATA_ALIGNMENT ((int) UNITS_PER_WORD)
332 #endif
333 #endif
334
335 /* CIE identifier.  */
336 #if HOST_BITS_PER_WIDE_INT >= 64
337 #define DWARF_CIE_ID \
338   (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
339 #else
340 #define DWARF_CIE_ID DW_CIE_ID
341 #endif
342
343 /* A pointer to the base of a table that contains frame description
344    information for each routine.  */
345 static GTY((length ("fde_table_allocated"))) dw_fde_ref fde_table;
346
347 /* Number of elements currently allocated for fde_table.  */
348 static GTY(()) unsigned fde_table_allocated;
349
350 /* Number of elements in fde_table currently in use.  */
351 static GTY(()) unsigned fde_table_in_use;
352
353 /* Size (in elements) of increments by which we may expand the
354    fde_table.  */
355 #define FDE_TABLE_INCREMENT 256
356
357 /* Get the current fde_table entry we should use.  */
358
359 static inline dw_fde_ref
360 current_fde (void)
361 {
362   return fde_table_in_use ? &fde_table[fde_table_in_use - 1] : NULL;
363 }
364
365 /* A list of call frame insns for the CIE.  */
366 static GTY(()) dw_cfi_ref cie_cfi_head;
367
368 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
369 /* Some DWARF extensions (e.g., MIPS/SGI) implement a subprogram
370    attribute that accelerates the lookup of the FDE associated
371    with the subprogram.  This variable holds the table index of the FDE
372    associated with the current function (body) definition.  */
373 static unsigned current_funcdef_fde;
374 #endif
375
376 struct GTY(()) indirect_string_node {
377   const char *str;
378   unsigned int refcount;
379   enum dwarf_form form;
380   char *label;
381 };
382
383 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
384
385 static GTY(()) int dw2_string_counter;
386 static GTY(()) unsigned long dwarf2out_cfi_label_num;
387
388 /* True if the compilation unit places functions in more than one section.  */
389 static GTY(()) bool have_multiple_function_sections = false;
390
391 /* Whether the default text and cold text sections have been used at all.  */
392
393 static GTY(()) bool text_section_used = false;
394 static GTY(()) bool cold_text_section_used = false;
395
396 /* The default cold text section.  */
397 static GTY(()) section *cold_text_section;
398
399 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
400
401 /* Forward declarations for functions defined in this file.  */
402
403 static char *stripattributes (const char *);
404 static const char *dwarf_cfi_name (unsigned);
405 static dw_cfi_ref new_cfi (void);
406 static void add_cfi (dw_cfi_ref *, dw_cfi_ref);
407 static void add_fde_cfi (const char *, dw_cfi_ref);
408 static void lookup_cfa_1 (dw_cfi_ref, dw_cfa_location *, dw_cfa_location *);
409 static void lookup_cfa (dw_cfa_location *);
410 static void reg_save (const char *, unsigned, unsigned, HOST_WIDE_INT);
411 #ifdef DWARF2_UNWIND_INFO
412 static void initial_return_save (rtx);
413 #endif
414 static HOST_WIDE_INT stack_adjust_offset (const_rtx, HOST_WIDE_INT,
415                                           HOST_WIDE_INT);
416 static void output_cfi (dw_cfi_ref, dw_fde_ref, int);
417 static void output_cfi_directive (dw_cfi_ref);
418 static void output_call_frame_info (int);
419 static void dwarf2out_note_section_used (void);
420 static void dwarf2out_stack_adjust (rtx, bool);
421 static void dwarf2out_args_size_adjust (HOST_WIDE_INT, const char *);
422 static void flush_queued_reg_saves (void);
423 static bool clobbers_queued_reg_save (const_rtx);
424 static void dwarf2out_frame_debug_expr (rtx, const char *);
425
426 /* Support for complex CFA locations.  */
427 static void output_cfa_loc (dw_cfi_ref);
428 static void output_cfa_loc_raw (dw_cfi_ref);
429 static void get_cfa_from_loc_descr (dw_cfa_location *,
430                                     struct dw_loc_descr_struct *);
431 static struct dw_loc_descr_struct *build_cfa_loc
432   (dw_cfa_location *, HOST_WIDE_INT);
433 static struct dw_loc_descr_struct *build_cfa_aligned_loc
434   (HOST_WIDE_INT, HOST_WIDE_INT);
435 static void def_cfa_1 (const char *, dw_cfa_location *);
436
437 /* How to start an assembler comment.  */
438 #ifndef ASM_COMMENT_START
439 #define ASM_COMMENT_START ";#"
440 #endif
441
442 /* Data and reference forms for relocatable data.  */
443 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
444 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
445
446 #ifndef DEBUG_FRAME_SECTION
447 #define DEBUG_FRAME_SECTION     ".debug_frame"
448 #endif
449
450 #ifndef FUNC_BEGIN_LABEL
451 #define FUNC_BEGIN_LABEL        "LFB"
452 #endif
453
454 #ifndef FUNC_END_LABEL
455 #define FUNC_END_LABEL          "LFE"
456 #endif
457
458 #ifndef FRAME_BEGIN_LABEL
459 #define FRAME_BEGIN_LABEL       "Lframe"
460 #endif
461 #define CIE_AFTER_SIZE_LABEL    "LSCIE"
462 #define CIE_END_LABEL           "LECIE"
463 #define FDE_LABEL               "LSFDE"
464 #define FDE_AFTER_SIZE_LABEL    "LASFDE"
465 #define FDE_END_LABEL           "LEFDE"
466 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
467 #define LINE_NUMBER_END_LABEL   "LELT"
468 #define LN_PROLOG_AS_LABEL      "LASLTP"
469 #define LN_PROLOG_END_LABEL     "LELTP"
470 #define DIE_LABEL_PREFIX        "DW"
471
472 /* The DWARF 2 CFA column which tracks the return address.  Normally this
473    is the column for PC, or the first column after all of the hard
474    registers.  */
475 #ifndef DWARF_FRAME_RETURN_COLUMN
476 #ifdef PC_REGNUM
477 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGNUM (PC_REGNUM)
478 #else
479 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGISTERS
480 #endif
481 #endif
482
483 /* The mapping from gcc register number to DWARF 2 CFA column number.  By
484    default, we just provide columns for all registers.  */
485 #ifndef DWARF_FRAME_REGNUM
486 #define DWARF_FRAME_REGNUM(REG) DBX_REGISTER_NUMBER (REG)
487 #endif
488 \f
489 /* Hook used by __throw.  */
490
491 rtx
492 expand_builtin_dwarf_sp_column (void)
493 {
494   unsigned int dwarf_regnum = DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM);
495   return GEN_INT (DWARF2_FRAME_REG_OUT (dwarf_regnum, 1));
496 }
497
498 /* Return a pointer to a copy of the section string name S with all
499    attributes stripped off, and an asterisk prepended (for assemble_name).  */
500
501 static inline char *
502 stripattributes (const char *s)
503 {
504   char *stripped = XNEWVEC (char, strlen (s) + 2);
505   char *p = stripped;
506
507   *p++ = '*';
508
509   while (*s && *s != ',')
510     *p++ = *s++;
511
512   *p = '\0';
513   return stripped;
514 }
515
516 /* MEM is a memory reference for the register size table, each element of
517    which has mode MODE.  Initialize column C as a return address column.  */
518
519 static void
520 init_return_column_size (enum machine_mode mode, rtx mem, unsigned int c)
521 {
522   HOST_WIDE_INT offset = c * GET_MODE_SIZE (mode);
523   HOST_WIDE_INT size = GET_MODE_SIZE (Pmode);
524   emit_move_insn (adjust_address (mem, mode, offset), GEN_INT (size));
525 }
526
527 /* Divide OFF by DWARF_CIE_DATA_ALIGNMENT, asserting no remainder.  */
528
529 static inline HOST_WIDE_INT
530 div_data_align (HOST_WIDE_INT off)
531 {
532   HOST_WIDE_INT r = off / DWARF_CIE_DATA_ALIGNMENT;
533   gcc_assert (r * DWARF_CIE_DATA_ALIGNMENT == off);
534   return r;
535 }
536
537 /* Return true if we need a signed version of a given opcode
538    (e.g. DW_CFA_offset_extended_sf vs DW_CFA_offset_extended).  */
539
540 static inline bool
541 need_data_align_sf_opcode (HOST_WIDE_INT off)
542 {
543   return DWARF_CIE_DATA_ALIGNMENT < 0 ? off > 0 : off < 0;
544 }
545
546 /* Generate code to initialize the register size table.  */
547
548 void
549 expand_builtin_init_dwarf_reg_sizes (tree address)
550 {
551   unsigned int i;
552   enum machine_mode mode = TYPE_MODE (char_type_node);
553   rtx addr = expand_normal (address);
554   rtx mem = gen_rtx_MEM (BLKmode, addr);
555   bool wrote_return_column = false;
556
557   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
558     {
559       int rnum = DWARF2_FRAME_REG_OUT (DWARF_FRAME_REGNUM (i), 1);
560
561       if (rnum < DWARF_FRAME_REGISTERS)
562         {
563           HOST_WIDE_INT offset = rnum * GET_MODE_SIZE (mode);
564           enum machine_mode save_mode = reg_raw_mode[i];
565           HOST_WIDE_INT size;
566
567           if (HARD_REGNO_CALL_PART_CLOBBERED (i, save_mode))
568             save_mode = choose_hard_reg_mode (i, 1, true);
569           if (DWARF_FRAME_REGNUM (i) == DWARF_FRAME_RETURN_COLUMN)
570             {
571               if (save_mode == VOIDmode)
572                 continue;
573               wrote_return_column = true;
574             }
575           size = GET_MODE_SIZE (save_mode);
576           if (offset < 0)
577             continue;
578
579           emit_move_insn (adjust_address (mem, mode, offset),
580                           gen_int_mode (size, mode));
581         }
582     }
583
584   if (!wrote_return_column)
585     init_return_column_size (mode, mem, DWARF_FRAME_RETURN_COLUMN);
586
587 #ifdef DWARF_ALT_FRAME_RETURN_COLUMN
588   init_return_column_size (mode, mem, DWARF_ALT_FRAME_RETURN_COLUMN);
589 #endif
590
591   targetm.init_dwarf_reg_sizes_extra (address);
592 }
593
594 /* Convert a DWARF call frame info. operation to its string name */
595
596 static const char *
597 dwarf_cfi_name (unsigned int cfi_opc)
598 {
599   switch (cfi_opc)
600     {
601     case DW_CFA_advance_loc:
602       return "DW_CFA_advance_loc";
603     case DW_CFA_offset:
604       return "DW_CFA_offset";
605     case DW_CFA_restore:
606       return "DW_CFA_restore";
607     case DW_CFA_nop:
608       return "DW_CFA_nop";
609     case DW_CFA_set_loc:
610       return "DW_CFA_set_loc";
611     case DW_CFA_advance_loc1:
612       return "DW_CFA_advance_loc1";
613     case DW_CFA_advance_loc2:
614       return "DW_CFA_advance_loc2";
615     case DW_CFA_advance_loc4:
616       return "DW_CFA_advance_loc4";
617     case DW_CFA_offset_extended:
618       return "DW_CFA_offset_extended";
619     case DW_CFA_restore_extended:
620       return "DW_CFA_restore_extended";
621     case DW_CFA_undefined:
622       return "DW_CFA_undefined";
623     case DW_CFA_same_value:
624       return "DW_CFA_same_value";
625     case DW_CFA_register:
626       return "DW_CFA_register";
627     case DW_CFA_remember_state:
628       return "DW_CFA_remember_state";
629     case DW_CFA_restore_state:
630       return "DW_CFA_restore_state";
631     case DW_CFA_def_cfa:
632       return "DW_CFA_def_cfa";
633     case DW_CFA_def_cfa_register:
634       return "DW_CFA_def_cfa_register";
635     case DW_CFA_def_cfa_offset:
636       return "DW_CFA_def_cfa_offset";
637
638     /* DWARF 3 */
639     case DW_CFA_def_cfa_expression:
640       return "DW_CFA_def_cfa_expression";
641     case DW_CFA_expression:
642       return "DW_CFA_expression";
643     case DW_CFA_offset_extended_sf:
644       return "DW_CFA_offset_extended_sf";
645     case DW_CFA_def_cfa_sf:
646       return "DW_CFA_def_cfa_sf";
647     case DW_CFA_def_cfa_offset_sf:
648       return "DW_CFA_def_cfa_offset_sf";
649
650     /* SGI/MIPS specific */
651     case DW_CFA_MIPS_advance_loc8:
652       return "DW_CFA_MIPS_advance_loc8";
653
654     /* GNU extensions */
655     case DW_CFA_GNU_window_save:
656       return "DW_CFA_GNU_window_save";
657     case DW_CFA_GNU_args_size:
658       return "DW_CFA_GNU_args_size";
659     case DW_CFA_GNU_negative_offset_extended:
660       return "DW_CFA_GNU_negative_offset_extended";
661
662     default:
663       return "DW_CFA_<unknown>";
664     }
665 }
666
667 /* Return a pointer to a newly allocated Call Frame Instruction.  */
668
669 static inline dw_cfi_ref
670 new_cfi (void)
671 {
672   dw_cfi_ref cfi = GGC_NEW (dw_cfi_node);
673
674   cfi->dw_cfi_next = NULL;
675   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = 0;
676   cfi->dw_cfi_oprnd2.dw_cfi_reg_num = 0;
677
678   return cfi;
679 }
680
681 /* Add a Call Frame Instruction to list of instructions.  */
682
683 static inline void
684 add_cfi (dw_cfi_ref *list_head, dw_cfi_ref cfi)
685 {
686   dw_cfi_ref *p;
687   dw_fde_ref fde = current_fde ();
688
689   /* When DRAP is used, CFA is defined with an expression.  Redefine
690      CFA may lead to a different CFA value.   */
691   /* ??? Of course, this heuristic fails when we're annotating epilogues,
692      because of course we'll always want to redefine the CFA back to the
693      stack pointer on the way out.  Where should we move this check?  */
694   if (0 && fde && fde->drap_reg != INVALID_REGNUM)
695     switch (cfi->dw_cfi_opc)
696       {
697         case DW_CFA_def_cfa_register:
698         case DW_CFA_def_cfa_offset:
699         case DW_CFA_def_cfa_offset_sf:
700         case DW_CFA_def_cfa:
701         case DW_CFA_def_cfa_sf:
702           gcc_unreachable ();
703
704         default:
705           break;
706       }
707
708   /* Find the end of the chain.  */
709   for (p = list_head; (*p) != NULL; p = &(*p)->dw_cfi_next)
710     ;
711
712   *p = cfi;
713 }
714
715 /* Generate a new label for the CFI info to refer to.  FORCE is true
716    if a label needs to be output even when using .cfi_* directives.  */
717
718 char *
719 dwarf2out_cfi_label (bool force)
720 {
721   static char label[20];
722
723   if (!force && dwarf2out_do_cfi_asm ())
724     {
725       /* In this case, we will be emitting the asm directive instead of
726          the label, so just return a placeholder to keep the rest of the
727          interfaces happy.  */
728       strcpy (label, "<do not output>");
729     }
730   else
731     {
732       ASM_GENERATE_INTERNAL_LABEL (label, "LCFI", dwarf2out_cfi_label_num++);
733       ASM_OUTPUT_LABEL (asm_out_file, label);
734     }
735
736   return label;
737 }
738
739 /* True if remember_state should be emitted before following CFI directive.  */
740 static bool emit_cfa_remember;
741
742 /* Add CFI to the current fde at the PC value indicated by LABEL if specified,
743    or to the CIE if LABEL is NULL.  */
744
745 static void
746 add_fde_cfi (const char *label, dw_cfi_ref cfi)
747 {
748   dw_cfi_ref *list_head;
749
750   if (emit_cfa_remember)
751     {
752       dw_cfi_ref cfi_remember;
753
754       /* Emit the state save.  */
755       emit_cfa_remember = false;
756       cfi_remember = new_cfi (); 
757       cfi_remember->dw_cfi_opc = DW_CFA_remember_state;
758       add_fde_cfi (label, cfi_remember);
759     }
760
761   list_head = &cie_cfi_head;
762
763   if (dwarf2out_do_cfi_asm ())
764     {
765       if (label)
766         {
767           dw_fde_ref fde = current_fde ();
768
769           gcc_assert (fde != NULL);
770
771           /* We still have to add the cfi to the list so that lookup_cfa
772              works later on.  When -g2 and above we even need to force
773              emitting of CFI labels and add to list a DW_CFA_set_loc for
774              convert_cfa_to_fb_loc_list purposes.  If we're generating
775              DWARF3 output we use DW_OP_call_frame_cfa and so don't use
776              convert_cfa_to_fb_loc_list.  */
777           if (dwarf_version == 2
778               && debug_info_level > DINFO_LEVEL_TERSE
779               && (write_symbols == DWARF2_DEBUG
780                   || write_symbols == VMS_AND_DWARF2_DEBUG))
781             {
782               switch (cfi->dw_cfi_opc)
783                 {
784                 case DW_CFA_def_cfa_offset:
785                 case DW_CFA_def_cfa_offset_sf:
786                 case DW_CFA_def_cfa_register:
787                 case DW_CFA_def_cfa:
788                 case DW_CFA_def_cfa_sf:
789                 case DW_CFA_def_cfa_expression:
790                 case DW_CFA_restore_state:
791                   if (*label == 0 || strcmp (label, "<do not output>") == 0)
792                     label = dwarf2out_cfi_label (true);
793
794                   if (fde->dw_fde_current_label == NULL
795                       || strcmp (label, fde->dw_fde_current_label) != 0)
796                     {
797                       dw_cfi_ref xcfi;
798
799                       label = xstrdup (label);
800
801                       /* Set the location counter to the new label.  */
802                       xcfi = new_cfi ();
803                       /* It doesn't metter whether DW_CFA_set_loc
804                          or DW_CFA_advance_loc4 is added here, those aren't
805                          emitted into assembly, only looked up by
806                          convert_cfa_to_fb_loc_list.  */
807                       xcfi->dw_cfi_opc = DW_CFA_set_loc;
808                       xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
809                       add_cfi (&fde->dw_fde_cfi, xcfi);
810                       fde->dw_fde_current_label = label;
811                     }
812                   break;
813                 default:
814                   break;
815                 }
816             }
817
818           output_cfi_directive (cfi);
819
820           list_head = &fde->dw_fde_cfi;
821         }
822       /* ??? If this is a CFI for the CIE, we don't emit.  This
823          assumes that the standard CIE contents that the assembler
824          uses matches the standard CIE contents that the compiler
825          uses.  This is probably a bad assumption.  I'm not quite
826          sure how to address this for now.  */
827     }
828   else if (label)
829     {
830       dw_fde_ref fde = current_fde ();
831
832       gcc_assert (fde != NULL);
833
834       if (*label == 0)
835         label = dwarf2out_cfi_label (false);
836
837       if (fde->dw_fde_current_label == NULL
838           || strcmp (label, fde->dw_fde_current_label) != 0)
839         {
840           dw_cfi_ref xcfi;
841
842           label = xstrdup (label);
843
844           /* Set the location counter to the new label.  */
845           xcfi = new_cfi ();
846           /* If we have a current label, advance from there, otherwise
847              set the location directly using set_loc.  */
848           xcfi->dw_cfi_opc = fde->dw_fde_current_label
849                              ? DW_CFA_advance_loc4
850                              : DW_CFA_set_loc;
851           xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
852           add_cfi (&fde->dw_fde_cfi, xcfi);
853
854           fde->dw_fde_current_label = label;
855         }
856
857       list_head = &fde->dw_fde_cfi;
858     }
859
860   add_cfi (list_head, cfi);
861 }
862
863 /* Subroutine of lookup_cfa.  */
864
865 static void
866 lookup_cfa_1 (dw_cfi_ref cfi, dw_cfa_location *loc, dw_cfa_location *remember)
867 {
868   switch (cfi->dw_cfi_opc)
869     {
870     case DW_CFA_def_cfa_offset:
871     case DW_CFA_def_cfa_offset_sf:
872       loc->offset = cfi->dw_cfi_oprnd1.dw_cfi_offset;
873       break;
874     case DW_CFA_def_cfa_register:
875       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
876       break;
877     case DW_CFA_def_cfa:
878     case DW_CFA_def_cfa_sf:
879       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
880       loc->offset = cfi->dw_cfi_oprnd2.dw_cfi_offset;
881       break;
882     case DW_CFA_def_cfa_expression:
883       get_cfa_from_loc_descr (loc, cfi->dw_cfi_oprnd1.dw_cfi_loc);
884       break;
885
886     case DW_CFA_remember_state:
887       gcc_assert (!remember->in_use);
888       *remember = *loc;
889       remember->in_use = 1;
890       break;
891     case DW_CFA_restore_state:
892       gcc_assert (remember->in_use);
893       *loc = *remember;
894       remember->in_use = 0;
895       break;
896
897     default:
898       break;
899     }
900 }
901
902 /* Find the previous value for the CFA.  */
903
904 static void
905 lookup_cfa (dw_cfa_location *loc)
906 {
907   dw_cfi_ref cfi;
908   dw_fde_ref fde;
909   dw_cfa_location remember;
910
911   memset (loc, 0, sizeof (*loc));
912   loc->reg = INVALID_REGNUM;
913   remember = *loc;
914
915   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
916     lookup_cfa_1 (cfi, loc, &remember);
917
918   fde = current_fde ();
919   if (fde)
920     for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
921       lookup_cfa_1 (cfi, loc, &remember);
922 }
923
924 /* The current rule for calculating the DWARF2 canonical frame address.  */
925 static dw_cfa_location cfa;
926
927 /* The register used for saving registers to the stack, and its offset
928    from the CFA.  */
929 static dw_cfa_location cfa_store;
930
931 /* The current save location around an epilogue.  */
932 static dw_cfa_location cfa_remember;
933
934 /* The running total of the size of arguments pushed onto the stack.  */
935 static HOST_WIDE_INT args_size;
936
937 /* The last args_size we actually output.  */
938 static HOST_WIDE_INT old_args_size;
939
940 /* Entry point to update the canonical frame address (CFA).
941    LABEL is passed to add_fde_cfi.  The value of CFA is now to be
942    calculated from REG+OFFSET.  */
943
944 void
945 dwarf2out_def_cfa (const char *label, unsigned int reg, HOST_WIDE_INT offset)
946 {
947   dw_cfa_location loc;
948   loc.indirect = 0;
949   loc.base_offset = 0;
950   loc.reg = reg;
951   loc.offset = offset;
952   def_cfa_1 (label, &loc);
953 }
954
955 /* Determine if two dw_cfa_location structures define the same data.  */
956
957 static bool
958 cfa_equal_p (const dw_cfa_location *loc1, const dw_cfa_location *loc2)
959 {
960   return (loc1->reg == loc2->reg
961           && loc1->offset == loc2->offset
962           && loc1->indirect == loc2->indirect
963           && (loc1->indirect == 0
964               || loc1->base_offset == loc2->base_offset));
965 }
966
967 /* This routine does the actual work.  The CFA is now calculated from
968    the dw_cfa_location structure.  */
969
970 static void
971 def_cfa_1 (const char *label, dw_cfa_location *loc_p)
972 {
973   dw_cfi_ref cfi;
974   dw_cfa_location old_cfa, loc;
975
976   cfa = *loc_p;
977   loc = *loc_p;
978
979   if (cfa_store.reg == loc.reg && loc.indirect == 0)
980     cfa_store.offset = loc.offset;
981
982   loc.reg = DWARF_FRAME_REGNUM (loc.reg);
983   lookup_cfa (&old_cfa);
984
985   /* If nothing changed, no need to issue any call frame instructions.  */
986   if (cfa_equal_p (&loc, &old_cfa))
987     return;
988
989   cfi = new_cfi ();
990
991   if (loc.reg == old_cfa.reg && !loc.indirect)
992     {
993       /* Construct a "DW_CFA_def_cfa_offset <offset>" instruction, indicating
994          the CFA register did not change but the offset did.  The data 
995          factoring for DW_CFA_def_cfa_offset_sf happens in output_cfi, or
996          in the assembler via the .cfi_def_cfa_offset directive.  */
997       if (need_data_align_sf_opcode (loc.offset))
998         cfi->dw_cfi_opc = DW_CFA_def_cfa_offset_sf;
999       else
1000         cfi->dw_cfi_opc = DW_CFA_def_cfa_offset;
1001       cfi->dw_cfi_oprnd1.dw_cfi_offset = loc.offset;
1002     }
1003
1004 #ifndef MIPS_DEBUGGING_INFO  /* SGI dbx thinks this means no offset.  */
1005   else if (loc.offset == old_cfa.offset
1006            && old_cfa.reg != INVALID_REGNUM
1007            && !loc.indirect)
1008     {
1009       /* Construct a "DW_CFA_def_cfa_register <register>" instruction,
1010          indicating the CFA register has changed to <register> but the
1011          offset has not changed.  */
1012       cfi->dw_cfi_opc = DW_CFA_def_cfa_register;
1013       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
1014     }
1015 #endif
1016
1017   else if (loc.indirect == 0)
1018     {
1019       /* Construct a "DW_CFA_def_cfa <register> <offset>" instruction,
1020          indicating the CFA register has changed to <register> with
1021          the specified offset.  The data factoring for DW_CFA_def_cfa_sf
1022          happens in output_cfi, or in the assembler via the .cfi_def_cfa
1023          directive.  */
1024       if (need_data_align_sf_opcode (loc.offset))
1025         cfi->dw_cfi_opc = DW_CFA_def_cfa_sf;
1026       else
1027         cfi->dw_cfi_opc = DW_CFA_def_cfa;
1028       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
1029       cfi->dw_cfi_oprnd2.dw_cfi_offset = loc.offset;
1030     }
1031   else
1032     {
1033       /* Construct a DW_CFA_def_cfa_expression instruction to
1034          calculate the CFA using a full location expression since no
1035          register-offset pair is available.  */
1036       struct dw_loc_descr_struct *loc_list;
1037
1038       cfi->dw_cfi_opc = DW_CFA_def_cfa_expression;
1039       loc_list = build_cfa_loc (&loc, 0);
1040       cfi->dw_cfi_oprnd1.dw_cfi_loc = loc_list;
1041     }
1042
1043   add_fde_cfi (label, cfi);
1044 }
1045
1046 /* Add the CFI for saving a register.  REG is the CFA column number.
1047    LABEL is passed to add_fde_cfi.
1048    If SREG is -1, the register is saved at OFFSET from the CFA;
1049    otherwise it is saved in SREG.  */
1050
1051 static void
1052 reg_save (const char *label, unsigned int reg, unsigned int sreg, HOST_WIDE_INT offset)
1053 {
1054   dw_cfi_ref cfi = new_cfi ();
1055   dw_fde_ref fde = current_fde ();
1056
1057   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
1058
1059   /* When stack is aligned, store REG using DW_CFA_expression with
1060      FP.  */
1061   if (fde
1062       && fde->stack_realign
1063       && sreg == INVALID_REGNUM)
1064     {
1065       cfi->dw_cfi_opc = DW_CFA_expression;
1066       cfi->dw_cfi_oprnd2.dw_cfi_reg_num = reg;
1067       cfi->dw_cfi_oprnd1.dw_cfi_loc
1068         = build_cfa_aligned_loc (offset, fde->stack_realignment);
1069     }
1070   else if (sreg == INVALID_REGNUM)
1071     {
1072       if (need_data_align_sf_opcode (offset))
1073         cfi->dw_cfi_opc = DW_CFA_offset_extended_sf;
1074       else if (reg & ~0x3f)
1075         cfi->dw_cfi_opc = DW_CFA_offset_extended;
1076       else
1077         cfi->dw_cfi_opc = DW_CFA_offset;
1078       cfi->dw_cfi_oprnd2.dw_cfi_offset = offset;
1079     }
1080   else if (sreg == reg)
1081     cfi->dw_cfi_opc = DW_CFA_same_value;
1082   else
1083     {
1084       cfi->dw_cfi_opc = DW_CFA_register;
1085       cfi->dw_cfi_oprnd2.dw_cfi_reg_num = sreg;
1086     }
1087
1088   add_fde_cfi (label, cfi);
1089 }
1090
1091 /* Add the CFI for saving a register window.  LABEL is passed to reg_save.
1092    This CFI tells the unwinder that it needs to restore the window registers
1093    from the previous frame's window save area.
1094
1095    ??? Perhaps we should note in the CIE where windows are saved (instead of
1096    assuming 0(cfa)) and what registers are in the window.  */
1097
1098 void
1099 dwarf2out_window_save (const char *label)
1100 {
1101   dw_cfi_ref cfi = new_cfi ();
1102
1103   cfi->dw_cfi_opc = DW_CFA_GNU_window_save;
1104   add_fde_cfi (label, cfi);
1105 }
1106
1107 /* Add a CFI to update the running total of the size of arguments
1108    pushed onto the stack.  */
1109
1110 void
1111 dwarf2out_args_size (const char *label, HOST_WIDE_INT size)
1112 {
1113   dw_cfi_ref cfi;
1114
1115   if (size == old_args_size)
1116     return;
1117
1118   old_args_size = size;
1119
1120   cfi = new_cfi ();
1121   cfi->dw_cfi_opc = DW_CFA_GNU_args_size;
1122   cfi->dw_cfi_oprnd1.dw_cfi_offset = size;
1123   add_fde_cfi (label, cfi);
1124 }
1125
1126 /* Entry point for saving a register to the stack.  REG is the GCC register
1127    number.  LABEL and OFFSET are passed to reg_save.  */
1128
1129 void
1130 dwarf2out_reg_save (const char *label, unsigned int reg, HOST_WIDE_INT offset)
1131 {
1132   reg_save (label, DWARF_FRAME_REGNUM (reg), INVALID_REGNUM, offset);
1133 }
1134
1135 /* Entry point for saving the return address in the stack.
1136    LABEL and OFFSET are passed to reg_save.  */
1137
1138 void
1139 dwarf2out_return_save (const char *label, HOST_WIDE_INT offset)
1140 {
1141   reg_save (label, DWARF_FRAME_RETURN_COLUMN, INVALID_REGNUM, offset);
1142 }
1143
1144 /* Entry point for saving the return address in a register.
1145    LABEL and SREG are passed to reg_save.  */
1146
1147 void
1148 dwarf2out_return_reg (const char *label, unsigned int sreg)
1149 {
1150   reg_save (label, DWARF_FRAME_RETURN_COLUMN, DWARF_FRAME_REGNUM (sreg), 0);
1151 }
1152
1153 #ifdef DWARF2_UNWIND_INFO
1154 /* Record the initial position of the return address.  RTL is
1155    INCOMING_RETURN_ADDR_RTX.  */
1156
1157 static void
1158 initial_return_save (rtx rtl)
1159 {
1160   unsigned int reg = INVALID_REGNUM;
1161   HOST_WIDE_INT offset = 0;
1162
1163   switch (GET_CODE (rtl))
1164     {
1165     case REG:
1166       /* RA is in a register.  */
1167       reg = DWARF_FRAME_REGNUM (REGNO (rtl));
1168       break;
1169
1170     case MEM:
1171       /* RA is on the stack.  */
1172       rtl = XEXP (rtl, 0);
1173       switch (GET_CODE (rtl))
1174         {
1175         case REG:
1176           gcc_assert (REGNO (rtl) == STACK_POINTER_REGNUM);
1177           offset = 0;
1178           break;
1179
1180         case PLUS:
1181           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1182           offset = INTVAL (XEXP (rtl, 1));
1183           break;
1184
1185         case MINUS:
1186           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1187           offset = -INTVAL (XEXP (rtl, 1));
1188           break;
1189
1190         default:
1191           gcc_unreachable ();
1192         }
1193
1194       break;
1195
1196     case PLUS:
1197       /* The return address is at some offset from any value we can
1198          actually load.  For instance, on the SPARC it is in %i7+8. Just
1199          ignore the offset for now; it doesn't matter for unwinding frames.  */
1200       gcc_assert (CONST_INT_P (XEXP (rtl, 1)));
1201       initial_return_save (XEXP (rtl, 0));
1202       return;
1203
1204     default:
1205       gcc_unreachable ();
1206     }
1207
1208   if (reg != DWARF_FRAME_RETURN_COLUMN)
1209     reg_save (NULL, DWARF_FRAME_RETURN_COLUMN, reg, offset - cfa.offset);
1210 }
1211 #endif
1212
1213 /* Given a SET, calculate the amount of stack adjustment it
1214    contains.  */
1215
1216 static HOST_WIDE_INT
1217 stack_adjust_offset (const_rtx pattern, HOST_WIDE_INT cur_args_size,
1218                      HOST_WIDE_INT cur_offset)
1219 {
1220   const_rtx src = SET_SRC (pattern);
1221   const_rtx dest = SET_DEST (pattern);
1222   HOST_WIDE_INT offset = 0;
1223   enum rtx_code code;
1224
1225   if (dest == stack_pointer_rtx)
1226     {
1227       code = GET_CODE (src);
1228
1229       /* Assume (set (reg sp) (reg whatever)) sets args_size
1230          level to 0.  */
1231       if (code == REG && src != stack_pointer_rtx)
1232         {
1233           offset = -cur_args_size;
1234 #ifndef STACK_GROWS_DOWNWARD
1235           offset = -offset;
1236 #endif
1237           return offset - cur_offset;
1238         }
1239
1240       if (! (code == PLUS || code == MINUS)
1241           || XEXP (src, 0) != stack_pointer_rtx
1242           || !CONST_INT_P (XEXP (src, 1)))
1243         return 0;
1244
1245       /* (set (reg sp) (plus (reg sp) (const_int))) */
1246       offset = INTVAL (XEXP (src, 1));
1247       if (code == PLUS)
1248         offset = -offset;
1249       return offset;
1250     }
1251
1252   if (MEM_P (src) && !MEM_P (dest))
1253     dest = src;
1254   if (MEM_P (dest))
1255     {
1256       /* (set (mem (pre_dec (reg sp))) (foo)) */
1257       src = XEXP (dest, 0);
1258       code = GET_CODE (src);
1259
1260       switch (code)
1261         {
1262         case PRE_MODIFY:
1263         case POST_MODIFY:
1264           if (XEXP (src, 0) == stack_pointer_rtx)
1265             {
1266               rtx val = XEXP (XEXP (src, 1), 1);
1267               /* We handle only adjustments by constant amount.  */
1268               gcc_assert (GET_CODE (XEXP (src, 1)) == PLUS
1269                           && CONST_INT_P (val));
1270               offset = -INTVAL (val);
1271               break;
1272             }
1273           return 0;
1274
1275         case PRE_DEC:
1276         case POST_DEC:
1277           if (XEXP (src, 0) == stack_pointer_rtx)
1278             {
1279               offset = GET_MODE_SIZE (GET_MODE (dest));
1280               break;
1281             }
1282           return 0;
1283
1284         case PRE_INC:
1285         case POST_INC:
1286           if (XEXP (src, 0) == stack_pointer_rtx)
1287             {
1288               offset = -GET_MODE_SIZE (GET_MODE (dest));
1289               break;
1290             }
1291           return 0;
1292
1293         default:
1294           return 0;
1295         }
1296     }
1297   else
1298     return 0;
1299
1300   return offset;
1301 }
1302
1303 /* Precomputed args_size for CODE_LABELs and BARRIERs preceeding them,
1304    indexed by INSN_UID.  */
1305
1306 static HOST_WIDE_INT *barrier_args_size;
1307
1308 /* Helper function for compute_barrier_args_size.  Handle one insn.  */
1309
1310 static HOST_WIDE_INT
1311 compute_barrier_args_size_1 (rtx insn, HOST_WIDE_INT cur_args_size,
1312                              VEC (rtx, heap) **next)
1313 {
1314   HOST_WIDE_INT offset = 0;
1315   int i;
1316
1317   if (! RTX_FRAME_RELATED_P (insn))
1318     {
1319       if (prologue_epilogue_contains (insn))
1320         /* Nothing */;
1321       else if (GET_CODE (PATTERN (insn)) == SET)
1322         offset = stack_adjust_offset (PATTERN (insn), cur_args_size, 0);
1323       else if (GET_CODE (PATTERN (insn)) == PARALLEL
1324                || GET_CODE (PATTERN (insn)) == SEQUENCE)
1325         {
1326           /* There may be stack adjustments inside compound insns.  Search
1327              for them.  */
1328           for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1329             if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1330               offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i),
1331                                              cur_args_size, offset);
1332         }
1333     }
1334   else
1335     {
1336       rtx expr = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
1337
1338       if (expr)
1339         {
1340           expr = XEXP (expr, 0);
1341           if (GET_CODE (expr) == PARALLEL
1342               || GET_CODE (expr) == SEQUENCE)
1343             for (i = 1; i < XVECLEN (expr, 0); i++)
1344               {
1345                 rtx elem = XVECEXP (expr, 0, i);
1346
1347                 if (GET_CODE (elem) == SET && !RTX_FRAME_RELATED_P (elem))
1348                   offset += stack_adjust_offset (elem, cur_args_size, offset);
1349               }
1350         }
1351     }
1352
1353 #ifndef STACK_GROWS_DOWNWARD
1354   offset = -offset;
1355 #endif
1356
1357   cur_args_size += offset;
1358   if (cur_args_size < 0)
1359     cur_args_size = 0;
1360
1361   if (JUMP_P (insn))
1362     {
1363       rtx dest = JUMP_LABEL (insn);
1364
1365       if (dest)
1366         {
1367           if (barrier_args_size [INSN_UID (dest)] < 0)
1368             {
1369               barrier_args_size [INSN_UID (dest)] = cur_args_size;
1370               VEC_safe_push (rtx, heap, *next, dest);
1371             }
1372         }
1373     }
1374
1375   return cur_args_size;
1376 }
1377
1378 /* Walk the whole function and compute args_size on BARRIERs.  */
1379
1380 static void
1381 compute_barrier_args_size (void)
1382 {
1383   int max_uid = get_max_uid (), i;
1384   rtx insn;
1385   VEC (rtx, heap) *worklist, *next, *tmp;
1386
1387   barrier_args_size = XNEWVEC (HOST_WIDE_INT, max_uid);
1388   for (i = 0; i < max_uid; i++)
1389     barrier_args_size[i] = -1;
1390
1391   worklist = VEC_alloc (rtx, heap, 20);
1392   next = VEC_alloc (rtx, heap, 20);
1393   insn = get_insns ();
1394   barrier_args_size[INSN_UID (insn)] = 0;
1395   VEC_quick_push (rtx, worklist, insn);
1396   for (;;)
1397     {
1398       while (!VEC_empty (rtx, worklist))
1399         {
1400           rtx prev, body, first_insn;
1401           HOST_WIDE_INT cur_args_size;
1402
1403           first_insn = insn = VEC_pop (rtx, worklist);
1404           cur_args_size = barrier_args_size[INSN_UID (insn)];
1405           prev = prev_nonnote_insn (insn);
1406           if (prev && BARRIER_P (prev))
1407             barrier_args_size[INSN_UID (prev)] = cur_args_size;
1408
1409           for (; insn; insn = NEXT_INSN (insn))
1410             {
1411               if (INSN_DELETED_P (insn) || NOTE_P (insn))
1412                 continue;
1413               if (BARRIER_P (insn))
1414                 break;
1415
1416               if (LABEL_P (insn))
1417                 {
1418                   if (insn == first_insn)
1419                     continue;
1420                   else if (barrier_args_size[INSN_UID (insn)] < 0)
1421                     {
1422                       barrier_args_size[INSN_UID (insn)] = cur_args_size;
1423                       continue;
1424                     }
1425                   else
1426                     {
1427                       /* The insns starting with this label have been
1428                          already scanned or are in the worklist.  */
1429                       break;
1430                     }
1431                 }
1432
1433               body = PATTERN (insn);
1434               if (GET_CODE (body) == SEQUENCE)
1435                 {
1436                   HOST_WIDE_INT dest_args_size = cur_args_size;
1437                   for (i = 1; i < XVECLEN (body, 0); i++)
1438                     if (INSN_ANNULLED_BRANCH_P (XVECEXP (body, 0, 0))
1439                         && INSN_FROM_TARGET_P (XVECEXP (body, 0, i)))
1440                       dest_args_size
1441                         = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1442                                                        dest_args_size, &next);
1443                     else
1444                       cur_args_size
1445                         = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1446                                                        cur_args_size, &next);
1447
1448                   if (INSN_ANNULLED_BRANCH_P (XVECEXP (body, 0, 0)))
1449                     compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1450                                                  dest_args_size, &next);
1451                   else
1452                     cur_args_size
1453                       = compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1454                                                      cur_args_size, &next);
1455                 }
1456               else
1457                 cur_args_size
1458                   = compute_barrier_args_size_1 (insn, cur_args_size, &next);
1459             }
1460         }
1461
1462       if (VEC_empty (rtx, next))
1463         break;
1464
1465       /* Swap WORKLIST with NEXT and truncate NEXT for next iteration.  */
1466       tmp = next;
1467       next = worklist;
1468       worklist = tmp;
1469       VEC_truncate (rtx, next, 0);
1470     }
1471
1472   VEC_free (rtx, heap, worklist);
1473   VEC_free (rtx, heap, next);
1474 }
1475
1476
1477 /* Check INSN to see if it looks like a push or a stack adjustment, and
1478    make a note of it if it does.  EH uses this information to find out how
1479    much extra space it needs to pop off the stack.  */
1480
1481 static void
1482 dwarf2out_stack_adjust (rtx insn, bool after_p)
1483 {
1484   HOST_WIDE_INT offset;
1485   const char *label;
1486   int i;
1487
1488   /* Don't handle epilogues at all.  Certainly it would be wrong to do so
1489      with this function.  Proper support would require all frame-related
1490      insns to be marked, and to be able to handle saving state around
1491      epilogues textually in the middle of the function.  */
1492   if (prologue_epilogue_contains (insn))
1493     return;
1494
1495   /* If INSN is an instruction from target of an annulled branch, the
1496      effects are for the target only and so current argument size
1497      shouldn't change at all.  */
1498   if (final_sequence
1499       && INSN_ANNULLED_BRANCH_P (XVECEXP (final_sequence, 0, 0))
1500       && INSN_FROM_TARGET_P (insn))
1501     return;
1502
1503   /* If only calls can throw, and we have a frame pointer,
1504      save up adjustments until we see the CALL_INSN.  */
1505   if (!flag_asynchronous_unwind_tables && cfa.reg != STACK_POINTER_REGNUM)
1506     {
1507       if (CALL_P (insn) && !after_p)
1508         {
1509           /* Extract the size of the args from the CALL rtx itself.  */
1510           insn = PATTERN (insn);
1511           if (GET_CODE (insn) == PARALLEL)
1512             insn = XVECEXP (insn, 0, 0);
1513           if (GET_CODE (insn) == SET)
1514             insn = SET_SRC (insn);
1515           gcc_assert (GET_CODE (insn) == CALL);
1516           dwarf2out_args_size ("", INTVAL (XEXP (insn, 1)));
1517         }
1518       return;
1519     }
1520
1521   if (CALL_P (insn) && !after_p)
1522     {
1523       if (!flag_asynchronous_unwind_tables)
1524         dwarf2out_args_size ("", args_size);
1525       return;
1526     }
1527   else if (BARRIER_P (insn))
1528     {
1529       /* Don't call compute_barrier_args_size () if the only
1530          BARRIER is at the end of function.  */
1531       if (barrier_args_size == NULL && next_nonnote_insn (insn))
1532         compute_barrier_args_size ();
1533       if (barrier_args_size == NULL)
1534         offset = 0;
1535       else
1536         {
1537           offset = barrier_args_size[INSN_UID (insn)];
1538           if (offset < 0)
1539             offset = 0;
1540         }
1541
1542       offset -= args_size;
1543 #ifndef STACK_GROWS_DOWNWARD
1544       offset = -offset;
1545 #endif
1546     }
1547   else if (GET_CODE (PATTERN (insn)) == SET)
1548     offset = stack_adjust_offset (PATTERN (insn), args_size, 0);
1549   else if (GET_CODE (PATTERN (insn)) == PARALLEL
1550            || GET_CODE (PATTERN (insn)) == SEQUENCE)
1551     {
1552       /* There may be stack adjustments inside compound insns.  Search
1553          for them.  */
1554       for (offset = 0, i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1555         if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1556           offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i),
1557                                          args_size, offset);
1558     }
1559   else
1560     return;
1561
1562   if (offset == 0)
1563     return;
1564
1565   label = dwarf2out_cfi_label (false);
1566   dwarf2out_args_size_adjust (offset, label);
1567 }
1568
1569 /* Adjust args_size based on stack adjustment OFFSET.  */
1570
1571 static void
1572 dwarf2out_args_size_adjust (HOST_WIDE_INT offset, const char *label)
1573 {
1574   if (cfa.reg == STACK_POINTER_REGNUM)
1575     cfa.offset += offset;
1576
1577   if (cfa_store.reg == STACK_POINTER_REGNUM)
1578     cfa_store.offset += offset;
1579
1580 #ifndef STACK_GROWS_DOWNWARD
1581   offset = -offset;
1582 #endif
1583
1584   args_size += offset;
1585   if (args_size < 0)
1586     args_size = 0;
1587
1588   def_cfa_1 (label, &cfa);
1589   if (flag_asynchronous_unwind_tables)
1590     dwarf2out_args_size (label, args_size);
1591 }
1592
1593 #endif
1594
1595 /* We delay emitting a register save until either (a) we reach the end
1596    of the prologue or (b) the register is clobbered.  This clusters
1597    register saves so that there are fewer pc advances.  */
1598
1599 struct GTY(()) queued_reg_save {
1600   struct queued_reg_save *next;
1601   rtx reg;
1602   HOST_WIDE_INT cfa_offset;
1603   rtx saved_reg;
1604 };
1605
1606 static GTY(()) struct queued_reg_save *queued_reg_saves;
1607
1608 /* The caller's ORIG_REG is saved in SAVED_IN_REG.  */
1609 struct GTY(()) reg_saved_in_data {
1610   rtx orig_reg;
1611   rtx saved_in_reg;
1612 };
1613
1614 /* A list of registers saved in other registers.
1615    The list intentionally has a small maximum capacity of 4; if your
1616    port needs more than that, you might consider implementing a
1617    more efficient data structure.  */
1618 static GTY(()) struct reg_saved_in_data regs_saved_in_regs[4];
1619 static GTY(()) size_t num_regs_saved_in_regs;
1620
1621 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
1622 static const char *last_reg_save_label;
1623
1624 /* Add an entry to QUEUED_REG_SAVES saying that REG is now saved at
1625    SREG, or if SREG is NULL then it is saved at OFFSET to the CFA.  */
1626
1627 static void
1628 queue_reg_save (const char *label, rtx reg, rtx sreg, HOST_WIDE_INT offset)
1629 {
1630   struct queued_reg_save *q;
1631
1632   /* Duplicates waste space, but it's also necessary to remove them
1633      for correctness, since the queue gets output in reverse
1634      order.  */
1635   for (q = queued_reg_saves; q != NULL; q = q->next)
1636     if (REGNO (q->reg) == REGNO (reg))
1637       break;
1638
1639   if (q == NULL)
1640     {
1641       q = GGC_NEW (struct queued_reg_save);
1642       q->next = queued_reg_saves;
1643       queued_reg_saves = q;
1644     }
1645
1646   q->reg = reg;
1647   q->cfa_offset = offset;
1648   q->saved_reg = sreg;
1649
1650   last_reg_save_label = label;
1651 }
1652
1653 /* Output all the entries in QUEUED_REG_SAVES.  */
1654
1655 static void
1656 flush_queued_reg_saves (void)
1657 {
1658   struct queued_reg_save *q;
1659
1660   for (q = queued_reg_saves; q; q = q->next)
1661     {
1662       size_t i;
1663       unsigned int reg, sreg;
1664
1665       for (i = 0; i < num_regs_saved_in_regs; i++)
1666         if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (q->reg))
1667           break;
1668       if (q->saved_reg && i == num_regs_saved_in_regs)
1669         {
1670           gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1671           num_regs_saved_in_regs++;
1672         }
1673       if (i != num_regs_saved_in_regs)
1674         {
1675           regs_saved_in_regs[i].orig_reg = q->reg;
1676           regs_saved_in_regs[i].saved_in_reg = q->saved_reg;
1677         }
1678
1679       reg = DWARF_FRAME_REGNUM (REGNO (q->reg));
1680       if (q->saved_reg)
1681         sreg = DWARF_FRAME_REGNUM (REGNO (q->saved_reg));
1682       else
1683         sreg = INVALID_REGNUM;
1684       reg_save (last_reg_save_label, reg, sreg, q->cfa_offset);
1685     }
1686
1687   queued_reg_saves = NULL;
1688   last_reg_save_label = NULL;
1689 }
1690
1691 /* Does INSN clobber any register which QUEUED_REG_SAVES lists a saved
1692    location for?  Or, does it clobber a register which we've previously
1693    said that some other register is saved in, and for which we now
1694    have a new location for?  */
1695
1696 static bool
1697 clobbers_queued_reg_save (const_rtx insn)
1698 {
1699   struct queued_reg_save *q;
1700
1701   for (q = queued_reg_saves; q; q = q->next)
1702     {
1703       size_t i;
1704       if (modified_in_p (q->reg, insn))
1705         return true;
1706       for (i = 0; i < num_regs_saved_in_regs; i++)
1707         if (REGNO (q->reg) == REGNO (regs_saved_in_regs[i].orig_reg)
1708             && modified_in_p (regs_saved_in_regs[i].saved_in_reg, insn))
1709           return true;
1710     }
1711
1712   return false;
1713 }
1714
1715 /* Entry point for saving the first register into the second.  */
1716
1717 void
1718 dwarf2out_reg_save_reg (const char *label, rtx reg, rtx sreg)
1719 {
1720   size_t i;
1721   unsigned int regno, sregno;
1722
1723   for (i = 0; i < num_regs_saved_in_regs; i++)
1724     if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (reg))
1725       break;
1726   if (i == num_regs_saved_in_regs)
1727     {
1728       gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1729       num_regs_saved_in_regs++;
1730     }
1731   regs_saved_in_regs[i].orig_reg = reg;
1732   regs_saved_in_regs[i].saved_in_reg = sreg;
1733
1734   regno = DWARF_FRAME_REGNUM (REGNO (reg));
1735   sregno = DWARF_FRAME_REGNUM (REGNO (sreg));
1736   reg_save (label, regno, sregno, 0);
1737 }
1738
1739 /* What register, if any, is currently saved in REG?  */
1740
1741 static rtx
1742 reg_saved_in (rtx reg)
1743 {
1744   unsigned int regn = REGNO (reg);
1745   size_t i;
1746   struct queued_reg_save *q;
1747
1748   for (q = queued_reg_saves; q; q = q->next)
1749     if (q->saved_reg && regn == REGNO (q->saved_reg))
1750       return q->reg;
1751
1752   for (i = 0; i < num_regs_saved_in_regs; i++)
1753     if (regs_saved_in_regs[i].saved_in_reg
1754         && regn == REGNO (regs_saved_in_regs[i].saved_in_reg))
1755       return regs_saved_in_regs[i].orig_reg;
1756
1757   return NULL_RTX;
1758 }
1759
1760
1761 /* A temporary register holding an integral value used in adjusting SP
1762    or setting up the store_reg.  The "offset" field holds the integer
1763    value, not an offset.  */
1764 static dw_cfa_location cfa_temp;
1765
1766 /* A subroutine of dwarf2out_frame_debug, process a REG_DEF_CFA note.  */
1767
1768 static void
1769 dwarf2out_frame_debug_def_cfa (rtx pat, const char *label)
1770 {
1771   memset (&cfa, 0, sizeof (cfa));
1772
1773   switch (GET_CODE (pat))
1774     {
1775     case PLUS:
1776       cfa.reg = REGNO (XEXP (pat, 0));
1777       cfa.offset = INTVAL (XEXP (pat, 1));
1778       break;
1779
1780     case REG:
1781       cfa.reg = REGNO (pat);
1782       break;
1783
1784     default:
1785       /* Recurse and define an expression.  */
1786       gcc_unreachable ();
1787     }
1788
1789   def_cfa_1 (label, &cfa);
1790 }
1791
1792 /* A subroutine of dwarf2out_frame_debug, process a REG_ADJUST_CFA note.  */
1793
1794 static void
1795 dwarf2out_frame_debug_adjust_cfa (rtx pat, const char *label)
1796 {
1797   rtx src, dest;
1798
1799   gcc_assert (GET_CODE (pat) == SET);
1800   dest = XEXP (pat, 0);
1801   src = XEXP (pat, 1);
1802
1803   switch (GET_CODE (src))
1804     {
1805     case PLUS:
1806       gcc_assert (REGNO (XEXP (src, 0)) == cfa.reg);
1807       cfa.offset -= INTVAL (XEXP (src, 1));
1808       break;
1809
1810     case REG:
1811         break;
1812
1813     default:
1814         gcc_unreachable ();
1815     }
1816
1817   cfa.reg = REGNO (dest);
1818   gcc_assert (cfa.indirect == 0);
1819
1820   def_cfa_1 (label, &cfa);
1821 }
1822
1823 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_OFFSET note.  */
1824
1825 static void
1826 dwarf2out_frame_debug_cfa_offset (rtx set, const char *label)
1827 {
1828   HOST_WIDE_INT offset;
1829   rtx src, addr, span;
1830
1831   src = XEXP (set, 1);
1832   addr = XEXP (set, 0);
1833   gcc_assert (MEM_P (addr));
1834   addr = XEXP (addr, 0);
1835   
1836   /* As documented, only consider extremely simple addresses.  */
1837   switch (GET_CODE (addr))
1838     {
1839     case REG:
1840       gcc_assert (REGNO (addr) == cfa.reg);
1841       offset = -cfa.offset;
1842       break;
1843     case PLUS:
1844       gcc_assert (REGNO (XEXP (addr, 0)) == cfa.reg);
1845       offset = INTVAL (XEXP (addr, 1)) - cfa.offset;
1846       break;
1847     default:
1848       gcc_unreachable ();
1849     }
1850
1851   span = targetm.dwarf_register_span (src);
1852
1853   /* ??? We'd like to use queue_reg_save, but we need to come up with
1854      a different flushing heuristic for epilogues.  */
1855   if (!span)
1856     reg_save (label, DWARF_FRAME_REGNUM (REGNO (src)), INVALID_REGNUM, offset);
1857   else
1858     {
1859       /* We have a PARALLEL describing where the contents of SRC live.
1860          Queue register saves for each piece of the PARALLEL.  */
1861       int par_index;
1862       int limit;
1863       HOST_WIDE_INT span_offset = offset;
1864
1865       gcc_assert (GET_CODE (span) == PARALLEL);
1866
1867       limit = XVECLEN (span, 0);
1868       for (par_index = 0; par_index < limit; par_index++)
1869         {
1870           rtx elem = XVECEXP (span, 0, par_index);
1871
1872           reg_save (label, DWARF_FRAME_REGNUM (REGNO (elem)),
1873                     INVALID_REGNUM, span_offset);
1874           span_offset += GET_MODE_SIZE (GET_MODE (elem));
1875         }
1876     }
1877 }
1878
1879 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_REGISTER note.  */
1880
1881 static void
1882 dwarf2out_frame_debug_cfa_register (rtx set, const char *label)
1883 {
1884   rtx src, dest;
1885   unsigned sregno, dregno;
1886
1887   src = XEXP (set, 1);
1888   dest = XEXP (set, 0);
1889
1890   if (src == pc_rtx)
1891     sregno = DWARF_FRAME_RETURN_COLUMN;
1892   else
1893     sregno = DWARF_FRAME_REGNUM (REGNO (src));
1894
1895   dregno = DWARF_FRAME_REGNUM (REGNO (dest));
1896
1897   /* ??? We'd like to use queue_reg_save, but we need to come up with
1898      a different flushing heuristic for epilogues.  */
1899   reg_save (label, sregno, dregno, 0);
1900 }
1901
1902 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_RESTORE note.  */
1903
1904 static void
1905 dwarf2out_frame_debug_cfa_restore (rtx reg, const char *label)
1906 {
1907   dw_cfi_ref cfi = new_cfi ();
1908   unsigned int regno = DWARF_FRAME_REGNUM (REGNO (reg));
1909
1910   cfi->dw_cfi_opc = (regno & ~0x3f ? DW_CFA_restore_extended : DW_CFA_restore);
1911   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = regno;
1912
1913   add_fde_cfi (label, cfi);
1914 }
1915
1916 /* Record call frame debugging information for an expression EXPR,
1917    which either sets SP or FP (adjusting how we calculate the frame
1918    address) or saves a register to the stack or another register.
1919    LABEL indicates the address of EXPR.
1920
1921    This function encodes a state machine mapping rtxes to actions on
1922    cfa, cfa_store, and cfa_temp.reg.  We describe these rules so
1923    users need not read the source code.
1924
1925   The High-Level Picture
1926
1927   Changes in the register we use to calculate the CFA: Currently we
1928   assume that if you copy the CFA register into another register, we
1929   should take the other one as the new CFA register; this seems to
1930   work pretty well.  If it's wrong for some target, it's simple
1931   enough not to set RTX_FRAME_RELATED_P on the insn in question.
1932
1933   Changes in the register we use for saving registers to the stack:
1934   This is usually SP, but not always.  Again, we deduce that if you
1935   copy SP into another register (and SP is not the CFA register),
1936   then the new register is the one we will be using for register
1937   saves.  This also seems to work.
1938
1939   Register saves: There's not much guesswork about this one; if
1940   RTX_FRAME_RELATED_P is set on an insn which modifies memory, it's a
1941   register save, and the register used to calculate the destination
1942   had better be the one we think we're using for this purpose.
1943   It's also assumed that a copy from a call-saved register to another
1944   register is saving that register if RTX_FRAME_RELATED_P is set on
1945   that instruction.  If the copy is from a call-saved register to
1946   the *same* register, that means that the register is now the same
1947   value as in the caller.
1948
1949   Except: If the register being saved is the CFA register, and the
1950   offset is nonzero, we are saving the CFA, so we assume we have to
1951   use DW_CFA_def_cfa_expression.  If the offset is 0, we assume that
1952   the intent is to save the value of SP from the previous frame.
1953
1954   In addition, if a register has previously been saved to a different
1955   register,
1956
1957   Invariants / Summaries of Rules
1958
1959   cfa          current rule for calculating the CFA.  It usually
1960                consists of a register and an offset.
1961   cfa_store    register used by prologue code to save things to the stack
1962                cfa_store.offset is the offset from the value of
1963                cfa_store.reg to the actual CFA
1964   cfa_temp     register holding an integral value.  cfa_temp.offset
1965                stores the value, which will be used to adjust the
1966                stack pointer.  cfa_temp is also used like cfa_store,
1967                to track stores to the stack via fp or a temp reg.
1968
1969   Rules  1- 4: Setting a register's value to cfa.reg or an expression
1970                with cfa.reg as the first operand changes the cfa.reg and its
1971                cfa.offset.  Rule 1 and 4 also set cfa_temp.reg and
1972                cfa_temp.offset.
1973
1974   Rules  6- 9: Set a non-cfa.reg register value to a constant or an
1975                expression yielding a constant.  This sets cfa_temp.reg
1976                and cfa_temp.offset.
1977
1978   Rule 5:      Create a new register cfa_store used to save items to the
1979                stack.
1980
1981   Rules 10-14: Save a register to the stack.  Define offset as the
1982                difference of the original location and cfa_store's
1983                location (or cfa_temp's location if cfa_temp is used).
1984
1985   Rules 16-20: If AND operation happens on sp in prologue, we assume
1986                stack is realigned.  We will use a group of DW_OP_XXX
1987                expressions to represent the location of the stored
1988                register instead of CFA+offset.
1989
1990   The Rules
1991
1992   "{a,b}" indicates a choice of a xor b.
1993   "<reg>:cfa.reg" indicates that <reg> must equal cfa.reg.
1994
1995   Rule 1:
1996   (set <reg1> <reg2>:cfa.reg)
1997   effects: cfa.reg = <reg1>
1998            cfa.offset unchanged
1999            cfa_temp.reg = <reg1>
2000            cfa_temp.offset = cfa.offset
2001
2002   Rule 2:
2003   (set sp ({minus,plus,losum} {sp,fp}:cfa.reg
2004                               {<const_int>,<reg>:cfa_temp.reg}))
2005   effects: cfa.reg = sp if fp used
2006            cfa.offset += {+/- <const_int>, cfa_temp.offset} if cfa.reg==sp
2007            cfa_store.offset += {+/- <const_int>, cfa_temp.offset}
2008              if cfa_store.reg==sp
2009
2010   Rule 3:
2011   (set fp ({minus,plus,losum} <reg>:cfa.reg <const_int>))
2012   effects: cfa.reg = fp
2013            cfa_offset += +/- <const_int>
2014
2015   Rule 4:
2016   (set <reg1> ({plus,losum} <reg2>:cfa.reg <const_int>))
2017   constraints: <reg1> != fp
2018                <reg1> != sp
2019   effects: cfa.reg = <reg1>
2020            cfa_temp.reg = <reg1>
2021            cfa_temp.offset = cfa.offset
2022
2023   Rule 5:
2024   (set <reg1> (plus <reg2>:cfa_temp.reg sp:cfa.reg))
2025   constraints: <reg1> != fp
2026                <reg1> != sp
2027   effects: cfa_store.reg = <reg1>
2028            cfa_store.offset = cfa.offset - cfa_temp.offset
2029
2030   Rule 6:
2031   (set <reg> <const_int>)
2032   effects: cfa_temp.reg = <reg>
2033            cfa_temp.offset = <const_int>
2034
2035   Rule 7:
2036   (set <reg1>:cfa_temp.reg (ior <reg2>:cfa_temp.reg <const_int>))
2037   effects: cfa_temp.reg = <reg1>
2038            cfa_temp.offset |= <const_int>
2039
2040   Rule 8:
2041   (set <reg> (high <exp>))
2042   effects: none
2043
2044   Rule 9:
2045   (set <reg> (lo_sum <exp> <const_int>))
2046   effects: cfa_temp.reg = <reg>
2047            cfa_temp.offset = <const_int>
2048
2049   Rule 10:
2050   (set (mem (pre_modify sp:cfa_store (???? <reg1> <const_int>))) <reg2>)
2051   effects: cfa_store.offset -= <const_int>
2052            cfa.offset = cfa_store.offset if cfa.reg == sp
2053            cfa.reg = sp
2054            cfa.base_offset = -cfa_store.offset
2055
2056   Rule 11:
2057   (set (mem ({pre_inc,pre_dec} sp:cfa_store.reg)) <reg>)
2058   effects: cfa_store.offset += -/+ mode_size(mem)
2059            cfa.offset = cfa_store.offset if cfa.reg == sp
2060            cfa.reg = sp
2061            cfa.base_offset = -cfa_store.offset
2062
2063   Rule 12:
2064   (set (mem ({minus,plus,losum} <reg1>:{cfa_store,cfa_temp} <const_int>))
2065
2066        <reg2>)
2067   effects: cfa.reg = <reg1>
2068            cfa.base_offset = -/+ <const_int> - {cfa_store,cfa_temp}.offset
2069
2070   Rule 13:
2071   (set (mem <reg1>:{cfa_store,cfa_temp}) <reg2>)
2072   effects: cfa.reg = <reg1>
2073            cfa.base_offset = -{cfa_store,cfa_temp}.offset
2074
2075   Rule 14:
2076   (set (mem (postinc <reg1>:cfa_temp <const_int>)) <reg2>)
2077   effects: cfa.reg = <reg1>
2078            cfa.base_offset = -cfa_temp.offset
2079            cfa_temp.offset -= mode_size(mem)
2080
2081   Rule 15:
2082   (set <reg> {unspec, unspec_volatile})
2083   effects: target-dependent
2084
2085   Rule 16:
2086   (set sp (and: sp <const_int>))
2087   constraints: cfa_store.reg == sp
2088   effects: current_fde.stack_realign = 1
2089            cfa_store.offset = 0
2090            fde->drap_reg = cfa.reg if cfa.reg != sp and cfa.reg != fp
2091
2092   Rule 17:
2093   (set (mem ({pre_inc, pre_dec} sp)) (mem (plus (cfa.reg) (const_int))))
2094   effects: cfa_store.offset += -/+ mode_size(mem)
2095
2096   Rule 18:
2097   (set (mem ({pre_inc, pre_dec} sp)) fp)
2098   constraints: fde->stack_realign == 1
2099   effects: cfa_store.offset = 0
2100            cfa.reg != HARD_FRAME_POINTER_REGNUM
2101
2102   Rule 19:
2103   (set (mem ({pre_inc, pre_dec} sp)) cfa.reg)
2104   constraints: fde->stack_realign == 1
2105                && cfa.offset == 0
2106                && cfa.indirect == 0
2107                && cfa.reg != HARD_FRAME_POINTER_REGNUM
2108   effects: Use DW_CFA_def_cfa_expression to define cfa
2109            cfa.reg == fde->drap_reg
2110
2111   Rule 20:
2112   (set reg fde->drap_reg)
2113   constraints: fde->vdrap_reg == INVALID_REGNUM
2114   effects: fde->vdrap_reg = reg.
2115   (set mem fde->drap_reg)
2116   constraints: fde->drap_reg_saved == 1
2117   effects: none.  */
2118
2119 static void
2120 dwarf2out_frame_debug_expr (rtx expr, const char *label)
2121 {
2122   rtx src, dest, span;
2123   HOST_WIDE_INT offset;
2124   dw_fde_ref fde;
2125
2126   /* If RTX_FRAME_RELATED_P is set on a PARALLEL, process each member of
2127      the PARALLEL independently. The first element is always processed if
2128      it is a SET. This is for backward compatibility.   Other elements
2129      are processed only if they are SETs and the RTX_FRAME_RELATED_P
2130      flag is set in them.  */
2131   if (GET_CODE (expr) == PARALLEL || GET_CODE (expr) == SEQUENCE)
2132     {
2133       int par_index;
2134       int limit = XVECLEN (expr, 0);
2135       rtx elem;
2136
2137       /* PARALLELs have strict read-modify-write semantics, so we
2138          ought to evaluate every rvalue before changing any lvalue.
2139          It's cumbersome to do that in general, but there's an
2140          easy approximation that is enough for all current users:
2141          handle register saves before register assignments.  */
2142       if (GET_CODE (expr) == PARALLEL)
2143         for (par_index = 0; par_index < limit; par_index++)
2144           {
2145             elem = XVECEXP (expr, 0, par_index);
2146             if (GET_CODE (elem) == SET
2147                 && MEM_P (SET_DEST (elem))
2148                 && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
2149               dwarf2out_frame_debug_expr (elem, label);
2150           }
2151
2152       for (par_index = 0; par_index < limit; par_index++)
2153         {
2154           elem = XVECEXP (expr, 0, par_index);
2155           if (GET_CODE (elem) == SET
2156               && (!MEM_P (SET_DEST (elem)) || GET_CODE (expr) == SEQUENCE)
2157               && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
2158             dwarf2out_frame_debug_expr (elem, label);
2159           else if (GET_CODE (elem) == SET
2160                    && par_index != 0
2161                    && !RTX_FRAME_RELATED_P (elem))
2162             {
2163               /* Stack adjustment combining might combine some post-prologue
2164                  stack adjustment into a prologue stack adjustment.  */
2165               HOST_WIDE_INT offset = stack_adjust_offset (elem, args_size, 0);
2166
2167               if (offset != 0)
2168                 dwarf2out_args_size_adjust (offset, label);
2169             }
2170         }
2171       return;
2172     }
2173
2174   gcc_assert (GET_CODE (expr) == SET);
2175
2176   src = SET_SRC (expr);
2177   dest = SET_DEST (expr);
2178
2179   if (REG_P (src))
2180     {
2181       rtx rsi = reg_saved_in (src);
2182       if (rsi)
2183         src = rsi;
2184     }
2185
2186   fde = current_fde ();
2187
2188   if (REG_P (src)
2189       && fde
2190       && fde->drap_reg == REGNO (src)
2191       && (fde->drap_reg_saved
2192           || REG_P (dest)))
2193     {
2194       /* Rule 20 */
2195       /* If we are saving dynamic realign argument pointer to a
2196          register, the destination is virtual dynamic realign
2197          argument pointer.  It may be used to access argument.  */
2198       if (REG_P (dest))
2199         {
2200           gcc_assert (fde->vdrap_reg == INVALID_REGNUM);
2201           fde->vdrap_reg = REGNO (dest);
2202         }
2203       return;
2204     }
2205
2206   switch (GET_CODE (dest))
2207     {
2208     case REG:
2209       switch (GET_CODE (src))
2210         {
2211           /* Setting FP from SP.  */
2212         case REG:
2213           if (cfa.reg == (unsigned) REGNO (src))
2214             {
2215               /* Rule 1 */
2216               /* Update the CFA rule wrt SP or FP.  Make sure src is
2217                  relative to the current CFA register.
2218
2219                  We used to require that dest be either SP or FP, but the
2220                  ARM copies SP to a temporary register, and from there to
2221                  FP.  So we just rely on the backends to only set
2222                  RTX_FRAME_RELATED_P on appropriate insns.  */
2223               cfa.reg = REGNO (dest);
2224               cfa_temp.reg = cfa.reg;
2225               cfa_temp.offset = cfa.offset;
2226             }
2227           else
2228             {
2229               /* Saving a register in a register.  */
2230               gcc_assert (!fixed_regs [REGNO (dest)]
2231                           /* For the SPARC and its register window.  */
2232                           || (DWARF_FRAME_REGNUM (REGNO (src))
2233                               == DWARF_FRAME_RETURN_COLUMN));
2234
2235               /* After stack is aligned, we can only save SP in FP
2236                  if drap register is used.  In this case, we have
2237                  to restore stack pointer with the CFA value and we
2238                  don't generate this DWARF information.  */
2239               if (fde
2240                   && fde->stack_realign
2241                   && REGNO (src) == STACK_POINTER_REGNUM)
2242                 gcc_assert (REGNO (dest) == HARD_FRAME_POINTER_REGNUM
2243                             && fde->drap_reg != INVALID_REGNUM
2244                             && cfa.reg != REGNO (src));
2245               else
2246                 queue_reg_save (label, src, dest, 0);
2247             }
2248           break;
2249
2250         case PLUS:
2251         case MINUS:
2252         case LO_SUM:
2253           if (dest == stack_pointer_rtx)
2254             {
2255               /* Rule 2 */
2256               /* Adjusting SP.  */
2257               switch (GET_CODE (XEXP (src, 1)))
2258                 {
2259                 case CONST_INT:
2260                   offset = INTVAL (XEXP (src, 1));
2261                   break;
2262                 case REG:
2263                   gcc_assert ((unsigned) REGNO (XEXP (src, 1))
2264                               == cfa_temp.reg);
2265                   offset = cfa_temp.offset;
2266                   break;
2267                 default:
2268                   gcc_unreachable ();
2269                 }
2270
2271               if (XEXP (src, 0) == hard_frame_pointer_rtx)
2272                 {
2273                   /* Restoring SP from FP in the epilogue.  */
2274                   gcc_assert (cfa.reg == (unsigned) HARD_FRAME_POINTER_REGNUM);
2275                   cfa.reg = STACK_POINTER_REGNUM;
2276                 }
2277               else if (GET_CODE (src) == LO_SUM)
2278                 /* Assume we've set the source reg of the LO_SUM from sp.  */
2279                 ;
2280               else
2281                 gcc_assert (XEXP (src, 0) == stack_pointer_rtx);
2282
2283               if (GET_CODE (src) != MINUS)
2284                 offset = -offset;
2285               if (cfa.reg == STACK_POINTER_REGNUM)
2286                 cfa.offset += offset;
2287               if (cfa_store.reg == STACK_POINTER_REGNUM)
2288                 cfa_store.offset += offset;
2289             }
2290           else if (dest == hard_frame_pointer_rtx)
2291             {
2292               /* Rule 3 */
2293               /* Either setting the FP from an offset of the SP,
2294                  or adjusting the FP */
2295               gcc_assert (frame_pointer_needed);
2296
2297               gcc_assert (REG_P (XEXP (src, 0))
2298                           && (unsigned) REGNO (XEXP (src, 0)) == cfa.reg
2299                           && CONST_INT_P (XEXP (src, 1)));
2300               offset = INTVAL (XEXP (src, 1));
2301               if (GET_CODE (src) != MINUS)
2302                 offset = -offset;
2303               cfa.offset += offset;
2304               cfa.reg = HARD_FRAME_POINTER_REGNUM;
2305             }
2306           else
2307             {
2308               gcc_assert (GET_CODE (src) != MINUS);
2309
2310               /* Rule 4 */
2311               if (REG_P (XEXP (src, 0))
2312                   && REGNO (XEXP (src, 0)) == cfa.reg
2313                   && CONST_INT_P (XEXP (src, 1)))
2314                 {
2315                   /* Setting a temporary CFA register that will be copied
2316                      into the FP later on.  */
2317                   offset = - INTVAL (XEXP (src, 1));
2318                   cfa.offset += offset;
2319                   cfa.reg = REGNO (dest);
2320                   /* Or used to save regs to the stack.  */
2321                   cfa_temp.reg = cfa.reg;
2322                   cfa_temp.offset = cfa.offset;
2323                 }
2324
2325               /* Rule 5 */
2326               else if (REG_P (XEXP (src, 0))
2327                        && REGNO (XEXP (src, 0)) == cfa_temp.reg
2328                        && XEXP (src, 1) == stack_pointer_rtx)
2329                 {
2330                   /* Setting a scratch register that we will use instead
2331                      of SP for saving registers to the stack.  */
2332                   gcc_assert (cfa.reg == STACK_POINTER_REGNUM);
2333                   cfa_store.reg = REGNO (dest);
2334                   cfa_store.offset = cfa.offset - cfa_temp.offset;
2335                 }
2336
2337               /* Rule 9 */
2338               else if (GET_CODE (src) == LO_SUM
2339                        && CONST_INT_P (XEXP (src, 1)))
2340                 {
2341                   cfa_temp.reg = REGNO (dest);
2342                   cfa_temp.offset = INTVAL (XEXP (src, 1));
2343                 }
2344               else
2345                 gcc_unreachable ();
2346             }
2347           break;
2348
2349           /* Rule 6 */
2350         case CONST_INT:
2351           cfa_temp.reg = REGNO (dest);
2352           cfa_temp.offset = INTVAL (src);
2353           break;
2354
2355           /* Rule 7 */
2356         case IOR:
2357           gcc_assert (REG_P (XEXP (src, 0))
2358                       && (unsigned) REGNO (XEXP (src, 0)) == cfa_temp.reg
2359                       && CONST_INT_P (XEXP (src, 1)));
2360
2361           if ((unsigned) REGNO (dest) != cfa_temp.reg)
2362             cfa_temp.reg = REGNO (dest);
2363           cfa_temp.offset |= INTVAL (XEXP (src, 1));
2364           break;
2365
2366           /* Skip over HIGH, assuming it will be followed by a LO_SUM,
2367              which will fill in all of the bits.  */
2368           /* Rule 8 */
2369         case HIGH:
2370           break;
2371
2372           /* Rule 15 */
2373         case UNSPEC:
2374         case UNSPEC_VOLATILE:
2375           gcc_assert (targetm.dwarf_handle_frame_unspec);
2376           targetm.dwarf_handle_frame_unspec (label, expr, XINT (src, 1));
2377           return;
2378
2379           /* Rule 16 */
2380         case AND:
2381           /* If this AND operation happens on stack pointer in prologue,
2382              we assume the stack is realigned and we extract the
2383              alignment.  */
2384           if (fde && XEXP (src, 0) == stack_pointer_rtx)
2385             {
2386               gcc_assert (cfa_store.reg == REGNO (XEXP (src, 0)));
2387               fde->stack_realign = 1;
2388               fde->stack_realignment = INTVAL (XEXP (src, 1));
2389               cfa_store.offset = 0;
2390
2391               if (cfa.reg != STACK_POINTER_REGNUM
2392                   && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2393                 fde->drap_reg = cfa.reg;
2394             }
2395           return;
2396
2397         default:
2398           gcc_unreachable ();
2399         }
2400
2401       def_cfa_1 (label, &cfa);
2402       break;
2403
2404     case MEM:
2405
2406       /* Saving a register to the stack.  Make sure dest is relative to the
2407          CFA register.  */
2408       switch (GET_CODE (XEXP (dest, 0)))
2409         {
2410           /* Rule 10 */
2411           /* With a push.  */
2412         case PRE_MODIFY:
2413           /* We can't handle variable size modifications.  */
2414           gcc_assert (GET_CODE (XEXP (XEXP (XEXP (dest, 0), 1), 1))
2415                       == CONST_INT);
2416           offset = -INTVAL (XEXP (XEXP (XEXP (dest, 0), 1), 1));
2417
2418           gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
2419                       && cfa_store.reg == STACK_POINTER_REGNUM);
2420
2421           cfa_store.offset += offset;
2422           if (cfa.reg == STACK_POINTER_REGNUM)
2423             cfa.offset = cfa_store.offset;
2424
2425           offset = -cfa_store.offset;
2426           break;
2427
2428           /* Rule 11 */
2429         case PRE_INC:
2430         case PRE_DEC:
2431           offset = GET_MODE_SIZE (GET_MODE (dest));
2432           if (GET_CODE (XEXP (dest, 0)) == PRE_INC)
2433             offset = -offset;
2434
2435           gcc_assert ((REGNO (XEXP (XEXP (dest, 0), 0))
2436                        == STACK_POINTER_REGNUM)
2437                       && cfa_store.reg == STACK_POINTER_REGNUM);
2438
2439           cfa_store.offset += offset;
2440
2441           /* Rule 18: If stack is aligned, we will use FP as a
2442              reference to represent the address of the stored
2443              regiser.  */
2444           if (fde
2445               && fde->stack_realign
2446               && src == hard_frame_pointer_rtx)
2447             {
2448               gcc_assert (cfa.reg != HARD_FRAME_POINTER_REGNUM);
2449               cfa_store.offset = 0;
2450             }
2451
2452           if (cfa.reg == STACK_POINTER_REGNUM)
2453             cfa.offset = cfa_store.offset;
2454
2455           offset = -cfa_store.offset;
2456           break;
2457
2458           /* Rule 12 */
2459           /* With an offset.  */
2460         case PLUS:
2461         case MINUS:
2462         case LO_SUM:
2463           {
2464             int regno;
2465
2466             gcc_assert (CONST_INT_P (XEXP (XEXP (dest, 0), 1))
2467                         && REG_P (XEXP (XEXP (dest, 0), 0)));
2468             offset = INTVAL (XEXP (XEXP (dest, 0), 1));
2469             if (GET_CODE (XEXP (dest, 0)) == MINUS)
2470               offset = -offset;
2471
2472             regno = REGNO (XEXP (XEXP (dest, 0), 0));
2473
2474             if (cfa_store.reg == (unsigned) regno)
2475               offset -= cfa_store.offset;
2476             else
2477               {
2478                 gcc_assert (cfa_temp.reg == (unsigned) regno);
2479                 offset -= cfa_temp.offset;
2480               }
2481           }
2482           break;
2483
2484           /* Rule 13 */
2485           /* Without an offset.  */
2486         case REG:
2487           {
2488             int regno = REGNO (XEXP (dest, 0));
2489
2490             if (cfa_store.reg == (unsigned) regno)
2491               offset = -cfa_store.offset;
2492             else
2493               {
2494                 gcc_assert (cfa_temp.reg == (unsigned) regno);
2495                 offset = -cfa_temp.offset;
2496               }
2497           }
2498           break;
2499
2500           /* Rule 14 */
2501         case POST_INC:
2502           gcc_assert (cfa_temp.reg
2503                       == (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)));
2504           offset = -cfa_temp.offset;
2505           cfa_temp.offset -= GET_MODE_SIZE (GET_MODE (dest));
2506           break;
2507
2508         default:
2509           gcc_unreachable ();
2510         }
2511
2512         /* Rule 17 */
2513         /* If the source operand of this MEM operation is not a
2514            register, basically the source is return address.  Here
2515            we only care how much stack grew and we don't save it.  */
2516       if (!REG_P (src))
2517         break;
2518
2519       if (REGNO (src) != STACK_POINTER_REGNUM
2520           && REGNO (src) != HARD_FRAME_POINTER_REGNUM
2521           && (unsigned) REGNO (src) == cfa.reg)
2522         {
2523           /* We're storing the current CFA reg into the stack.  */
2524
2525           if (cfa.offset == 0)
2526             {
2527               /* Rule 19 */
2528               /* If stack is aligned, putting CFA reg into stack means
2529                  we can no longer use reg + offset to represent CFA.
2530                  Here we use DW_CFA_def_cfa_expression instead.  The
2531                  result of this expression equals to the original CFA
2532                  value.  */
2533               if (fde
2534                   && fde->stack_realign
2535                   && cfa.indirect == 0
2536                   && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2537                 {
2538                   dw_cfa_location cfa_exp;
2539
2540                   gcc_assert (fde->drap_reg == cfa.reg);
2541
2542                   cfa_exp.indirect = 1;
2543                   cfa_exp.reg = HARD_FRAME_POINTER_REGNUM;
2544                   cfa_exp.base_offset = offset;
2545                   cfa_exp.offset = 0;
2546
2547                   fde->drap_reg_saved = 1;
2548
2549                   def_cfa_1 (label, &cfa_exp);
2550                   break;
2551                 }
2552
2553               /* If the source register is exactly the CFA, assume
2554                  we're saving SP like any other register; this happens
2555                  on the ARM.  */
2556               def_cfa_1 (label, &cfa);
2557               queue_reg_save (label, stack_pointer_rtx, NULL_RTX, offset);
2558               break;
2559             }
2560           else
2561             {
2562               /* Otherwise, we'll need to look in the stack to
2563                  calculate the CFA.  */
2564               rtx x = XEXP (dest, 0);
2565
2566               if (!REG_P (x))
2567                 x = XEXP (x, 0);
2568               gcc_assert (REG_P (x));
2569
2570               cfa.reg = REGNO (x);
2571               cfa.base_offset = offset;
2572               cfa.indirect = 1;
2573               def_cfa_1 (label, &cfa);
2574               break;
2575             }
2576         }
2577
2578       def_cfa_1 (label, &cfa);
2579       {
2580         span = targetm.dwarf_register_span (src);
2581
2582         if (!span)
2583           queue_reg_save (label, src, NULL_RTX, offset);
2584         else
2585           {
2586             /* We have a PARALLEL describing where the contents of SRC
2587                live.  Queue register saves for each piece of the
2588                PARALLEL.  */
2589             int par_index;
2590             int limit;
2591             HOST_WIDE_INT span_offset = offset;
2592
2593             gcc_assert (GET_CODE (span) == PARALLEL);
2594
2595             limit = XVECLEN (span, 0);
2596             for (par_index = 0; par_index < limit; par_index++)
2597               {
2598                 rtx elem = XVECEXP (span, 0, par_index);
2599
2600                 queue_reg_save (label, elem, NULL_RTX, span_offset);
2601                 span_offset += GET_MODE_SIZE (GET_MODE (elem));
2602               }
2603           }
2604       }
2605       break;
2606
2607     default:
2608       gcc_unreachable ();
2609     }
2610 }
2611
2612 /* Record call frame debugging information for INSN, which either
2613    sets SP or FP (adjusting how we calculate the frame address) or saves a
2614    register to the stack.  If INSN is NULL_RTX, initialize our state.
2615
2616    If AFTER_P is false, we're being called before the insn is emitted,
2617    otherwise after.  Call instructions get invoked twice.  */
2618
2619 void
2620 dwarf2out_frame_debug (rtx insn, bool after_p)
2621 {
2622   const char *label;
2623   rtx note, n;
2624   bool handled_one = false;
2625
2626   if (insn == NULL_RTX)
2627     {
2628       size_t i;
2629
2630       /* Flush any queued register saves.  */
2631       flush_queued_reg_saves ();
2632
2633       /* Set up state for generating call frame debug info.  */
2634       lookup_cfa (&cfa);
2635       gcc_assert (cfa.reg
2636                   == (unsigned long)DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM));
2637
2638       cfa.reg = STACK_POINTER_REGNUM;
2639       cfa_store = cfa;
2640       cfa_temp.reg = -1;
2641       cfa_temp.offset = 0;
2642
2643       for (i = 0; i < num_regs_saved_in_regs; i++)
2644         {
2645           regs_saved_in_regs[i].orig_reg = NULL_RTX;
2646           regs_saved_in_regs[i].saved_in_reg = NULL_RTX;
2647         }
2648       num_regs_saved_in_regs = 0;
2649
2650       if (barrier_args_size)
2651         {
2652           XDELETEVEC (barrier_args_size);
2653           barrier_args_size = NULL;
2654         }
2655       return;
2656     }
2657
2658   if (!NONJUMP_INSN_P (insn) || clobbers_queued_reg_save (insn))
2659     flush_queued_reg_saves ();
2660
2661   if (! RTX_FRAME_RELATED_P (insn))
2662     {
2663       if (!ACCUMULATE_OUTGOING_ARGS)
2664         dwarf2out_stack_adjust (insn, after_p);
2665       return;
2666     }
2667
2668   label = dwarf2out_cfi_label (false);
2669
2670   for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
2671     switch (REG_NOTE_KIND (note))
2672       {
2673       case REG_FRAME_RELATED_EXPR:
2674         insn = XEXP (note, 0);
2675         goto found;
2676
2677       case REG_CFA_DEF_CFA:
2678         dwarf2out_frame_debug_def_cfa (XEXP (note, 0), label);
2679         handled_one = true;
2680         break;
2681
2682       case REG_CFA_ADJUST_CFA:
2683         n = XEXP (note, 0);
2684         if (n == NULL)
2685           {
2686             n = PATTERN (insn);
2687             if (GET_CODE (n) == PARALLEL)
2688               n = XVECEXP (n, 0, 0);
2689           }
2690         dwarf2out_frame_debug_adjust_cfa (n, label);
2691         handled_one = true;
2692         break;
2693
2694       case REG_CFA_OFFSET:
2695         n = XEXP (note, 0);
2696         if (n == NULL)
2697           n = single_set (insn);
2698         dwarf2out_frame_debug_cfa_offset (n, label);
2699         handled_one = true;
2700         break;
2701
2702       case REG_CFA_REGISTER:
2703         n = XEXP (note, 0);
2704         if (n == NULL)
2705           {
2706             n = PATTERN (insn);
2707             if (GET_CODE (n) == PARALLEL)
2708               n = XVECEXP (n, 0, 0);
2709           }
2710         dwarf2out_frame_debug_cfa_register (n, label);
2711         handled_one = true;
2712         break;
2713
2714       case REG_CFA_RESTORE:
2715         n = XEXP (note, 0);
2716         if (n == NULL)
2717           {
2718             n = PATTERN (insn);
2719             if (GET_CODE (n) == PARALLEL)
2720               n = XVECEXP (n, 0, 0);
2721             n = XEXP (n, 0);
2722           }
2723         dwarf2out_frame_debug_cfa_restore (n, label);
2724         handled_one = true;
2725         break;
2726
2727       default:
2728         break;
2729       }
2730   if (handled_one)
2731     return;
2732
2733   insn = PATTERN (insn);
2734  found:
2735   dwarf2out_frame_debug_expr (insn, label);
2736 }
2737
2738 /* Determine if we need to save and restore CFI information around this
2739    epilogue.  If SIBCALL is true, then this is a sibcall epilogue.  If
2740    we do need to save/restore, then emit the save now, and insert a
2741    NOTE_INSN_CFA_RESTORE_STATE at the appropriate place in the stream.  */
2742
2743 void
2744 dwarf2out_begin_epilogue (rtx insn)
2745 {
2746   bool saw_frp = false;
2747   rtx i;
2748
2749   /* Scan forward to the return insn, noticing if there are possible
2750      frame related insns.  */
2751   for (i = NEXT_INSN (insn); i ; i = NEXT_INSN (i))
2752     {
2753       if (!INSN_P (i))
2754         continue;
2755
2756       /* Look for both regular and sibcalls to end the block.  */
2757       if (returnjump_p (i))
2758         break;
2759       if (CALL_P (i) && SIBLING_CALL_P (i))
2760         break;
2761
2762       if (RTX_FRAME_RELATED_P (i))
2763         saw_frp = true;
2764     }
2765
2766   /* If the port doesn't emit epilogue unwind info, we don't need a
2767      save/restore pair.  */
2768   if (!saw_frp)
2769     return;
2770
2771   /* Otherwise, search forward to see if the return insn was the last
2772      basic block of the function.  If so, we don't need save/restore.  */
2773   gcc_assert (i != NULL);
2774   i = next_real_insn (i);
2775   if (i == NULL)
2776     return;
2777
2778   /* Insert the restore before that next real insn in the stream, and before
2779      a potential NOTE_INSN_EPILOGUE_BEG -- we do need these notes to be
2780      properly nested.  This should be after any label or alignment.  This
2781      will be pushed into the CFI stream by the function below.  */
2782   while (1)
2783     {
2784       rtx p = PREV_INSN (i);
2785       if (!NOTE_P (p))
2786         break;
2787       if (NOTE_KIND (p) == NOTE_INSN_BASIC_BLOCK)
2788         break;
2789       i = p;
2790     }
2791   emit_note_before (NOTE_INSN_CFA_RESTORE_STATE, i);
2792
2793   emit_cfa_remember = true;
2794
2795   /* And emulate the state save.  */
2796   gcc_assert (!cfa_remember.in_use);
2797   cfa_remember = cfa;
2798   cfa_remember.in_use = 1;
2799 }
2800
2801 /* A "subroutine" of dwarf2out_begin_epilogue.  Emit the restore required.  */
2802
2803 void
2804 dwarf2out_frame_debug_restore_state (void)
2805 {
2806   dw_cfi_ref cfi = new_cfi (); 
2807   const char *label = dwarf2out_cfi_label (false);
2808
2809   cfi->dw_cfi_opc = DW_CFA_restore_state;
2810   add_fde_cfi (label, cfi);
2811
2812   gcc_assert (cfa_remember.in_use);
2813   cfa = cfa_remember;
2814   cfa_remember.in_use = 0;
2815 }
2816
2817 #endif
2818
2819 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used.  */
2820 static enum dw_cfi_oprnd_type dw_cfi_oprnd1_desc
2821  (enum dwarf_call_frame_info cfi);
2822
2823 static enum dw_cfi_oprnd_type
2824 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
2825 {
2826   switch (cfi)
2827     {
2828     case DW_CFA_nop:
2829     case DW_CFA_GNU_window_save:
2830     case DW_CFA_remember_state:
2831     case DW_CFA_restore_state:
2832       return dw_cfi_oprnd_unused;
2833
2834     case DW_CFA_set_loc:
2835     case DW_CFA_advance_loc1:
2836     case DW_CFA_advance_loc2:
2837     case DW_CFA_advance_loc4:
2838     case DW_CFA_MIPS_advance_loc8:
2839       return dw_cfi_oprnd_addr;
2840
2841     case DW_CFA_offset:
2842     case DW_CFA_offset_extended:
2843     case DW_CFA_def_cfa:
2844     case DW_CFA_offset_extended_sf:
2845     case DW_CFA_def_cfa_sf:
2846     case DW_CFA_restore:
2847     case DW_CFA_restore_extended:
2848     case DW_CFA_undefined:
2849     case DW_CFA_same_value:
2850     case DW_CFA_def_cfa_register:
2851     case DW_CFA_register:
2852       return dw_cfi_oprnd_reg_num;
2853
2854     case DW_CFA_def_cfa_offset:
2855     case DW_CFA_GNU_args_size:
2856     case DW_CFA_def_cfa_offset_sf:
2857       return dw_cfi_oprnd_offset;
2858
2859     case DW_CFA_def_cfa_expression:
2860     case DW_CFA_expression:
2861       return dw_cfi_oprnd_loc;
2862
2863     default:
2864       gcc_unreachable ();
2865     }
2866 }
2867
2868 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used.  */
2869 static enum dw_cfi_oprnd_type dw_cfi_oprnd2_desc
2870  (enum dwarf_call_frame_info cfi);
2871
2872 static enum dw_cfi_oprnd_type
2873 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
2874 {
2875   switch (cfi)
2876     {
2877     case DW_CFA_def_cfa:
2878     case DW_CFA_def_cfa_sf:
2879     case DW_CFA_offset:
2880     case DW_CFA_offset_extended_sf:
2881     case DW_CFA_offset_extended:
2882       return dw_cfi_oprnd_offset;
2883
2884     case DW_CFA_register:
2885       return dw_cfi_oprnd_reg_num;
2886
2887     default:
2888       return dw_cfi_oprnd_unused;
2889     }
2890 }
2891
2892 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
2893
2894 /* Switch to eh_frame_section.  If we don't have an eh_frame_section,
2895    switch to the data section instead, and write out a synthetic label
2896    for collect2.  */
2897
2898 static void
2899 switch_to_eh_frame_section (void)
2900 {
2901   tree label;
2902
2903 #ifdef EH_FRAME_SECTION_NAME
2904   if (eh_frame_section == 0)
2905     {
2906       int flags;
2907
2908       if (EH_TABLES_CAN_BE_READ_ONLY)
2909         {
2910           int fde_encoding;
2911           int per_encoding;
2912           int lsda_encoding;
2913
2914           fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
2915                                                        /*global=*/0);
2916           per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
2917                                                        /*global=*/1);
2918           lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
2919                                                         /*global=*/0);
2920           flags = ((! flag_pic
2921                     || ((fde_encoding & 0x70) != DW_EH_PE_absptr
2922                         && (fde_encoding & 0x70) != DW_EH_PE_aligned
2923                         && (per_encoding & 0x70) != DW_EH_PE_absptr
2924                         && (per_encoding & 0x70) != DW_EH_PE_aligned
2925                         && (lsda_encoding & 0x70) != DW_EH_PE_absptr
2926                         && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
2927                    ? 0 : SECTION_WRITE);
2928         }
2929       else
2930         flags = SECTION_WRITE;
2931       eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
2932     }
2933 #endif
2934
2935   if (eh_frame_section)
2936     switch_to_section (eh_frame_section);
2937   else
2938     {
2939       /* We have no special eh_frame section.  Put the information in
2940          the data section and emit special labels to guide collect2.  */
2941       switch_to_section (data_section);
2942       label = get_file_function_name ("F");
2943       ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
2944       targetm.asm_out.globalize_label (asm_out_file,
2945                                        IDENTIFIER_POINTER (label));
2946       ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
2947     }
2948 }
2949
2950 /* Output a Call Frame Information opcode and its operand(s).  */
2951
2952 static void
2953 output_cfi (dw_cfi_ref cfi, dw_fde_ref fde, int for_eh)
2954 {
2955   unsigned long r;
2956   HOST_WIDE_INT off;
2957
2958   if (cfi->dw_cfi_opc == DW_CFA_advance_loc)
2959     dw2_asm_output_data (1, (cfi->dw_cfi_opc
2960                              | (cfi->dw_cfi_oprnd1.dw_cfi_offset & 0x3f)),
2961                          "DW_CFA_advance_loc " HOST_WIDE_INT_PRINT_HEX,
2962                          ((unsigned HOST_WIDE_INT)
2963                           cfi->dw_cfi_oprnd1.dw_cfi_offset));
2964   else if (cfi->dw_cfi_opc == DW_CFA_offset)
2965     {
2966       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2967       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
2968                            "DW_CFA_offset, column 0x%lx", r);
2969       off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
2970       dw2_asm_output_data_uleb128 (off, NULL);
2971     }
2972   else if (cfi->dw_cfi_opc == DW_CFA_restore)
2973     {
2974       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2975       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
2976                            "DW_CFA_restore, column 0x%lx", r);
2977     }
2978   else
2979     {
2980       dw2_asm_output_data (1, cfi->dw_cfi_opc,
2981                            "%s", dwarf_cfi_name (cfi->dw_cfi_opc));
2982
2983       switch (cfi->dw_cfi_opc)
2984         {
2985         case DW_CFA_set_loc:
2986           if (for_eh)
2987             dw2_asm_output_encoded_addr_rtx (
2988                 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0),
2989                 gen_rtx_SYMBOL_REF (Pmode, cfi->dw_cfi_oprnd1.dw_cfi_addr),
2990                 false, NULL);
2991           else
2992             dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2993                                  cfi->dw_cfi_oprnd1.dw_cfi_addr, NULL);
2994           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2995           break;
2996
2997         case DW_CFA_advance_loc1:
2998           dw2_asm_output_delta (1, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2999                                 fde->dw_fde_current_label, NULL);
3000           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3001           break;
3002
3003         case DW_CFA_advance_loc2:
3004           dw2_asm_output_delta (2, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3005                                 fde->dw_fde_current_label, NULL);
3006           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3007           break;
3008
3009         case DW_CFA_advance_loc4:
3010           dw2_asm_output_delta (4, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3011                                 fde->dw_fde_current_label, NULL);
3012           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3013           break;
3014
3015         case DW_CFA_MIPS_advance_loc8:
3016           dw2_asm_output_delta (8, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3017                                 fde->dw_fde_current_label, NULL);
3018           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3019           break;
3020
3021         case DW_CFA_offset_extended:
3022           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3023           dw2_asm_output_data_uleb128 (r, NULL);
3024           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3025           dw2_asm_output_data_uleb128 (off, NULL);
3026           break;
3027
3028         case DW_CFA_def_cfa:
3029           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3030           dw2_asm_output_data_uleb128 (r, NULL);
3031           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
3032           break;
3033
3034         case DW_CFA_offset_extended_sf:
3035           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3036           dw2_asm_output_data_uleb128 (r, NULL);
3037           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3038           dw2_asm_output_data_sleb128 (off, NULL);
3039           break;
3040
3041         case DW_CFA_def_cfa_sf:
3042           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3043           dw2_asm_output_data_uleb128 (r, NULL);
3044           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3045           dw2_asm_output_data_sleb128 (off, NULL);
3046           break;
3047
3048         case DW_CFA_restore_extended:
3049         case DW_CFA_undefined:
3050         case DW_CFA_same_value:
3051         case DW_CFA_def_cfa_register:
3052           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3053           dw2_asm_output_data_uleb128 (r, NULL);
3054           break;
3055
3056         case DW_CFA_register:
3057           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3058           dw2_asm_output_data_uleb128 (r, NULL);
3059           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, for_eh);
3060           dw2_asm_output_data_uleb128 (r, NULL);
3061           break;
3062
3063         case DW_CFA_def_cfa_offset:
3064         case DW_CFA_GNU_args_size:
3065           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
3066           break;
3067
3068         case DW_CFA_def_cfa_offset_sf:
3069           off = div_data_align (cfi->dw_cfi_oprnd1.dw_cfi_offset);
3070           dw2_asm_output_data_sleb128 (off, NULL);
3071           break;
3072
3073         case DW_CFA_GNU_window_save:
3074           break;
3075
3076         case DW_CFA_def_cfa_expression:
3077         case DW_CFA_expression:
3078           output_cfa_loc (cfi);
3079           break;
3080
3081         case DW_CFA_GNU_negative_offset_extended:
3082           /* Obsoleted by DW_CFA_offset_extended_sf.  */
3083           gcc_unreachable ();
3084
3085         default:
3086           break;
3087         }
3088     }
3089 }
3090
3091 /* Similar, but do it via assembler directives instead.  */
3092
3093 static void
3094 output_cfi_directive (dw_cfi_ref cfi)
3095 {
3096   unsigned long r, r2;
3097
3098   switch (cfi->dw_cfi_opc)
3099     {
3100     case DW_CFA_advance_loc:
3101     case DW_CFA_advance_loc1:
3102     case DW_CFA_advance_loc2:
3103     case DW_CFA_advance_loc4:
3104     case DW_CFA_MIPS_advance_loc8:
3105     case DW_CFA_set_loc:
3106       /* Should only be created by add_fde_cfi in a code path not
3107          followed when emitting via directives.  The assembler is
3108          going to take care of this for us.  */
3109       gcc_unreachable ();
3110
3111     case DW_CFA_offset:
3112     case DW_CFA_offset_extended:
3113     case DW_CFA_offset_extended_sf:
3114       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3115       fprintf (asm_out_file, "\t.cfi_offset %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
3116                r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
3117       break;
3118
3119     case DW_CFA_restore:
3120     case DW_CFA_restore_extended:
3121       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3122       fprintf (asm_out_file, "\t.cfi_restore %lu\n", r);
3123       break;
3124
3125     case DW_CFA_undefined:
3126       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3127       fprintf (asm_out_file, "\t.cfi_undefined %lu\n", r);
3128       break;
3129
3130     case DW_CFA_same_value:
3131       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3132       fprintf (asm_out_file, "\t.cfi_same_value %lu\n", r);
3133       break;
3134
3135     case DW_CFA_def_cfa:
3136     case DW_CFA_def_cfa_sf:
3137       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3138       fprintf (asm_out_file, "\t.cfi_def_cfa %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
3139                r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
3140       break;
3141
3142     case DW_CFA_def_cfa_register:
3143       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3144       fprintf (asm_out_file, "\t.cfi_def_cfa_register %lu\n", r);
3145       break;
3146
3147     case DW_CFA_register:
3148       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3149       r2 = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, 1);
3150       fprintf (asm_out_file, "\t.cfi_register %lu, %lu\n", r, r2);
3151       break;
3152
3153     case DW_CFA_def_cfa_offset:
3154     case DW_CFA_def_cfa_offset_sf:
3155       fprintf (asm_out_file, "\t.cfi_def_cfa_offset "
3156                HOST_WIDE_INT_PRINT_DEC"\n",
3157                cfi->dw_cfi_oprnd1.dw_cfi_offset);
3158       break;
3159
3160     case DW_CFA_remember_state:
3161       fprintf (asm_out_file, "\t.cfi_remember_state\n");
3162       break;
3163     case DW_CFA_restore_state:
3164       fprintf (asm_out_file, "\t.cfi_restore_state\n");
3165       break;
3166
3167     case DW_CFA_GNU_args_size:
3168       fprintf (asm_out_file, "\t.cfi_escape 0x%x,", DW_CFA_GNU_args_size);
3169       dw2_asm_output_data_uleb128_raw (cfi->dw_cfi_oprnd1.dw_cfi_offset);
3170       if (flag_debug_asm)
3171         fprintf (asm_out_file, "\t%s args_size "HOST_WIDE_INT_PRINT_DEC,
3172                  ASM_COMMENT_START, cfi->dw_cfi_oprnd1.dw_cfi_offset);
3173       fputc ('\n', asm_out_file);
3174       break;
3175
3176     case DW_CFA_GNU_window_save:
3177       fprintf (asm_out_file, "\t.cfi_window_save\n");
3178       break;
3179
3180     case DW_CFA_def_cfa_expression:
3181     case DW_CFA_expression:
3182       fprintf (asm_out_file, "\t.cfi_escape 0x%x,", cfi->dw_cfi_opc);
3183       output_cfa_loc_raw (cfi);
3184       fputc ('\n', asm_out_file);
3185       break;
3186
3187     default:
3188       gcc_unreachable ();
3189     }
3190 }
3191
3192 /* Output the call frame information used to record information
3193    that relates to calculating the frame pointer, and records the
3194    location of saved registers.  */
3195
3196 static void
3197 output_call_frame_info (int for_eh)
3198 {
3199   unsigned int i;
3200   dw_fde_ref fde;
3201   dw_cfi_ref cfi;
3202   char l1[20], l2[20], section_start_label[20];
3203   bool any_lsda_needed = false;
3204   char augmentation[6];
3205   int augmentation_size;
3206   int fde_encoding = DW_EH_PE_absptr;
3207   int per_encoding = DW_EH_PE_absptr;
3208   int lsda_encoding = DW_EH_PE_absptr;
3209   int return_reg;
3210   int dw_cie_version;
3211
3212   /* Don't emit a CIE if there won't be any FDEs.  */
3213   if (fde_table_in_use == 0)
3214     return;
3215
3216   /* Nothing to do if the assembler's doing it all.  */
3217   if (dwarf2out_do_cfi_asm ())
3218     return;
3219
3220   /* If we make FDEs linkonce, we may have to emit an empty label for
3221      an FDE that wouldn't otherwise be emitted.  We want to avoid
3222      having an FDE kept around when the function it refers to is
3223      discarded.  Example where this matters: a primary function
3224      template in C++ requires EH information, but an explicit
3225      specialization doesn't.  */
3226   if (TARGET_USES_WEAK_UNWIND_INFO
3227       && ! flag_asynchronous_unwind_tables
3228       && flag_exceptions
3229       && for_eh)
3230     for (i = 0; i < fde_table_in_use; i++)
3231       if ((fde_table[i].nothrow || fde_table[i].all_throwers_are_sibcalls)
3232           && !fde_table[i].uses_eh_lsda
3233           && ! DECL_WEAK (fde_table[i].decl))
3234         targetm.asm_out.unwind_label (asm_out_file, fde_table[i].decl,
3235                                       for_eh, /* empty */ 1);
3236
3237   /* If we don't have any functions we'll want to unwind out of, don't
3238      emit any EH unwind information.  Note that if exceptions aren't
3239      enabled, we won't have collected nothrow information, and if we
3240      asked for asynchronous tables, we always want this info.  */
3241   if (for_eh)
3242     {
3243       bool any_eh_needed = !flag_exceptions || flag_asynchronous_unwind_tables;
3244
3245       for (i = 0; i < fde_table_in_use; i++)
3246         if (fde_table[i].uses_eh_lsda)
3247           any_eh_needed = any_lsda_needed = true;
3248         else if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
3249           any_eh_needed = true;
3250         else if (! fde_table[i].nothrow
3251                  && ! fde_table[i].all_throwers_are_sibcalls)
3252           any_eh_needed = true;
3253
3254       if (! any_eh_needed)
3255         return;
3256     }
3257
3258   /* We're going to be generating comments, so turn on app.  */
3259   if (flag_debug_asm)
3260     app_enable ();
3261
3262   if (for_eh)
3263     switch_to_eh_frame_section ();
3264   else
3265     {
3266       if (!debug_frame_section)
3267         debug_frame_section = get_section (DEBUG_FRAME_SECTION,
3268                                            SECTION_DEBUG, NULL);
3269       switch_to_section (debug_frame_section);
3270     }
3271
3272   ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
3273   ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
3274
3275   /* Output the CIE.  */
3276   ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
3277   ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
3278   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
3279     dw2_asm_output_data (4, 0xffffffff,
3280       "Initial length escape value indicating 64-bit DWARF extension");
3281   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
3282                         "Length of Common Information Entry");
3283   ASM_OUTPUT_LABEL (asm_out_file, l1);
3284
3285   /* Now that the CIE pointer is PC-relative for EH,
3286      use 0 to identify the CIE.  */
3287   dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
3288                        (for_eh ? 0 : DWARF_CIE_ID),
3289                        "CIE Identifier Tag");
3290
3291   /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
3292      use CIE version 1, unless that would produce incorrect results
3293      due to overflowing the return register column.  */
3294   return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
3295   dw_cie_version = 1;
3296   if (return_reg >= 256 || dwarf_version > 2)
3297     dw_cie_version = 3;
3298   dw2_asm_output_data (1, dw_cie_version, "CIE Version");
3299
3300   augmentation[0] = 0;
3301   augmentation_size = 0;
3302   if (for_eh)
3303     {
3304       char *p;
3305
3306       /* Augmentation:
3307          z      Indicates that a uleb128 is present to size the
3308                 augmentation section.
3309          L      Indicates the encoding (and thus presence) of
3310                 an LSDA pointer in the FDE augmentation.
3311          R      Indicates a non-default pointer encoding for
3312                 FDE code pointers.
3313          P      Indicates the presence of an encoding + language
3314                 personality routine in the CIE augmentation.  */
3315
3316       fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
3317       per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
3318       lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
3319
3320       p = augmentation + 1;
3321       if (eh_personality_libfunc)
3322         {
3323           *p++ = 'P';
3324           augmentation_size += 1 + size_of_encoded_value (per_encoding);
3325           assemble_external_libcall (eh_personality_libfunc);
3326         }
3327       if (any_lsda_needed)
3328         {
3329           *p++ = 'L';
3330           augmentation_size += 1;
3331         }
3332       if (fde_encoding != DW_EH_PE_absptr)
3333         {
3334           *p++ = 'R';
3335           augmentation_size += 1;
3336         }
3337       if (p > augmentation + 1)
3338         {
3339           augmentation[0] = 'z';
3340           *p = '\0';
3341         }
3342
3343       /* Ug.  Some platforms can't do unaligned dynamic relocations at all.  */
3344       if (eh_personality_libfunc && per_encoding == DW_EH_PE_aligned)
3345         {
3346           int offset = (  4             /* Length */
3347                         + 4             /* CIE Id */
3348                         + 1             /* CIE version */
3349                         + strlen (augmentation) + 1     /* Augmentation */
3350                         + size_of_uleb128 (1)           /* Code alignment */
3351                         + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
3352                         + 1             /* RA column */
3353                         + 1             /* Augmentation size */
3354                         + 1             /* Personality encoding */ );
3355           int pad = -offset & (PTR_SIZE - 1);
3356
3357           augmentation_size += pad;
3358
3359           /* Augmentations should be small, so there's scarce need to
3360              iterate for a solution.  Die if we exceed one uleb128 byte.  */
3361           gcc_assert (size_of_uleb128 (augmentation_size) == 1);
3362         }
3363     }
3364
3365   dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
3366   dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
3367   dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
3368                                "CIE Data Alignment Factor");
3369
3370   if (dw_cie_version == 1)
3371     dw2_asm_output_data (1, return_reg, "CIE RA Column");
3372   else
3373     dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
3374
3375   if (augmentation[0])
3376     {
3377       dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
3378       if (eh_personality_libfunc)
3379         {
3380           dw2_asm_output_data (1, per_encoding, "Personality (%s)",
3381                                eh_data_format_name (per_encoding));
3382           dw2_asm_output_encoded_addr_rtx (per_encoding,
3383                                            eh_personality_libfunc,
3384                                            true, NULL);
3385         }
3386
3387       if (any_lsda_needed)
3388         dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
3389                              eh_data_format_name (lsda_encoding));
3390
3391       if (fde_encoding != DW_EH_PE_absptr)
3392         dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
3393                              eh_data_format_name (fde_encoding));
3394     }
3395
3396   for (cfi = cie_cfi_head; cfi != NULL; cfi = cfi->dw_cfi_next)
3397     output_cfi (cfi, NULL, for_eh);
3398
3399   /* Pad the CIE out to an address sized boundary.  */
3400   ASM_OUTPUT_ALIGN (asm_out_file,
3401                     floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
3402   ASM_OUTPUT_LABEL (asm_out_file, l2);
3403
3404   /* Loop through all of the FDE's.  */
3405   for (i = 0; i < fde_table_in_use; i++)
3406     {
3407       fde = &fde_table[i];
3408
3409       /* Don't emit EH unwind info for leaf functions that don't need it.  */
3410       if (for_eh && !flag_asynchronous_unwind_tables && flag_exceptions
3411           && (fde->nothrow || fde->all_throwers_are_sibcalls)
3412           && ! (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
3413           && !fde->uses_eh_lsda)
3414         continue;
3415
3416       targetm.asm_out.unwind_label (asm_out_file, fde->decl, for_eh, /* empty */ 0);
3417       targetm.asm_out.internal_label (asm_out_file, FDE_LABEL, for_eh + i * 2);
3418       ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + i * 2);
3419       ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + i * 2);
3420       if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
3421         dw2_asm_output_data (4, 0xffffffff,
3422                              "Initial length escape value indicating 64-bit DWARF extension");
3423       dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
3424                             "FDE Length");
3425       ASM_OUTPUT_LABEL (asm_out_file, l1);
3426
3427       if (for_eh)
3428         dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
3429       else
3430         dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
3431                                debug_frame_section, "FDE CIE offset");
3432
3433       if (for_eh)
3434         {
3435           if (fde->dw_fde_switched_sections)
3436             {
3437               rtx sym_ref2 = gen_rtx_SYMBOL_REF (Pmode,
3438                                       fde->dw_fde_unlikely_section_label);
3439               rtx sym_ref3= gen_rtx_SYMBOL_REF (Pmode,
3440                                       fde->dw_fde_hot_section_label);
3441               SYMBOL_REF_FLAGS (sym_ref2) |= SYMBOL_FLAG_LOCAL;
3442               SYMBOL_REF_FLAGS (sym_ref3) |= SYMBOL_FLAG_LOCAL;
3443               dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref3, false,
3444                                                "FDE initial location");
3445               dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
3446                                     fde->dw_fde_hot_section_end_label,
3447                                     fde->dw_fde_hot_section_label,
3448                                     "FDE address range");
3449               dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref2, false,
3450                                                "FDE initial location");
3451               dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
3452                                     fde->dw_fde_unlikely_section_end_label,
3453                                     fde->dw_fde_unlikely_section_label,
3454                                     "FDE address range");
3455             }
3456           else
3457             {
3458               rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, fde->dw_fde_begin);
3459               SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
3460               dw2_asm_output_encoded_addr_rtx (fde_encoding,
3461                                                sym_ref,
3462                                                false,
3463                                                "FDE initial location");
3464               dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
3465                                     fde->dw_fde_end, fde->dw_fde_begin,
3466                                     "FDE address range");
3467             }
3468         }
3469       else
3470         {
3471           if (fde->dw_fde_switched_sections)
3472             {
3473               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
3474                                    fde->dw_fde_hot_section_label,
3475                                    "FDE initial location");
3476               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
3477                                     fde->dw_fde_hot_section_end_label,
3478                                     fde->dw_fde_hot_section_label,
3479                                     "FDE address range");
3480               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
3481                                    fde->dw_fde_unlikely_section_label,
3482                                    "FDE initial location");
3483               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
3484                                     fde->dw_fde_unlikely_section_end_label,
3485                                     fde->dw_fde_unlikely_section_label,
3486                                     "FDE address range");
3487             }
3488           else
3489             {
3490               dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
3491                                    "FDE initial location");
3492               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
3493                                     fde->dw_fde_end, fde->dw_fde_begin,
3494                                     "FDE address range");
3495             }
3496         }
3497
3498       if (augmentation[0])
3499         {
3500           if (any_lsda_needed)
3501             {
3502               int size = size_of_encoded_value (lsda_encoding);
3503
3504               if (lsda_encoding == DW_EH_PE_aligned)
3505                 {
3506                   int offset = (  4             /* Length */
3507                                 + 4             /* CIE offset */
3508                                 + 2 * size_of_encoded_value (fde_encoding)
3509                                 + 1             /* Augmentation size */ );
3510                   int pad = -offset & (PTR_SIZE - 1);
3511
3512                   size += pad;
3513                   gcc_assert (size_of_uleb128 (size) == 1);
3514                 }
3515
3516               dw2_asm_output_data_uleb128 (size, "Augmentation size");
3517
3518               if (fde->uses_eh_lsda)
3519                 {
3520                   ASM_GENERATE_INTERNAL_LABEL (l1, "LLSDA",
3521                                                fde->funcdef_number);
3522                   dw2_asm_output_encoded_addr_rtx (
3523                         lsda_encoding, gen_rtx_SYMBOL_REF (Pmode, l1),
3524                         false, "Language Specific Data Area");
3525                 }
3526               else
3527                 {
3528                   if (lsda_encoding == DW_EH_PE_aligned)
3529                     ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
3530                   dw2_asm_output_data
3531                     (size_of_encoded_value (lsda_encoding), 0,
3532                      "Language Specific Data Area (none)");
3533                 }
3534             }
3535           else
3536             dw2_asm_output_data_uleb128 (0, "Augmentation size");
3537         }
3538
3539       /* Loop through the Call Frame Instructions associated with
3540          this FDE.  */
3541       fde->dw_fde_current_label = fde->dw_fde_begin;
3542       for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
3543         output_cfi (cfi, fde, for_eh);
3544
3545       /* Pad the FDE out to an address sized boundary.  */
3546       ASM_OUTPUT_ALIGN (asm_out_file,
3547                         floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
3548       ASM_OUTPUT_LABEL (asm_out_file, l2);
3549     }
3550
3551   if (for_eh && targetm.terminate_dw2_eh_frame_info)
3552     dw2_asm_output_data (4, 0, "End of Table");
3553 #ifdef MIPS_DEBUGGING_INFO
3554   /* Work around Irix 6 assembler bug whereby labels at the end of a section
3555      get a value of 0.  Putting .align 0 after the label fixes it.  */
3556   ASM_OUTPUT_ALIGN (asm_out_file, 0);
3557 #endif
3558
3559   /* Turn off app to make assembly quicker.  */
3560   if (flag_debug_asm)
3561     app_disable ();
3562 }
3563
3564 /* Output a marker (i.e. a label) for the beginning of a function, before
3565    the prologue.  */
3566
3567 void
3568 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
3569                           const char *file ATTRIBUTE_UNUSED)
3570 {
3571   char label[MAX_ARTIFICIAL_LABEL_BYTES];
3572   char * dup_label;
3573   dw_fde_ref fde;
3574
3575   current_function_func_begin_label = NULL;
3576
3577 #ifdef TARGET_UNWIND_INFO
3578   /* ??? current_function_func_begin_label is also used by except.c
3579      for call-site information.  We must emit this label if it might
3580      be used.  */
3581   if ((! flag_exceptions || USING_SJLJ_EXCEPTIONS)
3582       && ! dwarf2out_do_frame ())
3583     return;
3584 #else
3585   if (! dwarf2out_do_frame ())
3586     return;
3587 #endif
3588
3589   switch_to_section (function_section (current_function_decl));
3590   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
3591                                current_function_funcdef_no);
3592   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
3593                           current_function_funcdef_no);
3594   dup_label = xstrdup (label);
3595   current_function_func_begin_label = dup_label;
3596
3597 #ifdef TARGET_UNWIND_INFO
3598   /* We can elide the fde allocation if we're not emitting debug info.  */
3599   if (! dwarf2out_do_frame ())
3600     return;
3601 #endif
3602
3603   /* Expand the fde table if necessary.  */
3604   if (fde_table_in_use == fde_table_allocated)
3605     {
3606       fde_table_allocated += FDE_TABLE_INCREMENT;
3607       fde_table = GGC_RESIZEVEC (dw_fde_node, fde_table, fde_table_allocated);
3608       memset (fde_table + fde_table_in_use, 0,
3609               FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
3610     }
3611
3612   /* Record the FDE associated with this function.  */
3613   current_funcdef_fde = fde_table_in_use;
3614
3615   /* Add the new FDE at the end of the fde_table.  */
3616   fde = &fde_table[fde_table_in_use++];
3617   fde->decl = current_function_decl;
3618   fde->dw_fde_begin = dup_label;
3619   fde->dw_fde_current_label = dup_label;
3620   fde->dw_fde_hot_section_label = NULL;
3621   fde->dw_fde_hot_section_end_label = NULL;
3622   fde->dw_fde_unlikely_section_label = NULL;
3623   fde->dw_fde_unlikely_section_end_label = NULL;
3624   fde->dw_fde_switched_sections = false;
3625   fde->dw_fde_end = NULL;
3626   fde->dw_fde_cfi = NULL;
3627   fde->funcdef_number = current_function_funcdef_no;
3628   fde->nothrow = crtl->nothrow;
3629   fde->uses_eh_lsda = crtl->uses_eh_lsda;
3630   fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
3631   fde->drap_reg = INVALID_REGNUM;
3632   fde->vdrap_reg = INVALID_REGNUM;
3633
3634   args_size = old_args_size = 0;
3635
3636   /* We only want to output line number information for the genuine dwarf2
3637      prologue case, not the eh frame case.  */
3638 #ifdef DWARF2_DEBUGGING_INFO
3639   if (file)
3640     dwarf2out_source_line (line, file, 0, true);
3641 #endif
3642
3643   if (dwarf2out_do_cfi_asm ())
3644     {
3645       int enc;
3646       rtx ref;
3647
3648       fprintf (asm_out_file, "\t.cfi_startproc\n");
3649
3650       if (eh_personality_libfunc)
3651         {
3652           enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1); 
3653           ref = eh_personality_libfunc;
3654
3655           /* ??? The GAS support isn't entirely consistent.  We have to
3656              handle indirect support ourselves, but PC-relative is done
3657              in the assembler.  Further, the assembler can't handle any
3658              of the weirder relocation types.  */
3659           if (enc & DW_EH_PE_indirect)
3660             ref = dw2_force_const_mem (ref, true);
3661
3662           fprintf (asm_out_file, "\t.cfi_personality 0x%x,", enc);
3663           output_addr_const (asm_out_file, ref);
3664           fputc ('\n', asm_out_file);
3665         }
3666
3667       if (crtl->uses_eh_lsda)
3668         {
3669           char lab[20];
3670
3671           enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
3672           ASM_GENERATE_INTERNAL_LABEL (lab, "LLSDA",
3673                                        current_function_funcdef_no);
3674           ref = gen_rtx_SYMBOL_REF (Pmode, lab);
3675           SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
3676
3677           if (enc & DW_EH_PE_indirect)
3678             ref = dw2_force_const_mem (ref, true);
3679
3680           fprintf (asm_out_file, "\t.cfi_lsda 0x%x,", enc);
3681           output_addr_const (asm_out_file, ref);
3682           fputc ('\n', asm_out_file);
3683         }
3684     }
3685 }
3686
3687 /* Output a marker (i.e. a label) for the absolute end of the generated code
3688    for a function definition.  This gets called *after* the epilogue code has
3689    been generated.  */
3690
3691 void
3692 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
3693                         const char *file ATTRIBUTE_UNUSED)
3694 {
3695   dw_fde_ref fde;
3696   char label[MAX_ARTIFICIAL_LABEL_BYTES];
3697
3698 #ifdef DWARF2_DEBUGGING_INFO
3699   last_var_location_insn = NULL_RTX;
3700 #endif
3701
3702   if (dwarf2out_do_cfi_asm ())
3703     fprintf (asm_out_file, "\t.cfi_endproc\n");
3704
3705   /* Output a label to mark the endpoint of the code generated for this
3706      function.  */
3707   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
3708                                current_function_funcdef_no);
3709   ASM_OUTPUT_LABEL (asm_out_file, label);
3710   fde = current_fde ();
3711   gcc_assert (fde != NULL);
3712   fde->dw_fde_end = xstrdup (label);
3713 }
3714
3715 void
3716 dwarf2out_frame_init (void)
3717 {
3718   /* Allocate the initial hunk of the fde_table.  */
3719   fde_table = GGC_CNEWVEC (dw_fde_node, FDE_TABLE_INCREMENT);
3720   fde_table_allocated = FDE_TABLE_INCREMENT;
3721   fde_table_in_use = 0;
3722
3723   /* Generate the CFA instructions common to all FDE's.  Do it now for the
3724      sake of lookup_cfa.  */
3725
3726   /* On entry, the Canonical Frame Address is at SP.  */
3727   dwarf2out_def_cfa (NULL, STACK_POINTER_REGNUM, INCOMING_FRAME_SP_OFFSET);
3728
3729 #ifdef DWARF2_UNWIND_INFO
3730   if (DWARF2_UNWIND_INFO || DWARF2_FRAME_INFO)
3731     initial_return_save (INCOMING_RETURN_ADDR_RTX);
3732 #endif
3733 }
3734
3735 void
3736 dwarf2out_frame_finish (void)
3737 {
3738   /* Output call frame information.  */
3739   if (DWARF2_FRAME_INFO)
3740     output_call_frame_info (0);
3741
3742 #ifndef TARGET_UNWIND_INFO
3743   /* Output another copy for the unwinder.  */
3744   if (! USING_SJLJ_EXCEPTIONS && (flag_unwind_tables || flag_exceptions))
3745     output_call_frame_info (1);
3746 #endif
3747 }
3748
3749 /* Note that the current function section is being used for code.  */
3750
3751 static void
3752 dwarf2out_note_section_used (void)
3753 {
3754   section *sec = current_function_section ();
3755   if (sec == text_section)
3756     text_section_used = true;
3757   else if (sec == cold_text_section)
3758     cold_text_section_used = true;
3759 }
3760
3761 void
3762 dwarf2out_switch_text_section (void)
3763 {
3764   dw_fde_ref fde = current_fde ();
3765
3766   gcc_assert (cfun && fde);
3767
3768   fde->dw_fde_switched_sections = true;
3769   fde->dw_fde_hot_section_label = crtl->subsections.hot_section_label;
3770   fde->dw_fde_hot_section_end_label = crtl->subsections.hot_section_end_label;
3771   fde->dw_fde_unlikely_section_label = crtl->subsections.cold_section_label;
3772   fde->dw_fde_unlikely_section_end_label = crtl->subsections.cold_section_end_label;
3773   have_multiple_function_sections = true;
3774
3775   /* Reset the current label on switching text sections, so that we
3776      don't attempt to advance_loc4 between labels in different sections.  */
3777   fde->dw_fde_current_label = NULL;
3778
3779   /* There is no need to mark used sections when not debugging.  */
3780   if (cold_text_section != NULL)
3781     dwarf2out_note_section_used ();
3782 }
3783 #endif
3784 \f
3785 /* And now, the subset of the debugging information support code necessary
3786    for emitting location expressions.  */
3787
3788 /* Data about a single source file.  */
3789 struct GTY(()) dwarf_file_data {
3790   const char * filename;
3791   int emitted_number;
3792 };
3793
3794 typedef struct dw_val_struct *dw_val_ref;
3795 typedef struct die_struct *dw_die_ref;
3796 typedef const struct die_struct *const_dw_die_ref;
3797 typedef struct dw_loc_descr_struct *dw_loc_descr_ref;
3798 typedef struct dw_loc_list_struct *dw_loc_list_ref;
3799
3800 typedef struct GTY(()) deferred_locations_struct
3801 {
3802   tree variable;
3803   dw_die_ref die;
3804 } deferred_locations;
3805
3806 DEF_VEC_O(deferred_locations);
3807 DEF_VEC_ALLOC_O(deferred_locations,gc);
3808
3809 static GTY(()) VEC(deferred_locations, gc) *deferred_locations_list;
3810
3811 /* Each DIE may have a series of attribute/value pairs.  Values
3812    can take on several forms.  The forms that are used in this
3813    implementation are listed below.  */
3814
3815 enum dw_val_class
3816 {
3817   dw_val_class_addr,
3818   dw_val_class_offset,
3819   dw_val_class_loc,
3820   dw_val_class_loc_list,
3821   dw_val_class_range_list,
3822   dw_val_class_const,
3823   dw_val_class_unsigned_const,
3824   dw_val_class_long_long,
3825   dw_val_class_vec,
3826   dw_val_class_flag,
3827   dw_val_class_die_ref,
3828   dw_val_class_fde_ref,
3829   dw_val_class_lbl_id,
3830   dw_val_class_lineptr,
3831   dw_val_class_str,
3832   dw_val_class_macptr,
3833   dw_val_class_file
3834 };
3835
3836 /* Describe a double word constant value.  */
3837 /* ??? Every instance of long_long in the code really means CONST_DOUBLE.  */
3838
3839 typedef struct GTY(()) dw_long_long_struct {
3840   unsigned long hi;
3841   unsigned long low;
3842 }
3843 dw_long_long_const;
3844
3845 /* Describe a floating point constant value, or a vector constant value.  */
3846
3847 typedef struct GTY(()) dw_vec_struct {
3848   unsigned char * GTY((length ("%h.length"))) array;
3849   unsigned length;
3850   unsigned elt_size;
3851 }
3852 dw_vec_const;
3853
3854 /* The dw_val_node describes an attribute's value, as it is
3855    represented internally.  */
3856
3857 typedef struct GTY(()) dw_val_struct {
3858   enum dw_val_class val_class;
3859   union dw_val_struct_union
3860     {
3861       rtx GTY ((tag ("dw_val_class_addr"))) val_addr;
3862       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_offset"))) val_offset;
3863       dw_loc_list_ref GTY ((tag ("dw_val_class_loc_list"))) val_loc_list;
3864       dw_loc_descr_ref GTY ((tag ("dw_val_class_loc"))) val_loc;
3865       HOST_WIDE_INT GTY ((default)) val_int;
3866       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_unsigned_const"))) val_unsigned;
3867       dw_long_long_const GTY ((tag ("dw_val_class_long_long"))) val_long_long;
3868       dw_vec_const GTY ((tag ("dw_val_class_vec"))) val_vec;
3869       struct dw_val_die_union
3870         {
3871           dw_die_ref die;
3872           int external;
3873         } GTY ((tag ("dw_val_class_die_ref"))) val_die_ref;
3874       unsigned GTY ((tag ("dw_val_class_fde_ref"))) val_fde_index;
3875       struct indirect_string_node * GTY ((tag ("dw_val_class_str"))) val_str;
3876       char * GTY ((tag ("dw_val_class_lbl_id"))) val_lbl_id;
3877       unsigned char GTY ((tag ("dw_val_class_flag"))) val_flag;
3878       struct dwarf_file_data * GTY ((tag ("dw_val_class_file"))) val_file;
3879     }
3880   GTY ((desc ("%1.val_class"))) v;
3881 }
3882 dw_val_node;
3883
3884 /* Locations in memory are described using a sequence of stack machine
3885    operations.  */
3886
3887 typedef struct GTY(()) dw_loc_descr_struct {
3888   dw_loc_descr_ref dw_loc_next;
3889   enum dwarf_location_atom dw_loc_opc;
3890   int dw_loc_addr;
3891   dw_val_node dw_loc_oprnd1;
3892   dw_val_node dw_loc_oprnd2;
3893 }
3894 dw_loc_descr_node;
3895
3896 /* Location lists are ranges + location descriptions for that range,
3897    so you can track variables that are in different places over
3898    their entire life.  */
3899 typedef struct GTY(()) dw_loc_list_struct {
3900   dw_loc_list_ref dw_loc_next;
3901   const char *begin; /* Label for begin address of range */
3902   const char *end;  /* Label for end address of range */
3903   char *ll_symbol; /* Label for beginning of location list.
3904                       Only on head of list */
3905   const char *section; /* Section this loclist is relative to */
3906   dw_loc_descr_ref expr;
3907 } dw_loc_list_node;
3908
3909 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
3910
3911 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
3912
3913 /* Convert a DWARF stack opcode into its string name.  */
3914
3915 static const char *
3916 dwarf_stack_op_name (unsigned int op)
3917 {
3918   switch (op)
3919     {
3920     case DW_OP_addr:
3921     case INTERNAL_DW_OP_tls_addr:
3922       return "DW_OP_addr";
3923     case DW_OP_deref:
3924       return "DW_OP_deref";
3925     case DW_OP_const1u:
3926       return "DW_OP_const1u";
3927     case DW_OP_const1s:
3928       return "DW_OP_const1s";
3929     case DW_OP_const2u:
3930       return "DW_OP_const2u";
3931     case DW_OP_const2s:
3932       return "DW_OP_const2s";
3933     case DW_OP_const4u:
3934       return "DW_OP_const4u";
3935     case DW_OP_const4s:
3936       return "DW_OP_const4s";
3937     case DW_OP_const8u:
3938       return "DW_OP_const8u";
3939     case DW_OP_const8s:
3940       return "DW_OP_const8s";
3941     case DW_OP_constu:
3942       return "DW_OP_constu";
3943     case DW_OP_consts:
3944       return "DW_OP_consts";
3945     case DW_OP_dup:
3946       return "DW_OP_dup";
3947     case DW_OP_drop:
3948       return "DW_OP_drop";
3949     case DW_OP_over:
3950       return "DW_OP_over";
3951     case DW_OP_pick:
3952       return "DW_OP_pick";
3953     case DW_OP_swap:
3954       return "DW_OP_swap";
3955     case DW_OP_rot:
3956       return "DW_OP_rot";
3957     case DW_OP_xderef:
3958       return "DW_OP_xderef";
3959     case DW_OP_abs:
3960       return "DW_OP_abs";
3961     case DW_OP_and:
3962       return "DW_OP_and";
3963     case DW_OP_div:
3964       return "DW_OP_div";
3965     case DW_OP_minus:
3966       return "DW_OP_minus";
3967     case DW_OP_mod:
3968       return "DW_OP_mod";
3969     case DW_OP_mul:
3970       return "DW_OP_mul";
3971     case DW_OP_neg:
3972       return "DW_OP_neg";
3973     case DW_OP_not:
3974       return "DW_OP_not";
3975     case DW_OP_or:
3976       return "DW_OP_or";
3977     case DW_OP_plus:
3978       return "DW_OP_plus";
3979     case DW_OP_plus_uconst:
3980       return "DW_OP_plus_uconst";
3981     case DW_OP_shl:
3982       return "DW_OP_shl";
3983     case DW_OP_shr:
3984       return "DW_OP_shr";
3985     case DW_OP_shra:
3986       return "DW_OP_shra";
3987     case DW_OP_xor:
3988       return "DW_OP_xor";
3989     case DW_OP_bra:
3990       return "DW_OP_bra";
3991     case DW_OP_eq:
3992       return "DW_OP_eq";
3993     case DW_OP_ge:
3994       return "DW_OP_ge";
3995     case DW_OP_gt:
3996       return "DW_OP_gt";
3997     case DW_OP_le:
3998       return "DW_OP_le";
3999     case DW_OP_lt:
4000       return "DW_OP_lt";
4001     case DW_OP_ne:
4002       return "DW_OP_ne";
4003     case DW_OP_skip:
4004       return "DW_OP_skip";
4005     case DW_OP_lit0:
4006       return "DW_OP_lit0";
4007     case DW_OP_lit1:
4008       return "DW_OP_lit1";
4009     case DW_OP_lit2:
4010       return "DW_OP_lit2";
4011     case DW_OP_lit3:
4012       return "DW_OP_lit3";
4013     case DW_OP_lit4:
4014       return "DW_OP_lit4";
4015     case DW_OP_lit5:
4016       return "DW_OP_lit5";
4017     case DW_OP_lit6:
4018       return "DW_OP_lit6";
4019     case DW_OP_lit7:
4020       return "DW_OP_lit7";
4021     case DW_OP_lit8:
4022       return "DW_OP_lit8";
4023     case DW_OP_lit9:
4024       return "DW_OP_lit9";
4025     case DW_OP_lit10:
4026       return "DW_OP_lit10";
4027     case DW_OP_lit11:
4028       return "DW_OP_lit11";
4029     case DW_OP_lit12:
4030       return "DW_OP_lit12";
4031     case DW_OP_lit13:
4032       return "DW_OP_lit13";
4033     case DW_OP_lit14:
4034       return "DW_OP_lit14";
4035     case DW_OP_lit15:
4036       return "DW_OP_lit15";
4037     case DW_OP_lit16:
4038       return "DW_OP_lit16";
4039     case DW_OP_lit17:
4040       return "DW_OP_lit17";
4041     case DW_OP_lit18:
4042       return "DW_OP_lit18";
4043     case DW_OP_lit19:
4044       return "DW_OP_lit19";
4045     case DW_OP_lit20:
4046       return "DW_OP_lit20";
4047     case DW_OP_lit21:
4048       return "DW_OP_lit21";
4049     case DW_OP_lit22:
4050       return "DW_OP_lit22";
4051     case DW_OP_lit23:
4052       return "DW_OP_lit23";
4053     case DW_OP_lit24:
4054       return "DW_OP_lit24";
4055     case DW_OP_lit25:
4056       return "DW_OP_lit25";
4057     case DW_OP_lit26:
4058       return "DW_OP_lit26";
4059     case DW_OP_lit27:
4060       return "DW_OP_lit27";
4061     case DW_OP_lit28:
4062       return "DW_OP_lit28";
4063     case DW_OP_lit29:
4064       return "DW_OP_lit29";
4065     case DW_OP_lit30:
4066       return "DW_OP_lit30";
4067     case DW_OP_lit31:
4068       return "DW_OP_lit31";
4069     case DW_OP_reg0:
4070       return "DW_OP_reg0";
4071     case DW_OP_reg1:
4072       return "DW_OP_reg1";
4073     case DW_OP_reg2:
4074       return "DW_OP_reg2";
4075     case DW_OP_reg3:
4076       return "DW_OP_reg3";
4077     case DW_OP_reg4:
4078       return "DW_OP_reg4";
4079     case DW_OP_reg5:
4080       return "DW_OP_reg5";
4081     case DW_OP_reg6:
4082       return "DW_OP_reg6";
4083     case DW_OP_reg7:
4084       return "DW_OP_reg7";
4085     case DW_OP_reg8:
4086       return "DW_OP_reg8";
4087     case DW_OP_reg9:
4088       return "DW_OP_reg9";
4089     case DW_OP_reg10:
4090       return "DW_OP_reg10";
4091     case DW_OP_reg11:
4092       return "DW_OP_reg11";
4093     case DW_OP_reg12:
4094       return "DW_OP_reg12";
4095     case DW_OP_reg13:
4096       return "DW_OP_reg13";
4097     case DW_OP_reg14:
4098       return "DW_OP_reg14";
4099     case DW_OP_reg15:
4100       return "DW_OP_reg15";
4101     case DW_OP_reg16:
4102       return "DW_OP_reg16";
4103     case DW_OP_reg17:
4104       return "DW_OP_reg17";
4105     case DW_OP_reg18:
4106       return "DW_OP_reg18";
4107     case DW_OP_reg19:
4108       return "DW_OP_reg19";
4109     case DW_OP_reg20:
4110       return "DW_OP_reg20";
4111     case DW_OP_reg21:
4112       return "DW_OP_reg21";
4113     case DW_OP_reg22:
4114       return "DW_OP_reg22";
4115     case DW_OP_reg23:
4116       return "DW_OP_reg23";
4117     case DW_OP_reg24:
4118       return "DW_OP_reg24";
4119     case DW_OP_reg25:
4120       return "DW_OP_reg25";
4121     case DW_OP_reg26:
4122       return "DW_OP_reg26";
4123     case DW_OP_reg27:
4124       return "DW_OP_reg27";
4125     case DW_OP_reg28:
4126       return "DW_OP_reg28";
4127     case DW_OP_reg29:
4128       return "DW_OP_reg29";
4129     case DW_OP_reg30:
4130       return "DW_OP_reg30";
4131     case DW_OP_reg31:
4132       return "DW_OP_reg31";
4133     case DW_OP_breg0:
4134       return "DW_OP_breg0";
4135     case DW_OP_breg1:
4136       return "DW_OP_breg1";
4137     case DW_OP_breg2:
4138       return "DW_OP_breg2";
4139     case DW_OP_breg3:
4140       return "DW_OP_breg3";
4141     case DW_OP_breg4:
4142       return "DW_OP_breg4";
4143     case DW_OP_breg5:
4144       return "DW_OP_breg5";
4145     case DW_OP_breg6:
4146       return "DW_OP_breg6";
4147     case DW_OP_breg7:
4148       return "DW_OP_breg7";
4149     case DW_OP_breg8:
4150       return "DW_OP_breg8";
4151     case DW_OP_breg9:
4152       return "DW_OP_breg9";
4153     case DW_OP_breg10:
4154       return "DW_OP_breg10";
4155     case DW_OP_breg11:
4156       return "DW_OP_breg11";
4157     case DW_OP_breg12:
4158       return "DW_OP_breg12";
4159     case DW_OP_breg13:
4160       return "DW_OP_breg13";
4161     case DW_OP_breg14:
4162       return "DW_OP_breg14";
4163     case DW_OP_breg15:
4164       return "DW_OP_breg15";
4165     case DW_OP_breg16:
4166       return "DW_OP_breg16";
4167     case DW_OP_breg17:
4168       return "DW_OP_breg17";
4169     case DW_OP_breg18:
4170       return "DW_OP_breg18";
4171     case DW_OP_breg19:
4172       return "DW_OP_breg19";
4173     case DW_OP_breg20:
4174       return "DW_OP_breg20";
4175     case DW_OP_breg21:
4176       return "DW_OP_breg21";
4177     case DW_OP_breg22:
4178       return "DW_OP_breg22";
4179     case DW_OP_breg23:
4180       return "DW_OP_breg23";
4181     case DW_OP_breg24:
4182       return "DW_OP_breg24";
4183     case DW_OP_breg25:
4184       return "DW_OP_breg25";
4185     case DW_OP_breg26:
4186       return "DW_OP_breg26";
4187     case DW_OP_breg27:
4188       return "DW_OP_breg27";
4189     case DW_OP_breg28:
4190       return "DW_OP_breg28";
4191     case DW_OP_breg29:
4192       return "DW_OP_breg29";
4193     case DW_OP_breg30:
4194       return "DW_OP_breg30";
4195     case DW_OP_breg31:
4196       return "DW_OP_breg31";
4197     case DW_OP_regx:
4198       return "DW_OP_regx";
4199     case DW_OP_fbreg:
4200       return "DW_OP_fbreg";
4201     case DW_OP_bregx:
4202       return "DW_OP_bregx";
4203     case DW_OP_piece:
4204       return "DW_OP_piece";
4205     case DW_OP_deref_size:
4206       return "DW_OP_deref_size";
4207     case DW_OP_xderef_size:
4208       return "DW_OP_xderef_size";
4209     case DW_OP_nop:
4210       return "DW_OP_nop";
4211
4212     case DW_OP_push_object_address:
4213       return "DW_OP_push_object_address";
4214     case DW_OP_call2:
4215       return "DW_OP_call2";
4216     case DW_OP_call4:
4217       return "DW_OP_call4";
4218     case DW_OP_call_ref:
4219       return "DW_OP_call_ref";
4220     case DW_OP_form_tls_address:
4221       return "DW_OP_form_tls_address";
4222     case DW_OP_call_frame_cfa:
4223       return "DW_OP_call_frame_cfa";
4224     case DW_OP_bit_piece:
4225       return "DW_OP_bit_piece";
4226
4227     case DW_OP_GNU_push_tls_address:
4228       return "DW_OP_GNU_push_tls_address";
4229     case DW_OP_GNU_uninit:
4230       return "DW_OP_GNU_uninit";
4231     case DW_OP_GNU_encoded_addr:
4232       return "DW_OP_GNU_encoded_addr";
4233
4234     default:
4235       return "OP_<unknown>";
4236     }
4237 }
4238
4239 /* Return a pointer to a newly allocated location description.  Location
4240    descriptions are simple expression terms that can be strung
4241    together to form more complicated location (address) descriptions.  */
4242
4243 static inline dw_loc_descr_ref
4244 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
4245                unsigned HOST_WIDE_INT oprnd2)
4246 {
4247   dw_loc_descr_ref descr = GGC_CNEW (dw_loc_descr_node);
4248
4249   descr->dw_loc_opc = op;
4250   descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
4251   descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
4252   descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
4253   descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
4254
4255   return descr;
4256 }
4257
4258 /* Return a pointer to a newly allocated location description for
4259    REG and OFFSET.  */
4260
4261 static inline dw_loc_descr_ref
4262 new_reg_loc_descr (unsigned int reg,  unsigned HOST_WIDE_INT offset)
4263 {
4264   if (reg <= 31)
4265     return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
4266                           offset, 0);
4267   else
4268     return new_loc_descr (DW_OP_bregx, reg, offset);
4269 }
4270
4271 /* Add a location description term to a location description expression.  */
4272
4273 static inline void
4274 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
4275 {
4276   dw_loc_descr_ref *d;
4277
4278   /* Find the end of the chain.  */
4279   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
4280     ;
4281
4282   *d = descr;
4283 }
4284
4285 /* Add a constant OFFSET to a location expression.  */
4286
4287 static void
4288 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
4289 {
4290   dw_loc_descr_ref loc;
4291   HOST_WIDE_INT *p;
4292
4293   gcc_assert (*list_head != NULL);
4294
4295   if (!offset)
4296     return;
4297
4298   /* Find the end of the chain.  */
4299   for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
4300     ;
4301
4302   p = NULL;
4303   if (loc->dw_loc_opc == DW_OP_fbreg
4304       || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
4305     p = &loc->dw_loc_oprnd1.v.val_int;
4306   else if (loc->dw_loc_opc == DW_OP_bregx)
4307     p = &loc->dw_loc_oprnd2.v.val_int;
4308
4309   /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
4310      offset.  Don't optimize if an signed integer overflow would happen.  */
4311   if (p != NULL
4312       && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
4313           || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
4314     *p += offset;
4315
4316   else if (offset > 0)
4317     loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
4318
4319   else
4320     {
4321       loc->dw_loc_next = int_loc_descriptor (offset);
4322       add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_plus, 0, 0));
4323     }
4324 }
4325
4326 /* Return the size of a location descriptor.  */
4327
4328 static unsigned long
4329 size_of_loc_descr (dw_loc_descr_ref loc)
4330 {
4331   unsigned long size = 1;
4332
4333   switch (loc->dw_loc_opc)
4334     {
4335     case DW_OP_addr:
4336     case INTERNAL_DW_OP_tls_addr:
4337       size += DWARF2_ADDR_SIZE;
4338       break;
4339     case DW_OP_const1u:
4340     case DW_OP_const1s:
4341       size += 1;
4342       break;
4343     case DW_OP_const2u:
4344     case DW_OP_const2s:
4345       size += 2;
4346       break;
4347     case DW_OP_const4u:
4348     case DW_OP_const4s:
4349       size += 4;
4350       break;
4351     case DW_OP_const8u:
4352     case DW_OP_const8s:
4353       size += 8;
4354       break;
4355     case DW_OP_constu:
4356       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4357       break;
4358     case DW_OP_consts:
4359       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4360       break;
4361     case DW_OP_pick:
4362       size += 1;
4363       break;
4364     case DW_OP_plus_uconst:
4365       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4366       break;
4367     case DW_OP_skip:
4368     case DW_OP_bra:
4369       size += 2;
4370       break;
4371     case DW_OP_breg0:
4372     case DW_OP_breg1:
4373     case DW_OP_breg2:
4374     case DW_OP_breg3:
4375     case DW_OP_breg4:
4376     case DW_OP_breg5:
4377     case DW_OP_breg6:
4378     case DW_OP_breg7:
4379     case DW_OP_breg8:
4380     case DW_OP_breg9:
4381     case DW_OP_breg10:
4382     case DW_OP_breg11:
4383     case DW_OP_breg12:
4384     case DW_OP_breg13:
4385     case DW_OP_breg14:
4386     case DW_OP_breg15:
4387     case DW_OP_breg16:
4388     case DW_OP_breg17:
4389     case DW_OP_breg18:
4390     case DW_OP_breg19:
4391     case DW_OP_breg20:
4392     case DW_OP_breg21:
4393     case DW_OP_breg22:
4394     case DW_OP_breg23:
4395     case DW_OP_breg24:
4396     case DW_OP_breg25:
4397     case DW_OP_breg26:
4398     case DW_OP_breg27:
4399     case DW_OP_breg28:
4400     case DW_OP_breg29:
4401     case DW_OP_breg30:
4402     case DW_OP_breg31:
4403       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4404       break;
4405     case DW_OP_regx:
4406       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4407       break;
4408     case DW_OP_fbreg:
4409       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4410       break;
4411     case DW_OP_bregx:
4412       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4413       size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
4414       break;
4415     case DW_OP_piece:
4416       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4417       break;
4418     case DW_OP_deref_size:
4419     case DW_OP_xderef_size:
4420       size += 1;
4421       break;
4422     case DW_OP_call2:
4423       size += 2;
4424       break;
4425     case DW_OP_call4:
4426       size += 4;
4427       break;
4428     case DW_OP_call_ref:
4429       size += DWARF2_ADDR_SIZE;
4430       break;
4431     default:
4432       break;
4433     }
4434
4435   return size;
4436 }
4437
4438 /* Return the size of a series of location descriptors.  */
4439
4440 static unsigned long
4441 size_of_locs (dw_loc_descr_ref loc)
4442 {
4443   dw_loc_descr_ref l;
4444   unsigned long size;
4445
4446   /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
4447      field, to avoid writing to a PCH file.  */
4448   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
4449     {
4450       if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
4451         break;
4452       size += size_of_loc_descr (l);
4453     }
4454   if (! l)
4455     return size;
4456
4457   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
4458     {
4459       l->dw_loc_addr = size;
4460       size += size_of_loc_descr (l);
4461     }
4462
4463   return size;
4464 }
4465
4466 /* Output location description stack opcode's operands (if any).  */
4467
4468 static void
4469 output_loc_operands (dw_loc_descr_ref loc)
4470 {
4471   dw_val_ref val1 = &loc->dw_loc_oprnd1;
4472   dw_val_ref val2 = &loc->dw_loc_oprnd2;
4473
4474   switch (loc->dw_loc_opc)
4475     {
4476 #ifdef DWARF2_DEBUGGING_INFO
4477     case DW_OP_addr:
4478       dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
4479       break;
4480     case DW_OP_const2u:
4481     case DW_OP_const2s:
4482       dw2_asm_output_data (2, val1->v.val_int, NULL);
4483       break;
4484     case DW_OP_const4u:
4485     case DW_OP_const4s:
4486       dw2_asm_output_data (4, val1->v.val_int, NULL);
4487       break;
4488     case DW_OP_const8u:
4489     case DW_OP_const8s:
4490       gcc_assert (HOST_BITS_PER_LONG >= 64);
4491       dw2_asm_output_data (8, val1->v.val_int, NULL);
4492       break;
4493     case DW_OP_skip:
4494     case DW_OP_bra:
4495       {
4496         int offset;
4497
4498         gcc_assert (val1->val_class == dw_val_class_loc);
4499         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
4500
4501         dw2_asm_output_data (2, offset, NULL);
4502       }
4503       break;
4504 #else
4505     case DW_OP_addr:
4506     case DW_OP_const2u:
4507     case DW_OP_const2s:
4508     case DW_OP_const4u:
4509     case DW_OP_const4s:
4510     case DW_OP_const8u:
4511     case DW_OP_const8s:
4512     case DW_OP_skip:
4513     case DW_OP_bra:
4514       /* We currently don't make any attempt to make sure these are
4515          aligned properly like we do for the main unwind info, so
4516          don't support emitting things larger than a byte if we're
4517          only doing unwinding.  */
4518       gcc_unreachable ();
4519 #endif
4520     case DW_OP_const1u:
4521     case DW_OP_const1s:
4522       dw2_asm_output_data (1, val1->v.val_int, NULL);
4523       break;
4524     case DW_OP_constu:
4525       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4526       break;
4527     case DW_OP_consts:
4528       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
4529       break;
4530     case DW_OP_pick:
4531       dw2_asm_output_data (1, val1->v.val_int, NULL);
4532       break;
4533     case DW_OP_plus_uconst:
4534       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4535       break;
4536     case DW_OP_breg0:
4537     case DW_OP_breg1:
4538     case DW_OP_breg2:
4539     case DW_OP_breg3:
4540     case DW_OP_breg4:
4541     case DW_OP_breg5:
4542     case DW_OP_breg6:
4543     case DW_OP_breg7:
4544     case DW_OP_breg8:
4545     case DW_OP_breg9:
4546     case DW_OP_breg10:
4547     case DW_OP_breg11:
4548     case DW_OP_breg12:
4549     case DW_OP_breg13:
4550     case DW_OP_breg14:
4551     case DW_OP_breg15:
4552     case DW_OP_breg16:
4553     case DW_OP_breg17:
4554     case DW_OP_breg18:
4555     case DW_OP_breg19:
4556     case DW_OP_breg20:
4557     case DW_OP_breg21:
4558     case DW_OP_breg22:
4559     case DW_OP_breg23:
4560     case DW_OP_breg24:
4561     case DW_OP_breg25:
4562     case DW_OP_breg26:
4563     case DW_OP_breg27:
4564     case DW_OP_breg28:
4565     case DW_OP_breg29:
4566     case DW_OP_breg30:
4567     case DW_OP_breg31:
4568       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
4569       break;
4570     case DW_OP_regx:
4571       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4572       break;
4573     case DW_OP_fbreg:
4574       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
4575       break;
4576     case DW_OP_bregx:
4577       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4578       dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
4579       break;
4580     case DW_OP_piece:
4581       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4582       break;
4583     case DW_OP_deref_size:
4584     case DW_OP_xderef_size:
4585       dw2_asm_output_data (1, val1->v.val_int, NULL);
4586       break;
4587
4588     case INTERNAL_DW_OP_tls_addr:
4589       if (targetm.asm_out.output_dwarf_dtprel)
4590         {
4591           targetm.asm_out.output_dwarf_dtprel (asm_out_file,
4592                                                DWARF2_ADDR_SIZE,
4593                                                val1->v.val_addr);
4594           fputc ('\n', asm_out_file);
4595         }
4596       else
4597         gcc_unreachable ();
4598       break;
4599
4600     default:
4601       /* Other codes have no operands.  */
4602       break;
4603     }
4604 }
4605
4606 /* Output a sequence of location operations.  */
4607
4608 static void
4609 output_loc_sequence (dw_loc_descr_ref loc)
4610 {
4611   for (; loc != NULL; loc = loc->dw_loc_next)
4612     {
4613       /* Output the opcode.  */
4614       dw2_asm_output_data (1, loc->dw_loc_opc,
4615                            "%s", dwarf_stack_op_name (loc->dw_loc_opc));
4616
4617       /* Output the operand(s) (if any).  */
4618       output_loc_operands (loc);
4619     }
4620 }
4621
4622 /* Output location description stack opcode's operands (if any).
4623    The output is single bytes on a line, suitable for .cfi_escape.  */
4624
4625 static void
4626 output_loc_operands_raw (dw_loc_descr_ref loc)
4627 {
4628   dw_val_ref val1 = &loc->dw_loc_oprnd1;
4629   dw_val_ref val2 = &loc->dw_loc_oprnd2;
4630
4631   switch (loc->dw_loc_opc)
4632     {
4633     case DW_OP_addr:
4634       /* We cannot output addresses in .cfi_escape, only bytes.  */
4635       gcc_unreachable ();
4636
4637     case DW_OP_const1u:
4638     case DW_OP_const1s:
4639     case DW_OP_pick:
4640     case DW_OP_deref_size:
4641     case DW_OP_xderef_size:
4642       fputc (',', asm_out_file);
4643       dw2_asm_output_data_raw (1, val1->v.val_int);
4644       break;
4645
4646     case DW_OP_const2u:
4647     case DW_OP_const2s:
4648       fputc (',', asm_out_file);
4649       dw2_asm_output_data_raw (2, val1->v.val_int);
4650       break;
4651
4652     case DW_OP_const4u:
4653     case DW_OP_const4s:
4654       fputc (',', asm_out_file);
4655       dw2_asm_output_data_raw (4, val1->v.val_int);
4656       break;
4657
4658     case DW_OP_const8u:
4659     case DW_OP_const8s:
4660       gcc_assert (HOST_BITS_PER_LONG >= 64);
4661       fputc (',', asm_out_file);
4662       dw2_asm_output_data_raw (8, val1->v.val_int);
4663       break;
4664
4665     case DW_OP_skip:
4666     case DW_OP_bra:
4667       {
4668         int offset;
4669
4670         gcc_assert (val1->val_class == dw_val_class_loc);
4671         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
4672
4673         fputc (',', asm_out_file);
4674         dw2_asm_output_data_raw (2, offset);
4675       }
4676       break;
4677
4678     case DW_OP_constu:
4679     case DW_OP_plus_uconst:
4680     case DW_OP_regx:
4681     case DW_OP_piece:
4682       fputc (',', asm_out_file);
4683       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
4684       break;
4685
4686     case DW_OP_consts:
4687     case DW_OP_breg0:
4688     case DW_OP_breg1:
4689     case DW_OP_breg2:
4690     case DW_OP_breg3:
4691     case DW_OP_breg4:
4692     case DW_OP_breg5:
4693     case DW_OP_breg6:
4694     case DW_OP_breg7:
4695     case DW_OP_breg8:
4696     case DW_OP_breg9:
4697     case DW_OP_breg10:
4698     case DW_OP_breg11:
4699     case DW_OP_breg12:
4700     case DW_OP_breg13:
4701     case DW_OP_breg14:
4702     case DW_OP_breg15:
4703     case DW_OP_breg16:
4704     case DW_OP_breg17:
4705     case DW_OP_breg18:
4706     case DW_OP_breg19:
4707     case DW_OP_breg20:
4708     case DW_OP_breg21:
4709     case DW_OP_breg22:
4710     case DW_OP_breg23:
4711     case DW_OP_breg24:
4712     case DW_OP_breg25:
4713     case DW_OP_breg26:
4714     case DW_OP_breg27:
4715     case DW_OP_breg28:
4716     case DW_OP_breg29:
4717     case DW_OP_breg30:
4718     case DW_OP_breg31:
4719     case DW_OP_fbreg:
4720       fputc (',', asm_out_file);
4721       dw2_asm_output_data_sleb128_raw (val1->v.val_int);
4722       break;
4723
4724     case DW_OP_bregx:
4725       fputc (',', asm_out_file);
4726       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
4727       fputc (',', asm_out_file);
4728       dw2_asm_output_data_sleb128_raw (val2->v.val_int);
4729       break;
4730
4731     case INTERNAL_DW_OP_tls_addr:
4732       gcc_unreachable ();
4733
4734     default:
4735       /* Other codes have no operands.  */
4736       break;
4737     }
4738 }
4739
4740 static void
4741 output_loc_sequence_raw (dw_loc_descr_ref loc)
4742 {
4743   while (1)
4744     {
4745       /* Output the opcode.  */
4746       fprintf (asm_out_file, "0x%x", loc->dw_loc_opc);
4747       output_loc_operands_raw (loc);
4748
4749       if (!loc->dw_loc_next)
4750         break;
4751       loc = loc->dw_loc_next;
4752
4753       fputc (',', asm_out_file);
4754     }
4755 }
4756
4757 /* This routine will generate the correct assembly data for a location
4758    description based on a cfi entry with a complex address.  */
4759
4760 static void
4761 output_cfa_loc (dw_cfi_ref cfi)
4762 {
4763   dw_loc_descr_ref loc;
4764   unsigned long size;
4765
4766   if (cfi->dw_cfi_opc == DW_CFA_expression)
4767     dw2_asm_output_data (1, cfi->dw_cfi_oprnd2.dw_cfi_reg_num, NULL);
4768
4769   /* Output the size of the block.  */
4770   loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
4771   size = size_of_locs (loc);
4772   dw2_asm_output_data_uleb128 (size, NULL);
4773
4774   /* Now output the operations themselves.  */
4775   output_loc_sequence (loc);
4776 }
4777
4778 /* Similar, but used for .cfi_escape.  */
4779
4780 static void
4781 output_cfa_loc_raw (dw_cfi_ref cfi)
4782 {
4783   dw_loc_descr_ref loc;
4784   unsigned long size;
4785
4786   if (cfi->dw_cfi_opc == DW_CFA_expression)
4787     fprintf (asm_out_file, "0x%x,", cfi->dw_cfi_oprnd2.dw_cfi_reg_num);
4788
4789   /* Output the size of the block.  */
4790   loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
4791   size = size_of_locs (loc);
4792   dw2_asm_output_data_uleb128_raw (size);
4793   fputc (',', asm_out_file);
4794
4795   /* Now output the operations themselves.  */
4796   output_loc_sequence_raw (loc);
4797 }
4798
4799 /* This function builds a dwarf location descriptor sequence from a
4800    dw_cfa_location, adding the given OFFSET to the result of the
4801    expression.  */
4802
4803 static struct dw_loc_descr_struct *
4804 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
4805 {
4806   struct dw_loc_descr_struct *head, *tmp;
4807
4808   offset += cfa->offset;
4809
4810   if (cfa->indirect)
4811     {
4812       head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
4813       head->dw_loc_oprnd1.val_class = dw_val_class_const;
4814       tmp = new_loc_descr (DW_OP_deref, 0, 0);
4815       add_loc_descr (&head, tmp);
4816       if (offset != 0)
4817         {
4818           tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
4819           add_loc_descr (&head, tmp);
4820         }
4821     }
4822   else
4823     head = new_reg_loc_descr (cfa->reg, offset);
4824
4825   return head;
4826 }
4827
4828 /* This function builds a dwarf location descriptor sequence for
4829    the address at OFFSET from the CFA when stack is aligned to
4830    ALIGNMENT byte.  */
4831
4832 static struct dw_loc_descr_struct *
4833 build_cfa_aligned_loc (HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
4834 {
4835   struct dw_loc_descr_struct *head;
4836   unsigned int dwarf_fp
4837     = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
4838
4839  /* When CFA is defined as FP+OFFSET, emulate stack alignment.  */
4840   if (cfa.reg == HARD_FRAME_POINTER_REGNUM && cfa.indirect == 0)
4841     {
4842       head = new_reg_loc_descr (dwarf_fp, 0);
4843       add_loc_descr (&head, int_loc_descriptor (alignment));
4844       add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
4845       loc_descr_plus_const (&head, offset);
4846     }
4847   else
4848     head = new_reg_loc_descr (dwarf_fp, offset);
4849   return head;
4850 }
4851
4852 /* This function fills in aa dw_cfa_location structure from a dwarf location
4853    descriptor sequence.  */
4854
4855 static void
4856 get_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_struct *loc)
4857 {
4858   struct dw_loc_descr_struct *ptr;
4859   cfa->offset = 0;
4860   cfa->base_offset = 0;
4861   cfa->indirect = 0;
4862   cfa->reg = -1;
4863
4864   for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
4865     {
4866       enum dwarf_location_atom op = ptr->dw_loc_opc;
4867
4868       switch (op)
4869         {
4870         case DW_OP_reg0:
4871         case DW_OP_reg1:
4872         case DW_OP_reg2:
4873         case DW_OP_reg3:
4874         case DW_OP_reg4:
4875         case DW_OP_reg5:
4876         case DW_OP_reg6:
4877         case DW_OP_reg7:
4878         case DW_OP_reg8:
4879         case DW_OP_reg9:
4880         case DW_OP_reg10:
4881         case DW_OP_reg11:
4882         case DW_OP_reg12:
4883         case DW_OP_reg13:
4884         case DW_OP_reg14:
4885         case DW_OP_reg15:
4886         case DW_OP_reg16:
4887         case DW_OP_reg17:
4888         case DW_OP_reg18:
4889         case DW_OP_reg19:
4890         case DW_OP_reg20:
4891         case DW_OP_reg21:
4892         case DW_OP_reg22:
4893         case DW_OP_reg23:
4894         case DW_OP_reg24:
4895         case DW_OP_reg25:
4896         case DW_OP_reg26:
4897         case DW_OP_reg27:
4898         case DW_OP_reg28:
4899         case DW_OP_reg29:
4900         case DW_OP_reg30:
4901         case DW_OP_reg31:
4902           cfa->reg = op - DW_OP_reg0;
4903           break;
4904         case DW_OP_regx:
4905           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
4906           break;
4907         case DW_OP_breg0:
4908         case DW_OP_breg1:
4909         case DW_OP_breg2:
4910         case DW_OP_breg3:
4911         case DW_OP_breg4:
4912         case DW_OP_breg5:
4913         case DW_OP_breg6:
4914         case DW_OP_breg7:
4915         case DW_OP_breg8:
4916         case DW_OP_breg9:
4917         case DW_OP_breg10:
4918         case DW_OP_breg11:
4919         case DW_OP_breg12:
4920         case DW_OP_breg13:
4921         case DW_OP_breg14:
4922         case DW_OP_breg15:
4923         case DW_OP_breg16:
4924         case DW_OP_breg17:
4925         case DW_OP_breg18:
4926         case DW_OP_breg19:
4927         case DW_OP_breg20:
4928         case DW_OP_breg21:
4929         case DW_OP_breg22:
4930         case DW_OP_breg23:
4931         case DW_OP_breg24:
4932         case DW_OP_breg25:
4933         case DW_OP_breg26:
4934         case DW_OP_breg27:
4935         case DW_OP_breg28:
4936         case DW_OP_breg29:
4937         case DW_OP_breg30:
4938         case DW_OP_breg31:
4939           cfa->reg = op - DW_OP_breg0;
4940           cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
4941           break;
4942         case DW_OP_bregx:
4943           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
4944           cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
4945           break;
4946         case DW_OP_deref:
4947           cfa->indirect = 1;
4948           break;
4949         case DW_OP_plus_uconst:
4950           cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
4951           break;
4952         default:
4953           internal_error ("DW_LOC_OP %s not implemented",
4954                           dwarf_stack_op_name (ptr->dw_loc_opc));
4955         }
4956     }
4957 }
4958 #endif /* .debug_frame support */
4959 \f
4960 /* And now, the support for symbolic debugging information.  */
4961 #ifdef DWARF2_DEBUGGING_INFO
4962
4963 /* .debug_str support.  */
4964 static int output_indirect_string (void **, void *);
4965
4966 static void dwarf2out_init (const char *);
4967 static void dwarf2out_finish (const char *);
4968 static void dwarf2out_define (unsigned int, const char *);
4969 static void dwarf2out_undef (unsigned int, const char *);
4970 static void dwarf2out_start_source_file (unsigned, const char *);
4971 static void dwarf2out_end_source_file (unsigned);
4972 static void dwarf2out_begin_block (unsigned, unsigned);
4973 static void dwarf2out_end_block (unsigned, unsigned);
4974 static bool dwarf2out_ignore_block (const_tree);
4975 static void dwarf2out_global_decl (tree);
4976 static void dwarf2out_type_decl (tree, int);
4977 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
4978 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
4979                                                  dw_die_ref);
4980 static void dwarf2out_abstract_function (tree);
4981 static void dwarf2out_var_location (rtx);
4982 static void dwarf2out_begin_function (tree);
4983 static void dwarf2out_set_name (tree, tree);
4984
4985 /* The debug hooks structure.  */
4986
4987 const struct gcc_debug_hooks dwarf2_debug_hooks =
4988 {
4989   dwarf2out_init,
4990   dwarf2out_finish,
4991   dwarf2out_define,
4992   dwarf2out_undef,
4993   dwarf2out_start_source_file,
4994   dwarf2out_end_source_file,
4995   dwarf2out_begin_block,
4996   dwarf2out_end_block,
4997   dwarf2out_ignore_block,
4998   dwarf2out_source_line,
4999   dwarf2out_begin_prologue,
5000   debug_nothing_int_charstar,   /* end_prologue */
5001   dwarf2out_end_epilogue,
5002   dwarf2out_begin_function,
5003   debug_nothing_int,            /* end_function */
5004   dwarf2out_decl,               /* function_decl */
5005   dwarf2out_global_decl,
5006   dwarf2out_type_decl,          /* type_decl */
5007   dwarf2out_imported_module_or_decl,
5008   debug_nothing_tree,           /* deferred_inline_function */
5009   /* The DWARF 2 backend tries to reduce debugging bloat by not
5010      emitting the abstract description of inline functions until
5011      something tries to reference them.  */
5012   dwarf2out_abstract_function,  /* outlining_inline_function */
5013   debug_nothing_rtx,            /* label */
5014   debug_nothing_int,            /* handle_pch */
5015   dwarf2out_var_location,
5016   dwarf2out_switch_text_section,
5017   dwarf2out_set_name,
5018   1                             /* start_end_main_source_file */
5019 };
5020 #endif
5021 \f
5022 /* NOTE: In the comments in this file, many references are made to
5023    "Debugging Information Entries".  This term is abbreviated as `DIE'
5024    throughout the remainder of this file.  */
5025
5026 /* An internal representation of the DWARF output is built, and then
5027    walked to generate the DWARF debugging info.  The walk of the internal
5028    representation is done after the entire program has been compiled.
5029    The types below are used to describe the internal representation.  */
5030
5031 /* Various DIE's use offsets relative to the beginning of the
5032    .debug_info section to refer to each other.  */
5033
5034 typedef long int dw_offset;
5035
5036 /* Define typedefs here to avoid circular dependencies.  */
5037
5038 typedef struct dw_attr_struct *dw_attr_ref;
5039 typedef struct dw_line_info_struct *dw_line_info_ref;
5040 typedef struct dw_separate_line_info_struct *dw_separate_line_info_ref;
5041 typedef struct pubname_struct *pubname_ref;
5042 typedef struct dw_ranges_struct *dw_ranges_ref;
5043 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
5044
5045 /* Each entry in the line_info_table maintains the file and
5046    line number associated with the label generated for that
5047    entry.  The label gives the PC value associated with
5048    the line number entry.  */
5049
5050 typedef struct GTY(()) dw_line_info_struct {
5051   unsigned long dw_file_num;
5052   unsigned long dw_line_num;
5053 }
5054 dw_line_info_entry;
5055
5056 /* Line information for functions in separate sections; each one gets its
5057    own sequence.  */
5058 typedef struct GTY(()) dw_separate_line_info_struct {
5059   unsigned long dw_file_num;
5060   unsigned long dw_line_num;
5061   unsigned long function;
5062 }
5063 dw_separate_line_info_entry;
5064
5065 /* Each DIE attribute has a field specifying the attribute kind,
5066    a link to the next attribute in the chain, and an attribute value.
5067    Attributes are typically linked below the DIE they modify.  */
5068
5069 typedef struct GTY(()) dw_attr_struct {
5070   enum dwarf_attribute dw_attr;
5071   dw_val_node dw_attr_val;
5072 }
5073 dw_attr_node;
5074
5075 DEF_VEC_O(dw_attr_node);
5076 DEF_VEC_ALLOC_O(dw_attr_node,gc);
5077
5078 /* The Debugging Information Entry (DIE) structure.  DIEs form a tree.
5079    The children of each node form a circular list linked by
5080    die_sib.  die_child points to the node *before* the "first" child node.  */
5081
5082 typedef struct GTY((chain_circular ("%h.die_sib"))) die_struct {
5083   enum dwarf_tag die_tag;
5084   char *die_symbol;
5085   VEC(dw_attr_node,gc) * die_attr;
5086   dw_die_ref die_parent;
5087   dw_die_ref die_child;
5088   dw_die_ref die_sib;
5089   dw_die_ref die_definition; /* ref from a specification to its definition */
5090   dw_offset die_offset;
5091   unsigned long die_abbrev;
5092   int die_mark;
5093   /* Die is used and must not be pruned as unused.  */
5094   int die_perennial_p;
5095   unsigned int decl_id;
5096 }
5097 die_node;
5098
5099 /* Evaluate 'expr' while 'c' is set to each child of DIE in order.  */
5100 #define FOR_EACH_CHILD(die, c, expr) do {       \
5101   c = die->die_child;                           \
5102   if (c) do {                                   \
5103     c = c->die_sib;                             \
5104     expr;                                       \
5105   } while (c != die->die_child);                \
5106 } while (0)
5107
5108 /* The pubname structure */
5109
5110 typedef struct GTY(()) pubname_struct {
5111   dw_die_ref die;
5112   const char *name;
5113 }
5114 pubname_entry;
5115
5116 DEF_VEC_O(pubname_entry);
5117 DEF_VEC_ALLOC_O(pubname_entry, gc);
5118
5119 struct GTY(()) dw_ranges_struct {
5120   /* If this is positive, it's a block number, otherwise it's a
5121      bitwise-negated index into dw_ranges_by_label.  */
5122   int num;
5123 };
5124
5125 struct GTY(()) dw_ranges_by_label_struct {
5126   const char *begin;
5127   const char *end;
5128 };
5129
5130 /* The limbo die list structure.  */
5131 typedef struct GTY(()) limbo_die_struct {
5132   dw_die_ref die;
5133   tree created_for;
5134   struct limbo_die_struct *next;
5135 }
5136 limbo_die_node;
5137
5138 /* How to start an assembler comment.  */
5139 #ifndef ASM_COMMENT_START
5140 #define ASM_COMMENT_START ";#"
5141 #endif
5142
5143 /* Define a macro which returns nonzero for a TYPE_DECL which was
5144    implicitly generated for a tagged type.
5145
5146    Note that unlike the gcc front end (which generates a NULL named
5147    TYPE_DECL node for each complete tagged type, each array type, and
5148    each function type node created) the g++ front end generates a
5149    _named_ TYPE_DECL node for each tagged type node created.
5150    These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
5151    generate a DW_TAG_typedef DIE for them.  */
5152
5153 #define TYPE_DECL_IS_STUB(decl)                         \
5154   (DECL_NAME (decl) == NULL_TREE                        \
5155    || (DECL_ARTIFICIAL (decl)                           \
5156        && is_tagged_type (TREE_TYPE (decl))             \
5157        && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl)))  \
5158            /* This is necessary for stub decls that     \
5159               appear in nested inline functions.  */    \
5160            || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
5161                && (decl_ultimate_origin (decl)          \
5162                    == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
5163
5164 /* Information concerning the compilation unit's programming
5165    language, and compiler version.  */
5166
5167 /* Fixed size portion of the DWARF compilation unit header.  */
5168 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
5169   (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
5170
5171 /* Fixed size portion of public names info.  */
5172 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
5173
5174 /* Fixed size portion of the address range info.  */
5175 #define DWARF_ARANGES_HEADER_SIZE                                       \
5176   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4,      \
5177                 DWARF2_ADDR_SIZE * 2)                                   \
5178    - DWARF_INITIAL_LENGTH_SIZE)
5179
5180 /* Size of padding portion in the address range info.  It must be
5181    aligned to twice the pointer size.  */
5182 #define DWARF_ARANGES_PAD_SIZE \
5183   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
5184                 DWARF2_ADDR_SIZE * 2)                              \
5185    - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
5186
5187 /* Use assembler line directives if available.  */
5188 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
5189 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
5190 #define DWARF2_ASM_LINE_DEBUG_INFO 1
5191 #else
5192 #define DWARF2_ASM_LINE_DEBUG_INFO 0
5193 #endif
5194 #endif
5195
5196 /* Minimum line offset in a special line info. opcode.
5197    This value was chosen to give a reasonable range of values.  */
5198 #define DWARF_LINE_BASE  -10
5199
5200 /* First special line opcode - leave room for the standard opcodes.  */
5201 #define DWARF_LINE_OPCODE_BASE  10
5202
5203 /* Range of line offsets in a special line info. opcode.  */
5204 #define DWARF_LINE_RANGE  (254-DWARF_LINE_OPCODE_BASE+1)
5205
5206 /* Flag that indicates the initial value of the is_stmt_start flag.
5207    In the present implementation, we do not mark any lines as
5208    the beginning of a source statement, because that information
5209    is not made available by the GCC front-end.  */
5210 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
5211
5212 #ifdef DWARF2_DEBUGGING_INFO
5213 /* This location is used by calc_die_sizes() to keep track
5214    the offset of each DIE within the .debug_info section.  */
5215 static unsigned long next_die_offset;
5216 #endif
5217
5218 /* Record the root of the DIE's built for the current compilation unit.  */
5219 static GTY(()) dw_die_ref comp_unit_die;
5220
5221 /* A list of DIEs with a NULL parent waiting to be relocated.  */
5222 static GTY(()) limbo_die_node *limbo_die_list;
5223
5224 /* A list of DIEs for which we may have to generate
5225    DW_AT_MIPS_linkage_name once their DECL_ASSEMBLER_NAMEs are
5226    set.  */
5227 static GTY(()) limbo_die_node *deferred_asm_name;
5228
5229 /* Filenames referenced by this compilation unit.  */
5230 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
5231
5232 /* A hash table of references to DIE's that describe declarations.
5233    The key is a DECL_UID() which is a unique number identifying each decl.  */
5234 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
5235
5236 /* A hash table of references to DIE's that describe COMMON blocks.
5237    The key is DECL_UID() ^ die_parent.  */
5238 static GTY ((param_is (struct die_struct))) htab_t common_block_die_table;
5239
5240 /* Node of the variable location list.  */
5241 struct GTY ((chain_next ("%h.next"))) var_loc_node {
5242   rtx GTY (()) var_loc_note;
5243   const char * GTY (()) label;
5244   const char * GTY (()) section_label;
5245   struct var_loc_node * GTY (()) next;
5246 };
5247
5248 /* Variable location list.  */
5249 struct GTY (()) var_loc_list_def {
5250   struct var_loc_node * GTY (()) first;
5251
5252   /* Do not mark the last element of the chained list because
5253      it is marked through the chain.  */
5254   struct var_loc_node * GTY ((skip ("%h"))) last;
5255
5256   /* DECL_UID of the variable decl.  */
5257   unsigned int decl_id;
5258 };
5259 typedef struct var_loc_list_def var_loc_list;
5260
5261
5262 /* Table of decl location linked lists.  */
5263 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
5264
5265 /* A pointer to the base of a list of references to DIE's that
5266    are uniquely identified by their tag, presence/absence of
5267    children DIE's, and list of attribute/value pairs.  */
5268 static GTY((length ("abbrev_die_table_allocated")))
5269   dw_die_ref *abbrev_die_table;
5270
5271 /* Number of elements currently allocated for abbrev_die_table.  */
5272 static GTY(()) unsigned abbrev_die_table_allocated;
5273
5274 /* Number of elements in type_die_table currently in use.  */
5275 static GTY(()) unsigned abbrev_die_table_in_use;
5276
5277 /* Size (in elements) of increments by which we may expand the
5278    abbrev_die_table.  */
5279 #define ABBREV_DIE_TABLE_INCREMENT 256
5280
5281 /* A pointer to the base of a table that contains line information
5282    for each source code line in .text in the compilation unit.  */
5283 static GTY((length ("line_info_table_allocated")))
5284      dw_line_info_ref line_info_table;
5285
5286 /* Number of elements currently allocated for line_info_table.  */
5287 static GTY(()) unsigned line_info_table_allocated;
5288
5289 /* Number of elements in line_info_table currently in use.  */
5290 static GTY(()) unsigned line_info_table_in_use;
5291
5292 /* A pointer to the base of a table that contains line information
5293    for each source code line outside of .text in the compilation unit.  */
5294 static GTY ((length ("separate_line_info_table_allocated")))
5295      dw_separate_line_info_ref separate_line_info_table;
5296
5297 /* Number of elements currently allocated for separate_line_info_table.  */
5298 static GTY(()) unsigned separate_line_info_table_allocated;
5299
5300 /* Number of elements in separate_line_info_table currently in use.  */
5301 static GTY(()) unsigned separate_line_info_table_in_use;
5302
5303 /* Size (in elements) of increments by which we may expand the
5304    line_info_table.  */
5305 #define LINE_INFO_TABLE_INCREMENT 1024
5306
5307 /* A pointer to the base of a table that contains a list of publicly
5308    accessible names.  */
5309 static GTY (()) VEC (pubname_entry, gc) *  pubname_table;
5310
5311 /* A pointer to the base of a table that contains a list of publicly
5312    accessible types.  */
5313 static GTY (()) VEC (pubname_entry, gc) * pubtype_table;
5314
5315 /* Array of dies for which we should generate .debug_arange info.  */
5316 static GTY((length ("arange_table_allocated"))) dw_die_ref *arange_table;
5317
5318 /* Number of elements currently allocated for arange_table.  */
5319 static GTY(()) unsigned arange_table_allocated;
5320
5321 /* Number of elements in arange_table currently in use.  */
5322 static GTY(()) unsigned arange_table_in_use;
5323
5324 /* Size (in elements) of increments by which we may expand the
5325    arange_table.  */
5326 #define ARANGE_TABLE_INCREMENT 64
5327
5328 /* Array of dies for which we should generate .debug_ranges info.  */
5329 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
5330
5331 /* Number of elements currently allocated for ranges_table.  */
5332 static GTY(()) unsigned ranges_table_allocated;
5333
5334 /* Number of elements in ranges_table currently in use.  */
5335 static GTY(()) unsigned ranges_table_in_use;
5336
5337 /* Array of pairs of labels referenced in ranges_table.  */
5338 static GTY ((length ("ranges_by_label_allocated")))
5339      dw_ranges_by_label_ref ranges_by_label;
5340
5341 /* Number of elements currently allocated for ranges_by_label.  */
5342 static GTY(()) unsigned ranges_by_label_allocated;
5343
5344 /* Number of elements in ranges_by_label currently in use.  */
5345 static GTY(()) unsigned ranges_by_label_in_use;
5346
5347 /* Size (in elements) of increments by which we may expand the
5348    ranges_table.  */
5349 #define RANGES_TABLE_INCREMENT 64
5350
5351 /* Whether we have location lists that need outputting */
5352 static GTY(()) bool have_location_lists;
5353
5354 /* Unique label counter.  */
5355 static GTY(()) unsigned int loclabel_num;
5356
5357 #ifdef DWARF2_DEBUGGING_INFO
5358 /* Record whether the function being analyzed contains inlined functions.  */
5359 static int current_function_has_inlines;
5360 #endif
5361 #if 0 && defined (MIPS_DEBUGGING_INFO)
5362 static int comp_unit_has_inlines;
5363 #endif
5364
5365 /* The last file entry emitted by maybe_emit_file().  */
5366 static GTY(()) struct dwarf_file_data * last_emitted_file;
5367
5368 /* Number of internal labels generated by gen_internal_sym().  */
5369 static GTY(()) int label_num;
5370
5371 /* Cached result of previous call to lookup_filename.  */
5372 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
5373
5374 #ifdef DWARF2_DEBUGGING_INFO
5375
5376 /* Offset from the "steady-state frame pointer" to the frame base,
5377    within the current function.  */
5378 static HOST_WIDE_INT frame_pointer_fb_offset;
5379
5380 /* Forward declarations for functions defined in this file.  */
5381
5382 static int is_pseudo_reg (const_rtx);
5383 static tree type_main_variant (tree);
5384 static int is_tagged_type (const_tree);
5385 static const char *dwarf_tag_name (unsigned);
5386 static const char *dwarf_attr_name (unsigned);
5387 static const char *dwarf_form_name (unsigned);
5388 static tree decl_ultimate_origin (const_tree);
5389 static tree decl_class_context (tree);
5390 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
5391 static inline enum dw_val_class AT_class (dw_attr_ref);
5392 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
5393 static inline unsigned AT_flag (dw_attr_ref);
5394 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
5395 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
5396 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
5397 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
5398 static void add_AT_long_long (dw_die_ref, enum dwarf_attribute, unsigned long,
5399                               unsigned long);
5400 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
5401                                unsigned int, unsigned char *);
5402 static hashval_t debug_str_do_hash (const void *);
5403 static int debug_str_eq (const void *, const void *);
5404 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
5405 static inline const char *AT_string (dw_attr_ref);
5406 static enum dwarf_form AT_string_form (dw_attr_ref);
5407 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
5408 static void add_AT_specification (dw_die_ref, dw_die_ref);
5409 static inline dw_die_ref AT_ref (dw_attr_ref);
5410 static inline int AT_ref_external (dw_attr_ref);
5411 static inline void set_AT_ref_external (dw_attr_ref, int);
5412 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
5413 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
5414 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
5415 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
5416                              dw_loc_list_ref);
5417 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
5418 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
5419 static inline rtx AT_addr (dw_attr_ref);
5420 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
5421 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
5422 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
5423 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
5424                            unsigned HOST_WIDE_INT);
5425 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
5426                                unsigned long);
5427 static inline const char *AT_lbl (dw_attr_ref);
5428 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
5429 static const char *get_AT_low_pc (dw_die_ref);
5430 static const char *get_AT_hi_pc (dw_die_ref);
5431 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
5432 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
5433 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
5434 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
5435 static bool is_c_family (void);
5436 static bool is_cxx (void);
5437 static bool is_java (void);
5438 static bool is_fortran (void);
5439 static bool is_ada (void);
5440 static void remove_AT (dw_die_ref, enum dwarf_attribute);
5441 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
5442 static void add_child_die (dw_die_ref, dw_die_ref);
5443 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
5444 static dw_die_ref lookup_type_die (tree);
5445 static void equate_type_number_to_die (tree, dw_die_ref);
5446 static hashval_t decl_die_table_hash (const void *);
5447 static int decl_die_table_eq (const void *, const void *);
5448 static dw_die_ref lookup_decl_die (tree);
5449 static hashval_t common_block_die_table_hash (const void *);
5450 static int common_block_die_table_eq (const void *, const void *);
5451 static hashval_t decl_loc_table_hash (const void *);
5452 static int decl_loc_table_eq (const void *, const void *);
5453 static var_loc_list *lookup_decl_loc (const_tree);
5454 static void equate_decl_number_to_die (tree, dw_die_ref);
5455 static void add_var_loc_to_decl (tree, struct var_loc_node *);
5456 static void print_spaces (FILE *);
5457 static void print_die (dw_die_ref, FILE *);
5458 static void print_dwarf_line_table (FILE *);
5459 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
5460 static dw_die_ref pop_compile_unit (dw_die_ref);
5461 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
5462 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
5463 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
5464 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
5465 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
5466 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
5467 static int same_die_p (dw_die_ref, dw_die_ref, int *);
5468 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
5469 static void compute_section_prefix (dw_die_ref);
5470 static int is_type_die (dw_die_ref);
5471 static int is_comdat_die (dw_die_ref);
5472 static int is_symbol_die (dw_die_ref);
5473 static void assign_symbol_names (dw_die_ref);
5474 static void break_out_includes (dw_die_ref);
5475 static hashval_t htab_cu_hash (const void *);
5476 static int htab_cu_eq (const void *, const void *);
5477 static void htab_cu_del (void *);
5478 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
5479 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
5480 static void add_sibling_attributes (dw_die_ref);
5481 static void build_abbrev_table (dw_die_ref);
5482 static void output_location_lists (dw_die_ref);
5483 static int constant_size (unsigned HOST_WIDE_INT);
5484 static unsigned long size_of_die (dw_die_ref);
5485 static void calc_die_sizes (dw_die_ref);
5486 static void mark_dies (dw_die_ref);
5487 static void unmark_dies (dw_die_ref);
5488 static void unmark_all_dies (dw_die_ref);
5489 static unsigned long size_of_pubnames (VEC (pubname_entry,gc) *);
5490 static unsigned long size_of_aranges (void);
5491 static enum dwarf_form value_format (dw_attr_ref);
5492 static void output_value_format (dw_attr_ref);
5493 static void output_abbrev_section (void);
5494 static void output_die_symbol (dw_die_ref);
5495 static void output_die (dw_die_ref);
5496 static void output_compilation_unit_header (void);
5497 static void output_comp_unit (dw_die_ref, int);
5498 static const char *dwarf2_name (tree, int);
5499 static void add_pubname (tree, dw_die_ref);
5500 static void add_pubname_string (const char *, dw_die_ref);
5501 static void add_pubtype (tree, dw_die_ref);
5502 static void output_pubnames (VEC (pubname_entry,gc) *);
5503 static void add_arange (tree, dw_die_ref);
5504 static void output_aranges (void);
5505 static unsigned int add_ranges_num (int);
5506 static unsigned int add_ranges (const_tree);
5507 static unsigned int add_ranges_by_labels (const char *, const char *);
5508 static void output_ranges (void);
5509 static void output_line_info (void);
5510 static void output_file_names (void);
5511 static dw_die_ref base_type_die (tree);
5512 static int is_base_type (tree);
5513 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
5514 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
5515 static int type_is_enum (const_tree);
5516 static unsigned int dbx_reg_number (const_rtx);
5517 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
5518 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
5519 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
5520                                                 enum var_init_status);
5521 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
5522                                                      enum var_init_status);
5523 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
5524                                          enum var_init_status);
5525 static int is_based_loc (const_rtx);
5526 static dw_loc_descr_ref mem_loc_descriptor (rtx, enum machine_mode mode,
5527                                             enum var_init_status);
5528 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
5529                                                enum var_init_status);
5530 static dw_loc_descr_ref loc_descriptor (rtx, enum var_init_status);
5531 static dw_loc_descr_ref loc_descriptor_from_tree_1 (tree, int);
5532 static dw_loc_descr_ref loc_descriptor_from_tree (tree);
5533 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
5534 static tree field_type (const_tree);
5535 static unsigned int simple_type_align_in_bits (const_tree);
5536 static unsigned int simple_decl_align_in_bits (const_tree);
5537 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
5538 static HOST_WIDE_INT field_byte_offset (const_tree);
5539 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
5540                                          dw_loc_descr_ref);
5541 static void add_data_member_location_attribute (dw_die_ref, tree);
5542 static void add_const_value_attribute (dw_die_ref, rtx);
5543 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
5544 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
5545 static void insert_float (const_rtx, unsigned char *);
5546 static rtx rtl_for_decl_location (tree);
5547 static void add_location_or_const_value_attribute (dw_die_ref, tree,
5548                                                    enum dwarf_attribute);
5549 static void tree_add_const_value_attribute (dw_die_ref, tree);
5550 static void add_name_attribute (dw_die_ref, const char *);
5551 static void add_comp_dir_attribute (dw_die_ref);
5552 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
5553 static void add_subscript_info (dw_die_ref, tree, bool);
5554 static void add_byte_size_attribute (dw_die_ref, tree);
5555 static void add_bit_offset_attribute (dw_die_ref, tree);
5556 static void add_bit_size_attribute (dw_die_ref, tree);
5557 static void add_prototyped_attribute (dw_die_ref, tree);
5558 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
5559 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
5560 static void add_src_coords_attributes (dw_die_ref, tree);
5561 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
5562 static void push_decl_scope (tree);
5563 static void pop_decl_scope (void);
5564 static dw_die_ref scope_die_for (tree, dw_die_ref);
5565 static inline int local_scope_p (dw_die_ref);
5566 static inline int class_scope_p (dw_die_ref);
5567 static inline int class_or_namespace_scope_p (dw_die_ref);
5568 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
5569 static void add_calling_convention_attribute (dw_die_ref, tree);
5570 static const char *type_tag (const_tree);
5571 static tree member_declared_type (const_tree);
5572 #if 0
5573 static const char *decl_start_label (tree);
5574 #endif
5575 static void gen_array_type_die (tree, dw_die_ref);
5576 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
5577 #if 0
5578 static void gen_entry_point_die (tree, dw_die_ref);
5579 #endif
5580 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
5581 static dw_die_ref gen_formal_parameter_die (tree, tree, dw_die_ref);
5582 static void gen_unspecified_parameters_die (tree, dw_die_ref);
5583 static void gen_formal_types_die (tree, dw_die_ref);
5584 static void gen_subprogram_die (tree, dw_die_ref);
5585 static void gen_variable_die (tree, tree, dw_die_ref);
5586 static void gen_const_die (tree, dw_die_ref);
5587 static void gen_label_die (tree, dw_die_ref);
5588 static void gen_lexical_block_die (tree, dw_die_ref, int);
5589 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
5590 static void gen_field_die (tree, dw_die_ref);
5591 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
5592 static dw_die_ref gen_compile_unit_die (const char *);
5593 static void gen_inheritance_die (tree, tree, dw_die_ref);
5594 static void gen_member_die (tree, dw_die_ref);
5595 static void gen_struct_or_union_type_die (tree, dw_die_ref,
5596                                                 enum debug_info_usage);
5597 static void gen_subroutine_type_die (tree, dw_die_ref);
5598 static void gen_typedef_die (tree, dw_die_ref);
5599 static void gen_type_die (tree, dw_die_ref);
5600 static void gen_block_die (tree, dw_die_ref, int);
5601 static void decls_for_scope (tree, dw_die_ref, int);
5602 static int is_redundant_typedef (const_tree);
5603 static void gen_namespace_die (tree, dw_die_ref);
5604 static void gen_decl_die (tree, tree, dw_die_ref);
5605 static dw_die_ref force_decl_die (tree);
5606 static dw_die_ref force_type_die (tree);
5607 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
5608 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
5609 static struct dwarf_file_data * lookup_filename (const char *);
5610 static void retry_incomplete_types (void);
5611 static void gen_type_die_for_member (tree, tree, dw_die_ref);
5612 static void splice_child_die (dw_die_ref, dw_die_ref);
5613 static int file_info_cmp (const void *, const void *);
5614 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
5615                                      const char *, const char *, unsigned);
5616 static void add_loc_descr_to_loc_list (dw_loc_list_ref *, dw_loc_descr_ref,
5617                                        const char *, const char *,
5618                                        const char *);
5619 static void output_loc_list (dw_loc_list_ref);
5620 static char *gen_internal_sym (const char *);
5621
5622 static void prune_unmark_dies (dw_die_ref);
5623 static void prune_unused_types_mark (dw_die_ref, int);
5624 static void prune_unused_types_walk (dw_die_ref);
5625 static void prune_unused_types_walk_attribs (dw_die_ref);
5626 static void prune_unused_types_prune (dw_die_ref);
5627 static void prune_unused_types (void);
5628 static int maybe_emit_file (struct dwarf_file_data *fd);
5629
5630 /* Section names used to hold DWARF debugging information.  */
5631 #ifndef DEBUG_INFO_SECTION
5632 #define DEBUG_INFO_SECTION      ".debug_info"
5633 #endif
5634 #ifndef DEBUG_ABBREV_SECTION
5635 #define DEBUG_ABBREV_SECTION    ".debug_abbrev"
5636 #endif
5637 #ifndef DEBUG_ARANGES_SECTION
5638 #define DEBUG_ARANGES_SECTION   ".debug_aranges"
5639 #endif
5640 #ifndef DEBUG_MACINFO_SECTION
5641 #define DEBUG_MACINFO_SECTION   ".debug_macinfo"
5642 #endif
5643 #ifndef DEBUG_LINE_SECTION
5644 #define DEBUG_LINE_SECTION      ".debug_line"
5645 #endif
5646 #ifndef DEBUG_LOC_SECTION
5647 #define DEBUG_LOC_SECTION       ".debug_loc"
5648 #endif
5649 #ifndef DEBUG_PUBNAMES_SECTION
5650 #define DEBUG_PUBNAMES_SECTION  ".debug_pubnames"
5651 #endif
5652 #ifndef DEBUG_PUBTYPES_SECTION
5653 #define DEBUG_PUBTYPES_SECTION  ".debug_pubtypes"
5654 #endif
5655 #ifndef DEBUG_STR_SECTION
5656 #define DEBUG_STR_SECTION       ".debug_str"
5657 #endif
5658 #ifndef DEBUG_RANGES_SECTION
5659 #define DEBUG_RANGES_SECTION    ".debug_ranges"
5660 #endif
5661
5662 /* Standard ELF section names for compiled code and data.  */
5663 #ifndef TEXT_SECTION_NAME
5664 #define TEXT_SECTION_NAME       ".text"
5665 #endif
5666
5667 /* Section flags for .debug_str section.  */
5668 #define DEBUG_STR_SECTION_FLAGS \
5669   (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings               \
5670    ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1        \
5671    : SECTION_DEBUG)
5672
5673 /* Labels we insert at beginning sections we can reference instead of
5674    the section names themselves.  */
5675
5676 #ifndef TEXT_SECTION_LABEL
5677 #define TEXT_SECTION_LABEL              "Ltext"
5678 #endif
5679 #ifndef COLD_TEXT_SECTION_LABEL
5680 #define COLD_TEXT_SECTION_LABEL         "Ltext_cold"
5681 #endif
5682 #ifndef DEBUG_LINE_SECTION_LABEL
5683 #define DEBUG_LINE_SECTION_LABEL        "Ldebug_line"
5684 #endif
5685 #ifndef DEBUG_INFO_SECTION_LABEL
5686 #define DEBUG_INFO_SECTION_LABEL        "Ldebug_info"
5687 #endif
5688 #ifndef DEBUG_ABBREV_SECTION_LABEL
5689 #define DEBUG_ABBREV_SECTION_LABEL      "Ldebug_abbrev"
5690 #endif
5691 #ifndef DEBUG_LOC_SECTION_LABEL
5692 #define DEBUG_LOC_SECTION_LABEL         "Ldebug_loc"
5693 #endif
5694 #ifndef DEBUG_RANGES_SECTION_LABEL
5695 #define DEBUG_RANGES_SECTION_LABEL      "Ldebug_ranges"
5696 #endif
5697 #ifndef DEBUG_MACINFO_SECTION_LABEL
5698 #define DEBUG_MACINFO_SECTION_LABEL     "Ldebug_macinfo"
5699 #endif
5700
5701 /* Definitions of defaults for formats and names of various special
5702    (artificial) labels which may be generated within this file (when the -g
5703    options is used and DWARF2_DEBUGGING_INFO is in effect.
5704    If necessary, these may be overridden from within the tm.h file, but
5705    typically, overriding these defaults is unnecessary.  */
5706
5707 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
5708 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5709 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5710 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
5711 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5712 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5713 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5714 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5715 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5716 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
5717
5718 #ifndef TEXT_END_LABEL
5719 #define TEXT_END_LABEL          "Letext"
5720 #endif
5721 #ifndef COLD_END_LABEL
5722 #define COLD_END_LABEL          "Letext_cold"
5723 #endif
5724 #ifndef BLOCK_BEGIN_LABEL
5725 #define BLOCK_BEGIN_LABEL       "LBB"
5726 #endif
5727 #ifndef BLOCK_END_LABEL
5728 #define BLOCK_END_LABEL         "LBE"
5729 #endif
5730 #ifndef LINE_CODE_LABEL
5731 #define LINE_CODE_LABEL         "LM"
5732 #endif
5733 #ifndef SEPARATE_LINE_CODE_LABEL
5734 #define SEPARATE_LINE_CODE_LABEL        "LSM"
5735 #endif
5736
5737 \f
5738 /* We allow a language front-end to designate a function that is to be
5739    called to "demangle" any name before it is put into a DIE.  */
5740
5741 static const char *(*demangle_name_func) (const char *);
5742
5743 void
5744 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
5745 {
5746   demangle_name_func = func;
5747 }
5748
5749 /* Test if rtl node points to a pseudo register.  */
5750
5751 static inline int
5752 is_pseudo_reg (const_rtx rtl)
5753 {
5754   return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
5755           || (GET_CODE (rtl) == SUBREG
5756               && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
5757 }
5758
5759 /* Return a reference to a type, with its const and volatile qualifiers
5760    removed.  */
5761
5762 static inline tree
5763 type_main_variant (tree type)
5764 {
5765   type = TYPE_MAIN_VARIANT (type);
5766
5767   /* ??? There really should be only one main variant among any group of
5768      variants of a given type (and all of the MAIN_VARIANT values for all
5769      members of the group should point to that one type) but sometimes the C
5770      front-end messes this up for array types, so we work around that bug
5771      here.  */
5772   if (TREE_CODE (type) == ARRAY_TYPE)
5773     while (type != TYPE_MAIN_VARIANT (type))
5774       type = TYPE_MAIN_VARIANT (type);
5775
5776   return type;
5777 }
5778
5779 /* Return nonzero if the given type node represents a tagged type.  */
5780
5781 static inline int
5782 is_tagged_type (const_tree type)
5783 {
5784   enum tree_code code = TREE_CODE (type);
5785
5786   return (code == RECORD_TYPE || code == UNION_TYPE
5787           || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
5788 }
5789
5790 /* Convert a DIE tag into its string name.  */
5791
5792 static const char *
5793 dwarf_tag_name (unsigned int tag)
5794 {
5795   switch (tag)
5796     {
5797     case DW_TAG_padding:
5798       return "DW_TAG_padding";
5799     case DW_TAG_array_type:
5800       return "DW_TAG_array_type";
5801     case DW_TAG_class_type:
5802       return "DW_TAG_class_type";
5803     case DW_TAG_entry_point:
5804       return "DW_TAG_entry_point";
5805     case DW_TAG_enumeration_type:
5806       return "DW_TAG_enumeration_type";
5807     case DW_TAG_formal_parameter:
5808       return "DW_TAG_formal_parameter";
5809     case DW_TAG_imported_declaration:
5810       return "DW_TAG_imported_declaration";
5811     case DW_TAG_label:
5812       return "DW_TAG_label";
5813     case DW_TAG_lexical_block:
5814       return "DW_TAG_lexical_block";
5815     case DW_TAG_member:
5816       return "DW_TAG_member";
5817     case DW_TAG_pointer_type:
5818       return "DW_TAG_pointer_type";
5819     case DW_TAG_reference_type:
5820       return "DW_TAG_reference_type";
5821     case DW_TAG_compile_unit:
5822       return "DW_TAG_compile_unit";
5823     case DW_TAG_string_type:
5824       return "DW_TAG_string_type";
5825     case DW_TAG_structure_type:
5826       return "DW_TAG_structure_type";
5827     case DW_TAG_subroutine_type:
5828       return "DW_TAG_subroutine_type";
5829     case DW_TAG_typedef:
5830       return "DW_TAG_typedef";
5831     case DW_TAG_union_type:
5832       return "DW_TAG_union_type";
5833     case DW_TAG_unspecified_parameters:
5834       return "DW_TAG_unspecified_parameters";
5835     case DW_TAG_variant:
5836       return "DW_TAG_variant";
5837     case DW_TAG_common_block:
5838       return "DW_TAG_common_block";
5839     case DW_TAG_common_inclusion:
5840       return "DW_TAG_common_inclusion";
5841     case DW_TAG_inheritance:
5842       return "DW_TAG_inheritance";
5843     case DW_TAG_inlined_subroutine:
5844       return "DW_TAG_inlined_subroutine";
5845     case DW_TAG_module:
5846       return "DW_TAG_module";
5847     case DW_TAG_ptr_to_member_type:
5848       return "DW_TAG_ptr_to_member_type";
5849     case DW_TAG_set_type:
5850       return "DW_TAG_set_type";
5851     case DW_TAG_subrange_type:
5852       return "DW_TAG_subrange_type";
5853     case DW_TAG_with_stmt:
5854       return "DW_TAG_with_stmt";
5855     case DW_TAG_access_declaration:
5856       return "DW_TAG_access_declaration";
5857     case DW_TAG_base_type:
5858       return "DW_TAG_base_type";
5859     case DW_TAG_catch_block:
5860       return "DW_TAG_catch_block";
5861     case DW_TAG_const_type:
5862       return "DW_TAG_const_type";
5863     case DW_TAG_constant:
5864       return "DW_TAG_constant";
5865     case DW_TAG_enumerator:
5866       return "DW_TAG_enumerator";
5867     case DW_TAG_file_type:
5868       return "DW_TAG_file_type";
5869     case DW_TAG_friend:
5870       return "DW_TAG_friend";
5871     case DW_TAG_namelist:
5872       return "DW_TAG_namelist";
5873     case DW_TAG_namelist_item:
5874       return "DW_TAG_namelist_item";
5875     case DW_TAG_packed_type:
5876       return "DW_TAG_packed_type";
5877     case DW_TAG_subprogram:
5878       return "DW_TAG_subprogram";
5879     case DW_TAG_template_type_param:
5880       return "DW_TAG_template_type_param";
5881     case DW_TAG_template_value_param:
5882       return "DW_TAG_template_value_param";
5883     case DW_TAG_thrown_type:
5884       return "DW_TAG_thrown_type";
5885     case DW_TAG_try_block:
5886       return "DW_TAG_try_block";
5887     case DW_TAG_variant_part:
5888       return "DW_TAG_variant_part";
5889     case DW_TAG_variable:
5890       return "DW_TAG_variable";
5891     case DW_TAG_volatile_type:
5892       return "DW_TAG_volatile_type";
5893     case DW_TAG_dwarf_procedure:
5894       return "DW_TAG_dwarf_procedure";
5895     case DW_TAG_restrict_type:
5896       return "DW_TAG_restrict_type";
5897     case DW_TAG_interface_type:
5898       return "DW_TAG_interface_type";
5899     case DW_TAG_namespace:
5900       return "DW_TAG_namespace";
5901     case DW_TAG_imported_module:
5902       return "DW_TAG_imported_module";
5903     case DW_TAG_unspecified_type:
5904       return "DW_TAG_unspecified_type";
5905     case DW_TAG_partial_unit:
5906       return "DW_TAG_partial_unit";
5907     case DW_TAG_imported_unit:
5908       return "DW_TAG_imported_unit";
5909     case DW_TAG_condition:
5910       return "DW_TAG_condition";
5911     case DW_TAG_shared_type:
5912       return "DW_TAG_shared_type";
5913     case DW_TAG_MIPS_loop:
5914       return "DW_TAG_MIPS_loop";
5915     case DW_TAG_format_label:
5916       return "DW_TAG_format_label";
5917     case DW_TAG_function_template:
5918       return "DW_TAG_function_template";
5919     case DW_TAG_class_template:
5920       return "DW_TAG_class_template";
5921     case DW_TAG_GNU_BINCL:
5922       return "DW_TAG_GNU_BINCL";
5923     case DW_TAG_GNU_EINCL:
5924       return "DW_TAG_GNU_EINCL";
5925     default:
5926       return "DW_TAG_<unknown>";
5927     }
5928 }
5929
5930 /* Convert a DWARF attribute code into its string name.  */
5931
5932 static const char *
5933 dwarf_attr_name (unsigned int attr)
5934 {
5935   switch (attr)
5936     {
5937     case DW_AT_sibling:
5938       return "DW_AT_sibling";
5939     case DW_AT_location:
5940       return "DW_AT_location";
5941     case DW_AT_name:
5942       return "DW_AT_name";
5943     case DW_AT_ordering:
5944       return "DW_AT_ordering";
5945     case DW_AT_subscr_data:
5946       return "DW_AT_subscr_data";
5947     case DW_AT_byte_size:
5948       return "DW_AT_byte_size";
5949     case DW_AT_bit_offset:
5950       return "DW_AT_bit_offset";
5951     case DW_AT_bit_size:
5952       return "DW_AT_bit_size";
5953     case DW_AT_element_list:
5954       return "DW_AT_element_list";
5955     case DW_AT_stmt_list:
5956       return "DW_AT_stmt_list";
5957     case DW_AT_low_pc:
5958       return "DW_AT_low_pc";
5959     case DW_AT_high_pc:
5960       return "DW_AT_high_pc";
5961     case DW_AT_language:
5962       return "DW_AT_language";
5963     case DW_AT_member:
5964       return "DW_AT_member";
5965     case DW_AT_discr:
5966       return "DW_AT_discr";
5967     case DW_AT_discr_value:
5968       return "DW_AT_discr_value";
5969     case DW_AT_visibility:
5970       return "DW_AT_visibility";
5971     case DW_AT_import:
5972       return "DW_AT_import";
5973     case DW_AT_string_length:
5974       return "DW_AT_string_length";
5975     case DW_AT_common_reference:
5976       return "DW_AT_common_reference";
5977     case DW_AT_comp_dir:
5978       return "DW_AT_comp_dir";
5979     case DW_AT_const_value:
5980       return "DW_AT_const_value";
5981     case DW_AT_containing_type:
5982       return "DW_AT_containing_type";
5983     case DW_AT_default_value:
5984       return "DW_AT_default_value";
5985     case DW_AT_inline:
5986       return "DW_AT_inline";
5987     case DW_AT_is_optional:
5988       return "DW_AT_is_optional";
5989     case DW_AT_lower_bound:
5990       return "DW_AT_lower_bound";
5991     case DW_AT_producer:
5992       return "DW_AT_producer";
5993     case DW_AT_prototyped:
5994       return "DW_AT_prototyped";
5995     case DW_AT_return_addr:
5996       return "DW_AT_return_addr";
5997     case DW_AT_start_scope:
5998       return "DW_AT_start_scope";
5999     case DW_AT_bit_stride:
6000       return "DW_AT_bit_stride";
6001     case DW_AT_upper_bound:
6002       return "DW_AT_upper_bound";
6003     case DW_AT_abstract_origin:
6004       return "DW_AT_abstract_origin";
6005     case DW_AT_accessibility:
6006       return "DW_AT_accessibility";
6007     case DW_AT_address_class:
6008       return "DW_AT_address_class";
6009     case DW_AT_artificial:
6010       return "DW_AT_artificial";
6011     case DW_AT_base_types:
6012       return "DW_AT_base_types";
6013     case DW_AT_calling_convention:
6014       return "DW_AT_calling_convention";
6015     case DW_AT_count:
6016       return "DW_AT_count";
6017     case DW_AT_data_member_location:
6018       return "DW_AT_data_member_location";
6019     case DW_AT_decl_column:
6020       return "DW_AT_decl_column";
6021     case DW_AT_decl_file:
6022       return "DW_AT_decl_file";
6023     case DW_AT_decl_line:
6024       return "DW_AT_decl_line";
6025     case DW_AT_declaration:
6026       return "DW_AT_declaration";
6027     case DW_AT_discr_list:
6028       return "DW_AT_discr_list";
6029     case DW_AT_encoding:
6030       return "DW_AT_encoding";
6031     case DW_AT_external:
6032       return "DW_AT_external";
6033     case DW_AT_explicit:
6034       return "DW_AT_explicit";
6035     case DW_AT_frame_base:
6036       return "DW_AT_frame_base";
6037     case DW_AT_friend:
6038       return "DW_AT_friend";
6039     case DW_AT_identifier_case:
6040       return "DW_AT_identifier_case";
6041     case DW_AT_macro_info:
6042       return "DW_AT_macro_info";
6043     case DW_AT_namelist_items:
6044       return "DW_AT_namelist_items";
6045     case DW_AT_priority:
6046       return "DW_AT_priority";
6047     case DW_AT_segment:
6048       return "DW_AT_segment";
6049     case DW_AT_specification:
6050       return "DW_AT_specification";
6051     case DW_AT_static_link:
6052       return "DW_AT_static_link";
6053     case DW_AT_type:
6054       return "DW_AT_type";
6055     case DW_AT_use_location:
6056       return "DW_AT_use_location";
6057     case DW_AT_variable_parameter:
6058       return "DW_AT_variable_parameter";
6059     case DW_AT_virtuality:
6060       return "DW_AT_virtuality";
6061     case DW_AT_vtable_elem_location:
6062       return "DW_AT_vtable_elem_location";
6063
6064     case DW_AT_allocated:
6065       return "DW_AT_allocated";
6066     case DW_AT_associated:
6067       return "DW_AT_associated";
6068     case DW_AT_data_location:
6069       return "DW_AT_data_location";
6070     case DW_AT_byte_stride:
6071       return "DW_AT_byte_stride";
6072     case DW_AT_entry_pc:
6073       return "DW_AT_entry_pc";
6074     case DW_AT_use_UTF8:
6075       return "DW_AT_use_UTF8";
6076     case DW_AT_extension:
6077       return "DW_AT_extension";
6078     case DW_AT_ranges:
6079       return "DW_AT_ranges";
6080     case DW_AT_trampoline:
6081       return "DW_AT_trampoline";
6082     case DW_AT_call_column:
6083       return "DW_AT_call_column";
6084     case DW_AT_call_file:
6085       return "DW_AT_call_file";
6086     case DW_AT_call_line:
6087       return "DW_AT_call_line";
6088
6089     case DW_AT_MIPS_fde:
6090       return "DW_AT_MIPS_fde";
6091     case DW_AT_MIPS_loop_begin:
6092       return "DW_AT_MIPS_loop_begin";
6093     case DW_AT_MIPS_tail_loop_begin:
6094       return "DW_AT_MIPS_tail_loop_begin";
6095     case DW_AT_MIPS_epilog_begin:
6096       return "DW_AT_MIPS_epilog_begin";
6097     case DW_AT_MIPS_loop_unroll_factor:
6098       return "DW_AT_MIPS_loop_unroll_factor";
6099     case DW_AT_MIPS_software_pipeline_depth:
6100       return "DW_AT_MIPS_software_pipeline_depth";
6101     case DW_AT_MIPS_linkage_name:
6102       return "DW_AT_MIPS_linkage_name";
6103     case DW_AT_MIPS_stride:
6104       return "DW_AT_MIPS_stride";
6105     case DW_AT_MIPS_abstract_name:
6106       return "DW_AT_MIPS_abstract_name";
6107     case DW_AT_MIPS_clone_origin:
6108       return "DW_AT_MIPS_clone_origin";
6109     case DW_AT_MIPS_has_inlines:
6110       return "DW_AT_MIPS_has_inlines";
6111
6112     case DW_AT_sf_names:
6113       return "DW_AT_sf_names";
6114     case DW_AT_src_info:
6115       return "DW_AT_src_info";
6116     case DW_AT_mac_info:
6117       return "DW_AT_mac_info";
6118     case DW_AT_src_coords:
6119       return "DW_AT_src_coords";
6120     case DW_AT_body_begin:
6121       return "DW_AT_body_begin";
6122     case DW_AT_body_end:
6123       return "DW_AT_body_end";
6124     case DW_AT_GNU_vector:
6125       return "DW_AT_GNU_vector";
6126
6127     case DW_AT_VMS_rtnbeg_pd_address:
6128       return "DW_AT_VMS_rtnbeg_pd_address";
6129
6130     default:
6131       return "DW_AT_<unknown>";
6132     }
6133 }
6134
6135 /* Convert a DWARF value form code into its string name.  */
6136
6137 static const char *
6138 dwarf_form_name (unsigned int form)
6139 {
6140   switch (form)
6141     {
6142     case DW_FORM_addr:
6143       return "DW_FORM_addr";
6144     case DW_FORM_block2:
6145       return "DW_FORM_block2";
6146     case DW_FORM_block4:
6147       return "DW_FORM_block4";
6148     case DW_FORM_data2:
6149       return "DW_FORM_data2";
6150     case DW_FORM_data4:
6151       return "DW_FORM_data4";
6152     case DW_FORM_data8:
6153       return "DW_FORM_data8";
6154     case DW_FORM_string:
6155       return "DW_FORM_string";
6156     case DW_FORM_block:
6157       return "DW_FORM_block";
6158     case DW_FORM_block1:
6159       return "DW_FORM_block1";
6160     case DW_FORM_data1:
6161       return "DW_FORM_data1";
6162     case DW_FORM_flag:
6163       return "DW_FORM_flag";
6164     case DW_FORM_sdata:
6165       return "DW_FORM_sdata";
6166     case DW_FORM_strp:
6167       return "DW_FORM_strp";
6168     case DW_FORM_udata:
6169       return "DW_FORM_udata";
6170     case DW_FORM_ref_addr:
6171       return "DW_FORM_ref_addr";
6172     case DW_FORM_ref1:
6173       return "DW_FORM_ref1";
6174     case DW_FORM_ref2:
6175       return "DW_FORM_ref2";
6176     case DW_FORM_ref4:
6177       return "DW_FORM_ref4";
6178     case DW_FORM_ref8:
6179       return "DW_FORM_ref8";
6180     case DW_FORM_ref_udata:
6181       return "DW_FORM_ref_udata";
6182     case DW_FORM_indirect:
6183       return "DW_FORM_indirect";
6184     default:
6185       return "DW_FORM_<unknown>";
6186     }
6187 }
6188 \f
6189 /* Determine the "ultimate origin" of a decl.  The decl may be an inlined
6190    instance of an inlined instance of a decl which is local to an inline
6191    function, so we have to trace all of the way back through the origin chain
6192    to find out what sort of node actually served as the original seed for the
6193    given block.  */
6194
6195 static tree
6196 decl_ultimate_origin (const_tree decl)
6197 {
6198   if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
6199     return NULL_TREE;
6200
6201   /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
6202      nodes in the function to point to themselves; ignore that if
6203      we're trying to output the abstract instance of this function.  */
6204   if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
6205     return NULL_TREE;
6206
6207   /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
6208      most distant ancestor, this should never happen.  */
6209   gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
6210
6211   return DECL_ABSTRACT_ORIGIN (decl);
6212 }
6213
6214 /* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
6215    of a virtual function may refer to a base class, so we check the 'this'
6216    parameter.  */
6217
6218 static tree
6219 decl_class_context (tree decl)
6220 {
6221   tree context = NULL_TREE;
6222
6223   if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
6224     context = DECL_CONTEXT (decl);
6225   else
6226     context = TYPE_MAIN_VARIANT
6227       (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
6228
6229   if (context && !TYPE_P (context))
6230     context = NULL_TREE;
6231
6232   return context;
6233 }
6234 \f
6235 /* Add an attribute/value pair to a DIE.  */
6236
6237 static inline void
6238 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
6239 {
6240   /* Maybe this should be an assert?  */
6241   if (die == NULL)
6242     return;
6243
6244   if (die->die_attr == NULL)
6245     die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
6246   VEC_safe_push (dw_attr_node, gc, die->die_attr, attr);
6247 }
6248
6249 static inline enum dw_val_class
6250 AT_class (dw_attr_ref a)
6251 {
6252   return a->dw_attr_val.val_class;
6253 }
6254
6255 /* Add a flag value attribute to a DIE.  */
6256
6257 static inline void
6258 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
6259 {
6260   dw_attr_node attr;
6261
6262   attr.dw_attr = attr_kind;
6263   attr.dw_attr_val.val_class = dw_val_class_flag;
6264   attr.dw_attr_val.v.val_flag = flag;
6265   add_dwarf_attr (die, &attr);
6266 }
6267
6268 static inline unsigned
6269 AT_flag (dw_attr_ref a)
6270 {
6271   gcc_assert (a && AT_class (a) == dw_val_class_flag);
6272   return a->dw_attr_val.v.val_flag;
6273 }
6274
6275 /* Add a signed integer attribute value to a DIE.  */
6276
6277 static inline void
6278 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
6279 {
6280   dw_attr_node attr;
6281
6282   attr.dw_attr = attr_kind;
6283   attr.dw_attr_val.val_class = dw_val_class_const;
6284   attr.dw_attr_val.v.val_int = int_val;
6285   add_dwarf_attr (die, &attr);
6286 }
6287
6288 static inline HOST_WIDE_INT
6289 AT_int (dw_attr_ref a)
6290 {
6291   gcc_assert (a && AT_class (a) == dw_val_class_const);
6292   return a->dw_attr_val.v.val_int;
6293 }
6294
6295 /* Add an unsigned integer attribute value to a DIE.  */
6296
6297 static inline void
6298 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
6299                  unsigned HOST_WIDE_INT unsigned_val)
6300 {
6301   dw_attr_node attr;
6302
6303   attr.dw_attr = attr_kind;
6304   attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
6305   attr.dw_attr_val.v.val_unsigned = unsigned_val;
6306   add_dwarf_attr (die, &attr);
6307 }
6308
6309 static inline unsigned HOST_WIDE_INT
6310 AT_unsigned (dw_attr_ref a)
6311 {
6312   gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
6313   return a->dw_attr_val.v.val_unsigned;
6314 }
6315
6316 /* Add an unsigned double integer attribute value to a DIE.  */
6317
6318 static inline void
6319 add_AT_long_long (dw_die_ref die, enum dwarf_attribute attr_kind,
6320                   long unsigned int val_hi, long unsigned int val_low)
6321 {
6322   dw_attr_node attr;
6323
6324   attr.dw_attr = attr_kind;
6325   attr.dw_attr_val.val_class = dw_val_class_long_long;
6326   attr.dw_attr_val.v.val_long_long.hi = val_hi;
6327   attr.dw_attr_val.v.val_long_long.low = val_low;
6328   add_dwarf_attr (die, &attr);
6329 }
6330
6331 /* Add a floating point attribute value to a DIE and return it.  */
6332
6333 static inline void
6334 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
6335             unsigned int length, unsigned int elt_size, unsigned char *array)
6336 {
6337   dw_attr_node attr;
6338
6339   attr.dw_attr = attr_kind;
6340   attr.dw_attr_val.val_class = dw_val_class_vec;
6341   attr.dw_attr_val.v.val_vec.length = length;
6342   attr.dw_attr_val.v.val_vec.elt_size = elt_size;
6343   attr.dw_attr_val.v.val_vec.array = array;
6344   add_dwarf_attr (die, &attr);
6345 }
6346
6347 /* Hash and equality functions for debug_str_hash.  */
6348
6349 static hashval_t
6350 debug_str_do_hash (const void *x)
6351 {
6352   return htab_hash_string (((const struct indirect_string_node *)x)->str);
6353 }
6354
6355 static int
6356 debug_str_eq (const void *x1, const void *x2)
6357 {
6358   return strcmp ((((const struct indirect_string_node *)x1)->str),
6359                  (const char *)x2) == 0;
6360 }
6361
6362 static struct indirect_string_node *
6363 find_AT_string (const char *str)
6364 {
6365   struct indirect_string_node *node;
6366   void **slot;
6367
6368   if (! debug_str_hash)
6369     debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
6370                                       debug_str_eq, NULL);
6371
6372   slot = htab_find_slot_with_hash (debug_str_hash, str,
6373                                    htab_hash_string (str), INSERT);
6374   if (*slot == NULL)
6375     {
6376       node = (struct indirect_string_node *)
6377                ggc_alloc_cleared (sizeof (struct indirect_string_node));
6378       node->str = ggc_strdup (str);
6379       *slot = node;
6380     }
6381   else
6382     node = (struct indirect_string_node *) *slot;
6383
6384   node->refcount++;
6385   return node;
6386 }
6387
6388 /* Add a string attribute value to a DIE.  */
6389
6390 static inline void
6391 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
6392 {
6393   dw_attr_node attr;
6394   struct indirect_string_node *node;
6395
6396   node = find_AT_string (str);
6397
6398   attr.dw_attr = attr_kind;
6399   attr.dw_attr_val.val_class = dw_val_class_str;
6400   attr.dw_attr_val.v.val_str = node;
6401   add_dwarf_attr (die, &attr);
6402 }
6403
6404 static inline const char *
6405 AT_string (dw_attr_ref a)
6406 {
6407   gcc_assert (a && AT_class (a) == dw_val_class_str);
6408   return a->dw_attr_val.v.val_str->str;
6409 }
6410
6411 /* Find out whether a string should be output inline in DIE
6412    or out-of-line in .debug_str section.  */
6413
6414 static enum dwarf_form
6415 AT_string_form (dw_attr_ref a)
6416 {
6417   struct indirect_string_node *node;
6418   unsigned int len;
6419   char label[32];
6420
6421   gcc_assert (a && AT_class (a) == dw_val_class_str);
6422
6423   node = a->dw_attr_val.v.val_str;
6424   if (node->form)
6425     return node->form;
6426
6427   len = strlen (node->str) + 1;
6428
6429   /* If the string is shorter or equal to the size of the reference, it is
6430      always better to put it inline.  */
6431   if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
6432     return node->form = DW_FORM_string;
6433
6434   /* If we cannot expect the linker to merge strings in .debug_str
6435      section, only put it into .debug_str if it is worth even in this
6436      single module.  */
6437   if ((debug_str_section->common.flags & SECTION_MERGE) == 0
6438       && (len - DWARF_OFFSET_SIZE) * node->refcount <= len)
6439     return node->form = DW_FORM_string;
6440
6441   ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
6442   ++dw2_string_counter;
6443   node->label = xstrdup (label);
6444
6445   return node->form = DW_FORM_strp;
6446 }
6447
6448 /* Add a DIE reference attribute value to a DIE.  */
6449
6450 static inline void
6451 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
6452 {
6453   dw_attr_node attr;
6454
6455   attr.dw_attr = attr_kind;
6456   attr.dw_attr_val.val_class = dw_val_class_die_ref;
6457   attr.dw_attr_val.v.val_die_ref.die = targ_die;
6458   attr.dw_attr_val.v.val_die_ref.external = 0;
6459   add_dwarf_attr (die, &attr);
6460 }
6461
6462 /* Add an AT_specification attribute to a DIE, and also make the back
6463    pointer from the specification to the definition.  */
6464
6465 static inline void
6466 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
6467 {
6468   add_AT_die_ref (die, DW_AT_specification, targ_die);
6469   gcc_assert (!targ_die->die_definition);
6470   targ_die->die_definition = die;
6471 }
6472
6473 static inline dw_die_ref
6474 AT_ref (dw_attr_ref a)
6475 {
6476   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
6477   return a->dw_attr_val.v.val_die_ref.die;
6478 }
6479
6480 static inline int
6481 AT_ref_external (dw_attr_ref a)
6482 {
6483   if (a && AT_class (a) == dw_val_class_die_ref)
6484     return a->dw_attr_val.v.val_die_ref.external;
6485
6486   return 0;
6487 }
6488
6489 static inline void
6490 set_AT_ref_external (dw_attr_ref a, int i)
6491 {
6492   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
6493   a->dw_attr_val.v.val_die_ref.external = i;
6494 }
6495
6496 /* Add an FDE reference attribute value to a DIE.  */
6497
6498 static inline void
6499 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
6500 {
6501   dw_attr_node attr;
6502
6503   attr.dw_attr = attr_kind;
6504   attr.dw_attr_val.val_class = dw_val_class_fde_ref;
6505   attr.dw_attr_val.v.val_fde_index = targ_fde;
6506   add_dwarf_attr (die, &attr);
6507 }
6508
6509 /* Add a location description attribute value to a DIE.  */
6510
6511 static inline void
6512 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
6513 {
6514   dw_attr_node attr;
6515
6516   attr.dw_attr = attr_kind;
6517   attr.dw_attr_val.val_class = dw_val_class_loc;
6518   attr.dw_attr_val.v.val_loc = loc;
6519   add_dwarf_attr (die, &attr);
6520 }
6521
6522 static inline dw_loc_descr_ref
6523 AT_loc (dw_attr_ref a)
6524 {
6525   gcc_assert (a && AT_class (a) == dw_val_class_loc);
6526   return a->dw_attr_val.v.val_loc;
6527 }
6528
6529 static inline void
6530 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
6531 {
6532   dw_attr_node attr;
6533
6534   attr.dw_attr = attr_kind;
6535   attr.dw_attr_val.val_class = dw_val_class_loc_list;
6536   attr.dw_attr_val.v.val_loc_list = loc_list;
6537   add_dwarf_attr (die, &attr);
6538   have_location_lists = true;
6539 }
6540
6541 static inline dw_loc_list_ref
6542 AT_loc_list (dw_attr_ref a)
6543 {
6544   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
6545   return a->dw_attr_val.v.val_loc_list;
6546 }
6547
6548 /* Add an address constant attribute value to a DIE.  */
6549
6550 static inline void
6551 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
6552 {
6553   dw_attr_node attr;
6554
6555   attr.dw_attr = attr_kind;
6556   attr.dw_attr_val.val_class = dw_val_class_addr;
6557   attr.dw_attr_val.v.val_addr = addr;
6558   add_dwarf_attr (die, &attr);
6559 }
6560
6561 /* Get the RTX from to an address DIE attribute.  */
6562
6563 static inline rtx
6564 AT_addr (dw_attr_ref a)
6565 {
6566   gcc_assert (a && AT_class (a) == dw_val_class_addr);
6567   return a->dw_attr_val.v.val_addr;
6568 }
6569
6570 /* Add a file attribute value to a DIE.  */
6571
6572 static inline void
6573 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
6574              struct dwarf_file_data *fd)
6575 {
6576   dw_attr_node attr;
6577
6578   attr.dw_attr = attr_kind;
6579   attr.dw_attr_val.val_class = dw_val_class_file;
6580   attr.dw_attr_val.v.val_file = fd;
6581   add_dwarf_attr (die, &attr);
6582 }
6583
6584 /* Get the dwarf_file_data from a file DIE attribute.  */
6585
6586 static inline struct dwarf_file_data *
6587 AT_file (dw_attr_ref a)
6588 {
6589   gcc_assert (a && AT_class (a) == dw_val_class_file);
6590   return a->dw_attr_val.v.val_file;
6591 }
6592
6593 /* Add a label identifier attribute value to a DIE.  */
6594
6595 static inline void
6596 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
6597 {
6598   dw_attr_node attr;
6599
6600   attr.dw_attr = attr_kind;
6601   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
6602   attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
6603   add_dwarf_attr (die, &attr);
6604 }
6605
6606 /* Add a section offset attribute value to a DIE, an offset into the
6607    debug_line section.  */
6608
6609 static inline void
6610 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
6611                 const char *label)
6612 {
6613   dw_attr_node attr;
6614
6615   attr.dw_attr = attr_kind;
6616   attr.dw_attr_val.val_class = dw_val_class_lineptr;
6617   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
6618   add_dwarf_attr (die, &attr);
6619 }
6620
6621 /* Add a section offset attribute value to a DIE, an offset into the
6622    debug_macinfo section.  */
6623
6624 static inline void
6625 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
6626                const char *label)
6627 {
6628   dw_attr_node attr;
6629
6630   attr.dw_attr = attr_kind;
6631   attr.dw_attr_val.val_class = dw_val_class_macptr;
6632   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
6633   add_dwarf_attr (die, &attr);
6634 }
6635
6636 /* Add an offset attribute value to a DIE.  */
6637
6638 static inline void
6639 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
6640                unsigned HOST_WIDE_INT offset)
6641 {
6642   dw_attr_node attr;
6643
6644   attr.dw_attr = attr_kind;
6645   attr.dw_attr_val.val_class = dw_val_class_offset;
6646   attr.dw_attr_val.v.val_offset = offset;
6647   add_dwarf_attr (die, &attr);
6648 }
6649
6650 /* Add an range_list attribute value to a DIE.  */
6651
6652 static void
6653 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
6654                    long unsigned int offset)
6655 {
6656   dw_attr_node attr;
6657
6658   attr.dw_attr = attr_kind;
6659   attr.dw_attr_val.val_class = dw_val_class_range_list;
6660   attr.dw_attr_val.v.val_offset = offset;
6661   add_dwarf_attr (die, &attr);
6662 }
6663
6664 static inline const char *
6665 AT_lbl (dw_attr_ref a)
6666 {
6667   gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
6668                     || AT_class (a) == dw_val_class_lineptr
6669                     || AT_class (a) == dw_val_class_macptr));
6670   return a->dw_attr_val.v.val_lbl_id;
6671 }
6672
6673 /* Get the attribute of type attr_kind.  */
6674
6675 static dw_attr_ref
6676 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
6677 {
6678   dw_attr_ref a;
6679   unsigned ix;
6680   dw_die_ref spec = NULL;
6681
6682   if (! die)
6683     return NULL;
6684
6685   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6686     if (a->dw_attr == attr_kind)
6687       return a;
6688     else if (a->dw_attr == DW_AT_specification
6689              || a->dw_attr == DW_AT_abstract_origin)
6690       spec = AT_ref (a);
6691
6692   if (spec)
6693     return get_AT (spec, attr_kind);
6694
6695   return NULL;
6696 }
6697
6698 /* Return the "low pc" attribute value, typically associated with a subprogram
6699    DIE.  Return null if the "low pc" attribute is either not present, or if it
6700    cannot be represented as an assembler label identifier.  */
6701
6702 static inline const char *
6703 get_AT_low_pc (dw_die_ref die)
6704 {
6705   dw_attr_ref a = get_AT (die, DW_AT_low_pc);
6706
6707   return a ? AT_lbl (a) : NULL;
6708 }
6709
6710 /* Return the "high pc" attribute value, typically associated with a subprogram
6711    DIE.  Return null if the "high pc" attribute is either not present, or if it
6712    cannot be represented as an assembler label identifier.  */
6713
6714 static inline const char *
6715 get_AT_hi_pc (dw_die_ref die)
6716 {
6717   dw_attr_ref a = get_AT (die, DW_AT_high_pc);
6718
6719   return a ? AT_lbl (a) : NULL;
6720 }
6721
6722 /* Return the value of the string attribute designated by ATTR_KIND, or
6723    NULL if it is not present.  */
6724
6725 static inline const char *
6726 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
6727 {
6728   dw_attr_ref a = get_AT (die, attr_kind);
6729
6730   return a ? AT_string (a) : NULL;
6731 }
6732
6733 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
6734    if it is not present.  */
6735
6736 static inline int
6737 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
6738 {
6739   dw_attr_ref a = get_AT (die, attr_kind);
6740
6741   return a ? AT_flag (a) : 0;
6742 }
6743
6744 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
6745    if it is not present.  */
6746
6747 static inline unsigned
6748 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
6749 {
6750   dw_attr_ref a = get_AT (die, attr_kind);
6751
6752   return a ? AT_unsigned (a) : 0;
6753 }
6754
6755 static inline dw_die_ref
6756 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
6757 {
6758   dw_attr_ref a = get_AT (die, attr_kind);
6759
6760   return a ? AT_ref (a) : NULL;
6761 }
6762
6763 static inline struct dwarf_file_data *
6764 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
6765 {
6766   dw_attr_ref a = get_AT (die, attr_kind);
6767
6768   return a ? AT_file (a) : NULL;
6769 }
6770
6771 /* Return TRUE if the language is C or C++.  */
6772
6773 static inline bool
6774 is_c_family (void)
6775 {
6776   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
6777
6778   return (lang == DW_LANG_C || lang == DW_LANG_C89 || lang == DW_LANG_ObjC
6779           || lang == DW_LANG_C99
6780           || lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus);
6781 }
6782
6783 /* Return TRUE if the language is C++.  */
6784
6785 static inline bool
6786 is_cxx (void)
6787 {
6788   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
6789
6790   return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
6791 }
6792
6793 /* Return TRUE if the language is Fortran.  */
6794
6795 static inline bool
6796 is_fortran (void)
6797 {
6798   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
6799
6800   return (lang == DW_LANG_Fortran77
6801           || lang == DW_LANG_Fortran90
6802           || lang == DW_LANG_Fortran95);
6803 }
6804
6805 /* Return TRUE if the language is Java.  */
6806
6807 static inline bool
6808 is_java (void)
6809 {
6810   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
6811
6812   return lang == DW_LANG_Java;
6813 }
6814
6815 /* Return TRUE if the language is Ada.  */
6816
6817 static inline bool
6818 is_ada (void)
6819 {
6820   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
6821
6822   return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
6823 }
6824
6825 /* Remove the specified attribute if present.  */
6826
6827 static void
6828 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
6829 {
6830   dw_attr_ref a;
6831   unsigned ix;
6832
6833   if (! die)
6834     return;
6835
6836   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6837     if (a->dw_attr == attr_kind)
6838       {
6839         if (AT_class (a) == dw_val_class_str)
6840           if (a->dw_attr_val.v.val_str->refcount)
6841             a->dw_attr_val.v.val_str->refcount--;
6842
6843         /* VEC_ordered_remove should help reduce the number of abbrevs
6844            that are needed.  */
6845         VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
6846         return;
6847       }
6848 }
6849
6850 /* Remove CHILD from its parent.  PREV must have the property that
6851    PREV->DIE_SIB == CHILD.  Does not alter CHILD.  */
6852
6853 static void
6854 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
6855 {
6856   gcc_assert (child->die_parent == prev->die_parent);
6857   gcc_assert (prev->die_sib == child);
6858   if (prev == child)
6859     {
6860       gcc_assert (child->die_parent->die_child == child);
6861       prev = NULL;
6862     }
6863   else
6864     prev->die_sib = child->die_sib;
6865   if (child->die_parent->die_child == child)
6866     child->die_parent->die_child = prev;
6867 }
6868
6869 /* Remove child DIE whose die_tag is TAG.  Do nothing if no child
6870    matches TAG.  */
6871
6872 static void
6873 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
6874 {
6875   dw_die_ref c;
6876
6877   c = die->die_child;
6878   if (c) do {
6879     dw_die_ref prev = c;
6880     c = c->die_sib;
6881     while (c->die_tag == tag)
6882       {
6883         remove_child_with_prev (c, prev);
6884         /* Might have removed every child.  */
6885         if (c == c->die_sib)
6886           return;
6887         c = c->die_sib;
6888       }
6889   } while (c != die->die_child);
6890 }
6891
6892 /* Add a CHILD_DIE as the last child of DIE.  */
6893
6894 static void
6895 add_child_die (dw_die_ref die, dw_die_ref child_die)
6896 {
6897   /* FIXME this should probably be an assert.  */
6898   if (! die || ! child_die)
6899     return;
6900   gcc_assert (die != child_die);
6901
6902   child_die->die_parent = die;
6903   if (die->die_child)
6904     {
6905       child_die->die_sib = die->die_child->die_sib;
6906       die->die_child->die_sib = child_die;
6907     }
6908   else
6909     child_die->die_sib = child_die;
6910   die->die_child = child_die;
6911 }
6912
6913 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
6914    is the specification, to the end of PARENT's list of children.
6915    This is done by removing and re-adding it.  */
6916
6917 static void
6918 splice_child_die (dw_die_ref parent, dw_die_ref child)
6919 {
6920   dw_die_ref p;
6921
6922   /* We want the declaration DIE from inside the class, not the
6923      specification DIE at toplevel.  */
6924   if (child->die_parent != parent)
6925     {
6926       dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
6927
6928       if (tmp)
6929         child = tmp;
6930     }
6931
6932   gcc_assert (child->die_parent == parent
6933               || (child->die_parent
6934                   == get_AT_ref (parent, DW_AT_specification)));
6935
6936   for (p = child->die_parent->die_child; ; p = p->die_sib)
6937     if (p->die_sib == child)
6938       {
6939         remove_child_with_prev (child, p);
6940         break;
6941       }
6942
6943   add_child_die (parent, child);
6944 }
6945
6946 /* Return a pointer to a newly created DIE node.  */
6947
6948 static inline dw_die_ref
6949 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
6950 {
6951   dw_die_ref die = GGC_CNEW (die_node);
6952
6953   die->die_tag = tag_value;
6954
6955   if (parent_die != NULL)
6956     add_child_die (parent_die, die);
6957   else
6958     {
6959       limbo_die_node *limbo_node;
6960
6961       limbo_node = GGC_CNEW (limbo_die_node);
6962       limbo_node->die = die;
6963       limbo_node->created_for = t;
6964       limbo_node->next = limbo_die_list;
6965       limbo_die_list = limbo_node;
6966     }
6967
6968   return die;
6969 }
6970
6971 /* Return the DIE associated with the given type specifier.  */
6972
6973 static inline dw_die_ref
6974 lookup_type_die (tree type)
6975 {
6976   return TYPE_SYMTAB_DIE (type);
6977 }
6978
6979 /* Equate a DIE to a given type specifier.  */
6980
6981 static inline void
6982 equate_type_number_to_die (tree type, dw_die_ref type_die)
6983 {
6984   TYPE_SYMTAB_DIE (type) = type_die;
6985 }
6986
6987 /* Returns a hash value for X (which really is a die_struct).  */
6988
6989 static hashval_t
6990 decl_die_table_hash (const void *x)
6991 {
6992   return (hashval_t) ((const_dw_die_ref) x)->decl_id;
6993 }
6994
6995 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
6996
6997 static int
6998 decl_die_table_eq (const void *x, const void *y)
6999 {
7000   return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
7001 }
7002
7003 /* Return the DIE associated with a given declaration.  */
7004
7005 static inline dw_die_ref
7006 lookup_decl_die (tree decl)
7007 {
7008   return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
7009 }
7010
7011 /* Returns a hash value for X (which really is a var_loc_list).  */
7012
7013 static hashval_t
7014 decl_loc_table_hash (const void *x)
7015 {
7016   return (hashval_t) ((const var_loc_list *) x)->decl_id;
7017 }
7018
7019 /* Return nonzero if decl_id of var_loc_list X is the same as
7020    UID of decl *Y.  */
7021
7022 static int
7023 decl_loc_table_eq (const void *x, const void *y)
7024 {
7025   return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
7026 }
7027
7028 /* Return the var_loc list associated with a given declaration.  */
7029
7030 static inline var_loc_list *
7031 lookup_decl_loc (const_tree decl)
7032 {
7033   return (var_loc_list *)
7034     htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
7035 }
7036
7037 /* Equate a DIE to a particular declaration.  */
7038
7039 static void
7040 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
7041 {
7042   unsigned int decl_id = DECL_UID (decl);
7043   void **slot;
7044
7045   slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
7046   *slot = decl_die;
7047   decl_die->decl_id = decl_id;
7048 }
7049
7050 /* Add a variable location node to the linked list for DECL.  */
7051
7052 static void
7053 add_var_loc_to_decl (tree decl, struct var_loc_node *loc)
7054 {
7055   unsigned int decl_id = DECL_UID (decl);
7056   var_loc_list *temp;
7057   void **slot;
7058
7059   slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
7060   if (*slot == NULL)
7061     {
7062       temp = GGC_CNEW (var_loc_list);
7063       temp->decl_id = decl_id;
7064       *slot = temp;
7065     }
7066   else
7067     temp = (var_loc_list *) *slot;
7068
7069   if (temp->last)
7070     {
7071       /* If the current location is the same as the end of the list,
7072          and either both or neither of the locations is uninitialized,
7073          we have nothing to do.  */
7074       if ((!rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->last->var_loc_note),
7075                          NOTE_VAR_LOCATION_LOC (loc->var_loc_note)))
7076           || ((NOTE_VAR_LOCATION_STATUS (temp->last->var_loc_note)
7077                != NOTE_VAR_LOCATION_STATUS (loc->var_loc_note))
7078               && ((NOTE_VAR_LOCATION_STATUS (temp->last->var_loc_note)
7079                    == VAR_INIT_STATUS_UNINITIALIZED)
7080                   || (NOTE_VAR_LOCATION_STATUS (loc->var_loc_note)
7081                       == VAR_INIT_STATUS_UNINITIALIZED))))
7082         {
7083           /* Add LOC to the end of list and update LAST.  */
7084           temp->last->next = loc;
7085           temp->last = loc;
7086         }
7087     }
7088   /* Do not add empty location to the beginning of the list.  */
7089   else if (NOTE_VAR_LOCATION_LOC (loc->var_loc_note) != NULL_RTX)
7090     {
7091       temp->first = loc;
7092       temp->last = loc;
7093     }
7094 }
7095 \f
7096 /* Keep track of the number of spaces used to indent the
7097    output of the debugging routines that print the structure of
7098    the DIE internal representation.  */
7099 static int print_indent;
7100
7101 /* Indent the line the number of spaces given by print_indent.  */
7102
7103 static inline void
7104 print_spaces (FILE *outfile)
7105 {
7106   fprintf (outfile, "%*s", print_indent, "");
7107 }
7108
7109 /* Print the information associated with a given DIE, and its children.
7110    This routine is a debugging aid only.  */
7111
7112 static void
7113 print_die (dw_die_ref die, FILE *outfile)
7114 {
7115   dw_attr_ref a;
7116   dw_die_ref c;
7117   unsigned ix;
7118
7119   print_spaces (outfile);
7120   fprintf (outfile, "DIE %4ld: %s\n",
7121            die->die_offset, dwarf_tag_name (die->die_tag));
7122   print_spaces (outfile);
7123   fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
7124   fprintf (outfile, " offset: %ld\n", die->die_offset);
7125
7126   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7127     {
7128       print_spaces (outfile);
7129       fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
7130
7131       switch (AT_class (a))
7132         {
7133         case dw_val_class_addr:
7134           fprintf (outfile, "address");
7135           break;
7136         case dw_val_class_offset:
7137           fprintf (outfile, "offset");
7138           break;
7139         case dw_val_class_loc:
7140           fprintf (outfile, "location descriptor");
7141           break;
7142         case dw_val_class_loc_list:
7143           fprintf (outfile, "location list -> label:%s",
7144                    AT_loc_list (a)->ll_symbol);
7145           break;
7146         case dw_val_class_range_list:
7147           fprintf (outfile, "range list");
7148           break;
7149         case dw_val_class_const:
7150           fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
7151           break;
7152         case dw_val_class_unsigned_const:
7153           fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
7154           break;
7155         case dw_val_class_long_long:
7156           fprintf (outfile, "constant (%lu,%lu)",
7157                    a->dw_attr_val.v.val_long_long.hi,
7158                    a->dw_attr_val.v.val_long_long.low);
7159           break;
7160         case dw_val_class_vec:
7161           fprintf (outfile, "floating-point or vector constant");
7162           break;
7163         case dw_val_class_flag:
7164           fprintf (outfile, "%u", AT_flag (a));
7165           break;
7166         case dw_val_class_die_ref:
7167           if (AT_ref (a) != NULL)
7168             {
7169               if (AT_ref (a)->die_symbol)
7170                 fprintf (outfile, "die -> label: %s", AT_ref (a)->die_symbol);
7171               else
7172                 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
7173             }
7174           else
7175             fprintf (outfile, "die -> <null>");
7176           break;
7177         case dw_val_class_lbl_id:
7178         case dw_val_class_lineptr:
7179         case dw_val_class_macptr:
7180           fprintf (outfile, "label: %s", AT_lbl (a));
7181           break;
7182         case dw_val_class_str:
7183           if (AT_string (a) != NULL)
7184             fprintf (outfile, "\"%s\"", AT_string (a));
7185           else
7186             fprintf (outfile, "<null>");
7187           break;
7188         case dw_val_class_file:
7189           fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
7190                    AT_file (a)->emitted_number);
7191           break;
7192         default:
7193           break;
7194         }
7195
7196       fprintf (outfile, "\n");
7197     }
7198
7199   if (die->die_child != NULL)
7200     {
7201       print_indent += 4;
7202       FOR_EACH_CHILD (die, c, print_die (c, outfile));
7203       print_indent -= 4;
7204     }
7205   if (print_indent == 0)
7206     fprintf (outfile, "\n");
7207 }
7208
7209 /* Print the contents of the source code line number correspondence table.
7210    This routine is a debugging aid only.  */
7211
7212 static void
7213 print_dwarf_line_table (FILE *outfile)
7214 {
7215   unsigned i;
7216   dw_line_info_ref line_info;
7217
7218   fprintf (outfile, "\n\nDWARF source line information\n");
7219   for (i = 1; i < line_info_table_in_use; i++)
7220     {
7221       line_info = &line_info_table[i];
7222       fprintf (outfile, "%5d: %4ld %6ld\n", i,
7223                line_info->dw_file_num,
7224                line_info->dw_line_num);
7225     }
7226
7227   fprintf (outfile, "\n\n");
7228 }
7229
7230 /* Print the information collected for a given DIE.  */
7231
7232 void
7233 debug_dwarf_die (dw_die_ref die)
7234 {
7235   print_die (die, stderr);
7236 }
7237
7238 /* Print all DWARF information collected for the compilation unit.
7239    This routine is a debugging aid only.  */
7240
7241 void
7242 debug_dwarf (void)
7243 {
7244   print_indent = 0;
7245   print_die (comp_unit_die, stderr);
7246   if (! DWARF2_ASM_LINE_DEBUG_INFO)
7247     print_dwarf_line_table (stderr);
7248 }
7249 \f
7250 /* Start a new compilation unit DIE for an include file.  OLD_UNIT is the CU
7251    for the enclosing include file, if any.  BINCL_DIE is the DW_TAG_GNU_BINCL
7252    DIE that marks the start of the DIEs for this include file.  */
7253
7254 static dw_die_ref
7255 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
7256 {
7257   const char *filename = get_AT_string (bincl_die, DW_AT_name);
7258   dw_die_ref new_unit = gen_compile_unit_die (filename);
7259
7260   new_unit->die_sib = old_unit;
7261   return new_unit;
7262 }
7263
7264 /* Close an include-file CU and reopen the enclosing one.  */
7265
7266 static dw_die_ref
7267 pop_compile_unit (dw_die_ref old_unit)
7268 {
7269   dw_die_ref new_unit = old_unit->die_sib;
7270
7271   old_unit->die_sib = NULL;
7272   return new_unit;
7273 }
7274
7275 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
7276 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
7277
7278 /* Calculate the checksum of a location expression.  */
7279
7280 static inline void
7281 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
7282 {
7283   CHECKSUM (loc->dw_loc_opc);
7284   CHECKSUM (loc->dw_loc_oprnd1);
7285   CHECKSUM (loc->dw_loc_oprnd2);
7286 }
7287
7288 /* Calculate the checksum of an attribute.  */
7289
7290 static void
7291 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
7292 {
7293   dw_loc_descr_ref loc;
7294   rtx r;
7295
7296   CHECKSUM (at->dw_attr);
7297
7298   /* We don't care that this was compiled with a different compiler
7299      snapshot; if the output is the same, that's what matters.  */
7300   if (at->dw_attr == DW_AT_producer)
7301     return;
7302
7303   switch (AT_class (at))
7304     {
7305     case dw_val_class_const:
7306       CHECKSUM (at->dw_attr_val.v.val_int);
7307       break;
7308     case dw_val_class_unsigned_const:
7309       CHECKSUM (at->dw_attr_val.v.val_unsigned);
7310       break;
7311     case dw_val_class_long_long:
7312       CHECKSUM (at->dw_attr_val.v.val_long_long);
7313       break;
7314     case dw_val_class_vec:
7315       CHECKSUM (at->dw_attr_val.v.val_vec);
7316       break;
7317     case dw_val_class_flag:
7318       CHECKSUM (at->dw_attr_val.v.val_flag);
7319       break;
7320     case dw_val_class_str:
7321       CHECKSUM_STRING (AT_string (at));
7322       break;
7323
7324     case dw_val_class_addr:
7325       r = AT_addr (at);
7326       gcc_assert (GET_CODE (r) == SYMBOL_REF);
7327       CHECKSUM_STRING (XSTR (r, 0));
7328       break;
7329
7330     case dw_val_class_offset:
7331       CHECKSUM (at->dw_attr_val.v.val_offset);
7332       break;
7333
7334     case dw_val_class_loc:
7335       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
7336         loc_checksum (loc, ctx);
7337       break;
7338
7339     case dw_val_class_die_ref:
7340       die_checksum (AT_ref (at), ctx, mark);
7341       break;
7342
7343     case dw_val_class_fde_ref:
7344     case dw_val_class_lbl_id:
7345     case dw_val_class_lineptr:
7346     case dw_val_class_macptr:
7347       break;
7348
7349     case dw_val_class_file:
7350       CHECKSUM_STRING (AT_file (at)->filename);
7351       break;
7352
7353     default:
7354       break;
7355     }
7356 }
7357
7358 /* Calculate the checksum of a DIE.  */
7359
7360 static void
7361 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
7362 {
7363   dw_die_ref c;
7364   dw_attr_ref a;
7365   unsigned ix;
7366
7367   /* To avoid infinite recursion.  */
7368   if (die->die_mark)
7369     {
7370       CHECKSUM (die->die_mark);
7371       return;
7372     }
7373   die->die_mark = ++(*mark);
7374
7375   CHECKSUM (die->die_tag);
7376
7377   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7378     attr_checksum (a, ctx, mark);
7379
7380   FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
7381 }
7382
7383 #undef CHECKSUM
7384 #undef CHECKSUM_STRING
7385
7386 /* Do the location expressions look same?  */
7387 static inline int
7388 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
7389 {
7390   return loc1->dw_loc_opc == loc2->dw_loc_opc
7391          && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
7392          && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
7393 }
7394
7395 /* Do the values look the same?  */
7396 static int
7397 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
7398 {
7399   dw_loc_descr_ref loc1, loc2;
7400   rtx r1, r2;
7401
7402   if (v1->val_class != v2->val_class)
7403     return 0;
7404
7405   switch (v1->val_class)
7406     {
7407     case dw_val_class_const:
7408       return v1->v.val_int == v2->v.val_int;
7409     case dw_val_class_unsigned_const:
7410       return v1->v.val_unsigned == v2->v.val_unsigned;
7411     case dw_val_class_long_long:
7412       return v1->v.val_long_long.hi == v2->v.val_long_long.hi
7413              && v1->v.val_long_long.low == v2->v.val_long_long.low;
7414     case dw_val_class_vec:
7415       if (v1->v.val_vec.length != v2->v.val_vec.length
7416           || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
7417         return 0;
7418       if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
7419                   v1->v.val_vec.length * v1->v.val_vec.elt_size))
7420         return 0;
7421       return 1;
7422     case dw_val_class_flag:
7423       return v1->v.val_flag == v2->v.val_flag;
7424     case dw_val_class_str:
7425       return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
7426
7427     case dw_val_class_addr:
7428       r1 = v1->v.val_addr;
7429       r2 = v2->v.val_addr;
7430       if (GET_CODE (r1) != GET_CODE (r2))
7431         return 0;
7432       gcc_assert (GET_CODE (r1) == SYMBOL_REF);
7433       return !strcmp (XSTR (r1, 0), XSTR (r2, 0));
7434
7435     case dw_val_class_offset:
7436       return v1->v.val_offset == v2->v.val_offset;
7437
7438     case dw_val_class_loc:
7439       for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
7440            loc1 && loc2;
7441            loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
7442         if (!same_loc_p (loc1, loc2, mark))
7443           return 0;
7444       return !loc1 && !loc2;
7445
7446     case dw_val_class_die_ref:
7447       return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
7448
7449     case dw_val_class_fde_ref:
7450     case dw_val_class_lbl_id:
7451     case dw_val_class_lineptr:
7452     case dw_val_class_macptr:
7453       return 1;
7454
7455     case dw_val_class_file:
7456       return v1->v.val_file == v2->v.val_file;
7457
7458     default:
7459       return 1;
7460     }
7461 }
7462
7463 /* Do the attributes look the same?  */
7464
7465 static int
7466 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
7467 {
7468   if (at1->dw_attr != at2->dw_attr)
7469     return 0;
7470
7471   /* We don't care that this was compiled with a different compiler
7472      snapshot; if the output is the same, that's what matters. */
7473   if (at1->dw_attr == DW_AT_producer)
7474     return 1;
7475
7476   return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
7477 }
7478
7479 /* Do the dies look the same?  */
7480
7481 static int
7482 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
7483 {
7484   dw_die_ref c1, c2;
7485   dw_attr_ref a1;
7486   unsigned ix;
7487
7488   /* To avoid infinite recursion.  */
7489   if (die1->die_mark)
7490     return die1->die_mark == die2->die_mark;
7491   die1->die_mark = die2->die_mark = ++(*mark);
7492
7493   if (die1->die_tag != die2->die_tag)
7494     return 0;
7495
7496   if (VEC_length (dw_attr_node, die1->die_attr)
7497       != VEC_length (dw_attr_node, die2->die_attr))
7498     return 0;
7499
7500   for (ix = 0; VEC_iterate (dw_attr_node, die1->die_attr, ix, a1); ix++)
7501     if (!same_attr_p (a1, VEC_index (dw_attr_node, die2->die_attr, ix), mark))
7502       return 0;
7503
7504   c1 = die1->die_child;
7505   c2 = die2->die_child;
7506   if (! c1)
7507     {
7508       if (c2)
7509         return 0;
7510     }
7511   else
7512     for (;;)
7513       {
7514         if (!same_die_p (c1, c2, mark))
7515           return 0;
7516         c1 = c1->die_sib;
7517         c2 = c2->die_sib;
7518         if (c1 == die1->die_child)
7519           {
7520             if (c2 == die2->die_child)
7521               break;
7522             else
7523               return 0;
7524           }
7525     }
7526
7527   return 1;
7528 }
7529
7530 /* Do the dies look the same?  Wrapper around same_die_p.  */
7531
7532 static int
7533 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
7534 {
7535   int mark = 0;
7536   int ret = same_die_p (die1, die2, &mark);
7537
7538   unmark_all_dies (die1);
7539   unmark_all_dies (die2);
7540
7541   return ret;
7542 }
7543
7544 /* The prefix to attach to symbols on DIEs in the current comdat debug
7545    info section.  */
7546 static char *comdat_symbol_id;
7547
7548 /* The index of the current symbol within the current comdat CU.  */
7549 static unsigned int comdat_symbol_number;
7550
7551 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
7552    children, and set comdat_symbol_id accordingly.  */
7553
7554 static void
7555 compute_section_prefix (dw_die_ref unit_die)
7556 {
7557   const char *die_name = get_AT_string (unit_die, DW_AT_name);
7558   const char *base = die_name ? lbasename (die_name) : "anonymous";
7559   char *name = XALLOCAVEC (char, strlen (base) + 64);
7560   char *p;
7561   int i, mark;
7562   unsigned char checksum[16];
7563   struct md5_ctx ctx;
7564
7565   /* Compute the checksum of the DIE, then append part of it as hex digits to
7566      the name filename of the unit.  */
7567
7568   md5_init_ctx (&ctx);
7569   mark = 0;
7570   die_checksum (unit_die, &ctx, &mark);
7571   unmark_all_dies (unit_die);
7572   md5_finish_ctx (&ctx, checksum);
7573
7574   sprintf (name, "%s.", base);
7575   clean_symbol_name (name);
7576
7577   p = name + strlen (name);
7578   for (i = 0; i < 4; i++)
7579     {
7580       sprintf (p, "%.2x", checksum[i]);
7581       p += 2;
7582     }
7583
7584   comdat_symbol_id = unit_die->die_symbol = xstrdup (name);
7585   comdat_symbol_number = 0;
7586 }
7587
7588 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P.  */
7589
7590 static int
7591 is_type_die (dw_die_ref die)
7592 {
7593   switch (die->die_tag)
7594     {
7595     case DW_TAG_array_type:
7596     case DW_TAG_class_type:
7597     case DW_TAG_interface_type:
7598     case DW_TAG_enumeration_type:
7599     case DW_TAG_pointer_type:
7600     case DW_TAG_reference_type:
7601     case DW_TAG_string_type:
7602     case DW_TAG_structure_type:
7603     case DW_TAG_subroutine_type:
7604     case DW_TAG_union_type:
7605     case DW_TAG_ptr_to_member_type:
7606     case DW_TAG_set_type:
7607     case DW_TAG_subrange_type:
7608     case DW_TAG_base_type:
7609     case DW_TAG_const_type:
7610     case DW_TAG_file_type:
7611     case DW_TAG_packed_type:
7612     case DW_TAG_volatile_type:
7613     case DW_TAG_typedef:
7614       return 1;
7615     default:
7616       return 0;
7617     }
7618 }
7619
7620 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
7621    Basically, we want to choose the bits that are likely to be shared between
7622    compilations (types) and leave out the bits that are specific to individual
7623    compilations (functions).  */
7624
7625 static int
7626 is_comdat_die (dw_die_ref c)
7627 {
7628   /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
7629      we do for stabs.  The advantage is a greater likelihood of sharing between
7630      objects that don't include headers in the same order (and therefore would
7631      put the base types in a different comdat).  jason 8/28/00 */
7632
7633   if (c->die_tag == DW_TAG_base_type)
7634     return 0;
7635
7636   if (c->die_tag == DW_TAG_pointer_type
7637       || c->die_tag == DW_TAG_reference_type
7638       || c->die_tag == DW_TAG_const_type
7639       || c->die_tag == DW_TAG_volatile_type)
7640     {
7641       dw_die_ref t = get_AT_ref (c, DW_AT_type);
7642
7643       return t ? is_comdat_die (t) : 0;
7644     }
7645
7646   return is_type_die (c);
7647 }
7648
7649 /* Returns 1 iff C is the sort of DIE that might be referred to from another
7650    compilation unit.  */
7651
7652 static int
7653 is_symbol_die (dw_die_ref c)
7654 {
7655   return (is_type_die (c)
7656           || (get_AT (c, DW_AT_declaration)
7657               && !get_AT (c, DW_AT_specification))
7658           || c->die_tag == DW_TAG_namespace
7659           || c->die_tag == DW_TAG_module);
7660 }
7661
7662 static char *
7663 gen_internal_sym (const char *prefix)
7664 {
7665   char buf[256];
7666
7667   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
7668   return xstrdup (buf);
7669 }
7670
7671 /* Assign symbols to all worthy DIEs under DIE.  */
7672
7673 static void
7674 assign_symbol_names (dw_die_ref die)
7675 {
7676   dw_die_ref c;
7677
7678   if (is_symbol_die (die))
7679     {
7680       if (comdat_symbol_id)
7681         {
7682           char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
7683
7684           sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
7685                    comdat_symbol_id, comdat_symbol_number++);
7686           die->die_symbol = xstrdup (p);
7687         }
7688       else
7689         die->die_symbol = gen_internal_sym ("LDIE");
7690     }
7691
7692   FOR_EACH_CHILD (die, c, assign_symbol_names (c));
7693 }
7694
7695 struct cu_hash_table_entry
7696 {
7697   dw_die_ref cu;
7698   unsigned min_comdat_num, max_comdat_num;
7699   struct cu_hash_table_entry *next;
7700 };
7701
7702 /* Routines to manipulate hash table of CUs.  */
7703 static hashval_t
7704 htab_cu_hash (const void *of)
7705 {
7706   const struct cu_hash_table_entry *const entry =
7707     (const struct cu_hash_table_entry *) of;
7708
7709   return htab_hash_string (entry->cu->die_symbol);
7710 }
7711
7712 static int
7713 htab_cu_eq (const void *of1, const void *of2)
7714 {
7715   const struct cu_hash_table_entry *const entry1 =
7716     (const struct cu_hash_table_entry *) of1;
7717   const struct die_struct *const entry2 = (const struct die_struct *) of2;
7718
7719   return !strcmp (entry1->cu->die_symbol, entry2->die_symbol);
7720 }
7721
7722 static void
7723 htab_cu_del (void *what)
7724 {
7725   struct cu_hash_table_entry *next,
7726     *entry = (struct cu_hash_table_entry *) what;
7727
7728   while (entry)
7729     {
7730       next = entry->next;
7731       free (entry);
7732       entry = next;
7733     }
7734 }
7735
7736 /* Check whether we have already seen this CU and set up SYM_NUM
7737    accordingly.  */
7738 static int
7739 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
7740 {
7741   struct cu_hash_table_entry dummy;
7742   struct cu_hash_table_entry **slot, *entry, *last = &dummy;
7743
7744   dummy.max_comdat_num = 0;
7745
7746   slot = (struct cu_hash_table_entry **)
7747     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
7748         INSERT);
7749   entry = *slot;
7750
7751   for (; entry; last = entry, entry = entry->next)
7752     {
7753       if (same_die_p_wrap (cu, entry->cu))
7754         break;
7755     }
7756
7757   if (entry)
7758     {
7759       *sym_num = entry->min_comdat_num;
7760       return 1;
7761     }
7762
7763   entry = XCNEW (struct cu_hash_table_entry);
7764   entry->cu = cu;
7765   entry->min_comdat_num = *sym_num = last->max_comdat_num;
7766   entry->next = *slot;
7767   *slot = entry;
7768
7769   return 0;
7770 }
7771
7772 /* Record SYM_NUM to record of CU in HTABLE.  */
7773 static void
7774 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
7775 {
7776   struct cu_hash_table_entry **slot, *entry;
7777
7778   slot = (struct cu_hash_table_entry **)
7779     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
7780         NO_INSERT);
7781   entry = *slot;
7782
7783   entry->max_comdat_num = sym_num;
7784 }
7785
7786 /* Traverse the DIE (which is always comp_unit_die), and set up
7787    additional compilation units for each of the include files we see
7788    bracketed by BINCL/EINCL.  */
7789
7790 static void
7791 break_out_includes (dw_die_ref die)
7792 {
7793   dw_die_ref c;
7794   dw_die_ref unit = NULL;
7795   limbo_die_node *node, **pnode;
7796   htab_t cu_hash_table;
7797
7798   c = die->die_child;
7799   if (c) do {
7800     dw_die_ref prev = c;
7801     c = c->die_sib;
7802     while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
7803            || (unit && is_comdat_die (c)))
7804       {
7805         dw_die_ref next = c->die_sib;
7806
7807         /* This DIE is for a secondary CU; remove it from the main one.  */
7808         remove_child_with_prev (c, prev);
7809
7810         if (c->die_tag == DW_TAG_GNU_BINCL)
7811           unit = push_new_compile_unit (unit, c);
7812         else if (c->die_tag == DW_TAG_GNU_EINCL)
7813           unit = pop_compile_unit (unit);
7814         else
7815           add_child_die (unit, c);
7816         c = next;
7817         if (c == die->die_child)
7818           break;
7819       }
7820   } while (c != die->die_child);
7821
7822 #if 0
7823   /* We can only use this in debugging, since the frontend doesn't check
7824      to make sure that we leave every include file we enter.  */
7825   gcc_assert (!unit);
7826 #endif
7827
7828   assign_symbol_names (die);
7829   cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
7830   for (node = limbo_die_list, pnode = &limbo_die_list;
7831        node;
7832        node = node->next)
7833     {
7834       int is_dupl;
7835
7836       compute_section_prefix (node->die);
7837       is_dupl = check_duplicate_cu (node->die, cu_hash_table,
7838                         &comdat_symbol_number);
7839       assign_symbol_names (node->die);
7840       if (is_dupl)
7841         *pnode = node->next;
7842       else
7843         {
7844           pnode = &node->next;
7845           record_comdat_symbol_number (node->die, cu_hash_table,
7846                 comdat_symbol_number);
7847         }
7848     }
7849   htab_delete (cu_hash_table);
7850 }
7851
7852 /* Traverse the DIE and add a sibling attribute if it may have the
7853    effect of speeding up access to siblings.  To save some space,
7854    avoid generating sibling attributes for DIE's without children.  */
7855
7856 static void
7857 add_sibling_attributes (dw_die_ref die)
7858 {
7859   dw_die_ref c;
7860
7861   if (! die->die_child)
7862     return;
7863
7864   if (die->die_parent && die != die->die_parent->die_child)
7865     add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
7866
7867   FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
7868 }
7869
7870 /* Output all location lists for the DIE and its children.  */
7871
7872 static void
7873 output_location_lists (dw_die_ref die)
7874 {
7875   dw_die_ref c;
7876   dw_attr_ref a;
7877   unsigned ix;
7878
7879   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7880     if (AT_class (a) == dw_val_class_loc_list)
7881       output_loc_list (AT_loc_list (a));
7882
7883   FOR_EACH_CHILD (die, c, output_location_lists (c));
7884 }
7885
7886 /* The format of each DIE (and its attribute value pairs) is encoded in an
7887    abbreviation table.  This routine builds the abbreviation table and assigns
7888    a unique abbreviation id for each abbreviation entry.  The children of each
7889    die are visited recursively.  */
7890
7891 static void
7892 build_abbrev_table (dw_die_ref die)
7893 {
7894   unsigned long abbrev_id;
7895   unsigned int n_alloc;
7896   dw_die_ref c;
7897   dw_attr_ref a;
7898   unsigned ix;
7899
7900   /* Scan the DIE references, and mark as external any that refer to
7901      DIEs from other CUs (i.e. those which are not marked).  */
7902   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7903     if (AT_class (a) == dw_val_class_die_ref
7904         && AT_ref (a)->die_mark == 0)
7905       {
7906         gcc_assert (AT_ref (a)->die_symbol);
7907         set_AT_ref_external (a, 1);
7908       }
7909
7910   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7911     {
7912       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7913       dw_attr_ref die_a, abbrev_a;
7914       unsigned ix;
7915       bool ok = true;
7916
7917       if (abbrev->die_tag != die->die_tag)
7918         continue;
7919       if ((abbrev->die_child != NULL) != (die->die_child != NULL))
7920         continue;
7921
7922       if (VEC_length (dw_attr_node, abbrev->die_attr)
7923           != VEC_length (dw_attr_node, die->die_attr))
7924         continue;
7925
7926       for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, die_a); ix++)
7927         {
7928           abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
7929           if ((abbrev_a->dw_attr != die_a->dw_attr)
7930               || (value_format (abbrev_a) != value_format (die_a)))
7931             {
7932               ok = false;
7933               break;
7934             }
7935         }
7936       if (ok)
7937         break;
7938     }
7939
7940   if (abbrev_id >= abbrev_die_table_in_use)
7941     {
7942       if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
7943         {
7944           n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
7945           abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
7946                                             n_alloc);
7947
7948           memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
7949                  (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
7950           abbrev_die_table_allocated = n_alloc;
7951         }
7952
7953       ++abbrev_die_table_in_use;
7954       abbrev_die_table[abbrev_id] = die;
7955     }
7956
7957   die->die_abbrev = abbrev_id;
7958   FOR_EACH_CHILD (die, c, build_abbrev_table (c));
7959 }
7960 \f
7961 /* Return the power-of-two number of bytes necessary to represent VALUE.  */
7962
7963 static int
7964 constant_size (unsigned HOST_WIDE_INT value)
7965 {
7966   int log;
7967
7968   if (value == 0)
7969     log = 0;
7970   else
7971     log = floor_log2 (value);
7972
7973   log = log / 8;
7974   log = 1 << (floor_log2 (log) + 1);
7975
7976   return log;
7977 }
7978
7979 /* Return the size of a DIE as it is represented in the
7980    .debug_info section.  */
7981
7982 static unsigned long
7983 size_of_die (dw_die_ref die)
7984 {
7985   unsigned long size = 0;
7986   dw_attr_ref a;
7987   unsigned ix;
7988
7989   size += size_of_uleb128 (die->die_abbrev);
7990   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7991     {
7992       switch (AT_class (a))
7993         {
7994         case dw_val_class_addr:
7995           size += DWARF2_ADDR_SIZE;
7996           break;
7997         case dw_val_class_offset:
7998           size += DWARF_OFFSET_SIZE;
7999           break;
8000         case dw_val_class_loc:
8001           {
8002             unsigned long lsize = size_of_locs (AT_loc (a));
8003
8004             /* Block length.  */
8005             size += constant_size (lsize);
8006             size += lsize;
8007           }
8008           break;
8009         case dw_val_class_loc_list:
8010           size += DWARF_OFFSET_SIZE;
8011           break;
8012         case dw_val_class_range_list:
8013           size += DWARF_OFFSET_SIZE;
8014           break;
8015         case dw_val_class_const:
8016           size += size_of_sleb128 (AT_int (a));
8017           break;
8018         case dw_val_class_unsigned_const:
8019           size += constant_size (AT_unsigned (a));
8020           break;
8021         case dw_val_class_long_long:
8022           size += 1 + 2*HOST_BITS_PER_LONG/HOST_BITS_PER_CHAR; /* block */
8023           break;
8024         case dw_val_class_vec:
8025           size += constant_size (a->dw_attr_val.v.val_vec.length
8026                                  * a->dw_attr_val.v.val_vec.elt_size)
8027                   + a->dw_attr_val.v.val_vec.length
8028                     * a->dw_attr_val.v.val_vec.elt_size; /* block */
8029           break;
8030         case dw_val_class_flag:
8031           size += 1;
8032           break;
8033         case dw_val_class_die_ref:
8034           /* In DWARF2, DW_FORM_ref_addr is sized by target address length,
8035              whereas in DWARF3 it's always sized as an offset.  */
8036           if (AT_ref_external (a) && dwarf_version == 2)
8037             size += DWARF2_ADDR_SIZE;
8038           else
8039             size += DWARF_OFFSET_SIZE;
8040           break;
8041         case dw_val_class_fde_ref:
8042           size += DWARF_OFFSET_SIZE;
8043           break;
8044         case dw_val_class_lbl_id:
8045           size += DWARF2_ADDR_SIZE;
8046           break;
8047         case dw_val_class_lineptr:
8048         case dw_val_class_macptr:
8049           size += DWARF_OFFSET_SIZE;
8050           break;
8051         case dw_val_class_str:
8052           if (AT_string_form (a) == DW_FORM_strp)
8053             size += DWARF_OFFSET_SIZE;
8054           else
8055             size += strlen (a->dw_attr_val.v.val_str->str) + 1;
8056           break;
8057         case dw_val_class_file:
8058           size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
8059           break;
8060         default:
8061           gcc_unreachable ();
8062         }
8063     }
8064
8065   return size;
8066 }
8067
8068 /* Size the debugging information associated with a given DIE.  Visits the
8069    DIE's children recursively.  Updates the global variable next_die_offset, on
8070    each time through.  Uses the current value of next_die_offset to update the
8071    die_offset field in each DIE.  */
8072
8073 static void
8074 calc_die_sizes (dw_die_ref die)
8075 {
8076   dw_die_ref c;
8077
8078   die->die_offset = next_die_offset;
8079   next_die_offset += size_of_die (die);
8080
8081   FOR_EACH_CHILD (die, c, calc_die_sizes (c));
8082
8083   if (die->die_child != NULL)
8084     /* Count the null byte used to terminate sibling lists.  */
8085     next_die_offset += 1;
8086 }
8087
8088 /* Set the marks for a die and its children.  We do this so
8089    that we know whether or not a reference needs to use FORM_ref_addr; only
8090    DIEs in the same CU will be marked.  We used to clear out the offset
8091    and use that as the flag, but ran into ordering problems.  */
8092
8093 static void
8094 mark_dies (dw_die_ref die)
8095 {
8096   dw_die_ref c;
8097
8098   gcc_assert (!die->die_mark);
8099
8100   die->die_mark = 1;
8101   FOR_EACH_CHILD (die, c, mark_dies (c));
8102 }
8103
8104 /* Clear the marks for a die and its children.  */
8105
8106 static void
8107 unmark_dies (dw_die_ref die)
8108 {
8109   dw_die_ref c;
8110
8111   gcc_assert (die->die_mark);
8112
8113   die->die_mark = 0;
8114   FOR_EACH_CHILD (die, c, unmark_dies (c));
8115 }
8116
8117 /* Clear the marks for a die, its children and referred dies.  */
8118
8119 static void
8120 unmark_all_dies (dw_die_ref die)
8121 {
8122   dw_die_ref c;
8123   dw_attr_ref a;
8124   unsigned ix;
8125
8126   if (!die->die_mark)
8127     return;
8128   die->die_mark = 0;
8129
8130   FOR_EACH_CHILD (die, c, unmark_all_dies (c));
8131
8132   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
8133     if (AT_class (a) == dw_val_class_die_ref)
8134       unmark_all_dies (AT_ref (a));
8135 }
8136
8137 /* Return the size of the .debug_pubnames or .debug_pubtypes table
8138    generated for the compilation unit.  */
8139
8140 static unsigned long
8141 size_of_pubnames (VEC (pubname_entry, gc) * names)
8142 {
8143   unsigned long size;
8144   unsigned i;
8145   pubname_ref p;
8146
8147   size = DWARF_PUBNAMES_HEADER_SIZE;
8148   for (i = 0; VEC_iterate (pubname_entry, names, i, p); i++)
8149     if (names != pubtype_table
8150         || p->die->die_offset != 0
8151         || !flag_eliminate_unused_debug_types)
8152       size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
8153
8154   size += DWARF_OFFSET_SIZE;
8155   return size;
8156 }
8157
8158 /* Return the size of the information in the .debug_aranges section.  */
8159
8160 static unsigned long
8161 size_of_aranges (void)
8162 {
8163   unsigned long size;
8164
8165   size = DWARF_ARANGES_HEADER_SIZE;
8166
8167   /* Count the address/length pair for this compilation unit.  */
8168   if (text_section_used)
8169     size += 2 * DWARF2_ADDR_SIZE;
8170   if (cold_text_section_used)
8171     size += 2 * DWARF2_ADDR_SIZE;
8172   size += 2 * DWARF2_ADDR_SIZE * arange_table_in_use;
8173
8174   /* Count the two zero words used to terminated the address range table.  */
8175   size += 2 * DWARF2_ADDR_SIZE;
8176   return size;
8177 }
8178 \f
8179 /* Select the encoding of an attribute value.  */
8180
8181 static enum dwarf_form
8182 value_format (dw_attr_ref a)
8183 {
8184   switch (a->dw_attr_val.val_class)
8185     {
8186     case dw_val_class_addr:
8187       return DW_FORM_addr;
8188     case dw_val_class_range_list:
8189     case dw_val_class_offset:
8190     case dw_val_class_loc_list:
8191       switch (DWARF_OFFSET_SIZE)
8192         {
8193         case 4:
8194           return DW_FORM_data4;
8195         case 8:
8196           return DW_FORM_data8;
8197         default:
8198           gcc_unreachable ();
8199         }
8200     case dw_val_class_loc:
8201       switch (constant_size (size_of_locs (AT_loc (a))))
8202         {
8203         case 1:
8204           return DW_FORM_block1;
8205         case 2:
8206           return DW_FORM_block2;
8207         default:
8208           gcc_unreachable ();
8209         }
8210     case dw_val_class_const:
8211       return DW_FORM_sdata;
8212     case dw_val_class_unsigned_const:
8213       switch (constant_size (AT_unsigned (a)))
8214         {
8215         case 1:
8216           return DW_FORM_data1;
8217         case 2:
8218           return DW_FORM_data2;
8219         case 4:
8220           return DW_FORM_data4;
8221         case 8:
8222           return DW_FORM_data8;
8223         default:
8224           gcc_unreachable ();
8225         }
8226     case dw_val_class_long_long:
8227       return DW_FORM_block1;
8228     case dw_val_class_vec:
8229       switch (constant_size (a->dw_attr_val.v.val_vec.length
8230                              * a->dw_attr_val.v.val_vec.elt_size))
8231         {
8232         case 1:
8233           return DW_FORM_block1;
8234         case 2:
8235           return DW_FORM_block2;
8236         case 4:
8237           return DW_FORM_block4;
8238         default:
8239           gcc_unreachable ();
8240         }
8241     case dw_val_class_flag:
8242       return DW_FORM_flag;
8243     case dw_val_class_die_ref:
8244       if (AT_ref_external (a))
8245         return DW_FORM_ref_addr;
8246       else
8247         return DW_FORM_ref;
8248     case dw_val_class_fde_ref:
8249       return DW_FORM_data;
8250     case dw_val_class_lbl_id:
8251       return DW_FORM_addr;
8252     case dw_val_class_lineptr:
8253     case dw_val_class_macptr:
8254       return DW_FORM_data;
8255     case dw_val_class_str:
8256       return AT_string_form (a);
8257     case dw_val_class_file:
8258       switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
8259         {
8260         case 1:
8261           return DW_FORM_data1;
8262         case 2:
8263           return DW_FORM_data2;
8264         case 4:
8265           return DW_FORM_data4;
8266         default:
8267           gcc_unreachable ();
8268         }
8269
8270     default:
8271       gcc_unreachable ();
8272     }
8273 }
8274
8275 /* Output the encoding of an attribute value.  */
8276
8277 static void
8278 output_value_format (dw_attr_ref a)
8279 {
8280   enum dwarf_form form = value_format (a);
8281
8282   dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
8283 }
8284
8285 /* Output the .debug_abbrev section which defines the DIE abbreviation
8286    table.  */
8287
8288 static void
8289 output_abbrev_section (void)
8290 {
8291   unsigned long abbrev_id;
8292
8293   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8294     {
8295       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
8296       unsigned ix;
8297       dw_attr_ref a_attr;
8298
8299       dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
8300       dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
8301                                    dwarf_tag_name (abbrev->die_tag));
8302
8303       if (abbrev->die_child != NULL)
8304         dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
8305       else
8306         dw2_asm_output_data (1, DW_children_no, "DW_children_no");
8307
8308       for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
8309            ix++)
8310         {
8311           dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
8312                                        dwarf_attr_name (a_attr->dw_attr));
8313           output_value_format (a_attr);
8314         }
8315
8316       dw2_asm_output_data (1, 0, NULL);
8317       dw2_asm_output_data (1, 0, NULL);
8318     }
8319
8320   /* Terminate the table.  */
8321   dw2_asm_output_data (1, 0, NULL);
8322 }
8323
8324 /* Output a symbol we can use to refer to this DIE from another CU.  */
8325
8326 static inline void
8327 output_die_symbol (dw_die_ref die)
8328 {
8329   char *sym = die->die_symbol;
8330
8331   if (sym == 0)
8332     return;
8333
8334   if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
8335     /* We make these global, not weak; if the target doesn't support
8336        .linkonce, it doesn't support combining the sections, so debugging
8337        will break.  */
8338     targetm.asm_out.globalize_label (asm_out_file, sym);
8339
8340   ASM_OUTPUT_LABEL (asm_out_file, sym);
8341 }
8342
8343 /* Return a new location list, given the begin and end range, and the
8344    expression. gensym tells us whether to generate a new internal symbol for
8345    this location list node, which is done for the head of the list only.  */
8346
8347 static inline dw_loc_list_ref
8348 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
8349               const char *section, unsigned int gensym)
8350 {
8351   dw_loc_list_ref retlist = GGC_CNEW (dw_loc_list_node);
8352
8353   retlist->begin = begin;
8354   retlist->end = end;
8355   retlist->expr = expr;
8356   retlist->section = section;
8357   if (gensym)
8358     retlist->ll_symbol = gen_internal_sym ("LLST");
8359
8360   return retlist;
8361 }
8362
8363 /* Add a location description expression to a location list.  */
8364
8365 static inline void
8366 add_loc_descr_to_loc_list (dw_loc_list_ref *list_head, dw_loc_descr_ref descr,
8367                            const char *begin, const char *end,
8368                            const char *section)
8369 {
8370   dw_loc_list_ref *d;
8371
8372   /* Find the end of the chain.  */
8373   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
8374     ;
8375
8376   /* Add a new location list node to the list.  */
8377   *d = new_loc_list (descr, begin, end, section, 0);
8378 }
8379
8380 /* Output the location list given to us.  */
8381
8382 static void
8383 output_loc_list (dw_loc_list_ref list_head)
8384 {
8385   dw_loc_list_ref curr = list_head;
8386
8387   ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
8388
8389   /* Walk the location list, and output each range + expression.  */
8390   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
8391     {
8392       unsigned long size;
8393       /* Don't output an entry that starts and ends at the same address.  */
8394       if (strcmp (curr->begin, curr->end) == 0)
8395         continue;
8396       if (!have_multiple_function_sections)
8397         {
8398           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
8399                                 "Location list begin address (%s)",
8400                                 list_head->ll_symbol);
8401           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
8402                                 "Location list end address (%s)",
8403                                 list_head->ll_symbol);
8404         }
8405       else
8406         {
8407           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
8408                                "Location list begin address (%s)",
8409                                list_head->ll_symbol);
8410           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
8411                                "Location list end address (%s)",
8412                                list_head->ll_symbol);
8413         }
8414       size = size_of_locs (curr->expr);
8415
8416       /* Output the block length for this list of location operations.  */
8417       gcc_assert (size <= 0xffff);
8418       dw2_asm_output_data (2, size, "%s", "Location expression size");
8419
8420       output_loc_sequence (curr->expr);
8421     }
8422
8423   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8424                        "Location list terminator begin (%s)",
8425                        list_head->ll_symbol);
8426   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8427                        "Location list terminator end (%s)",
8428                        list_head->ll_symbol);
8429 }
8430
8431 /* Output the DIE and its attributes.  Called recursively to generate
8432    the definitions of each child DIE.  */
8433
8434 static void
8435 output_die (dw_die_ref die)
8436 {
8437   dw_attr_ref a;
8438   dw_die_ref c;
8439   unsigned long size;
8440   unsigned ix;
8441
8442   /* If someone in another CU might refer to us, set up a symbol for
8443      them to point to.  */
8444   if (die->die_symbol)
8445     output_die_symbol (die);
8446
8447   dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (0x%lx) %s)",
8448                                (unsigned long)die->die_offset,
8449                                dwarf_tag_name (die->die_tag));
8450
8451   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
8452     {
8453       const char *name = dwarf_attr_name (a->dw_attr);
8454
8455       switch (AT_class (a))
8456         {
8457         case dw_val_class_addr:
8458           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8459           break;
8460
8461         case dw_val_class_offset:
8462           dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8463                                "%s", name);
8464           break;
8465
8466         case dw_val_class_range_list:
8467           {
8468             char *p = strchr (ranges_section_label, '\0');
8469
8470             sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
8471                      a->dw_attr_val.v.val_offset);
8472             dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8473                                    debug_ranges_section, "%s", name);
8474             *p = '\0';
8475           }
8476           break;
8477
8478         case dw_val_class_loc:
8479           size = size_of_locs (AT_loc (a));
8480
8481           /* Output the block length for this list of location operations.  */
8482           dw2_asm_output_data (constant_size (size), size, "%s", name);
8483
8484           output_loc_sequence (AT_loc (a));
8485           break;
8486
8487         case dw_val_class_const:
8488           /* ??? It would be slightly more efficient to use a scheme like is
8489              used for unsigned constants below, but gdb 4.x does not sign
8490              extend.  Gdb 5.x does sign extend.  */
8491           dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8492           break;
8493
8494         case dw_val_class_unsigned_const:
8495           dw2_asm_output_data (constant_size (AT_unsigned (a)),
8496                                AT_unsigned (a), "%s", name);
8497           break;
8498
8499         case dw_val_class_long_long:
8500           {
8501             unsigned HOST_WIDE_INT first, second;
8502
8503             dw2_asm_output_data (1,
8504                                  2 * HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
8505                                  "%s", name);
8506
8507             if (WORDS_BIG_ENDIAN)
8508               {
8509                 first = a->dw_attr_val.v.val_long_long.hi;
8510                 second = a->dw_attr_val.v.val_long_long.low;
8511               }
8512             else
8513               {
8514                 first = a->dw_attr_val.v.val_long_long.low;
8515                 second = a->dw_attr_val.v.val_long_long.hi;
8516               }
8517
8518             dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
8519                                  first, "long long constant");
8520             dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
8521                                  second, NULL);
8522           }
8523           break;
8524
8525         case dw_val_class_vec:
8526           {
8527             unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
8528             unsigned int len = a->dw_attr_val.v.val_vec.length;
8529             unsigned int i;
8530             unsigned char *p;
8531
8532             dw2_asm_output_data (constant_size (len * elt_size),
8533                                  len * elt_size, "%s", name);
8534             if (elt_size > sizeof (HOST_WIDE_INT))
8535               {
8536                 elt_size /= 2;
8537                 len *= 2;
8538               }
8539             for (i = 0, p = a->dw_attr_val.v.val_vec.array;
8540                  i < len;
8541                  i++, p += elt_size)
8542               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
8543                                    "fp or vector constant word %u", i);
8544             break;
8545           }
8546
8547         case dw_val_class_flag:
8548           dw2_asm_output_data (1, AT_flag (a), "%s", name);
8549           break;
8550
8551         case dw_val_class_loc_list:
8552           {
8553             char *sym = AT_loc_list (a)->ll_symbol;
8554
8555             gcc_assert (sym);
8556             dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8557                                    "%s", name);
8558           }
8559           break;
8560
8561         case dw_val_class_die_ref:
8562           if (AT_ref_external (a))
8563             {
8564               char *sym = AT_ref (a)->die_symbol;
8565               int size;
8566
8567               gcc_assert (sym);
8568
8569               /* In DWARF2, DW_FORM_ref_addr is sized by target address
8570                  length, whereas in DWARF3 it's always sized as an offset.  */
8571               if (dwarf_version == 2)
8572                 size = DWARF2_ADDR_SIZE;
8573               else
8574                 size = DWARF_OFFSET_SIZE;
8575               dw2_asm_output_offset (size, sym, debug_info_section, "%s", name);
8576             }
8577           else
8578             {
8579               gcc_assert (AT_ref (a)->die_offset);
8580               dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
8581                                    "%s", name);
8582             }
8583           break;
8584
8585         case dw_val_class_fde_ref:
8586           {
8587             char l1[20];
8588
8589             ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
8590                                          a->dw_attr_val.v.val_fde_index * 2);
8591             dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
8592                                    "%s", name);
8593           }
8594           break;
8595
8596         case dw_val_class_lbl_id:
8597           dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8598           break;
8599
8600         case dw_val_class_lineptr:
8601           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8602                                  debug_line_section, "%s", name);
8603           break;
8604
8605         case dw_val_class_macptr:
8606           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8607                                  debug_macinfo_section, "%s", name);
8608           break;
8609
8610         case dw_val_class_str:
8611           if (AT_string_form (a) == DW_FORM_strp)
8612             dw2_asm_output_offset (DWARF_OFFSET_SIZE,
8613                                    a->dw_attr_val.v.val_str->label,
8614                                    debug_str_section,
8615                                    "%s: \"%s\"", name, AT_string (a));
8616           else
8617             dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
8618           break;
8619
8620         case dw_val_class_file:
8621           {
8622             int f = maybe_emit_file (a->dw_attr_val.v.val_file);
8623
8624             dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
8625                                  a->dw_attr_val.v.val_file->filename);
8626             break;
8627           }
8628
8629         default:
8630           gcc_unreachable ();
8631         }
8632     }
8633
8634   FOR_EACH_CHILD (die, c, output_die (c));
8635
8636   /* Add null byte to terminate sibling list.  */
8637   if (die->die_child != NULL)
8638     dw2_asm_output_data (1, 0, "end of children of DIE 0x%lx",
8639                          (unsigned long) die->die_offset);
8640 }
8641
8642 /* Output the compilation unit that appears at the beginning of the
8643    .debug_info section, and precedes the DIE descriptions.  */
8644
8645 static void
8646 output_compilation_unit_header (void)
8647 {
8648   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8649     dw2_asm_output_data (4, 0xffffffff,
8650       "Initial length escape value indicating 64-bit DWARF extension");
8651   dw2_asm_output_data (DWARF_OFFSET_SIZE,
8652                        next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
8653                        "Length of Compilation Unit Info");
8654   dw2_asm_output_data (2, dwarf_version, "DWARF version number");
8655   dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
8656                          debug_abbrev_section,
8657                          "Offset Into Abbrev. Section");
8658   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8659 }
8660
8661 /* Output the compilation unit DIE and its children.  */
8662
8663 static void
8664 output_comp_unit (dw_die_ref die, int output_if_empty)
8665 {
8666   const char *secname;
8667   char *oldsym, *tmp;
8668
8669   /* Unless we are outputting main CU, we may throw away empty ones.  */
8670   if (!output_if_empty && die->die_child == NULL)
8671     return;
8672
8673   /* Even if there are no children of this DIE, we must output the information
8674      about the compilation unit.  Otherwise, on an empty translation unit, we
8675      will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
8676      will then complain when examining the file.  First mark all the DIEs in
8677      this CU so we know which get local refs.  */
8678   mark_dies (die);
8679
8680   build_abbrev_table (die);
8681
8682   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
8683   next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8684   calc_die_sizes (die);
8685
8686   oldsym = die->die_symbol;
8687   if (oldsym)
8688     {
8689       tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
8690
8691       sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
8692       secname = tmp;
8693       die->die_symbol = NULL;
8694       switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
8695     }
8696   else
8697     switch_to_section (debug_info_section);
8698
8699   /* Output debugging information.  */
8700   output_compilation_unit_header ();
8701   output_die (die);
8702
8703   /* Leave the marks on the main CU, so we can check them in
8704      output_pubnames.  */
8705   if (oldsym)
8706     {
8707       unmark_dies (die);
8708       die->die_symbol = oldsym;
8709     }
8710 }
8711
8712 /* Return the DWARF2/3 pubname associated with a decl.  */
8713
8714 static const char *
8715 dwarf2_name (tree decl, int scope)
8716 {
8717   return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
8718 }
8719
8720 /* Add a new entry to .debug_pubnames if appropriate.  */
8721
8722 static void
8723 add_pubname_string (const char *str, dw_die_ref die)
8724 {
8725   pubname_entry e;
8726
8727   e.die = die;
8728   e.name = xstrdup (str);
8729   VEC_safe_push (pubname_entry, gc, pubname_table, &e);
8730 }
8731
8732 static void
8733 add_pubname (tree decl, dw_die_ref die)
8734 {
8735   if (TREE_PUBLIC (decl))
8736     add_pubname_string (dwarf2_name (decl, 1), die);
8737 }
8738
8739 /* Add a new entry to .debug_pubtypes if appropriate.  */
8740
8741 static void
8742 add_pubtype (tree decl, dw_die_ref die)
8743 {
8744   pubname_entry e;
8745
8746   e.name = NULL;
8747   if ((TREE_PUBLIC (decl)
8748        || die->die_parent == comp_unit_die)
8749       && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
8750     {
8751       e.die = die;
8752       if (TYPE_P (decl))
8753         {
8754           if (TYPE_NAME (decl))
8755             {
8756               if (TREE_CODE (TYPE_NAME (decl)) == IDENTIFIER_NODE)
8757                 e.name = IDENTIFIER_POINTER (TYPE_NAME (decl));
8758               else if (TREE_CODE (TYPE_NAME (decl)) == TYPE_DECL
8759                        && DECL_NAME (TYPE_NAME (decl)))
8760                 e.name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (decl)));
8761               else
8762                e.name = xstrdup ((const char *) get_AT_string (die, DW_AT_name));
8763             }
8764         }
8765       else
8766         e.name = xstrdup (dwarf2_name (decl, 1));
8767
8768       /* If we don't have a name for the type, there's no point in adding
8769          it to the table.  */
8770       if (e.name && e.name[0] != '\0')
8771         VEC_safe_push (pubname_entry, gc, pubtype_table, &e);
8772     }
8773 }
8774
8775 /* Output the public names table used to speed up access to externally
8776    visible names; or the public types table used to find type definitions.  */
8777
8778 static void
8779 output_pubnames (VEC (pubname_entry, gc) * names)
8780 {
8781   unsigned i;
8782   unsigned long pubnames_length = size_of_pubnames (names);
8783   pubname_ref pub;
8784
8785   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8786     dw2_asm_output_data (4, 0xffffffff,
8787       "Initial length escape value indicating 64-bit DWARF extension");
8788   if (names == pubname_table)
8789     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
8790                          "Length of Public Names Info");
8791   else
8792     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
8793                          "Length of Public Type Names Info");
8794   /* Version number for pubnames/pubtypes is still 2, even in DWARF3.  */
8795   dw2_asm_output_data (2, 2, "DWARF Version");
8796   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
8797                          debug_info_section,
8798                          "Offset of Compilation Unit Info");
8799   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
8800                        "Compilation Unit Length");
8801
8802   for (i = 0; VEC_iterate (pubname_entry, names, i, pub); i++)
8803     {
8804       /* We shouldn't see pubnames for DIEs outside of the main CU.  */
8805       if (names == pubname_table)
8806         gcc_assert (pub->die->die_mark);
8807
8808       if (names != pubtype_table
8809           || pub->die->die_offset != 0
8810           || !flag_eliminate_unused_debug_types)
8811         {
8812           dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
8813                                "DIE offset");
8814
8815           dw2_asm_output_nstring (pub->name, -1, "external name");
8816         }
8817     }
8818
8819   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
8820 }
8821
8822 /* Add a new entry to .debug_aranges if appropriate.  */
8823
8824 static void
8825 add_arange (tree decl, dw_die_ref die)
8826 {
8827   if (! DECL_SECTION_NAME (decl))
8828     return;
8829
8830   if (arange_table_in_use == arange_table_allocated)
8831     {
8832       arange_table_allocated += ARANGE_TABLE_INCREMENT;
8833       arange_table = GGC_RESIZEVEC (dw_die_ref, arange_table,
8834                                     arange_table_allocated);
8835       memset (arange_table + arange_table_in_use, 0,
8836               ARANGE_TABLE_INCREMENT * sizeof (dw_die_ref));
8837     }
8838
8839   arange_table[arange_table_in_use++] = die;
8840 }
8841
8842 /* Output the information that goes into the .debug_aranges table.
8843    Namely, define the beginning and ending address range of the
8844    text section generated for this compilation unit.  */
8845
8846 static void
8847 output_aranges (void)
8848 {
8849   unsigned i;
8850   unsigned long aranges_length = size_of_aranges ();
8851
8852   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8853     dw2_asm_output_data (4, 0xffffffff,
8854       "Initial length escape value indicating 64-bit DWARF extension");
8855   dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
8856                        "Length of Address Ranges Info");
8857   /* Version number for aranges is still 2, even in DWARF3.  */
8858   dw2_asm_output_data (2, 2, "DWARF Version");
8859   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
8860                          debug_info_section,
8861                          "Offset of Compilation Unit Info");
8862   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
8863   dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
8864
8865   /* We need to align to twice the pointer size here.  */
8866   if (DWARF_ARANGES_PAD_SIZE)
8867     {
8868       /* Pad using a 2 byte words so that padding is correct for any
8869          pointer size.  */
8870       dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
8871                            2 * DWARF2_ADDR_SIZE);
8872       for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
8873         dw2_asm_output_data (2, 0, NULL);
8874     }
8875
8876   /* It is necessary not to output these entries if the sections were
8877      not used; if the sections were not used, the length will be 0 and
8878      the address may end up as 0 if the section is discarded by ld
8879      --gc-sections, leaving an invalid (0, 0) entry that can be
8880      confused with the terminator.  */
8881   if (text_section_used)
8882     {
8883       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
8884       dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
8885                             text_section_label, "Length");
8886     }
8887   if (cold_text_section_used)
8888     {
8889       dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
8890                            "Address");
8891       dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
8892                             cold_text_section_label, "Length");
8893     }
8894
8895   for (i = 0; i < arange_table_in_use; i++)
8896     {
8897       dw_die_ref die = arange_table[i];
8898
8899       /* We shouldn't see aranges for DIEs outside of the main CU.  */
8900       gcc_assert (die->die_mark);
8901
8902       if (die->die_tag == DW_TAG_subprogram)
8903         {
8904           dw2_asm_output_addr (DWARF2_ADDR_SIZE, get_AT_low_pc (die),
8905                                "Address");
8906           dw2_asm_output_delta (DWARF2_ADDR_SIZE, get_AT_hi_pc (die),
8907                                 get_AT_low_pc (die), "Length");
8908         }
8909       else
8910         {
8911           /* A static variable; extract the symbol from DW_AT_location.
8912              Note that this code isn't currently hit, as we only emit
8913              aranges for functions (jason 9/23/99).  */
8914           dw_attr_ref a = get_AT (die, DW_AT_location);
8915           dw_loc_descr_ref loc;
8916
8917           gcc_assert (a && AT_class (a) == dw_val_class_loc);
8918
8919           loc = AT_loc (a);
8920           gcc_assert (loc->dw_loc_opc == DW_OP_addr);
8921
8922           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE,
8923                                    loc->dw_loc_oprnd1.v.val_addr, "Address");
8924           dw2_asm_output_data (DWARF2_ADDR_SIZE,
8925                                get_AT_unsigned (die, DW_AT_byte_size),
8926                                "Length");
8927         }
8928     }
8929
8930   /* Output the terminator words.  */
8931   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8932   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8933 }
8934
8935 /* Add a new entry to .debug_ranges.  Return the offset at which it
8936    was placed.  */
8937
8938 static unsigned int
8939 add_ranges_num (int num)
8940 {
8941   unsigned int in_use = ranges_table_in_use;
8942
8943   if (in_use == ranges_table_allocated)
8944     {
8945       ranges_table_allocated += RANGES_TABLE_INCREMENT;
8946       ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
8947                                     ranges_table_allocated);
8948       memset (ranges_table + ranges_table_in_use, 0,
8949               RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
8950     }
8951
8952   ranges_table[in_use].num = num;
8953   ranges_table_in_use = in_use + 1;
8954
8955   return in_use * 2 * DWARF2_ADDR_SIZE;
8956 }
8957
8958 /* Add a new entry to .debug_ranges corresponding to a block, or a
8959    range terminator if BLOCK is NULL.  */
8960
8961 static unsigned int
8962 add_ranges (const_tree block)
8963 {
8964   return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
8965 }
8966
8967 /* Add a new entry to .debug_ranges corresponding to a pair of
8968    labels.  */
8969
8970 static unsigned int
8971 add_ranges_by_labels (const char *begin, const char *end)
8972 {
8973   unsigned int in_use = ranges_by_label_in_use;
8974
8975   if (in_use == ranges_by_label_allocated)
8976     {
8977       ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
8978       ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
8979                                        ranges_by_label,
8980                                        ranges_by_label_allocated);
8981       memset (ranges_by_label + ranges_by_label_in_use, 0,
8982               RANGES_TABLE_INCREMENT
8983               * sizeof (struct dw_ranges_by_label_struct));
8984     }
8985
8986   ranges_by_label[in_use].begin = begin;
8987   ranges_by_label[in_use].end = end;
8988   ranges_by_label_in_use = in_use + 1;
8989
8990   return add_ranges_num (-(int)in_use - 1);
8991 }
8992
8993 static void
8994 output_ranges (void)
8995 {
8996   unsigned i;
8997   static const char *const start_fmt = "Offset 0x%x";
8998   const char *fmt = start_fmt;
8999
9000   for (i = 0; i < ranges_table_in_use; i++)
9001     {
9002       int block_num = ranges_table[i].num;
9003
9004       if (block_num > 0)
9005         {
9006           char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
9007           char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
9008
9009           ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
9010           ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
9011
9012           /* If all code is in the text section, then the compilation
9013              unit base address defaults to DW_AT_low_pc, which is the
9014              base of the text section.  */
9015           if (!have_multiple_function_sections)
9016             {
9017               dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
9018                                     text_section_label,
9019                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
9020               dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
9021                                     text_section_label, NULL);
9022             }
9023
9024           /* Otherwise, the compilation unit base address is zero,
9025              which allows us to use absolute addresses, and not worry
9026              about whether the target supports cross-section
9027              arithmetic.  */
9028           else
9029             {
9030               dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
9031                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
9032               dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
9033             }
9034
9035           fmt = NULL;
9036         }
9037
9038       /* Negative block_num stands for an index into ranges_by_label.  */
9039       else if (block_num < 0)
9040         {
9041           int lab_idx = - block_num - 1;
9042
9043           if (!have_multiple_function_sections)
9044             {
9045               gcc_unreachable ();
9046 #if 0
9047               /* If we ever use add_ranges_by_labels () for a single
9048                  function section, all we have to do is to take out
9049                  the #if 0 above.  */
9050               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9051                                     ranges_by_label[lab_idx].begin,
9052                                     text_section_label,
9053                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
9054               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9055                                     ranges_by_label[lab_idx].end,
9056                                     text_section_label, NULL);
9057 #endif
9058             }
9059           else
9060             {
9061               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9062                                    ranges_by_label[lab_idx].begin,
9063                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
9064               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9065                                    ranges_by_label[lab_idx].end,
9066                                    NULL);
9067             }
9068         }
9069       else
9070         {
9071           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9072           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9073           fmt = start_fmt;
9074         }
9075     }
9076 }
9077
9078 /* Data structure containing information about input files.  */
9079 struct file_info
9080 {
9081   const char *path;     /* Complete file name.  */
9082   const char *fname;    /* File name part.  */
9083   int length;           /* Length of entire string.  */
9084   struct dwarf_file_data * file_idx;    /* Index in input file table.  */
9085   int dir_idx;          /* Index in directory table.  */
9086 };
9087
9088 /* Data structure containing information about directories with source
9089    files.  */
9090 struct dir_info
9091 {
9092   const char *path;     /* Path including directory name.  */
9093   int length;           /* Path length.  */
9094   int prefix;           /* Index of directory entry which is a prefix.  */
9095   int count;            /* Number of files in this directory.  */
9096   int dir_idx;          /* Index of directory used as base.  */
9097 };
9098
9099 /* Callback function for file_info comparison.  We sort by looking at
9100    the directories in the path.  */
9101
9102 static int
9103 file_info_cmp (const void *p1, const void *p2)
9104 {
9105   const struct file_info *const s1 = (const struct file_info *) p1;
9106   const struct file_info *const s2 = (const struct file_info *) p2;
9107   const unsigned char *cp1;
9108   const unsigned char *cp2;
9109
9110   /* Take care of file names without directories.  We need to make sure that
9111      we return consistent values to qsort since some will get confused if
9112      we return the same value when identical operands are passed in opposite
9113      orders.  So if neither has a directory, return 0 and otherwise return
9114      1 or -1 depending on which one has the directory.  */
9115   if ((s1->path == s1->fname || s2->path == s2->fname))
9116     return (s2->path == s2->fname) - (s1->path == s1->fname);
9117
9118   cp1 = (const unsigned char *) s1->path;
9119   cp2 = (const unsigned char *) s2->path;
9120
9121   while (1)
9122     {
9123       ++cp1;
9124       ++cp2;
9125       /* Reached the end of the first path?  If so, handle like above.  */
9126       if ((cp1 == (const unsigned char *) s1->fname)
9127           || (cp2 == (const unsigned char *) s2->fname))
9128         return ((cp2 == (const unsigned char *) s2->fname)
9129                 - (cp1 == (const unsigned char *) s1->fname));
9130
9131       /* Character of current path component the same?  */
9132       else if (*cp1 != *cp2)
9133         return *cp1 - *cp2;
9134     }
9135 }
9136
9137 struct file_name_acquire_data
9138 {
9139   struct file_info *files;
9140   int used_files;
9141   int max_files;
9142 };
9143
9144 /* Traversal function for the hash table.  */
9145
9146 static int
9147 file_name_acquire (void ** slot, void *data)
9148 {
9149   struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
9150   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
9151   struct file_info *fi;
9152   const char *f;
9153
9154   gcc_assert (fnad->max_files >= d->emitted_number);
9155
9156   if (! d->emitted_number)
9157     return 1;
9158
9159   gcc_assert (fnad->max_files != fnad->used_files);
9160
9161   fi = fnad->files + fnad->used_files++;
9162
9163   /* Skip all leading "./".  */
9164   f = d->filename;
9165   while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
9166     f += 2;
9167
9168   /* Create a new array entry.  */
9169   fi->path = f;
9170   fi->length = strlen (f);
9171   fi->file_idx = d;
9172
9173   /* Search for the file name part.  */
9174   f = strrchr (f, DIR_SEPARATOR);
9175 #if defined (DIR_SEPARATOR_2)
9176   {
9177     char *g = strrchr (fi->path, DIR_SEPARATOR_2);
9178
9179     if (g != NULL)
9180       {
9181         if (f == NULL || f < g)
9182           f = g;
9183       }
9184   }
9185 #endif
9186
9187   fi->fname = f == NULL ? fi->path : f + 1;
9188   return 1;
9189 }
9190
9191 /* Output the directory table and the file name table.  We try to minimize
9192    the total amount of memory needed.  A heuristic is used to avoid large
9193    slowdowns with many input files.  */
9194
9195 static void
9196 output_file_names (void)
9197 {
9198   struct file_name_acquire_data fnad;
9199   int numfiles;
9200   struct file_info *files;
9201   struct dir_info *dirs;
9202   int *saved;
9203   int *savehere;
9204   int *backmap;
9205   int ndirs;
9206   int idx_offset;
9207   int i;
9208   int idx;
9209
9210   if (!last_emitted_file)
9211     {
9212       dw2_asm_output_data (1, 0, "End directory table");
9213       dw2_asm_output_data (1, 0, "End file name table");
9214       return;
9215     }
9216
9217   numfiles = last_emitted_file->emitted_number;
9218
9219   /* Allocate the various arrays we need.  */
9220   files = XALLOCAVEC (struct file_info, numfiles);
9221   dirs = XALLOCAVEC (struct dir_info, numfiles);
9222
9223   fnad.files = files;
9224   fnad.used_files = 0;
9225   fnad.max_files = numfiles;
9226   htab_traverse (file_table, file_name_acquire, &fnad);
9227   gcc_assert (fnad.used_files == fnad.max_files);
9228
9229   qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
9230
9231   /* Find all the different directories used.  */
9232   dirs[0].path = files[0].path;
9233   dirs[0].length = files[0].fname - files[0].path;
9234   dirs[0].prefix = -1;
9235   dirs[0].count = 1;
9236   dirs[0].dir_idx = 0;
9237   files[0].dir_idx = 0;
9238   ndirs = 1;
9239
9240   for (i = 1; i < numfiles; i++)
9241     if (files[i].fname - files[i].path == dirs[ndirs - 1].length
9242         && memcmp (dirs[ndirs - 1].path, files[i].path,
9243                    dirs[ndirs - 1].length) == 0)
9244       {
9245         /* Same directory as last entry.  */
9246         files[i].dir_idx = ndirs - 1;
9247         ++dirs[ndirs - 1].count;
9248       }
9249     else
9250       {
9251         int j;
9252
9253         /* This is a new directory.  */
9254         dirs[ndirs].path = files[i].path;
9255         dirs[ndirs].length = files[i].fname - files[i].path;
9256         dirs[ndirs].count = 1;
9257         dirs[ndirs].dir_idx = ndirs;
9258         files[i].dir_idx = ndirs;
9259
9260         /* Search for a prefix.  */
9261         dirs[ndirs].prefix = -1;
9262         for (j = 0; j < ndirs; j++)
9263           if (dirs[j].length < dirs[ndirs].length
9264               && dirs[j].length > 1
9265               && (dirs[ndirs].prefix == -1
9266                   || dirs[j].length > dirs[dirs[ndirs].prefix].length)
9267               && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
9268             dirs[ndirs].prefix = j;
9269
9270         ++ndirs;
9271       }
9272
9273   /* Now to the actual work.  We have to find a subset of the directories which
9274      allow expressing the file name using references to the directory table
9275      with the least amount of characters.  We do not do an exhaustive search
9276      where we would have to check out every combination of every single
9277      possible prefix.  Instead we use a heuristic which provides nearly optimal
9278      results in most cases and never is much off.  */
9279   saved = XALLOCAVEC (int, ndirs);
9280   savehere = XALLOCAVEC (int, ndirs);
9281
9282   memset (saved, '\0', ndirs * sizeof (saved[0]));
9283   for (i = 0; i < ndirs; i++)
9284     {
9285       int j;
9286       int total;
9287
9288       /* We can always save some space for the current directory.  But this
9289          does not mean it will be enough to justify adding the directory.  */
9290       savehere[i] = dirs[i].length;
9291       total = (savehere[i] - saved[i]) * dirs[i].count;
9292
9293       for (j = i + 1; j < ndirs; j++)
9294         {
9295           savehere[j] = 0;
9296           if (saved[j] < dirs[i].length)
9297             {
9298               /* Determine whether the dirs[i] path is a prefix of the
9299                  dirs[j] path.  */
9300               int k;
9301
9302               k = dirs[j].prefix;
9303               while (k != -1 && k != (int) i)
9304                 k = dirs[k].prefix;
9305
9306               if (k == (int) i)
9307                 {
9308                   /* Yes it is.  We can possibly save some memory by
9309                      writing the filenames in dirs[j] relative to
9310                      dirs[i].  */
9311                   savehere[j] = dirs[i].length;
9312                   total += (savehere[j] - saved[j]) * dirs[j].count;
9313                 }
9314             }
9315         }
9316
9317       /* Check whether we can save enough to justify adding the dirs[i]
9318          directory.  */
9319       if (total > dirs[i].length + 1)
9320         {
9321           /* It's worthwhile adding.  */
9322           for (j = i; j < ndirs; j++)
9323             if (savehere[j] > 0)
9324               {
9325                 /* Remember how much we saved for this directory so far.  */
9326                 saved[j] = savehere[j];
9327
9328                 /* Remember the prefix directory.  */
9329                 dirs[j].dir_idx = i;
9330               }
9331         }
9332     }
9333
9334   /* Emit the directory name table.  */
9335   idx = 1;
9336   idx_offset = dirs[0].length > 0 ? 1 : 0;
9337   for (i = 1 - idx_offset; i < ndirs; i++)
9338     dw2_asm_output_nstring (dirs[i].path, dirs[i].length - 1,
9339                             "Directory Entry: 0x%x", i + idx_offset);
9340
9341   dw2_asm_output_data (1, 0, "End directory table");
9342
9343   /* We have to emit them in the order of emitted_number since that's
9344      used in the debug info generation.  To do this efficiently we
9345      generate a back-mapping of the indices first.  */
9346   backmap = XALLOCAVEC (int, numfiles);
9347   for (i = 0; i < numfiles; i++)
9348     backmap[files[i].file_idx->emitted_number - 1] = i;
9349
9350   /* Now write all the file names.  */
9351   for (i = 0; i < numfiles; i++)
9352     {
9353       int file_idx = backmap[i];
9354       int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
9355
9356       dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
9357                               "File Entry: 0x%x", (unsigned) i + 1);
9358
9359       /* Include directory index.  */
9360       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9361
9362       /* Modification time.  */
9363       dw2_asm_output_data_uleb128 (0, NULL);
9364
9365       /* File length in bytes.  */
9366       dw2_asm_output_data_uleb128 (0, NULL);
9367     }
9368
9369   dw2_asm_output_data (1, 0, "End file name table");
9370 }
9371
9372
9373 /* Output the source line number correspondence information.  This
9374    information goes into the .debug_line section.  */
9375
9376 static void
9377 output_line_info (void)
9378 {
9379   char l1[20], l2[20], p1[20], p2[20];
9380   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
9381   char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
9382   unsigned opc;
9383   unsigned n_op_args;
9384   unsigned long lt_index;
9385   unsigned long current_line;
9386   long line_offset;
9387   long line_delta;
9388   unsigned long current_file;
9389   unsigned long function;
9390
9391   ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
9392   ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
9393   ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
9394   ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
9395
9396   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9397     dw2_asm_output_data (4, 0xffffffff,
9398       "Initial length escape value indicating 64-bit DWARF extension");
9399   dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
9400                         "Length of Source Line Info");
9401   ASM_OUTPUT_LABEL (asm_out_file, l1);
9402
9403   dw2_asm_output_data (2, dwarf_version, "DWARF Version");
9404   dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
9405   ASM_OUTPUT_LABEL (asm_out_file, p1);
9406
9407   /* Define the architecture-dependent minimum instruction length (in
9408    bytes).  In this implementation of DWARF, this field is used for
9409    information purposes only.  Since GCC generates assembly language,
9410    we have no a priori knowledge of how many instruction bytes are
9411    generated for each source line, and therefore can use only the
9412    DW_LNE_set_address and DW_LNS_fixed_advance_pc line information
9413    commands.  Accordingly, we fix this as `1', which is "correct
9414    enough" for all architectures, and don't let the target override.  */
9415   dw2_asm_output_data (1, 1,
9416                        "Minimum Instruction Length");
9417
9418   dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
9419                        "Default is_stmt_start flag");
9420   dw2_asm_output_data (1, DWARF_LINE_BASE,
9421                        "Line Base Value (Special Opcodes)");
9422   dw2_asm_output_data (1, DWARF_LINE_RANGE,
9423                        "Line Range Value (Special Opcodes)");
9424   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
9425                        "Special Opcode Base");
9426
9427   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
9428     {
9429       switch (opc)
9430         {
9431         case DW_LNS_advance_pc:
9432         case DW_LNS_advance_line:
9433         case DW_LNS_set_file:
9434         case DW_LNS_set_column:
9435         case DW_LNS_fixed_advance_pc:
9436           n_op_args = 1;
9437           break;
9438         default:
9439           n_op_args = 0;
9440           break;
9441         }
9442
9443       dw2_asm_output_data (1, n_op_args, "opcode: 0x%x has %d args",
9444                            opc, n_op_args);
9445     }
9446
9447   /* Write out the information about the files we use.  */
9448   output_file_names ();
9449   ASM_OUTPUT_LABEL (asm_out_file, p2);
9450
9451   /* We used to set the address register to the first location in the text
9452      section here, but that didn't accomplish anything since we already
9453      have a line note for the opening brace of the first function.  */
9454
9455   /* Generate the line number to PC correspondence table, encoded as
9456      a series of state machine operations.  */
9457   current_file = 1;
9458   current_line = 1;
9459
9460   if (cfun && in_cold_section_p)
9461     strcpy (prev_line_label, crtl->subsections.cold_section_label);
9462   else
9463     strcpy (prev_line_label, text_section_label);
9464   for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
9465     {
9466       dw_line_info_ref line_info = &line_info_table[lt_index];
9467
9468 #if 0
9469       /* Disable this optimization for now; GDB wants to see two line notes
9470          at the beginning of a function so it can find the end of the
9471          prologue.  */
9472
9473       /* Don't emit anything for redundant notes.  Just updating the
9474          address doesn't accomplish anything, because we already assume
9475          that anything after the last address is this line.  */
9476       if (line_info->dw_line_num == current_line
9477           && line_info->dw_file_num == current_file)
9478         continue;
9479 #endif
9480
9481       /* Emit debug info for the address of the current line.
9482
9483          Unfortunately, we have little choice here currently, and must always
9484          use the most general form.  GCC does not know the address delta
9485          itself, so we can't use DW_LNS_advance_pc.  Many ports do have length
9486          attributes which will give an upper bound on the address range.  We
9487          could perhaps use length attributes to determine when it is safe to
9488          use DW_LNS_fixed_advance_pc.  */
9489
9490       ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
9491       if (0)
9492         {
9493           /* This can handle deltas up to 0xffff.  This takes 3 bytes.  */
9494           dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
9495                                "DW_LNS_fixed_advance_pc");
9496           dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
9497         }
9498       else
9499         {
9500           /* This can handle any delta.  This takes
9501              4+DWARF2_ADDR_SIZE bytes.  */
9502           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9503           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9504           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9505           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9506         }
9507
9508       strcpy (prev_line_label, line_label);
9509
9510       /* Emit debug info for the source file of the current line, if
9511          different from the previous line.  */
9512       if (line_info->dw_file_num != current_file)
9513         {
9514           current_file = line_info->dw_file_num;
9515           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
9516           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
9517         }
9518
9519       /* Emit debug info for the current line number, choosing the encoding
9520          that uses the least amount of space.  */
9521       if (line_info->dw_line_num != current_line)
9522         {
9523           line_offset = line_info->dw_line_num - current_line;
9524           line_delta = line_offset - DWARF_LINE_BASE;
9525           current_line = line_info->dw_line_num;
9526           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
9527             /* This can handle deltas from -10 to 234, using the current
9528                definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.  This
9529                takes 1 byte.  */
9530             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
9531                                  "line %lu", current_line);
9532           else
9533             {
9534               /* This can handle any delta.  This takes at least 4 bytes,
9535                  depending on the value being encoded.  */
9536               dw2_asm_output_data (1, DW_LNS_advance_line,
9537                                    "advance to line %lu", current_line);
9538               dw2_asm_output_data_sleb128 (line_offset, NULL);
9539               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
9540             }
9541         }
9542       else
9543         /* We still need to start a new row, so output a copy insn.  */
9544         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
9545     }
9546
9547   /* Emit debug info for the address of the end of the function.  */
9548   if (0)
9549     {
9550       dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
9551                            "DW_LNS_fixed_advance_pc");
9552       dw2_asm_output_delta (2, text_end_label, prev_line_label, NULL);
9553     }
9554   else
9555     {
9556       dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9557       dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9558       dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9559       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_end_label, NULL);
9560     }
9561
9562   dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
9563   dw2_asm_output_data_uleb128 (1, NULL);
9564   dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
9565
9566   function = 0;
9567   current_file = 1;
9568   current_line = 1;
9569   for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
9570     {
9571       dw_separate_line_info_ref line_info
9572         = &separate_line_info_table[lt_index];
9573
9574 #if 0
9575       /* Don't emit anything for redundant notes.  */
9576       if (line_info->dw_line_num == current_line
9577           && line_info->dw_file_num == current_file
9578           && line_info->function == function)
9579         goto cont;
9580 #endif
9581
9582       /* Emit debug info for the address of the current line.  If this is
9583          a new function, or the first line of a function, then we need
9584          to handle it differently.  */
9585       ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
9586                                    lt_index);
9587       if (function != line_info->function)
9588         {
9589           function = line_info->function;
9590
9591           /* Set the address register to the first line in the function.  */
9592           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9593           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9594           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9595           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9596         }
9597       else
9598         {
9599           /* ??? See the DW_LNS_advance_pc comment above.  */
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, line_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, line_label, NULL);
9612             }
9613         }
9614
9615       strcpy (prev_line_label, line_label);
9616
9617       /* Emit debug info for the source file of the current line, if
9618          different from the previous line.  */
9619       if (line_info->dw_file_num != current_file)
9620         {
9621           current_file = line_info->dw_file_num;
9622           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
9623           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
9624         }
9625
9626       /* Emit debug info for the current line number, choosing the encoding
9627          that uses the least amount of space.  */
9628       if (line_info->dw_line_num != current_line)
9629         {
9630           line_offset = line_info->dw_line_num - current_line;
9631           line_delta = line_offset - DWARF_LINE_BASE;
9632           current_line = line_info->dw_line_num;
9633           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
9634             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
9635                                  "line %lu", current_line);
9636           else
9637             {
9638               dw2_asm_output_data (1, DW_LNS_advance_line,
9639                                    "advance to line %lu", current_line);
9640               dw2_asm_output_data_sleb128 (line_offset, NULL);
9641               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
9642             }
9643         }
9644       else
9645         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
9646
9647 #if 0
9648     cont:
9649 #endif
9650
9651       lt_index++;
9652
9653       /* If we're done with a function, end its sequence.  */
9654       if (lt_index == separate_line_info_table_in_use
9655           || separate_line_info_table[lt_index].function != function)
9656         {
9657           current_file = 1;
9658           current_line = 1;
9659
9660           /* Emit debug info for the address of the end of the function.  */
9661           ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
9662           if (0)
9663             {
9664               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
9665                                    "DW_LNS_fixed_advance_pc");
9666               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
9667             }
9668           else
9669             {
9670               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9671               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9672               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9673               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9674             }
9675
9676           /* Output the marker for the end of this sequence.  */
9677           dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
9678           dw2_asm_output_data_uleb128 (1, NULL);
9679           dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
9680         }
9681     }
9682
9683   /* Output the marker for the end of the line number info.  */
9684   ASM_OUTPUT_LABEL (asm_out_file, l2);
9685 }
9686 \f
9687 /* Given a pointer to a tree node for some base type, return a pointer to
9688    a DIE that describes the given type.
9689
9690    This routine must only be called for GCC type nodes that correspond to
9691    Dwarf base (fundamental) types.  */
9692
9693 static dw_die_ref
9694 base_type_die (tree type)
9695 {
9696   dw_die_ref base_type_result;
9697   enum dwarf_type encoding;
9698
9699   if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
9700     return 0;
9701
9702   /* If this is a subtype that should not be emitted as a subrange type,
9703      use the base type.  See subrange_type_for_debug_p.  */
9704   if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
9705     type = TREE_TYPE (type);
9706
9707   switch (TREE_CODE (type))
9708     {
9709     case INTEGER_TYPE:
9710       if (TYPE_STRING_FLAG (type))
9711         {
9712           if (TYPE_UNSIGNED (type))
9713             encoding = DW_ATE_unsigned_char;
9714           else
9715             encoding = DW_ATE_signed_char;
9716         }
9717       else if (TYPE_UNSIGNED (type))
9718         encoding = DW_ATE_unsigned;
9719       else
9720         encoding = DW_ATE_signed;
9721       break;
9722
9723     case REAL_TYPE:
9724       if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
9725         encoding = DW_ATE_decimal_float;
9726       else
9727         encoding = DW_ATE_float;
9728       break;
9729
9730     case FIXED_POINT_TYPE:
9731       if (TYPE_UNSIGNED (type))
9732         encoding = DW_ATE_unsigned_fixed;
9733       else
9734         encoding = DW_ATE_signed_fixed;
9735       break;
9736
9737       /* Dwarf2 doesn't know anything about complex ints, so use
9738          a user defined type for it.  */
9739     case COMPLEX_TYPE:
9740       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
9741         encoding = DW_ATE_complex_float;
9742       else
9743         encoding = DW_ATE_lo_user;
9744       break;
9745
9746     case BOOLEAN_TYPE:
9747       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
9748       encoding = DW_ATE_boolean;
9749       break;
9750
9751     default:
9752       /* No other TREE_CODEs are Dwarf fundamental types.  */
9753       gcc_unreachable ();
9754     }
9755
9756   base_type_result = new_die (DW_TAG_base_type, comp_unit_die, type);
9757
9758   /* This probably indicates a bug.  */
9759   if (! TYPE_NAME (type))
9760     add_name_attribute (base_type_result, "__unknown__");
9761
9762   add_AT_unsigned (base_type_result, DW_AT_byte_size,
9763                    int_size_in_bytes (type));
9764   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
9765
9766   return base_type_result;
9767 }
9768
9769 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
9770    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
9771
9772 static inline int
9773 is_base_type (tree type)
9774 {
9775   switch (TREE_CODE (type))
9776     {
9777     case ERROR_MARK:
9778     case VOID_TYPE:
9779     case INTEGER_TYPE:
9780     case REAL_TYPE:
9781     case FIXED_POINT_TYPE:
9782     case COMPLEX_TYPE:
9783     case BOOLEAN_TYPE:
9784       return 1;
9785
9786     case ARRAY_TYPE:
9787     case RECORD_TYPE:
9788     case UNION_TYPE:
9789     case QUAL_UNION_TYPE:
9790     case ENUMERAL_TYPE:
9791     case FUNCTION_TYPE:
9792     case METHOD_TYPE:
9793     case POINTER_TYPE:
9794     case REFERENCE_TYPE:
9795     case OFFSET_TYPE:
9796     case LANG_TYPE:
9797     case VECTOR_TYPE:
9798       return 0;
9799
9800     default:
9801       gcc_unreachable ();
9802     }
9803
9804   return 0;
9805 }
9806
9807 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
9808    node, return the size in bits for the type if it is a constant, or else
9809    return the alignment for the type if the type's size is not constant, or
9810    else return BITS_PER_WORD if the type actually turns out to be an
9811    ERROR_MARK node.  */
9812
9813 static inline unsigned HOST_WIDE_INT
9814 simple_type_size_in_bits (const_tree type)
9815 {
9816   if (TREE_CODE (type) == ERROR_MARK)
9817     return BITS_PER_WORD;
9818   else if (TYPE_SIZE (type) == NULL_TREE)
9819     return 0;
9820   else if (host_integerp (TYPE_SIZE (type), 1))
9821     return tree_low_cst (TYPE_SIZE (type), 1);
9822   else
9823     return TYPE_ALIGN (type);
9824 }
9825
9826 /*  Given a pointer to a tree node for a subrange type, return a pointer
9827     to a DIE that describes the given type.  */
9828
9829 static dw_die_ref
9830 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
9831 {
9832   dw_die_ref subrange_die;
9833   const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
9834
9835   if (context_die == NULL)
9836     context_die = comp_unit_die;
9837
9838   subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
9839
9840   if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
9841     {
9842       /* The size of the subrange type and its base type do not match,
9843          so we need to generate a size attribute for the subrange type.  */
9844       add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
9845     }
9846
9847   if (low)
9848     add_bound_info (subrange_die, DW_AT_lower_bound, low);
9849   if (high)
9850     add_bound_info (subrange_die, DW_AT_upper_bound, high);
9851
9852   return subrange_die;
9853 }
9854
9855 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
9856    entry that chains various modifiers in front of the given type.  */
9857
9858 static dw_die_ref
9859 modified_type_die (tree type, int is_const_type, int is_volatile_type,
9860                    dw_die_ref context_die)
9861 {
9862   enum tree_code code = TREE_CODE (type);
9863   dw_die_ref mod_type_die;
9864   dw_die_ref sub_die = NULL;
9865   tree item_type = NULL;
9866   tree qualified_type;
9867   tree name, low, high;
9868
9869   if (code == ERROR_MARK)
9870     return NULL;
9871
9872   /* See if we already have the appropriately qualified variant of
9873      this type.  */
9874   qualified_type
9875     = get_qualified_type (type,
9876                           ((is_const_type ? TYPE_QUAL_CONST : 0)
9877                            | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
9878
9879   /* If we do, then we can just use its DIE, if it exists.  */
9880   if (qualified_type)
9881     {
9882       mod_type_die = lookup_type_die (qualified_type);
9883       if (mod_type_die)
9884         return mod_type_die;
9885     }
9886
9887   name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
9888
9889   /* Handle C typedef types.  */
9890   if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name))
9891     {
9892       tree dtype = TREE_TYPE (name);
9893
9894       if (qualified_type == dtype)
9895         {
9896           /* For a named type, use the typedef.  */
9897           gen_type_die (qualified_type, context_die);
9898           return lookup_type_die (qualified_type);
9899         }
9900       else if (is_const_type < TYPE_READONLY (dtype)
9901                || is_volatile_type < TYPE_VOLATILE (dtype)
9902                || (is_const_type <= TYPE_READONLY (dtype)
9903                    && is_volatile_type <= TYPE_VOLATILE (dtype)
9904                    && DECL_ORIGINAL_TYPE (name) != type))
9905         /* cv-unqualified version of named type.  Just use the unnamed
9906            type to which it refers.  */
9907         return modified_type_die (DECL_ORIGINAL_TYPE (name),
9908                                   is_const_type, is_volatile_type,
9909                                   context_die);
9910       /* Else cv-qualified version of named type; fall through.  */
9911     }
9912
9913   if (is_const_type)
9914     {
9915       mod_type_die = new_die (DW_TAG_const_type, comp_unit_die, type);
9916       sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
9917     }
9918   else if (is_volatile_type)
9919     {
9920       mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die, type);
9921       sub_die = modified_type_die (type, 0, 0, context_die);
9922     }
9923   else if (code == POINTER_TYPE)
9924     {
9925       mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die, type);
9926       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
9927                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
9928       item_type = TREE_TYPE (type);
9929     }
9930   else if (code == REFERENCE_TYPE)
9931     {
9932       mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die, type);
9933       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
9934                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
9935       item_type = TREE_TYPE (type);
9936     }
9937   else if (code == INTEGER_TYPE
9938            && TREE_TYPE (type) != NULL_TREE
9939            && subrange_type_for_debug_p (type, &low, &high))
9940     {
9941       mod_type_die = subrange_type_die (type, low, high, context_die);
9942       item_type = TREE_TYPE (type);
9943     }
9944   else if (is_base_type (type))
9945     mod_type_die = base_type_die (type);
9946   else
9947     {
9948       gen_type_die (type, context_die);
9949
9950       /* We have to get the type_main_variant here (and pass that to the
9951          `lookup_type_die' routine) because the ..._TYPE node we have
9952          might simply be a *copy* of some original type node (where the
9953          copy was created to help us keep track of typedef names) and
9954          that copy might have a different TYPE_UID from the original
9955          ..._TYPE node.  */
9956       if (TREE_CODE (type) != VECTOR_TYPE)
9957         return lookup_type_die (type_main_variant (type));
9958       else
9959         /* Vectors have the debugging information in the type,
9960            not the main variant.  */
9961         return lookup_type_die (type);
9962     }
9963
9964   /* Builtin types don't have a DECL_ORIGINAL_TYPE.  For those,
9965      don't output a DW_TAG_typedef, since there isn't one in the
9966      user's program; just attach a DW_AT_name to the type.  */
9967   if (name
9968       && (TREE_CODE (name) != TYPE_DECL
9969           || (TREE_TYPE (name) == qualified_type && DECL_NAME (name))))
9970     {
9971       if (TREE_CODE (name) == TYPE_DECL)
9972         /* Could just call add_name_and_src_coords_attributes here,
9973            but since this is a builtin type it doesn't have any
9974            useful source coordinates anyway.  */
9975         name = DECL_NAME (name);
9976       add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
9977     }
9978
9979   if (qualified_type)
9980     equate_type_number_to_die (qualified_type, mod_type_die);
9981
9982   if (item_type)
9983     /* We must do this after the equate_type_number_to_die call, in case
9984        this is a recursive type.  This ensures that the modified_type_die
9985        recursion will terminate even if the type is recursive.  Recursive
9986        types are possible in Ada.  */
9987     sub_die = modified_type_die (item_type,
9988                                  TYPE_READONLY (item_type),
9989                                  TYPE_VOLATILE (item_type),
9990                                  context_die);
9991
9992   if (sub_die != NULL)
9993     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
9994
9995   return mod_type_die;
9996 }
9997
9998 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
9999    an enumerated type.  */
10000
10001 static inline int
10002 type_is_enum (const_tree type)
10003 {
10004   return TREE_CODE (type) == ENUMERAL_TYPE;
10005 }
10006
10007 /* Return the DBX register number described by a given RTL node.  */
10008
10009 static unsigned int
10010 dbx_reg_number (const_rtx rtl)
10011 {
10012   unsigned regno = REGNO (rtl);
10013
10014   gcc_assert (regno < FIRST_PSEUDO_REGISTER);
10015
10016 #ifdef LEAF_REG_REMAP
10017   if (current_function_uses_only_leaf_regs)
10018     {
10019       int leaf_reg = LEAF_REG_REMAP (regno);
10020       if (leaf_reg != -1)
10021         regno = (unsigned) leaf_reg;
10022     }
10023 #endif
10024
10025   return DBX_REGISTER_NUMBER (regno);
10026 }
10027
10028 /* Optionally add a DW_OP_piece term to a location description expression.
10029    DW_OP_piece is only added if the location description expression already
10030    doesn't end with DW_OP_piece.  */
10031
10032 static void
10033 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
10034 {
10035   dw_loc_descr_ref loc;
10036
10037   if (*list_head != NULL)
10038     {
10039       /* Find the end of the chain.  */
10040       for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
10041         ;
10042
10043       if (loc->dw_loc_opc != DW_OP_piece)
10044         loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
10045     }
10046 }
10047
10048 /* Return a location descriptor that designates a machine register or
10049    zero if there is none.  */
10050
10051 static dw_loc_descr_ref
10052 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
10053 {
10054   rtx regs;
10055
10056   if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
10057     return 0;
10058
10059   regs = targetm.dwarf_register_span (rtl);
10060
10061   if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
10062     return multiple_reg_loc_descriptor (rtl, regs, initialized);
10063   else
10064     return one_reg_loc_descriptor (dbx_reg_number (rtl), initialized);
10065 }
10066
10067 /* Return a location descriptor that designates a machine register for
10068    a given hard register number.  */
10069
10070 static dw_loc_descr_ref
10071 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
10072 {
10073   dw_loc_descr_ref reg_loc_descr;
10074
10075   if (regno <= 31)
10076     reg_loc_descr
10077       = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
10078   else
10079     reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
10080
10081   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10082     add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10083
10084   return reg_loc_descr;
10085 }
10086
10087 /* Given an RTL of a register, return a location descriptor that
10088    designates a value that spans more than one register.  */
10089
10090 static dw_loc_descr_ref
10091 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
10092                              enum var_init_status initialized)
10093 {
10094   int nregs, size, i;
10095   unsigned reg;
10096   dw_loc_descr_ref loc_result = NULL;
10097
10098   reg = REGNO (rtl);
10099 #ifdef LEAF_REG_REMAP
10100   if (current_function_uses_only_leaf_regs)
10101     {
10102       int leaf_reg = LEAF_REG_REMAP (reg);
10103       if (leaf_reg != -1)
10104         reg = (unsigned) leaf_reg;
10105     }
10106 #endif
10107   gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
10108   nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
10109
10110   /* Simple, contiguous registers.  */
10111   if (regs == NULL_RTX)
10112     {
10113       size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
10114
10115       loc_result = NULL;
10116       while (nregs--)
10117         {
10118           dw_loc_descr_ref t;
10119
10120           t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
10121                                       VAR_INIT_STATUS_INITIALIZED);
10122           add_loc_descr (&loc_result, t);
10123           add_loc_descr_op_piece (&loc_result, size);
10124           ++reg;
10125         }
10126       return loc_result;
10127     }
10128
10129   /* Now onto stupid register sets in non contiguous locations.  */
10130
10131   gcc_assert (GET_CODE (regs) == PARALLEL);
10132
10133   size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
10134   loc_result = NULL;
10135
10136   for (i = 0; i < XVECLEN (regs, 0); ++i)
10137     {
10138       dw_loc_descr_ref t;
10139
10140       t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
10141                                   VAR_INIT_STATUS_INITIALIZED);
10142       add_loc_descr (&loc_result, t);
10143       size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
10144       add_loc_descr_op_piece (&loc_result, size);
10145     }
10146
10147   if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10148     add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10149   return loc_result;
10150 }
10151
10152 #endif /* DWARF2_DEBUGGING_INFO */
10153
10154 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
10155
10156 /* Return a location descriptor that designates a constant.  */
10157
10158 static dw_loc_descr_ref
10159 int_loc_descriptor (HOST_WIDE_INT i)
10160 {
10161   enum dwarf_location_atom op;
10162
10163   /* Pick the smallest representation of a constant, rather than just
10164      defaulting to the LEB encoding.  */
10165   if (i >= 0)
10166     {
10167       if (i <= 31)
10168         op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
10169       else if (i <= 0xff)
10170         op = DW_OP_const1u;
10171       else if (i <= 0xffff)
10172         op = DW_OP_const2u;
10173       else if (HOST_BITS_PER_WIDE_INT == 32
10174                || i <= 0xffffffff)
10175         op = DW_OP_const4u;
10176       else
10177         op = DW_OP_constu;
10178     }
10179   else
10180     {
10181       if (i >= -0x80)
10182         op = DW_OP_const1s;
10183       else if (i >= -0x8000)
10184         op = DW_OP_const2s;
10185       else if (HOST_BITS_PER_WIDE_INT == 32
10186                || i >= -0x80000000)
10187         op = DW_OP_const4s;
10188       else
10189         op = DW_OP_consts;
10190     }
10191
10192   return new_loc_descr (op, i, 0);
10193 }
10194 #endif
10195
10196 #ifdef DWARF2_DEBUGGING_INFO
10197
10198 /* Return a location descriptor that designates a base+offset location.  */
10199
10200 static dw_loc_descr_ref
10201 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
10202                  enum var_init_status initialized)
10203 {
10204   unsigned int regno;
10205   dw_loc_descr_ref result;
10206   dw_fde_ref fde = current_fde ();
10207
10208   /* We only use "frame base" when we're sure we're talking about the
10209      post-prologue local stack frame.  We do this by *not* running
10210      register elimination until this point, and recognizing the special
10211      argument pointer and soft frame pointer rtx's.  */
10212   if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
10213     {
10214       rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
10215
10216       if (elim != reg)
10217         {
10218           if (GET_CODE (elim) == PLUS)
10219             {
10220               offset += INTVAL (XEXP (elim, 1));
10221               elim = XEXP (elim, 0);
10222             }
10223           gcc_assert ((SUPPORTS_STACK_ALIGNMENT
10224                        && (elim == hard_frame_pointer_rtx
10225                            || elim == stack_pointer_rtx))
10226                       || elim == (frame_pointer_needed
10227                                   ? hard_frame_pointer_rtx
10228                                   : stack_pointer_rtx));
10229
10230           /* If drap register is used to align stack, use frame
10231              pointer + offset to access stack variables.  If stack
10232              is aligned without drap, use stack pointer + offset to
10233              access stack variables.  */
10234           if (crtl->stack_realign_tried
10235               && cfa.reg == HARD_FRAME_POINTER_REGNUM
10236               && reg == frame_pointer_rtx)
10237             {
10238               int base_reg
10239                 = DWARF_FRAME_REGNUM (cfa.indirect
10240                                       ? HARD_FRAME_POINTER_REGNUM
10241                                       : STACK_POINTER_REGNUM);
10242               return new_reg_loc_descr (base_reg, offset);
10243             }
10244
10245           offset += frame_pointer_fb_offset;
10246           return new_loc_descr (DW_OP_fbreg, offset, 0);
10247         }
10248     }
10249   else if (fde
10250            && fde->drap_reg != INVALID_REGNUM
10251            && (fde->drap_reg == REGNO (reg)
10252                || fde->vdrap_reg == REGNO (reg)))
10253     {
10254       /* Use cfa+offset to represent the location of arguments passed
10255          on stack when drap is used to align stack.  */
10256       return new_loc_descr (DW_OP_fbreg, offset, 0);
10257     }
10258
10259   regno = dbx_reg_number (reg);
10260   if (regno <= 31)
10261     result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
10262                             offset, 0);
10263   else
10264     result = new_loc_descr (DW_OP_bregx, regno, offset);
10265
10266   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10267     add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10268
10269   return result;
10270 }
10271
10272 /* Return true if this RTL expression describes a base+offset calculation.  */
10273
10274 static inline int
10275 is_based_loc (const_rtx rtl)
10276 {
10277   return (GET_CODE (rtl) == PLUS
10278           && ((REG_P (XEXP (rtl, 0))
10279                && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
10280                && CONST_INT_P (XEXP (rtl, 1)))));
10281 }
10282
10283 /* Return a descriptor that describes the concatenation of N locations
10284    used to form the address of a memory location.  */
10285
10286 static dw_loc_descr_ref
10287 concatn_mem_loc_descriptor (rtx concatn, enum machine_mode mode,
10288                             enum var_init_status initialized)
10289 {
10290   unsigned int i;
10291   dw_loc_descr_ref cc_loc_result = NULL;
10292   unsigned int n = XVECLEN (concatn, 0);
10293
10294   for (i = 0; i < n; ++i)
10295     {
10296       dw_loc_descr_ref ref;
10297       rtx x = XVECEXP (concatn, 0, i);
10298
10299       ref = mem_loc_descriptor (x, mode, VAR_INIT_STATUS_INITIALIZED);
10300       if (ref == NULL)
10301         return NULL;
10302
10303       add_loc_descr (&cc_loc_result, ref);
10304       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
10305     }
10306
10307   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10308     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10309
10310   return cc_loc_result;
10311 }
10312
10313 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
10314    failed.  */
10315
10316 static dw_loc_descr_ref
10317 tls_mem_loc_descriptor (rtx mem)
10318 {
10319   tree base;
10320   dw_loc_descr_ref loc_result;
10321
10322   if (MEM_EXPR (mem) == NULL_TREE || MEM_OFFSET (mem) == NULL_RTX)
10323     return NULL;
10324
10325   base = get_base_address (MEM_EXPR (mem));
10326   if (base == NULL
10327       || TREE_CODE (base) != VAR_DECL
10328       || !DECL_THREAD_LOCAL_P (base))
10329     return NULL;
10330
10331   loc_result = loc_descriptor_from_tree_1 (MEM_EXPR (mem), 2);
10332   if (loc_result == NULL)
10333     return NULL;
10334
10335   if (INTVAL (MEM_OFFSET (mem)))
10336     loc_descr_plus_const (&loc_result, INTVAL (MEM_OFFSET (mem)));
10337
10338   return loc_result;
10339 }
10340
10341 /* The following routine converts the RTL for a variable or parameter
10342    (resident in memory) into an equivalent Dwarf representation of a
10343    mechanism for getting the address of that same variable onto the top of a
10344    hypothetical "address evaluation" stack.
10345
10346    When creating memory location descriptors, we are effectively transforming
10347    the RTL for a memory-resident object into its Dwarf postfix expression
10348    equivalent.  This routine recursively descends an RTL tree, turning
10349    it into Dwarf postfix code as it goes.
10350
10351    MODE is the mode of the memory reference, needed to handle some
10352    autoincrement addressing modes.
10353
10354    CAN_USE_FBREG is a flag whether we can use DW_AT_frame_base in the
10355    location list for RTL.
10356
10357    Return 0 if we can't represent the location.  */
10358
10359 static dw_loc_descr_ref
10360 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
10361                     enum var_init_status initialized)
10362 {
10363   dw_loc_descr_ref mem_loc_result = NULL;
10364   enum dwarf_location_atom op;
10365
10366   /* Note that for a dynamically sized array, the location we will generate a
10367      description of here will be the lowest numbered location which is
10368      actually within the array.  That's *not* necessarily the same as the
10369      zeroth element of the array.  */
10370
10371   rtl = targetm.delegitimize_address (rtl);
10372
10373   switch (GET_CODE (rtl))
10374     {
10375     case POST_INC:
10376     case POST_DEC:
10377     case POST_MODIFY:
10378       /* POST_INC and POST_DEC can be handled just like a SUBREG.  So we
10379          just fall into the SUBREG code.  */
10380
10381       /* ... fall through ...  */
10382
10383     case SUBREG:
10384       /* The case of a subreg may arise when we have a local (register)
10385          variable or a formal (register) parameter which doesn't quite fill
10386          up an entire register.  For now, just assume that it is
10387          legitimate to make the Dwarf info refer to the whole register which
10388          contains the given subreg.  */
10389       rtl = XEXP (rtl, 0);
10390
10391       /* ... fall through ...  */
10392
10393     case REG:
10394       /* Whenever a register number forms a part of the description of the
10395          method for calculating the (dynamic) address of a memory resident
10396          object, DWARF rules require the register number be referred to as
10397          a "base register".  This distinction is not based in any way upon
10398          what category of register the hardware believes the given register
10399          belongs to.  This is strictly DWARF terminology we're dealing with
10400          here. Note that in cases where the location of a memory-resident
10401          data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
10402          OP_CONST (0)) the actual DWARF location descriptor that we generate
10403          may just be OP_BASEREG (basereg).  This may look deceptively like
10404          the object in question was allocated to a register (rather than in
10405          memory) so DWARF consumers need to be aware of the subtle
10406          distinction between OP_REG and OP_BASEREG.  */
10407       if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
10408         mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
10409       else if (stack_realign_drap
10410                && crtl->drap_reg
10411                && crtl->args.internal_arg_pointer == rtl
10412                && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
10413         {
10414           /* If RTL is internal_arg_pointer, which has been optimized
10415              out, use DRAP instead.  */
10416           mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
10417                                             VAR_INIT_STATUS_INITIALIZED);
10418         }
10419       break;
10420
10421     case MEM:
10422       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
10423                                            VAR_INIT_STATUS_INITIALIZED);
10424       if (mem_loc_result == NULL)
10425         mem_loc_result = tls_mem_loc_descriptor (rtl);
10426       if (mem_loc_result != 0)
10427         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
10428       break;
10429
10430     case LO_SUM:
10431          rtl = XEXP (rtl, 1);
10432
10433       /* ... fall through ...  */
10434
10435     case LABEL_REF:
10436       /* Some ports can transform a symbol ref into a label ref, because
10437          the symbol ref is too far away and has to be dumped into a constant
10438          pool.  */
10439     case CONST:
10440     case SYMBOL_REF:
10441       /* Alternatively, the symbol in the constant pool might be referenced
10442          by a different symbol.  */
10443       if (GET_CODE (rtl) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (rtl))
10444         {
10445           bool marked;
10446           rtx tmp = get_pool_constant_mark (rtl, &marked);
10447
10448           if (GET_CODE (tmp) == SYMBOL_REF)
10449             {
10450               rtl = tmp;
10451               if (CONSTANT_POOL_ADDRESS_P (tmp))
10452                 get_pool_constant_mark (tmp, &marked);
10453               else
10454                 marked = true;
10455             }
10456
10457           /* If all references to this pool constant were optimized away,
10458              it was not output and thus we can't represent it.
10459              FIXME: might try to use DW_OP_const_value here, though
10460              DW_OP_piece complicates it.  */
10461           if (!marked)
10462             return 0;
10463         }
10464
10465       mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
10466       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
10467       mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
10468       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
10469       break;
10470
10471     case PRE_MODIFY:
10472       /* Extract the PLUS expression nested inside and fall into
10473          PLUS code below.  */
10474       rtl = XEXP (rtl, 1);
10475       goto plus;
10476
10477     case PRE_INC:
10478     case PRE_DEC:
10479       /* Turn these into a PLUS expression and fall into the PLUS code
10480          below.  */
10481       rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
10482                           GEN_INT (GET_CODE (rtl) == PRE_INC
10483                                    ? GET_MODE_UNIT_SIZE (mode)
10484                                    : -GET_MODE_UNIT_SIZE (mode)));
10485
10486       /* ... fall through ...  */
10487
10488     case PLUS:
10489     plus:
10490       if (is_based_loc (rtl))
10491         mem_loc_result = based_loc_descr (XEXP (rtl, 0),
10492                                           INTVAL (XEXP (rtl, 1)),
10493                                           VAR_INIT_STATUS_INITIALIZED);
10494       else
10495         {
10496           mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode,
10497                                                VAR_INIT_STATUS_INITIALIZED);
10498           if (mem_loc_result == 0)
10499             break;
10500
10501           if (CONST_INT_P (XEXP (rtl, 1)))
10502             loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
10503           else
10504             {
10505               dw_loc_descr_ref mem_loc_result2
10506                 = mem_loc_descriptor (XEXP (rtl, 1), mode,
10507                                       VAR_INIT_STATUS_INITIALIZED);
10508               if (mem_loc_result2 == 0)
10509                 break;
10510               add_loc_descr (&mem_loc_result, mem_loc_result2);
10511               add_loc_descr (&mem_loc_result,
10512                              new_loc_descr (DW_OP_plus, 0, 0));
10513             }
10514         }
10515       break;
10516
10517     /* If a pseudo-reg is optimized away, it is possible for it to
10518        be replaced with a MEM containing a multiply or shift.  */
10519     case MULT:
10520       op = DW_OP_mul;
10521       goto do_binop;
10522
10523     case ASHIFT:
10524       op = DW_OP_shl;
10525       goto do_binop;
10526
10527     case ASHIFTRT:
10528       op = DW_OP_shra;
10529       goto do_binop;
10530
10531     case LSHIFTRT:
10532       op = DW_OP_shr;
10533       goto do_binop;
10534
10535     do_binop:
10536       {
10537         dw_loc_descr_ref op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
10538                                                    VAR_INIT_STATUS_INITIALIZED);
10539         dw_loc_descr_ref op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
10540                                                    VAR_INIT_STATUS_INITIALIZED);
10541
10542         if (op0 == 0 || op1 == 0)
10543           break;
10544
10545         mem_loc_result = op0;
10546         add_loc_descr (&mem_loc_result, op1);
10547         add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
10548         break;
10549       }
10550
10551     case CONST_INT:
10552       mem_loc_result = int_loc_descriptor (INTVAL (rtl));
10553       break;
10554
10555     case CONCATN:
10556       mem_loc_result = concatn_mem_loc_descriptor (rtl, mode,
10557                                                    VAR_INIT_STATUS_INITIALIZED);
10558       break;
10559
10560     case UNSPEC:
10561       /* If delegitimize_address couldn't do anything with the UNSPEC, we
10562          can't express it in the debug info.  This can happen e.g. with some
10563          TLS UNSPECs.  */
10564       break;
10565
10566     default:
10567       gcc_unreachable ();
10568     }
10569
10570   if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10571     add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10572
10573   return mem_loc_result;
10574 }
10575
10576 /* Return a descriptor that describes the concatenation of two locations.
10577    This is typically a complex variable.  */
10578
10579 static dw_loc_descr_ref
10580 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
10581 {
10582   dw_loc_descr_ref cc_loc_result = NULL;
10583   dw_loc_descr_ref x0_ref = loc_descriptor (x0, VAR_INIT_STATUS_INITIALIZED);
10584   dw_loc_descr_ref x1_ref = loc_descriptor (x1, VAR_INIT_STATUS_INITIALIZED);
10585
10586   if (x0_ref == 0 || x1_ref == 0)
10587     return 0;
10588
10589   cc_loc_result = x0_ref;
10590   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
10591
10592   add_loc_descr (&cc_loc_result, x1_ref);
10593   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
10594
10595   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10596     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10597
10598   return cc_loc_result;
10599 }
10600
10601 /* Return a descriptor that describes the concatenation of N
10602    locations.  */
10603
10604 static dw_loc_descr_ref
10605 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
10606 {
10607   unsigned int i;
10608   dw_loc_descr_ref cc_loc_result = NULL;
10609   unsigned int n = XVECLEN (concatn, 0);
10610
10611   for (i = 0; i < n; ++i)
10612     {
10613       dw_loc_descr_ref ref;
10614       rtx x = XVECEXP (concatn, 0, i);
10615
10616       ref = loc_descriptor (x, VAR_INIT_STATUS_INITIALIZED);
10617       if (ref == NULL)
10618         return NULL;
10619
10620       add_loc_descr (&cc_loc_result, ref);
10621       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
10622     }
10623
10624   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10625     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10626
10627   return cc_loc_result;
10628 }
10629
10630 /* Output a proper Dwarf location descriptor for a variable or parameter
10631    which is either allocated in a register or in a memory location.  For a
10632    register, we just generate an OP_REG and the register number.  For a
10633    memory location we provide a Dwarf postfix expression describing how to
10634    generate the (dynamic) address of the object onto the address stack.
10635
10636    If we don't know how to describe it, return 0.  */
10637
10638 static dw_loc_descr_ref
10639 loc_descriptor (rtx rtl, enum var_init_status initialized)
10640 {
10641   dw_loc_descr_ref loc_result = NULL;
10642
10643   switch (GET_CODE (rtl))
10644     {
10645     case SUBREG:
10646       /* The case of a subreg may arise when we have a local (register)
10647          variable or a formal (register) parameter which doesn't quite fill
10648          up an entire register.  For now, just assume that it is
10649          legitimate to make the Dwarf info refer to the whole register which
10650          contains the given subreg.  */
10651       rtl = SUBREG_REG (rtl);
10652
10653       /* ... fall through ...  */
10654
10655     case REG:
10656       loc_result = reg_loc_descriptor (rtl, initialized);
10657       break;
10658
10659     case MEM:
10660       loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
10661                                        initialized);
10662       if (loc_result == NULL)
10663         loc_result = tls_mem_loc_descriptor (rtl);
10664       break;
10665
10666     case CONCAT:
10667       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
10668                                           initialized);
10669       break;
10670
10671     case CONCATN:
10672       loc_result = concatn_loc_descriptor (rtl, initialized);
10673       break;
10674
10675     case VAR_LOCATION:
10676       /* Single part.  */
10677       if (GET_CODE (XEXP (rtl, 1)) != PARALLEL)
10678         {
10679           loc_result = loc_descriptor (XEXP (XEXP (rtl, 1), 0), initialized);
10680           break;
10681         }
10682
10683       rtl = XEXP (rtl, 1);
10684       /* FALLTHRU */
10685
10686     case PARALLEL:
10687       {
10688         rtvec par_elems = XVEC (rtl, 0);
10689         int num_elem = GET_NUM_ELEM (par_elems);
10690         enum machine_mode mode;
10691         int i;
10692
10693         /* Create the first one, so we have something to add to.  */
10694         loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
10695                                      initialized);
10696         if (loc_result == NULL)
10697           return NULL;
10698         mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
10699         add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
10700         for (i = 1; i < num_elem; i++)
10701           {
10702             dw_loc_descr_ref temp;
10703
10704             temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
10705                                    initialized);
10706             if (temp == NULL)
10707               return NULL;
10708             add_loc_descr (&loc_result, temp);
10709             mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
10710             add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
10711           }
10712       }
10713       break;
10714
10715     default:
10716       gcc_unreachable ();
10717     }
10718
10719   return loc_result;
10720 }
10721
10722 /* Similar, but generate the descriptor from trees instead of rtl.  This comes
10723    up particularly with variable length arrays.  WANT_ADDRESS is 2 if this is
10724    a top-level invocation of loc_descriptor_from_tree; is 1 if this is not a
10725    top-level invocation, and we require the address of LOC; is 0 if we require
10726    the value of LOC.  */
10727
10728 static dw_loc_descr_ref
10729 loc_descriptor_from_tree_1 (tree loc, int want_address)
10730 {
10731   dw_loc_descr_ref ret, ret1;
10732   int have_address = 0;
10733   enum dwarf_location_atom op;
10734
10735   /* ??? Most of the time we do not take proper care for sign/zero
10736      extending the values properly.  Hopefully this won't be a real
10737      problem...  */
10738
10739   switch (TREE_CODE (loc))
10740     {
10741     case ERROR_MARK:
10742       return 0;
10743
10744     case PLACEHOLDER_EXPR:
10745       /* This case involves extracting fields from an object to determine the
10746          position of other fields.  We don't try to encode this here.  The
10747          only user of this is Ada, which encodes the needed information using
10748          the names of types.  */
10749       return 0;
10750
10751     case CALL_EXPR:
10752       return 0;
10753
10754     case PREINCREMENT_EXPR:
10755     case PREDECREMENT_EXPR:
10756     case POSTINCREMENT_EXPR:
10757     case POSTDECREMENT_EXPR:
10758       /* There are no opcodes for these operations.  */
10759       return 0;
10760
10761     case ADDR_EXPR:
10762       /* If we already want an address, there's nothing we can do.  */
10763       if (want_address)
10764         return 0;
10765
10766       /* Otherwise, process the argument and look for the address.  */
10767       return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 1);
10768
10769     case VAR_DECL:
10770       if (DECL_THREAD_LOCAL_P (loc))
10771         {
10772           rtx rtl;
10773           enum dwarf_location_atom first_op;
10774           enum dwarf_location_atom second_op;
10775
10776           if (targetm.have_tls)
10777             {
10778               /* If this is not defined, we have no way to emit the
10779                  data.  */
10780               if (!targetm.asm_out.output_dwarf_dtprel)
10781                 return 0;
10782
10783                /* The way DW_OP_GNU_push_tls_address is specified, we
10784                   can only look up addresses of objects in the current
10785                   module.  */
10786               if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
10787                 return 0;
10788               first_op = (enum dwarf_location_atom) INTERNAL_DW_OP_tls_addr;
10789               second_op = DW_OP_GNU_push_tls_address;
10790             }
10791           else
10792             {
10793               if (!targetm.emutls.debug_form_tls_address)
10794                 return 0;
10795               loc = emutls_decl (loc);
10796               first_op = DW_OP_addr;
10797               second_op = DW_OP_form_tls_address;
10798             }
10799
10800           rtl = rtl_for_decl_location (loc);
10801           if (rtl == NULL_RTX)
10802             return 0;
10803
10804           if (!MEM_P (rtl))
10805             return 0;
10806           rtl = XEXP (rtl, 0);
10807           if (! CONSTANT_P (rtl))
10808             return 0;
10809
10810           ret = new_loc_descr (first_op, 0, 0);
10811           ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
10812           ret->dw_loc_oprnd1.v.val_addr = rtl;
10813
10814           ret1 = new_loc_descr (second_op, 0, 0);
10815           add_loc_descr (&ret, ret1);
10816
10817           have_address = 1;
10818           break;
10819         }
10820       /* FALLTHRU */
10821
10822     case PARM_DECL:
10823       if (DECL_HAS_VALUE_EXPR_P (loc))
10824         return loc_descriptor_from_tree_1 (DECL_VALUE_EXPR (loc),
10825                                            want_address);
10826       /* FALLTHRU */
10827
10828     case RESULT_DECL:
10829     case FUNCTION_DECL:
10830       {
10831         rtx rtl = rtl_for_decl_location (loc);
10832
10833         if (rtl == NULL_RTX)
10834           return 0;
10835         else if (CONST_INT_P (rtl))
10836           {
10837             HOST_WIDE_INT val = INTVAL (rtl);
10838             if (TYPE_UNSIGNED (TREE_TYPE (loc)))
10839               val &= GET_MODE_MASK (DECL_MODE (loc));
10840             ret = int_loc_descriptor (val);
10841           }
10842         else if (GET_CODE (rtl) == CONST_STRING)
10843           return 0;
10844         else if (CONSTANT_P (rtl))
10845           {
10846             ret = new_loc_descr (DW_OP_addr, 0, 0);
10847             ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
10848             ret->dw_loc_oprnd1.v.val_addr = rtl;
10849           }
10850         else
10851           {
10852             enum machine_mode mode;
10853
10854             /* Certain constructs can only be represented at top-level.  */
10855             if (want_address == 2)
10856               return loc_descriptor (rtl, VAR_INIT_STATUS_INITIALIZED);
10857
10858             mode = GET_MODE (rtl);
10859             if (MEM_P (rtl))
10860               {
10861                 rtl = XEXP (rtl, 0);
10862                 have_address = 1;
10863               }
10864             ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
10865           }
10866       }
10867       break;
10868
10869     case INDIRECT_REF:
10870       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
10871       have_address = 1;
10872       break;
10873
10874     case COMPOUND_EXPR:
10875       return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), want_address);
10876
10877     CASE_CONVERT:
10878     case VIEW_CONVERT_EXPR:
10879     case SAVE_EXPR:
10880     case MODIFY_EXPR:
10881       return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), want_address);
10882
10883     case COMPONENT_REF:
10884     case BIT_FIELD_REF:
10885     case ARRAY_REF:
10886     case ARRAY_RANGE_REF:
10887       {
10888         tree obj, offset;
10889         HOST_WIDE_INT bitsize, bitpos, bytepos;
10890         enum machine_mode mode;
10891         int volatilep;
10892         int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
10893
10894         obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
10895                                    &unsignedp, &volatilep, false);
10896
10897         if (obj == loc)
10898           return 0;
10899
10900         ret = loc_descriptor_from_tree_1 (obj, 1);
10901         if (ret == 0
10902             || bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
10903           return 0;
10904
10905         if (offset != NULL_TREE)
10906           {
10907             /* Variable offset.  */
10908             ret1 = loc_descriptor_from_tree_1 (offset, 0);
10909             if (ret1 == 0)
10910               return 0;
10911             add_loc_descr (&ret, ret1);
10912             add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
10913           }
10914
10915         bytepos = bitpos / BITS_PER_UNIT;
10916         loc_descr_plus_const (&ret, bytepos);
10917
10918         have_address = 1;
10919         break;
10920       }
10921
10922     case INTEGER_CST:
10923       if (host_integerp (loc, 0))
10924         ret = int_loc_descriptor (tree_low_cst (loc, 0));
10925       else
10926         return 0;
10927       break;
10928
10929     case CONSTRUCTOR:
10930       {
10931         /* Get an RTL for this, if something has been emitted.  */
10932         rtx rtl = lookup_constant_def (loc);
10933         enum machine_mode mode;
10934
10935         if (!rtl || !MEM_P (rtl))
10936           return 0;
10937         mode = GET_MODE (rtl);
10938         rtl = XEXP (rtl, 0);
10939         ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
10940         have_address = 1;
10941         break;
10942       }
10943
10944     case TRUTH_AND_EXPR:
10945     case TRUTH_ANDIF_EXPR:
10946     case BIT_AND_EXPR:
10947       op = DW_OP_and;
10948       goto do_binop;
10949
10950     case TRUTH_XOR_EXPR:
10951     case BIT_XOR_EXPR:
10952       op = DW_OP_xor;
10953       goto do_binop;
10954
10955     case TRUTH_OR_EXPR:
10956     case TRUTH_ORIF_EXPR:
10957     case BIT_IOR_EXPR:
10958       op = DW_OP_or;
10959       goto do_binop;
10960
10961     case FLOOR_DIV_EXPR:
10962     case CEIL_DIV_EXPR:
10963     case ROUND_DIV_EXPR:
10964     case TRUNC_DIV_EXPR:
10965       op = DW_OP_div;
10966       goto do_binop;
10967
10968     case MINUS_EXPR:
10969       op = DW_OP_minus;
10970       goto do_binop;
10971
10972     case FLOOR_MOD_EXPR:
10973     case CEIL_MOD_EXPR:
10974     case ROUND_MOD_EXPR:
10975     case TRUNC_MOD_EXPR:
10976       op = DW_OP_mod;
10977       goto do_binop;
10978
10979     case MULT_EXPR:
10980       op = DW_OP_mul;
10981       goto do_binop;
10982
10983     case LSHIFT_EXPR:
10984       op = DW_OP_shl;
10985       goto do_binop;
10986
10987     case RSHIFT_EXPR:
10988       op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
10989       goto do_binop;
10990
10991     case POINTER_PLUS_EXPR:
10992     case PLUS_EXPR:
10993       if (TREE_CODE (TREE_OPERAND (loc, 1)) == INTEGER_CST
10994           && host_integerp (TREE_OPERAND (loc, 1), 0))
10995         {
10996           ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
10997           if (ret == 0)
10998             return 0;
10999
11000           loc_descr_plus_const (&ret, tree_low_cst (TREE_OPERAND (loc, 1), 0));
11001           break;
11002         }
11003
11004       op = DW_OP_plus;
11005       goto do_binop;
11006
11007     case LE_EXPR:
11008       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
11009         return 0;
11010
11011       op = DW_OP_le;
11012       goto do_binop;
11013
11014     case GE_EXPR:
11015       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
11016         return 0;
11017
11018       op = DW_OP_ge;
11019       goto do_binop;
11020
11021     case LT_EXPR:
11022       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
11023         return 0;
11024
11025       op = DW_OP_lt;
11026       goto do_binop;
11027
11028     case GT_EXPR:
11029       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
11030         return 0;
11031
11032       op = DW_OP_gt;
11033       goto do_binop;
11034
11035     case EQ_EXPR:
11036       op = DW_OP_eq;
11037       goto do_binop;
11038
11039     case NE_EXPR:
11040       op = DW_OP_ne;
11041       goto do_binop;
11042
11043     do_binop:
11044       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
11045       ret1 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
11046       if (ret == 0 || ret1 == 0)
11047         return 0;
11048
11049       add_loc_descr (&ret, ret1);
11050       add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11051       break;
11052
11053     case TRUTH_NOT_EXPR:
11054     case BIT_NOT_EXPR:
11055       op = DW_OP_not;
11056       goto do_unop;
11057
11058     case ABS_EXPR:
11059       op = DW_OP_abs;
11060       goto do_unop;
11061
11062     case NEGATE_EXPR:
11063       op = DW_OP_neg;
11064       goto do_unop;
11065
11066     do_unop:
11067       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
11068       if (ret == 0)
11069         return 0;
11070
11071       add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11072       break;
11073
11074     case MIN_EXPR:
11075     case MAX_EXPR:
11076       {
11077         const enum tree_code code =
11078           TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
11079
11080         loc = build3 (COND_EXPR, TREE_TYPE (loc),
11081                       build2 (code, integer_type_node,
11082                               TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
11083                       TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
11084       }
11085
11086       /* ... fall through ...  */
11087
11088     case COND_EXPR:
11089       {
11090         dw_loc_descr_ref lhs
11091           = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
11092         dw_loc_descr_ref rhs
11093           = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 2), 0);
11094         dw_loc_descr_ref bra_node, jump_node, tmp;
11095
11096         ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
11097         if (ret == 0 || lhs == 0 || rhs == 0)
11098           return 0;
11099
11100         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
11101         add_loc_descr (&ret, bra_node);
11102
11103         add_loc_descr (&ret, rhs);
11104         jump_node = new_loc_descr (DW_OP_skip, 0, 0);
11105         add_loc_descr (&ret, jump_node);
11106
11107         add_loc_descr (&ret, lhs);
11108         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
11109         bra_node->dw_loc_oprnd1.v.val_loc = lhs;
11110
11111         /* ??? Need a node to point the skip at.  Use a nop.  */
11112         tmp = new_loc_descr (DW_OP_nop, 0, 0);
11113         add_loc_descr (&ret, tmp);
11114         jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
11115         jump_node->dw_loc_oprnd1.v.val_loc = tmp;
11116       }
11117       break;
11118
11119     case FIX_TRUNC_EXPR:
11120       return 0;
11121
11122     default:
11123       /* Leave front-end specific codes as simply unknown.  This comes
11124          up, for instance, with the C STMT_EXPR.  */
11125       if ((unsigned int) TREE_CODE (loc)
11126           >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
11127         return 0;
11128
11129 #ifdef ENABLE_CHECKING
11130       /* Otherwise this is a generic code; we should just lists all of
11131          these explicitly.  We forgot one.  */
11132       gcc_unreachable ();
11133 #else
11134       /* In a release build, we want to degrade gracefully: better to
11135          generate incomplete debugging information than to crash.  */
11136       return NULL;
11137 #endif
11138     }
11139
11140   /* Show if we can't fill the request for an address.  */
11141   if (want_address && !have_address)
11142     return 0;
11143
11144   /* If we've got an address and don't want one, dereference.  */
11145   if (!want_address && have_address && ret)
11146     {
11147       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
11148
11149       if (size > DWARF2_ADDR_SIZE || size == -1)
11150         return 0;
11151       else if (size == DWARF2_ADDR_SIZE)
11152         op = DW_OP_deref;
11153       else
11154         op = DW_OP_deref_size;
11155
11156       add_loc_descr (&ret, new_loc_descr (op, size, 0));
11157     }
11158
11159   return ret;
11160 }
11161
11162 static inline dw_loc_descr_ref
11163 loc_descriptor_from_tree (tree loc)
11164 {
11165   return loc_descriptor_from_tree_1 (loc, 2);
11166 }
11167
11168 /* Given a value, round it up to the lowest multiple of `boundary'
11169    which is not less than the value itself.  */
11170
11171 static inline HOST_WIDE_INT
11172 ceiling (HOST_WIDE_INT value, unsigned int boundary)
11173 {
11174   return (((value + boundary - 1) / boundary) * boundary);
11175 }
11176
11177 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
11178    pointer to the declared type for the relevant field variable, or return
11179    `integer_type_node' if the given node turns out to be an
11180    ERROR_MARK node.  */
11181
11182 static inline tree
11183 field_type (const_tree decl)
11184 {
11185   tree type;
11186
11187   if (TREE_CODE (decl) == ERROR_MARK)
11188     return integer_type_node;
11189
11190   type = DECL_BIT_FIELD_TYPE (decl);
11191   if (type == NULL_TREE)
11192     type = TREE_TYPE (decl);
11193
11194   return type;
11195 }
11196
11197 /* Given a pointer to a tree node, return the alignment in bits for
11198    it, or else return BITS_PER_WORD if the node actually turns out to
11199    be an ERROR_MARK node.  */
11200
11201 static inline unsigned
11202 simple_type_align_in_bits (const_tree type)
11203 {
11204   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
11205 }
11206
11207 static inline unsigned
11208 simple_decl_align_in_bits (const_tree decl)
11209 {
11210   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
11211 }
11212
11213 /* Return the result of rounding T up to ALIGN.  */
11214
11215 static inline HOST_WIDE_INT
11216 round_up_to_align (HOST_WIDE_INT t, unsigned int align)
11217 {
11218   /* We must be careful if T is negative because HOST_WIDE_INT can be
11219      either "above" or "below" unsigned int as per the C promotion
11220      rules, depending on the host, thus making the signedness of the
11221      direct multiplication and division unpredictable.  */
11222   unsigned HOST_WIDE_INT u = (unsigned HOST_WIDE_INT) t;
11223
11224   u += align - 1;
11225   u /= align;
11226   u *= align;
11227
11228   return (HOST_WIDE_INT) u;
11229 }
11230
11231 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
11232    lowest addressed byte of the "containing object" for the given FIELD_DECL,
11233    or return 0 if we are unable to determine what that offset is, either
11234    because the argument turns out to be a pointer to an ERROR_MARK node, or
11235    because the offset is actually variable.  (We can't handle the latter case
11236    just yet).  */
11237
11238 static HOST_WIDE_INT
11239 field_byte_offset (const_tree decl)
11240 {
11241   HOST_WIDE_INT object_offset_in_bits;
11242   HOST_WIDE_INT bitpos_int;
11243
11244   if (TREE_CODE (decl) == ERROR_MARK)
11245     return 0;
11246
11247   gcc_assert (TREE_CODE (decl) == FIELD_DECL);
11248
11249   /* We cannot yet cope with fields whose positions are variable, so
11250      for now, when we see such things, we simply return 0.  Someday, we may
11251      be able to handle such cases, but it will be damn difficult.  */
11252   if (! host_integerp (bit_position (decl), 0))
11253     return 0;
11254
11255   bitpos_int = int_bit_position (decl);
11256
11257 #ifdef PCC_BITFIELD_TYPE_MATTERS
11258   if (PCC_BITFIELD_TYPE_MATTERS)
11259     {
11260       tree type;
11261       tree field_size_tree;
11262       HOST_WIDE_INT deepest_bitpos;
11263       unsigned HOST_WIDE_INT field_size_in_bits;
11264       unsigned int type_align_in_bits;
11265       unsigned int decl_align_in_bits;
11266       unsigned HOST_WIDE_INT type_size_in_bits;
11267
11268       type = field_type (decl);
11269       type_size_in_bits = simple_type_size_in_bits (type);
11270       type_align_in_bits = simple_type_align_in_bits (type);
11271
11272       field_size_tree = DECL_SIZE (decl);
11273
11274       /* The size could be unspecified if there was an error, or for
11275          a flexible array member.  */
11276       if (!field_size_tree)
11277         field_size_tree = bitsize_zero_node;
11278
11279       /* If the size of the field is not constant, use the type size.  */
11280       if (host_integerp (field_size_tree, 1))
11281         field_size_in_bits = tree_low_cst (field_size_tree, 1);
11282       else
11283         field_size_in_bits = type_size_in_bits;
11284
11285       decl_align_in_bits = simple_decl_align_in_bits (decl);
11286
11287       /* The GCC front-end doesn't make any attempt to keep track of the
11288          starting bit offset (relative to the start of the containing
11289          structure type) of the hypothetical "containing object" for a
11290          bit-field.  Thus, when computing the byte offset value for the
11291          start of the "containing object" of a bit-field, we must deduce
11292          this information on our own. This can be rather tricky to do in
11293          some cases.  For example, handling the following structure type
11294          definition when compiling for an i386/i486 target (which only
11295          aligns long long's to 32-bit boundaries) can be very tricky:
11296
11297          struct S { int field1; long long field2:31; };
11298
11299          Fortunately, there is a simple rule-of-thumb which can be used
11300          in such cases.  When compiling for an i386/i486, GCC will
11301          allocate 8 bytes for the structure shown above.  It decides to
11302          do this based upon one simple rule for bit-field allocation.
11303          GCC allocates each "containing object" for each bit-field at
11304          the first (i.e. lowest addressed) legitimate alignment boundary
11305          (based upon the required minimum alignment for the declared
11306          type of the field) which it can possibly use, subject to the
11307          condition that there is still enough available space remaining
11308          in the containing object (when allocated at the selected point)
11309          to fully accommodate all of the bits of the bit-field itself.
11310
11311          This simple rule makes it obvious why GCC allocates 8 bytes for
11312          each object of the structure type shown above.  When looking
11313          for a place to allocate the "containing object" for `field2',
11314          the compiler simply tries to allocate a 64-bit "containing
11315          object" at each successive 32-bit boundary (starting at zero)
11316          until it finds a place to allocate that 64- bit field such that
11317          at least 31 contiguous (and previously unallocated) bits remain
11318          within that selected 64 bit field.  (As it turns out, for the
11319          example above, the compiler finds it is OK to allocate the
11320          "containing object" 64-bit field at bit-offset zero within the
11321          structure type.)
11322
11323          Here we attempt to work backwards from the limited set of facts
11324          we're given, and we try to deduce from those facts, where GCC
11325          must have believed that the containing object started (within
11326          the structure type). The value we deduce is then used (by the
11327          callers of this routine) to generate DW_AT_location and
11328          DW_AT_bit_offset attributes for fields (both bit-fields and, in
11329          the case of DW_AT_location, regular fields as well).  */
11330
11331       /* Figure out the bit-distance from the start of the structure to
11332          the "deepest" bit of the bit-field.  */
11333       deepest_bitpos = bitpos_int + field_size_in_bits;
11334
11335       /* This is the tricky part.  Use some fancy footwork to deduce
11336          where the lowest addressed bit of the containing object must
11337          be.  */
11338       object_offset_in_bits = deepest_bitpos - type_size_in_bits;
11339
11340       /* Round up to type_align by default.  This works best for
11341          bitfields.  */
11342       object_offset_in_bits
11343         = round_up_to_align (object_offset_in_bits, type_align_in_bits);
11344
11345       if (object_offset_in_bits > bitpos_int)
11346         {
11347           object_offset_in_bits = deepest_bitpos - type_size_in_bits;
11348
11349           /* Round up to decl_align instead.  */
11350           object_offset_in_bits
11351             = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
11352         }
11353     }
11354   else
11355 #endif
11356     object_offset_in_bits = bitpos_int;
11357
11358   return object_offset_in_bits / BITS_PER_UNIT;
11359 }
11360 \f
11361 /* The following routines define various Dwarf attributes and any data
11362    associated with them.  */
11363
11364 /* Add a location description attribute value to a DIE.
11365
11366    This emits location attributes suitable for whole variables and
11367    whole parameters.  Note that the location attributes for struct fields are
11368    generated by the routine `data_member_location_attribute' below.  */
11369
11370 static inline void
11371 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
11372                              dw_loc_descr_ref descr)
11373 {
11374   if (descr != 0)
11375     add_AT_loc (die, attr_kind, descr);
11376 }
11377
11378 /* Attach the specialized form of location attribute used for data members of
11379    struct and union types.  In the special case of a FIELD_DECL node which
11380    represents a bit-field, the "offset" part of this special location
11381    descriptor must indicate the distance in bytes from the lowest-addressed
11382    byte of the containing struct or union type to the lowest-addressed byte of
11383    the "containing object" for the bit-field.  (See the `field_byte_offset'
11384    function above).
11385
11386    For any given bit-field, the "containing object" is a hypothetical object
11387    (of some integral or enum type) within which the given bit-field lives.  The
11388    type of this hypothetical "containing object" is always the same as the
11389    declared type of the individual bit-field itself (for GCC anyway... the
11390    DWARF spec doesn't actually mandate this).  Note that it is the size (in
11391    bytes) of the hypothetical "containing object" which will be given in the
11392    DW_AT_byte_size attribute for this bit-field.  (See the
11393    `byte_size_attribute' function below.)  It is also used when calculating the
11394    value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
11395    function below.)  */
11396
11397 static void
11398 add_data_member_location_attribute (dw_die_ref die, tree decl)
11399 {
11400   HOST_WIDE_INT offset;
11401   dw_loc_descr_ref loc_descr = 0;
11402
11403   if (TREE_CODE (decl) == TREE_BINFO)
11404     {
11405       /* We're working on the TAG_inheritance for a base class.  */
11406       if (BINFO_VIRTUAL_P (decl) && is_cxx ())
11407         {
11408           /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
11409              aren't at a fixed offset from all (sub)objects of the same
11410              type.  We need to extract the appropriate offset from our
11411              vtable.  The following dwarf expression means
11412
11413                BaseAddr = ObAddr + *((*ObAddr) - Offset)
11414
11415              This is specific to the V3 ABI, of course.  */
11416
11417           dw_loc_descr_ref tmp;
11418
11419           /* Make a copy of the object address.  */
11420           tmp = new_loc_descr (DW_OP_dup, 0, 0);
11421           add_loc_descr (&loc_descr, tmp);
11422
11423           /* Extract the vtable address.  */
11424           tmp = new_loc_descr (DW_OP_deref, 0, 0);
11425           add_loc_descr (&loc_descr, tmp);
11426
11427           /* Calculate the address of the offset.  */
11428           offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
11429           gcc_assert (offset < 0);
11430
11431           tmp = int_loc_descriptor (-offset);
11432           add_loc_descr (&loc_descr, tmp);
11433           tmp = new_loc_descr (DW_OP_minus, 0, 0);
11434           add_loc_descr (&loc_descr, tmp);
11435
11436           /* Extract the offset.  */
11437           tmp = new_loc_descr (DW_OP_deref, 0, 0);
11438           add_loc_descr (&loc_descr, tmp);
11439
11440           /* Add it to the object address.  */
11441           tmp = new_loc_descr (DW_OP_plus, 0, 0);
11442           add_loc_descr (&loc_descr, tmp);
11443         }
11444       else
11445         offset = tree_low_cst (BINFO_OFFSET (decl), 0);
11446     }
11447   else
11448     offset = field_byte_offset (decl);
11449
11450   if (! loc_descr)
11451     {
11452       enum dwarf_location_atom op;
11453
11454       /* The DWARF2 standard says that we should assume that the structure
11455          address is already on the stack, so we can specify a structure field
11456          address by using DW_OP_plus_uconst.  */
11457
11458 #ifdef MIPS_DEBUGGING_INFO
11459       /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
11460          operator correctly.  It works only if we leave the offset on the
11461          stack.  */
11462       op = DW_OP_constu;
11463 #else
11464       op = DW_OP_plus_uconst;
11465 #endif
11466
11467       loc_descr = new_loc_descr (op, offset, 0);
11468     }
11469
11470   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
11471 }
11472
11473 /* Writes integer values to dw_vec_const array.  */
11474
11475 static void
11476 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
11477 {
11478   while (size != 0)
11479     {
11480       *dest++ = val & 0xff;
11481       val >>= 8;
11482       --size;
11483     }
11484 }
11485
11486 /* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
11487
11488 static HOST_WIDE_INT
11489 extract_int (const unsigned char *src, unsigned int size)
11490 {
11491   HOST_WIDE_INT val = 0;
11492
11493   src += size;
11494   while (size != 0)
11495     {
11496       val <<= 8;
11497       val |= *--src & 0xff;
11498       --size;
11499     }
11500   return val;
11501 }
11502
11503 /* Writes floating point values to dw_vec_const array.  */
11504
11505 static void
11506 insert_float (const_rtx rtl, unsigned char *array)
11507 {
11508   REAL_VALUE_TYPE rv;
11509   long val[4];
11510   int i;
11511
11512   REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
11513   real_to_target (val, &rv, GET_MODE (rtl));
11514
11515   /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
11516   for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
11517     {
11518       insert_int (val[i], 4, array);
11519       array += 4;
11520     }
11521 }
11522
11523 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
11524    does not have a "location" either in memory or in a register.  These
11525    things can arise in GNU C when a constant is passed as an actual parameter
11526    to an inlined function.  They can also arise in C++ where declared
11527    constants do not necessarily get memory "homes".  */
11528
11529 static void
11530 add_const_value_attribute (dw_die_ref die, rtx rtl)
11531 {
11532   switch (GET_CODE (rtl))
11533     {
11534     case CONST_INT:
11535       {
11536         HOST_WIDE_INT val = INTVAL (rtl);
11537
11538         if (val < 0)
11539           add_AT_int (die, DW_AT_const_value, val);
11540         else
11541           add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
11542       }
11543       break;
11544
11545     case CONST_DOUBLE:
11546       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
11547          floating-point constant.  A CONST_DOUBLE is used whenever the
11548          constant requires more than one word in order to be adequately
11549          represented.  We output CONST_DOUBLEs as blocks.  */
11550       {
11551         enum machine_mode mode = GET_MODE (rtl);
11552
11553         if (SCALAR_FLOAT_MODE_P (mode))
11554           {
11555             unsigned int length = GET_MODE_SIZE (mode);
11556             unsigned char *array = GGC_NEWVEC (unsigned char, length);
11557
11558             insert_float (rtl, array);
11559             add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
11560           }
11561         else
11562           {
11563             /* ??? We really should be using HOST_WIDE_INT throughout.  */
11564             gcc_assert (HOST_BITS_PER_LONG == HOST_BITS_PER_WIDE_INT);
11565
11566             add_AT_long_long (die, DW_AT_const_value,
11567                               CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
11568           }
11569       }
11570       break;
11571
11572     case CONST_VECTOR:
11573       {
11574         enum machine_mode mode = GET_MODE (rtl);
11575         unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
11576         unsigned int length = CONST_VECTOR_NUNITS (rtl);
11577         unsigned char *array = GGC_NEWVEC (unsigned char, length * elt_size);
11578         unsigned int i;
11579         unsigned char *p;
11580
11581         switch (GET_MODE_CLASS (mode))
11582           {
11583           case MODE_VECTOR_INT:
11584             for (i = 0, p = array; i < length; i++, p += elt_size)
11585               {
11586                 rtx elt = CONST_VECTOR_ELT (rtl, i);
11587                 HOST_WIDE_INT lo, hi;
11588
11589                 switch (GET_CODE (elt))
11590                   {
11591                   case CONST_INT:
11592                     lo = INTVAL (elt);
11593                     hi = -(lo < 0);
11594                     break;
11595
11596                   case CONST_DOUBLE:
11597                     lo = CONST_DOUBLE_LOW (elt);
11598                     hi = CONST_DOUBLE_HIGH (elt);
11599                     break;
11600
11601                   default:
11602                     gcc_unreachable ();
11603                   }
11604
11605                 if (elt_size <= sizeof (HOST_WIDE_INT))
11606                   insert_int (lo, elt_size, p);
11607                 else
11608                   {
11609                     unsigned char *p0 = p;
11610                     unsigned char *p1 = p + sizeof (HOST_WIDE_INT);
11611
11612                     gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
11613                     if (WORDS_BIG_ENDIAN)
11614                       {
11615                         p0 = p1;
11616                         p1 = p;
11617                       }
11618                     insert_int (lo, sizeof (HOST_WIDE_INT), p0);
11619                     insert_int (hi, sizeof (HOST_WIDE_INT), p1);
11620                   }
11621               }
11622             break;
11623
11624           case MODE_VECTOR_FLOAT:
11625             for (i = 0, p = array; i < length; i++, p += elt_size)
11626               {
11627                 rtx elt = CONST_VECTOR_ELT (rtl, i);
11628                 insert_float (elt, p);
11629               }
11630             break;
11631
11632           default:
11633             gcc_unreachable ();
11634           }
11635
11636         add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
11637       }
11638       break;
11639
11640     case CONST_STRING:
11641       add_AT_string (die, DW_AT_const_value, XSTR (rtl, 0));
11642       break;
11643
11644     case SYMBOL_REF:
11645     case LABEL_REF:
11646     case CONST:
11647       add_AT_addr (die, DW_AT_const_value, rtl);
11648       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
11649       break;
11650
11651     case PLUS:
11652       /* In cases where an inlined instance of an inline function is passed
11653          the address of an `auto' variable (which is local to the caller) we
11654          can get a situation where the DECL_RTL of the artificial local
11655          variable (for the inlining) which acts as a stand-in for the
11656          corresponding formal parameter (of the inline function) will look
11657          like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
11658          exactly a compile-time constant expression, but it isn't the address
11659          of the (artificial) local variable either.  Rather, it represents the
11660          *value* which the artificial local variable always has during its
11661          lifetime.  We currently have no way to represent such quasi-constant
11662          values in Dwarf, so for now we just punt and generate nothing.  */
11663       break;
11664
11665     default:
11666       /* No other kinds of rtx should be possible here.  */
11667       gcc_unreachable ();
11668     }
11669
11670 }
11671
11672 /* Determine whether the evaluation of EXPR references any variables
11673    or functions which aren't otherwise used (and therefore may not be
11674    output).  */
11675 static tree
11676 reference_to_unused (tree * tp, int * walk_subtrees,
11677                      void * data ATTRIBUTE_UNUSED)
11678 {
11679   if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
11680     *walk_subtrees = 0;
11681
11682   if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
11683       && ! TREE_ASM_WRITTEN (*tp))
11684     return *tp;
11685   /* ???  The C++ FE emits debug information for using decls, so
11686      putting gcc_unreachable here falls over.  See PR31899.  For now
11687      be conservative.  */
11688   else if (!cgraph_global_info_ready
11689            && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
11690     return *tp;
11691   else if (DECL_P (*tp) && TREE_CODE (*tp) == VAR_DECL)
11692     {
11693       struct varpool_node *node = varpool_node (*tp);
11694       if (!node->needed)
11695         return *tp;
11696     }
11697   else if (DECL_P (*tp) && TREE_CODE (*tp) == FUNCTION_DECL
11698            && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
11699     {
11700       struct cgraph_node *node = cgraph_node (*tp);
11701       if (node->process || TREE_ASM_WRITTEN (*tp))
11702         return *tp;
11703     }
11704   else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
11705     return *tp;
11706
11707   return NULL_TREE;
11708 }
11709
11710 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
11711    for use in a later add_const_value_attribute call.  */
11712
11713 static rtx
11714 rtl_for_decl_init (tree init, tree type)
11715 {
11716   rtx rtl = NULL_RTX;
11717
11718   /* If a variable is initialized with a string constant without embedded
11719      zeros, build CONST_STRING.  */
11720   if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
11721     {
11722       tree enttype = TREE_TYPE (type);
11723       tree domain = TYPE_DOMAIN (type);
11724       enum machine_mode mode = TYPE_MODE (enttype);
11725
11726       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
11727           && domain
11728           && integer_zerop (TYPE_MIN_VALUE (domain))
11729           && compare_tree_int (TYPE_MAX_VALUE (domain),
11730                                TREE_STRING_LENGTH (init) - 1) == 0
11731           && ((size_t) TREE_STRING_LENGTH (init)
11732               == strlen (TREE_STRING_POINTER (init)) + 1))
11733         rtl = gen_rtx_CONST_STRING (VOIDmode,
11734                                     ggc_strdup (TREE_STRING_POINTER (init)));
11735     }
11736   /* Other aggregates, and complex values, could be represented using
11737      CONCAT: FIXME!  */
11738   else if (AGGREGATE_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
11739     ;
11740   /* Vectors only work if their mode is supported by the target.
11741      FIXME: generic vectors ought to work too.  */
11742   else if (TREE_CODE (type) == VECTOR_TYPE && TYPE_MODE (type) == BLKmode)
11743     ;
11744   /* If the initializer is something that we know will expand into an
11745      immediate RTL constant, expand it now.  We must be careful not to
11746      reference variables which won't be output.  */
11747   else if (initializer_constant_valid_p (init, type)
11748            && ! walk_tree (&init, reference_to_unused, NULL, NULL))
11749     {
11750       /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
11751          possible.  */
11752       if (TREE_CODE (type) == VECTOR_TYPE)
11753         switch (TREE_CODE (init))
11754           {
11755           case VECTOR_CST:
11756             break;
11757           case CONSTRUCTOR:
11758             if (TREE_CONSTANT (init))
11759               {
11760                 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
11761                 bool constant_p = true;
11762                 tree value;
11763                 unsigned HOST_WIDE_INT ix;
11764
11765                 /* Even when ctor is constant, it might contain non-*_CST
11766                    elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
11767                    belong into VECTOR_CST nodes.  */
11768                 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
11769                   if (!CONSTANT_CLASS_P (value))
11770                     {
11771                       constant_p = false;
11772                       break;
11773                     }
11774
11775                 if (constant_p)
11776                   {
11777                     init = build_vector_from_ctor (type, elts);
11778                     break;
11779                   }
11780               }
11781             /* FALLTHRU */
11782
11783           default:
11784             return NULL;
11785           }
11786
11787       rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
11788
11789       /* If expand_expr returns a MEM, it wasn't immediate.  */
11790       gcc_assert (!rtl || !MEM_P (rtl));
11791     }
11792
11793   return rtl;
11794 }
11795
11796 /* Generate RTL for the variable DECL to represent its location.  */
11797
11798 static rtx
11799 rtl_for_decl_location (tree decl)
11800 {
11801   rtx rtl;
11802
11803   /* Here we have to decide where we are going to say the parameter "lives"
11804      (as far as the debugger is concerned).  We only have a couple of
11805      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
11806
11807      DECL_RTL normally indicates where the parameter lives during most of the
11808      activation of the function.  If optimization is enabled however, this
11809      could be either NULL or else a pseudo-reg.  Both of those cases indicate
11810      that the parameter doesn't really live anywhere (as far as the code
11811      generation parts of GCC are concerned) during most of the function's
11812      activation.  That will happen (for example) if the parameter is never
11813      referenced within the function.
11814
11815      We could just generate a location descriptor here for all non-NULL
11816      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
11817      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
11818      where DECL_RTL is NULL or is a pseudo-reg.
11819
11820      Note however that we can only get away with using DECL_INCOMING_RTL as
11821      a backup substitute for DECL_RTL in certain limited cases.  In cases
11822      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
11823      we can be sure that the parameter was passed using the same type as it is
11824      declared to have within the function, and that its DECL_INCOMING_RTL
11825      points us to a place where a value of that type is passed.
11826
11827      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
11828      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
11829      because in these cases DECL_INCOMING_RTL points us to a value of some
11830      type which is *different* from the type of the parameter itself.  Thus,
11831      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
11832      such cases, the debugger would end up (for example) trying to fetch a
11833      `float' from a place which actually contains the first part of a
11834      `double'.  That would lead to really incorrect and confusing
11835      output at debug-time.
11836
11837      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
11838      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
11839      are a couple of exceptions however.  On little-endian machines we can
11840      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
11841      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
11842      an integral type that is smaller than TREE_TYPE (decl). These cases arise
11843      when (on a little-endian machine) a non-prototyped function has a
11844      parameter declared to be of type `short' or `char'.  In such cases,
11845      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
11846      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
11847      passed `int' value.  If the debugger then uses that address to fetch
11848      a `short' or a `char' (on a little-endian machine) the result will be
11849      the correct data, so we allow for such exceptional cases below.
11850
11851      Note that our goal here is to describe the place where the given formal
11852      parameter lives during most of the function's activation (i.e. between the
11853      end of the prologue and the start of the epilogue).  We'll do that as best
11854      as we can. Note however that if the given formal parameter is modified
11855      sometime during the execution of the function, then a stack backtrace (at
11856      debug-time) will show the function as having been called with the *new*
11857      value rather than the value which was originally passed in.  This happens
11858      rarely enough that it is not a major problem, but it *is* a problem, and
11859      I'd like to fix it.
11860
11861      A future version of dwarf2out.c may generate two additional attributes for
11862      any given DW_TAG_formal_parameter DIE which will describe the "passed
11863      type" and the "passed location" for the given formal parameter in addition
11864      to the attributes we now generate to indicate the "declared type" and the
11865      "active location" for each parameter.  This additional set of attributes
11866      could be used by debuggers for stack backtraces. Separately, note that
11867      sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
11868      This happens (for example) for inlined-instances of inline function formal
11869      parameters which are never referenced.  This really shouldn't be
11870      happening.  All PARM_DECL nodes should get valid non-NULL
11871      DECL_INCOMING_RTL values.  FIXME.  */
11872
11873   /* Use DECL_RTL as the "location" unless we find something better.  */
11874   rtl = DECL_RTL_IF_SET (decl);
11875
11876   /* When generating abstract instances, ignore everything except
11877      constants, symbols living in memory, and symbols living in
11878      fixed registers.  */
11879   if (! reload_completed)
11880     {
11881       if (rtl
11882           && (CONSTANT_P (rtl)
11883               || (MEM_P (rtl)
11884                   && CONSTANT_P (XEXP (rtl, 0)))
11885               || (REG_P (rtl)
11886                   && TREE_CODE (decl) == VAR_DECL
11887                   && TREE_STATIC (decl))))
11888         {
11889           rtl = targetm.delegitimize_address (rtl);
11890           return rtl;
11891         }
11892       rtl = NULL_RTX;
11893     }
11894   else if (TREE_CODE (decl) == PARM_DECL)
11895     {
11896       if (rtl == NULL_RTX || is_pseudo_reg (rtl))
11897         {
11898           tree declared_type = TREE_TYPE (decl);
11899           tree passed_type = DECL_ARG_TYPE (decl);
11900           enum machine_mode dmode = TYPE_MODE (declared_type);
11901           enum machine_mode pmode = TYPE_MODE (passed_type);
11902
11903           /* This decl represents a formal parameter which was optimized out.
11904              Note that DECL_INCOMING_RTL may be NULL in here, but we handle
11905              all cases where (rtl == NULL_RTX) just below.  */
11906           if (dmode == pmode)
11907             rtl = DECL_INCOMING_RTL (decl);
11908           else if (SCALAR_INT_MODE_P (dmode)
11909                    && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
11910                    && DECL_INCOMING_RTL (decl))
11911             {
11912               rtx inc = DECL_INCOMING_RTL (decl);
11913               if (REG_P (inc))
11914                 rtl = inc;
11915               else if (MEM_P (inc))
11916                 {
11917                   if (BYTES_BIG_ENDIAN)
11918                     rtl = adjust_address_nv (inc, dmode,
11919                                              GET_MODE_SIZE (pmode)
11920                                              - GET_MODE_SIZE (dmode));
11921                   else
11922                     rtl = inc;
11923                 }
11924             }
11925         }
11926
11927       /* If the parm was passed in registers, but lives on the stack, then
11928          make a big endian correction if the mode of the type of the
11929          parameter is not the same as the mode of the rtl.  */
11930       /* ??? This is the same series of checks that are made in dbxout.c before
11931          we reach the big endian correction code there.  It isn't clear if all
11932          of these checks are necessary here, but keeping them all is the safe
11933          thing to do.  */
11934       else if (MEM_P (rtl)
11935                && XEXP (rtl, 0) != const0_rtx
11936                && ! CONSTANT_P (XEXP (rtl, 0))
11937                /* Not passed in memory.  */
11938                && !MEM_P (DECL_INCOMING_RTL (decl))
11939                /* Not passed by invisible reference.  */
11940                && (!REG_P (XEXP (rtl, 0))
11941                    || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
11942                    || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
11943 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
11944                    || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
11945 #endif
11946                      )
11947                /* Big endian correction check.  */
11948                && BYTES_BIG_ENDIAN
11949                && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
11950                && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
11951                    < UNITS_PER_WORD))
11952         {
11953           int offset = (UNITS_PER_WORD
11954                         - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
11955
11956           rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
11957                              plus_constant (XEXP (rtl, 0), offset));
11958         }
11959     }
11960   else if (TREE_CODE (decl) == VAR_DECL
11961            && rtl
11962            && MEM_P (rtl)
11963            && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
11964            && BYTES_BIG_ENDIAN)
11965     {
11966       int rsize = GET_MODE_SIZE (GET_MODE (rtl));
11967       int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
11968
11969       /* If a variable is declared "register" yet is smaller than
11970          a register, then if we store the variable to memory, it
11971          looks like we're storing a register-sized value, when in
11972          fact we are not.  We need to adjust the offset of the
11973          storage location to reflect the actual value's bytes,
11974          else gdb will not be able to display it.  */
11975       if (rsize > dsize)
11976         rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
11977                            plus_constant (XEXP (rtl, 0), rsize-dsize));
11978     }
11979
11980   /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
11981      and will have been substituted directly into all expressions that use it.
11982      C does not have such a concept, but C++ and other languages do.  */
11983   if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
11984     rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
11985
11986   if (rtl)
11987     rtl = targetm.delegitimize_address (rtl);
11988
11989   /* If we don't look past the constant pool, we risk emitting a
11990      reference to a constant pool entry that isn't referenced from
11991      code, and thus is not emitted.  */
11992   if (rtl)
11993     rtl = avoid_constant_pool_reference (rtl);
11994
11995   return rtl;
11996 }
11997
11998 /* We need to figure out what section we should use as the base for the
11999    address ranges where a given location is valid.
12000    1. If this particular DECL has a section associated with it, use that.
12001    2. If this function has a section associated with it, use that.
12002    3. Otherwise, use the text section.
12003    XXX: If you split a variable across multiple sections, we won't notice.  */
12004
12005 static const char *
12006 secname_for_decl (const_tree decl)
12007 {
12008   const char *secname;
12009
12010   if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
12011     {
12012       tree sectree = DECL_SECTION_NAME (decl);
12013       secname = TREE_STRING_POINTER (sectree);
12014     }
12015   else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
12016     {
12017       tree sectree = DECL_SECTION_NAME (current_function_decl);
12018       secname = TREE_STRING_POINTER (sectree);
12019     }
12020   else if (cfun && in_cold_section_p)
12021     secname = crtl->subsections.cold_section_label;
12022   else
12023     secname = text_section_label;
12024
12025   return secname;
12026 }
12027
12028 /* Check whether decl is a Fortran COMMON symbol.  If not, NULL_TREE is
12029    returned.  If so, the decl for the COMMON block is returned, and the
12030    value is the offset into the common block for the symbol.  */
12031
12032 static tree
12033 fortran_common (tree decl, HOST_WIDE_INT *value)
12034 {
12035   tree val_expr, cvar;
12036   enum machine_mode mode;
12037   HOST_WIDE_INT bitsize, bitpos;
12038   tree offset;
12039   int volatilep = 0, unsignedp = 0;
12040
12041   /* If the decl isn't a VAR_DECL, or if it isn't public or static, or if
12042      it does not have a value (the offset into the common area), or if it
12043      is thread local (as opposed to global) then it isn't common, and shouldn't
12044      be handled as such.  */
12045   if (TREE_CODE (decl) != VAR_DECL
12046       || !TREE_PUBLIC (decl)
12047       || !TREE_STATIC (decl)
12048       || !DECL_HAS_VALUE_EXPR_P (decl)
12049       || !is_fortran ())
12050     return NULL_TREE;
12051
12052   val_expr = DECL_VALUE_EXPR (decl);
12053   if (TREE_CODE (val_expr) != COMPONENT_REF)
12054     return NULL_TREE;
12055
12056   cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
12057                               &mode, &unsignedp, &volatilep, true);
12058
12059   if (cvar == NULL_TREE
12060       || TREE_CODE (cvar) != VAR_DECL
12061       || DECL_ARTIFICIAL (cvar)
12062       || !TREE_PUBLIC (cvar))
12063     return NULL_TREE;
12064
12065   *value = 0;
12066   if (offset != NULL)
12067     {
12068       if (!host_integerp (offset, 0))
12069         return NULL_TREE;
12070       *value = tree_low_cst (offset, 0);
12071     }
12072   if (bitpos != 0)
12073     *value += bitpos / BITS_PER_UNIT;
12074
12075   return cvar;
12076 }
12077
12078 /* Dereference a location expression LOC if DECL is passed by invisible
12079    reference.  */
12080
12081 static dw_loc_descr_ref
12082 loc_by_reference (dw_loc_descr_ref loc, tree decl)
12083 {
12084   HOST_WIDE_INT size;
12085   enum dwarf_location_atom op;
12086
12087   if (loc == NULL)
12088     return NULL;
12089
12090   if ((TREE_CODE (decl) != PARM_DECL
12091        && TREE_CODE (decl) != RESULT_DECL
12092        && TREE_CODE (decl) != VAR_DECL)
12093       || !DECL_BY_REFERENCE (decl))
12094     return loc;
12095
12096   /* If loc is DW_OP_reg{0...31,x}, don't add DW_OP_deref, instead
12097      change it into corresponding DW_OP_breg{0...31,x} 0.  Then the
12098      location expression is considered to be address of a memory location,
12099      rather than the register itself.  */
12100   if (((loc->dw_loc_opc >= DW_OP_reg0 && loc->dw_loc_opc <= DW_OP_reg31)
12101        || loc->dw_loc_opc == DW_OP_regx)
12102       && (loc->dw_loc_next == NULL
12103           || (loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_uninit
12104               && loc->dw_loc_next->dw_loc_next == NULL)))
12105     {
12106       if (loc->dw_loc_opc == DW_OP_regx)
12107         {
12108           loc->dw_loc_opc = DW_OP_bregx;
12109           loc->dw_loc_oprnd2.v.val_int = 0;
12110         }
12111       else
12112         {
12113           loc->dw_loc_opc
12114             = (enum dwarf_location_atom)
12115               (loc->dw_loc_opc + (DW_OP_breg0 - DW_OP_reg0));
12116           loc->dw_loc_oprnd1.v.val_int = 0;
12117         }
12118       return loc;
12119     }
12120
12121   size = int_size_in_bytes (TREE_TYPE (decl));
12122   if (size > DWARF2_ADDR_SIZE || size == -1)
12123     return 0;
12124   else if (size == DWARF2_ADDR_SIZE)
12125     op = DW_OP_deref;
12126   else
12127     op = DW_OP_deref_size;
12128   add_loc_descr (&loc, new_loc_descr (op, size, 0));
12129   return loc;
12130 }
12131
12132 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
12133    data attribute for a variable or a parameter.  We generate the
12134    DW_AT_const_value attribute only in those cases where the given variable
12135    or parameter does not have a true "location" either in memory or in a
12136    register.  This can happen (for example) when a constant is passed as an
12137    actual argument in a call to an inline function.  (It's possible that
12138    these things can crop up in other ways also.)  Note that one type of
12139    constant value which can be passed into an inlined function is a constant
12140    pointer.  This can happen for example if an actual argument in an inlined
12141    function call evaluates to a compile-time constant address.  */
12142
12143 static void
12144 add_location_or_const_value_attribute (dw_die_ref die, tree decl,
12145                                        enum dwarf_attribute attr)
12146 {
12147   rtx rtl;
12148   dw_loc_descr_ref descr;
12149   var_loc_list *loc_list;
12150   struct var_loc_node *node;
12151   if (TREE_CODE (decl) == ERROR_MARK)
12152     return;
12153
12154   gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
12155               || TREE_CODE (decl) == RESULT_DECL);
12156
12157   /* See if we possibly have multiple locations for this variable.  */
12158   loc_list = lookup_decl_loc (decl);
12159
12160   /* If it truly has multiple locations, the first and last node will
12161      differ.  */
12162   if (loc_list && loc_list->first != loc_list->last)
12163     {
12164       const char *endname, *secname;
12165       dw_loc_list_ref list;
12166       rtx varloc;
12167       enum var_init_status initialized;
12168
12169       /* Now that we know what section we are using for a base,
12170          actually construct the list of locations.
12171          The first location information is what is passed to the
12172          function that creates the location list, and the remaining
12173          locations just get added on to that list.
12174          Note that we only know the start address for a location
12175          (IE location changes), so to build the range, we use
12176          the range [current location start, next location start].
12177          This means we have to special case the last node, and generate
12178          a range of [last location start, end of function label].  */
12179
12180       node = loc_list->first;
12181       varloc = NOTE_VAR_LOCATION (node->var_loc_note);
12182       secname = secname_for_decl (decl);
12183
12184       if (NOTE_VAR_LOCATION_LOC (node->var_loc_note))
12185         initialized = NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
12186       else
12187         initialized = VAR_INIT_STATUS_INITIALIZED;
12188
12189       descr = loc_by_reference (loc_descriptor (varloc, initialized), decl);
12190       list = new_loc_list (descr, node->label, node->next->label, secname, 1);
12191       node = node->next;
12192
12193       for (; node->next; node = node->next)
12194         if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
12195           {
12196             /* The variable has a location between NODE->LABEL and
12197                NODE->NEXT->LABEL.  */
12198             enum var_init_status initialized =
12199               NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
12200             varloc = NOTE_VAR_LOCATION (node->var_loc_note);
12201             descr = loc_by_reference (loc_descriptor (varloc, initialized),
12202                                       decl);
12203             add_loc_descr_to_loc_list (&list, descr,
12204                                        node->label, node->next->label, secname);
12205           }
12206
12207       /* If the variable has a location at the last label
12208          it keeps its location until the end of function.  */
12209       if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
12210         {
12211           char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
12212           enum var_init_status initialized =
12213             NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
12214
12215           varloc = NOTE_VAR_LOCATION (node->var_loc_note);
12216           if (!current_function_decl)
12217             endname = text_end_label;
12218           else
12219             {
12220               ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
12221                                            current_function_funcdef_no);
12222               endname = ggc_strdup (label_id);
12223             }
12224           descr = loc_by_reference (loc_descriptor (varloc, initialized),
12225                                     decl);
12226           add_loc_descr_to_loc_list (&list, descr,
12227                                      node->label, endname, secname);
12228         }
12229
12230       /* Finally, add the location list to the DIE, and we are done.  */
12231       add_AT_loc_list (die, attr, list);
12232       return;
12233     }
12234
12235   /* Try to get some constant RTL for this decl, and use that as the value of
12236      the location.  */
12237
12238   rtl = rtl_for_decl_location (decl);
12239   if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING))
12240     {
12241       add_const_value_attribute (die, rtl);
12242       return;
12243     }
12244
12245   /* If we have tried to generate the location otherwise, and it
12246      didn't work out (we wouldn't be here if we did), and we have a one entry
12247      location list, try generating a location from that.  */
12248   if (loc_list && loc_list->first)
12249     {
12250       enum var_init_status status;
12251       node = loc_list->first;
12252       status = NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
12253       descr = loc_descriptor (NOTE_VAR_LOCATION (node->var_loc_note), status);
12254       if (descr)
12255         {
12256           descr = loc_by_reference (descr, decl);
12257           add_AT_location_description (die, attr, descr);
12258           return;
12259         }
12260     }
12261
12262   /* We couldn't get any rtl, so try directly generating the location
12263      description from the tree.  */
12264   descr = loc_descriptor_from_tree (decl);
12265   if (descr)
12266     {
12267       descr = loc_by_reference (descr, decl);
12268       add_AT_location_description (die, attr, descr);
12269       return;
12270     }
12271   /* None of that worked, so it must not really have a location;
12272      try adding a constant value attribute from the DECL_INITIAL.  */
12273   tree_add_const_value_attribute (die, decl);
12274 }
12275
12276 /* Add VARIABLE and DIE into deferred locations list.  */
12277
12278 static void
12279 defer_location (tree variable, dw_die_ref die)
12280 {
12281   deferred_locations entry;
12282   entry.variable = variable;
12283   entry.die = die;
12284   VEC_safe_push (deferred_locations, gc, deferred_locations_list, &entry);
12285 }
12286
12287 /* Helper function for tree_add_const_value_attribute.  Natively encode
12288    initializer INIT into an array.  Return true if successful.  */
12289
12290 static bool
12291 native_encode_initializer (tree init, unsigned char *array, int size)
12292 {
12293   tree type;
12294
12295   if (init == NULL_TREE)
12296     return false;
12297
12298   STRIP_NOPS (init);
12299   switch (TREE_CODE (init))
12300     {
12301     case STRING_CST:
12302       type = TREE_TYPE (init);
12303       if (TREE_CODE (type) == ARRAY_TYPE)
12304         {
12305           tree enttype = TREE_TYPE (type);
12306           enum machine_mode mode = TYPE_MODE (enttype);
12307
12308           if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
12309             return false;
12310           if (int_size_in_bytes (type) != size)
12311             return false;
12312           if (size > TREE_STRING_LENGTH (init))
12313             {
12314               memcpy (array, TREE_STRING_POINTER (init),
12315                       TREE_STRING_LENGTH (init));
12316               memset (array + TREE_STRING_LENGTH (init),
12317                       '\0', size - TREE_STRING_LENGTH (init));
12318             }
12319           else
12320             memcpy (array, TREE_STRING_POINTER (init), size);
12321           return true;
12322         }
12323       return false;
12324     case CONSTRUCTOR:
12325       type = TREE_TYPE (init);
12326       if (int_size_in_bytes (type) != size)
12327         return false;
12328       if (TREE_CODE (type) == ARRAY_TYPE)
12329         {
12330           HOST_WIDE_INT min_index;
12331           unsigned HOST_WIDE_INT cnt;
12332           int curpos = 0, fieldsize;
12333           constructor_elt *ce;
12334
12335           if (TYPE_DOMAIN (type) == NULL_TREE
12336               || !host_integerp (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0))
12337             return false;
12338
12339           fieldsize = int_size_in_bytes (TREE_TYPE (type));
12340           if (fieldsize <= 0)
12341             return false;
12342
12343           min_index = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0);
12344           memset (array, '\0', size);
12345           for (cnt = 0;
12346                VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce);
12347                cnt++)
12348             {
12349               tree val = ce->value;
12350               tree index = ce->index;
12351               int pos = curpos;
12352               if (index && TREE_CODE (index) == RANGE_EXPR)
12353                 pos = (tree_low_cst (TREE_OPERAND (index, 0), 0) - min_index)
12354                       * fieldsize;
12355               else if (index)
12356                 pos = (tree_low_cst (index, 0) - min_index) * fieldsize;
12357
12358               if (val)
12359                 {
12360                   STRIP_NOPS (val);
12361                   if (!native_encode_initializer (val, array + pos, fieldsize))
12362                     return false;
12363                 }
12364               curpos = pos + fieldsize;
12365               if (index && TREE_CODE (index) == RANGE_EXPR)
12366                 {
12367                   int count = tree_low_cst (TREE_OPERAND (index, 1), 0)
12368                               - tree_low_cst (TREE_OPERAND (index, 0), 0);
12369                   while (count > 0)
12370                     {
12371                       if (val)
12372                         memcpy (array + curpos, array + pos, fieldsize);
12373                       curpos += fieldsize;
12374                     }
12375                 }
12376               gcc_assert (curpos <= size);
12377             }
12378           return true;
12379         }
12380       else if (TREE_CODE (type) == RECORD_TYPE
12381                || TREE_CODE (type) == UNION_TYPE)
12382         {
12383           tree field = NULL_TREE;
12384           unsigned HOST_WIDE_INT cnt;
12385           constructor_elt *ce;
12386
12387           if (int_size_in_bytes (type) != size)
12388             return false;
12389
12390           if (TREE_CODE (type) == RECORD_TYPE)
12391             field = TYPE_FIELDS (type);
12392
12393           for (cnt = 0;
12394                VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce);
12395                cnt++, field = field ? TREE_CHAIN (field) : 0)
12396             {
12397               tree val = ce->value;
12398               int pos, fieldsize;
12399
12400               if (ce->index != 0)
12401                 field = ce->index;
12402
12403               if (val)
12404                 STRIP_NOPS (val);
12405
12406               if (field == NULL_TREE || DECL_BIT_FIELD (field))
12407                 return false;
12408
12409               if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
12410                   && TYPE_DOMAIN (TREE_TYPE (field))
12411                   && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
12412                 return false;
12413               else if (DECL_SIZE_UNIT (field) == NULL_TREE
12414                        || !host_integerp (DECL_SIZE_UNIT (field), 0))
12415                 return false;
12416               fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 0);
12417               pos = int_byte_position (field);
12418               gcc_assert (pos + fieldsize <= size);
12419               if (val
12420                   && !native_encode_initializer (val, array + pos, fieldsize))
12421                 return false;
12422             }
12423           return true;
12424         }
12425       return false;
12426     case VIEW_CONVERT_EXPR:
12427     case NON_LVALUE_EXPR:
12428       return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
12429     default:
12430       return native_encode_expr (init, array, size) == size;
12431     }
12432 }
12433
12434 /* If we don't have a copy of this variable in memory for some reason (such
12435    as a C++ member constant that doesn't have an out-of-line definition),
12436    we should tell the debugger about the constant value.  */
12437
12438 static void
12439 tree_add_const_value_attribute (dw_die_ref var_die, tree decl)
12440 {
12441   tree init;
12442   tree type = TREE_TYPE (decl);
12443   rtx rtl;
12444
12445   if (TREE_CODE (decl) != VAR_DECL && TREE_CODE (decl) != CONST_DECL)
12446     return;
12447
12448   init = DECL_INITIAL (decl);
12449   if (TREE_READONLY (decl) && ! TREE_THIS_VOLATILE (decl) && init)
12450     /* OK */;
12451   else
12452     return;
12453
12454   rtl = rtl_for_decl_init (init, type);
12455   if (rtl)
12456     add_const_value_attribute (var_die, rtl);
12457   /* If the host and target are sane, try harder.  */
12458   else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
12459            && initializer_constant_valid_p (init, type))
12460     {
12461       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
12462       if (size > 0 && (int) size == size)
12463         {
12464           unsigned char *array = GGC_CNEWVEC (unsigned char, size);
12465
12466           if (native_encode_initializer (init, array, size))
12467             add_AT_vec (var_die, DW_AT_const_value, size, 1, array);
12468         }
12469     }
12470 }
12471
12472 /* Convert the CFI instructions for the current function into a
12473    location list.  This is used for DW_AT_frame_base when we targeting
12474    a dwarf2 consumer that does not support the dwarf3
12475    DW_OP_call_frame_cfa.  OFFSET is a constant to be added to all CFA
12476    expressions.  */
12477
12478 static dw_loc_list_ref
12479 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
12480 {
12481   dw_fde_ref fde;
12482   dw_loc_list_ref list, *list_tail;
12483   dw_cfi_ref cfi;
12484   dw_cfa_location last_cfa, next_cfa;
12485   const char *start_label, *last_label, *section;
12486   dw_cfa_location remember;
12487
12488   fde = current_fde ();
12489   gcc_assert (fde != NULL);
12490
12491   section = secname_for_decl (current_function_decl);
12492   list_tail = &list;
12493   list = NULL;
12494
12495   memset (&next_cfa, 0, sizeof (next_cfa));
12496   next_cfa.reg = INVALID_REGNUM;
12497   remember = next_cfa;
12498
12499   start_label = fde->dw_fde_begin;
12500
12501   /* ??? Bald assumption that the CIE opcode list does not contain
12502      advance opcodes.  */
12503   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
12504     lookup_cfa_1 (cfi, &next_cfa, &remember);
12505
12506   last_cfa = next_cfa;
12507   last_label = start_label;
12508
12509   for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
12510     switch (cfi->dw_cfi_opc)
12511       {
12512       case DW_CFA_set_loc:
12513       case DW_CFA_advance_loc1:
12514       case DW_CFA_advance_loc2:
12515       case DW_CFA_advance_loc4:
12516         if (!cfa_equal_p (&last_cfa, &next_cfa))
12517           {
12518             *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
12519                                        start_label, last_label, section,
12520                                        list == NULL);
12521
12522             list_tail = &(*list_tail)->dw_loc_next;
12523             last_cfa = next_cfa;
12524             start_label = last_label;
12525           }
12526         last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
12527         break;
12528
12529       case DW_CFA_advance_loc:
12530         /* The encoding is complex enough that we should never emit this.  */
12531         gcc_unreachable ();
12532
12533       default:
12534         lookup_cfa_1 (cfi, &next_cfa, &remember);
12535         break;
12536       }
12537
12538   if (!cfa_equal_p (&last_cfa, &next_cfa))
12539     {
12540       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
12541                                  start_label, last_label, section,
12542                                  list == NULL);
12543       list_tail = &(*list_tail)->dw_loc_next;
12544       start_label = last_label;
12545     }
12546   *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
12547                              start_label, fde->dw_fde_end, section,
12548                              list == NULL);
12549
12550   return list;
12551 }
12552
12553 /* Compute a displacement from the "steady-state frame pointer" to the
12554    frame base (often the same as the CFA), and store it in
12555    frame_pointer_fb_offset.  OFFSET is added to the displacement
12556    before the latter is negated.  */
12557
12558 static void
12559 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
12560 {
12561   rtx reg, elim;
12562
12563 #ifdef FRAME_POINTER_CFA_OFFSET
12564   reg = frame_pointer_rtx;
12565   offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
12566 #else
12567   reg = arg_pointer_rtx;
12568   offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
12569 #endif
12570
12571   elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
12572   if (GET_CODE (elim) == PLUS)
12573     {
12574       offset += INTVAL (XEXP (elim, 1));
12575       elim = XEXP (elim, 0);
12576     }
12577
12578   gcc_assert ((SUPPORTS_STACK_ALIGNMENT
12579                && (elim == hard_frame_pointer_rtx
12580                    || elim == stack_pointer_rtx))
12581               || elim == (frame_pointer_needed
12582                           ? hard_frame_pointer_rtx
12583                           : stack_pointer_rtx));
12584
12585   frame_pointer_fb_offset = -offset;
12586 }
12587
12588 /* Generate a DW_AT_name attribute given some string value to be included as
12589    the value of the attribute.  */
12590
12591 static void
12592 add_name_attribute (dw_die_ref die, const char *name_string)
12593 {
12594   if (name_string != NULL && *name_string != 0)
12595     {
12596       if (demangle_name_func)
12597         name_string = (*demangle_name_func) (name_string);
12598
12599       add_AT_string (die, DW_AT_name, name_string);
12600     }
12601 }
12602
12603 /* Generate a DW_AT_comp_dir attribute for DIE.  */
12604
12605 static void
12606 add_comp_dir_attribute (dw_die_ref die)
12607 {
12608   const char *wd = get_src_pwd ();
12609   if (wd != NULL)
12610     add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
12611 }
12612
12613 /* Given a tree node describing an array bound (either lower or upper) output
12614    a representation for that bound.  */
12615
12616 static void
12617 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
12618 {
12619   switch (TREE_CODE (bound))
12620     {
12621     case ERROR_MARK:
12622       return;
12623
12624     /* All fixed-bounds are represented by INTEGER_CST nodes.  */
12625     case INTEGER_CST:
12626       if (! host_integerp (bound, 0)
12627           || (bound_attr == DW_AT_lower_bound
12628               && (((is_c_family () || is_java ()) &&  integer_zerop (bound))
12629                   || (is_fortran () && integer_onep (bound)))))
12630         /* Use the default.  */
12631         ;
12632       else
12633         add_AT_unsigned (subrange_die, bound_attr, tree_low_cst (bound, 0));
12634       break;
12635
12636     CASE_CONVERT:
12637     case VIEW_CONVERT_EXPR:
12638       add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
12639       break;
12640
12641     case SAVE_EXPR:
12642       break;
12643
12644     case VAR_DECL:
12645     case PARM_DECL:
12646     case RESULT_DECL:
12647       {
12648         dw_die_ref decl_die = lookup_decl_die (bound);
12649         dw_loc_descr_ref loc;
12650
12651         /* ??? Can this happen, or should the variable have been bound
12652            first?  Probably it can, since I imagine that we try to create
12653            the types of parameters in the order in which they exist in
12654            the list, and won't have created a forward reference to a
12655            later parameter.  */
12656         if (decl_die != NULL)
12657           add_AT_die_ref (subrange_die, bound_attr, decl_die);
12658         else
12659           {
12660             loc = loc_descriptor_from_tree_1 (bound, 0);
12661             add_AT_location_description (subrange_die, bound_attr, loc);
12662           }
12663         break;
12664       }
12665
12666     default:
12667       {
12668         /* Otherwise try to create a stack operation procedure to
12669            evaluate the value of the array bound.  */
12670
12671         dw_die_ref ctx, decl_die;
12672         dw_loc_descr_ref loc;
12673
12674         loc = loc_descriptor_from_tree (bound);
12675         if (loc == NULL)
12676           break;
12677
12678         if (current_function_decl == 0)
12679           ctx = comp_unit_die;
12680         else
12681           ctx = lookup_decl_die (current_function_decl);
12682
12683         decl_die = new_die (DW_TAG_variable, ctx, bound);
12684         add_AT_flag (decl_die, DW_AT_artificial, 1);
12685         add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
12686         add_AT_loc (decl_die, DW_AT_location, loc);
12687
12688         add_AT_die_ref (subrange_die, bound_attr, decl_die);
12689         break;
12690       }
12691     }
12692 }
12693
12694 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
12695    possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
12696    Note that the block of subscript information for an array type also
12697    includes information about the element type of the given array type.  */
12698
12699 static void
12700 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
12701 {
12702   unsigned dimension_number;
12703   tree lower, upper;
12704   dw_die_ref subrange_die;
12705
12706   for (dimension_number = 0;
12707        TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
12708        type = TREE_TYPE (type), dimension_number++)
12709     {
12710       tree domain = TYPE_DOMAIN (type);
12711
12712       if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
12713         break;
12714
12715       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
12716          and (in GNU C only) variable bounds.  Handle all three forms
12717          here.  */
12718       subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
12719       if (domain)
12720         {
12721           /* We have an array type with specified bounds.  */
12722           lower = TYPE_MIN_VALUE (domain);
12723           upper = TYPE_MAX_VALUE (domain);
12724
12725           /* Define the index type.  */
12726           if (TREE_TYPE (domain))
12727             {
12728               /* ??? This is probably an Ada unnamed subrange type.  Ignore the
12729                  TREE_TYPE field.  We can't emit debug info for this
12730                  because it is an unnamed integral type.  */
12731               if (TREE_CODE (domain) == INTEGER_TYPE
12732                   && TYPE_NAME (domain) == NULL_TREE
12733                   && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
12734                   && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
12735                 ;
12736               else
12737                 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
12738                                     type_die);
12739             }
12740
12741           /* ??? If upper is NULL, the array has unspecified length,
12742              but it does have a lower bound.  This happens with Fortran
12743                dimension arr(N:*)
12744              Since the debugger is definitely going to need to know N
12745              to produce useful results, go ahead and output the lower
12746              bound solo, and hope the debugger can cope.  */
12747
12748           add_bound_info (subrange_die, DW_AT_lower_bound, lower);
12749           if (upper)
12750             add_bound_info (subrange_die, DW_AT_upper_bound, upper);
12751         }
12752
12753       /* Otherwise we have an array type with an unspecified length.  The
12754          DWARF-2 spec does not say how to handle this; let's just leave out the
12755          bounds.  */
12756     }
12757 }
12758
12759 static void
12760 add_byte_size_attribute (dw_die_ref die, tree tree_node)
12761 {
12762   unsigned size;
12763
12764   switch (TREE_CODE (tree_node))
12765     {
12766     case ERROR_MARK:
12767       size = 0;
12768       break;
12769     case ENUMERAL_TYPE:
12770     case RECORD_TYPE:
12771     case UNION_TYPE:
12772     case QUAL_UNION_TYPE:
12773       size = int_size_in_bytes (tree_node);
12774       break;
12775     case FIELD_DECL:
12776       /* For a data member of a struct or union, the DW_AT_byte_size is
12777          generally given as the number of bytes normally allocated for an
12778          object of the *declared* type of the member itself.  This is true
12779          even for bit-fields.  */
12780       size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
12781       break;
12782     default:
12783       gcc_unreachable ();
12784     }
12785
12786   /* Note that `size' might be -1 when we get to this point.  If it is, that
12787      indicates that the byte size of the entity in question is variable.  We
12788      have no good way of expressing this fact in Dwarf at the present time,
12789      so just let the -1 pass on through.  */
12790   add_AT_unsigned (die, DW_AT_byte_size, size);
12791 }
12792
12793 /* For a FIELD_DECL node which represents a bit-field, output an attribute
12794    which specifies the distance in bits from the highest order bit of the
12795    "containing object" for the bit-field to the highest order bit of the
12796    bit-field itself.
12797
12798    For any given bit-field, the "containing object" is a hypothetical object
12799    (of some integral or enum type) within which the given bit-field lives.  The
12800    type of this hypothetical "containing object" is always the same as the
12801    declared type of the individual bit-field itself.  The determination of the
12802    exact location of the "containing object" for a bit-field is rather
12803    complicated.  It's handled by the `field_byte_offset' function (above).
12804
12805    Note that it is the size (in bytes) of the hypothetical "containing object"
12806    which will be given in the DW_AT_byte_size attribute for this bit-field.
12807    (See `byte_size_attribute' above).  */
12808
12809 static inline void
12810 add_bit_offset_attribute (dw_die_ref die, tree decl)
12811 {
12812   HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
12813   tree type = DECL_BIT_FIELD_TYPE (decl);
12814   HOST_WIDE_INT bitpos_int;
12815   HOST_WIDE_INT highest_order_object_bit_offset;
12816   HOST_WIDE_INT highest_order_field_bit_offset;
12817   HOST_WIDE_INT unsigned bit_offset;
12818
12819   /* Must be a field and a bit field.  */
12820   gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
12821
12822   /* We can't yet handle bit-fields whose offsets are variable, so if we
12823      encounter such things, just return without generating any attribute
12824      whatsoever.  Likewise for variable or too large size.  */
12825   if (! host_integerp (bit_position (decl), 0)
12826       || ! host_integerp (DECL_SIZE (decl), 1))
12827     return;
12828
12829   bitpos_int = int_bit_position (decl);
12830
12831   /* Note that the bit offset is always the distance (in bits) from the
12832      highest-order bit of the "containing object" to the highest-order bit of
12833      the bit-field itself.  Since the "high-order end" of any object or field
12834      is different on big-endian and little-endian machines, the computation
12835      below must take account of these differences.  */
12836   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
12837   highest_order_field_bit_offset = bitpos_int;
12838
12839   if (! BYTES_BIG_ENDIAN)
12840     {
12841       highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
12842       highest_order_object_bit_offset += simple_type_size_in_bits (type);
12843     }
12844
12845   bit_offset
12846     = (! BYTES_BIG_ENDIAN
12847        ? highest_order_object_bit_offset - highest_order_field_bit_offset
12848        : highest_order_field_bit_offset - highest_order_object_bit_offset);
12849
12850   add_AT_unsigned (die, DW_AT_bit_offset, bit_offset);
12851 }
12852
12853 /* For a FIELD_DECL node which represents a bit field, output an attribute
12854    which specifies the length in bits of the given field.  */
12855
12856 static inline void
12857 add_bit_size_attribute (dw_die_ref die, tree decl)
12858 {
12859   /* Must be a field and a bit field.  */
12860   gcc_assert (TREE_CODE (decl) == FIELD_DECL
12861               && DECL_BIT_FIELD_TYPE (decl));
12862
12863   if (host_integerp (DECL_SIZE (decl), 1))
12864     add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
12865 }
12866
12867 /* If the compiled language is ANSI C, then add a 'prototyped'
12868    attribute, if arg types are given for the parameters of a function.  */
12869
12870 static inline void
12871 add_prototyped_attribute (dw_die_ref die, tree func_type)
12872 {
12873   if (get_AT_unsigned (comp_unit_die, DW_AT_language) == DW_LANG_C89
12874       && TYPE_ARG_TYPES (func_type) != NULL)
12875     add_AT_flag (die, DW_AT_prototyped, 1);
12876 }
12877
12878 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
12879    by looking in either the type declaration or object declaration
12880    equate table.  */
12881
12882 static inline dw_die_ref
12883 add_abstract_origin_attribute (dw_die_ref die, tree origin)
12884 {
12885   dw_die_ref origin_die = NULL;
12886
12887   if (TREE_CODE (origin) != FUNCTION_DECL)
12888     {
12889       /* We may have gotten separated from the block for the inlined
12890          function, if we're in an exception handler or some such; make
12891          sure that the abstract function has been written out.
12892
12893          Doing this for nested functions is wrong, however; functions are
12894          distinct units, and our context might not even be inline.  */
12895       tree fn = origin;
12896
12897       if (TYPE_P (fn))
12898         fn = TYPE_STUB_DECL (fn);
12899
12900       fn = decl_function_context (fn);
12901       if (fn)
12902         dwarf2out_abstract_function (fn);
12903     }
12904
12905   if (DECL_P (origin))
12906     origin_die = lookup_decl_die (origin);
12907   else if (TYPE_P (origin))
12908     origin_die = lookup_type_die (origin);
12909
12910   /* XXX: Functions that are never lowered don't always have correct block
12911      trees (in the case of java, they simply have no block tree, in some other
12912      languages).  For these functions, there is nothing we can really do to
12913      output correct debug info for inlined functions in all cases.  Rather
12914      than die, we'll just produce deficient debug info now, in that we will
12915      have variables without a proper abstract origin.  In the future, when all
12916      functions are lowered, we should re-add a gcc_assert (origin_die)
12917      here.  */
12918
12919   if (origin_die)
12920     add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
12921   return origin_die;
12922 }
12923
12924 /* We do not currently support the pure_virtual attribute.  */
12925
12926 static inline void
12927 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
12928 {
12929   if (DECL_VINDEX (func_decl))
12930     {
12931       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
12932
12933       if (host_integerp (DECL_VINDEX (func_decl), 0))
12934         add_AT_loc (die, DW_AT_vtable_elem_location,
12935                     new_loc_descr (DW_OP_constu,
12936                                    tree_low_cst (DECL_VINDEX (func_decl), 0),
12937                                    0));
12938
12939       /* GNU extension: Record what type this method came from originally.  */
12940       if (debug_info_level > DINFO_LEVEL_TERSE)
12941         add_AT_die_ref (die, DW_AT_containing_type,
12942                         lookup_type_die (DECL_CONTEXT (func_decl)));
12943     }
12944 }
12945 \f
12946 /* Add source coordinate attributes for the given decl.  */
12947
12948 static void
12949 add_src_coords_attributes (dw_die_ref die, tree decl)
12950 {
12951   expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
12952
12953   add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
12954   add_AT_unsigned (die, DW_AT_decl_line, s.line);
12955 }
12956
12957 /* Add a DW_AT_name attribute and source coordinate attribute for the
12958    given decl, but only if it actually has a name.  */
12959
12960 static void
12961 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
12962 {
12963   tree decl_name;
12964
12965   decl_name = DECL_NAME (decl);
12966   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
12967     {
12968       add_name_attribute (die, dwarf2_name (decl, 0));
12969       if (! DECL_ARTIFICIAL (decl))
12970         add_src_coords_attributes (die, decl);
12971
12972       if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
12973           && TREE_PUBLIC (decl)
12974           && !DECL_ABSTRACT (decl)
12975           && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
12976           && !is_fortran ())
12977         {
12978           /* Defer until we have an assembler name set.  */
12979           if (!DECL_ASSEMBLER_NAME_SET_P (decl))
12980             {
12981               limbo_die_node *asm_name;
12982
12983               asm_name = GGC_CNEW (limbo_die_node);
12984               asm_name->die = die;
12985               asm_name->created_for = decl;
12986               asm_name->next = deferred_asm_name;
12987               deferred_asm_name = asm_name;
12988             }
12989           else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
12990             add_AT_string (die, DW_AT_MIPS_linkage_name,
12991                            IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
12992         }
12993     }
12994
12995 #ifdef VMS_DEBUGGING_INFO
12996   /* Get the function's name, as described by its RTL.  This may be different
12997      from the DECL_NAME name used in the source file.  */
12998   if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
12999     {
13000       add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
13001                    XEXP (DECL_RTL (decl), 0));
13002       VEC_safe_push (tree, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
13003     }
13004 #endif
13005 }
13006
13007 /* Push a new declaration scope.  */
13008
13009 static void
13010 push_decl_scope (tree scope)
13011 {
13012   VEC_safe_push (tree, gc, decl_scope_table, scope);
13013 }
13014
13015 /* Pop a declaration scope.  */
13016
13017 static inline void
13018 pop_decl_scope (void)
13019 {
13020   VEC_pop (tree, decl_scope_table);
13021 }
13022
13023 /* Return the DIE for the scope that immediately contains this type.
13024    Non-named types get global scope.  Named types nested in other
13025    types get their containing scope if it's open, or global scope
13026    otherwise.  All other types (i.e. function-local named types) get
13027    the current active scope.  */
13028
13029 static dw_die_ref
13030 scope_die_for (tree t, dw_die_ref context_die)
13031 {
13032   dw_die_ref scope_die = NULL;
13033   tree containing_scope;
13034   int i;
13035
13036   /* Non-types always go in the current scope.  */
13037   gcc_assert (TYPE_P (t));
13038
13039   containing_scope = TYPE_CONTEXT (t);
13040
13041   /* Use the containing namespace if it was passed in (for a declaration).  */
13042   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
13043     {
13044       if (context_die == lookup_decl_die (containing_scope))
13045         /* OK */;
13046       else
13047         containing_scope = NULL_TREE;
13048     }
13049
13050   /* Ignore function type "scopes" from the C frontend.  They mean that
13051      a tagged type is local to a parmlist of a function declarator, but
13052      that isn't useful to DWARF.  */
13053   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
13054     containing_scope = NULL_TREE;
13055
13056   if (containing_scope == NULL_TREE)
13057     scope_die = comp_unit_die;
13058   else if (TYPE_P (containing_scope))
13059     {
13060       /* For types, we can just look up the appropriate DIE.  But
13061          first we check to see if we're in the middle of emitting it
13062          so we know where the new DIE should go.  */
13063       for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
13064         if (VEC_index (tree, decl_scope_table, i) == containing_scope)
13065           break;
13066
13067       if (i < 0)
13068         {
13069           gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
13070                       || TREE_ASM_WRITTEN (containing_scope));
13071
13072           /* If none of the current dies are suitable, we get file scope.  */
13073           scope_die = comp_unit_die;
13074         }
13075       else
13076         scope_die = lookup_type_die (containing_scope);
13077     }
13078   else
13079     scope_die = context_die;
13080
13081   return scope_die;
13082 }
13083
13084 /* Returns nonzero if CONTEXT_DIE is internal to a function.  */
13085
13086 static inline int
13087 local_scope_p (dw_die_ref context_die)
13088 {
13089   for (; context_die; context_die = context_die->die_parent)
13090     if (context_die->die_tag == DW_TAG_inlined_subroutine
13091         || context_die->die_tag == DW_TAG_subprogram)
13092       return 1;
13093
13094   return 0;
13095 }
13096
13097 /* Returns nonzero if CONTEXT_DIE is a class.  */
13098
13099 static inline int
13100 class_scope_p (dw_die_ref context_die)
13101 {
13102   return (context_die
13103           && (context_die->die_tag == DW_TAG_structure_type
13104               || context_die->die_tag == DW_TAG_class_type
13105               || context_die->die_tag == DW_TAG_interface_type
13106               || context_die->die_tag == DW_TAG_union_type));
13107 }
13108
13109 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
13110    whether or not to treat a DIE in this context as a declaration.  */
13111
13112 static inline int
13113 class_or_namespace_scope_p (dw_die_ref context_die)
13114 {
13115   return (class_scope_p (context_die)
13116           || (context_die && context_die->die_tag == DW_TAG_namespace));
13117 }
13118
13119 /* Many forms of DIEs require a "type description" attribute.  This
13120    routine locates the proper "type descriptor" die for the type given
13121    by 'type', and adds a DW_AT_type attribute below the given die.  */
13122
13123 static void
13124 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
13125                     int decl_volatile, dw_die_ref context_die)
13126 {
13127   enum tree_code code  = TREE_CODE (type);
13128   dw_die_ref type_die  = NULL;
13129
13130   /* ??? If this type is an unnamed subrange type of an integral, floating-point
13131      or fixed-point type, use the inner type.  This is because we have no
13132      support for unnamed types in base_type_die.  This can happen if this is
13133      an Ada subrange type.  Correct solution is emit a subrange type die.  */
13134   if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
13135       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
13136     type = TREE_TYPE (type), code = TREE_CODE (type);
13137
13138   if (code == ERROR_MARK
13139       /* Handle a special case.  For functions whose return type is void, we
13140          generate *no* type attribute.  (Note that no object may have type
13141          `void', so this only applies to function return types).  */
13142       || code == VOID_TYPE)
13143     return;
13144
13145   type_die = modified_type_die (type,
13146                                 decl_const || TYPE_READONLY (type),
13147                                 decl_volatile || TYPE_VOLATILE (type),
13148                                 context_die);
13149
13150   if (type_die != NULL)
13151     add_AT_die_ref (object_die, DW_AT_type, type_die);
13152 }
13153
13154 /* Given an object die, add the calling convention attribute for the
13155    function call type.  */
13156 static void
13157 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
13158 {
13159   enum dwarf_calling_convention value = DW_CC_normal;
13160
13161   value = ((enum dwarf_calling_convention)
13162            targetm.dwarf_calling_convention (TREE_TYPE (decl)));
13163
13164   /* DWARF doesn't provide a way to identify a program's source-level
13165      entry point.  DW_AT_calling_convention attributes are only meant
13166      to describe functions' calling conventions.  However, lacking a
13167      better way to signal the Fortran main program, we use this for the
13168      time being, following existing custom.  */
13169   if (is_fortran ()
13170       && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
13171     value = DW_CC_program;
13172
13173   /* Only add the attribute if the backend requests it, and
13174      is not DW_CC_normal.  */
13175   if (value && (value != DW_CC_normal))
13176     add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
13177 }
13178
13179 /* Given a tree pointer to a struct, class, union, or enum type node, return
13180    a pointer to the (string) tag name for the given type, or zero if the type
13181    was declared without a tag.  */
13182
13183 static const char *
13184 type_tag (const_tree type)
13185 {
13186   const char *name = 0;
13187
13188   if (TYPE_NAME (type) != 0)
13189     {
13190       tree t = 0;
13191
13192       /* Find the IDENTIFIER_NODE for the type name.  */
13193       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
13194         t = TYPE_NAME (type);
13195
13196       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
13197          a TYPE_DECL node, regardless of whether or not a `typedef' was
13198          involved.  */
13199       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
13200                && ! DECL_IGNORED_P (TYPE_NAME (type)))
13201         {
13202           /* We want to be extra verbose.  Don't call dwarf_name if
13203              DECL_NAME isn't set.  The default hook for decl_printable_name
13204              doesn't like that, and in this context it's correct to return
13205              0, instead of "<anonymous>" or the like.  */
13206           if (DECL_NAME (TYPE_NAME (type)))
13207             name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
13208         }
13209
13210       /* Now get the name as a string, or invent one.  */
13211       if (!name && t != 0)
13212         name = IDENTIFIER_POINTER (t);
13213     }
13214
13215   return (name == 0 || *name == '\0') ? 0 : name;
13216 }
13217
13218 /* Return the type associated with a data member, make a special check
13219    for bit field types.  */
13220
13221 static inline tree
13222 member_declared_type (const_tree member)
13223 {
13224   return (DECL_BIT_FIELD_TYPE (member)
13225           ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
13226 }
13227
13228 /* Get the decl's label, as described by its RTL. This may be different
13229    from the DECL_NAME name used in the source file.  */
13230
13231 #if 0
13232 static const char *
13233 decl_start_label (tree decl)
13234 {
13235   rtx x;
13236   const char *fnname;
13237
13238   x = DECL_RTL (decl);
13239   gcc_assert (MEM_P (x));
13240
13241   x = XEXP (x, 0);
13242   gcc_assert (GET_CODE (x) == SYMBOL_REF);
13243
13244   fnname = XSTR (x, 0);
13245   return fnname;
13246 }
13247 #endif
13248 \f
13249 /* These routines generate the internal representation of the DIE's for
13250    the compilation unit.  Debugging information is collected by walking
13251    the declaration trees passed in from dwarf2out_decl().  */
13252
13253 static void
13254 gen_array_type_die (tree type, dw_die_ref context_die)
13255 {
13256   dw_die_ref scope_die = scope_die_for (type, context_die);
13257   dw_die_ref array_die;
13258
13259   /* GNU compilers represent multidimensional array types as sequences of one
13260      dimensional array types whose element types are themselves array types.
13261      We sometimes squish that down to a single array_type DIE with multiple
13262      subscripts in the Dwarf debugging info.  The draft Dwarf specification
13263      say that we are allowed to do this kind of compression in C, because
13264      there is no difference between an array of arrays and a multidimensional
13265      array.  We don't do this for Ada to remain as close as possible to the
13266      actual representation, which is especially important against the language
13267      flexibilty wrt arrays of variable size.  */
13268
13269   bool collapse_nested_arrays = !is_ada ();
13270   tree element_type;
13271
13272   /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
13273      DW_TAG_string_type doesn't have DW_AT_type attribute).  */
13274   if (TYPE_STRING_FLAG (type)
13275       && TREE_CODE (type) == ARRAY_TYPE
13276       && is_fortran ()
13277       && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
13278     {
13279       HOST_WIDE_INT size;
13280
13281       array_die = new_die (DW_TAG_string_type, scope_die, type);
13282       add_name_attribute (array_die, type_tag (type));
13283       equate_type_number_to_die (type, array_die);
13284       size = int_size_in_bytes (type);
13285       if (size >= 0)
13286         add_AT_unsigned (array_die, DW_AT_byte_size, size);
13287       else if (TYPE_DOMAIN (type) != NULL_TREE
13288                && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
13289                && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
13290         {
13291           tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
13292           dw_loc_descr_ref loc = loc_descriptor_from_tree (szdecl);
13293
13294           size = int_size_in_bytes (TREE_TYPE (szdecl));
13295           if (loc && size > 0)
13296             {
13297               add_AT_loc (array_die, DW_AT_string_length, loc);
13298               if (size != DWARF2_ADDR_SIZE)
13299                 add_AT_unsigned (array_die, DW_AT_byte_size, size);
13300             }
13301         }
13302       return;
13303     }
13304
13305   /* ??? The SGI dwarf reader fails for array of array of enum types
13306      (e.g. const enum machine_mode insn_operand_mode[2][10]) unless the inner
13307      array type comes before the outer array type.  We thus call gen_type_die
13308      before we new_die and must prevent nested array types collapsing for this
13309      target.  */
13310
13311 #ifdef MIPS_DEBUGGING_INFO
13312   gen_type_die (TREE_TYPE (type), context_die);
13313   collapse_nested_arrays = false;
13314 #endif
13315
13316   array_die = new_die (DW_TAG_array_type, scope_die, type);
13317   add_name_attribute (array_die, type_tag (type));
13318   equate_type_number_to_die (type, array_die);
13319
13320   if (TREE_CODE (type) == VECTOR_TYPE)
13321     {
13322       /* The frontend feeds us a representation for the vector as a struct
13323          containing an array.  Pull out the array type.  */
13324       type = TREE_TYPE (TYPE_FIELDS (TYPE_DEBUG_REPRESENTATION_TYPE (type)));
13325       add_AT_flag (array_die, DW_AT_GNU_vector, 1);
13326     }
13327
13328   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
13329   if (is_fortran ()
13330       && TREE_CODE (type) == ARRAY_TYPE
13331       && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
13332       && !TYPE_STRING_FLAG (TREE_TYPE (type)))
13333     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
13334
13335 #if 0
13336   /* We default the array ordering.  SDB will probably do
13337      the right things even if DW_AT_ordering is not present.  It's not even
13338      an issue until we start to get into multidimensional arrays anyway.  If
13339      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
13340      then we'll have to put the DW_AT_ordering attribute back in.  (But if
13341      and when we find out that we need to put these in, we will only do so
13342      for multidimensional arrays.  */
13343   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
13344 #endif
13345
13346 #ifdef MIPS_DEBUGGING_INFO
13347   /* The SGI compilers handle arrays of unknown bound by setting
13348      AT_declaration and not emitting any subrange DIEs.  */
13349   if (! TYPE_DOMAIN (type))
13350     add_AT_flag (array_die, DW_AT_declaration, 1);
13351   else
13352 #endif
13353     add_subscript_info (array_die, type, collapse_nested_arrays);
13354
13355   /* Add representation of the type of the elements of this array type and
13356      emit the corresponding DIE if we haven't done it already.  */  
13357   element_type = TREE_TYPE (type);
13358   if (collapse_nested_arrays)
13359     while (TREE_CODE (element_type) == ARRAY_TYPE)
13360       {
13361         if (TYPE_STRING_FLAG (element_type) && is_fortran ())
13362           break;
13363         element_type = TREE_TYPE (element_type);
13364       }
13365
13366 #ifndef MIPS_DEBUGGING_INFO
13367   gen_type_die (element_type, context_die);
13368 #endif
13369
13370   add_type_attribute (array_die, element_type, 0, 0, context_die);
13371
13372   if (get_AT (array_die, DW_AT_name))
13373     add_pubtype (type, array_die);
13374 }
13375
13376 static dw_loc_descr_ref
13377 descr_info_loc (tree val, tree base_decl)
13378 {
13379   HOST_WIDE_INT size;
13380   dw_loc_descr_ref loc, loc2;
13381   enum dwarf_location_atom op;
13382
13383   if (val == base_decl)
13384     return new_loc_descr (DW_OP_push_object_address, 0, 0);
13385
13386   switch (TREE_CODE (val))
13387     {
13388     CASE_CONVERT:
13389       return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
13390     case VAR_DECL:
13391       return loc_descriptor_from_tree_1 (val, 0);
13392     case INTEGER_CST:
13393       if (host_integerp (val, 0))
13394         return int_loc_descriptor (tree_low_cst (val, 0));
13395       break;
13396     case INDIRECT_REF:
13397       size = int_size_in_bytes (TREE_TYPE (val));
13398       if (size < 0)
13399         break;
13400       loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
13401       if (!loc)
13402         break;
13403       if (size == DWARF2_ADDR_SIZE)
13404         add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
13405       else
13406         add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
13407       return loc;
13408     case POINTER_PLUS_EXPR:
13409     case PLUS_EXPR:
13410       if (host_integerp (TREE_OPERAND (val, 1), 1)
13411           && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
13412              < 16384)
13413         {
13414           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
13415           if (!loc)
13416             break;
13417           loc_descr_plus_const (&loc, tree_low_cst (TREE_OPERAND (val, 1), 0));
13418         }
13419       else
13420         {
13421           op = DW_OP_plus;
13422         do_binop:
13423           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
13424           if (!loc)
13425             break;
13426           loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
13427           if (!loc2)
13428             break;
13429           add_loc_descr (&loc, loc2);
13430           add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
13431         }
13432       return loc;
13433     case MINUS_EXPR:
13434       op = DW_OP_minus;
13435       goto do_binop;
13436     case MULT_EXPR:
13437       op = DW_OP_mul;
13438       goto do_binop;
13439     case EQ_EXPR:
13440       op = DW_OP_eq;
13441       goto do_binop;
13442     case NE_EXPR:
13443       op = DW_OP_ne;
13444       goto do_binop;
13445     default:
13446       break;
13447     }
13448   return NULL;
13449 }
13450
13451 static void
13452 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
13453                       tree val, tree base_decl)
13454 {
13455   dw_loc_descr_ref loc;
13456
13457   if (host_integerp (val, 0))
13458     {
13459       add_AT_unsigned (die, attr, tree_low_cst (val, 0));
13460       return;
13461     }
13462
13463   loc = descr_info_loc (val, base_decl);
13464   if (!loc)
13465     return;
13466
13467   add_AT_loc (die, attr, loc);
13468 }
13469
13470 /* This routine generates DIE for array with hidden descriptor, details
13471    are filled into *info by a langhook.  */
13472
13473 static void
13474 gen_descr_array_type_die (tree type, struct array_descr_info *info,
13475                           dw_die_ref context_die)
13476 {
13477   dw_die_ref scope_die = scope_die_for (type, context_die);
13478   dw_die_ref array_die;
13479   int dim;
13480
13481   array_die = new_die (DW_TAG_array_type, scope_die, type);
13482   add_name_attribute (array_die, type_tag (type));
13483   equate_type_number_to_die (type, array_die);
13484
13485   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
13486   if (is_fortran ()
13487       && info->ndimensions >= 2)
13488     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
13489
13490   if (info->data_location)
13491     add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
13492                           info->base_decl);
13493   if (info->associated)
13494     add_descr_info_field (array_die, DW_AT_associated, info->associated,
13495                           info->base_decl);
13496   if (info->allocated)
13497     add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
13498                           info->base_decl);
13499
13500   for (dim = 0; dim < info->ndimensions; dim++)
13501     {
13502       dw_die_ref subrange_die
13503         = new_die (DW_TAG_subrange_type, array_die, NULL);
13504
13505       if (info->dimen[dim].lower_bound)
13506         {
13507           /* If it is the default value, omit it.  */
13508           if ((is_c_family () || is_java ())
13509               && integer_zerop (info->dimen[dim].lower_bound))
13510             ;
13511           else if (is_fortran ()
13512                    && integer_onep (info->dimen[dim].lower_bound))
13513             ;
13514           else
13515             add_descr_info_field (subrange_die, DW_AT_lower_bound,
13516                                   info->dimen[dim].lower_bound,
13517                                   info->base_decl);
13518         }
13519       if (info->dimen[dim].upper_bound)
13520         add_descr_info_field (subrange_die, DW_AT_upper_bound,
13521                               info->dimen[dim].upper_bound,
13522                               info->base_decl);
13523       if (info->dimen[dim].stride)
13524         add_descr_info_field (subrange_die, DW_AT_byte_stride,
13525                               info->dimen[dim].stride,
13526                               info->base_decl);
13527     }
13528
13529   gen_type_die (info->element_type, context_die);
13530   add_type_attribute (array_die, info->element_type, 0, 0, context_die);
13531
13532   if (get_AT (array_die, DW_AT_name))
13533     add_pubtype (type, array_die);
13534 }
13535
13536 #if 0
13537 static void
13538 gen_entry_point_die (tree decl, dw_die_ref context_die)
13539 {
13540   tree origin = decl_ultimate_origin (decl);
13541   dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
13542
13543   if (origin != NULL)
13544     add_abstract_origin_attribute (decl_die, origin);
13545   else
13546     {
13547       add_name_and_src_coords_attributes (decl_die, decl);
13548       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
13549                           0, 0, context_die);
13550     }
13551
13552   if (DECL_ABSTRACT (decl))
13553     equate_decl_number_to_die (decl, decl_die);
13554   else
13555     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
13556 }
13557 #endif
13558
13559 /* Walk through the list of incomplete types again, trying once more to
13560    emit full debugging info for them.  */
13561
13562 static void
13563 retry_incomplete_types (void)
13564 {
13565   int i;
13566
13567   for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
13568     gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die);
13569 }
13570
13571 /* Determine what tag to use for a record type.  */
13572
13573 static enum dwarf_tag
13574 record_type_tag (tree type)
13575 {
13576   if (! lang_hooks.types.classify_record)
13577     return DW_TAG_structure_type;
13578
13579   switch (lang_hooks.types.classify_record (type))
13580     {
13581     case RECORD_IS_STRUCT:
13582       return DW_TAG_structure_type;
13583
13584     case RECORD_IS_CLASS:
13585       return DW_TAG_class_type;
13586
13587     case RECORD_IS_INTERFACE:
13588       return DW_TAG_interface_type;
13589
13590     default:
13591       gcc_unreachable ();
13592     }
13593 }
13594
13595 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
13596    include all of the information about the enumeration values also. Each
13597    enumerated type name/value is listed as a child of the enumerated type
13598    DIE.  */
13599
13600 static dw_die_ref
13601 gen_enumeration_type_die (tree type, dw_die_ref context_die)
13602 {
13603   dw_die_ref type_die = lookup_type_die (type);
13604
13605   if (type_die == NULL)
13606     {
13607       type_die = new_die (DW_TAG_enumeration_type,
13608                           scope_die_for (type, context_die), type);
13609       equate_type_number_to_die (type, type_die);
13610       add_name_attribute (type_die, type_tag (type));
13611     }
13612   else if (! TYPE_SIZE (type))
13613     return type_die;
13614   else
13615     remove_AT (type_die, DW_AT_declaration);
13616
13617   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
13618      given enum type is incomplete, do not generate the DW_AT_byte_size
13619      attribute or the DW_AT_element_list attribute.  */
13620   if (TYPE_SIZE (type))
13621     {
13622       tree link;
13623
13624       TREE_ASM_WRITTEN (type) = 1;
13625       add_byte_size_attribute (type_die, type);
13626       if (TYPE_STUB_DECL (type) != NULL_TREE)
13627         add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
13628
13629       /* If the first reference to this type was as the return type of an
13630          inline function, then it may not have a parent.  Fix this now.  */
13631       if (type_die->die_parent == NULL)
13632         add_child_die (scope_die_for (type, context_die), type_die);
13633
13634       for (link = TYPE_VALUES (type);
13635            link != NULL; link = TREE_CHAIN (link))
13636         {
13637           dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
13638           tree value = TREE_VALUE (link);
13639
13640           add_name_attribute (enum_die,
13641                               IDENTIFIER_POINTER (TREE_PURPOSE (link)));
13642
13643           if (TREE_CODE (value) == CONST_DECL)
13644             value = DECL_INITIAL (value);
13645
13646           if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
13647             /* DWARF2 does not provide a way of indicating whether or
13648                not enumeration constants are signed or unsigned.  GDB
13649                always assumes the values are signed, so we output all
13650                values as if they were signed.  That means that
13651                enumeration constants with very large unsigned values
13652                will appear to have negative values in the debugger.  */
13653             add_AT_int (enum_die, DW_AT_const_value,
13654                         tree_low_cst (value, tree_int_cst_sgn (value) > 0));
13655         }
13656     }
13657   else
13658     add_AT_flag (type_die, DW_AT_declaration, 1);
13659
13660   if (get_AT (type_die, DW_AT_name))
13661     add_pubtype (type, type_die);
13662
13663   return type_die;
13664 }
13665
13666 /* Generate a DIE to represent either a real live formal parameter decl or to
13667    represent just the type of some formal parameter position in some function
13668    type.
13669
13670    Note that this routine is a bit unusual because its argument may be a
13671    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
13672    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
13673    node.  If it's the former then this function is being called to output a
13674    DIE to represent a formal parameter object (or some inlining thereof).  If
13675    it's the latter, then this function is only being called to output a
13676    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
13677    argument type of some subprogram type.  */
13678
13679 static dw_die_ref
13680 gen_formal_parameter_die (tree node, tree origin, dw_die_ref context_die)
13681 {
13682   tree node_or_origin = node ? node : origin;
13683   dw_die_ref parm_die
13684     = new_die (DW_TAG_formal_parameter, context_die, node);
13685
13686   switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
13687     {
13688     case tcc_declaration:
13689       if (!origin)
13690         origin = decl_ultimate_origin (node);
13691       if (origin != NULL)
13692         add_abstract_origin_attribute (parm_die, origin);
13693       else
13694         {
13695           tree type = TREE_TYPE (node);
13696           add_name_and_src_coords_attributes (parm_die, node);
13697           if (DECL_BY_REFERENCE (node))
13698             add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
13699                                 context_die);
13700           else
13701             add_type_attribute (parm_die, type,
13702                                 TREE_READONLY (node),
13703                                 TREE_THIS_VOLATILE (node),
13704                                 context_die);
13705           if (DECL_ARTIFICIAL (node))
13706             add_AT_flag (parm_die, DW_AT_artificial, 1);
13707         }
13708
13709       if (node)
13710         equate_decl_number_to_die (node, parm_die);
13711       if (! DECL_ABSTRACT (node_or_origin))
13712         add_location_or_const_value_attribute (parm_die, node_or_origin,
13713                                                DW_AT_location);
13714
13715       break;
13716
13717     case tcc_type:
13718       /* We were called with some kind of a ..._TYPE node.  */
13719       add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
13720       break;
13721
13722     default:
13723       gcc_unreachable ();
13724     }
13725
13726   return parm_die;
13727 }
13728
13729 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
13730    at the end of an (ANSI prototyped) formal parameters list.  */
13731
13732 static void
13733 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
13734 {
13735   new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
13736 }
13737
13738 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
13739    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
13740    parameters as specified in some function type specification (except for
13741    those which appear as part of a function *definition*).  */
13742
13743 static void
13744 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
13745 {
13746   tree link;
13747   tree formal_type = NULL;
13748   tree first_parm_type;
13749   tree arg;
13750
13751   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
13752     {
13753       arg = DECL_ARGUMENTS (function_or_method_type);
13754       function_or_method_type = TREE_TYPE (function_or_method_type);
13755     }
13756   else
13757     arg = NULL_TREE;
13758
13759   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
13760
13761   /* Make our first pass over the list of formal parameter types and output a
13762      DW_TAG_formal_parameter DIE for each one.  */
13763   for (link = first_parm_type; link; )
13764     {
13765       dw_die_ref parm_die;
13766
13767       formal_type = TREE_VALUE (link);
13768       if (formal_type == void_type_node)
13769         break;
13770
13771       /* Output a (nameless) DIE to represent the formal parameter itself.  */
13772       parm_die = gen_formal_parameter_die (formal_type, NULL, context_die);
13773       if ((TREE_CODE (function_or_method_type) == METHOD_TYPE
13774            && link == first_parm_type)
13775           || (arg && DECL_ARTIFICIAL (arg)))
13776         add_AT_flag (parm_die, DW_AT_artificial, 1);
13777
13778       link = TREE_CHAIN (link);
13779       if (arg)
13780         arg = TREE_CHAIN (arg);
13781     }
13782
13783   /* If this function type has an ellipsis, add a
13784      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
13785   if (formal_type != void_type_node)
13786     gen_unspecified_parameters_die (function_or_method_type, context_die);
13787
13788   /* Make our second (and final) pass over the list of formal parameter types
13789      and output DIEs to represent those types (as necessary).  */
13790   for (link = TYPE_ARG_TYPES (function_or_method_type);
13791        link && TREE_VALUE (link);
13792        link = TREE_CHAIN (link))
13793     gen_type_die (TREE_VALUE (link), context_die);
13794 }
13795
13796 /* We want to generate the DIE for TYPE so that we can generate the
13797    die for MEMBER, which has been defined; we will need to refer back
13798    to the member declaration nested within TYPE.  If we're trying to
13799    generate minimal debug info for TYPE, processing TYPE won't do the
13800    trick; we need to attach the member declaration by hand.  */
13801
13802 static void
13803 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
13804 {
13805   gen_type_die (type, context_die);
13806
13807   /* If we're trying to avoid duplicate debug info, we may not have
13808      emitted the member decl for this function.  Emit it now.  */
13809   if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
13810       && ! lookup_decl_die (member))
13811     {
13812       dw_die_ref type_die;
13813       gcc_assert (!decl_ultimate_origin (member));
13814
13815       push_decl_scope (type);
13816       type_die = lookup_type_die (type);
13817       if (TREE_CODE (member) == FUNCTION_DECL)
13818         gen_subprogram_die (member, type_die);
13819       else if (TREE_CODE (member) == FIELD_DECL)
13820         {
13821           /* Ignore the nameless fields that are used to skip bits but handle
13822              C++ anonymous unions and structs.  */
13823           if (DECL_NAME (member) != NULL_TREE
13824               || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
13825               || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
13826             {
13827               gen_type_die (member_declared_type (member), type_die);
13828               gen_field_die (member, type_die);
13829             }
13830         }
13831       else
13832         gen_variable_die (member, NULL_TREE, type_die);
13833
13834       pop_decl_scope ();
13835     }
13836 }
13837
13838 /* Generate the DWARF2 info for the "abstract" instance of a function which we
13839    may later generate inlined and/or out-of-line instances of.  */
13840
13841 static void
13842 dwarf2out_abstract_function (tree decl)
13843 {
13844   dw_die_ref old_die;
13845   tree save_fn;
13846   tree context;
13847   int was_abstract = DECL_ABSTRACT (decl);
13848
13849   /* Make sure we have the actual abstract inline, not a clone.  */
13850   decl = DECL_ORIGIN (decl);
13851   htab_empty (decl_loc_table);
13852
13853   old_die = lookup_decl_die (decl);
13854   if (old_die && get_AT (old_die, DW_AT_inline))
13855     /* We've already generated the abstract instance.  */
13856     return;
13857
13858   /* Be sure we've emitted the in-class declaration DIE (if any) first, so
13859      we don't get confused by DECL_ABSTRACT.  */
13860   if (debug_info_level > DINFO_LEVEL_TERSE)
13861     {
13862       context = decl_class_context (decl);
13863       if (context)
13864         gen_type_die_for_member
13865           (context, decl, decl_function_context (decl) ? NULL : comp_unit_die);
13866     }
13867
13868   /* Pretend we've just finished compiling this function.  */
13869   save_fn = current_function_decl;
13870   current_function_decl = decl;
13871   push_cfun (DECL_STRUCT_FUNCTION (decl));
13872
13873   set_decl_abstract_flags (decl, 1);
13874   dwarf2out_decl (decl);
13875   if (! was_abstract)
13876     set_decl_abstract_flags (decl, 0);
13877
13878   current_function_decl = save_fn;
13879   pop_cfun ();
13880 }
13881
13882 /* Helper function of premark_used_types() which gets called through
13883    htab_traverse_resize().
13884
13885    Marks the DIE of a given type in *SLOT as perennial, so it never gets
13886    marked as unused by prune_unused_types.  */
13887 static int
13888 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
13889 {
13890   tree type;
13891   dw_die_ref die;
13892
13893   type = (tree) *slot;
13894   die = lookup_type_die (type);
13895   if (die != NULL)
13896     die->die_perennial_p = 1;
13897   return 1;
13898 }
13899
13900 /* Mark all members of used_types_hash as perennial.  */
13901 static void
13902 premark_used_types (void)
13903 {
13904   if (cfun && cfun->used_types_hash)
13905     htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
13906 }
13907
13908 /* Generate a DIE to represent a declared function (either file-scope or
13909    block-local).  */
13910
13911 static void
13912 gen_subprogram_die (tree decl, dw_die_ref context_die)
13913 {
13914   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
13915   tree origin = decl_ultimate_origin (decl);
13916   dw_die_ref subr_die;
13917   tree fn_arg_types;
13918   tree outer_scope;
13919   dw_die_ref old_die = lookup_decl_die (decl);
13920   int declaration = (current_function_decl != decl
13921                      || class_or_namespace_scope_p (context_die));
13922
13923   premark_used_types ();
13924
13925   /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
13926      started to generate the abstract instance of an inline, decided to output
13927      its containing class, and proceeded to emit the declaration of the inline
13928      from the member list for the class.  If so, DECLARATION takes priority;
13929      we'll get back to the abstract instance when done with the class.  */
13930
13931   /* The class-scope declaration DIE must be the primary DIE.  */
13932   if (origin && declaration && class_or_namespace_scope_p (context_die))
13933     {
13934       origin = NULL;
13935       gcc_assert (!old_die);
13936     }
13937
13938   /* Now that the C++ front end lazily declares artificial member fns, we
13939      might need to retrofit the declaration into its class.  */
13940   if (!declaration && !origin && !old_die
13941       && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
13942       && !class_or_namespace_scope_p (context_die)
13943       && debug_info_level > DINFO_LEVEL_TERSE)
13944     old_die = force_decl_die (decl);
13945
13946   if (origin != NULL)
13947     {
13948       gcc_assert (!declaration || local_scope_p (context_die));
13949
13950       /* Fixup die_parent for the abstract instance of a nested
13951          inline function.  */
13952       if (old_die && old_die->die_parent == NULL)
13953         add_child_die (context_die, old_die);
13954
13955       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
13956       add_abstract_origin_attribute (subr_die, origin);
13957     }
13958   else if (old_die)
13959     {
13960       expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
13961       struct dwarf_file_data * file_index = lookup_filename (s.file);
13962
13963       if (!get_AT_flag (old_die, DW_AT_declaration)
13964           /* We can have a normal definition following an inline one in the
13965              case of redefinition of GNU C extern inlines.
13966              It seems reasonable to use AT_specification in this case.  */
13967           && !get_AT (old_die, DW_AT_inline))
13968         {
13969           /* Detect and ignore this case, where we are trying to output
13970              something we have already output.  */
13971           return;
13972         }
13973
13974       /* If the definition comes from the same place as the declaration,
13975          maybe use the old DIE.  We always want the DIE for this function
13976          that has the *_pc attributes to be under comp_unit_die so the
13977          debugger can find it.  We also need to do this for abstract
13978          instances of inlines, since the spec requires the out-of-line copy
13979          to have the same parent.  For local class methods, this doesn't
13980          apply; we just use the old DIE.  */
13981       if ((old_die->die_parent == comp_unit_die || context_die == NULL)
13982           && (DECL_ARTIFICIAL (decl)
13983               || (get_AT_file (old_die, DW_AT_decl_file) == file_index
13984                   && (get_AT_unsigned (old_die, DW_AT_decl_line)
13985                       == (unsigned) s.line))))
13986         {
13987           subr_die = old_die;
13988
13989           /* Clear out the declaration attribute and the formal parameters.
13990              Do not remove all children, because it is possible that this
13991              declaration die was forced using force_decl_die(). In such
13992              cases die that forced declaration die (e.g. TAG_imported_module)
13993              is one of the children that we do not want to remove.  */
13994           remove_AT (subr_die, DW_AT_declaration);
13995           remove_child_TAG (subr_die, DW_TAG_formal_parameter);
13996         }
13997       else
13998         {
13999           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
14000           add_AT_specification (subr_die, old_die);
14001           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
14002             add_AT_file (subr_die, DW_AT_decl_file, file_index);
14003           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
14004             add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
14005         }
14006     }
14007   else
14008     {
14009       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
14010
14011       if (TREE_PUBLIC (decl))
14012         add_AT_flag (subr_die, DW_AT_external, 1);
14013
14014       add_name_and_src_coords_attributes (subr_die, decl);
14015       if (debug_info_level > DINFO_LEVEL_TERSE)
14016         {
14017           add_prototyped_attribute (subr_die, TREE_TYPE (decl));
14018           add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
14019                               0, 0, context_die);
14020         }
14021
14022       add_pure_or_virtual_attribute (subr_die, decl);
14023       if (DECL_ARTIFICIAL (decl))
14024         add_AT_flag (subr_die, DW_AT_artificial, 1);
14025
14026       if (TREE_PROTECTED (decl))
14027         add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_protected);
14028       else if (TREE_PRIVATE (decl))
14029         add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_private);
14030     }
14031
14032   if (declaration)
14033     {
14034       if (!old_die || !get_AT (old_die, DW_AT_inline))
14035         {
14036           add_AT_flag (subr_die, DW_AT_declaration, 1);
14037
14038           /* If this is an explicit function declaration then generate
14039              a DW_AT_explicit attribute.  */
14040           if (lang_hooks.decls.function_decl_explicit_p (decl))
14041             add_AT_flag (subr_die, DW_AT_explicit, 1);
14042
14043           /* The first time we see a member function, it is in the context of
14044              the class to which it belongs.  We make sure of this by emitting
14045              the class first.  The next time is the definition, which is
14046              handled above.  The two may come from the same source text.
14047
14048              Note that force_decl_die() forces function declaration die. It is
14049              later reused to represent definition.  */
14050           equate_decl_number_to_die (decl, subr_die);
14051         }
14052     }
14053   else if (DECL_ABSTRACT (decl))
14054     {
14055       if (DECL_DECLARED_INLINE_P (decl))
14056         {
14057           if (cgraph_function_possibly_inlined_p (decl))
14058             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
14059           else
14060             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
14061         }
14062       else
14063         {
14064           if (cgraph_function_possibly_inlined_p (decl))
14065             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
14066           else
14067             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
14068         }
14069
14070       if (DECL_DECLARED_INLINE_P (decl)
14071           && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
14072         add_AT_flag (subr_die, DW_AT_artificial, 1);
14073
14074       equate_decl_number_to_die (decl, subr_die);
14075     }
14076   else if (!DECL_EXTERNAL (decl))
14077     {
14078       HOST_WIDE_INT cfa_fb_offset;
14079
14080       if (!old_die || !get_AT (old_die, DW_AT_inline))
14081         equate_decl_number_to_die (decl, subr_die);
14082
14083       if (!flag_reorder_blocks_and_partition)
14084         {
14085           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
14086                                        current_function_funcdef_no);
14087           add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
14088           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
14089                                        current_function_funcdef_no);
14090           add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
14091
14092           add_pubname (decl, subr_die);
14093           add_arange (decl, subr_die);
14094         }
14095       else
14096         {  /* Do nothing for now; maybe need to duplicate die, one for
14097               hot section and one for cold section, then use the hot/cold
14098               section begin/end labels to generate the aranges...  */
14099           /*
14100             add_AT_lbl_id (subr_die, DW_AT_low_pc, hot_section_label);
14101             add_AT_lbl_id (subr_die, DW_AT_high_pc, hot_section_end_label);
14102             add_AT_lbl_id (subr_die, DW_AT_lo_user, unlikely_section_label);
14103             add_AT_lbl_id (subr_die, DW_AT_hi_user, cold_section_end_label);
14104
14105             add_pubname (decl, subr_die);
14106             add_arange (decl, subr_die);
14107             add_arange (decl, subr_die);
14108            */
14109         }
14110
14111 #ifdef MIPS_DEBUGGING_INFO
14112       /* Add a reference to the FDE for this routine.  */
14113       add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
14114 #endif
14115
14116       cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
14117
14118       /* We define the "frame base" as the function's CFA.  This is more
14119          convenient for several reasons: (1) It's stable across the prologue
14120          and epilogue, which makes it better than just a frame pointer,
14121          (2) With dwarf3, there exists a one-byte encoding that allows us
14122          to reference the .debug_frame data by proxy, but failing that,
14123          (3) We can at least reuse the code inspection and interpretation
14124          code that determines the CFA position at various points in the
14125          function.  */
14126       if (dwarf_version >= 3)
14127         {
14128           dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
14129           add_AT_loc (subr_die, DW_AT_frame_base, op);
14130         }
14131       else
14132         {
14133           dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
14134           if (list->dw_loc_next)
14135             add_AT_loc_list (subr_die, DW_AT_frame_base, list);
14136           else
14137             add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
14138         }
14139
14140       /* Compute a displacement from the "steady-state frame pointer" to
14141          the CFA.  The former is what all stack slots and argument slots
14142          will reference in the rtl; the later is what we've told the
14143          debugger about.  We'll need to adjust all frame_base references
14144          by this displacement.  */
14145       compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
14146
14147       if (cfun->static_chain_decl)
14148         add_AT_location_description (subr_die, DW_AT_static_link,
14149                  loc_descriptor_from_tree (cfun->static_chain_decl));
14150     }
14151
14152   /* Now output descriptions of the arguments for this function. This gets
14153      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
14154      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
14155      `...' at the end of the formal parameter list.  In order to find out if
14156      there was a trailing ellipsis or not, we must instead look at the type
14157      associated with the FUNCTION_DECL.  This will be a node of type
14158      FUNCTION_TYPE. If the chain of type nodes hanging off of this
14159      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
14160      an ellipsis at the end.  */
14161
14162   /* In the case where we are describing a mere function declaration, all we
14163      need to do here (and all we *can* do here) is to describe the *types* of
14164      its formal parameters.  */
14165   if (debug_info_level <= DINFO_LEVEL_TERSE)
14166     ;
14167   else if (declaration)
14168     gen_formal_types_die (decl, subr_die);
14169   else
14170     {
14171       /* Generate DIEs to represent all known formal parameters.  */
14172       tree arg_decls = DECL_ARGUMENTS (decl);
14173       tree parm;
14174
14175       /* When generating DIEs, generate the unspecified_parameters DIE
14176          instead if we come across the arg "__builtin_va_alist" */
14177       for (parm = arg_decls; parm; parm = TREE_CHAIN (parm))
14178         if (TREE_CODE (parm) == PARM_DECL)
14179           {
14180             if (DECL_NAME (parm)
14181                 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (parm)),
14182                             "__builtin_va_alist"))
14183               gen_unspecified_parameters_die (parm, subr_die);
14184             else
14185               gen_decl_die (parm, NULL, subr_die);
14186           }
14187
14188       /* Decide whether we need an unspecified_parameters DIE at the end.
14189          There are 2 more cases to do this for: 1) the ansi ... declaration -
14190          this is detectable when the end of the arg list is not a
14191          void_type_node 2) an unprototyped function declaration (not a
14192          definition).  This just means that we have no info about the
14193          parameters at all.  */
14194       fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
14195       if (fn_arg_types != NULL)
14196         {
14197           /* This is the prototyped case, check for....  */
14198           if (TREE_VALUE (tree_last (fn_arg_types)) != void_type_node)
14199             gen_unspecified_parameters_die (decl, subr_die);
14200         }
14201       else if (DECL_INITIAL (decl) == NULL_TREE)
14202         gen_unspecified_parameters_die (decl, subr_die);
14203     }
14204
14205   /* Output Dwarf info for all of the stuff within the body of the function
14206      (if it has one - it may be just a declaration).  */
14207   outer_scope = DECL_INITIAL (decl);
14208
14209   /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
14210      a function.  This BLOCK actually represents the outermost binding contour
14211      for the function, i.e. the contour in which the function's formal
14212      parameters and labels get declared. Curiously, it appears that the front
14213      end doesn't actually put the PARM_DECL nodes for the current function onto
14214      the BLOCK_VARS list for this outer scope, but are strung off of the
14215      DECL_ARGUMENTS list for the function instead.
14216
14217      The BLOCK_VARS list for the `outer_scope' does provide us with a list of
14218      the LABEL_DECL nodes for the function however, and we output DWARF info
14219      for those in decls_for_scope.  Just within the `outer_scope' there will be
14220      a BLOCK node representing the function's outermost pair of curly braces,
14221      and any blocks used for the base and member initializers of a C++
14222      constructor function.  */
14223   if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
14224     {
14225       /* Emit a DW_TAG_variable DIE for a named return value.  */
14226       if (DECL_NAME (DECL_RESULT (decl)))
14227         gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
14228
14229       current_function_has_inlines = 0;
14230       decls_for_scope (outer_scope, subr_die, 0);
14231
14232 #if 0 && defined (MIPS_DEBUGGING_INFO)
14233       if (current_function_has_inlines)
14234         {
14235           add_AT_flag (subr_die, DW_AT_MIPS_has_inlines, 1);
14236           if (! comp_unit_has_inlines)
14237             {
14238               add_AT_flag (comp_unit_die, DW_AT_MIPS_has_inlines, 1);
14239               comp_unit_has_inlines = 1;
14240             }
14241         }
14242 #endif
14243     }
14244   /* Add the calling convention attribute if requested.  */
14245   add_calling_convention_attribute (subr_die, decl);
14246
14247 }
14248
14249 /* Returns a hash value for X (which really is a die_struct).  */
14250
14251 static hashval_t
14252 common_block_die_table_hash (const void *x)
14253 {
14254   const_dw_die_ref d = (const_dw_die_ref) x;
14255   return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
14256 }
14257
14258 /* Return nonzero if decl_id and die_parent of die_struct X is the same
14259    as decl_id and die_parent of die_struct Y.  */
14260
14261 static int
14262 common_block_die_table_eq (const void *x, const void *y)
14263 {
14264   const_dw_die_ref d = (const_dw_die_ref) x;
14265   const_dw_die_ref e = (const_dw_die_ref) y;
14266   return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
14267 }
14268
14269 /* Generate a DIE to represent a declared data object.
14270    Either DECL or ORIGIN must be non-null.  */
14271
14272 static void
14273 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
14274 {
14275   HOST_WIDE_INT off;
14276   tree com_decl;
14277   tree decl_or_origin = decl ? decl : origin;
14278   dw_die_ref var_die;
14279   dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
14280   dw_die_ref origin_die;
14281   int declaration = (DECL_EXTERNAL (decl_or_origin)
14282                      /* If DECL is COMDAT and has not actually been
14283                         emitted, we cannot take its address; there
14284                         might end up being no definition anywhere in
14285                         the program.  For example, consider the C++
14286                         test case:
14287
14288                           template <class T>
14289                           struct S { static const int i = 7; };
14290
14291                           template <class T>
14292                           const int S<T>::i;
14293
14294                           int f() { return S<int>::i; }
14295
14296                         Here, S<int>::i is not DECL_EXTERNAL, but no
14297                         definition is required, so the compiler will
14298                         not emit a definition.  */
14299                      || (TREE_CODE (decl_or_origin) == VAR_DECL
14300                          && DECL_COMDAT (decl_or_origin)
14301                          && !TREE_ASM_WRITTEN (decl_or_origin))
14302                      || class_or_namespace_scope_p (context_die));
14303
14304   if (!origin)
14305     origin = decl_ultimate_origin (decl);
14306
14307   com_decl = fortran_common (decl_or_origin, &off);
14308
14309   /* Symbol in common gets emitted as a child of the common block, in the form
14310      of a data member.  */
14311   if (com_decl)
14312     {
14313       tree field;
14314       dw_die_ref com_die;
14315       dw_loc_descr_ref loc;
14316       die_node com_die_arg;
14317
14318       var_die = lookup_decl_die (decl_or_origin);
14319       if (var_die)
14320         {
14321           if (get_AT (var_die, DW_AT_location) == NULL)
14322             {
14323               loc = loc_descriptor_from_tree (com_decl);
14324               if (loc)
14325                 {
14326                   if (off)
14327                     {
14328                       /* Optimize the common case.  */
14329                       if (loc->dw_loc_opc == DW_OP_addr
14330                           && loc->dw_loc_next == NULL
14331                           && GET_CODE (loc->dw_loc_oprnd1.v.val_addr)
14332                              == SYMBOL_REF)
14333                         loc->dw_loc_oprnd1.v.val_addr
14334                           = plus_constant (loc->dw_loc_oprnd1.v.val_addr, off);
14335                         else
14336                           loc_descr_plus_const (&loc, off);
14337                     }
14338                   add_AT_loc (var_die, DW_AT_location, loc);
14339                   remove_AT (var_die, DW_AT_declaration);
14340                 }
14341             }
14342           return;
14343         }
14344
14345       if (common_block_die_table == NULL)
14346         common_block_die_table
14347           = htab_create_ggc (10, common_block_die_table_hash,
14348                              common_block_die_table_eq, NULL);
14349
14350       field = TREE_OPERAND (DECL_VALUE_EXPR (decl), 0);
14351       com_die_arg.decl_id = DECL_UID (com_decl);
14352       com_die_arg.die_parent = context_die;
14353       com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
14354       loc = loc_descriptor_from_tree (com_decl);
14355       if (com_die == NULL)
14356         {
14357           const char *cnam
14358             = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
14359           void **slot;
14360
14361           com_die = new_die (DW_TAG_common_block, context_die, decl);
14362           add_name_and_src_coords_attributes (com_die, com_decl);
14363           if (loc)
14364             {
14365               add_AT_loc (com_die, DW_AT_location, loc);
14366               /* Avoid sharing the same loc descriptor between
14367                  DW_TAG_common_block and DW_TAG_variable.  */
14368               loc = loc_descriptor_from_tree (com_decl);
14369             }
14370           else if (DECL_EXTERNAL (decl))
14371             add_AT_flag (com_die, DW_AT_declaration, 1);
14372           add_pubname_string (cnam, com_die); /* ??? needed? */
14373           com_die->decl_id = DECL_UID (com_decl);
14374           slot = htab_find_slot (common_block_die_table, com_die, INSERT);
14375           *slot = (void *) com_die;
14376         }
14377       else if (get_AT (com_die, DW_AT_location) == NULL && loc)
14378         {
14379           add_AT_loc (com_die, DW_AT_location, loc);
14380           loc = loc_descriptor_from_tree (com_decl);
14381           remove_AT (com_die, DW_AT_declaration);
14382         }
14383       var_die = new_die (DW_TAG_variable, com_die, decl);
14384       add_name_and_src_coords_attributes (var_die, decl);
14385       add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
14386                           TREE_THIS_VOLATILE (decl), context_die);
14387       add_AT_flag (var_die, DW_AT_external, 1);
14388       if (loc)
14389         {
14390           if (off)
14391             {
14392               /* Optimize the common case.  */
14393               if (loc->dw_loc_opc == DW_OP_addr
14394                   && loc->dw_loc_next == NULL
14395                   && GET_CODE (loc->dw_loc_oprnd1.v.val_addr) == 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         }
14403       else if (DECL_EXTERNAL (decl))
14404         add_AT_flag (var_die, DW_AT_declaration, 1);
14405       equate_decl_number_to_die (decl, var_die);
14406       return;
14407     }
14408
14409   /* If the compiler emitted a definition for the DECL declaration
14410      and if we already emitted a DIE for it, don't emit a second
14411      DIE for it again.  */
14412   if (old_die
14413       && declaration
14414       && old_die->die_parent == context_die)
14415     return;
14416
14417   /* For static data members, the declaration in the class is supposed
14418      to have DW_TAG_member tag; the specification should still be
14419      DW_TAG_variable referencing the DW_TAG_member DIE.  */
14420   if (declaration && class_scope_p (context_die))
14421     var_die = new_die (DW_TAG_member, context_die, decl);
14422   else
14423     var_die = new_die (DW_TAG_variable, context_die, decl);
14424
14425   origin_die = NULL;
14426   if (origin != NULL)
14427     origin_die = add_abstract_origin_attribute (var_die, origin);
14428
14429   /* Loop unrolling can create multiple blocks that refer to the same
14430      static variable, so we must test for the DW_AT_declaration flag.
14431
14432      ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
14433      copy decls and set the DECL_ABSTRACT flag on them instead of
14434      sharing them.
14435
14436      ??? Duplicated blocks have been rewritten to use .debug_ranges.
14437
14438      ??? The declare_in_namespace support causes us to get two DIEs for one
14439      variable, both of which are declarations.  We want to avoid considering
14440      one to be a specification, so we must test that this DIE is not a
14441      declaration.  */
14442   else if (old_die && TREE_STATIC (decl) && ! declaration
14443            && get_AT_flag (old_die, DW_AT_declaration) == 1)
14444     {
14445       /* This is a definition of a C++ class level static.  */
14446       add_AT_specification (var_die, old_die);
14447       if (DECL_NAME (decl))
14448         {
14449           expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
14450           struct dwarf_file_data * file_index = lookup_filename (s.file);
14451
14452           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
14453             add_AT_file (var_die, DW_AT_decl_file, file_index);
14454
14455           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
14456             add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
14457         }
14458     }
14459   else
14460     {
14461       tree type = TREE_TYPE (decl);
14462
14463       add_name_and_src_coords_attributes (var_die, decl);
14464       if ((TREE_CODE (decl) == PARM_DECL
14465            || TREE_CODE (decl) == RESULT_DECL
14466            || TREE_CODE (decl) == VAR_DECL)
14467           && DECL_BY_REFERENCE (decl))
14468         add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
14469       else
14470         add_type_attribute (var_die, type, TREE_READONLY (decl),
14471                             TREE_THIS_VOLATILE (decl), context_die);
14472
14473       if (TREE_PUBLIC (decl))
14474         add_AT_flag (var_die, DW_AT_external, 1);
14475
14476       if (DECL_ARTIFICIAL (decl))
14477         add_AT_flag (var_die, DW_AT_artificial, 1);
14478
14479       if (TREE_PROTECTED (decl))
14480         add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_protected);
14481       else if (TREE_PRIVATE (decl))
14482         add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_private);
14483     }
14484
14485   if (declaration)
14486     add_AT_flag (var_die, DW_AT_declaration, 1);
14487
14488   if (decl && (DECL_ABSTRACT (decl) || declaration))
14489     equate_decl_number_to_die (decl, var_die);
14490
14491   if (! declaration
14492       && (! DECL_ABSTRACT (decl_or_origin)
14493           /* Local static vars are shared between all clones/inlines,
14494              so emit DW_AT_location on the abstract DIE if DECL_RTL is
14495              already set.  */
14496           || (TREE_CODE (decl_or_origin) == VAR_DECL
14497               && TREE_STATIC (decl_or_origin)
14498               && DECL_RTL_SET_P (decl_or_origin)))
14499       /* When abstract origin already has DW_AT_location attribute, no need
14500          to add it again.  */
14501       && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
14502     {
14503       if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
14504           && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
14505         defer_location (decl_or_origin, var_die);
14506       else
14507         add_location_or_const_value_attribute (var_die,
14508                                                decl_or_origin,
14509                                                DW_AT_location);
14510       add_pubname (decl_or_origin, var_die);
14511     }
14512   else
14513     tree_add_const_value_attribute (var_die, decl_or_origin);
14514 }
14515
14516 /* Generate a DIE to represent a named constant.  */
14517
14518 static void
14519 gen_const_die (tree decl, dw_die_ref context_die)
14520 {
14521   dw_die_ref const_die;
14522   tree type = TREE_TYPE (decl);
14523
14524   const_die = new_die (DW_TAG_constant, context_die, decl);
14525   add_name_and_src_coords_attributes (const_die, decl);
14526   add_type_attribute (const_die, type, 1, 0, context_die);
14527   if (TREE_PUBLIC (decl))
14528     add_AT_flag (const_die, DW_AT_external, 1);
14529   if (DECL_ARTIFICIAL (decl))
14530     add_AT_flag (const_die, DW_AT_artificial, 1);
14531   tree_add_const_value_attribute (const_die, decl);
14532 }
14533
14534 /* Generate a DIE to represent a label identifier.  */
14535
14536 static void
14537 gen_label_die (tree decl, dw_die_ref context_die)
14538 {
14539   tree origin = decl_ultimate_origin (decl);
14540   dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
14541   rtx insn;
14542   char label[MAX_ARTIFICIAL_LABEL_BYTES];
14543
14544   if (origin != NULL)
14545     add_abstract_origin_attribute (lbl_die, origin);
14546   else
14547     add_name_and_src_coords_attributes (lbl_die, decl);
14548
14549   if (DECL_ABSTRACT (decl))
14550     equate_decl_number_to_die (decl, lbl_die);
14551   else
14552     {
14553       insn = DECL_RTL_IF_SET (decl);
14554
14555       /* Deleted labels are programmer specified labels which have been
14556          eliminated because of various optimizations.  We still emit them
14557          here so that it is possible to put breakpoints on them.  */
14558       if (insn
14559           && (LABEL_P (insn)
14560               || ((NOTE_P (insn)
14561                    && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
14562         {
14563           /* When optimization is enabled (via -O) some parts of the compiler
14564              (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
14565              represent source-level labels which were explicitly declared by
14566              the user.  This really shouldn't be happening though, so catch
14567              it if it ever does happen.  */
14568           gcc_assert (!INSN_DELETED_P (insn));
14569
14570           ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
14571           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
14572         }
14573     }
14574 }
14575
14576 /* A helper function for gen_inlined_subroutine_die.  Add source coordinate
14577    attributes to the DIE for a block STMT, to describe where the inlined
14578    function was called from.  This is similar to add_src_coords_attributes.  */
14579
14580 static inline void
14581 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
14582 {
14583   expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
14584
14585   add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
14586   add_AT_unsigned (die, DW_AT_call_line, s.line);
14587 }
14588
14589
14590 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
14591    Add low_pc and high_pc attributes to the DIE for a block STMT.  */
14592
14593 static inline void
14594 add_high_low_attributes (tree stmt, dw_die_ref die)
14595 {
14596   char label[MAX_ARTIFICIAL_LABEL_BYTES];
14597
14598   if (BLOCK_FRAGMENT_CHAIN (stmt))
14599     {
14600       tree chain;
14601
14602       if (inlined_function_outer_scope_p (stmt))
14603         {
14604           ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
14605                                        BLOCK_NUMBER (stmt));
14606           add_AT_lbl_id (die, DW_AT_entry_pc, label);
14607         }
14608
14609       add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
14610
14611       chain = BLOCK_FRAGMENT_CHAIN (stmt);
14612       do
14613         {
14614           add_ranges (chain);
14615           chain = BLOCK_FRAGMENT_CHAIN (chain);
14616         }
14617       while (chain);
14618       add_ranges (NULL);
14619     }
14620   else
14621     {
14622       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
14623                                    BLOCK_NUMBER (stmt));
14624       add_AT_lbl_id (die, DW_AT_low_pc, label);
14625       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
14626                                    BLOCK_NUMBER (stmt));
14627       add_AT_lbl_id (die, DW_AT_high_pc, label);
14628     }
14629 }
14630
14631 /* Generate a DIE for a lexical block.  */
14632
14633 static void
14634 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
14635 {
14636   dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
14637
14638   if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
14639     add_high_low_attributes (stmt, stmt_die);
14640
14641   decls_for_scope (stmt, stmt_die, depth);
14642 }
14643
14644 /* Generate a DIE for an inlined subprogram.  */
14645
14646 static void
14647 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
14648 {
14649   tree decl = block_ultimate_origin (stmt);
14650
14651   /* Emit info for the abstract instance first, if we haven't yet.  We
14652      must emit this even if the block is abstract, otherwise when we
14653      emit the block below (or elsewhere), we may end up trying to emit
14654      a die whose origin die hasn't been emitted, and crashing.  */
14655   dwarf2out_abstract_function (decl);
14656
14657   if (! BLOCK_ABSTRACT (stmt))
14658     {
14659       dw_die_ref subr_die
14660         = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
14661
14662       add_abstract_origin_attribute (subr_die, decl);
14663       if (TREE_ASM_WRITTEN (stmt))
14664         add_high_low_attributes (stmt, subr_die);
14665       add_call_src_coords_attributes (stmt, subr_die);
14666
14667       decls_for_scope (stmt, subr_die, depth);
14668       current_function_has_inlines = 1;
14669     }
14670   else
14671     /* We may get here if we're the outer block of function A that was
14672        inlined into function B that was inlined into function C.  When
14673        generating debugging info for C, dwarf2out_abstract_function(B)
14674        would mark all inlined blocks as abstract, including this one.
14675        So, we wouldn't (and shouldn't) expect labels to be generated
14676        for this one.  Instead, just emit debugging info for
14677        declarations within the block.  This is particularly important
14678        in the case of initializers of arguments passed from B to us:
14679        if they're statement expressions containing declarations, we
14680        wouldn't generate dies for their abstract variables, and then,
14681        when generating dies for the real variables, we'd die (pun
14682        intended :-)  */
14683     gen_lexical_block_die (stmt, context_die, depth);
14684 }
14685
14686 /* Generate a DIE for a field in a record, or structure.  */
14687
14688 static void
14689 gen_field_die (tree decl, dw_die_ref context_die)
14690 {
14691   dw_die_ref decl_die;
14692
14693   if (TREE_TYPE (decl) == error_mark_node)
14694     return;
14695
14696   decl_die = new_die (DW_TAG_member, context_die, decl);
14697   add_name_and_src_coords_attributes (decl_die, decl);
14698   add_type_attribute (decl_die, member_declared_type (decl),
14699                       TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
14700                       context_die);
14701
14702   if (DECL_BIT_FIELD_TYPE (decl))
14703     {
14704       add_byte_size_attribute (decl_die, decl);
14705       add_bit_size_attribute (decl_die, decl);
14706       add_bit_offset_attribute (decl_die, decl);
14707     }
14708
14709   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
14710     add_data_member_location_attribute (decl_die, decl);
14711
14712   if (DECL_ARTIFICIAL (decl))
14713     add_AT_flag (decl_die, DW_AT_artificial, 1);
14714
14715   if (TREE_PROTECTED (decl))
14716     add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_protected);
14717   else if (TREE_PRIVATE (decl))
14718     add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_private);
14719
14720   /* Equate decl number to die, so that we can look up this decl later on.  */
14721   equate_decl_number_to_die (decl, decl_die);
14722 }
14723
14724 #if 0
14725 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
14726    Use modified_type_die instead.
14727    We keep this code here just in case these types of DIEs may be needed to
14728    represent certain things in other languages (e.g. Pascal) someday.  */
14729
14730 static void
14731 gen_pointer_type_die (tree type, dw_die_ref context_die)
14732 {
14733   dw_die_ref ptr_die
14734     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
14735
14736   equate_type_number_to_die (type, ptr_die);
14737   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
14738   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
14739 }
14740
14741 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
14742    Use modified_type_die instead.
14743    We keep this code here just in case these types of DIEs may be needed to
14744    represent certain things in other languages (e.g. Pascal) someday.  */
14745
14746 static void
14747 gen_reference_type_die (tree type, dw_die_ref context_die)
14748 {
14749   dw_die_ref ref_die
14750     = new_die (DW_TAG_reference_type, scope_die_for (type, context_die), type);
14751
14752   equate_type_number_to_die (type, ref_die);
14753   add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
14754   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
14755 }
14756 #endif
14757
14758 /* Generate a DIE for a pointer to a member type.  */
14759
14760 static void
14761 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
14762 {
14763   dw_die_ref ptr_die
14764     = new_die (DW_TAG_ptr_to_member_type,
14765                scope_die_for (type, context_die), type);
14766
14767   equate_type_number_to_die (type, ptr_die);
14768   add_AT_die_ref (ptr_die, DW_AT_containing_type,
14769                   lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
14770   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
14771 }
14772
14773 /* Generate the DIE for the compilation unit.  */
14774
14775 static dw_die_ref
14776 gen_compile_unit_die (const char *filename)
14777 {
14778   dw_die_ref die;
14779   char producer[250];
14780   const char *language_string = lang_hooks.name;
14781   int language;
14782
14783   die = new_die (DW_TAG_compile_unit, NULL, NULL);
14784
14785   if (filename)
14786     {
14787       add_name_attribute (die, filename);
14788       /* Don't add cwd for <built-in>.  */
14789       if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
14790         add_comp_dir_attribute (die);
14791     }
14792
14793   sprintf (producer, "%s %s", language_string, version_string);
14794
14795 #ifdef MIPS_DEBUGGING_INFO
14796   /* The MIPS/SGI compilers place the 'cc' command line options in the producer
14797      string.  The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
14798      not appear in the producer string, the debugger reaches the conclusion
14799      that the object file is stripped and has no debugging information.
14800      To get the MIPS/SGI debugger to believe that there is debugging
14801      information in the object file, we add a -g to the producer string.  */
14802   if (debug_info_level > DINFO_LEVEL_TERSE)
14803     strcat (producer, " -g");
14804 #endif
14805
14806   add_AT_string (die, DW_AT_producer, producer);
14807
14808   if (strcmp (language_string, "GNU C++") == 0)
14809     language = DW_LANG_C_plus_plus;
14810   else if (strcmp (language_string, "GNU Ada") == 0)
14811     language = DW_LANG_Ada95;
14812   else if (strcmp (language_string, "GNU F77") == 0)
14813     language = DW_LANG_Fortran77;
14814   else if (strcmp (language_string, "GNU Fortran") == 0)
14815     language = DW_LANG_Fortran95;
14816   else if (strcmp (language_string, "GNU Pascal") == 0)
14817     language = DW_LANG_Pascal83;
14818   else if (strcmp (language_string, "GNU Java") == 0)
14819     language = DW_LANG_Java;
14820   else if (strcmp (language_string, "GNU Objective-C") == 0)
14821     language = DW_LANG_ObjC;
14822   else if (strcmp (language_string, "GNU Objective-C++") == 0)
14823     language = DW_LANG_ObjC_plus_plus;
14824   else
14825     language = DW_LANG_C89;
14826
14827   add_AT_unsigned (die, DW_AT_language, language);
14828   return die;
14829 }
14830
14831 /* Generate the DIE for a base class.  */
14832
14833 static void
14834 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
14835 {
14836   dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
14837
14838   add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
14839   add_data_member_location_attribute (die, binfo);
14840
14841   if (BINFO_VIRTUAL_P (binfo))
14842     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
14843
14844   if (access == access_public_node)
14845     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
14846   else if (access == access_protected_node)
14847     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
14848 }
14849
14850 /* Generate a DIE for a class member.  */
14851
14852 static void
14853 gen_member_die (tree type, dw_die_ref context_die)
14854 {
14855   tree member;
14856   tree binfo = TYPE_BINFO (type);
14857   dw_die_ref child;
14858
14859   /* If this is not an incomplete type, output descriptions of each of its
14860      members. Note that as we output the DIEs necessary to represent the
14861      members of this record or union type, we will also be trying to output
14862      DIEs to represent the *types* of those members. However the `type'
14863      function (above) will specifically avoid generating type DIEs for member
14864      types *within* the list of member DIEs for this (containing) type except
14865      for those types (of members) which are explicitly marked as also being
14866      members of this (containing) type themselves.  The g++ front- end can
14867      force any given type to be treated as a member of some other (containing)
14868      type by setting the TYPE_CONTEXT of the given (member) type to point to
14869      the TREE node representing the appropriate (containing) type.  */
14870
14871   /* First output info about the base classes.  */
14872   if (binfo)
14873     {
14874       VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
14875       int i;
14876       tree base;
14877
14878       for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
14879         gen_inheritance_die (base,
14880                              (accesses ? VEC_index (tree, accesses, i)
14881                               : access_public_node), context_die);
14882     }
14883
14884   /* Now output info about the data members and type members.  */
14885   for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member))
14886     {
14887       /* If we thought we were generating minimal debug info for TYPE
14888          and then changed our minds, some of the member declarations
14889          may have already been defined.  Don't define them again, but
14890          do put them in the right order.  */
14891
14892       child = lookup_decl_die (member);
14893       if (child)
14894         splice_child_die (context_die, child);
14895       else
14896         gen_decl_die (member, NULL, context_die);
14897     }
14898
14899   /* Now output info about the function members (if any).  */
14900   for (member = TYPE_METHODS (type); member; member = TREE_CHAIN (member))
14901     {
14902       /* Don't include clones in the member list.  */
14903       if (DECL_ABSTRACT_ORIGIN (member))
14904         continue;
14905
14906       child = lookup_decl_die (member);
14907       if (child)
14908         splice_child_die (context_die, child);
14909       else
14910         gen_decl_die (member, NULL, context_die);
14911     }
14912 }
14913
14914 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
14915    is set, we pretend that the type was never defined, so we only get the
14916    member DIEs needed by later specification DIEs.  */
14917
14918 static void
14919 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
14920                                 enum debug_info_usage usage)
14921 {
14922   dw_die_ref type_die = lookup_type_die (type);
14923   dw_die_ref scope_die = 0;
14924   int nested = 0;
14925   int complete = (TYPE_SIZE (type)
14926                   && (! TYPE_STUB_DECL (type)
14927                       || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
14928   int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
14929   complete = complete && should_emit_struct_debug (type, usage);
14930
14931   if (type_die && ! complete)
14932     return;
14933
14934   if (TYPE_CONTEXT (type) != NULL_TREE
14935       && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
14936           || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
14937     nested = 1;
14938
14939   scope_die = scope_die_for (type, context_die);
14940
14941   if (! type_die || (nested && scope_die == comp_unit_die))
14942     /* First occurrence of type or toplevel definition of nested class.  */
14943     {
14944       dw_die_ref old_die = type_die;
14945
14946       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
14947                           ? record_type_tag (type) : DW_TAG_union_type,
14948                           scope_die, type);
14949       equate_type_number_to_die (type, type_die);
14950       if (old_die)
14951         add_AT_specification (type_die, old_die);
14952       else
14953         add_name_attribute (type_die, type_tag (type));
14954     }
14955   else
14956     remove_AT (type_die, DW_AT_declaration);
14957
14958   /* If this type has been completed, then give it a byte_size attribute and
14959      then give a list of members.  */
14960   if (complete && !ns_decl)
14961     {
14962       /* Prevent infinite recursion in cases where the type of some member of
14963          this type is expressed in terms of this type itself.  */
14964       TREE_ASM_WRITTEN (type) = 1;
14965       add_byte_size_attribute (type_die, type);
14966       if (TYPE_STUB_DECL (type) != NULL_TREE)
14967         add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
14968
14969       /* If the first reference to this type was as the return type of an
14970          inline function, then it may not have a parent.  Fix this now.  */
14971       if (type_die->die_parent == NULL)
14972         add_child_die (scope_die, type_die);
14973
14974       push_decl_scope (type);
14975       gen_member_die (type, type_die);
14976       pop_decl_scope ();
14977
14978       /* GNU extension: Record what type our vtable lives in.  */
14979       if (TYPE_VFIELD (type))
14980         {
14981           tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
14982
14983           gen_type_die (vtype, context_die);
14984           add_AT_die_ref (type_die, DW_AT_containing_type,
14985                           lookup_type_die (vtype));
14986         }
14987     }
14988   else
14989     {
14990       add_AT_flag (type_die, DW_AT_declaration, 1);
14991
14992       /* We don't need to do this for function-local types.  */
14993       if (TYPE_STUB_DECL (type)
14994           && ! decl_function_context (TYPE_STUB_DECL (type)))
14995         VEC_safe_push (tree, gc, incomplete_types, type);
14996     }
14997
14998   if (get_AT (type_die, DW_AT_name))
14999     add_pubtype (type, type_die);
15000 }
15001
15002 /* Generate a DIE for a subroutine _type_.  */
15003
15004 static void
15005 gen_subroutine_type_die (tree type, dw_die_ref context_die)
15006 {
15007   tree return_type = TREE_TYPE (type);
15008   dw_die_ref subr_die
15009     = new_die (DW_TAG_subroutine_type,
15010                scope_die_for (type, context_die), type);
15011
15012   equate_type_number_to_die (type, subr_die);
15013   add_prototyped_attribute (subr_die, type);
15014   add_type_attribute (subr_die, return_type, 0, 0, context_die);
15015   gen_formal_types_die (type, subr_die);
15016
15017   if (get_AT (subr_die, DW_AT_name))
15018     add_pubtype (type, subr_die);
15019 }
15020
15021 /* Generate a DIE for a type definition.  */
15022
15023 static void
15024 gen_typedef_die (tree decl, dw_die_ref context_die)
15025 {
15026   dw_die_ref type_die;
15027   tree origin;
15028
15029   if (TREE_ASM_WRITTEN (decl))
15030     return;
15031
15032   TREE_ASM_WRITTEN (decl) = 1;
15033   type_die = new_die (DW_TAG_typedef, context_die, decl);
15034   origin = decl_ultimate_origin (decl);
15035   if (origin != NULL)
15036     add_abstract_origin_attribute (type_die, origin);
15037   else
15038     {
15039       tree type;
15040
15041       add_name_and_src_coords_attributes (type_die, decl);
15042       if (DECL_ORIGINAL_TYPE (decl))
15043         {
15044           type = DECL_ORIGINAL_TYPE (decl);
15045
15046           gcc_assert (type != TREE_TYPE (decl));
15047           equate_type_number_to_die (TREE_TYPE (decl), type_die);
15048         }
15049       else
15050         type = TREE_TYPE (decl);
15051
15052       add_type_attribute (type_die, type, TREE_READONLY (decl),
15053                           TREE_THIS_VOLATILE (decl), context_die);
15054     }
15055
15056   if (DECL_ABSTRACT (decl))
15057     equate_decl_number_to_die (decl, type_die);
15058
15059   if (get_AT (type_die, DW_AT_name))
15060     add_pubtype (decl, type_die);
15061 }
15062
15063 /* Generate a type description DIE.  */
15064
15065 static void
15066 gen_type_die_with_usage (tree type, dw_die_ref context_die,
15067                                 enum debug_info_usage usage)
15068 {
15069   int need_pop;
15070   struct array_descr_info info;
15071
15072   if (type == NULL_TREE || type == error_mark_node)
15073     return;
15074
15075   if (TYPE_NAME (type) && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
15076       && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
15077     {
15078       if (TREE_ASM_WRITTEN (type))
15079         return;
15080
15081       /* Prevent broken recursion; we can't hand off to the same type.  */
15082       gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
15083
15084       /* Use the DIE of the containing namespace as the parent DIE of
15085          the type description DIE we want to generate.  */
15086       if (DECL_CONTEXT (TYPE_NAME (type))
15087           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
15088         context_die = lookup_decl_die (DECL_CONTEXT (TYPE_NAME (type)));
15089
15090       TREE_ASM_WRITTEN (type) = 1;
15091       gen_decl_die (TYPE_NAME (type), NULL, context_die);
15092       return;
15093     }
15094
15095   /* If this is an array type with hidden descriptor, handle it first.  */
15096   if (!TREE_ASM_WRITTEN (type)
15097       && lang_hooks.types.get_array_descr_info
15098       && lang_hooks.types.get_array_descr_info (type, &info))
15099     {
15100       gen_descr_array_type_die (type, &info, context_die);
15101       TREE_ASM_WRITTEN (type) = 1;
15102       return;
15103     }
15104
15105   /* We are going to output a DIE to represent the unqualified version
15106      of this type (i.e. without any const or volatile qualifiers) so
15107      get the main variant (i.e. the unqualified version) of this type
15108      now.  (Vectors are special because the debugging info is in the
15109      cloned type itself).  */
15110   if (TREE_CODE (type) != VECTOR_TYPE)
15111     type = type_main_variant (type);
15112
15113   if (TREE_ASM_WRITTEN (type))
15114     return;
15115
15116   switch (TREE_CODE (type))
15117     {
15118     case ERROR_MARK:
15119       break;
15120
15121     case POINTER_TYPE:
15122     case REFERENCE_TYPE:
15123       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
15124          ensures that the gen_type_die recursion will terminate even if the
15125          type is recursive.  Recursive types are possible in Ada.  */
15126       /* ??? We could perhaps do this for all types before the switch
15127          statement.  */
15128       TREE_ASM_WRITTEN (type) = 1;
15129
15130       /* For these types, all that is required is that we output a DIE (or a
15131          set of DIEs) to represent the "basis" type.  */
15132       gen_type_die_with_usage (TREE_TYPE (type), context_die,
15133                                 DINFO_USAGE_IND_USE);
15134       break;
15135
15136     case OFFSET_TYPE:
15137       /* This code is used for C++ pointer-to-data-member types.
15138          Output a description of the relevant class type.  */
15139       gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
15140                                         DINFO_USAGE_IND_USE);
15141
15142       /* Output a description of the type of the object pointed to.  */
15143       gen_type_die_with_usage (TREE_TYPE (type), context_die,
15144                                         DINFO_USAGE_IND_USE);
15145
15146       /* Now output a DIE to represent this pointer-to-data-member type
15147          itself.  */
15148       gen_ptr_to_mbr_type_die (type, context_die);
15149       break;
15150
15151     case FUNCTION_TYPE:
15152       /* Force out return type (in case it wasn't forced out already).  */
15153       gen_type_die_with_usage (TREE_TYPE (type), context_die,
15154                                         DINFO_USAGE_DIR_USE);
15155       gen_subroutine_type_die (type, context_die);
15156       break;
15157
15158     case METHOD_TYPE:
15159       /* Force out return type (in case it wasn't forced out already).  */
15160       gen_type_die_with_usage (TREE_TYPE (type), context_die,
15161                                         DINFO_USAGE_DIR_USE);
15162       gen_subroutine_type_die (type, context_die);
15163       break;
15164
15165     case ARRAY_TYPE:
15166       gen_array_type_die (type, context_die);
15167       break;
15168
15169     case VECTOR_TYPE:
15170       gen_array_type_die (type, context_die);
15171       break;
15172
15173     case ENUMERAL_TYPE:
15174     case RECORD_TYPE:
15175     case UNION_TYPE:
15176     case QUAL_UNION_TYPE:
15177       /* If this is a nested type whose containing class hasn't been written
15178          out yet, writing it out will cover this one, too.  This does not apply
15179          to instantiations of member class templates; they need to be added to
15180          the containing class as they are generated.  FIXME: This hurts the
15181          idea of combining type decls from multiple TUs, since we can't predict
15182          what set of template instantiations we'll get.  */
15183       if (TYPE_CONTEXT (type)
15184           && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
15185           && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
15186         {
15187           gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
15188
15189           if (TREE_ASM_WRITTEN (type))
15190             return;
15191
15192           /* If that failed, attach ourselves to the stub.  */
15193           push_decl_scope (TYPE_CONTEXT (type));
15194           context_die = lookup_type_die (TYPE_CONTEXT (type));
15195           need_pop = 1;
15196         }
15197       else
15198         {
15199           context_die = declare_in_namespace (type, context_die);
15200           need_pop = 0;
15201         }
15202
15203       if (TREE_CODE (type) == ENUMERAL_TYPE)
15204         {
15205           /* This might have been written out by the call to
15206              declare_in_namespace.  */
15207           if (!TREE_ASM_WRITTEN (type))
15208             gen_enumeration_type_die (type, context_die);
15209         }
15210       else
15211         gen_struct_or_union_type_die (type, context_die, usage);
15212
15213       if (need_pop)
15214         pop_decl_scope ();
15215
15216       /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
15217          it up if it is ever completed.  gen_*_type_die will set it for us
15218          when appropriate.  */
15219       return;
15220
15221     case VOID_TYPE:
15222     case INTEGER_TYPE:
15223     case REAL_TYPE:
15224     case FIXED_POINT_TYPE:
15225     case COMPLEX_TYPE:
15226     case BOOLEAN_TYPE:
15227       /* No DIEs needed for fundamental types.  */
15228       break;
15229
15230     case LANG_TYPE:
15231       /* No Dwarf representation currently defined.  */
15232       break;
15233
15234     default:
15235       gcc_unreachable ();
15236     }
15237
15238   TREE_ASM_WRITTEN (type) = 1;
15239 }
15240
15241 static void
15242 gen_type_die (tree type, dw_die_ref context_die)
15243 {
15244   gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
15245 }
15246
15247 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
15248    things which are local to the given block.  */
15249
15250 static void
15251 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
15252 {
15253   int must_output_die = 0;
15254   bool inlined_func;
15255
15256   /* Ignore blocks that are NULL.  */
15257   if (stmt == NULL_TREE)
15258     return;
15259
15260   inlined_func = inlined_function_outer_scope_p (stmt);
15261
15262   /* If the block is one fragment of a non-contiguous block, do not
15263      process the variables, since they will have been done by the
15264      origin block.  Do process subblocks.  */
15265   if (BLOCK_FRAGMENT_ORIGIN (stmt))
15266     {
15267       tree sub;
15268
15269       for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
15270         gen_block_die (sub, context_die, depth + 1);
15271
15272       return;
15273     }
15274
15275   /* Determine if we need to output any Dwarf DIEs at all to represent this
15276      block.  */
15277   if (inlined_func)
15278     /* The outer scopes for inlinings *must* always be represented.  We
15279        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
15280     must_output_die = 1;
15281   else
15282     {
15283       /* Determine if this block directly contains any "significant"
15284          local declarations which we will need to output DIEs for.  */
15285       if (debug_info_level > DINFO_LEVEL_TERSE)
15286         /* We are not in terse mode so *any* local declaration counts
15287            as being a "significant" one.  */
15288         must_output_die = ((BLOCK_VARS (stmt) != NULL
15289                             || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
15290                            && (TREE_USED (stmt)
15291                                || TREE_ASM_WRITTEN (stmt)
15292                                || BLOCK_ABSTRACT (stmt)));
15293       else if ((TREE_USED (stmt)
15294                 || TREE_ASM_WRITTEN (stmt)
15295                 || BLOCK_ABSTRACT (stmt))
15296                && !dwarf2out_ignore_block (stmt))
15297         must_output_die = 1;
15298     }
15299
15300   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
15301      DIE for any block which contains no significant local declarations at
15302      all.  Rather, in such cases we just call `decls_for_scope' so that any
15303      needed Dwarf info for any sub-blocks will get properly generated. Note
15304      that in terse mode, our definition of what constitutes a "significant"
15305      local declaration gets restricted to include only inlined function
15306      instances and local (nested) function definitions.  */
15307   if (must_output_die)
15308     {
15309       if (inlined_func)
15310         gen_inlined_subroutine_die (stmt, context_die, depth);
15311       else
15312         gen_lexical_block_die (stmt, context_die, depth);
15313     }
15314   else
15315     decls_for_scope (stmt, context_die, depth);
15316 }
15317
15318 /* Process variable DECL (or variable with origin ORIGIN) within
15319    block STMT and add it to CONTEXT_DIE.  */
15320 static void
15321 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
15322 {
15323   dw_die_ref die;
15324   tree decl_or_origin = decl ? decl : origin;
15325   tree ultimate_origin = origin ? decl_ultimate_origin (origin) : NULL;
15326
15327   if (ultimate_origin)
15328     origin = ultimate_origin;
15329
15330   if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
15331     die = lookup_decl_die (decl_or_origin);
15332   else if (TREE_CODE (decl_or_origin) == TYPE_DECL
15333            && TYPE_DECL_IS_STUB (decl_or_origin))
15334     die = lookup_type_die (TREE_TYPE (decl_or_origin));
15335   else
15336     die = NULL;
15337
15338   if (die != NULL && die->die_parent == NULL)
15339     add_child_die (context_die, die);
15340   else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
15341     dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
15342                                          stmt, context_die);
15343   else
15344     gen_decl_die (decl, origin, context_die);
15345 }
15346
15347 /* Generate all of the decls declared within a given scope and (recursively)
15348    all of its sub-blocks.  */
15349
15350 static void
15351 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
15352 {
15353   tree decl;
15354   unsigned int i;
15355   tree subblocks;
15356
15357   /* Ignore NULL blocks.  */
15358   if (stmt == NULL_TREE)
15359     return;
15360
15361   /* Output the DIEs to represent all of the data objects and typedefs
15362      declared directly within this block but not within any nested
15363      sub-blocks.  Also, nested function and tag DIEs have been
15364      generated with a parent of NULL; fix that up now.  */
15365   for (decl = BLOCK_VARS (stmt); decl != NULL; decl = TREE_CHAIN (decl))
15366     process_scope_var (stmt, decl, NULL_TREE, context_die);
15367   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
15368     process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
15369                        context_die);
15370
15371   /* If we're at -g1, we're not interested in subblocks.  */
15372   if (debug_info_level <= DINFO_LEVEL_TERSE)
15373     return;
15374
15375   /* Output the DIEs to represent all sub-blocks (and the items declared
15376      therein) of this block.  */
15377   for (subblocks = BLOCK_SUBBLOCKS (stmt);
15378        subblocks != NULL;
15379        subblocks = BLOCK_CHAIN (subblocks))
15380     gen_block_die (subblocks, context_die, depth + 1);
15381 }
15382
15383 /* Is this a typedef we can avoid emitting?  */
15384
15385 static inline int
15386 is_redundant_typedef (const_tree decl)
15387 {
15388   if (TYPE_DECL_IS_STUB (decl))
15389     return 1;
15390
15391   if (DECL_ARTIFICIAL (decl)
15392       && DECL_CONTEXT (decl)
15393       && is_tagged_type (DECL_CONTEXT (decl))
15394       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
15395       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
15396     /* Also ignore the artificial member typedef for the class name.  */
15397     return 1;
15398
15399   return 0;
15400 }
15401
15402 /* Returns the DIE for a context.  */
15403
15404 static inline dw_die_ref
15405 get_context_die (tree context)
15406 {
15407   if (context)
15408     {
15409       /* Find die that represents this context.  */
15410       if (TYPE_P (context))
15411         return force_type_die (context);
15412       else
15413         return force_decl_die (context);
15414     }
15415   return comp_unit_die;
15416 }
15417
15418 /* Returns the DIE for decl.  A DIE will always be returned.  */
15419
15420 static dw_die_ref
15421 force_decl_die (tree decl)
15422 {
15423   dw_die_ref decl_die;
15424   unsigned saved_external_flag;
15425   tree save_fn = NULL_TREE;
15426   decl_die = lookup_decl_die (decl);
15427   if (!decl_die)
15428     {
15429       dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
15430
15431       decl_die = lookup_decl_die (decl);
15432       if (decl_die)
15433         return decl_die;
15434
15435       switch (TREE_CODE (decl))
15436         {
15437         case FUNCTION_DECL:
15438           /* Clear current_function_decl, so that gen_subprogram_die thinks
15439              that this is a declaration. At this point, we just want to force
15440              declaration die.  */
15441           save_fn = current_function_decl;
15442           current_function_decl = NULL_TREE;
15443           gen_subprogram_die (decl, context_die);
15444           current_function_decl = save_fn;
15445           break;
15446
15447         case VAR_DECL:
15448           /* Set external flag to force declaration die. Restore it after
15449            gen_decl_die() call.  */
15450           saved_external_flag = DECL_EXTERNAL (decl);
15451           DECL_EXTERNAL (decl) = 1;
15452           gen_decl_die (decl, NULL, context_die);
15453           DECL_EXTERNAL (decl) = saved_external_flag;
15454           break;
15455
15456         case NAMESPACE_DECL:
15457           dwarf2out_decl (decl);
15458           break;
15459
15460         default:
15461           gcc_unreachable ();
15462         }
15463
15464       /* We should be able to find the DIE now.  */
15465       if (!decl_die)
15466         decl_die = lookup_decl_die (decl);
15467       gcc_assert (decl_die);
15468     }
15469
15470   return decl_die;
15471 }
15472
15473 /* Returns the DIE for TYPE, that must not be a base type.  A DIE is
15474    always returned.  */
15475
15476 static dw_die_ref
15477 force_type_die (tree type)
15478 {
15479   dw_die_ref type_die;
15480
15481   type_die = lookup_type_die (type);
15482   if (!type_die)
15483     {
15484       dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
15485
15486       type_die = modified_type_die (type, TYPE_READONLY (type),
15487                                     TYPE_VOLATILE (type), context_die);
15488       gcc_assert (type_die);
15489     }
15490   return type_die;
15491 }
15492
15493 /* Force out any required namespaces to be able to output DECL,
15494    and return the new context_die for it, if it's changed.  */
15495
15496 static dw_die_ref
15497 setup_namespace_context (tree thing, dw_die_ref context_die)
15498 {
15499   tree context = (DECL_P (thing)
15500                   ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
15501   if (context && TREE_CODE (context) == NAMESPACE_DECL)
15502     /* Force out the namespace.  */
15503     context_die = force_decl_die (context);
15504
15505   return context_die;
15506 }
15507
15508 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
15509    type) within its namespace, if appropriate.
15510
15511    For compatibility with older debuggers, namespace DIEs only contain
15512    declarations; all definitions are emitted at CU scope.  */
15513
15514 static dw_die_ref
15515 declare_in_namespace (tree thing, dw_die_ref context_die)
15516 {
15517   dw_die_ref ns_context;
15518
15519   if (debug_info_level <= DINFO_LEVEL_TERSE)
15520     return context_die;
15521
15522   /* If this decl is from an inlined function, then don't try to emit it in its
15523      namespace, as we will get confused.  It would have already been emitted
15524      when the abstract instance of the inline function was emitted anyways.  */
15525   if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
15526     return context_die;
15527
15528   ns_context = setup_namespace_context (thing, context_die);
15529
15530   if (ns_context != context_die)
15531     {
15532       if (is_fortran ())
15533         return ns_context;
15534       if (DECL_P (thing))
15535         gen_decl_die (thing, NULL, ns_context);
15536       else
15537         gen_type_die (thing, ns_context);
15538     }
15539   return context_die;
15540 }
15541
15542 /* Generate a DIE for a namespace or namespace alias.  */
15543
15544 static void
15545 gen_namespace_die (tree decl, dw_die_ref context_die)
15546 {
15547   dw_die_ref namespace_die;
15548
15549   /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
15550      they are an alias of.  */
15551   if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
15552     {
15553       /* Output a real namespace or module.  */
15554       context_die = setup_namespace_context (decl, comp_unit_die);
15555       namespace_die = new_die (is_fortran ()
15556                                ? DW_TAG_module : DW_TAG_namespace,
15557                                context_die, decl);
15558       /* For Fortran modules defined in different CU don't add src coords.  */
15559       if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
15560         add_name_attribute (namespace_die, dwarf2_name (decl, 0));
15561       else
15562         add_name_and_src_coords_attributes (namespace_die, decl);
15563       if (DECL_EXTERNAL (decl))
15564         add_AT_flag (namespace_die, DW_AT_declaration, 1);
15565       equate_decl_number_to_die (decl, namespace_die);
15566     }
15567   else
15568     {
15569       /* Output a namespace alias.  */
15570
15571       /* Force out the namespace we are an alias of, if necessary.  */
15572       dw_die_ref origin_die
15573         = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
15574
15575       if (DECL_CONTEXT (decl) == NULL_TREE
15576           || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
15577         context_die = setup_namespace_context (decl, comp_unit_die);
15578       /* Now create the namespace alias DIE.  */
15579       namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
15580       add_name_and_src_coords_attributes (namespace_die, decl);
15581       add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
15582       equate_decl_number_to_die (decl, namespace_die);
15583     }
15584 }
15585
15586 /* Generate Dwarf debug information for a decl described by DECL.  */
15587
15588 static void
15589 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
15590 {
15591   tree decl_or_origin = decl ? decl : origin;
15592   tree class_origin = NULL;
15593
15594   if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
15595     return;
15596
15597   switch (TREE_CODE (decl_or_origin))
15598     {
15599     case ERROR_MARK:
15600       break;
15601
15602     case CONST_DECL:
15603       if (!is_fortran ())
15604         {
15605           /* The individual enumerators of an enum type get output when we output
15606              the Dwarf representation of the relevant enum type itself.  */
15607           break;
15608         }
15609
15610       /* Emit its type.  */
15611       gen_type_die (TREE_TYPE (decl), context_die);
15612
15613       /* And its containing namespace.  */
15614       context_die = declare_in_namespace (decl, context_die);
15615
15616       gen_const_die (decl, context_die);
15617       break;
15618
15619     case FUNCTION_DECL:
15620       /* Don't output any DIEs to represent mere function declarations,
15621          unless they are class members or explicit block externs.  */
15622       if (DECL_INITIAL (decl_or_origin) == NULL_TREE
15623           && DECL_CONTEXT (decl_or_origin) == NULL_TREE
15624           && (current_function_decl == NULL_TREE
15625               || DECL_ARTIFICIAL (decl_or_origin)))
15626         break;
15627
15628 #if 0
15629       /* FIXME */
15630       /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
15631          on local redeclarations of global functions.  That seems broken.  */
15632       if (current_function_decl != decl)
15633         /* This is only a declaration.  */;
15634 #endif
15635
15636       /* If we're emitting a clone, emit info for the abstract instance.  */
15637       if (origin || DECL_ORIGIN (decl) != decl)
15638         dwarf2out_abstract_function (origin ? origin : DECL_ABSTRACT_ORIGIN (decl));
15639
15640       /* If we're emitting an out-of-line copy of an inline function,
15641          emit info for the abstract instance and set up to refer to it.  */
15642       else if (cgraph_function_possibly_inlined_p (decl)
15643                && ! DECL_ABSTRACT (decl)
15644                && ! class_or_namespace_scope_p (context_die)
15645                /* dwarf2out_abstract_function won't emit a die if this is just
15646                   a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
15647                   that case, because that works only if we have a die.  */
15648                && DECL_INITIAL (decl) != NULL_TREE)
15649         {
15650           dwarf2out_abstract_function (decl);
15651           set_decl_origin_self (decl);
15652         }
15653
15654       /* Otherwise we're emitting the primary DIE for this decl.  */
15655       else if (debug_info_level > DINFO_LEVEL_TERSE)
15656         {
15657           /* Before we describe the FUNCTION_DECL itself, make sure that we
15658              have described its return type.  */
15659           gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
15660
15661           /* And its virtual context.  */
15662           if (DECL_VINDEX (decl) != NULL_TREE)
15663             gen_type_die (DECL_CONTEXT (decl), context_die);
15664
15665           /* And its containing type.  */
15666           if (!origin)
15667             origin = decl_class_context (decl);
15668           if (origin != NULL_TREE)
15669             gen_type_die_for_member (origin, decl, context_die);
15670
15671           /* And its containing namespace.  */
15672           context_die = declare_in_namespace (decl, context_die);
15673         }
15674
15675       /* Now output a DIE to represent the function itself.  */
15676       if (decl)
15677         gen_subprogram_die (decl, context_die);
15678       break;
15679
15680     case TYPE_DECL:
15681       /* If we are in terse mode, don't generate any DIEs to represent any
15682          actual typedefs.  */
15683       if (debug_info_level <= DINFO_LEVEL_TERSE)
15684         break;
15685
15686       /* In the special case of a TYPE_DECL node representing the declaration
15687          of some type tag, if the given TYPE_DECL is marked as having been
15688          instantiated from some other (original) TYPE_DECL node (e.g. one which
15689          was generated within the original definition of an inline function) we
15690          used to generate a special (abbreviated) DW_TAG_structure_type,
15691          DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  But nothing
15692          should be actually referencing those DIEs, as variable DIEs with that
15693          type would be emitted already in the abstract origin, so it was always
15694          removed during unused type prunning.  Don't add anything in this
15695          case.  */
15696       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
15697         break;
15698
15699       if (is_redundant_typedef (decl))
15700         gen_type_die (TREE_TYPE (decl), context_die);
15701       else
15702         /* Output a DIE to represent the typedef itself.  */
15703         gen_typedef_die (decl, context_die);
15704       break;
15705
15706     case LABEL_DECL:
15707       if (debug_info_level >= DINFO_LEVEL_NORMAL)
15708         gen_label_die (decl, context_die);
15709       break;
15710
15711     case VAR_DECL:
15712     case RESULT_DECL:
15713       /* If we are in terse mode, don't generate any DIEs to represent any
15714          variable declarations or definitions.  */
15715       if (debug_info_level <= DINFO_LEVEL_TERSE)
15716         break;
15717
15718       /* Output any DIEs that are needed to specify the type of this data
15719          object.  */
15720       if ((TREE_CODE (decl_or_origin) == RESULT_DECL
15721            || TREE_CODE (decl_or_origin) == VAR_DECL)
15722           && DECL_BY_REFERENCE (decl_or_origin))
15723         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
15724       else
15725         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
15726
15727       /* And its containing type.  */
15728       class_origin = decl_class_context (decl_or_origin);
15729       if (class_origin != NULL_TREE)
15730         gen_type_die_for_member (class_origin, decl_or_origin, context_die);
15731
15732       /* And its containing namespace.  */
15733       context_die = declare_in_namespace (decl_or_origin, context_die);
15734
15735       /* Now output the DIE to represent the data object itself.  This gets
15736          complicated because of the possibility that the VAR_DECL really
15737          represents an inlined instance of a formal parameter for an inline
15738          function.  */
15739       if (!origin)
15740         origin = decl_ultimate_origin (decl);
15741       if (origin != NULL_TREE && TREE_CODE (origin) == PARM_DECL)
15742         gen_formal_parameter_die (decl, origin, context_die);
15743       else
15744         gen_variable_die (decl, origin, context_die);
15745       break;
15746
15747     case FIELD_DECL:
15748       /* Ignore the nameless fields that are used to skip bits but handle C++
15749          anonymous unions and structs.  */
15750       if (DECL_NAME (decl) != NULL_TREE
15751           || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
15752           || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
15753         {
15754           gen_type_die (member_declared_type (decl), context_die);
15755           gen_field_die (decl, context_die);
15756         }
15757       break;
15758
15759     case PARM_DECL:
15760       if (DECL_BY_REFERENCE (decl_or_origin))
15761         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
15762       else
15763         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
15764       gen_formal_parameter_die (decl, origin, context_die);
15765       break;
15766
15767     case NAMESPACE_DECL:
15768     case IMPORTED_DECL:
15769       gen_namespace_die (decl, context_die);
15770       break;
15771
15772     default:
15773       /* Probably some frontend-internal decl.  Assume we don't care.  */
15774       gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
15775       break;
15776     }
15777 }
15778 \f
15779 /* Output debug information for global decl DECL.  Called from toplev.c after
15780    compilation proper has finished.  */
15781
15782 static void
15783 dwarf2out_global_decl (tree decl)
15784 {
15785   /* Output DWARF2 information for file-scope tentative data object
15786      declarations, file-scope (extern) function declarations (which
15787      had no corresponding body) and file-scope tagged type declarations
15788      and definitions which have not yet been forced out.  */
15789   if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
15790     dwarf2out_decl (decl);
15791 }
15792
15793 /* Output debug information for type decl DECL.  Called from toplev.c
15794    and from language front ends (to record built-in types).  */
15795 static void
15796 dwarf2out_type_decl (tree decl, int local)
15797 {
15798   if (!local)
15799     dwarf2out_decl (decl);
15800 }
15801
15802 /* Output debug information for imported module or decl DECL.
15803    NAME is non-NULL name in the lexical block if the decl has been renamed.
15804    LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
15805    that DECL belongs to.
15806    LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK.  */
15807 static void
15808 dwarf2out_imported_module_or_decl_1 (tree decl,
15809                                      tree name,
15810                                      tree lexical_block,
15811                                      dw_die_ref lexical_block_die)
15812 {
15813   expanded_location xloc;
15814   dw_die_ref imported_die = NULL;
15815   dw_die_ref at_import_die;
15816
15817   if (TREE_CODE (decl) == IMPORTED_DECL)
15818     {
15819       xloc = expand_location (DECL_SOURCE_LOCATION (decl));
15820       decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
15821       gcc_assert (decl);
15822     }
15823   else
15824     xloc = expand_location (input_location);
15825
15826   if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
15827     {
15828       if (is_base_type (TREE_TYPE (decl)))
15829         at_import_die = base_type_die (TREE_TYPE (decl));
15830       else
15831         at_import_die = force_type_die (TREE_TYPE (decl));
15832       /* For namespace N { typedef void T; } using N::T; base_type_die
15833          returns NULL, but DW_TAG_imported_declaration requires
15834          the DW_AT_import tag.  Force creation of DW_TAG_typedef.  */
15835       if (!at_import_die)
15836         {
15837           gcc_assert (TREE_CODE (decl) == TYPE_DECL);
15838           gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
15839           at_import_die = lookup_type_die (TREE_TYPE (decl));
15840           gcc_assert (at_import_die);
15841         }
15842     }
15843   else
15844     {
15845       at_import_die = lookup_decl_die (decl);
15846       if (!at_import_die)
15847         {
15848           /* If we're trying to avoid duplicate debug info, we may not have
15849              emitted the member decl for this field.  Emit it now.  */
15850           if (TREE_CODE (decl) == FIELD_DECL)
15851             {
15852               tree type = DECL_CONTEXT (decl);
15853
15854               if (TYPE_CONTEXT (type)
15855                   && TYPE_P (TYPE_CONTEXT (type))
15856                   && !should_emit_struct_debug (TYPE_CONTEXT (type),
15857                                                 DINFO_USAGE_DIR_USE))
15858                 return;
15859               gen_type_die_for_member (type, decl,
15860                                        get_context_die (TYPE_CONTEXT (type)));
15861             }
15862           at_import_die = force_decl_die (decl);
15863         }
15864     }
15865
15866   if (TREE_CODE (decl) == NAMESPACE_DECL)
15867     imported_die = new_die (DW_TAG_imported_module,
15868                             lexical_block_die,
15869                             lexical_block);
15870   else
15871     imported_die = new_die (DW_TAG_imported_declaration,
15872                             lexical_block_die,
15873                             lexical_block);
15874
15875   add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
15876   add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
15877   if (name)
15878     add_AT_string (imported_die, DW_AT_name,
15879                    IDENTIFIER_POINTER (name));
15880   add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
15881 }
15882
15883 /* Output debug information for imported module or decl DECL.
15884    NAME is non-NULL name in context if the decl has been renamed.
15885    CHILD is true if decl is one of the renamed decls as part of
15886    importing whole module.  */
15887
15888 static void
15889 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
15890                                    bool child)
15891 {
15892   /* dw_die_ref at_import_die;  */
15893   dw_die_ref scope_die;
15894
15895   if (debug_info_level <= DINFO_LEVEL_TERSE)
15896     return;
15897
15898   gcc_assert (decl);
15899
15900   /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
15901      We need decl DIE for reference and scope die. First, get DIE for the decl
15902      itself.  */
15903
15904   /* Get the scope die for decl context. Use comp_unit_die for global module
15905      or decl. If die is not found for non globals, force new die.  */
15906   if (context
15907       && TYPE_P (context)
15908       && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
15909     return;
15910   scope_die = get_context_die (context);
15911
15912   if (child)
15913     {
15914       gcc_assert (scope_die->die_child);
15915       gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
15916       gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
15917       scope_die = scope_die->die_child;
15918     }
15919
15920   /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
15921   dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
15922
15923 }
15924
15925 /* Write the debugging output for DECL.  */
15926
15927 void
15928 dwarf2out_decl (tree decl)
15929 {
15930   dw_die_ref context_die = comp_unit_die;
15931
15932   switch (TREE_CODE (decl))
15933     {
15934     case ERROR_MARK:
15935       return;
15936
15937     case FUNCTION_DECL:
15938       /* What we would really like to do here is to filter out all mere
15939          file-scope declarations of file-scope functions which are never
15940          referenced later within this translation unit (and keep all of ones
15941          that *are* referenced later on) but we aren't clairvoyant, so we have
15942          no idea which functions will be referenced in the future (i.e. later
15943          on within the current translation unit). So here we just ignore all
15944          file-scope function declarations which are not also definitions.  If
15945          and when the debugger needs to know something about these functions,
15946          it will have to hunt around and find the DWARF information associated
15947          with the definition of the function.
15948
15949          We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
15950          nodes represent definitions and which ones represent mere
15951          declarations.  We have to check DECL_INITIAL instead. That's because
15952          the C front-end supports some weird semantics for "extern inline"
15953          function definitions.  These can get inlined within the current
15954          translation unit (and thus, we need to generate Dwarf info for their
15955          abstract instances so that the Dwarf info for the concrete inlined
15956          instances can have something to refer to) but the compiler never
15957          generates any out-of-lines instances of such things (despite the fact
15958          that they *are* definitions).
15959
15960          The important point is that the C front-end marks these "extern
15961          inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
15962          them anyway. Note that the C++ front-end also plays some similar games
15963          for inline function definitions appearing within include files which
15964          also contain `#pragma interface' pragmas.  */
15965       if (DECL_INITIAL (decl) == NULL_TREE)
15966         return;
15967
15968       /* If we're a nested function, initially use a parent of NULL; if we're
15969          a plain function, this will be fixed up in decls_for_scope.  If
15970          we're a method, it will be ignored, since we already have a DIE.  */
15971       if (decl_function_context (decl)
15972           /* But if we're in terse mode, we don't care about scope.  */
15973           && debug_info_level > DINFO_LEVEL_TERSE)
15974         context_die = NULL;
15975       break;
15976
15977     case VAR_DECL:
15978       /* Ignore this VAR_DECL if it refers to a file-scope extern data object
15979          declaration and if the declaration was never even referenced from
15980          within this entire compilation unit.  We suppress these DIEs in
15981          order to save space in the .debug section (by eliminating entries
15982          which are probably useless).  Note that we must not suppress
15983          block-local extern declarations (whether used or not) because that
15984          would screw-up the debugger's name lookup mechanism and cause it to
15985          miss things which really ought to be in scope at a given point.  */
15986       if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
15987         return;
15988
15989       /* For local statics lookup proper context die.  */
15990       if (TREE_STATIC (decl) && decl_function_context (decl))
15991         context_die = lookup_decl_die (DECL_CONTEXT (decl));
15992
15993       /* If we are in terse mode, don't generate any DIEs to represent any
15994          variable declarations or definitions.  */
15995       if (debug_info_level <= DINFO_LEVEL_TERSE)
15996         return;
15997       break;
15998
15999     case CONST_DECL:
16000       if (debug_info_level <= DINFO_LEVEL_TERSE)
16001         return;
16002       if (!is_fortran ())
16003         return;
16004       if (TREE_STATIC (decl) && decl_function_context (decl))
16005         context_die = lookup_decl_die (DECL_CONTEXT (decl));
16006       break;
16007
16008     case NAMESPACE_DECL:
16009     case IMPORTED_DECL:
16010       if (debug_info_level <= DINFO_LEVEL_TERSE)
16011         return;
16012       if (lookup_decl_die (decl) != NULL)
16013         return;
16014       break;
16015
16016     case TYPE_DECL:
16017       /* Don't emit stubs for types unless they are needed by other DIEs.  */
16018       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
16019         return;
16020
16021       /* Don't bother trying to generate any DIEs to represent any of the
16022          normal built-in types for the language we are compiling.  */
16023       if (DECL_IS_BUILTIN (decl))
16024         {
16025           /* OK, we need to generate one for `bool' so GDB knows what type
16026              comparisons have.  */
16027           if (is_cxx ()
16028               && TREE_CODE (TREE_TYPE (decl)) == BOOLEAN_TYPE
16029               && ! DECL_IGNORED_P (decl))
16030             modified_type_die (TREE_TYPE (decl), 0, 0, NULL);
16031
16032           return;
16033         }
16034
16035       /* If we are in terse mode, don't generate any DIEs for types.  */
16036       if (debug_info_level <= DINFO_LEVEL_TERSE)
16037         return;
16038
16039       /* If we're a function-scope tag, initially use a parent of NULL;
16040          this will be fixed up in decls_for_scope.  */
16041       if (decl_function_context (decl))
16042         context_die = NULL;
16043
16044       break;
16045
16046     default:
16047       return;
16048     }
16049
16050   gen_decl_die (decl, NULL, context_die);
16051 }
16052
16053 /* Output a marker (i.e. a label) for the beginning of the generated code for
16054    a lexical block.  */
16055
16056 static void
16057 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
16058                        unsigned int blocknum)
16059 {
16060   switch_to_section (current_function_section ());
16061   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
16062 }
16063
16064 /* Output a marker (i.e. a label) for the end of the generated code for a
16065    lexical block.  */
16066
16067 static void
16068 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
16069 {
16070   switch_to_section (current_function_section ());
16071   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
16072 }
16073
16074 /* Returns nonzero if it is appropriate not to emit any debugging
16075    information for BLOCK, because it doesn't contain any instructions.
16076
16077    Don't allow this for blocks with nested functions or local classes
16078    as we would end up with orphans, and in the presence of scheduling
16079    we may end up calling them anyway.  */
16080
16081 static bool
16082 dwarf2out_ignore_block (const_tree block)
16083 {
16084   tree decl;
16085   unsigned int i;
16086
16087   for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
16088     if (TREE_CODE (decl) == FUNCTION_DECL
16089         || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
16090       return 0;
16091   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
16092     {
16093       decl = BLOCK_NONLOCALIZED_VAR (block, i);
16094       if (TREE_CODE (decl) == FUNCTION_DECL
16095           || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
16096       return 0;
16097     }
16098
16099   return 1;
16100 }
16101
16102 /* Hash table routines for file_hash.  */
16103
16104 static int
16105 file_table_eq (const void *p1_p, const void *p2_p)
16106 {
16107   const struct dwarf_file_data *const p1 =
16108     (const struct dwarf_file_data *) p1_p;
16109   const char *const p2 = (const char *) p2_p;
16110   return strcmp (p1->filename, p2) == 0;
16111 }
16112
16113 static hashval_t
16114 file_table_hash (const void *p_p)
16115 {
16116   const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
16117   return htab_hash_string (p->filename);
16118 }
16119
16120 /* Lookup FILE_NAME (in the list of filenames that we know about here in
16121    dwarf2out.c) and return its "index".  The index of each (known) filename is
16122    just a unique number which is associated with only that one filename.  We
16123    need such numbers for the sake of generating labels (in the .debug_sfnames
16124    section) and references to those files numbers (in the .debug_srcinfo
16125    and.debug_macinfo sections).  If the filename given as an argument is not
16126    found in our current list, add it to the list and assign it the next
16127    available unique index number.  In order to speed up searches, we remember
16128    the index of the filename was looked up last.  This handles the majority of
16129    all searches.  */
16130
16131 static struct dwarf_file_data *
16132 lookup_filename (const char *file_name)
16133 {
16134   void ** slot;
16135   struct dwarf_file_data * created;
16136
16137   /* Check to see if the file name that was searched on the previous
16138      call matches this file name.  If so, return the index.  */
16139   if (file_table_last_lookup
16140       && (file_name == file_table_last_lookup->filename
16141           || strcmp (file_table_last_lookup->filename, file_name) == 0))
16142     return file_table_last_lookup;
16143
16144   /* Didn't match the previous lookup, search the table.  */
16145   slot = htab_find_slot_with_hash (file_table, file_name,
16146                                    htab_hash_string (file_name), INSERT);
16147   if (*slot)
16148     return (struct dwarf_file_data *) *slot;
16149
16150   created = GGC_NEW (struct dwarf_file_data);
16151   created->filename = file_name;
16152   created->emitted_number = 0;
16153   *slot = created;
16154   return created;
16155 }
16156
16157 /* If the assembler will construct the file table, then translate the compiler
16158    internal file table number into the assembler file table number, and emit
16159    a .file directive if we haven't already emitted one yet.  The file table
16160    numbers are different because we prune debug info for unused variables and
16161    types, which may include filenames.  */
16162
16163 static int
16164 maybe_emit_file (struct dwarf_file_data * fd)
16165 {
16166   if (! fd->emitted_number)
16167     {
16168       if (last_emitted_file)
16169         fd->emitted_number = last_emitted_file->emitted_number + 1;
16170       else
16171         fd->emitted_number = 1;
16172       last_emitted_file = fd;
16173
16174       if (DWARF2_ASM_LINE_DEBUG_INFO)
16175         {
16176           fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
16177           output_quoted_string (asm_out_file,
16178                                 remap_debug_filename (fd->filename));
16179           fputc ('\n', asm_out_file);
16180         }
16181     }
16182
16183   return fd->emitted_number;
16184 }
16185
16186 /* Replace DW_AT_name for the decl with name.  */
16187  
16188 static void
16189 dwarf2out_set_name (tree decl, tree name)
16190 {
16191   dw_die_ref die;
16192   dw_attr_ref attr;
16193
16194   die = TYPE_SYMTAB_DIE (decl);
16195   if (!die)
16196     return;
16197
16198   attr = get_AT (die, DW_AT_name);
16199   if (attr)
16200     {
16201       struct indirect_string_node *node;
16202
16203       node = find_AT_string (dwarf2_name (name, 0));
16204       /* replace the string.  */
16205       attr->dw_attr_val.v.val_str = node;
16206     }
16207
16208   else
16209     add_name_attribute (die, dwarf2_name (name, 0));
16210 }
16211
16212 /* Called by the final INSN scan whenever we see a var location.  We
16213    use it to drop labels in the right places, and throw the location in
16214    our lookup table.  */
16215
16216 static void
16217 dwarf2out_var_location (rtx loc_note)
16218 {
16219   char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
16220   struct var_loc_node *newloc;
16221   rtx next_real;
16222   static const char *last_label;
16223   static bool last_in_cold_section_p;
16224   tree decl;
16225
16226   if (!DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
16227     return;
16228
16229   next_real = next_real_insn (loc_note);
16230   /* If there are no instructions which would be affected by this note,
16231      don't do anything.  */
16232   if (next_real == NULL_RTX)
16233     return;
16234
16235   newloc = GGC_CNEW (struct var_loc_node);
16236   /* If there were no real insns between note we processed last time
16237      and this note, use the label we emitted last time.  */
16238   if (last_var_location_insn != NULL_RTX
16239       && last_var_location_insn == next_real
16240       && last_in_cold_section_p == in_cold_section_p)
16241     newloc->label = last_label;
16242   else
16243     {
16244       ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
16245       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
16246       loclabel_num++;
16247       newloc->label = ggc_strdup (loclabel);
16248     }
16249   newloc->var_loc_note = loc_note;
16250   newloc->next = NULL;
16251
16252   if (cfun && in_cold_section_p)
16253     newloc->section_label = crtl->subsections.cold_section_label;
16254   else
16255     newloc->section_label = text_section_label;
16256
16257   last_var_location_insn = next_real;
16258   last_label = newloc->label;
16259   last_in_cold_section_p = in_cold_section_p;
16260   decl = NOTE_VAR_LOCATION_DECL (loc_note);
16261   add_var_loc_to_decl (decl, newloc);
16262 }
16263
16264 /* We need to reset the locations at the beginning of each
16265    function. We can't do this in the end_function hook, because the
16266    declarations that use the locations won't have been output when
16267    that hook is called.  Also compute have_multiple_function_sections here.  */
16268
16269 static void
16270 dwarf2out_begin_function (tree fun)
16271 {
16272   htab_empty (decl_loc_table);
16273
16274   if (function_section (fun) != text_section)
16275     have_multiple_function_sections = true;
16276
16277   dwarf2out_note_section_used ();
16278 }
16279
16280 /* Output a label to mark the beginning of a source code line entry
16281    and record information relating to this source line, in
16282    'line_info_table' for later output of the .debug_line section.  */
16283
16284 static void
16285 dwarf2out_source_line (unsigned int line, const char *filename,
16286                        int discriminator, bool is_stmt)
16287 {
16288   static bool last_is_stmt = true;
16289
16290   if (debug_info_level >= DINFO_LEVEL_NORMAL
16291       && line != 0)
16292     {
16293       int file_num = maybe_emit_file (lookup_filename (filename));
16294
16295       switch_to_section (current_function_section ());
16296
16297       /* If requested, emit something human-readable.  */
16298       if (flag_debug_asm)
16299         fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
16300                  filename, line);
16301
16302       if (DWARF2_ASM_LINE_DEBUG_INFO)
16303         {
16304           /* Emit the .loc directive understood by GNU as.  */
16305           fprintf (asm_out_file, "\t.loc %d %d 0", file_num, line);
16306           if (is_stmt != last_is_stmt)
16307             {
16308               fprintf (asm_out_file, " is_stmt %d", is_stmt ? 1 : 0);
16309               last_is_stmt = is_stmt;
16310             }
16311           if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
16312             fprintf (asm_out_file, " discriminator %d", discriminator);
16313           fputc ('\n', asm_out_file);
16314
16315           /* Indicate that line number info exists.  */
16316           line_info_table_in_use++;
16317         }
16318       else if (function_section (current_function_decl) != text_section)
16319         {
16320           dw_separate_line_info_ref line_info;
16321           targetm.asm_out.internal_label (asm_out_file,
16322                                           SEPARATE_LINE_CODE_LABEL,
16323                                           separate_line_info_table_in_use);
16324
16325           /* Expand the line info table if necessary.  */
16326           if (separate_line_info_table_in_use
16327               == separate_line_info_table_allocated)
16328             {
16329               separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
16330               separate_line_info_table
16331                 = GGC_RESIZEVEC (dw_separate_line_info_entry,
16332                                  separate_line_info_table,
16333                                  separate_line_info_table_allocated);
16334               memset (separate_line_info_table
16335                        + separate_line_info_table_in_use,
16336                       0,
16337                       (LINE_INFO_TABLE_INCREMENT
16338                        * sizeof (dw_separate_line_info_entry)));
16339             }
16340
16341           /* Add the new entry at the end of the line_info_table.  */
16342           line_info
16343             = &separate_line_info_table[separate_line_info_table_in_use++];
16344           line_info->dw_file_num = file_num;
16345           line_info->dw_line_num = line;
16346           line_info->function = current_function_funcdef_no;
16347         }
16348       else
16349         {
16350           dw_line_info_ref line_info;
16351
16352           targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL,
16353                                      line_info_table_in_use);
16354
16355           /* Expand the line info table if necessary.  */
16356           if (line_info_table_in_use == line_info_table_allocated)
16357             {
16358               line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
16359               line_info_table
16360                 = GGC_RESIZEVEC (dw_line_info_entry, line_info_table,
16361                                  line_info_table_allocated);
16362               memset (line_info_table + line_info_table_in_use, 0,
16363                       LINE_INFO_TABLE_INCREMENT * sizeof (dw_line_info_entry));
16364             }
16365
16366           /* Add the new entry at the end of the line_info_table.  */
16367           line_info = &line_info_table[line_info_table_in_use++];
16368           line_info->dw_file_num = file_num;
16369           line_info->dw_line_num = line;
16370         }
16371     }
16372 }
16373
16374 /* Record the beginning of a new source file.  */
16375
16376 static void
16377 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
16378 {
16379   if (flag_eliminate_dwarf2_dups)
16380     {
16381       /* Record the beginning of the file for break_out_includes.  */
16382       dw_die_ref bincl_die;
16383
16384       bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die, NULL);
16385       add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
16386     }
16387
16388   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
16389     {
16390       int file_num = maybe_emit_file (lookup_filename (filename));
16391
16392       switch_to_section (debug_macinfo_section);
16393       dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
16394       dw2_asm_output_data_uleb128 (lineno, "Included from line number %d",
16395                                    lineno);
16396
16397       dw2_asm_output_data_uleb128 (file_num, "file %s", filename);
16398     }
16399 }
16400
16401 /* Record the end of a source file.  */
16402
16403 static void
16404 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
16405 {
16406   if (flag_eliminate_dwarf2_dups)
16407     /* Record the end of the file for break_out_includes.  */
16408     new_die (DW_TAG_GNU_EINCL, comp_unit_die, NULL);
16409
16410   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
16411     {
16412       switch_to_section (debug_macinfo_section);
16413       dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
16414     }
16415 }
16416
16417 /* Called from debug_define in toplev.c.  The `buffer' parameter contains
16418    the tail part of the directive line, i.e. the part which is past the
16419    initial whitespace, #, whitespace, directive-name, whitespace part.  */
16420
16421 static void
16422 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
16423                   const char *buffer ATTRIBUTE_UNUSED)
16424 {
16425   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
16426     {
16427       switch_to_section (debug_macinfo_section);
16428       dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
16429       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
16430       dw2_asm_output_nstring (buffer, -1, "The macro");
16431     }
16432 }
16433
16434 /* Called from debug_undef in toplev.c.  The `buffer' parameter contains
16435    the tail part of the directive line, i.e. the part which is past the
16436    initial whitespace, #, whitespace, directive-name, whitespace part.  */
16437
16438 static void
16439 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
16440                  const char *buffer ATTRIBUTE_UNUSED)
16441 {
16442   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
16443     {
16444       switch_to_section (debug_macinfo_section);
16445       dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
16446       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
16447       dw2_asm_output_nstring (buffer, -1, "The macro");
16448     }
16449 }
16450
16451 /* Set up for Dwarf output at the start of compilation.  */
16452
16453 static void
16454 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
16455 {
16456   /* Allocate the file_table.  */
16457   file_table = htab_create_ggc (50, file_table_hash,
16458                                 file_table_eq, NULL);
16459
16460   /* Allocate the decl_die_table.  */
16461   decl_die_table = htab_create_ggc (10, decl_die_table_hash,
16462                                     decl_die_table_eq, NULL);
16463
16464   /* Allocate the decl_loc_table.  */
16465   decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
16466                                     decl_loc_table_eq, NULL);
16467
16468   /* Allocate the initial hunk of the decl_scope_table.  */
16469   decl_scope_table = VEC_alloc (tree, gc, 256);
16470
16471   /* Allocate the initial hunk of the abbrev_die_table.  */
16472   abbrev_die_table = GGC_CNEWVEC (dw_die_ref, ABBREV_DIE_TABLE_INCREMENT);
16473   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
16474   /* Zero-th entry is allocated, but unused.  */
16475   abbrev_die_table_in_use = 1;
16476
16477   /* Allocate the initial hunk of the line_info_table.  */
16478   line_info_table = GGC_CNEWVEC (dw_line_info_entry, LINE_INFO_TABLE_INCREMENT);
16479   line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
16480
16481   /* Zero-th entry is allocated, but unused.  */
16482   line_info_table_in_use = 1;
16483
16484   /* Allocate the pubtypes and pubnames vectors.  */
16485   pubname_table = VEC_alloc (pubname_entry, gc, 32);
16486   pubtype_table = VEC_alloc (pubname_entry, gc, 32);
16487
16488   /* Generate the initial DIE for the .debug section.  Note that the (string)
16489      value given in the DW_AT_name attribute of the DW_TAG_compile_unit DIE
16490      will (typically) be a relative pathname and that this pathname should be
16491      taken as being relative to the directory from which the compiler was
16492      invoked when the given (base) source file was compiled.  We will fill
16493      in this value in dwarf2out_finish.  */
16494   comp_unit_die = gen_compile_unit_die (NULL);
16495
16496   incomplete_types = VEC_alloc (tree, gc, 64);
16497
16498   used_rtx_array = VEC_alloc (rtx, gc, 32);
16499
16500   debug_info_section = get_section (DEBUG_INFO_SECTION,
16501                                     SECTION_DEBUG, NULL);
16502   debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
16503                                       SECTION_DEBUG, NULL);
16504   debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
16505                                        SECTION_DEBUG, NULL);
16506   debug_macinfo_section = get_section (DEBUG_MACINFO_SECTION,
16507                                        SECTION_DEBUG, NULL);
16508   debug_line_section = get_section (DEBUG_LINE_SECTION,
16509                                     SECTION_DEBUG, NULL);
16510   debug_loc_section = get_section (DEBUG_LOC_SECTION,
16511                                    SECTION_DEBUG, NULL);
16512   debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
16513                                         SECTION_DEBUG, NULL);
16514   debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
16515                                         SECTION_DEBUG, NULL);
16516   debug_str_section = get_section (DEBUG_STR_SECTION,
16517                                    DEBUG_STR_SECTION_FLAGS, NULL);
16518   debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
16519                                       SECTION_DEBUG, NULL);
16520   debug_frame_section = get_section (DEBUG_FRAME_SECTION,
16521                                      SECTION_DEBUG, NULL);
16522
16523   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
16524   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
16525                                DEBUG_ABBREV_SECTION_LABEL, 0);
16526   ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
16527   ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
16528                                COLD_TEXT_SECTION_LABEL, 0);
16529   ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
16530
16531   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
16532                                DEBUG_INFO_SECTION_LABEL, 0);
16533   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
16534                                DEBUG_LINE_SECTION_LABEL, 0);
16535   ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
16536                                DEBUG_RANGES_SECTION_LABEL, 0);
16537   switch_to_section (debug_abbrev_section);
16538   ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
16539   switch_to_section (debug_info_section);
16540   ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
16541   switch_to_section (debug_line_section);
16542   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
16543
16544   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
16545     {
16546       switch_to_section (debug_macinfo_section);
16547       ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
16548                                    DEBUG_MACINFO_SECTION_LABEL, 0);
16549       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
16550     }
16551
16552   switch_to_section (text_section);
16553   ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
16554   if (flag_reorder_blocks_and_partition)
16555     {
16556       cold_text_section = unlikely_text_section ();
16557       switch_to_section (cold_text_section);
16558       ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
16559     }
16560 }
16561
16562 /* A helper function for dwarf2out_finish called through
16563    ht_forall.  Emit one queued .debug_str string.  */
16564
16565 static int
16566 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
16567 {
16568   struct indirect_string_node *node = (struct indirect_string_node *) *h;
16569
16570   if (node->form == DW_FORM_strp)
16571     {
16572       switch_to_section (debug_str_section);
16573       ASM_OUTPUT_LABEL (asm_out_file, node->label);
16574       assemble_string (node->str, strlen (node->str) + 1);
16575     }
16576
16577   return 1;
16578 }
16579
16580 #if ENABLE_ASSERT_CHECKING
16581 /* Verify that all marks are clear.  */
16582
16583 static void
16584 verify_marks_clear (dw_die_ref die)
16585 {
16586   dw_die_ref c;
16587
16588   gcc_assert (! die->die_mark);
16589   FOR_EACH_CHILD (die, c, verify_marks_clear (c));
16590 }
16591 #endif /* ENABLE_ASSERT_CHECKING */
16592
16593 /* Clear the marks for a die and its children.
16594    Be cool if the mark isn't set.  */
16595
16596 static void
16597 prune_unmark_dies (dw_die_ref die)
16598 {
16599   dw_die_ref c;
16600
16601   if (die->die_mark)
16602     die->die_mark = 0;
16603   FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
16604 }
16605
16606 /* Given DIE that we're marking as used, find any other dies
16607    it references as attributes and mark them as used.  */
16608
16609 static void
16610 prune_unused_types_walk_attribs (dw_die_ref die)
16611 {
16612   dw_attr_ref a;
16613   unsigned ix;
16614
16615   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
16616     {
16617       if (a->dw_attr_val.val_class == dw_val_class_die_ref)
16618         {
16619           /* A reference to another DIE.
16620              Make sure that it will get emitted.  */
16621           prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
16622         }
16623       /* Set the string's refcount to 0 so that prune_unused_types_mark
16624          accounts properly for it.  */
16625       if (AT_class (a) == dw_val_class_str)
16626         a->dw_attr_val.v.val_str->refcount = 0;
16627     }
16628 }
16629
16630
16631 /* Mark DIE as being used.  If DOKIDS is true, then walk down
16632    to DIE's children.  */
16633
16634 static void
16635 prune_unused_types_mark (dw_die_ref die, int dokids)
16636 {
16637   dw_die_ref c;
16638
16639   if (die->die_mark == 0)
16640     {
16641       /* We haven't done this node yet.  Mark it as used.  */
16642       die->die_mark = 1;
16643
16644       /* We also have to mark its parents as used.
16645          (But we don't want to mark our parents' kids due to this.)  */
16646       if (die->die_parent)
16647         prune_unused_types_mark (die->die_parent, 0);
16648
16649       /* Mark any referenced nodes.  */
16650       prune_unused_types_walk_attribs (die);
16651
16652       /* If this node is a specification,
16653          also mark the definition, if it exists.  */
16654       if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
16655         prune_unused_types_mark (die->die_definition, 1);
16656     }
16657
16658   if (dokids && die->die_mark != 2)
16659     {
16660       /* We need to walk the children, but haven't done so yet.
16661          Remember that we've walked the kids.  */
16662       die->die_mark = 2;
16663
16664       /* If this is an array type, we need to make sure our
16665          kids get marked, even if they're types.  */
16666       if (die->die_tag == DW_TAG_array_type)
16667         FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
16668       else
16669         FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
16670     }
16671 }
16672
16673 /* For local classes, look if any static member functions were emitted
16674    and if so, mark them.  */
16675
16676 static void
16677 prune_unused_types_walk_local_classes (dw_die_ref die)
16678 {
16679   dw_die_ref c;
16680
16681   if (die->die_mark == 2)
16682     return;
16683
16684   switch (die->die_tag)
16685     {
16686     case DW_TAG_structure_type:
16687     case DW_TAG_union_type:
16688     case DW_TAG_class_type:
16689       break;
16690
16691     case DW_TAG_subprogram:
16692       if (!get_AT_flag (die, DW_AT_declaration)
16693           || die->die_definition != NULL)
16694         prune_unused_types_mark (die, 1);
16695       return;
16696
16697     default:
16698       return;
16699     }
16700
16701   /* Mark children.  */
16702   FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
16703 }
16704
16705 /* Walk the tree DIE and mark types that we actually use.  */
16706
16707 static void
16708 prune_unused_types_walk (dw_die_ref die)
16709 {
16710   dw_die_ref c;
16711
16712   /* Don't do anything if this node is already marked and
16713      children have been marked as well.  */
16714   if (die->die_mark == 2)
16715     return;
16716
16717   switch (die->die_tag)
16718     {
16719     case DW_TAG_structure_type:
16720     case DW_TAG_union_type:
16721     case DW_TAG_class_type:
16722       if (die->die_perennial_p)
16723         break;
16724
16725       for (c = die->die_parent; c; c = c->die_parent)
16726         if (c->die_tag == DW_TAG_subprogram)
16727           break;
16728
16729       /* Finding used static member functions inside of classes
16730          is needed just for local classes, because for other classes
16731          static member function DIEs with DW_AT_specification
16732          are emitted outside of the DW_TAG_*_type.  If we ever change
16733          it, we'd need to call this even for non-local classes.  */
16734       if (c)
16735         prune_unused_types_walk_local_classes (die);
16736
16737       /* It's a type node --- don't mark it.  */
16738       return;
16739
16740     case DW_TAG_const_type:
16741     case DW_TAG_packed_type:
16742     case DW_TAG_pointer_type:
16743     case DW_TAG_reference_type:
16744     case DW_TAG_volatile_type:
16745     case DW_TAG_typedef:
16746     case DW_TAG_array_type:
16747     case DW_TAG_interface_type:
16748     case DW_TAG_friend:
16749     case DW_TAG_variant_part:
16750     case DW_TAG_enumeration_type:
16751     case DW_TAG_subroutine_type:
16752     case DW_TAG_string_type:
16753     case DW_TAG_set_type:
16754     case DW_TAG_subrange_type:
16755     case DW_TAG_ptr_to_member_type:
16756     case DW_TAG_file_type:
16757       if (die->die_perennial_p)
16758         break;
16759
16760       /* It's a type node --- don't mark it.  */
16761       return;
16762
16763     default:
16764       /* Mark everything else.  */
16765       break;
16766   }
16767
16768   if (die->die_mark == 0)
16769     {
16770       die->die_mark = 1;
16771
16772       /* Now, mark any dies referenced from here.  */
16773       prune_unused_types_walk_attribs (die);
16774     }
16775
16776   die->die_mark = 2;
16777
16778   /* Mark children.  */
16779   FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
16780 }
16781
16782 /* Increment the string counts on strings referred to from DIE's
16783    attributes.  */
16784
16785 static void
16786 prune_unused_types_update_strings (dw_die_ref die)
16787 {
16788   dw_attr_ref a;
16789   unsigned ix;
16790
16791   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
16792     if (AT_class (a) == dw_val_class_str)
16793       {
16794         struct indirect_string_node *s = a->dw_attr_val.v.val_str;
16795         s->refcount++;
16796         /* Avoid unnecessarily putting strings that are used less than
16797            twice in the hash table.  */
16798         if (s->refcount
16799             == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
16800           {
16801             void ** slot;
16802             slot = htab_find_slot_with_hash (debug_str_hash, s->str,
16803                                              htab_hash_string (s->str),
16804                                              INSERT);
16805             gcc_assert (*slot == NULL);
16806             *slot = s;
16807           }
16808       }
16809 }
16810
16811 /* Remove from the tree DIE any dies that aren't marked.  */
16812
16813 static void
16814 prune_unused_types_prune (dw_die_ref die)
16815 {
16816   dw_die_ref c;
16817
16818   gcc_assert (die->die_mark);
16819   prune_unused_types_update_strings (die);
16820
16821   if (! die->die_child)
16822     return;
16823
16824   c = die->die_child;
16825   do {
16826     dw_die_ref prev = c;
16827     for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
16828       if (c == die->die_child)
16829         {
16830           /* No marked children between 'prev' and the end of the list.  */
16831           if (prev == c)
16832             /* No marked children at all.  */
16833             die->die_child = NULL;
16834           else
16835             {
16836               prev->die_sib = c->die_sib;
16837               die->die_child = prev;
16838             }
16839           return;
16840         }
16841
16842     if (c != prev->die_sib)
16843       prev->die_sib = c;
16844     prune_unused_types_prune (c);
16845   } while (c != die->die_child);
16846 }
16847
16848
16849 /* Remove dies representing declarations that we never use.  */
16850
16851 static void
16852 prune_unused_types (void)
16853 {
16854   unsigned int i;
16855   limbo_die_node *node;
16856   pubname_ref pub;
16857
16858 #if ENABLE_ASSERT_CHECKING
16859   /* All the marks should already be clear.  */
16860   verify_marks_clear (comp_unit_die);
16861   for (node = limbo_die_list; node; node = node->next)
16862     verify_marks_clear (node->die);
16863 #endif /* ENABLE_ASSERT_CHECKING */
16864
16865   /* Set the mark on nodes that are actually used.  */
16866   prune_unused_types_walk (comp_unit_die);
16867   for (node = limbo_die_list; node; node = node->next)
16868     prune_unused_types_walk (node->die);
16869
16870   /* Also set the mark on nodes referenced from the
16871      pubname_table or arange_table.  */
16872   for (i = 0; VEC_iterate (pubname_entry, pubname_table, i, pub); i++)
16873     prune_unused_types_mark (pub->die, 1);
16874   for (i = 0; i < arange_table_in_use; i++)
16875     prune_unused_types_mark (arange_table[i], 1);
16876
16877   /* Get rid of nodes that aren't marked; and update the string counts.  */
16878   if (debug_str_hash)
16879     htab_empty (debug_str_hash);
16880   prune_unused_types_prune (comp_unit_die);
16881   for (node = limbo_die_list; node; node = node->next)
16882     prune_unused_types_prune (node->die);
16883
16884   /* Leave the marks clear.  */
16885   prune_unmark_dies (comp_unit_die);
16886   for (node = limbo_die_list; node; node = node->next)
16887     prune_unmark_dies (node->die);
16888 }
16889
16890 /* Set the parameter to true if there are any relative pathnames in
16891    the file table.  */
16892 static int
16893 file_table_relative_p (void ** slot, void *param)
16894 {
16895   bool *p = (bool *) param;
16896   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
16897   if (!IS_ABSOLUTE_PATH (d->filename))
16898     {
16899       *p = true;
16900       return 0;
16901     }
16902   return 1;
16903 }
16904
16905 /* Move a DW_AT_MIPS_linkage_name attribute just added to dw_die_ref
16906    to the location it would have been added, should we know its
16907    DECL_ASSEMBLER_NAME when we added other attributes.  This will
16908    probably improve compactness of debug info, removing equivalent
16909    abbrevs, and hide any differences caused by deferring the
16910    computation of the assembler name, triggered by e.g. PCH.  */
16911
16912 static inline void
16913 move_linkage_attr (dw_die_ref die)
16914 {
16915   unsigned ix = VEC_length (dw_attr_node, die->die_attr);
16916   dw_attr_node linkage = *VEC_index (dw_attr_node, die->die_attr, ix - 1);
16917
16918   gcc_assert (linkage.dw_attr == DW_AT_MIPS_linkage_name);
16919
16920   while (--ix > 0)
16921     {
16922       dw_attr_node *prev = VEC_index (dw_attr_node, die->die_attr, ix - 1);
16923
16924       if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
16925         break;
16926     }
16927
16928   if (ix != VEC_length (dw_attr_node, die->die_attr) - 1)
16929     {
16930       VEC_pop (dw_attr_node, die->die_attr);
16931       VEC_quick_insert (dw_attr_node, die->die_attr, ix, &linkage);
16932     }
16933 }
16934
16935 /* Output stuff that dwarf requires at the end of every file,
16936    and generate the DWARF-2 debugging info.  */
16937
16938 static void
16939 dwarf2out_finish (const char *filename)
16940 {
16941   limbo_die_node *node, *next_node;
16942   dw_die_ref die = 0;
16943   unsigned int i;
16944
16945   /* Add the name for the main input file now.  We delayed this from
16946      dwarf2out_init to avoid complications with PCH.  */
16947   add_name_attribute (comp_unit_die, remap_debug_filename (filename));
16948   if (!IS_ABSOLUTE_PATH (filename))
16949     add_comp_dir_attribute (comp_unit_die);
16950   else if (get_AT (comp_unit_die, DW_AT_comp_dir) == NULL)
16951     {
16952       bool p = false;
16953       htab_traverse (file_table, file_table_relative_p, &p);
16954       if (p)
16955         add_comp_dir_attribute (comp_unit_die);
16956     }
16957
16958   for (i = 0; i < VEC_length (deferred_locations, deferred_locations_list); i++)
16959     {
16960       add_location_or_const_value_attribute (
16961         VEC_index (deferred_locations, deferred_locations_list, i)->die,
16962         VEC_index (deferred_locations, deferred_locations_list, i)->variable,
16963         DW_AT_location);
16964     }
16965
16966   /* Traverse the limbo die list, and add parent/child links.  The only
16967      dies without parents that should be here are concrete instances of
16968      inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
16969      For concrete instances, we can get the parent die from the abstract
16970      instance.  */
16971   for (node = limbo_die_list; node; node = next_node)
16972     {
16973       next_node = node->next;
16974       die = node->die;
16975
16976       if (die->die_parent == NULL)
16977         {
16978           dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
16979
16980           if (origin)
16981             add_child_die (origin->die_parent, die);
16982           else if (die == comp_unit_die)
16983             ;
16984           else if (errorcount > 0 || sorrycount > 0)
16985             /* It's OK to be confused by errors in the input.  */
16986             add_child_die (comp_unit_die, die);
16987           else
16988             {
16989               /* In certain situations, the lexical block containing a
16990                  nested function can be optimized away, which results
16991                  in the nested function die being orphaned.  Likewise
16992                  with the return type of that nested function.  Force
16993                  this to be a child of the containing function.
16994
16995                  It may happen that even the containing function got fully
16996                  inlined and optimized out.  In that case we are lost and
16997                  assign the empty child.  This should not be big issue as
16998                  the function is likely unreachable too.  */
16999               tree context = NULL_TREE;
17000
17001               gcc_assert (node->created_for);
17002
17003               if (DECL_P (node->created_for))
17004                 context = DECL_CONTEXT (node->created_for);
17005               else if (TYPE_P (node->created_for))
17006                 context = TYPE_CONTEXT (node->created_for);
17007
17008               gcc_assert (context
17009                           && (TREE_CODE (context) == FUNCTION_DECL
17010                               || TREE_CODE (context) == NAMESPACE_DECL));
17011
17012               origin = lookup_decl_die (context);
17013               if (origin)
17014                 add_child_die (origin, die);
17015               else
17016                 add_child_die (comp_unit_die, die);
17017             }
17018         }
17019     }
17020
17021   limbo_die_list = NULL;
17022
17023   for (node = deferred_asm_name; node; node = node->next)
17024     {
17025       tree decl = node->created_for;
17026       if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
17027         {
17028           add_AT_string (node->die, DW_AT_MIPS_linkage_name,
17029                          IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
17030           move_linkage_attr (node->die);
17031         }
17032     }
17033
17034   deferred_asm_name = NULL;
17035
17036   /* Walk through the list of incomplete types again, trying once more to
17037      emit full debugging info for them.  */
17038   retry_incomplete_types ();
17039
17040   if (flag_eliminate_unused_debug_types)
17041     prune_unused_types ();
17042
17043   /* Generate separate CUs for each of the include files we've seen.
17044      They will go into limbo_die_list.  */
17045   if (flag_eliminate_dwarf2_dups)
17046     break_out_includes (comp_unit_die);
17047
17048   /* Traverse the DIE's and add add sibling attributes to those DIE's
17049      that have children.  */
17050   add_sibling_attributes (comp_unit_die);
17051   for (node = limbo_die_list; node; node = node->next)
17052     add_sibling_attributes (node->die);
17053
17054   /* Output a terminator label for the .text section.  */
17055   switch_to_section (text_section);
17056   targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
17057   if (flag_reorder_blocks_and_partition)
17058     {
17059       switch_to_section (unlikely_text_section ());
17060       targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
17061     }
17062
17063   /* We can only use the low/high_pc attributes if all of the code was
17064      in .text.  */
17065   if (!have_multiple_function_sections)
17066     {
17067       add_AT_lbl_id (comp_unit_die, DW_AT_low_pc, text_section_label);
17068       add_AT_lbl_id (comp_unit_die, DW_AT_high_pc, text_end_label);
17069     }
17070
17071   else
17072     {
17073       unsigned fde_idx = 0;
17074
17075       /* We need to give .debug_loc and .debug_ranges an appropriate
17076          "base address".  Use zero so that these addresses become
17077          absolute.  Historically, we've emitted the unexpected
17078          DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
17079          Emit both to give time for other tools to adapt.  */
17080       add_AT_addr (comp_unit_die, DW_AT_low_pc, const0_rtx);
17081       add_AT_addr (comp_unit_die, DW_AT_entry_pc, const0_rtx);
17082
17083       add_AT_range_list (comp_unit_die, DW_AT_ranges,
17084                          add_ranges_by_labels (text_section_label,
17085                                                text_end_label));
17086       if (flag_reorder_blocks_and_partition)
17087         add_ranges_by_labels (cold_text_section_label,
17088                               cold_end_label);
17089
17090       for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
17091         {
17092           dw_fde_ref fde = &fde_table[fde_idx];
17093
17094           if (fde->dw_fde_switched_sections)
17095             {
17096               add_ranges_by_labels (fde->dw_fde_hot_section_label,
17097                                     fde->dw_fde_hot_section_end_label);
17098               add_ranges_by_labels (fde->dw_fde_unlikely_section_label,
17099                                     fde->dw_fde_unlikely_section_end_label);
17100             }
17101           else
17102             add_ranges_by_labels (fde->dw_fde_begin,
17103                                   fde->dw_fde_end);
17104         }
17105
17106       add_ranges (NULL);
17107     }
17108
17109   /* Output location list section if necessary.  */
17110   if (have_location_lists)
17111     {
17112       /* Output the location lists info.  */
17113       switch_to_section (debug_loc_section);
17114       ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
17115                                    DEBUG_LOC_SECTION_LABEL, 0);
17116       ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
17117       output_location_lists (die);
17118     }
17119
17120   if (debug_info_level >= DINFO_LEVEL_NORMAL)
17121     add_AT_lineptr (comp_unit_die, DW_AT_stmt_list,
17122                     debug_line_section_label);
17123
17124   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
17125     add_AT_macptr (comp_unit_die, DW_AT_macro_info, macinfo_section_label);
17126
17127   /* Output all of the compilation units.  We put the main one last so that
17128      the offsets are available to output_pubnames.  */
17129   for (node = limbo_die_list; node; node = node->next)
17130     output_comp_unit (node->die, 0);
17131
17132   /* Output the main compilation unit if non-empty or if .debug_macinfo
17133      has been emitted.  */
17134   output_comp_unit (comp_unit_die, debug_info_level >= DINFO_LEVEL_VERBOSE);
17135
17136   /* Output the abbreviation table.  */
17137   switch_to_section (debug_abbrev_section);
17138   output_abbrev_section ();
17139
17140   /* Output public names table if necessary.  */
17141   if (!VEC_empty (pubname_entry, pubname_table))
17142     {
17143       switch_to_section (debug_pubnames_section);
17144       output_pubnames (pubname_table);
17145     }
17146
17147   /* Output public types table if necessary.  */
17148   /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
17149      It shouldn't hurt to emit it always, since pure DWARF2 consumers
17150      simply won't look for the section.  */
17151   if (!VEC_empty (pubname_entry, pubtype_table))
17152     {
17153       switch_to_section (debug_pubtypes_section);
17154       output_pubnames (pubtype_table);
17155     }
17156
17157   /* Output the address range information.  We only put functions in the arange
17158      table, so don't write it out if we don't have any.  */
17159   if (fde_table_in_use)
17160     {
17161       switch_to_section (debug_aranges_section);
17162       output_aranges ();
17163     }
17164
17165   /* Output ranges section if necessary.  */
17166   if (ranges_table_in_use)
17167     {
17168       switch_to_section (debug_ranges_section);
17169       ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
17170       output_ranges ();
17171     }
17172
17173   /* Output the source line correspondence table.  We must do this
17174      even if there is no line information.  Otherwise, on an empty
17175      translation unit, we will generate a present, but empty,
17176      .debug_info section.  IRIX 6.5 `nm' will then complain when
17177      examining the file.  This is done late so that any filenames
17178      used by the debug_info section are marked as 'used'.  */
17179   if (! DWARF2_ASM_LINE_DEBUG_INFO)
17180     {
17181       switch_to_section (debug_line_section);
17182       output_line_info ();
17183     }
17184
17185   /* Have to end the macro section.  */
17186   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
17187     {
17188       switch_to_section (debug_macinfo_section);
17189       dw2_asm_output_data (1, 0, "End compilation unit");
17190     }
17191
17192   /* If we emitted any DW_FORM_strp form attribute, output the string
17193      table too.  */
17194   if (debug_str_hash)
17195     htab_traverse (debug_str_hash, output_indirect_string, NULL);
17196 }
17197 #else
17198
17199 /* This should never be used, but its address is needed for comparisons.  */
17200 const struct gcc_debug_hooks dwarf2_debug_hooks =
17201 {
17202   0,            /* init */
17203   0,            /* finish */
17204   0,            /* define */
17205   0,            /* undef */
17206   0,            /* start_source_file */
17207   0,            /* end_source_file */
17208   0,            /* begin_block */
17209   0,            /* end_block */
17210   0,            /* ignore_block */
17211   0,            /* source_line */
17212   0,            /* begin_prologue */
17213   0,            /* end_prologue */
17214   0,            /* end_epilogue */
17215   0,            /* begin_function */
17216   0,            /* end_function */
17217   0,            /* function_decl */
17218   0,            /* global_decl */
17219   0,            /* type_decl */
17220   0,            /* imported_module_or_decl */
17221   0,            /* deferred_inline_function */
17222   0,            /* outlining_inline_function */
17223   0,            /* label */
17224   0,            /* handle_pch */
17225   0,            /* var_location */
17226   0,            /* switch_text_section */
17227   0,            /* set_name */
17228   0             /* start_end_main_source_file */
17229 };
17230
17231 #endif /* DWARF2_DEBUGGING_INFO */
17232
17233 #include "gt-dwarf2out.h"