OSDN Git Service

d4799b163b53440f92bf026462a4e0e1d5e6d345
[pf3gnuchains/gcc-fork.git] / gcc / dwarf2out.c
1 /* Output Dwarf2 format symbol table information from GCC.
2    Copyright (C) 1992, 1993, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
3    2003, 2004, 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
4    Contributed by Gary Funck (gary@intrepid.com).
5    Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
6    Extensively modified by Jason Merrill (jason@cygnus.com).
7
8 This file is part of GCC.
9
10 GCC is free software; you can redistribute it and/or modify it under
11 the terms of the GNU General Public License as published by the Free
12 Software Foundation; either version 3, or (at your option) any later
13 version.
14
15 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
16 WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
18 for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING3.  If not see
22 <http://www.gnu.org/licenses/>.  */
23
24 /* TODO: Emit .debug_line header even when there are no functions, since
25            the file numbers are used by .debug_info.  Alternately, leave
26            out locations for types and decls.
27          Avoid talking about ctors and op= for PODs.
28          Factor out common prologue sequences into multiple CIEs.  */
29
30 /* The first part of this file deals with the DWARF 2 frame unwind
31    information, which is also used by the GCC efficient exception handling
32    mechanism.  The second part, controlled only by an #ifdef
33    DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
34    information.  */
35
36 /* DWARF2 Abbreviation Glossary:
37
38    CFA = Canonical Frame Address
39            a fixed address on the stack which identifies a call frame.
40            We define it to be the value of SP just before the call insn.
41            The CFA register and offset, which may change during the course
42            of the function, are used to calculate its value at runtime.
43
44    CFI = Call Frame Instruction
45            an instruction for the DWARF2 abstract machine
46
47    CIE = Common Information Entry
48            information describing information common to one or more FDEs
49
50    DIE = Debugging Information Entry
51
52    FDE = Frame Description Entry
53            information describing the stack call frame, in particular,
54            how to restore registers
55
56    DW_CFA_... = DWARF2 CFA call frame instruction
57    DW_TAG_... = DWARF2 DIE tag */
58
59 #include "config.h"
60 #include "system.h"
61 #include "coretypes.h"
62 #include "tm.h"
63 #include "tree.h"
64 #include "version.h"
65 #include "flags.h"
66 #include "real.h"
67 #include "rtl.h"
68 #include "hard-reg-set.h"
69 #include "regs.h"
70 #include "insn-config.h"
71 #include "reload.h"
72 #include "function.h"
73 #include "output.h"
74 #include "expr.h"
75 #include "libfuncs.h"
76 #include "except.h"
77 #include "dwarf2.h"
78 #include "dwarf2out.h"
79 #include "dwarf2asm.h"
80 #include "toplev.h"
81 #include "varray.h"
82 #include "ggc.h"
83 #include "md5.h"
84 #include "tm_p.h"
85 #include "diagnostic.h"
86 #include "debug.h"
87 #include "target.h"
88 #include "langhooks.h"
89 #include "hashtab.h"
90 #include "cgraph.h"
91 #include "input.h"
92
93 #ifdef DWARF2_DEBUGGING_INFO
94 static void dwarf2out_source_line (unsigned int, const char *);
95 #endif
96
97 #ifndef DWARF2_FRAME_INFO
98 # ifdef DWARF2_DEBUGGING_INFO
99 #  define DWARF2_FRAME_INFO \
100   (write_symbols == DWARF2_DEBUG || write_symbols == VMS_AND_DWARF2_DEBUG)
101 # else
102 #  define DWARF2_FRAME_INFO 0
103 # endif
104 #endif
105
106 /* Map register numbers held in the call frame info that gcc has
107    collected using DWARF_FRAME_REGNUM to those that should be output in
108    .debug_frame and .eh_frame.  */
109 #ifndef DWARF2_FRAME_REG_OUT
110 #define DWARF2_FRAME_REG_OUT(REGNO, FOR_EH) (REGNO)
111 #endif
112
113 /* Save the result of dwarf2out_do_frame across PCH.  */
114 static GTY(()) bool saved_do_cfi_asm = 0;
115
116 /* Decide whether we want to emit frame unwind information for the current
117    translation unit.  */
118
119 int
120 dwarf2out_do_frame (void)
121 {
122   /* We want to emit correct CFA location expressions or lists, so we
123      have to return true if we're going to output debug info, even if
124      we're not going to output frame or unwind info.  */
125   return (write_symbols == DWARF2_DEBUG
126           || write_symbols == VMS_AND_DWARF2_DEBUG
127           || DWARF2_FRAME_INFO || saved_do_cfi_asm
128 #ifdef DWARF2_UNWIND_INFO
129           || (DWARF2_UNWIND_INFO
130               && (flag_unwind_tables
131                   || (flag_exceptions && ! USING_SJLJ_EXCEPTIONS)))
132 #endif
133           );
134 }
135
136 /* Decide whether to emit frame unwind via assembler directives.  */
137
138 int
139 dwarf2out_do_cfi_asm (void)
140 {
141   int enc;
142
143 #ifdef MIPS_DEBUGGING_INFO
144   return false;
145 #endif
146   if (!flag_dwarf2_cfi_asm || !dwarf2out_do_frame ())
147     return false;
148   if (saved_do_cfi_asm || !eh_personality_libfunc)
149     return true;
150   if (!HAVE_GAS_CFI_PERSONALITY_DIRECTIVE)
151     return false;
152
153   /* Make sure the personality encoding is one the assembler can support.
154      In particular, aligned addresses can't be handled.  */
155   enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,/*global=*/1);
156   if ((enc & 0x70) != 0 && (enc & 0x70) != DW_EH_PE_pcrel)
157     return false;
158   enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,/*global=*/0);
159   if ((enc & 0x70) != 0 && (enc & 0x70) != DW_EH_PE_pcrel)
160     return false;
161
162   saved_do_cfi_asm = true;
163   return true;
164 }
165
166 /* The size of the target's pointer type.  */
167 #ifndef PTR_SIZE
168 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
169 #endif
170
171 /* Array of RTXes referenced by the debugging information, which therefore
172    must be kept around forever.  */
173 static GTY(()) VEC(rtx,gc) *used_rtx_array;
174
175 /* A pointer to the base of a list of incomplete types which might be
176    completed at some later time.  incomplete_types_list needs to be a
177    VEC(tree,gc) because we want to tell the garbage collector about
178    it.  */
179 static GTY(()) VEC(tree,gc) *incomplete_types;
180
181 /* A pointer to the base of a table of references to declaration
182    scopes.  This table is a display which tracks the nesting
183    of declaration scopes at the current scope and containing
184    scopes.  This table is used to find the proper place to
185    define type declaration DIE's.  */
186 static GTY(()) VEC(tree,gc) *decl_scope_table;
187
188 /* Pointers to various DWARF2 sections.  */
189 static GTY(()) section *debug_info_section;
190 static GTY(()) section *debug_abbrev_section;
191 static GTY(()) section *debug_aranges_section;
192 static GTY(()) section *debug_macinfo_section;
193 static GTY(()) section *debug_line_section;
194 static GTY(()) section *debug_loc_section;
195 static GTY(()) section *debug_pubnames_section;
196 static GTY(()) section *debug_pubtypes_section;
197 static GTY(()) section *debug_str_section;
198 static GTY(()) section *debug_ranges_section;
199 static GTY(()) section *debug_frame_section;
200
201 /* How to start an assembler comment.  */
202 #ifndef ASM_COMMENT_START
203 #define ASM_COMMENT_START ";#"
204 #endif
205
206 typedef struct dw_cfi_struct *dw_cfi_ref;
207 typedef struct dw_fde_struct *dw_fde_ref;
208 typedef union  dw_cfi_oprnd_struct *dw_cfi_oprnd_ref;
209
210 /* Call frames are described using a sequence of Call Frame
211    Information instructions.  The register number, offset
212    and address fields are provided as possible operands;
213    their use is selected by the opcode field.  */
214
215 enum dw_cfi_oprnd_type {
216   dw_cfi_oprnd_unused,
217   dw_cfi_oprnd_reg_num,
218   dw_cfi_oprnd_offset,
219   dw_cfi_oprnd_addr,
220   dw_cfi_oprnd_loc
221 };
222
223 typedef union dw_cfi_oprnd_struct GTY(())
224 {
225   unsigned int GTY ((tag ("dw_cfi_oprnd_reg_num"))) dw_cfi_reg_num;
226   HOST_WIDE_INT GTY ((tag ("dw_cfi_oprnd_offset"))) dw_cfi_offset;
227   const char * GTY ((tag ("dw_cfi_oprnd_addr"))) dw_cfi_addr;
228   struct dw_loc_descr_struct * GTY ((tag ("dw_cfi_oprnd_loc"))) dw_cfi_loc;
229 }
230 dw_cfi_oprnd;
231
232 typedef struct dw_cfi_struct GTY(())
233 {
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 cfa_loc GTY(())
249 {
250   HOST_WIDE_INT offset;
251   HOST_WIDE_INT base_offset;
252   unsigned int reg;
253   int indirect;            /* 1 if CFA is accessed via a dereference.  */
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 dw_fde_struct GTY(())
263 {
264   tree decl;
265   const char *dw_fde_begin;
266   const char *dw_fde_current_label;
267   const char *dw_fde_end;
268   const char *dw_fde_hot_section_label;
269   const char *dw_fde_hot_section_end_label;
270   const char *dw_fde_unlikely_section_label;
271   const char *dw_fde_unlikely_section_end_label;
272   bool dw_fde_switched_sections;
273   dw_cfi_ref dw_fde_cfi;
274   unsigned funcdef_number;
275   HOST_WIDE_INT stack_realignment;
276   /* Dynamic realign argument pointer register.  */
277   unsigned int drap_reg;
278   /* Virtual dynamic realign argument pointer register.  */
279   unsigned int vdrap_reg;
280   unsigned all_throwers_are_sibcalls : 1;
281   unsigned nothrow : 1;
282   unsigned uses_eh_lsda : 1;
283   /* Whether we did stack realign in this call frame.  */
284   unsigned stack_realign : 1;
285   /* Whether dynamic realign argument pointer register has been saved.  */
286   unsigned drap_reg_saved: 1;
287 }
288 dw_fde_node;
289
290 /* Maximum size (in bytes) of an artificially generated label.  */
291 #define MAX_ARTIFICIAL_LABEL_BYTES      30
292
293 /* The size of addresses as they appear in the Dwarf 2 data.
294    Some architectures use word addresses to refer to code locations,
295    but Dwarf 2 info always uses byte addresses.  On such machines,
296    Dwarf 2 addresses need to be larger than the architecture's
297    pointers.  */
298 #ifndef DWARF2_ADDR_SIZE
299 #define DWARF2_ADDR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
300 #endif
301
302 /* The size in bytes of a DWARF field indicating an offset or length
303    relative to a debug info section, specified to be 4 bytes in the
304    DWARF-2 specification.  The SGI/MIPS ABI defines it to be the same
305    as PTR_SIZE.  */
306
307 #ifndef DWARF_OFFSET_SIZE
308 #define DWARF_OFFSET_SIZE 4
309 #endif
310
311 /* According to the (draft) DWARF 3 specification, the initial length
312    should either be 4 or 12 bytes.  When it's 12 bytes, the first 4
313    bytes are 0xffffffff, followed by the length stored in the next 8
314    bytes.
315
316    However, the SGI/MIPS ABI uses an initial length which is equal to
317    DWARF_OFFSET_SIZE.  It is defined (elsewhere) accordingly.  */
318
319 #ifndef DWARF_INITIAL_LENGTH_SIZE
320 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
321 #endif
322
323 #define DWARF_VERSION 2
324
325 /* Round SIZE up to the nearest BOUNDARY.  */
326 #define DWARF_ROUND(SIZE,BOUNDARY) \
327   ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
328
329 /* Offsets recorded in opcodes are a multiple of this alignment factor.  */
330 #ifndef DWARF_CIE_DATA_ALIGNMENT
331 #ifdef STACK_GROWS_DOWNWARD
332 #define DWARF_CIE_DATA_ALIGNMENT (-((int) UNITS_PER_WORD))
333 #else
334 #define DWARF_CIE_DATA_ALIGNMENT ((int) UNITS_PER_WORD)
335 #endif
336 #endif
337
338 /* CIE identifier.  */
339 #if HOST_BITS_PER_WIDE_INT >= 64
340 #define DWARF_CIE_ID \
341   (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
342 #else
343 #define DWARF_CIE_ID DW_CIE_ID
344 #endif
345
346 /* A pointer to the base of a table that contains frame description
347    information for each routine.  */
348 static GTY((length ("fde_table_allocated"))) dw_fde_ref fde_table;
349
350 /* Number of elements currently allocated for fde_table.  */
351 static GTY(()) unsigned fde_table_allocated;
352
353 /* Number of elements in fde_table currently in use.  */
354 static GTY(()) unsigned fde_table_in_use;
355
356 /* Size (in elements) of increments by which we may expand the
357    fde_table.  */
358 #define FDE_TABLE_INCREMENT 256
359
360 /* Get the current fde_table entry we should use.  */
361
362 static inline dw_fde_ref
363 current_fde (void)
364 {
365   return fde_table_in_use ? &fde_table[fde_table_in_use - 1] : NULL;
366 }
367
368 /* A list of call frame insns for the CIE.  */
369 static GTY(()) dw_cfi_ref cie_cfi_head;
370
371 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
372 /* Some DWARF extensions (e.g., MIPS/SGI) implement a subprogram
373    attribute that accelerates the lookup of the FDE associated
374    with the subprogram.  This variable holds the table index of the FDE
375    associated with the current function (body) definition.  */
376 static unsigned current_funcdef_fde;
377 #endif
378
379 struct indirect_string_node GTY(())
380 {
381   const char *str;
382   unsigned int refcount;
383   unsigned int form;
384   char *label;
385 };
386
387 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
388
389 static GTY(()) int dw2_string_counter;
390 static GTY(()) unsigned long dwarf2out_cfi_label_num;
391
392 /* True if the compilation unit places functions in more than one section.  */
393 static GTY(()) bool have_multiple_function_sections = false;
394
395 /* Whether the default text and cold text sections have been used at all.  */
396
397 static GTY(()) bool text_section_used = false;
398 static GTY(()) bool cold_text_section_used = false;
399
400 /* The default cold text section.  */
401 static GTY(()) section *cold_text_section;
402
403 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
404
405 /* Forward declarations for functions defined in this file.  */
406
407 static char *stripattributes (const char *);
408 static const char *dwarf_cfi_name (unsigned);
409 static dw_cfi_ref new_cfi (void);
410 static void add_cfi (dw_cfi_ref *, dw_cfi_ref);
411 static void add_fde_cfi (const char *, dw_cfi_ref);
412 static void lookup_cfa_1 (dw_cfi_ref, dw_cfa_location *);
413 static void lookup_cfa (dw_cfa_location *);
414 static void reg_save (const char *, unsigned, unsigned, HOST_WIDE_INT);
415 #ifdef DWARF2_UNWIND_INFO
416 static void initial_return_save (rtx);
417 #endif
418 static HOST_WIDE_INT stack_adjust_offset (const_rtx, HOST_WIDE_INT,
419                                           HOST_WIDE_INT);
420 static void output_cfi (dw_cfi_ref, dw_fde_ref, int);
421 static void output_cfi_directive (dw_cfi_ref);
422 static void output_call_frame_info (int);
423 static void dwarf2out_note_section_used (void);
424 static void dwarf2out_stack_adjust (rtx, bool);
425 static void dwarf2out_args_size_adjust (HOST_WIDE_INT, const char *);
426 static void flush_queued_reg_saves (void);
427 static bool clobbers_queued_reg_save (const_rtx);
428 static void dwarf2out_frame_debug_expr (rtx, const char *);
429
430 /* Support for complex CFA locations.  */
431 static void output_cfa_loc (dw_cfi_ref);
432 static void output_cfa_loc_raw (dw_cfi_ref);
433 static void get_cfa_from_loc_descr (dw_cfa_location *,
434                                     struct dw_loc_descr_struct *);
435 static struct dw_loc_descr_struct *build_cfa_loc
436   (dw_cfa_location *, HOST_WIDE_INT);
437 static struct dw_loc_descr_struct *build_cfa_aligned_loc
438   (HOST_WIDE_INT, HOST_WIDE_INT);
439 static void def_cfa_1 (const char *, dw_cfa_location *);
440
441 /* How to start an assembler comment.  */
442 #ifndef ASM_COMMENT_START
443 #define ASM_COMMENT_START ";#"
444 #endif
445
446 /* Data and reference forms for relocatable data.  */
447 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
448 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
449
450 #ifndef DEBUG_FRAME_SECTION
451 #define DEBUG_FRAME_SECTION     ".debug_frame"
452 #endif
453
454 #ifndef FUNC_BEGIN_LABEL
455 #define FUNC_BEGIN_LABEL        "LFB"
456 #endif
457
458 #ifndef FUNC_END_LABEL
459 #define FUNC_END_LABEL          "LFE"
460 #endif
461
462 #ifndef FRAME_BEGIN_LABEL
463 #define FRAME_BEGIN_LABEL       "Lframe"
464 #endif
465 #define CIE_AFTER_SIZE_LABEL    "LSCIE"
466 #define CIE_END_LABEL           "LECIE"
467 #define FDE_LABEL               "LSFDE"
468 #define FDE_AFTER_SIZE_LABEL    "LASFDE"
469 #define FDE_END_LABEL           "LEFDE"
470 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
471 #define LINE_NUMBER_END_LABEL   "LELT"
472 #define LN_PROLOG_AS_LABEL      "LASLTP"
473 #define LN_PROLOG_END_LABEL     "LELTP"
474 #define DIE_LABEL_PREFIX        "DW"
475
476 /* The DWARF 2 CFA column which tracks the return address.  Normally this
477    is the column for PC, or the first column after all of the hard
478    registers.  */
479 #ifndef DWARF_FRAME_RETURN_COLUMN
480 #ifdef PC_REGNUM
481 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGNUM (PC_REGNUM)
482 #else
483 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGISTERS
484 #endif
485 #endif
486
487 /* The mapping from gcc register number to DWARF 2 CFA column number.  By
488    default, we just provide columns for all registers.  */
489 #ifndef DWARF_FRAME_REGNUM
490 #define DWARF_FRAME_REGNUM(REG) DBX_REGISTER_NUMBER (REG)
491 #endif
492 \f
493 /* Hook used by __throw.  */
494
495 rtx
496 expand_builtin_dwarf_sp_column (void)
497 {
498   unsigned int dwarf_regnum = DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM);
499   return GEN_INT (DWARF2_FRAME_REG_OUT (dwarf_regnum, 1));
500 }
501
502 /* Return a pointer to a copy of the section string name S with all
503    attributes stripped off, and an asterisk prepended (for assemble_name).  */
504
505 static inline char *
506 stripattributes (const char *s)
507 {
508   char *stripped = XNEWVEC (char, strlen (s) + 2);
509   char *p = stripped;
510
511   *p++ = '*';
512
513   while (*s && *s != ',')
514     *p++ = *s++;
515
516   *p = '\0';
517   return stripped;
518 }
519
520 /* MEM is a memory reference for the register size table, each element of
521    which has mode MODE.  Initialize column C as a return address column.  */
522
523 static void
524 init_return_column_size (enum machine_mode mode, rtx mem, unsigned int c)
525 {
526   HOST_WIDE_INT offset = c * GET_MODE_SIZE (mode);
527   HOST_WIDE_INT size = GET_MODE_SIZE (Pmode);
528   emit_move_insn (adjust_address (mem, mode, offset), GEN_INT (size));
529 }
530
531 /* Generate code to initialize the register size table.  */
532
533 void
534 expand_builtin_init_dwarf_reg_sizes (tree address)
535 {
536   unsigned int i;
537   enum machine_mode mode = TYPE_MODE (char_type_node);
538   rtx addr = expand_normal (address);
539   rtx mem = gen_rtx_MEM (BLKmode, addr);
540   bool wrote_return_column = false;
541
542   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
543     {
544       int rnum = DWARF2_FRAME_REG_OUT (DWARF_FRAME_REGNUM (i), 1);
545
546       if (rnum < DWARF_FRAME_REGISTERS)
547         {
548           HOST_WIDE_INT offset = rnum * GET_MODE_SIZE (mode);
549           enum machine_mode save_mode = reg_raw_mode[i];
550           HOST_WIDE_INT size;
551
552           if (HARD_REGNO_CALL_PART_CLOBBERED (i, save_mode))
553             save_mode = choose_hard_reg_mode (i, 1, true);
554           if (DWARF_FRAME_REGNUM (i) == DWARF_FRAME_RETURN_COLUMN)
555             {
556               if (save_mode == VOIDmode)
557                 continue;
558               wrote_return_column = true;
559             }
560           size = GET_MODE_SIZE (save_mode);
561           if (offset < 0)
562             continue;
563
564           emit_move_insn (adjust_address (mem, mode, offset),
565                           gen_int_mode (size, mode));
566         }
567     }
568
569   if (!wrote_return_column)
570     init_return_column_size (mode, mem, DWARF_FRAME_RETURN_COLUMN);
571
572 #ifdef DWARF_ALT_FRAME_RETURN_COLUMN
573   init_return_column_size (mode, mem, DWARF_ALT_FRAME_RETURN_COLUMN);
574 #endif
575
576   targetm.init_dwarf_reg_sizes_extra (address);
577 }
578
579 /* Convert a DWARF call frame info. operation to its string name */
580
581 static const char *
582 dwarf_cfi_name (unsigned int cfi_opc)
583 {
584   switch (cfi_opc)
585     {
586     case DW_CFA_advance_loc:
587       return "DW_CFA_advance_loc";
588     case DW_CFA_offset:
589       return "DW_CFA_offset";
590     case DW_CFA_restore:
591       return "DW_CFA_restore";
592     case DW_CFA_nop:
593       return "DW_CFA_nop";
594     case DW_CFA_set_loc:
595       return "DW_CFA_set_loc";
596     case DW_CFA_advance_loc1:
597       return "DW_CFA_advance_loc1";
598     case DW_CFA_advance_loc2:
599       return "DW_CFA_advance_loc2";
600     case DW_CFA_advance_loc4:
601       return "DW_CFA_advance_loc4";
602     case DW_CFA_offset_extended:
603       return "DW_CFA_offset_extended";
604     case DW_CFA_restore_extended:
605       return "DW_CFA_restore_extended";
606     case DW_CFA_undefined:
607       return "DW_CFA_undefined";
608     case DW_CFA_same_value:
609       return "DW_CFA_same_value";
610     case DW_CFA_register:
611       return "DW_CFA_register";
612     case DW_CFA_remember_state:
613       return "DW_CFA_remember_state";
614     case DW_CFA_restore_state:
615       return "DW_CFA_restore_state";
616     case DW_CFA_def_cfa:
617       return "DW_CFA_def_cfa";
618     case DW_CFA_def_cfa_register:
619       return "DW_CFA_def_cfa_register";
620     case DW_CFA_def_cfa_offset:
621       return "DW_CFA_def_cfa_offset";
622
623     /* DWARF 3 */
624     case DW_CFA_def_cfa_expression:
625       return "DW_CFA_def_cfa_expression";
626     case DW_CFA_expression:
627       return "DW_CFA_expression";
628     case DW_CFA_offset_extended_sf:
629       return "DW_CFA_offset_extended_sf";
630     case DW_CFA_def_cfa_sf:
631       return "DW_CFA_def_cfa_sf";
632     case DW_CFA_def_cfa_offset_sf:
633       return "DW_CFA_def_cfa_offset_sf";
634
635     /* SGI/MIPS specific */
636     case DW_CFA_MIPS_advance_loc8:
637       return "DW_CFA_MIPS_advance_loc8";
638
639     /* GNU extensions */
640     case DW_CFA_GNU_window_save:
641       return "DW_CFA_GNU_window_save";
642     case DW_CFA_GNU_args_size:
643       return "DW_CFA_GNU_args_size";
644     case DW_CFA_GNU_negative_offset_extended:
645       return "DW_CFA_GNU_negative_offset_extended";
646
647     default:
648       return "DW_CFA_<unknown>";
649     }
650 }
651
652 /* Return a pointer to a newly allocated Call Frame Instruction.  */
653
654 static inline dw_cfi_ref
655 new_cfi (void)
656 {
657   dw_cfi_ref cfi = GGC_NEW (dw_cfi_node);
658
659   cfi->dw_cfi_next = NULL;
660   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = 0;
661   cfi->dw_cfi_oprnd2.dw_cfi_reg_num = 0;
662
663   return cfi;
664 }
665
666 /* Add a Call Frame Instruction to list of instructions.  */
667
668 static inline void
669 add_cfi (dw_cfi_ref *list_head, dw_cfi_ref cfi)
670 {
671   dw_cfi_ref *p;
672   dw_fde_ref fde = current_fde ();
673
674   /* When DRAP is used, CFA is defined with an expression.  Redefine
675      CFA may lead to a different CFA value.   */
676   if (fde && fde->drap_reg != INVALID_REGNUM)
677     switch (cfi->dw_cfi_opc)
678       {
679         case DW_CFA_def_cfa_register:
680         case DW_CFA_def_cfa_offset:
681         case DW_CFA_def_cfa_offset_sf:
682         case DW_CFA_def_cfa:
683         case DW_CFA_def_cfa_sf:
684           gcc_unreachable ();
685
686         default:
687           break;
688       }
689
690   /* Find the end of the chain.  */
691   for (p = list_head; (*p) != NULL; p = &(*p)->dw_cfi_next)
692     ;
693
694   *p = cfi;
695 }
696
697 /* Generate a new label for the CFI info to refer to.  */
698
699 char *
700 dwarf2out_cfi_label (void)
701 {
702   static char label[20];
703
704   if (dwarf2out_do_cfi_asm ())
705     {
706       /* In this case, we will be emitting the asm directive instead of
707          the label, so just return a placeholder to keep the rest of the
708          interfaces happy.  */
709       strcpy (label, "<do not output>");
710     }
711   else
712     {
713       ASM_GENERATE_INTERNAL_LABEL (label, "LCFI", dwarf2out_cfi_label_num++);
714       ASM_OUTPUT_LABEL (asm_out_file, label);
715     }
716
717   return label;
718 }
719
720 /* Add CFI to the current fde at the PC value indicated by LABEL if specified,
721    or to the CIE if LABEL is NULL.  */
722
723 static void
724 add_fde_cfi (const char *label, dw_cfi_ref cfi)
725 {
726   dw_cfi_ref *list_head = &cie_cfi_head;
727
728   if (dwarf2out_do_cfi_asm ())
729     {
730       if (label)
731         {
732           output_cfi_directive (cfi);
733
734           /* We still have to add the cfi to the list so that
735              lookup_cfa works later on.  */
736           list_head = &current_fde ()->dw_fde_cfi;
737         }
738       /* ??? If this is a CFI for the CIE, we don't emit.  This
739          assumes that the standard CIE contents that the assembler
740          uses matches the standard CIE contents that the compiler
741          uses.  This is probably a bad assumption.  I'm not quite
742          sure how to address this for now.  */
743     }
744   else if (label)
745     {
746       dw_fde_ref fde = current_fde ();
747
748       gcc_assert (fde != NULL);
749
750       if (*label == 0)
751         label = dwarf2out_cfi_label ();
752
753       if (fde->dw_fde_current_label == NULL
754           || strcmp (label, fde->dw_fde_current_label) != 0)
755         {
756           dw_cfi_ref xcfi;
757
758           label = xstrdup (label);
759
760           /* Set the location counter to the new label.  */
761           xcfi = new_cfi ();
762           /* If we have a current label, advance from there, otherwise
763              set the location directly using set_loc.  */
764           xcfi->dw_cfi_opc = fde->dw_fde_current_label
765                              ? DW_CFA_advance_loc4
766                              : DW_CFA_set_loc;
767           xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
768           add_cfi (&fde->dw_fde_cfi, xcfi);
769
770           fde->dw_fde_current_label = label;
771         }
772
773       list_head = &fde->dw_fde_cfi;
774     }
775
776   add_cfi (list_head, cfi);
777 }
778
779 /* Subroutine of lookup_cfa.  */
780
781 static void
782 lookup_cfa_1 (dw_cfi_ref cfi, dw_cfa_location *loc)
783 {
784   switch (cfi->dw_cfi_opc)
785     {
786     case DW_CFA_def_cfa_offset:
787     case DW_CFA_def_cfa_offset_sf:
788       loc->offset = cfi->dw_cfi_oprnd1.dw_cfi_offset;
789       break;
790     case DW_CFA_def_cfa_register:
791       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
792       break;
793     case DW_CFA_def_cfa:
794     case DW_CFA_def_cfa_sf:
795       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
796       loc->offset = cfi->dw_cfi_oprnd2.dw_cfi_offset;
797       break;
798     case DW_CFA_def_cfa_expression:
799       get_cfa_from_loc_descr (loc, cfi->dw_cfi_oprnd1.dw_cfi_loc);
800       break;
801     default:
802       break;
803     }
804 }
805
806 /* Find the previous value for the CFA.  */
807
808 static void
809 lookup_cfa (dw_cfa_location *loc)
810 {
811   dw_cfi_ref cfi;
812   dw_fde_ref fde;
813
814   loc->reg = INVALID_REGNUM;
815   loc->offset = 0;
816   loc->indirect = 0;
817   loc->base_offset = 0;
818
819   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
820     lookup_cfa_1 (cfi, loc);
821
822   fde = current_fde ();
823   if (fde)
824     for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
825       lookup_cfa_1 (cfi, loc);
826 }
827
828 /* The current rule for calculating the DWARF2 canonical frame address.  */
829 static dw_cfa_location cfa;
830
831 /* The register used for saving registers to the stack, and its offset
832    from the CFA.  */
833 static dw_cfa_location cfa_store;
834
835 /* The running total of the size of arguments pushed onto the stack.  */
836 static HOST_WIDE_INT args_size;
837
838 /* The last args_size we actually output.  */
839 static HOST_WIDE_INT old_args_size;
840
841 /* Entry point to update the canonical frame address (CFA).
842    LABEL is passed to add_fde_cfi.  The value of CFA is now to be
843    calculated from REG+OFFSET.  */
844
845 void
846 dwarf2out_def_cfa (const char *label, unsigned int reg, HOST_WIDE_INT offset)
847 {
848   dw_cfa_location loc;
849   loc.indirect = 0;
850   loc.base_offset = 0;
851   loc.reg = reg;
852   loc.offset = offset;
853   def_cfa_1 (label, &loc);
854 }
855
856 /* Determine if two dw_cfa_location structures define the same data.  */
857
858 static bool
859 cfa_equal_p (const dw_cfa_location *loc1, const dw_cfa_location *loc2)
860 {
861   return (loc1->reg == loc2->reg
862           && loc1->offset == loc2->offset
863           && loc1->indirect == loc2->indirect
864           && (loc1->indirect == 0
865               || loc1->base_offset == loc2->base_offset));
866 }
867
868 /* This routine does the actual work.  The CFA is now calculated from
869    the dw_cfa_location structure.  */
870
871 static void
872 def_cfa_1 (const char *label, dw_cfa_location *loc_p)
873 {
874   dw_cfi_ref cfi;
875   dw_cfa_location old_cfa, loc;
876
877   cfa = *loc_p;
878   loc = *loc_p;
879
880   if (cfa_store.reg == loc.reg && loc.indirect == 0)
881     cfa_store.offset = loc.offset;
882
883   loc.reg = DWARF_FRAME_REGNUM (loc.reg);
884   lookup_cfa (&old_cfa);
885
886   /* If nothing changed, no need to issue any call frame instructions.  */
887   if (cfa_equal_p (&loc, &old_cfa))
888     return;
889
890   cfi = new_cfi ();
891
892   if (loc.reg == old_cfa.reg && !loc.indirect)
893     {
894       /* Construct a "DW_CFA_def_cfa_offset <offset>" instruction, indicating
895          the CFA register did not change but the offset did.  The data 
896          factoring for DW_CFA_def_cfa_offset_sf happens in output_cfi, or
897          in the assembler via the .cfi_def_cfa_offset directive.  */
898       if (loc.offset < 0)
899         cfi->dw_cfi_opc = DW_CFA_def_cfa_offset_sf;
900       else
901         cfi->dw_cfi_opc = DW_CFA_def_cfa_offset;
902       cfi->dw_cfi_oprnd1.dw_cfi_offset = loc.offset;
903     }
904
905 #ifndef MIPS_DEBUGGING_INFO  /* SGI dbx thinks this means no offset.  */
906   else if (loc.offset == old_cfa.offset
907            && old_cfa.reg != INVALID_REGNUM
908            && !loc.indirect)
909     {
910       /* Construct a "DW_CFA_def_cfa_register <register>" instruction,
911          indicating the CFA register has changed to <register> but the
912          offset has not changed.  */
913       cfi->dw_cfi_opc = DW_CFA_def_cfa_register;
914       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
915     }
916 #endif
917
918   else if (loc.indirect == 0)
919     {
920       /* Construct a "DW_CFA_def_cfa <register> <offset>" instruction,
921          indicating the CFA register has changed to <register> with
922          the specified offset.  The data factoring for DW_CFA_def_cfa_sf
923          happens in output_cfi, or in the assembler via the .cfi_def_cfa
924          directive.  */
925       if (loc.offset < 0)
926         cfi->dw_cfi_opc = DW_CFA_def_cfa_sf;
927       else
928         cfi->dw_cfi_opc = DW_CFA_def_cfa;
929       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
930       cfi->dw_cfi_oprnd2.dw_cfi_offset = loc.offset;
931     }
932   else
933     {
934       /* Construct a DW_CFA_def_cfa_expression instruction to
935          calculate the CFA using a full location expression since no
936          register-offset pair is available.  */
937       struct dw_loc_descr_struct *loc_list;
938
939       cfi->dw_cfi_opc = DW_CFA_def_cfa_expression;
940       loc_list = build_cfa_loc (&loc, 0);
941       cfi->dw_cfi_oprnd1.dw_cfi_loc = loc_list;
942     }
943
944   add_fde_cfi (label, cfi);
945 }
946
947 /* Add the CFI for saving a register.  REG is the CFA column number.
948    LABEL is passed to add_fde_cfi.
949    If SREG is -1, the register is saved at OFFSET from the CFA;
950    otherwise it is saved in SREG.  */
951
952 static void
953 reg_save (const char *label, unsigned int reg, unsigned int sreg, HOST_WIDE_INT offset)
954 {
955   dw_cfi_ref cfi = new_cfi ();
956   dw_fde_ref fde = current_fde ();
957
958   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
959
960   /* When stack is aligned, store REG using DW_CFA_expression with
961      FP.  */
962   if (fde
963       && fde->stack_realign
964       && sreg == INVALID_REGNUM)
965     {
966       cfi->dw_cfi_opc = DW_CFA_expression;
967       cfi->dw_cfi_oprnd2.dw_cfi_reg_num = reg;
968       cfi->dw_cfi_oprnd1.dw_cfi_loc
969         = build_cfa_aligned_loc (offset, fde->stack_realignment);
970     }
971   else if (sreg == INVALID_REGNUM)
972     {
973       if (offset < 0)
974         cfi->dw_cfi_opc = DW_CFA_offset_extended_sf;
975       else if (reg & ~0x3f)
976         cfi->dw_cfi_opc = DW_CFA_offset_extended;
977       else
978         cfi->dw_cfi_opc = DW_CFA_offset;
979       cfi->dw_cfi_oprnd2.dw_cfi_offset = offset;
980     }
981   else if (sreg == reg)
982     cfi->dw_cfi_opc = DW_CFA_same_value;
983   else
984     {
985       cfi->dw_cfi_opc = DW_CFA_register;
986       cfi->dw_cfi_oprnd2.dw_cfi_reg_num = sreg;
987     }
988
989   add_fde_cfi (label, cfi);
990 }
991
992 /* Add the CFI for saving a register window.  LABEL is passed to reg_save.
993    This CFI tells the unwinder that it needs to restore the window registers
994    from the previous frame's window save area.
995
996    ??? Perhaps we should note in the CIE where windows are saved (instead of
997    assuming 0(cfa)) and what registers are in the window.  */
998
999 void
1000 dwarf2out_window_save (const char *label)
1001 {
1002   dw_cfi_ref cfi = new_cfi ();
1003
1004   cfi->dw_cfi_opc = DW_CFA_GNU_window_save;
1005   add_fde_cfi (label, cfi);
1006 }
1007
1008 /* Add a CFI to update the running total of the size of arguments
1009    pushed onto the stack.  */
1010
1011 void
1012 dwarf2out_args_size (const char *label, HOST_WIDE_INT size)
1013 {
1014   dw_cfi_ref cfi;
1015
1016   if (size == old_args_size)
1017     return;
1018
1019   old_args_size = size;
1020
1021   cfi = new_cfi ();
1022   cfi->dw_cfi_opc = DW_CFA_GNU_args_size;
1023   cfi->dw_cfi_oprnd1.dw_cfi_offset = size;
1024   add_fde_cfi (label, cfi);
1025 }
1026
1027 /* Entry point for saving a register to the stack.  REG is the GCC register
1028    number.  LABEL and OFFSET are passed to reg_save.  */
1029
1030 void
1031 dwarf2out_reg_save (const char *label, unsigned int reg, HOST_WIDE_INT offset)
1032 {
1033   reg_save (label, DWARF_FRAME_REGNUM (reg), INVALID_REGNUM, offset);
1034 }
1035
1036 /* Entry point for saving the return address in the stack.
1037    LABEL and OFFSET are passed to reg_save.  */
1038
1039 void
1040 dwarf2out_return_save (const char *label, HOST_WIDE_INT offset)
1041 {
1042   reg_save (label, DWARF_FRAME_RETURN_COLUMN, INVALID_REGNUM, offset);
1043 }
1044
1045 /* Entry point for saving the return address in a register.
1046    LABEL and SREG are passed to reg_save.  */
1047
1048 void
1049 dwarf2out_return_reg (const char *label, unsigned int sreg)
1050 {
1051   reg_save (label, DWARF_FRAME_RETURN_COLUMN, DWARF_FRAME_REGNUM (sreg), 0);
1052 }
1053
1054 #ifdef DWARF2_UNWIND_INFO
1055 /* Record the initial position of the return address.  RTL is
1056    INCOMING_RETURN_ADDR_RTX.  */
1057
1058 static void
1059 initial_return_save (rtx rtl)
1060 {
1061   unsigned int reg = INVALID_REGNUM;
1062   HOST_WIDE_INT offset = 0;
1063
1064   switch (GET_CODE (rtl))
1065     {
1066     case REG:
1067       /* RA is in a register.  */
1068       reg = DWARF_FRAME_REGNUM (REGNO (rtl));
1069       break;
1070
1071     case MEM:
1072       /* RA is on the stack.  */
1073       rtl = XEXP (rtl, 0);
1074       switch (GET_CODE (rtl))
1075         {
1076         case REG:
1077           gcc_assert (REGNO (rtl) == STACK_POINTER_REGNUM);
1078           offset = 0;
1079           break;
1080
1081         case PLUS:
1082           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1083           offset = INTVAL (XEXP (rtl, 1));
1084           break;
1085
1086         case MINUS:
1087           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1088           offset = -INTVAL (XEXP (rtl, 1));
1089           break;
1090
1091         default:
1092           gcc_unreachable ();
1093         }
1094
1095       break;
1096
1097     case PLUS:
1098       /* The return address is at some offset from any value we can
1099          actually load.  For instance, on the SPARC it is in %i7+8. Just
1100          ignore the offset for now; it doesn't matter for unwinding frames.  */
1101       gcc_assert (GET_CODE (XEXP (rtl, 1)) == CONST_INT);
1102       initial_return_save (XEXP (rtl, 0));
1103       return;
1104
1105     default:
1106       gcc_unreachable ();
1107     }
1108
1109   if (reg != DWARF_FRAME_RETURN_COLUMN)
1110     reg_save (NULL, DWARF_FRAME_RETURN_COLUMN, reg, offset - cfa.offset);
1111 }
1112 #endif
1113
1114 /* Given a SET, calculate the amount of stack adjustment it
1115    contains.  */
1116
1117 static HOST_WIDE_INT
1118 stack_adjust_offset (const_rtx pattern, HOST_WIDE_INT cur_args_size,
1119                      HOST_WIDE_INT cur_offset)
1120 {
1121   const_rtx src = SET_SRC (pattern);
1122   const_rtx dest = SET_DEST (pattern);
1123   HOST_WIDE_INT offset = 0;
1124   enum rtx_code code;
1125
1126   if (dest == stack_pointer_rtx)
1127     {
1128       code = GET_CODE (src);
1129
1130       /* Assume (set (reg sp) (reg whatever)) sets args_size
1131          level to 0.  */
1132       if (code == REG && src != stack_pointer_rtx)
1133         {
1134           offset = -cur_args_size;
1135 #ifndef STACK_GROWS_DOWNWARD
1136           offset = -offset;
1137 #endif
1138           return offset - cur_offset;
1139         }
1140
1141       if (! (code == PLUS || code == MINUS)
1142           || XEXP (src, 0) != stack_pointer_rtx
1143           || GET_CODE (XEXP (src, 1)) != CONST_INT)
1144         return 0;
1145
1146       /* (set (reg sp) (plus (reg sp) (const_int))) */
1147       offset = INTVAL (XEXP (src, 1));
1148       if (code == PLUS)
1149         offset = -offset;
1150       return offset;
1151     }
1152
1153   if (MEM_P (src) && !MEM_P (dest))
1154     dest = src;
1155   if (MEM_P (dest))
1156     {
1157       /* (set (mem (pre_dec (reg sp))) (foo)) */
1158       src = XEXP (dest, 0);
1159       code = GET_CODE (src);
1160
1161       switch (code)
1162         {
1163         case PRE_MODIFY:
1164         case POST_MODIFY:
1165           if (XEXP (src, 0) == stack_pointer_rtx)
1166             {
1167               rtx val = XEXP (XEXP (src, 1), 1);
1168               /* We handle only adjustments by constant amount.  */
1169               gcc_assert (GET_CODE (XEXP (src, 1)) == PLUS
1170                           && GET_CODE (val) == CONST_INT);
1171               offset = -INTVAL (val);
1172               break;
1173             }
1174           return 0;
1175
1176         case PRE_DEC:
1177         case POST_DEC:
1178           if (XEXP (src, 0) == stack_pointer_rtx)
1179             {
1180               offset = GET_MODE_SIZE (GET_MODE (dest));
1181               break;
1182             }
1183           return 0;
1184
1185         case PRE_INC:
1186         case POST_INC:
1187           if (XEXP (src, 0) == stack_pointer_rtx)
1188             {
1189               offset = -GET_MODE_SIZE (GET_MODE (dest));
1190               break;
1191             }
1192           return 0;
1193
1194         default:
1195           return 0;
1196         }
1197     }
1198   else
1199     return 0;
1200
1201   return offset;
1202 }
1203
1204 /* Precomputed args_size for CODE_LABELs and BARRIERs preceeding them,
1205    indexed by INSN_UID.  */
1206
1207 static HOST_WIDE_INT *barrier_args_size;
1208
1209 /* Helper function for compute_barrier_args_size.  Handle one insn.  */
1210
1211 static HOST_WIDE_INT
1212 compute_barrier_args_size_1 (rtx insn, HOST_WIDE_INT cur_args_size,
1213                              VEC (rtx, heap) **next)
1214 {
1215   HOST_WIDE_INT offset = 0;
1216   int i;
1217
1218   if (! RTX_FRAME_RELATED_P (insn))
1219     {
1220       if (prologue_epilogue_contains (insn)
1221           || sibcall_epilogue_contains (insn))
1222         /* Nothing */;
1223       else if (GET_CODE (PATTERN (insn)) == SET)
1224         offset = stack_adjust_offset (PATTERN (insn), cur_args_size, 0);
1225       else if (GET_CODE (PATTERN (insn)) == PARALLEL
1226                || GET_CODE (PATTERN (insn)) == SEQUENCE)
1227         {
1228           /* There may be stack adjustments inside compound insns.  Search
1229              for them.  */
1230           for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1231             if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1232               offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i),
1233                                              cur_args_size, offset);
1234         }
1235     }
1236   else
1237     {
1238       rtx expr = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
1239
1240       if (expr)
1241         {
1242           expr = XEXP (expr, 0);
1243           if (GET_CODE (expr) == PARALLEL
1244               || GET_CODE (expr) == SEQUENCE)
1245             for (i = 1; i < XVECLEN (expr, 0); i++)
1246               {
1247                 rtx elem = XVECEXP (expr, 0, i);
1248
1249                 if (GET_CODE (elem) == SET && !RTX_FRAME_RELATED_P (elem))
1250                   offset += stack_adjust_offset (elem, cur_args_size, offset);
1251               }
1252         }
1253     }
1254
1255 #ifndef STACK_GROWS_DOWNWARD
1256   offset = -offset;
1257 #endif
1258
1259   cur_args_size += offset;
1260   if (cur_args_size < 0)
1261     cur_args_size = 0;
1262
1263   if (JUMP_P (insn))
1264     {
1265       rtx dest = JUMP_LABEL (insn);
1266
1267       if (dest)
1268         {
1269           if (barrier_args_size [INSN_UID (dest)] < 0)
1270             {
1271               barrier_args_size [INSN_UID (dest)] = cur_args_size;
1272               VEC_safe_push (rtx, heap, *next, dest);
1273             }
1274         }
1275     }
1276
1277   return cur_args_size;
1278 }
1279
1280 /* Walk the whole function and compute args_size on BARRIERs.  */
1281
1282 static void
1283 compute_barrier_args_size (void)
1284 {
1285   int max_uid = get_max_uid (), i;
1286   rtx insn;
1287   VEC (rtx, heap) *worklist, *next, *tmp;
1288
1289   barrier_args_size = XNEWVEC (HOST_WIDE_INT, max_uid);
1290   for (i = 0; i < max_uid; i++)
1291     barrier_args_size[i] = -1;
1292
1293   worklist = VEC_alloc (rtx, heap, 20);
1294   next = VEC_alloc (rtx, heap, 20);
1295   insn = get_insns ();
1296   barrier_args_size[INSN_UID (insn)] = 0;
1297   VEC_quick_push (rtx, worklist, insn);
1298   for (;;)
1299     {
1300       while (!VEC_empty (rtx, worklist))
1301         {
1302           rtx prev, body, first_insn;
1303           HOST_WIDE_INT cur_args_size;
1304
1305           first_insn = insn = VEC_pop (rtx, worklist);
1306           cur_args_size = barrier_args_size[INSN_UID (insn)];
1307           prev = prev_nonnote_insn (insn);
1308           if (prev && BARRIER_P (prev))
1309             barrier_args_size[INSN_UID (prev)] = cur_args_size;
1310
1311           for (; insn; insn = NEXT_INSN (insn))
1312             {
1313               if (INSN_DELETED_P (insn) || NOTE_P (insn))
1314                 continue;
1315               if (BARRIER_P (insn))
1316                 break;
1317
1318               if (LABEL_P (insn))
1319                 {
1320                   if (insn == first_insn)
1321                     continue;
1322                   else if (barrier_args_size[INSN_UID (insn)] < 0)
1323                     {
1324                       barrier_args_size[INSN_UID (insn)] = cur_args_size;
1325                       continue;
1326                     }
1327                   else
1328                     {
1329                       /* The insns starting with this label have been
1330                          already scanned or are in the worklist.  */
1331                       break;
1332                     }
1333                 }
1334
1335               body = PATTERN (insn);
1336               if (GET_CODE (body) == SEQUENCE)
1337                 {
1338                   HOST_WIDE_INT dest_args_size = cur_args_size;
1339                   for (i = 1; i < XVECLEN (body, 0); i++)
1340                     if (INSN_ANNULLED_BRANCH_P (XVECEXP (body, 0, 0))
1341                         && INSN_FROM_TARGET_P (XVECEXP (body, 0, i)))
1342                       dest_args_size
1343                         = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1344                                                        dest_args_size, &next);
1345                     else
1346                       cur_args_size
1347                         = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1348                                                        cur_args_size, &next);
1349
1350                   if (INSN_ANNULLED_BRANCH_P (XVECEXP (body, 0, 0)))
1351                     compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1352                                                  dest_args_size, &next);
1353                   else
1354                     cur_args_size
1355                       = compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1356                                                      cur_args_size, &next);
1357                 }
1358               else
1359                 cur_args_size
1360                   = compute_barrier_args_size_1 (insn, cur_args_size, &next);
1361             }
1362         }
1363
1364       if (VEC_empty (rtx, next))
1365         break;
1366
1367       /* Swap WORKLIST with NEXT and truncate NEXT for next iteration.  */
1368       tmp = next;
1369       next = worklist;
1370       worklist = tmp;
1371       VEC_truncate (rtx, next, 0);
1372     }
1373
1374   VEC_free (rtx, heap, worklist);
1375   VEC_free (rtx, heap, next);
1376 }
1377
1378
1379 /* Check INSN to see if it looks like a push or a stack adjustment, and
1380    make a note of it if it does.  EH uses this information to find out how
1381    much extra space it needs to pop off the stack.  */
1382
1383 static void
1384 dwarf2out_stack_adjust (rtx insn, bool after_p)
1385 {
1386   HOST_WIDE_INT offset;
1387   const char *label;
1388   int i;
1389
1390   /* Don't handle epilogues at all.  Certainly it would be wrong to do so
1391      with this function.  Proper support would require all frame-related
1392      insns to be marked, and to be able to handle saving state around
1393      epilogues textually in the middle of the function.  */
1394   if (prologue_epilogue_contains (insn) || sibcall_epilogue_contains (insn))
1395     return;
1396
1397   /* If INSN is an instruction from target of an annulled branch, the
1398      effects are for the target only and so current argument size
1399      shouldn't change at all.  */
1400   if (final_sequence
1401       && INSN_ANNULLED_BRANCH_P (XVECEXP (final_sequence, 0, 0))
1402       && INSN_FROM_TARGET_P (insn))
1403     return;
1404
1405   /* If only calls can throw, and we have a frame pointer,
1406      save up adjustments until we see the CALL_INSN.  */
1407   if (!flag_asynchronous_unwind_tables && cfa.reg != STACK_POINTER_REGNUM)
1408     {
1409       if (CALL_P (insn) && !after_p)
1410         {
1411           /* Extract the size of the args from the CALL rtx itself.  */
1412           insn = PATTERN (insn);
1413           if (GET_CODE (insn) == PARALLEL)
1414             insn = XVECEXP (insn, 0, 0);
1415           if (GET_CODE (insn) == SET)
1416             insn = SET_SRC (insn);
1417           gcc_assert (GET_CODE (insn) == CALL);
1418           dwarf2out_args_size ("", INTVAL (XEXP (insn, 1)));
1419         }
1420       return;
1421     }
1422
1423   if (CALL_P (insn) && !after_p)
1424     {
1425       if (!flag_asynchronous_unwind_tables)
1426         dwarf2out_args_size ("", args_size);
1427       return;
1428     }
1429   else if (BARRIER_P (insn))
1430     {
1431       /* Don't call compute_barrier_args_size () if the only
1432          BARRIER is at the end of function.  */
1433       if (barrier_args_size == NULL && next_nonnote_insn (insn))
1434         compute_barrier_args_size ();
1435       if (barrier_args_size == NULL)
1436         offset = 0;
1437       else
1438         {
1439           offset = barrier_args_size[INSN_UID (insn)];
1440           if (offset < 0)
1441             offset = 0;
1442         }
1443
1444       offset -= args_size;
1445 #ifndef STACK_GROWS_DOWNWARD
1446       offset = -offset;
1447 #endif
1448     }
1449   else if (GET_CODE (PATTERN (insn)) == SET)
1450     offset = stack_adjust_offset (PATTERN (insn), args_size, 0);
1451   else if (GET_CODE (PATTERN (insn)) == PARALLEL
1452            || GET_CODE (PATTERN (insn)) == SEQUENCE)
1453     {
1454       /* There may be stack adjustments inside compound insns.  Search
1455          for them.  */
1456       for (offset = 0, i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1457         if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1458           offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i),
1459                                          args_size, offset);
1460     }
1461   else
1462     return;
1463
1464   if (offset == 0)
1465     return;
1466
1467   label = dwarf2out_cfi_label ();
1468   dwarf2out_args_size_adjust (offset, label);
1469 }
1470
1471 /* Adjust args_size based on stack adjustment OFFSET.  */
1472
1473 static void
1474 dwarf2out_args_size_adjust (HOST_WIDE_INT offset, const char *label)
1475 {
1476   if (cfa.reg == STACK_POINTER_REGNUM)
1477     cfa.offset += offset;
1478
1479   if (cfa_store.reg == STACK_POINTER_REGNUM)
1480     cfa_store.offset += offset;
1481
1482 #ifndef STACK_GROWS_DOWNWARD
1483   offset = -offset;
1484 #endif
1485
1486   args_size += offset;
1487   if (args_size < 0)
1488     args_size = 0;
1489
1490   def_cfa_1 (label, &cfa);
1491   if (flag_asynchronous_unwind_tables)
1492     dwarf2out_args_size (label, args_size);
1493 }
1494
1495 #endif
1496
1497 /* We delay emitting a register save until either (a) we reach the end
1498    of the prologue or (b) the register is clobbered.  This clusters
1499    register saves so that there are fewer pc advances.  */
1500
1501 struct queued_reg_save GTY(())
1502 {
1503   struct queued_reg_save *next;
1504   rtx reg;
1505   HOST_WIDE_INT cfa_offset;
1506   rtx saved_reg;
1507 };
1508
1509 static GTY(()) struct queued_reg_save *queued_reg_saves;
1510
1511 /* The caller's ORIG_REG is saved in SAVED_IN_REG.  */
1512 struct reg_saved_in_data GTY(()) {
1513   rtx orig_reg;
1514   rtx saved_in_reg;
1515 };
1516
1517 /* A list of registers saved in other registers.
1518    The list intentionally has a small maximum capacity of 4; if your
1519    port needs more than that, you might consider implementing a
1520    more efficient data structure.  */
1521 static GTY(()) struct reg_saved_in_data regs_saved_in_regs[4];
1522 static GTY(()) size_t num_regs_saved_in_regs;
1523
1524 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
1525 static const char *last_reg_save_label;
1526
1527 /* Add an entry to QUEUED_REG_SAVES saying that REG is now saved at
1528    SREG, or if SREG is NULL then it is saved at OFFSET to the CFA.  */
1529
1530 static void
1531 queue_reg_save (const char *label, rtx reg, rtx sreg, HOST_WIDE_INT offset)
1532 {
1533   struct queued_reg_save *q;
1534
1535   /* Duplicates waste space, but it's also necessary to remove them
1536      for correctness, since the queue gets output in reverse
1537      order.  */
1538   for (q = queued_reg_saves; q != NULL; q = q->next)
1539     if (REGNO (q->reg) == REGNO (reg))
1540       break;
1541
1542   if (q == NULL)
1543     {
1544       q = GGC_NEW (struct queued_reg_save);
1545       q->next = queued_reg_saves;
1546       queued_reg_saves = q;
1547     }
1548
1549   q->reg = reg;
1550   q->cfa_offset = offset;
1551   q->saved_reg = sreg;
1552
1553   last_reg_save_label = label;
1554 }
1555
1556 /* Output all the entries in QUEUED_REG_SAVES.  */
1557
1558 static void
1559 flush_queued_reg_saves (void)
1560 {
1561   struct queued_reg_save *q;
1562
1563   for (q = queued_reg_saves; q; q = q->next)
1564     {
1565       size_t i;
1566       unsigned int reg, sreg;
1567
1568       for (i = 0; i < num_regs_saved_in_regs; i++)
1569         if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (q->reg))
1570           break;
1571       if (q->saved_reg && i == num_regs_saved_in_regs)
1572         {
1573           gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1574           num_regs_saved_in_regs++;
1575         }
1576       if (i != num_regs_saved_in_regs)
1577         {
1578           regs_saved_in_regs[i].orig_reg = q->reg;
1579           regs_saved_in_regs[i].saved_in_reg = q->saved_reg;
1580         }
1581
1582       reg = DWARF_FRAME_REGNUM (REGNO (q->reg));
1583       if (q->saved_reg)
1584         sreg = DWARF_FRAME_REGNUM (REGNO (q->saved_reg));
1585       else
1586         sreg = INVALID_REGNUM;
1587       reg_save (last_reg_save_label, reg, sreg, q->cfa_offset);
1588     }
1589
1590   queued_reg_saves = NULL;
1591   last_reg_save_label = NULL;
1592 }
1593
1594 /* Does INSN clobber any register which QUEUED_REG_SAVES lists a saved
1595    location for?  Or, does it clobber a register which we've previously
1596    said that some other register is saved in, and for which we now
1597    have a new location for?  */
1598
1599 static bool
1600 clobbers_queued_reg_save (const_rtx insn)
1601 {
1602   struct queued_reg_save *q;
1603
1604   for (q = queued_reg_saves; q; q = q->next)
1605     {
1606       size_t i;
1607       if (modified_in_p (q->reg, insn))
1608         return true;
1609       for (i = 0; i < num_regs_saved_in_regs; i++)
1610         if (REGNO (q->reg) == REGNO (regs_saved_in_regs[i].orig_reg)
1611             && modified_in_p (regs_saved_in_regs[i].saved_in_reg, insn))
1612           return true;
1613     }
1614
1615   return false;
1616 }
1617
1618 /* Entry point for saving the first register into the second.  */
1619
1620 void
1621 dwarf2out_reg_save_reg (const char *label, rtx reg, rtx sreg)
1622 {
1623   size_t i;
1624   unsigned int regno, sregno;
1625
1626   for (i = 0; i < num_regs_saved_in_regs; i++)
1627     if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (reg))
1628       break;
1629   if (i == num_regs_saved_in_regs)
1630     {
1631       gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1632       num_regs_saved_in_regs++;
1633     }
1634   regs_saved_in_regs[i].orig_reg = reg;
1635   regs_saved_in_regs[i].saved_in_reg = sreg;
1636
1637   regno = DWARF_FRAME_REGNUM (REGNO (reg));
1638   sregno = DWARF_FRAME_REGNUM (REGNO (sreg));
1639   reg_save (label, regno, sregno, 0);
1640 }
1641
1642 /* What register, if any, is currently saved in REG?  */
1643
1644 static rtx
1645 reg_saved_in (rtx reg)
1646 {
1647   unsigned int regn = REGNO (reg);
1648   size_t i;
1649   struct queued_reg_save *q;
1650
1651   for (q = queued_reg_saves; q; q = q->next)
1652     if (q->saved_reg && regn == REGNO (q->saved_reg))
1653       return q->reg;
1654
1655   for (i = 0; i < num_regs_saved_in_regs; i++)
1656     if (regs_saved_in_regs[i].saved_in_reg
1657         && regn == REGNO (regs_saved_in_regs[i].saved_in_reg))
1658       return regs_saved_in_regs[i].orig_reg;
1659
1660   return NULL_RTX;
1661 }
1662
1663
1664 /* A temporary register holding an integral value used in adjusting SP
1665    or setting up the store_reg.  The "offset" field holds the integer
1666    value, not an offset.  */
1667 static dw_cfa_location cfa_temp;
1668
1669 /* Record call frame debugging information for an expression EXPR,
1670    which either sets SP or FP (adjusting how we calculate the frame
1671    address) or saves a register to the stack or another register.
1672    LABEL indicates the address of EXPR.
1673
1674    This function encodes a state machine mapping rtxes to actions on
1675    cfa, cfa_store, and cfa_temp.reg.  We describe these rules so
1676    users need not read the source code.
1677
1678   The High-Level Picture
1679
1680   Changes in the register we use to calculate the CFA: Currently we
1681   assume that if you copy the CFA register into another register, we
1682   should take the other one as the new CFA register; this seems to
1683   work pretty well.  If it's wrong for some target, it's simple
1684   enough not to set RTX_FRAME_RELATED_P on the insn in question.
1685
1686   Changes in the register we use for saving registers to the stack:
1687   This is usually SP, but not always.  Again, we deduce that if you
1688   copy SP into another register (and SP is not the CFA register),
1689   then the new register is the one we will be using for register
1690   saves.  This also seems to work.
1691
1692   Register saves: There's not much guesswork about this one; if
1693   RTX_FRAME_RELATED_P is set on an insn which modifies memory, it's a
1694   register save, and the register used to calculate the destination
1695   had better be the one we think we're using for this purpose.
1696   It's also assumed that a copy from a call-saved register to another
1697   register is saving that register if RTX_FRAME_RELATED_P is set on
1698   that instruction.  If the copy is from a call-saved register to
1699   the *same* register, that means that the register is now the same
1700   value as in the caller.
1701
1702   Except: If the register being saved is the CFA register, and the
1703   offset is nonzero, we are saving the CFA, so we assume we have to
1704   use DW_CFA_def_cfa_expression.  If the offset is 0, we assume that
1705   the intent is to save the value of SP from the previous frame.
1706
1707   In addition, if a register has previously been saved to a different
1708   register,
1709
1710   Invariants / Summaries of Rules
1711
1712   cfa          current rule for calculating the CFA.  It usually
1713                consists of a register and an offset.
1714   cfa_store    register used by prologue code to save things to the stack
1715                cfa_store.offset is the offset from the value of
1716                cfa_store.reg to the actual CFA
1717   cfa_temp     register holding an integral value.  cfa_temp.offset
1718                stores the value, which will be used to adjust the
1719                stack pointer.  cfa_temp is also used like cfa_store,
1720                to track stores to the stack via fp or a temp reg.
1721
1722   Rules  1- 4: Setting a register's value to cfa.reg or an expression
1723                with cfa.reg as the first operand changes the cfa.reg and its
1724                cfa.offset.  Rule 1 and 4 also set cfa_temp.reg and
1725                cfa_temp.offset.
1726
1727   Rules  6- 9: Set a non-cfa.reg register value to a constant or an
1728                expression yielding a constant.  This sets cfa_temp.reg
1729                and cfa_temp.offset.
1730
1731   Rule 5:      Create a new register cfa_store used to save items to the
1732                stack.
1733
1734   Rules 10-14: Save a register to the stack.  Define offset as the
1735                difference of the original location and cfa_store's
1736                location (or cfa_temp's location if cfa_temp is used).
1737
1738   Rules 16-20: If AND operation happens on sp in prologue, we assume
1739                stack is realigned.  We will use a group of DW_OP_XXX
1740                expressions to represent the location of the stored
1741                register instead of CFA+offset.
1742
1743   The Rules
1744
1745   "{a,b}" indicates a choice of a xor b.
1746   "<reg>:cfa.reg" indicates that <reg> must equal cfa.reg.
1747
1748   Rule 1:
1749   (set <reg1> <reg2>:cfa.reg)
1750   effects: cfa.reg = <reg1>
1751            cfa.offset unchanged
1752            cfa_temp.reg = <reg1>
1753            cfa_temp.offset = cfa.offset
1754
1755   Rule 2:
1756   (set sp ({minus,plus,losum} {sp,fp}:cfa.reg
1757                               {<const_int>,<reg>:cfa_temp.reg}))
1758   effects: cfa.reg = sp if fp used
1759            cfa.offset += {+/- <const_int>, cfa_temp.offset} if cfa.reg==sp
1760            cfa_store.offset += {+/- <const_int>, cfa_temp.offset}
1761              if cfa_store.reg==sp
1762
1763   Rule 3:
1764   (set fp ({minus,plus,losum} <reg>:cfa.reg <const_int>))
1765   effects: cfa.reg = fp
1766            cfa_offset += +/- <const_int>
1767
1768   Rule 4:
1769   (set <reg1> ({plus,losum} <reg2>:cfa.reg <const_int>))
1770   constraints: <reg1> != fp
1771                <reg1> != sp
1772   effects: cfa.reg = <reg1>
1773            cfa_temp.reg = <reg1>
1774            cfa_temp.offset = cfa.offset
1775
1776   Rule 5:
1777   (set <reg1> (plus <reg2>:cfa_temp.reg sp:cfa.reg))
1778   constraints: <reg1> != fp
1779                <reg1> != sp
1780   effects: cfa_store.reg = <reg1>
1781            cfa_store.offset = cfa.offset - cfa_temp.offset
1782
1783   Rule 6:
1784   (set <reg> <const_int>)
1785   effects: cfa_temp.reg = <reg>
1786            cfa_temp.offset = <const_int>
1787
1788   Rule 7:
1789   (set <reg1>:cfa_temp.reg (ior <reg2>:cfa_temp.reg <const_int>))
1790   effects: cfa_temp.reg = <reg1>
1791            cfa_temp.offset |= <const_int>
1792
1793   Rule 8:
1794   (set <reg> (high <exp>))
1795   effects: none
1796
1797   Rule 9:
1798   (set <reg> (lo_sum <exp> <const_int>))
1799   effects: cfa_temp.reg = <reg>
1800            cfa_temp.offset = <const_int>
1801
1802   Rule 10:
1803   (set (mem (pre_modify sp:cfa_store (???? <reg1> <const_int>))) <reg2>)
1804   effects: cfa_store.offset -= <const_int>
1805            cfa.offset = cfa_store.offset if cfa.reg == sp
1806            cfa.reg = sp
1807            cfa.base_offset = -cfa_store.offset
1808
1809   Rule 11:
1810   (set (mem ({pre_inc,pre_dec} sp:cfa_store.reg)) <reg>)
1811   effects: cfa_store.offset += -/+ mode_size(mem)
1812            cfa.offset = cfa_store.offset if cfa.reg == sp
1813            cfa.reg = sp
1814            cfa.base_offset = -cfa_store.offset
1815
1816   Rule 12:
1817   (set (mem ({minus,plus,losum} <reg1>:{cfa_store,cfa_temp} <const_int>))
1818
1819        <reg2>)
1820   effects: cfa.reg = <reg1>
1821            cfa.base_offset = -/+ <const_int> - {cfa_store,cfa_temp}.offset
1822
1823   Rule 13:
1824   (set (mem <reg1>:{cfa_store,cfa_temp}) <reg2>)
1825   effects: cfa.reg = <reg1>
1826            cfa.base_offset = -{cfa_store,cfa_temp}.offset
1827
1828   Rule 14:
1829   (set (mem (postinc <reg1>:cfa_temp <const_int>)) <reg2>)
1830   effects: cfa.reg = <reg1>
1831            cfa.base_offset = -cfa_temp.offset
1832            cfa_temp.offset -= mode_size(mem)
1833
1834   Rule 15:
1835   (set <reg> {unspec, unspec_volatile})
1836   effects: target-dependent
1837
1838   Rule 16:
1839   (set sp (and: sp <const_int>))
1840   constraints: cfa_store.reg == sp
1841   effects: current_fde.stack_realign = 1
1842            cfa_store.offset = 0
1843            fde->drap_reg = cfa.reg if cfa.reg != sp and cfa.reg != fp
1844
1845   Rule 17:
1846   (set (mem ({pre_inc, pre_dec} sp)) (mem (plus (cfa.reg) (const_int))))
1847   effects: cfa_store.offset += -/+ mode_size(mem)
1848
1849   Rule 18:
1850   (set (mem ({pre_inc, pre_dec} sp)) fp)
1851   constraints: fde->stack_realign == 1
1852   effects: cfa_store.offset = 0
1853            cfa.reg != HARD_FRAME_POINTER_REGNUM
1854
1855   Rule 19:
1856   (set (mem ({pre_inc, pre_dec} sp)) cfa.reg)
1857   constraints: fde->stack_realign == 1
1858                && cfa.offset == 0
1859                && cfa.indirect == 0
1860                && cfa.reg != HARD_FRAME_POINTER_REGNUM
1861   effects: Use DW_CFA_def_cfa_expression to define cfa
1862            cfa.reg == fde->drap_reg
1863
1864   Rule 20:
1865   (set reg fde->drap_reg)
1866   constraints: fde->vdrap_reg == INVALID_REGNUM
1867   effects: fde->vdrap_reg = reg.
1868   (set mem fde->drap_reg)
1869   constraints: fde->drap_reg_saved == 1
1870   effects: none.  */
1871
1872 static void
1873 dwarf2out_frame_debug_expr (rtx expr, const char *label)
1874 {
1875   rtx src, dest, span;
1876   HOST_WIDE_INT offset;
1877   dw_fde_ref fde;
1878
1879   /* If RTX_FRAME_RELATED_P is set on a PARALLEL, process each member of
1880      the PARALLEL independently. The first element is always processed if
1881      it is a SET. This is for backward compatibility.   Other elements
1882      are processed only if they are SETs and the RTX_FRAME_RELATED_P
1883      flag is set in them.  */
1884   if (GET_CODE (expr) == PARALLEL || GET_CODE (expr) == SEQUENCE)
1885     {
1886       int par_index;
1887       int limit = XVECLEN (expr, 0);
1888       rtx elem;
1889
1890       /* PARALLELs have strict read-modify-write semantics, so we
1891          ought to evaluate every rvalue before changing any lvalue.
1892          It's cumbersome to do that in general, but there's an
1893          easy approximation that is enough for all current users:
1894          handle register saves before register assignments.  */
1895       if (GET_CODE (expr) == PARALLEL)
1896         for (par_index = 0; par_index < limit; par_index++)
1897           {
1898             elem = XVECEXP (expr, 0, par_index);
1899             if (GET_CODE (elem) == SET
1900                 && MEM_P (SET_DEST (elem))
1901                 && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
1902               dwarf2out_frame_debug_expr (elem, label);
1903           }
1904
1905       for (par_index = 0; par_index < limit; par_index++)
1906         {
1907           elem = XVECEXP (expr, 0, par_index);
1908           if (GET_CODE (elem) == SET
1909               && (!MEM_P (SET_DEST (elem)) || GET_CODE (expr) == SEQUENCE)
1910               && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
1911             dwarf2out_frame_debug_expr (elem, label);
1912           else if (GET_CODE (elem) == SET
1913                    && par_index != 0
1914                    && !RTX_FRAME_RELATED_P (elem))
1915             {
1916               /* Stack adjustment combining might combine some post-prologue
1917                  stack adjustment into a prologue stack adjustment.  */
1918               HOST_WIDE_INT offset = stack_adjust_offset (elem, args_size, 0);
1919
1920               if (offset != 0)
1921                 dwarf2out_args_size_adjust (offset, label);
1922             }
1923         }
1924       return;
1925     }
1926
1927   gcc_assert (GET_CODE (expr) == SET);
1928
1929   src = SET_SRC (expr);
1930   dest = SET_DEST (expr);
1931
1932   if (REG_P (src))
1933     {
1934       rtx rsi = reg_saved_in (src);
1935       if (rsi)
1936         src = rsi;
1937     }
1938
1939   fde = current_fde ();
1940
1941   if (GET_CODE (src) == REG
1942       && fde
1943       && fde->drap_reg == REGNO (src)
1944       && (fde->drap_reg_saved
1945           || GET_CODE (dest) == REG))
1946     {
1947       /* Rule 20 */
1948       /* If we are saving dynamic realign argument pointer to a
1949          register, the destination is virtual dynamic realign
1950          argument pointer.  It may be used to access argument.  */
1951       if (GET_CODE (dest) == REG)
1952         {
1953           gcc_assert (fde->vdrap_reg == INVALID_REGNUM);
1954           fde->vdrap_reg = REGNO (dest);
1955         }
1956       return;
1957     }
1958
1959   switch (GET_CODE (dest))
1960     {
1961     case REG:
1962       switch (GET_CODE (src))
1963         {
1964           /* Setting FP from SP.  */
1965         case REG:
1966           if (cfa.reg == (unsigned) REGNO (src))
1967             {
1968               /* Rule 1 */
1969               /* Update the CFA rule wrt SP or FP.  Make sure src is
1970                  relative to the current CFA register.
1971
1972                  We used to require that dest be either SP or FP, but the
1973                  ARM copies SP to a temporary register, and from there to
1974                  FP.  So we just rely on the backends to only set
1975                  RTX_FRAME_RELATED_P on appropriate insns.  */
1976               cfa.reg = REGNO (dest);
1977               cfa_temp.reg = cfa.reg;
1978               cfa_temp.offset = cfa.offset;
1979             }
1980           else
1981             {
1982               /* Saving a register in a register.  */
1983               gcc_assert (!fixed_regs [REGNO (dest)]
1984                           /* For the SPARC and its register window.  */
1985                           || (DWARF_FRAME_REGNUM (REGNO (src))
1986                               == DWARF_FRAME_RETURN_COLUMN));
1987
1988               /* After stack is aligned, we can only save SP in FP
1989                  if drap register is used.  In this case, we have
1990                  to restore stack pointer with the CFA value and we
1991                  don't generate this DWARF information.  */
1992               if (fde
1993                   && fde->stack_realign
1994                   && REGNO (src) == STACK_POINTER_REGNUM)
1995                 gcc_assert (REGNO (dest) == HARD_FRAME_POINTER_REGNUM
1996                             && fde->drap_reg != INVALID_REGNUM
1997                             && cfa.reg != REGNO (src));
1998               else
1999                 queue_reg_save (label, src, dest, 0);
2000             }
2001           break;
2002
2003         case PLUS:
2004         case MINUS:
2005         case LO_SUM:
2006           if (dest == stack_pointer_rtx)
2007             {
2008               /* Rule 2 */
2009               /* Adjusting SP.  */
2010               switch (GET_CODE (XEXP (src, 1)))
2011                 {
2012                 case CONST_INT:
2013                   offset = INTVAL (XEXP (src, 1));
2014                   break;
2015                 case REG:
2016                   gcc_assert ((unsigned) REGNO (XEXP (src, 1))
2017                               == cfa_temp.reg);
2018                   offset = cfa_temp.offset;
2019                   break;
2020                 default:
2021                   gcc_unreachable ();
2022                 }
2023
2024               if (XEXP (src, 0) == hard_frame_pointer_rtx)
2025                 {
2026                   /* Restoring SP from FP in the epilogue.  */
2027                   gcc_assert (cfa.reg == (unsigned) HARD_FRAME_POINTER_REGNUM);
2028                   cfa.reg = STACK_POINTER_REGNUM;
2029                 }
2030               else if (GET_CODE (src) == LO_SUM)
2031                 /* Assume we've set the source reg of the LO_SUM from sp.  */
2032                 ;
2033               else
2034                 gcc_assert (XEXP (src, 0) == stack_pointer_rtx);
2035
2036               if (GET_CODE (src) != MINUS)
2037                 offset = -offset;
2038               if (cfa.reg == STACK_POINTER_REGNUM)
2039                 cfa.offset += offset;
2040               if (cfa_store.reg == STACK_POINTER_REGNUM)
2041                 cfa_store.offset += offset;
2042             }
2043           else if (dest == hard_frame_pointer_rtx)
2044             {
2045               /* Rule 3 */
2046               /* Either setting the FP from an offset of the SP,
2047                  or adjusting the FP */
2048               gcc_assert (frame_pointer_needed);
2049
2050               gcc_assert (REG_P (XEXP (src, 0))
2051                           && (unsigned) REGNO (XEXP (src, 0)) == cfa.reg
2052                           && GET_CODE (XEXP (src, 1)) == CONST_INT);
2053               offset = INTVAL (XEXP (src, 1));
2054               if (GET_CODE (src) != MINUS)
2055                 offset = -offset;
2056               cfa.offset += offset;
2057               cfa.reg = HARD_FRAME_POINTER_REGNUM;
2058             }
2059           else
2060             {
2061               gcc_assert (GET_CODE (src) != MINUS);
2062
2063               /* Rule 4 */
2064               if (REG_P (XEXP (src, 0))
2065                   && REGNO (XEXP (src, 0)) == cfa.reg
2066                   && GET_CODE (XEXP (src, 1)) == CONST_INT)
2067                 {
2068                   /* Setting a temporary CFA register that will be copied
2069                      into the FP later on.  */
2070                   offset = - INTVAL (XEXP (src, 1));
2071                   cfa.offset += offset;
2072                   cfa.reg = REGNO (dest);
2073                   /* Or used to save regs to the stack.  */
2074                   cfa_temp.reg = cfa.reg;
2075                   cfa_temp.offset = cfa.offset;
2076                 }
2077
2078               /* Rule 5 */
2079               else if (REG_P (XEXP (src, 0))
2080                        && REGNO (XEXP (src, 0)) == cfa_temp.reg
2081                        && XEXP (src, 1) == stack_pointer_rtx)
2082                 {
2083                   /* Setting a scratch register that we will use instead
2084                      of SP for saving registers to the stack.  */
2085                   gcc_assert (cfa.reg == STACK_POINTER_REGNUM);
2086                   cfa_store.reg = REGNO (dest);
2087                   cfa_store.offset = cfa.offset - cfa_temp.offset;
2088                 }
2089
2090               /* Rule 9 */
2091               else if (GET_CODE (src) == LO_SUM
2092                        && GET_CODE (XEXP (src, 1)) == CONST_INT)
2093                 {
2094                   cfa_temp.reg = REGNO (dest);
2095                   cfa_temp.offset = INTVAL (XEXP (src, 1));
2096                 }
2097               else
2098                 gcc_unreachable ();
2099             }
2100           break;
2101
2102           /* Rule 6 */
2103         case CONST_INT:
2104           cfa_temp.reg = REGNO (dest);
2105           cfa_temp.offset = INTVAL (src);
2106           break;
2107
2108           /* Rule 7 */
2109         case IOR:
2110           gcc_assert (REG_P (XEXP (src, 0))
2111                       && (unsigned) REGNO (XEXP (src, 0)) == cfa_temp.reg
2112                       && GET_CODE (XEXP (src, 1)) == CONST_INT);
2113
2114           if ((unsigned) REGNO (dest) != cfa_temp.reg)
2115             cfa_temp.reg = REGNO (dest);
2116           cfa_temp.offset |= INTVAL (XEXP (src, 1));
2117           break;
2118
2119           /* Skip over HIGH, assuming it will be followed by a LO_SUM,
2120              which will fill in all of the bits.  */
2121           /* Rule 8 */
2122         case HIGH:
2123           break;
2124
2125           /* Rule 15 */
2126         case UNSPEC:
2127         case UNSPEC_VOLATILE:
2128           gcc_assert (targetm.dwarf_handle_frame_unspec);
2129           targetm.dwarf_handle_frame_unspec (label, expr, XINT (src, 1));
2130           return;
2131
2132           /* Rule 16 */
2133         case AND:
2134           /* If this AND operation happens on stack pointer in prologue,
2135              we assume the stack is realigned and we extract the
2136              alignment.  */
2137           if (fde && XEXP (src, 0) == stack_pointer_rtx)
2138             {
2139               gcc_assert (cfa_store.reg == REGNO (XEXP (src, 0)));
2140               fde->stack_realign = 1;
2141               fde->stack_realignment = INTVAL (XEXP (src, 1));
2142               cfa_store.offset = 0;
2143
2144               if (cfa.reg != STACK_POINTER_REGNUM
2145                   && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2146                 fde->drap_reg = cfa.reg;
2147             }
2148           return;
2149
2150         default:
2151           gcc_unreachable ();
2152         }
2153
2154       def_cfa_1 (label, &cfa);
2155       break;
2156
2157     case MEM:
2158
2159       /* Saving a register to the stack.  Make sure dest is relative to the
2160          CFA register.  */
2161       switch (GET_CODE (XEXP (dest, 0)))
2162         {
2163           /* Rule 10 */
2164           /* With a push.  */
2165         case PRE_MODIFY:
2166           /* We can't handle variable size modifications.  */
2167           gcc_assert (GET_CODE (XEXP (XEXP (XEXP (dest, 0), 1), 1))
2168                       == CONST_INT);
2169           offset = -INTVAL (XEXP (XEXP (XEXP (dest, 0), 1), 1));
2170
2171           gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
2172                       && cfa_store.reg == STACK_POINTER_REGNUM);
2173
2174           cfa_store.offset += offset;
2175           if (cfa.reg == STACK_POINTER_REGNUM)
2176             cfa.offset = cfa_store.offset;
2177
2178           offset = -cfa_store.offset;
2179           break;
2180
2181           /* Rule 11 */
2182         case PRE_INC:
2183         case PRE_DEC:
2184           offset = GET_MODE_SIZE (GET_MODE (dest));
2185           if (GET_CODE (XEXP (dest, 0)) == PRE_INC)
2186             offset = -offset;
2187
2188           gcc_assert ((REGNO (XEXP (XEXP (dest, 0), 0))
2189                        == STACK_POINTER_REGNUM)
2190                       && cfa_store.reg == STACK_POINTER_REGNUM);
2191
2192           cfa_store.offset += offset;
2193
2194           /* Rule 18: If stack is aligned, we will use FP as a
2195              reference to represent the address of the stored
2196              regiser.  */
2197           if (fde
2198               && fde->stack_realign
2199               && src == hard_frame_pointer_rtx)
2200             {
2201               gcc_assert (cfa.reg != HARD_FRAME_POINTER_REGNUM);
2202               cfa_store.offset = 0;
2203             }
2204
2205           if (cfa.reg == STACK_POINTER_REGNUM)
2206             cfa.offset = cfa_store.offset;
2207
2208           offset = -cfa_store.offset;
2209           break;
2210
2211           /* Rule 12 */
2212           /* With an offset.  */
2213         case PLUS:
2214         case MINUS:
2215         case LO_SUM:
2216           {
2217             int regno;
2218
2219             gcc_assert (GET_CODE (XEXP (XEXP (dest, 0), 1)) == CONST_INT
2220                         && REG_P (XEXP (XEXP (dest, 0), 0)));
2221             offset = INTVAL (XEXP (XEXP (dest, 0), 1));
2222             if (GET_CODE (XEXP (dest, 0)) == MINUS)
2223               offset = -offset;
2224
2225             regno = REGNO (XEXP (XEXP (dest, 0), 0));
2226
2227             if (cfa_store.reg == (unsigned) regno)
2228               offset -= cfa_store.offset;
2229             else
2230               {
2231                 gcc_assert (cfa_temp.reg == (unsigned) regno);
2232                 offset -= cfa_temp.offset;
2233               }
2234           }
2235           break;
2236
2237           /* Rule 13 */
2238           /* Without an offset.  */
2239         case REG:
2240           {
2241             int regno = REGNO (XEXP (dest, 0));
2242
2243             if (cfa_store.reg == (unsigned) regno)
2244               offset = -cfa_store.offset;
2245             else
2246               {
2247                 gcc_assert (cfa_temp.reg == (unsigned) regno);
2248                 offset = -cfa_temp.offset;
2249               }
2250           }
2251           break;
2252
2253           /* Rule 14 */
2254         case POST_INC:
2255           gcc_assert (cfa_temp.reg
2256                       == (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)));
2257           offset = -cfa_temp.offset;
2258           cfa_temp.offset -= GET_MODE_SIZE (GET_MODE (dest));
2259           break;
2260
2261         default:
2262           gcc_unreachable ();
2263         }
2264
2265         /* Rule 17 */
2266         /* If the source operand of this MEM operation is not a
2267            register, basically the source is return address.  Here
2268            we only care how much stack grew and we don't save it.  */
2269       if (!REG_P (src))
2270         break;
2271
2272       if (REGNO (src) != STACK_POINTER_REGNUM
2273           && REGNO (src) != HARD_FRAME_POINTER_REGNUM
2274           && (unsigned) REGNO (src) == cfa.reg)
2275         {
2276           /* We're storing the current CFA reg into the stack.  */
2277
2278           if (cfa.offset == 0)
2279             {
2280               /* Rule 19 */
2281               /* If stack is aligned, putting CFA reg into stack means
2282                  we can no longer use reg + offset to represent CFA.
2283                  Here we use DW_CFA_def_cfa_expression instead.  The
2284                  result of this expression equals to the original CFA
2285                  value.  */
2286               if (fde
2287                   && fde->stack_realign
2288                   && cfa.indirect == 0
2289                   && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2290                 {
2291                   dw_cfa_location cfa_exp;
2292
2293                   gcc_assert (fde->drap_reg == cfa.reg);
2294
2295                   cfa_exp.indirect = 1;
2296                   cfa_exp.reg = HARD_FRAME_POINTER_REGNUM;
2297                   cfa_exp.base_offset = offset;
2298                   cfa_exp.offset = 0;
2299
2300                   fde->drap_reg_saved = 1;
2301
2302                   def_cfa_1 (label, &cfa_exp);
2303                   break;
2304                 }
2305
2306               /* If the source register is exactly the CFA, assume
2307                  we're saving SP like any other register; this happens
2308                  on the ARM.  */
2309               def_cfa_1 (label, &cfa);
2310               queue_reg_save (label, stack_pointer_rtx, NULL_RTX, offset);
2311               break;
2312             }
2313           else
2314             {
2315               /* Otherwise, we'll need to look in the stack to
2316                  calculate the CFA.  */
2317               rtx x = XEXP (dest, 0);
2318
2319               if (!REG_P (x))
2320                 x = XEXP (x, 0);
2321               gcc_assert (REG_P (x));
2322
2323               cfa.reg = REGNO (x);
2324               cfa.base_offset = offset;
2325               cfa.indirect = 1;
2326               def_cfa_1 (label, &cfa);
2327               break;
2328             }
2329         }
2330
2331       def_cfa_1 (label, &cfa);
2332       {
2333         span = targetm.dwarf_register_span (src);
2334
2335         if (!span)
2336           queue_reg_save (label, src, NULL_RTX, offset);
2337         else
2338           {
2339             /* We have a PARALLEL describing where the contents of SRC
2340                live.  Queue register saves for each piece of the
2341                PARALLEL.  */
2342             int par_index;
2343             int limit;
2344             HOST_WIDE_INT span_offset = offset;
2345
2346             gcc_assert (GET_CODE (span) == PARALLEL);
2347
2348             limit = XVECLEN (span, 0);
2349             for (par_index = 0; par_index < limit; par_index++)
2350               {
2351                 rtx elem = XVECEXP (span, 0, par_index);
2352
2353                 queue_reg_save (label, elem, NULL_RTX, span_offset);
2354                 span_offset += GET_MODE_SIZE (GET_MODE (elem));
2355               }
2356           }
2357       }
2358       break;
2359
2360     default:
2361       gcc_unreachable ();
2362     }
2363 }
2364
2365 /* Record call frame debugging information for INSN, which either
2366    sets SP or FP (adjusting how we calculate the frame address) or saves a
2367    register to the stack.  If INSN is NULL_RTX, initialize our state.
2368
2369    If AFTER_P is false, we're being called before the insn is emitted,
2370    otherwise after.  Call instructions get invoked twice.  */
2371
2372 void
2373 dwarf2out_frame_debug (rtx insn, bool after_p)
2374 {
2375   const char *label;
2376   rtx src;
2377
2378   if (insn == NULL_RTX)
2379     {
2380       size_t i;
2381
2382       /* Flush any queued register saves.  */
2383       flush_queued_reg_saves ();
2384
2385       /* Set up state for generating call frame debug info.  */
2386       lookup_cfa (&cfa);
2387       gcc_assert (cfa.reg
2388                   == (unsigned long)DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM));
2389
2390       cfa.reg = STACK_POINTER_REGNUM;
2391       cfa_store = cfa;
2392       cfa_temp.reg = -1;
2393       cfa_temp.offset = 0;
2394
2395       for (i = 0; i < num_regs_saved_in_regs; i++)
2396         {
2397           regs_saved_in_regs[i].orig_reg = NULL_RTX;
2398           regs_saved_in_regs[i].saved_in_reg = NULL_RTX;
2399         }
2400       num_regs_saved_in_regs = 0;
2401
2402       if (barrier_args_size)
2403         {
2404           XDELETEVEC (barrier_args_size);
2405           barrier_args_size = NULL;
2406         }
2407       return;
2408     }
2409
2410   if (!NONJUMP_INSN_P (insn) || clobbers_queued_reg_save (insn))
2411     flush_queued_reg_saves ();
2412
2413   if (! RTX_FRAME_RELATED_P (insn))
2414     {
2415       if (!ACCUMULATE_OUTGOING_ARGS)
2416         dwarf2out_stack_adjust (insn, after_p);
2417       return;
2418     }
2419
2420   label = dwarf2out_cfi_label ();
2421   src = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
2422   if (src)
2423     insn = XEXP (src, 0);
2424   else
2425     insn = PATTERN (insn);
2426
2427   dwarf2out_frame_debug_expr (insn, label);
2428 }
2429
2430 #endif
2431
2432 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used.  */
2433 static enum dw_cfi_oprnd_type dw_cfi_oprnd1_desc
2434  (enum dwarf_call_frame_info cfi);
2435
2436 static enum dw_cfi_oprnd_type
2437 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
2438 {
2439   switch (cfi)
2440     {
2441     case DW_CFA_nop:
2442     case DW_CFA_GNU_window_save:
2443       return dw_cfi_oprnd_unused;
2444
2445     case DW_CFA_set_loc:
2446     case DW_CFA_advance_loc1:
2447     case DW_CFA_advance_loc2:
2448     case DW_CFA_advance_loc4:
2449     case DW_CFA_MIPS_advance_loc8:
2450       return dw_cfi_oprnd_addr;
2451
2452     case DW_CFA_offset:
2453     case DW_CFA_offset_extended:
2454     case DW_CFA_def_cfa:
2455     case DW_CFA_offset_extended_sf:
2456     case DW_CFA_def_cfa_sf:
2457     case DW_CFA_restore_extended:
2458     case DW_CFA_undefined:
2459     case DW_CFA_same_value:
2460     case DW_CFA_def_cfa_register:
2461     case DW_CFA_register:
2462       return dw_cfi_oprnd_reg_num;
2463
2464     case DW_CFA_def_cfa_offset:
2465     case DW_CFA_GNU_args_size:
2466     case DW_CFA_def_cfa_offset_sf:
2467       return dw_cfi_oprnd_offset;
2468
2469     case DW_CFA_def_cfa_expression:
2470     case DW_CFA_expression:
2471       return dw_cfi_oprnd_loc;
2472
2473     default:
2474       gcc_unreachable ();
2475     }
2476 }
2477
2478 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used.  */
2479 static enum dw_cfi_oprnd_type dw_cfi_oprnd2_desc
2480  (enum dwarf_call_frame_info cfi);
2481
2482 static enum dw_cfi_oprnd_type
2483 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
2484 {
2485   switch (cfi)
2486     {
2487     case DW_CFA_def_cfa:
2488     case DW_CFA_def_cfa_sf:
2489     case DW_CFA_offset:
2490     case DW_CFA_offset_extended_sf:
2491     case DW_CFA_offset_extended:
2492       return dw_cfi_oprnd_offset;
2493
2494     case DW_CFA_register:
2495       return dw_cfi_oprnd_reg_num;
2496
2497     default:
2498       return dw_cfi_oprnd_unused;
2499     }
2500 }
2501
2502 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
2503
2504 /* Switch to eh_frame_section.  If we don't have an eh_frame_section,
2505    switch to the data section instead, and write out a synthetic label
2506    for collect2.  */
2507
2508 static void
2509 switch_to_eh_frame_section (void)
2510 {
2511   tree label;
2512
2513 #ifdef EH_FRAME_SECTION_NAME
2514   if (eh_frame_section == 0)
2515     {
2516       int flags;
2517
2518       if (EH_TABLES_CAN_BE_READ_ONLY)
2519         {
2520           int fde_encoding;
2521           int per_encoding;
2522           int lsda_encoding;
2523
2524           fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
2525                                                        /*global=*/0);
2526           per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
2527                                                        /*global=*/1);
2528           lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
2529                                                         /*global=*/0);
2530           flags = ((! flag_pic
2531                     || ((fde_encoding & 0x70) != DW_EH_PE_absptr
2532                         && (fde_encoding & 0x70) != DW_EH_PE_aligned
2533                         && (per_encoding & 0x70) != DW_EH_PE_absptr
2534                         && (per_encoding & 0x70) != DW_EH_PE_aligned
2535                         && (lsda_encoding & 0x70) != DW_EH_PE_absptr
2536                         && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
2537                    ? 0 : SECTION_WRITE);
2538         }
2539       else
2540         flags = SECTION_WRITE;
2541       eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
2542     }
2543 #endif
2544
2545   if (eh_frame_section)
2546     switch_to_section (eh_frame_section);
2547   else
2548     {
2549       /* We have no special eh_frame section.  Put the information in
2550          the data section and emit special labels to guide collect2.  */
2551       switch_to_section (data_section);
2552       label = get_file_function_name ("F");
2553       ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
2554       targetm.asm_out.globalize_label (asm_out_file,
2555                                        IDENTIFIER_POINTER (label));
2556       ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
2557     }
2558 }
2559
2560 /* Divide OFF by DWARF_CIE_DATA_ALIGNMENT, asserting no remainder.  */
2561
2562 static HOST_WIDE_INT
2563 div_data_align (HOST_WIDE_INT off)
2564 {
2565   HOST_WIDE_INT r = off / DWARF_CIE_DATA_ALIGNMENT;
2566   gcc_assert (r * DWARF_CIE_DATA_ALIGNMENT == off);
2567   return r;
2568 }
2569
2570 /* Output a Call Frame Information opcode and its operand(s).  */
2571
2572 static void
2573 output_cfi (dw_cfi_ref cfi, dw_fde_ref fde, int for_eh)
2574 {
2575   unsigned long r;
2576   HOST_WIDE_INT off;
2577
2578   if (cfi->dw_cfi_opc == DW_CFA_advance_loc)
2579     dw2_asm_output_data (1, (cfi->dw_cfi_opc
2580                              | (cfi->dw_cfi_oprnd1.dw_cfi_offset & 0x3f)),
2581                          "DW_CFA_advance_loc " HOST_WIDE_INT_PRINT_HEX,
2582                          ((unsigned HOST_WIDE_INT)
2583                           cfi->dw_cfi_oprnd1.dw_cfi_offset));
2584   else if (cfi->dw_cfi_opc == DW_CFA_offset)
2585     {
2586       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2587       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
2588                            "DW_CFA_offset, column 0x%lx", r);
2589       off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
2590       dw2_asm_output_data_uleb128 (off, NULL);
2591     }
2592   else if (cfi->dw_cfi_opc == DW_CFA_restore)
2593     {
2594       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2595       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
2596                            "DW_CFA_restore, column 0x%lx", r);
2597     }
2598   else
2599     {
2600       dw2_asm_output_data (1, cfi->dw_cfi_opc,
2601                            "%s", dwarf_cfi_name (cfi->dw_cfi_opc));
2602
2603       switch (cfi->dw_cfi_opc)
2604         {
2605         case DW_CFA_set_loc:
2606           if (for_eh)
2607             dw2_asm_output_encoded_addr_rtx (
2608                 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0),
2609                 gen_rtx_SYMBOL_REF (Pmode, cfi->dw_cfi_oprnd1.dw_cfi_addr),
2610                 false, NULL);
2611           else
2612             dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2613                                  cfi->dw_cfi_oprnd1.dw_cfi_addr, NULL);
2614           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2615           break;
2616
2617         case DW_CFA_advance_loc1:
2618           dw2_asm_output_delta (1, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2619                                 fde->dw_fde_current_label, NULL);
2620           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2621           break;
2622
2623         case DW_CFA_advance_loc2:
2624           dw2_asm_output_delta (2, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2625                                 fde->dw_fde_current_label, NULL);
2626           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2627           break;
2628
2629         case DW_CFA_advance_loc4:
2630           dw2_asm_output_delta (4, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2631                                 fde->dw_fde_current_label, NULL);
2632           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2633           break;
2634
2635         case DW_CFA_MIPS_advance_loc8:
2636           dw2_asm_output_delta (8, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2637                                 fde->dw_fde_current_label, NULL);
2638           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2639           break;
2640
2641         case DW_CFA_offset_extended:
2642           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2643           dw2_asm_output_data_uleb128 (r, NULL);
2644           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
2645           dw2_asm_output_data_uleb128 (off, NULL);
2646           break;
2647
2648         case DW_CFA_def_cfa:
2649           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2650           dw2_asm_output_data_uleb128 (r, NULL);
2651           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
2652           break;
2653
2654         case DW_CFA_offset_extended_sf:
2655           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2656           dw2_asm_output_data_uleb128 (r, NULL);
2657           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
2658           dw2_asm_output_data_sleb128 (off, NULL);
2659           break;
2660
2661         case DW_CFA_def_cfa_sf:
2662           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2663           dw2_asm_output_data_uleb128 (r, NULL);
2664           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
2665           dw2_asm_output_data_sleb128 (off, NULL);
2666           break;
2667
2668         case DW_CFA_restore_extended:
2669         case DW_CFA_undefined:
2670         case DW_CFA_same_value:
2671         case DW_CFA_def_cfa_register:
2672           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2673           dw2_asm_output_data_uleb128 (r, NULL);
2674           break;
2675
2676         case DW_CFA_register:
2677           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2678           dw2_asm_output_data_uleb128 (r, NULL);
2679           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, for_eh);
2680           dw2_asm_output_data_uleb128 (r, NULL);
2681           break;
2682
2683         case DW_CFA_def_cfa_offset:
2684         case DW_CFA_GNU_args_size:
2685           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
2686           break;
2687
2688         case DW_CFA_def_cfa_offset_sf:
2689           off = div_data_align (cfi->dw_cfi_oprnd1.dw_cfi_offset);
2690           dw2_asm_output_data_sleb128 (off, NULL);
2691           break;
2692
2693         case DW_CFA_GNU_window_save:
2694           break;
2695
2696         case DW_CFA_def_cfa_expression:
2697         case DW_CFA_expression:
2698           output_cfa_loc (cfi);
2699           break;
2700
2701         case DW_CFA_GNU_negative_offset_extended:
2702           /* Obsoleted by DW_CFA_offset_extended_sf.  */
2703           gcc_unreachable ();
2704
2705         default:
2706           break;
2707         }
2708     }
2709 }
2710
2711 /* Similar, but do it via assembler directives instead.  */
2712
2713 static void
2714 output_cfi_directive (dw_cfi_ref cfi)
2715 {
2716   unsigned long r, r2;
2717
2718   switch (cfi->dw_cfi_opc)
2719     {
2720     case DW_CFA_advance_loc:
2721     case DW_CFA_advance_loc1:
2722     case DW_CFA_advance_loc2:
2723     case DW_CFA_advance_loc4:
2724     case DW_CFA_MIPS_advance_loc8:
2725     case DW_CFA_set_loc:
2726       /* Should only be created by add_fde_cfi in a code path not
2727          followed when emitting via directives.  The assembler is
2728          going to take care of this for us.  */
2729       gcc_unreachable ();
2730
2731     case DW_CFA_offset:
2732     case DW_CFA_offset_extended:
2733     case DW_CFA_offset_extended_sf:
2734       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 0);
2735       fprintf (asm_out_file, "\t.cfi_offset %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
2736                r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
2737       break;
2738
2739     case DW_CFA_restore:
2740     case DW_CFA_restore_extended:
2741       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 0);
2742       fprintf (asm_out_file, "\t.cfi_restore %lu\n", r);
2743       break;
2744
2745     case DW_CFA_undefined:
2746       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 0);
2747       fprintf (asm_out_file, "\t.cfi_undefined %lu\n", r);
2748       break;
2749
2750     case DW_CFA_same_value:
2751       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 0);
2752       fprintf (asm_out_file, "\t.cfi_same_value %lu\n", r);
2753       break;
2754
2755     case DW_CFA_def_cfa:
2756     case DW_CFA_def_cfa_sf:
2757       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 0);
2758       fprintf (asm_out_file, "\t.cfi_def_cfa %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
2759                r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
2760       break;
2761
2762     case DW_CFA_def_cfa_register:
2763       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 0);
2764       fprintf (asm_out_file, "\t.cfi_def_cfa_register %lu\n", r);
2765       break;
2766
2767     case DW_CFA_register:
2768       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 0);
2769       r2 = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, 0);
2770       fprintf (asm_out_file, "\t.cfi_register %lu, %lu\n", r, r2);
2771       break;
2772
2773     case DW_CFA_def_cfa_offset:
2774     case DW_CFA_def_cfa_offset_sf:
2775       fprintf (asm_out_file, "\t.cfi_def_cfa_offset "
2776                HOST_WIDE_INT_PRINT_DEC"\n",
2777                cfi->dw_cfi_oprnd1.dw_cfi_offset);
2778       break;
2779
2780     case DW_CFA_GNU_args_size:
2781       fprintf (asm_out_file, "\t.cfi_escape 0x%x,", DW_CFA_GNU_args_size);
2782       dw2_asm_output_data_uleb128_raw (cfi->dw_cfi_oprnd1.dw_cfi_offset);
2783       if (flag_debug_asm)
2784         fprintf (asm_out_file, "\t%s args_size "HOST_WIDE_INT_PRINT_DEC,
2785                  ASM_COMMENT_START, cfi->dw_cfi_oprnd1.dw_cfi_offset);
2786       fputc ('\n', asm_out_file);
2787       break;
2788
2789     case DW_CFA_GNU_window_save:
2790       fprintf (asm_out_file, "\t.cfi_window_save\n");
2791       break;
2792
2793     case DW_CFA_def_cfa_expression:
2794     case DW_CFA_expression:
2795       fprintf (asm_out_file, "\t.cfi_escape 0x%x,", cfi->dw_cfi_opc);
2796       output_cfa_loc_raw (cfi);
2797       fputc ('\n', asm_out_file);
2798       break;
2799
2800     default:
2801       gcc_unreachable ();
2802     }
2803 }
2804
2805 /* Output the call frame information used to record information
2806    that relates to calculating the frame pointer, and records the
2807    location of saved registers.  */
2808
2809 static void
2810 output_call_frame_info (int for_eh)
2811 {
2812   unsigned int i;
2813   dw_fde_ref fde;
2814   dw_cfi_ref cfi;
2815   char l1[20], l2[20], section_start_label[20];
2816   bool any_lsda_needed = false;
2817   char augmentation[6];
2818   int augmentation_size;
2819   int fde_encoding = DW_EH_PE_absptr;
2820   int per_encoding = DW_EH_PE_absptr;
2821   int lsda_encoding = DW_EH_PE_absptr;
2822   int return_reg;
2823
2824   /* Don't emit a CIE if there won't be any FDEs.  */
2825   if (fde_table_in_use == 0)
2826     return;
2827
2828   /* Nothing to do if the assembler's doing it all.  */
2829   if (dwarf2out_do_cfi_asm ())
2830     return;
2831
2832   /* If we make FDEs linkonce, we may have to emit an empty label for
2833      an FDE that wouldn't otherwise be emitted.  We want to avoid
2834      having an FDE kept around when the function it refers to is
2835      discarded.  Example where this matters: a primary function
2836      template in C++ requires EH information, but an explicit
2837      specialization doesn't.  */
2838   if (TARGET_USES_WEAK_UNWIND_INFO
2839       && ! flag_asynchronous_unwind_tables
2840       && flag_exceptions
2841       && for_eh)
2842     for (i = 0; i < fde_table_in_use; i++)
2843       if ((fde_table[i].nothrow || fde_table[i].all_throwers_are_sibcalls)
2844           && !fde_table[i].uses_eh_lsda
2845           && ! DECL_WEAK (fde_table[i].decl))
2846         targetm.asm_out.unwind_label (asm_out_file, fde_table[i].decl,
2847                                       for_eh, /* empty */ 1);
2848
2849   /* If we don't have any functions we'll want to unwind out of, don't
2850      emit any EH unwind information.  Note that if exceptions aren't
2851      enabled, we won't have collected nothrow information, and if we
2852      asked for asynchronous tables, we always want this info.  */
2853   if (for_eh)
2854     {
2855       bool any_eh_needed = !flag_exceptions || flag_asynchronous_unwind_tables;
2856
2857       for (i = 0; i < fde_table_in_use; i++)
2858         if (fde_table[i].uses_eh_lsda)
2859           any_eh_needed = any_lsda_needed = true;
2860         else if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
2861           any_eh_needed = true;
2862         else if (! fde_table[i].nothrow
2863                  && ! fde_table[i].all_throwers_are_sibcalls)
2864           any_eh_needed = true;
2865
2866       if (! any_eh_needed)
2867         return;
2868     }
2869
2870   /* We're going to be generating comments, so turn on app.  */
2871   if (flag_debug_asm)
2872     app_enable ();
2873
2874   if (for_eh)
2875     switch_to_eh_frame_section ();
2876   else
2877     {
2878       if (!debug_frame_section)
2879         debug_frame_section = get_section (DEBUG_FRAME_SECTION,
2880                                            SECTION_DEBUG, NULL);
2881       switch_to_section (debug_frame_section);
2882     }
2883
2884   ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
2885   ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
2886
2887   /* Output the CIE.  */
2888   ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
2889   ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
2890   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
2891     dw2_asm_output_data (4, 0xffffffff,
2892       "Initial length escape value indicating 64-bit DWARF extension");
2893   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
2894                         "Length of Common Information Entry");
2895   ASM_OUTPUT_LABEL (asm_out_file, l1);
2896
2897   /* Now that the CIE pointer is PC-relative for EH,
2898      use 0 to identify the CIE.  */
2899   dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
2900                        (for_eh ? 0 : DWARF_CIE_ID),
2901                        "CIE Identifier Tag");
2902
2903   dw2_asm_output_data (1, DW_CIE_VERSION, "CIE Version");
2904
2905   augmentation[0] = 0;
2906   augmentation_size = 0;
2907   if (for_eh)
2908     {
2909       char *p;
2910
2911       /* Augmentation:
2912          z      Indicates that a uleb128 is present to size the
2913                 augmentation section.
2914          L      Indicates the encoding (and thus presence) of
2915                 an LSDA pointer in the FDE augmentation.
2916          R      Indicates a non-default pointer encoding for
2917                 FDE code pointers.
2918          P      Indicates the presence of an encoding + language
2919                 personality routine in the CIE augmentation.  */
2920
2921       fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
2922       per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
2923       lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
2924
2925       p = augmentation + 1;
2926       if (eh_personality_libfunc)
2927         {
2928           *p++ = 'P';
2929           augmentation_size += 1 + size_of_encoded_value (per_encoding);
2930           assemble_external_libcall (eh_personality_libfunc);
2931         }
2932       if (any_lsda_needed)
2933         {
2934           *p++ = 'L';
2935           augmentation_size += 1;
2936         }
2937       if (fde_encoding != DW_EH_PE_absptr)
2938         {
2939           *p++ = 'R';
2940           augmentation_size += 1;
2941         }
2942       if (p > augmentation + 1)
2943         {
2944           augmentation[0] = 'z';
2945           *p = '\0';
2946         }
2947
2948       /* Ug.  Some platforms can't do unaligned dynamic relocations at all.  */
2949       if (eh_personality_libfunc && per_encoding == DW_EH_PE_aligned)
2950         {
2951           int offset = (  4             /* Length */
2952                         + 4             /* CIE Id */
2953                         + 1             /* CIE version */
2954                         + strlen (augmentation) + 1     /* Augmentation */
2955                         + size_of_uleb128 (1)           /* Code alignment */
2956                         + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
2957                         + 1             /* RA column */
2958                         + 1             /* Augmentation size */
2959                         + 1             /* Personality encoding */ );
2960           int pad = -offset & (PTR_SIZE - 1);
2961
2962           augmentation_size += pad;
2963
2964           /* Augmentations should be small, so there's scarce need to
2965              iterate for a solution.  Die if we exceed one uleb128 byte.  */
2966           gcc_assert (size_of_uleb128 (augmentation_size) == 1);
2967         }
2968     }
2969
2970   dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
2971   dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
2972   dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
2973                                "CIE Data Alignment Factor");
2974
2975   return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
2976   if (DW_CIE_VERSION == 1)
2977     dw2_asm_output_data (1, return_reg, "CIE RA Column");
2978   else
2979     dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
2980
2981   if (augmentation[0])
2982     {
2983       dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
2984       if (eh_personality_libfunc)
2985         {
2986           dw2_asm_output_data (1, per_encoding, "Personality (%s)",
2987                                eh_data_format_name (per_encoding));
2988           dw2_asm_output_encoded_addr_rtx (per_encoding,
2989                                            eh_personality_libfunc,
2990                                            true, NULL);
2991         }
2992
2993       if (any_lsda_needed)
2994         dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
2995                              eh_data_format_name (lsda_encoding));
2996
2997       if (fde_encoding != DW_EH_PE_absptr)
2998         dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
2999                              eh_data_format_name (fde_encoding));
3000     }
3001
3002   for (cfi = cie_cfi_head; cfi != NULL; cfi = cfi->dw_cfi_next)
3003     output_cfi (cfi, NULL, for_eh);
3004
3005   /* Pad the CIE out to an address sized boundary.  */
3006   ASM_OUTPUT_ALIGN (asm_out_file,
3007                     floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
3008   ASM_OUTPUT_LABEL (asm_out_file, l2);
3009
3010   /* Loop through all of the FDE's.  */
3011   for (i = 0; i < fde_table_in_use; i++)
3012     {
3013       fde = &fde_table[i];
3014
3015       /* Don't emit EH unwind info for leaf functions that don't need it.  */
3016       if (for_eh && !flag_asynchronous_unwind_tables && flag_exceptions
3017           && (fde->nothrow || fde->all_throwers_are_sibcalls)
3018           && ! (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
3019           && !fde->uses_eh_lsda)
3020         continue;
3021
3022       targetm.asm_out.unwind_label (asm_out_file, fde->decl, for_eh, /* empty */ 0);
3023       targetm.asm_out.internal_label (asm_out_file, FDE_LABEL, for_eh + i * 2);
3024       ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + i * 2);
3025       ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + i * 2);
3026       if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
3027         dw2_asm_output_data (4, 0xffffffff,
3028                              "Initial length escape value indicating 64-bit DWARF extension");
3029       dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
3030                             "FDE Length");
3031       ASM_OUTPUT_LABEL (asm_out_file, l1);
3032
3033       if (for_eh)
3034         dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
3035       else
3036         dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
3037                                debug_frame_section, "FDE CIE offset");
3038
3039       if (for_eh)
3040         {
3041           if (fde->dw_fde_switched_sections)
3042             {
3043               rtx sym_ref2 = gen_rtx_SYMBOL_REF (Pmode,
3044                                       fde->dw_fde_unlikely_section_label);
3045               rtx sym_ref3= gen_rtx_SYMBOL_REF (Pmode,
3046                                       fde->dw_fde_hot_section_label);
3047               SYMBOL_REF_FLAGS (sym_ref2) |= SYMBOL_FLAG_LOCAL;
3048               SYMBOL_REF_FLAGS (sym_ref3) |= SYMBOL_FLAG_LOCAL;
3049               dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref3, false,
3050                                                "FDE initial location");
3051               dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
3052                                     fde->dw_fde_hot_section_end_label,
3053                                     fde->dw_fde_hot_section_label,
3054                                     "FDE address range");
3055               dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref2, false,
3056                                                "FDE initial location");
3057               dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
3058                                     fde->dw_fde_unlikely_section_end_label,
3059                                     fde->dw_fde_unlikely_section_label,
3060                                     "FDE address range");
3061             }
3062           else
3063             {
3064               rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, fde->dw_fde_begin);
3065               SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
3066               dw2_asm_output_encoded_addr_rtx (fde_encoding,
3067                                                sym_ref,
3068                                                false,
3069                                                "FDE initial location");
3070               dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
3071                                     fde->dw_fde_end, fde->dw_fde_begin,
3072                                     "FDE address range");
3073             }
3074         }
3075       else
3076         {
3077           if (fde->dw_fde_switched_sections)
3078             {
3079               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
3080                                    fde->dw_fde_hot_section_label,
3081                                    "FDE initial location");
3082               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
3083                                     fde->dw_fde_hot_section_end_label,
3084                                     fde->dw_fde_hot_section_label,
3085                                     "FDE address range");
3086               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
3087                                    fde->dw_fde_unlikely_section_label,
3088                                    "FDE initial location");
3089               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
3090                                     fde->dw_fde_unlikely_section_end_label,
3091                                     fde->dw_fde_unlikely_section_label,
3092                                     "FDE address range");
3093             }
3094           else
3095             {
3096               dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
3097                                    "FDE initial location");
3098               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
3099                                     fde->dw_fde_end, fde->dw_fde_begin,
3100                                     "FDE address range");
3101             }
3102         }
3103
3104       if (augmentation[0])
3105         {
3106           if (any_lsda_needed)
3107             {
3108               int size = size_of_encoded_value (lsda_encoding);
3109
3110               if (lsda_encoding == DW_EH_PE_aligned)
3111                 {
3112                   int offset = (  4             /* Length */
3113                                 + 4             /* CIE offset */
3114                                 + 2 * size_of_encoded_value (fde_encoding)
3115                                 + 1             /* Augmentation size */ );
3116                   int pad = -offset & (PTR_SIZE - 1);
3117
3118                   size += pad;
3119                   gcc_assert (size_of_uleb128 (size) == 1);
3120                 }
3121
3122               dw2_asm_output_data_uleb128 (size, "Augmentation size");
3123
3124               if (fde->uses_eh_lsda)
3125                 {
3126                   ASM_GENERATE_INTERNAL_LABEL (l1, "LLSDA",
3127                                                fde->funcdef_number);
3128                   dw2_asm_output_encoded_addr_rtx (
3129                         lsda_encoding, gen_rtx_SYMBOL_REF (Pmode, l1),
3130                         false, "Language Specific Data Area");
3131                 }
3132               else
3133                 {
3134                   if (lsda_encoding == DW_EH_PE_aligned)
3135                     ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
3136                   dw2_asm_output_data
3137                     (size_of_encoded_value (lsda_encoding), 0,
3138                      "Language Specific Data Area (none)");
3139                 }
3140             }
3141           else
3142             dw2_asm_output_data_uleb128 (0, "Augmentation size");
3143         }
3144
3145       /* Loop through the Call Frame Instructions associated with
3146          this FDE.  */
3147       fde->dw_fde_current_label = fde->dw_fde_begin;
3148       for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
3149         output_cfi (cfi, fde, for_eh);
3150
3151       /* Pad the FDE out to an address sized boundary.  */
3152       ASM_OUTPUT_ALIGN (asm_out_file,
3153                         floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
3154       ASM_OUTPUT_LABEL (asm_out_file, l2);
3155     }
3156
3157   if (for_eh && targetm.terminate_dw2_eh_frame_info)
3158     dw2_asm_output_data (4, 0, "End of Table");
3159 #ifdef MIPS_DEBUGGING_INFO
3160   /* Work around Irix 6 assembler bug whereby labels at the end of a section
3161      get a value of 0.  Putting .align 0 after the label fixes it.  */
3162   ASM_OUTPUT_ALIGN (asm_out_file, 0);
3163 #endif
3164
3165   /* Turn off app to make assembly quicker.  */
3166   if (flag_debug_asm)
3167     app_disable ();
3168 }
3169
3170 /* Output a marker (i.e. a label) for the beginning of a function, before
3171    the prologue.  */
3172
3173 void
3174 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
3175                           const char *file ATTRIBUTE_UNUSED)
3176 {
3177   char label[MAX_ARTIFICIAL_LABEL_BYTES];
3178   char * dup_label;
3179   dw_fde_ref fde;
3180
3181   current_function_func_begin_label = NULL;
3182
3183 #ifdef TARGET_UNWIND_INFO
3184   /* ??? current_function_func_begin_label is also used by except.c
3185      for call-site information.  We must emit this label if it might
3186      be used.  */
3187   if ((! flag_exceptions || USING_SJLJ_EXCEPTIONS)
3188       && ! dwarf2out_do_frame ())
3189     return;
3190 #else
3191   if (! dwarf2out_do_frame ())
3192     return;
3193 #endif
3194
3195   switch_to_section (function_section (current_function_decl));
3196   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
3197                                current_function_funcdef_no);
3198   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
3199                           current_function_funcdef_no);
3200   dup_label = xstrdup (label);
3201   current_function_func_begin_label = dup_label;
3202
3203 #ifdef TARGET_UNWIND_INFO
3204   /* We can elide the fde allocation if we're not emitting debug info.  */
3205   if (! dwarf2out_do_frame ())
3206     return;
3207 #endif
3208
3209   /* Expand the fde table if necessary.  */
3210   if (fde_table_in_use == fde_table_allocated)
3211     {
3212       fde_table_allocated += FDE_TABLE_INCREMENT;
3213       fde_table = GGC_RESIZEVEC (dw_fde_node, fde_table, fde_table_allocated);
3214       memset (fde_table + fde_table_in_use, 0,
3215               FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
3216     }
3217
3218   /* Record the FDE associated with this function.  */
3219   current_funcdef_fde = fde_table_in_use;
3220
3221   /* Add the new FDE at the end of the fde_table.  */
3222   fde = &fde_table[fde_table_in_use++];
3223   fde->decl = current_function_decl;
3224   fde->dw_fde_begin = dup_label;
3225   fde->dw_fde_current_label = dup_label;
3226   fde->dw_fde_hot_section_label = NULL;
3227   fde->dw_fde_hot_section_end_label = NULL;
3228   fde->dw_fde_unlikely_section_label = NULL;
3229   fde->dw_fde_unlikely_section_end_label = NULL;
3230   fde->dw_fde_switched_sections = false;
3231   fde->dw_fde_end = NULL;
3232   fde->dw_fde_cfi = NULL;
3233   fde->funcdef_number = current_function_funcdef_no;
3234   fde->nothrow = crtl->nothrow;
3235   fde->uses_eh_lsda = crtl->uses_eh_lsda;
3236   fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
3237   fde->drap_reg = INVALID_REGNUM;
3238   fde->vdrap_reg = INVALID_REGNUM;
3239
3240   args_size = old_args_size = 0;
3241
3242   /* We only want to output line number information for the genuine dwarf2
3243      prologue case, not the eh frame case.  */
3244 #ifdef DWARF2_DEBUGGING_INFO
3245   if (file)
3246     dwarf2out_source_line (line, file);
3247 #endif
3248
3249   if (dwarf2out_do_cfi_asm ())
3250     {
3251       int enc;
3252       rtx ref;
3253
3254       fprintf (asm_out_file, "\t.cfi_startproc\n");
3255
3256       if (eh_personality_libfunc)
3257         {
3258           enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1); 
3259           ref = eh_personality_libfunc;
3260
3261           /* ??? The GAS support isn't entirely consistent.  We have to
3262              handle indirect support ourselves, but PC-relative is done
3263              in the assembler.  Further, the assembler can't handle any
3264              of the weirder relocation types.  */
3265           if (enc & DW_EH_PE_indirect)
3266             ref = dw2_force_const_mem (ref, true);
3267
3268           fprintf (asm_out_file, "\t.cfi_personality 0x%x,", enc);
3269           output_addr_const (asm_out_file, ref);
3270           fputc ('\n', asm_out_file);
3271         }
3272
3273       if (crtl->uses_eh_lsda)
3274         {
3275           char lab[20];
3276
3277           enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
3278           ASM_GENERATE_INTERNAL_LABEL (lab, "LLSDA",
3279                                        current_function_funcdef_no);
3280           ref = gen_rtx_SYMBOL_REF (Pmode, lab);
3281           SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
3282
3283           if (enc & DW_EH_PE_indirect)
3284             ref = dw2_force_const_mem (ref, true);
3285
3286           fprintf (asm_out_file, "\t.cfi_lsda 0x%x,", enc);
3287           output_addr_const (asm_out_file, ref);
3288           fputc ('\n', asm_out_file);
3289         }
3290     }
3291 }
3292
3293 /* Output a marker (i.e. a label) for the absolute end of the generated code
3294    for a function definition.  This gets called *after* the epilogue code has
3295    been generated.  */
3296
3297 void
3298 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
3299                         const char *file ATTRIBUTE_UNUSED)
3300 {
3301   dw_fde_ref fde;
3302   char label[MAX_ARTIFICIAL_LABEL_BYTES];
3303
3304   if (dwarf2out_do_cfi_asm ())
3305     fprintf (asm_out_file, "\t.cfi_endproc\n");
3306
3307   /* Output a label to mark the endpoint of the code generated for this
3308      function.  */
3309   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
3310                                current_function_funcdef_no);
3311   ASM_OUTPUT_LABEL (asm_out_file, label);
3312   fde = current_fde ();
3313   gcc_assert (fde != NULL);
3314   fde->dw_fde_end = xstrdup (label);
3315 }
3316
3317 void
3318 dwarf2out_frame_init (void)
3319 {
3320   /* Allocate the initial hunk of the fde_table.  */
3321   fde_table = GGC_CNEWVEC (dw_fde_node, FDE_TABLE_INCREMENT);
3322   fde_table_allocated = FDE_TABLE_INCREMENT;
3323   fde_table_in_use = 0;
3324
3325   /* Generate the CFA instructions common to all FDE's.  Do it now for the
3326      sake of lookup_cfa.  */
3327
3328   /* On entry, the Canonical Frame Address is at SP.  */
3329   dwarf2out_def_cfa (NULL, STACK_POINTER_REGNUM, INCOMING_FRAME_SP_OFFSET);
3330
3331 #ifdef DWARF2_UNWIND_INFO
3332   if (DWARF2_UNWIND_INFO || DWARF2_FRAME_INFO)
3333     initial_return_save (INCOMING_RETURN_ADDR_RTX);
3334 #endif
3335 }
3336
3337 void
3338 dwarf2out_frame_finish (void)
3339 {
3340   /* Output call frame information.  */
3341   if (DWARF2_FRAME_INFO)
3342     output_call_frame_info (0);
3343
3344 #ifndef TARGET_UNWIND_INFO
3345   /* Output another copy for the unwinder.  */
3346   if (! USING_SJLJ_EXCEPTIONS && (flag_unwind_tables || flag_exceptions))
3347     output_call_frame_info (1);
3348 #endif
3349 }
3350
3351 /* Note that the current function section is being used for code.  */
3352
3353 static void
3354 dwarf2out_note_section_used (void)
3355 {
3356   section *sec = current_function_section ();
3357   if (sec == text_section)
3358     text_section_used = true;
3359   else if (sec == cold_text_section)
3360     cold_text_section_used = true;
3361 }
3362
3363 void
3364 dwarf2out_switch_text_section (void)
3365 {
3366   dw_fde_ref fde = current_fde ();
3367
3368   gcc_assert (cfun && fde);
3369
3370   fde->dw_fde_switched_sections = true;
3371   fde->dw_fde_hot_section_label = crtl->subsections.hot_section_label;
3372   fde->dw_fde_hot_section_end_label = crtl->subsections.hot_section_end_label;
3373   fde->dw_fde_unlikely_section_label = crtl->subsections.cold_section_label;
3374   fde->dw_fde_unlikely_section_end_label = crtl->subsections.cold_section_end_label;
3375   have_multiple_function_sections = true;
3376
3377   /* Reset the current label on switching text sections, so that we
3378      don't attempt to advance_loc4 between labels in different sections.  */
3379   fde->dw_fde_current_label = NULL;
3380
3381   /* There is no need to mark used sections when not debugging.  */
3382   if (cold_text_section != NULL)
3383     dwarf2out_note_section_used ();
3384 }
3385 #endif
3386 \f
3387 /* And now, the subset of the debugging information support code necessary
3388    for emitting location expressions.  */
3389
3390 /* Data about a single source file.  */
3391 struct dwarf_file_data GTY(())
3392 {
3393   const char * filename;
3394   int emitted_number;
3395 };
3396
3397 /* We need some way to distinguish DW_OP_addr with a direct symbol
3398    relocation from DW_OP_addr with a dtp-relative symbol relocation.  */
3399 #define INTERNAL_DW_OP_tls_addr         (0x100 + DW_OP_addr)
3400
3401
3402 typedef struct dw_val_struct *dw_val_ref;
3403 typedef struct die_struct *dw_die_ref;
3404 typedef const struct die_struct *const_dw_die_ref;
3405 typedef struct dw_loc_descr_struct *dw_loc_descr_ref;
3406 typedef struct dw_loc_list_struct *dw_loc_list_ref;
3407
3408 typedef struct deferred_locations_struct GTY(()) 
3409 {
3410   tree variable;
3411   dw_die_ref die;
3412 } deferred_locations;
3413
3414 DEF_VEC_O(deferred_locations);
3415 DEF_VEC_ALLOC_O(deferred_locations,gc);
3416
3417 static GTY(()) VEC(deferred_locations, gc) *deferred_locations_list;
3418
3419 /* Each DIE may have a series of attribute/value pairs.  Values
3420    can take on several forms.  The forms that are used in this
3421    implementation are listed below.  */
3422
3423 enum dw_val_class
3424 {
3425   dw_val_class_addr,
3426   dw_val_class_offset,
3427   dw_val_class_loc,
3428   dw_val_class_loc_list,
3429   dw_val_class_range_list,
3430   dw_val_class_const,
3431   dw_val_class_unsigned_const,
3432   dw_val_class_long_long,
3433   dw_val_class_vec,
3434   dw_val_class_flag,
3435   dw_val_class_die_ref,
3436   dw_val_class_fde_ref,
3437   dw_val_class_lbl_id,
3438   dw_val_class_lineptr,
3439   dw_val_class_str,
3440   dw_val_class_macptr,
3441   dw_val_class_file
3442 };
3443
3444 /* Describe a double word constant value.  */
3445 /* ??? Every instance of long_long in the code really means CONST_DOUBLE.  */
3446
3447 typedef struct dw_long_long_struct GTY(())
3448 {
3449   unsigned long hi;
3450   unsigned long low;
3451 }
3452 dw_long_long_const;
3453
3454 /* Describe a floating point constant value, or a vector constant value.  */
3455
3456 typedef struct dw_vec_struct GTY(())
3457 {
3458   unsigned char * GTY((length ("%h.length"))) array;
3459   unsigned length;
3460   unsigned elt_size;
3461 }
3462 dw_vec_const;
3463
3464 /* The dw_val_node describes an attribute's value, as it is
3465    represented internally.  */
3466
3467 typedef struct dw_val_struct GTY(())
3468 {
3469   enum dw_val_class val_class;
3470   union dw_val_struct_union
3471     {
3472       rtx GTY ((tag ("dw_val_class_addr"))) val_addr;
3473       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_offset"))) val_offset;
3474       dw_loc_list_ref GTY ((tag ("dw_val_class_loc_list"))) val_loc_list;
3475       dw_loc_descr_ref GTY ((tag ("dw_val_class_loc"))) val_loc;
3476       HOST_WIDE_INT GTY ((default)) val_int;
3477       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_unsigned_const"))) val_unsigned;
3478       dw_long_long_const GTY ((tag ("dw_val_class_long_long"))) val_long_long;
3479       dw_vec_const GTY ((tag ("dw_val_class_vec"))) val_vec;
3480       struct dw_val_die_union
3481         {
3482           dw_die_ref die;
3483           int external;
3484         } GTY ((tag ("dw_val_class_die_ref"))) val_die_ref;
3485       unsigned GTY ((tag ("dw_val_class_fde_ref"))) val_fde_index;
3486       struct indirect_string_node * GTY ((tag ("dw_val_class_str"))) val_str;
3487       char * GTY ((tag ("dw_val_class_lbl_id"))) val_lbl_id;
3488       unsigned char GTY ((tag ("dw_val_class_flag"))) val_flag;
3489       struct dwarf_file_data * GTY ((tag ("dw_val_class_file"))) val_file;
3490     }
3491   GTY ((desc ("%1.val_class"))) v;
3492 }
3493 dw_val_node;
3494
3495 /* Locations in memory are described using a sequence of stack machine
3496    operations.  */
3497
3498 typedef struct dw_loc_descr_struct GTY(())
3499 {
3500   dw_loc_descr_ref dw_loc_next;
3501   enum dwarf_location_atom dw_loc_opc;
3502   int dw_loc_addr;
3503   dw_val_node dw_loc_oprnd1;
3504   dw_val_node dw_loc_oprnd2;
3505 }
3506 dw_loc_descr_node;
3507
3508 /* Location lists are ranges + location descriptions for that range,
3509    so you can track variables that are in different places over
3510    their entire life.  */
3511 typedef struct dw_loc_list_struct GTY(())
3512 {
3513   dw_loc_list_ref dw_loc_next;
3514   const char *begin; /* Label for begin address of range */
3515   const char *end;  /* Label for end address of range */
3516   char *ll_symbol; /* Label for beginning of location list.
3517                       Only on head of list */
3518   const char *section; /* Section this loclist is relative to */
3519   dw_loc_descr_ref expr;
3520 } dw_loc_list_node;
3521
3522 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
3523
3524 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
3525
3526 /* Convert a DWARF stack opcode into its string name.  */
3527
3528 static const char *
3529 dwarf_stack_op_name (unsigned int op)
3530 {
3531   switch (op)
3532     {
3533     case DW_OP_addr:
3534     case INTERNAL_DW_OP_tls_addr:
3535       return "DW_OP_addr";
3536     case DW_OP_deref:
3537       return "DW_OP_deref";
3538     case DW_OP_const1u:
3539       return "DW_OP_const1u";
3540     case DW_OP_const1s:
3541       return "DW_OP_const1s";
3542     case DW_OP_const2u:
3543       return "DW_OP_const2u";
3544     case DW_OP_const2s:
3545       return "DW_OP_const2s";
3546     case DW_OP_const4u:
3547       return "DW_OP_const4u";
3548     case DW_OP_const4s:
3549       return "DW_OP_const4s";
3550     case DW_OP_const8u:
3551       return "DW_OP_const8u";
3552     case DW_OP_const8s:
3553       return "DW_OP_const8s";
3554     case DW_OP_constu:
3555       return "DW_OP_constu";
3556     case DW_OP_consts:
3557       return "DW_OP_consts";
3558     case DW_OP_dup:
3559       return "DW_OP_dup";
3560     case DW_OP_drop:
3561       return "DW_OP_drop";
3562     case DW_OP_over:
3563       return "DW_OP_over";
3564     case DW_OP_pick:
3565       return "DW_OP_pick";
3566     case DW_OP_swap:
3567       return "DW_OP_swap";
3568     case DW_OP_rot:
3569       return "DW_OP_rot";
3570     case DW_OP_xderef:
3571       return "DW_OP_xderef";
3572     case DW_OP_abs:
3573       return "DW_OP_abs";
3574     case DW_OP_and:
3575       return "DW_OP_and";
3576     case DW_OP_div:
3577       return "DW_OP_div";
3578     case DW_OP_minus:
3579       return "DW_OP_minus";
3580     case DW_OP_mod:
3581       return "DW_OP_mod";
3582     case DW_OP_mul:
3583       return "DW_OP_mul";
3584     case DW_OP_neg:
3585       return "DW_OP_neg";
3586     case DW_OP_not:
3587       return "DW_OP_not";
3588     case DW_OP_or:
3589       return "DW_OP_or";
3590     case DW_OP_plus:
3591       return "DW_OP_plus";
3592     case DW_OP_plus_uconst:
3593       return "DW_OP_plus_uconst";
3594     case DW_OP_shl:
3595       return "DW_OP_shl";
3596     case DW_OP_shr:
3597       return "DW_OP_shr";
3598     case DW_OP_shra:
3599       return "DW_OP_shra";
3600     case DW_OP_xor:
3601       return "DW_OP_xor";
3602     case DW_OP_bra:
3603       return "DW_OP_bra";
3604     case DW_OP_eq:
3605       return "DW_OP_eq";
3606     case DW_OP_ge:
3607       return "DW_OP_ge";
3608     case DW_OP_gt:
3609       return "DW_OP_gt";
3610     case DW_OP_le:
3611       return "DW_OP_le";
3612     case DW_OP_lt:
3613       return "DW_OP_lt";
3614     case DW_OP_ne:
3615       return "DW_OP_ne";
3616     case DW_OP_skip:
3617       return "DW_OP_skip";
3618     case DW_OP_lit0:
3619       return "DW_OP_lit0";
3620     case DW_OP_lit1:
3621       return "DW_OP_lit1";
3622     case DW_OP_lit2:
3623       return "DW_OP_lit2";
3624     case DW_OP_lit3:
3625       return "DW_OP_lit3";
3626     case DW_OP_lit4:
3627       return "DW_OP_lit4";
3628     case DW_OP_lit5:
3629       return "DW_OP_lit5";
3630     case DW_OP_lit6:
3631       return "DW_OP_lit6";
3632     case DW_OP_lit7:
3633       return "DW_OP_lit7";
3634     case DW_OP_lit8:
3635       return "DW_OP_lit8";
3636     case DW_OP_lit9:
3637       return "DW_OP_lit9";
3638     case DW_OP_lit10:
3639       return "DW_OP_lit10";
3640     case DW_OP_lit11:
3641       return "DW_OP_lit11";
3642     case DW_OP_lit12:
3643       return "DW_OP_lit12";
3644     case DW_OP_lit13:
3645       return "DW_OP_lit13";
3646     case DW_OP_lit14:
3647       return "DW_OP_lit14";
3648     case DW_OP_lit15:
3649       return "DW_OP_lit15";
3650     case DW_OP_lit16:
3651       return "DW_OP_lit16";
3652     case DW_OP_lit17:
3653       return "DW_OP_lit17";
3654     case DW_OP_lit18:
3655       return "DW_OP_lit18";
3656     case DW_OP_lit19:
3657       return "DW_OP_lit19";
3658     case DW_OP_lit20:
3659       return "DW_OP_lit20";
3660     case DW_OP_lit21:
3661       return "DW_OP_lit21";
3662     case DW_OP_lit22:
3663       return "DW_OP_lit22";
3664     case DW_OP_lit23:
3665       return "DW_OP_lit23";
3666     case DW_OP_lit24:
3667       return "DW_OP_lit24";
3668     case DW_OP_lit25:
3669       return "DW_OP_lit25";
3670     case DW_OP_lit26:
3671       return "DW_OP_lit26";
3672     case DW_OP_lit27:
3673       return "DW_OP_lit27";
3674     case DW_OP_lit28:
3675       return "DW_OP_lit28";
3676     case DW_OP_lit29:
3677       return "DW_OP_lit29";
3678     case DW_OP_lit30:
3679       return "DW_OP_lit30";
3680     case DW_OP_lit31:
3681       return "DW_OP_lit31";
3682     case DW_OP_reg0:
3683       return "DW_OP_reg0";
3684     case DW_OP_reg1:
3685       return "DW_OP_reg1";
3686     case DW_OP_reg2:
3687       return "DW_OP_reg2";
3688     case DW_OP_reg3:
3689       return "DW_OP_reg3";
3690     case DW_OP_reg4:
3691       return "DW_OP_reg4";
3692     case DW_OP_reg5:
3693       return "DW_OP_reg5";
3694     case DW_OP_reg6:
3695       return "DW_OP_reg6";
3696     case DW_OP_reg7:
3697       return "DW_OP_reg7";
3698     case DW_OP_reg8:
3699       return "DW_OP_reg8";
3700     case DW_OP_reg9:
3701       return "DW_OP_reg9";
3702     case DW_OP_reg10:
3703       return "DW_OP_reg10";
3704     case DW_OP_reg11:
3705       return "DW_OP_reg11";
3706     case DW_OP_reg12:
3707       return "DW_OP_reg12";
3708     case DW_OP_reg13:
3709       return "DW_OP_reg13";
3710     case DW_OP_reg14:
3711       return "DW_OP_reg14";
3712     case DW_OP_reg15:
3713       return "DW_OP_reg15";
3714     case DW_OP_reg16:
3715       return "DW_OP_reg16";
3716     case DW_OP_reg17:
3717       return "DW_OP_reg17";
3718     case DW_OP_reg18:
3719       return "DW_OP_reg18";
3720     case DW_OP_reg19:
3721       return "DW_OP_reg19";
3722     case DW_OP_reg20:
3723       return "DW_OP_reg20";
3724     case DW_OP_reg21:
3725       return "DW_OP_reg21";
3726     case DW_OP_reg22:
3727       return "DW_OP_reg22";
3728     case DW_OP_reg23:
3729       return "DW_OP_reg23";
3730     case DW_OP_reg24:
3731       return "DW_OP_reg24";
3732     case DW_OP_reg25:
3733       return "DW_OP_reg25";
3734     case DW_OP_reg26:
3735       return "DW_OP_reg26";
3736     case DW_OP_reg27:
3737       return "DW_OP_reg27";
3738     case DW_OP_reg28:
3739       return "DW_OP_reg28";
3740     case DW_OP_reg29:
3741       return "DW_OP_reg29";
3742     case DW_OP_reg30:
3743       return "DW_OP_reg30";
3744     case DW_OP_reg31:
3745       return "DW_OP_reg31";
3746     case DW_OP_breg0:
3747       return "DW_OP_breg0";
3748     case DW_OP_breg1:
3749       return "DW_OP_breg1";
3750     case DW_OP_breg2:
3751       return "DW_OP_breg2";
3752     case DW_OP_breg3:
3753       return "DW_OP_breg3";
3754     case DW_OP_breg4:
3755       return "DW_OP_breg4";
3756     case DW_OP_breg5:
3757       return "DW_OP_breg5";
3758     case DW_OP_breg6:
3759       return "DW_OP_breg6";
3760     case DW_OP_breg7:
3761       return "DW_OP_breg7";
3762     case DW_OP_breg8:
3763       return "DW_OP_breg8";
3764     case DW_OP_breg9:
3765       return "DW_OP_breg9";
3766     case DW_OP_breg10:
3767       return "DW_OP_breg10";
3768     case DW_OP_breg11:
3769       return "DW_OP_breg11";
3770     case DW_OP_breg12:
3771       return "DW_OP_breg12";
3772     case DW_OP_breg13:
3773       return "DW_OP_breg13";
3774     case DW_OP_breg14:
3775       return "DW_OP_breg14";
3776     case DW_OP_breg15:
3777       return "DW_OP_breg15";
3778     case DW_OP_breg16:
3779       return "DW_OP_breg16";
3780     case DW_OP_breg17:
3781       return "DW_OP_breg17";
3782     case DW_OP_breg18:
3783       return "DW_OP_breg18";
3784     case DW_OP_breg19:
3785       return "DW_OP_breg19";
3786     case DW_OP_breg20:
3787       return "DW_OP_breg20";
3788     case DW_OP_breg21:
3789       return "DW_OP_breg21";
3790     case DW_OP_breg22:
3791       return "DW_OP_breg22";
3792     case DW_OP_breg23:
3793       return "DW_OP_breg23";
3794     case DW_OP_breg24:
3795       return "DW_OP_breg24";
3796     case DW_OP_breg25:
3797       return "DW_OP_breg25";
3798     case DW_OP_breg26:
3799       return "DW_OP_breg26";
3800     case DW_OP_breg27:
3801       return "DW_OP_breg27";
3802     case DW_OP_breg28:
3803       return "DW_OP_breg28";
3804     case DW_OP_breg29:
3805       return "DW_OP_breg29";
3806     case DW_OP_breg30:
3807       return "DW_OP_breg30";
3808     case DW_OP_breg31:
3809       return "DW_OP_breg31";
3810     case DW_OP_regx:
3811       return "DW_OP_regx";
3812     case DW_OP_fbreg:
3813       return "DW_OP_fbreg";
3814     case DW_OP_bregx:
3815       return "DW_OP_bregx";
3816     case DW_OP_piece:
3817       return "DW_OP_piece";
3818     case DW_OP_deref_size:
3819       return "DW_OP_deref_size";
3820     case DW_OP_xderef_size:
3821       return "DW_OP_xderef_size";
3822     case DW_OP_nop:
3823       return "DW_OP_nop";
3824     case DW_OP_push_object_address:
3825       return "DW_OP_push_object_address";
3826     case DW_OP_call2:
3827       return "DW_OP_call2";
3828     case DW_OP_call4:
3829       return "DW_OP_call4";
3830     case DW_OP_call_ref:
3831       return "DW_OP_call_ref";
3832     case DW_OP_GNU_push_tls_address:
3833       return "DW_OP_GNU_push_tls_address";
3834     case DW_OP_GNU_uninit:
3835       return "DW_OP_GNU_uninit";
3836     default:
3837       return "OP_<unknown>";
3838     }
3839 }
3840
3841 /* Return a pointer to a newly allocated location description.  Location
3842    descriptions are simple expression terms that can be strung
3843    together to form more complicated location (address) descriptions.  */
3844
3845 static inline dw_loc_descr_ref
3846 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
3847                unsigned HOST_WIDE_INT oprnd2)
3848 {
3849   dw_loc_descr_ref descr = GGC_CNEW (dw_loc_descr_node);
3850
3851   descr->dw_loc_opc = op;
3852   descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
3853   descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
3854   descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
3855   descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
3856
3857   return descr;
3858 }
3859
3860 /* Return a pointer to a newly allocated location description for
3861    REG and OFFSET.  */
3862
3863 static inline dw_loc_descr_ref
3864 new_reg_loc_descr (unsigned int reg,  unsigned HOST_WIDE_INT offset)
3865 {
3866   if (offset)
3867     {
3868       if (reg <= 31)
3869         return new_loc_descr (DW_OP_breg0 + reg, offset, 0);
3870       else
3871         return new_loc_descr (DW_OP_bregx, reg, offset);
3872     }
3873   else if (reg <= 31)
3874     return new_loc_descr (DW_OP_reg0 + reg, 0, 0);
3875   else
3876    return new_loc_descr (DW_OP_regx, reg, 0);
3877 }
3878
3879 /* Add a location description term to a location description expression.  */
3880
3881 static inline void
3882 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
3883 {
3884   dw_loc_descr_ref *d;
3885
3886   /* Find the end of the chain.  */
3887   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
3888     ;
3889
3890   *d = descr;
3891 }
3892
3893 /* Return the size of a location descriptor.  */
3894
3895 static unsigned long
3896 size_of_loc_descr (dw_loc_descr_ref loc)
3897 {
3898   unsigned long size = 1;
3899
3900   switch (loc->dw_loc_opc)
3901     {
3902     case DW_OP_addr:
3903     case INTERNAL_DW_OP_tls_addr:
3904       size += DWARF2_ADDR_SIZE;
3905       break;
3906     case DW_OP_const1u:
3907     case DW_OP_const1s:
3908       size += 1;
3909       break;
3910     case DW_OP_const2u:
3911     case DW_OP_const2s:
3912       size += 2;
3913       break;
3914     case DW_OP_const4u:
3915     case DW_OP_const4s:
3916       size += 4;
3917       break;
3918     case DW_OP_const8u:
3919     case DW_OP_const8s:
3920       size += 8;
3921       break;
3922     case DW_OP_constu:
3923       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3924       break;
3925     case DW_OP_consts:
3926       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3927       break;
3928     case DW_OP_pick:
3929       size += 1;
3930       break;
3931     case DW_OP_plus_uconst:
3932       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3933       break;
3934     case DW_OP_skip:
3935     case DW_OP_bra:
3936       size += 2;
3937       break;
3938     case DW_OP_breg0:
3939     case DW_OP_breg1:
3940     case DW_OP_breg2:
3941     case DW_OP_breg3:
3942     case DW_OP_breg4:
3943     case DW_OP_breg5:
3944     case DW_OP_breg6:
3945     case DW_OP_breg7:
3946     case DW_OP_breg8:
3947     case DW_OP_breg9:
3948     case DW_OP_breg10:
3949     case DW_OP_breg11:
3950     case DW_OP_breg12:
3951     case DW_OP_breg13:
3952     case DW_OP_breg14:
3953     case DW_OP_breg15:
3954     case DW_OP_breg16:
3955     case DW_OP_breg17:
3956     case DW_OP_breg18:
3957     case DW_OP_breg19:
3958     case DW_OP_breg20:
3959     case DW_OP_breg21:
3960     case DW_OP_breg22:
3961     case DW_OP_breg23:
3962     case DW_OP_breg24:
3963     case DW_OP_breg25:
3964     case DW_OP_breg26:
3965     case DW_OP_breg27:
3966     case DW_OP_breg28:
3967     case DW_OP_breg29:
3968     case DW_OP_breg30:
3969     case DW_OP_breg31:
3970       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3971       break;
3972     case DW_OP_regx:
3973       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3974       break;
3975     case DW_OP_fbreg:
3976       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3977       break;
3978     case DW_OP_bregx:
3979       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3980       size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
3981       break;
3982     case DW_OP_piece:
3983       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3984       break;
3985     case DW_OP_deref_size:
3986     case DW_OP_xderef_size:
3987       size += 1;
3988       break;
3989     case DW_OP_call2:
3990       size += 2;
3991       break;
3992     case DW_OP_call4:
3993       size += 4;
3994       break;
3995     case DW_OP_call_ref:
3996       size += DWARF2_ADDR_SIZE;
3997       break;
3998     default:
3999       break;
4000     }
4001
4002   return size;
4003 }
4004
4005 /* Return the size of a series of location descriptors.  */
4006
4007 static unsigned long
4008 size_of_locs (dw_loc_descr_ref loc)
4009 {
4010   dw_loc_descr_ref l;
4011   unsigned long size;
4012
4013   /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
4014      field, to avoid writing to a PCH file.  */
4015   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
4016     {
4017       if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
4018         break;
4019       size += size_of_loc_descr (l);
4020     }
4021   if (! l)
4022     return size;
4023
4024   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
4025     {
4026       l->dw_loc_addr = size;
4027       size += size_of_loc_descr (l);
4028     }
4029
4030   return size;
4031 }
4032
4033 /* Output location description stack opcode's operands (if any).  */
4034
4035 static void
4036 output_loc_operands (dw_loc_descr_ref loc)
4037 {
4038   dw_val_ref val1 = &loc->dw_loc_oprnd1;
4039   dw_val_ref val2 = &loc->dw_loc_oprnd2;
4040
4041   switch (loc->dw_loc_opc)
4042     {
4043 #ifdef DWARF2_DEBUGGING_INFO
4044     case DW_OP_addr:
4045       dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
4046       break;
4047     case DW_OP_const2u:
4048     case DW_OP_const2s:
4049       dw2_asm_output_data (2, val1->v.val_int, NULL);
4050       break;
4051     case DW_OP_const4u:
4052     case DW_OP_const4s:
4053       dw2_asm_output_data (4, val1->v.val_int, NULL);
4054       break;
4055     case DW_OP_const8u:
4056     case DW_OP_const8s:
4057       gcc_assert (HOST_BITS_PER_LONG >= 64);
4058       dw2_asm_output_data (8, val1->v.val_int, NULL);
4059       break;
4060     case DW_OP_skip:
4061     case DW_OP_bra:
4062       {
4063         int offset;
4064
4065         gcc_assert (val1->val_class == dw_val_class_loc);
4066         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
4067
4068         dw2_asm_output_data (2, offset, NULL);
4069       }
4070       break;
4071 #else
4072     case DW_OP_addr:
4073     case DW_OP_const2u:
4074     case DW_OP_const2s:
4075     case DW_OP_const4u:
4076     case DW_OP_const4s:
4077     case DW_OP_const8u:
4078     case DW_OP_const8s:
4079     case DW_OP_skip:
4080     case DW_OP_bra:
4081       /* We currently don't make any attempt to make sure these are
4082          aligned properly like we do for the main unwind info, so
4083          don't support emitting things larger than a byte if we're
4084          only doing unwinding.  */
4085       gcc_unreachable ();
4086 #endif
4087     case DW_OP_const1u:
4088     case DW_OP_const1s:
4089       dw2_asm_output_data (1, val1->v.val_int, NULL);
4090       break;
4091     case DW_OP_constu:
4092       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4093       break;
4094     case DW_OP_consts:
4095       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
4096       break;
4097     case DW_OP_pick:
4098       dw2_asm_output_data (1, val1->v.val_int, NULL);
4099       break;
4100     case DW_OP_plus_uconst:
4101       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4102       break;
4103     case DW_OP_breg0:
4104     case DW_OP_breg1:
4105     case DW_OP_breg2:
4106     case DW_OP_breg3:
4107     case DW_OP_breg4:
4108     case DW_OP_breg5:
4109     case DW_OP_breg6:
4110     case DW_OP_breg7:
4111     case DW_OP_breg8:
4112     case DW_OP_breg9:
4113     case DW_OP_breg10:
4114     case DW_OP_breg11:
4115     case DW_OP_breg12:
4116     case DW_OP_breg13:
4117     case DW_OP_breg14:
4118     case DW_OP_breg15:
4119     case DW_OP_breg16:
4120     case DW_OP_breg17:
4121     case DW_OP_breg18:
4122     case DW_OP_breg19:
4123     case DW_OP_breg20:
4124     case DW_OP_breg21:
4125     case DW_OP_breg22:
4126     case DW_OP_breg23:
4127     case DW_OP_breg24:
4128     case DW_OP_breg25:
4129     case DW_OP_breg26:
4130     case DW_OP_breg27:
4131     case DW_OP_breg28:
4132     case DW_OP_breg29:
4133     case DW_OP_breg30:
4134     case DW_OP_breg31:
4135       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
4136       break;
4137     case DW_OP_regx:
4138       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4139       break;
4140     case DW_OP_fbreg:
4141       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
4142       break;
4143     case DW_OP_bregx:
4144       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4145       dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
4146       break;
4147     case DW_OP_piece:
4148       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4149       break;
4150     case DW_OP_deref_size:
4151     case DW_OP_xderef_size:
4152       dw2_asm_output_data (1, val1->v.val_int, NULL);
4153       break;
4154
4155     case INTERNAL_DW_OP_tls_addr:
4156       if (targetm.asm_out.output_dwarf_dtprel)
4157         {
4158           targetm.asm_out.output_dwarf_dtprel (asm_out_file,
4159                                                DWARF2_ADDR_SIZE,
4160                                                val1->v.val_addr);
4161           fputc ('\n', asm_out_file);
4162         }
4163       else
4164         gcc_unreachable ();
4165       break;
4166
4167     default:
4168       /* Other codes have no operands.  */
4169       break;
4170     }
4171 }
4172
4173 /* Output a sequence of location operations.  */
4174
4175 static void
4176 output_loc_sequence (dw_loc_descr_ref loc)
4177 {
4178   for (; loc != NULL; loc = loc->dw_loc_next)
4179     {
4180       /* Output the opcode.  */
4181       dw2_asm_output_data (1, loc->dw_loc_opc,
4182                            "%s", dwarf_stack_op_name (loc->dw_loc_opc));
4183
4184       /* Output the operand(s) (if any).  */
4185       output_loc_operands (loc);
4186     }
4187 }
4188
4189 /* Output location description stack opcode's operands (if any).
4190    The output is single bytes on a line, suitable for .cfi_escape.  */
4191
4192 static void
4193 output_loc_operands_raw (dw_loc_descr_ref loc)
4194 {
4195   dw_val_ref val1 = &loc->dw_loc_oprnd1;
4196   dw_val_ref val2 = &loc->dw_loc_oprnd2;
4197
4198   switch (loc->dw_loc_opc)
4199     {
4200     case DW_OP_addr:
4201       /* We cannot output addresses in .cfi_escape, only bytes.  */
4202       gcc_unreachable ();
4203
4204     case DW_OP_const1u:
4205     case DW_OP_const1s:
4206     case DW_OP_pick:
4207     case DW_OP_deref_size:
4208     case DW_OP_xderef_size:
4209       fputc (',', asm_out_file);
4210       dw2_asm_output_data_raw (1, val1->v.val_int);
4211       break;
4212
4213     case DW_OP_const2u:
4214     case DW_OP_const2s:
4215       fputc (',', asm_out_file);
4216       dw2_asm_output_data_raw (2, val1->v.val_int);
4217       break;
4218
4219     case DW_OP_const4u:
4220     case DW_OP_const4s:
4221       fputc (',', asm_out_file);
4222       dw2_asm_output_data_raw (4, val1->v.val_int);
4223       break;
4224
4225     case DW_OP_const8u:
4226     case DW_OP_const8s:
4227       gcc_assert (HOST_BITS_PER_LONG >= 64);
4228       fputc (',', asm_out_file);
4229       dw2_asm_output_data_raw (8, val1->v.val_int);
4230       break;
4231
4232     case DW_OP_skip:
4233     case DW_OP_bra:
4234       {
4235         int offset;
4236
4237         gcc_assert (val1->val_class == dw_val_class_loc);
4238         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
4239
4240         fputc (',', asm_out_file);
4241         dw2_asm_output_data_raw (2, offset);
4242       }
4243       break;
4244
4245     case DW_OP_constu:
4246     case DW_OP_plus_uconst:
4247     case DW_OP_regx:
4248     case DW_OP_piece:
4249       fputc (',', asm_out_file);
4250       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
4251       break;
4252
4253     case DW_OP_consts:
4254     case DW_OP_breg0:
4255     case DW_OP_breg1:
4256     case DW_OP_breg2:
4257     case DW_OP_breg3:
4258     case DW_OP_breg4:
4259     case DW_OP_breg5:
4260     case DW_OP_breg6:
4261     case DW_OP_breg7:
4262     case DW_OP_breg8:
4263     case DW_OP_breg9:
4264     case DW_OP_breg10:
4265     case DW_OP_breg11:
4266     case DW_OP_breg12:
4267     case DW_OP_breg13:
4268     case DW_OP_breg14:
4269     case DW_OP_breg15:
4270     case DW_OP_breg16:
4271     case DW_OP_breg17:
4272     case DW_OP_breg18:
4273     case DW_OP_breg19:
4274     case DW_OP_breg20:
4275     case DW_OP_breg21:
4276     case DW_OP_breg22:
4277     case DW_OP_breg23:
4278     case DW_OP_breg24:
4279     case DW_OP_breg25:
4280     case DW_OP_breg26:
4281     case DW_OP_breg27:
4282     case DW_OP_breg28:
4283     case DW_OP_breg29:
4284     case DW_OP_breg30:
4285     case DW_OP_breg31:
4286     case DW_OP_fbreg:
4287       fputc (',', asm_out_file);
4288       dw2_asm_output_data_sleb128_raw (val1->v.val_int);
4289       break;
4290
4291     case DW_OP_bregx:
4292       fputc (',', asm_out_file);
4293       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
4294       fputc (',', asm_out_file);
4295       dw2_asm_output_data_sleb128_raw (val2->v.val_int);
4296       break;
4297
4298     case INTERNAL_DW_OP_tls_addr:
4299       gcc_unreachable ();
4300
4301     default:
4302       /* Other codes have no operands.  */
4303       break;
4304     }
4305 }
4306
4307 static void
4308 output_loc_sequence_raw (dw_loc_descr_ref loc)
4309 {
4310   while (1)
4311     {
4312       /* Output the opcode.  */
4313       fprintf (asm_out_file, "0x%x", loc->dw_loc_opc);
4314       output_loc_operands_raw (loc);
4315
4316       if (!loc->dw_loc_next)
4317         break;
4318       loc = loc->dw_loc_next;
4319
4320       fputc (',', asm_out_file);
4321     }
4322 }
4323
4324 /* This routine will generate the correct assembly data for a location
4325    description based on a cfi entry with a complex address.  */
4326
4327 static void
4328 output_cfa_loc (dw_cfi_ref cfi)
4329 {
4330   dw_loc_descr_ref loc;
4331   unsigned long size;
4332
4333   if (cfi->dw_cfi_opc == DW_CFA_expression)
4334     dw2_asm_output_data (1, cfi->dw_cfi_oprnd2.dw_cfi_reg_num, NULL);
4335
4336   /* Output the size of the block.  */
4337   loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
4338   size = size_of_locs (loc);
4339   dw2_asm_output_data_uleb128 (size, NULL);
4340
4341   /* Now output the operations themselves.  */
4342   output_loc_sequence (loc);
4343 }
4344
4345 /* Similar, but used for .cfi_escape.  */
4346
4347 static void
4348 output_cfa_loc_raw (dw_cfi_ref cfi)
4349 {
4350   dw_loc_descr_ref loc;
4351   unsigned long size;
4352
4353   if (cfi->dw_cfi_opc == DW_CFA_expression)
4354     fprintf (asm_out_file, "0x%x,", cfi->dw_cfi_oprnd2.dw_cfi_reg_num);
4355
4356   /* Output the size of the block.  */
4357   loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
4358   size = size_of_locs (loc);
4359   dw2_asm_output_data_uleb128_raw (size);
4360   fputc (',', asm_out_file);
4361
4362   /* Now output the operations themselves.  */
4363   output_loc_sequence_raw (loc);
4364 }
4365
4366 /* This function builds a dwarf location descriptor sequence from a
4367    dw_cfa_location, adding the given OFFSET to the result of the
4368    expression.  */
4369
4370 static struct dw_loc_descr_struct *
4371 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
4372 {
4373   struct dw_loc_descr_struct *head, *tmp;
4374
4375   offset += cfa->offset;
4376
4377   if (cfa->indirect)
4378     {
4379       head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
4380       head->dw_loc_oprnd1.val_class = dw_val_class_const;
4381       tmp = new_loc_descr (DW_OP_deref, 0, 0);
4382       add_loc_descr (&head, tmp);
4383       if (offset != 0)
4384         {
4385           tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
4386           add_loc_descr (&head, tmp);
4387         }
4388     }
4389   else
4390     head = new_reg_loc_descr (cfa->reg, offset);
4391
4392   return head;
4393 }
4394
4395 /* This function builds a dwarf location descriptor sequence for
4396    the address at OFFSET from the CFA when stack is aligned to
4397    ALIGNMENT byte.  */
4398
4399 static struct dw_loc_descr_struct *
4400 build_cfa_aligned_loc (HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
4401 {
4402   struct dw_loc_descr_struct *head;
4403   unsigned int dwarf_fp
4404     = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
4405
4406  /* When CFA is defined as FP+OFFSET, emulate stack alignment.  */
4407   if (cfa.reg == HARD_FRAME_POINTER_REGNUM && cfa.indirect == 0)
4408     {
4409       head = new_reg_loc_descr (dwarf_fp, 0);
4410       add_loc_descr (&head, int_loc_descriptor (alignment));
4411       add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
4412
4413       add_loc_descr (&head, int_loc_descriptor (offset));
4414       add_loc_descr (&head, new_loc_descr (DW_OP_plus, 0, 0));
4415     }
4416   else
4417     head = new_reg_loc_descr (dwarf_fp, offset);
4418   return head;
4419 }
4420
4421 /* This function fills in aa dw_cfa_location structure from a dwarf location
4422    descriptor sequence.  */
4423
4424 static void
4425 get_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_struct *loc)
4426 {
4427   struct dw_loc_descr_struct *ptr;
4428   cfa->offset = 0;
4429   cfa->base_offset = 0;
4430   cfa->indirect = 0;
4431   cfa->reg = -1;
4432
4433   for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
4434     {
4435       enum dwarf_location_atom op = ptr->dw_loc_opc;
4436
4437       switch (op)
4438         {
4439         case DW_OP_reg0:
4440         case DW_OP_reg1:
4441         case DW_OP_reg2:
4442         case DW_OP_reg3:
4443         case DW_OP_reg4:
4444         case DW_OP_reg5:
4445         case DW_OP_reg6:
4446         case DW_OP_reg7:
4447         case DW_OP_reg8:
4448         case DW_OP_reg9:
4449         case DW_OP_reg10:
4450         case DW_OP_reg11:
4451         case DW_OP_reg12:
4452         case DW_OP_reg13:
4453         case DW_OP_reg14:
4454         case DW_OP_reg15:
4455         case DW_OP_reg16:
4456         case DW_OP_reg17:
4457         case DW_OP_reg18:
4458         case DW_OP_reg19:
4459         case DW_OP_reg20:
4460         case DW_OP_reg21:
4461         case DW_OP_reg22:
4462         case DW_OP_reg23:
4463         case DW_OP_reg24:
4464         case DW_OP_reg25:
4465         case DW_OP_reg26:
4466         case DW_OP_reg27:
4467         case DW_OP_reg28:
4468         case DW_OP_reg29:
4469         case DW_OP_reg30:
4470         case DW_OP_reg31:
4471           cfa->reg = op - DW_OP_reg0;
4472           break;
4473         case DW_OP_regx:
4474           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
4475           break;
4476         case DW_OP_breg0:
4477         case DW_OP_breg1:
4478         case DW_OP_breg2:
4479         case DW_OP_breg3:
4480         case DW_OP_breg4:
4481         case DW_OP_breg5:
4482         case DW_OP_breg6:
4483         case DW_OP_breg7:
4484         case DW_OP_breg8:
4485         case DW_OP_breg9:
4486         case DW_OP_breg10:
4487         case DW_OP_breg11:
4488         case DW_OP_breg12:
4489         case DW_OP_breg13:
4490         case DW_OP_breg14:
4491         case DW_OP_breg15:
4492         case DW_OP_breg16:
4493         case DW_OP_breg17:
4494         case DW_OP_breg18:
4495         case DW_OP_breg19:
4496         case DW_OP_breg20:
4497         case DW_OP_breg21:
4498         case DW_OP_breg22:
4499         case DW_OP_breg23:
4500         case DW_OP_breg24:
4501         case DW_OP_breg25:
4502         case DW_OP_breg26:
4503         case DW_OP_breg27:
4504         case DW_OP_breg28:
4505         case DW_OP_breg29:
4506         case DW_OP_breg30:
4507         case DW_OP_breg31:
4508           cfa->reg = op - DW_OP_breg0;
4509           cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
4510           break;
4511         case DW_OP_bregx:
4512           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
4513           cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
4514           break;
4515         case DW_OP_deref:
4516           cfa->indirect = 1;
4517           break;
4518         case DW_OP_plus_uconst:
4519           cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
4520           break;
4521         default:
4522           internal_error ("DW_LOC_OP %s not implemented",
4523                           dwarf_stack_op_name (ptr->dw_loc_opc));
4524         }
4525     }
4526 }
4527 #endif /* .debug_frame support */
4528 \f
4529 /* And now, the support for symbolic debugging information.  */
4530 #ifdef DWARF2_DEBUGGING_INFO
4531
4532 /* .debug_str support.  */
4533 static int output_indirect_string (void **, void *);
4534
4535 static void dwarf2out_init (const char *);
4536 static void dwarf2out_finish (const char *);
4537 static void dwarf2out_define (unsigned int, const char *);
4538 static void dwarf2out_undef (unsigned int, const char *);
4539 static void dwarf2out_start_source_file (unsigned, const char *);
4540 static void dwarf2out_end_source_file (unsigned);
4541 static void dwarf2out_begin_block (unsigned, unsigned);
4542 static void dwarf2out_end_block (unsigned, unsigned);
4543 static bool dwarf2out_ignore_block (const_tree);
4544 static void dwarf2out_global_decl (tree);
4545 static void dwarf2out_type_decl (tree, int);
4546 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
4547 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
4548                                                  dw_die_ref);
4549 static void dwarf2out_abstract_function (tree);
4550 static void dwarf2out_var_location (rtx);
4551 static void dwarf2out_begin_function (tree);
4552 static void dwarf2out_set_name (tree, tree);
4553
4554 /* The debug hooks structure.  */
4555
4556 const struct gcc_debug_hooks dwarf2_debug_hooks =
4557 {
4558   dwarf2out_init,
4559   dwarf2out_finish,
4560   dwarf2out_define,
4561   dwarf2out_undef,
4562   dwarf2out_start_source_file,
4563   dwarf2out_end_source_file,
4564   dwarf2out_begin_block,
4565   dwarf2out_end_block,
4566   dwarf2out_ignore_block,
4567   dwarf2out_source_line,
4568   dwarf2out_begin_prologue,
4569   debug_nothing_int_charstar,   /* end_prologue */
4570   dwarf2out_end_epilogue,
4571   dwarf2out_begin_function,
4572   debug_nothing_int,            /* end_function */
4573   dwarf2out_decl,               /* function_decl */
4574   dwarf2out_global_decl,
4575   dwarf2out_type_decl,          /* type_decl */
4576   dwarf2out_imported_module_or_decl,
4577   debug_nothing_tree,           /* deferred_inline_function */
4578   /* The DWARF 2 backend tries to reduce debugging bloat by not
4579      emitting the abstract description of inline functions until
4580      something tries to reference them.  */
4581   dwarf2out_abstract_function,  /* outlining_inline_function */
4582   debug_nothing_rtx,            /* label */
4583   debug_nothing_int,            /* handle_pch */
4584   dwarf2out_var_location,
4585   dwarf2out_switch_text_section,
4586   dwarf2out_set_name,
4587   1                             /* start_end_main_source_file */
4588 };
4589 #endif
4590 \f
4591 /* NOTE: In the comments in this file, many references are made to
4592    "Debugging Information Entries".  This term is abbreviated as `DIE'
4593    throughout the remainder of this file.  */
4594
4595 /* An internal representation of the DWARF output is built, and then
4596    walked to generate the DWARF debugging info.  The walk of the internal
4597    representation is done after the entire program has been compiled.
4598    The types below are used to describe the internal representation.  */
4599
4600 /* Various DIE's use offsets relative to the beginning of the
4601    .debug_info section to refer to each other.  */
4602
4603 typedef long int dw_offset;
4604
4605 /* Define typedefs here to avoid circular dependencies.  */
4606
4607 typedef struct dw_attr_struct *dw_attr_ref;
4608 typedef struct dw_line_info_struct *dw_line_info_ref;
4609 typedef struct dw_separate_line_info_struct *dw_separate_line_info_ref;
4610 typedef struct pubname_struct *pubname_ref;
4611 typedef struct dw_ranges_struct *dw_ranges_ref;
4612 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
4613
4614 /* Each entry in the line_info_table maintains the file and
4615    line number associated with the label generated for that
4616    entry.  The label gives the PC value associated with
4617    the line number entry.  */
4618
4619 typedef struct dw_line_info_struct GTY(())
4620 {
4621   unsigned long dw_file_num;
4622   unsigned long dw_line_num;
4623 }
4624 dw_line_info_entry;
4625
4626 /* Line information for functions in separate sections; each one gets its
4627    own sequence.  */
4628 typedef struct dw_separate_line_info_struct GTY(())
4629 {
4630   unsigned long dw_file_num;
4631   unsigned long dw_line_num;
4632   unsigned long function;
4633 }
4634 dw_separate_line_info_entry;
4635
4636 /* Each DIE attribute has a field specifying the attribute kind,
4637    a link to the next attribute in the chain, and an attribute value.
4638    Attributes are typically linked below the DIE they modify.  */
4639
4640 typedef struct dw_attr_struct GTY(())
4641 {
4642   enum dwarf_attribute dw_attr;
4643   dw_val_node dw_attr_val;
4644 }
4645 dw_attr_node;
4646
4647 DEF_VEC_O(dw_attr_node);
4648 DEF_VEC_ALLOC_O(dw_attr_node,gc);
4649
4650 /* The Debugging Information Entry (DIE) structure.  DIEs form a tree.
4651    The children of each node form a circular list linked by
4652    die_sib.  die_child points to the node *before* the "first" child node.  */
4653
4654 typedef struct die_struct GTY((chain_circular ("%h.die_sib")))
4655 {
4656   enum dwarf_tag die_tag;
4657   char *die_symbol;
4658   VEC(dw_attr_node,gc) * die_attr;
4659   dw_die_ref die_parent;
4660   dw_die_ref die_child;
4661   dw_die_ref die_sib;
4662   dw_die_ref die_definition; /* ref from a specification to its definition */
4663   dw_offset die_offset;
4664   unsigned long die_abbrev;
4665   int die_mark;
4666   /* Die is used and must not be pruned as unused.  */
4667   int die_perennial_p;
4668   unsigned int decl_id;
4669 }
4670 die_node;
4671
4672 /* Evaluate 'expr' while 'c' is set to each child of DIE in order.  */
4673 #define FOR_EACH_CHILD(die, c, expr) do {       \
4674   c = die->die_child;                           \
4675   if (c) do {                                   \
4676     c = c->die_sib;                             \
4677     expr;                                       \
4678   } while (c != die->die_child);                \
4679 } while (0)
4680
4681 /* The pubname structure */
4682
4683 typedef struct pubname_struct GTY(())
4684 {
4685   dw_die_ref die;
4686   const char *name;
4687 }
4688 pubname_entry;
4689
4690 DEF_VEC_O(pubname_entry);
4691 DEF_VEC_ALLOC_O(pubname_entry, gc);
4692
4693 struct dw_ranges_struct GTY(())
4694 {
4695   /* If this is positive, it's a block number, otherwise it's a
4696      bitwise-negated index into dw_ranges_by_label.  */
4697   int num;
4698 };
4699
4700 struct dw_ranges_by_label_struct GTY(())
4701 {
4702   const char *begin;
4703   const char *end;
4704 };
4705
4706 /* The limbo die list structure.  */
4707 typedef struct limbo_die_struct GTY(())
4708 {
4709   dw_die_ref die;
4710   tree created_for;
4711   struct limbo_die_struct *next;
4712 }
4713 limbo_die_node;
4714
4715 /* How to start an assembler comment.  */
4716 #ifndef ASM_COMMENT_START
4717 #define ASM_COMMENT_START ";#"
4718 #endif
4719
4720 /* Define a macro which returns nonzero for a TYPE_DECL which was
4721    implicitly generated for a tagged type.
4722
4723    Note that unlike the gcc front end (which generates a NULL named
4724    TYPE_DECL node for each complete tagged type, each array type, and
4725    each function type node created) the g++ front end generates a
4726    _named_ TYPE_DECL node for each tagged type node created.
4727    These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
4728    generate a DW_TAG_typedef DIE for them.  */
4729
4730 #define TYPE_DECL_IS_STUB(decl)                         \
4731   (DECL_NAME (decl) == NULL_TREE                        \
4732    || (DECL_ARTIFICIAL (decl)                           \
4733        && is_tagged_type (TREE_TYPE (decl))             \
4734        && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl)))  \
4735            /* This is necessary for stub decls that     \
4736               appear in nested inline functions.  */    \
4737            || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
4738                && (decl_ultimate_origin (decl)          \
4739                    == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
4740
4741 /* Information concerning the compilation unit's programming
4742    language, and compiler version.  */
4743
4744 /* Fixed size portion of the DWARF compilation unit header.  */
4745 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
4746   (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
4747
4748 /* Fixed size portion of public names info.  */
4749 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
4750
4751 /* Fixed size portion of the address range info.  */
4752 #define DWARF_ARANGES_HEADER_SIZE                                       \
4753   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4,      \
4754                 DWARF2_ADDR_SIZE * 2)                                   \
4755    - DWARF_INITIAL_LENGTH_SIZE)
4756
4757 /* Size of padding portion in the address range info.  It must be
4758    aligned to twice the pointer size.  */
4759 #define DWARF_ARANGES_PAD_SIZE \
4760   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
4761                 DWARF2_ADDR_SIZE * 2)                              \
4762    - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
4763
4764 /* Use assembler line directives if available.  */
4765 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
4766 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
4767 #define DWARF2_ASM_LINE_DEBUG_INFO 1
4768 #else
4769 #define DWARF2_ASM_LINE_DEBUG_INFO 0
4770 #endif
4771 #endif
4772
4773 /* Minimum line offset in a special line info. opcode.
4774    This value was chosen to give a reasonable range of values.  */
4775 #define DWARF_LINE_BASE  -10
4776
4777 /* First special line opcode - leave room for the standard opcodes.  */
4778 #define DWARF_LINE_OPCODE_BASE  10
4779
4780 /* Range of line offsets in a special line info. opcode.  */
4781 #define DWARF_LINE_RANGE  (254-DWARF_LINE_OPCODE_BASE+1)
4782
4783 /* Flag that indicates the initial value of the is_stmt_start flag.
4784    In the present implementation, we do not mark any lines as
4785    the beginning of a source statement, because that information
4786    is not made available by the GCC front-end.  */
4787 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
4788
4789 #ifdef DWARF2_DEBUGGING_INFO
4790 /* This location is used by calc_die_sizes() to keep track
4791    the offset of each DIE within the .debug_info section.  */
4792 static unsigned long next_die_offset;
4793 #endif
4794
4795 /* Record the root of the DIE's built for the current compilation unit.  */
4796 static GTY(()) dw_die_ref comp_unit_die;
4797
4798 /* A list of DIEs with a NULL parent waiting to be relocated.  */
4799 static GTY(()) limbo_die_node *limbo_die_list;
4800
4801 /* Filenames referenced by this compilation unit.  */
4802 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
4803
4804 /* A hash table of references to DIE's that describe declarations.
4805    The key is a DECL_UID() which is a unique number identifying each decl.  */
4806 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
4807
4808 /* A hash table of references to DIE's that describe COMMON blocks.
4809    The key is DECL_UID() ^ die_parent.  */
4810 static GTY ((param_is (struct die_struct))) htab_t common_block_die_table;
4811
4812 /* Node of the variable location list.  */
4813 struct var_loc_node GTY ((chain_next ("%h.next")))
4814 {
4815   rtx GTY (()) var_loc_note;
4816   const char * GTY (()) label;
4817   const char * GTY (()) section_label;
4818   struct var_loc_node * GTY (()) next;
4819 };
4820
4821 /* Variable location list.  */
4822 struct var_loc_list_def GTY (())
4823 {
4824   struct var_loc_node * GTY (()) first;
4825
4826   /* Do not mark the last element of the chained list because
4827      it is marked through the chain.  */
4828   struct var_loc_node * GTY ((skip ("%h"))) last;
4829
4830   /* DECL_UID of the variable decl.  */
4831   unsigned int decl_id;
4832 };
4833 typedef struct var_loc_list_def var_loc_list;
4834
4835
4836 /* Table of decl location linked lists.  */
4837 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
4838
4839 /* A pointer to the base of a list of references to DIE's that
4840    are uniquely identified by their tag, presence/absence of
4841    children DIE's, and list of attribute/value pairs.  */
4842 static GTY((length ("abbrev_die_table_allocated")))
4843   dw_die_ref *abbrev_die_table;
4844
4845 /* Number of elements currently allocated for abbrev_die_table.  */
4846 static GTY(()) unsigned abbrev_die_table_allocated;
4847
4848 /* Number of elements in type_die_table currently in use.  */
4849 static GTY(()) unsigned abbrev_die_table_in_use;
4850
4851 /* Size (in elements) of increments by which we may expand the
4852    abbrev_die_table.  */
4853 #define ABBREV_DIE_TABLE_INCREMENT 256
4854
4855 /* A pointer to the base of a table that contains line information
4856    for each source code line in .text in the compilation unit.  */
4857 static GTY((length ("line_info_table_allocated")))
4858      dw_line_info_ref line_info_table;
4859
4860 /* Number of elements currently allocated for line_info_table.  */
4861 static GTY(()) unsigned line_info_table_allocated;
4862
4863 /* Number of elements in line_info_table currently in use.  */
4864 static GTY(()) unsigned line_info_table_in_use;
4865
4866 /* A pointer to the base of a table that contains line information
4867    for each source code line outside of .text in the compilation unit.  */
4868 static GTY ((length ("separate_line_info_table_allocated")))
4869      dw_separate_line_info_ref separate_line_info_table;
4870
4871 /* Number of elements currently allocated for separate_line_info_table.  */
4872 static GTY(()) unsigned separate_line_info_table_allocated;
4873
4874 /* Number of elements in separate_line_info_table currently in use.  */
4875 static GTY(()) unsigned separate_line_info_table_in_use;
4876
4877 /* Size (in elements) of increments by which we may expand the
4878    line_info_table.  */
4879 #define LINE_INFO_TABLE_INCREMENT 1024
4880
4881 /* A pointer to the base of a table that contains a list of publicly
4882    accessible names.  */
4883 static GTY (()) VEC (pubname_entry, gc) *  pubname_table;
4884
4885 /* A pointer to the base of a table that contains a list of publicly
4886    accessible types.  */
4887 static GTY (()) VEC (pubname_entry, gc) * pubtype_table;
4888
4889 /* Array of dies for which we should generate .debug_arange info.  */
4890 static GTY((length ("arange_table_allocated"))) dw_die_ref *arange_table;
4891
4892 /* Number of elements currently allocated for arange_table.  */
4893 static GTY(()) unsigned arange_table_allocated;
4894
4895 /* Number of elements in arange_table currently in use.  */
4896 static GTY(()) unsigned arange_table_in_use;
4897
4898 /* Size (in elements) of increments by which we may expand the
4899    arange_table.  */
4900 #define ARANGE_TABLE_INCREMENT 64
4901
4902 /* Array of dies for which we should generate .debug_ranges info.  */
4903 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
4904
4905 /* Number of elements currently allocated for ranges_table.  */
4906 static GTY(()) unsigned ranges_table_allocated;
4907
4908 /* Number of elements in ranges_table currently in use.  */
4909 static GTY(()) unsigned ranges_table_in_use;
4910
4911 /* Array of pairs of labels referenced in ranges_table.  */
4912 static GTY ((length ("ranges_by_label_allocated")))
4913      dw_ranges_by_label_ref ranges_by_label;
4914
4915 /* Number of elements currently allocated for ranges_by_label.  */
4916 static GTY(()) unsigned ranges_by_label_allocated;
4917
4918 /* Number of elements in ranges_by_label currently in use.  */
4919 static GTY(()) unsigned ranges_by_label_in_use;
4920
4921 /* Size (in elements) of increments by which we may expand the
4922    ranges_table.  */
4923 #define RANGES_TABLE_INCREMENT 64
4924
4925 /* Whether we have location lists that need outputting */
4926 static GTY(()) bool have_location_lists;
4927
4928 /* Unique label counter.  */
4929 static GTY(()) unsigned int loclabel_num;
4930
4931 #ifdef DWARF2_DEBUGGING_INFO
4932 /* Record whether the function being analyzed contains inlined functions.  */
4933 static int current_function_has_inlines;
4934 #endif
4935 #if 0 && defined (MIPS_DEBUGGING_INFO)
4936 static int comp_unit_has_inlines;
4937 #endif
4938
4939 /* The last file entry emitted by maybe_emit_file().  */
4940 static GTY(()) struct dwarf_file_data * last_emitted_file;
4941
4942 /* Number of internal labels generated by gen_internal_sym().  */
4943 static GTY(()) int label_num;
4944
4945 /* Cached result of previous call to lookup_filename.  */
4946 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
4947
4948 #ifdef DWARF2_DEBUGGING_INFO
4949
4950 /* Offset from the "steady-state frame pointer" to the frame base,
4951    within the current function.  */
4952 static HOST_WIDE_INT frame_pointer_fb_offset;
4953
4954 /* Forward declarations for functions defined in this file.  */
4955
4956 static int is_pseudo_reg (const_rtx);
4957 static tree type_main_variant (tree);
4958 static int is_tagged_type (const_tree);
4959 static const char *dwarf_tag_name (unsigned);
4960 static const char *dwarf_attr_name (unsigned);
4961 static const char *dwarf_form_name (unsigned);
4962 static tree decl_ultimate_origin (const_tree);
4963 static tree decl_class_context (tree);
4964 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
4965 static inline enum dw_val_class AT_class (dw_attr_ref);
4966 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
4967 static inline unsigned AT_flag (dw_attr_ref);
4968 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
4969 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
4970 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
4971 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
4972 static void add_AT_long_long (dw_die_ref, enum dwarf_attribute, unsigned long,
4973                               unsigned long);
4974 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
4975                                unsigned int, unsigned char *);
4976 static hashval_t debug_str_do_hash (const void *);
4977 static int debug_str_eq (const void *, const void *);
4978 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
4979 static inline const char *AT_string (dw_attr_ref);
4980 static int AT_string_form (dw_attr_ref);
4981 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
4982 static void add_AT_specification (dw_die_ref, dw_die_ref);
4983 static inline dw_die_ref AT_ref (dw_attr_ref);
4984 static inline int AT_ref_external (dw_attr_ref);
4985 static inline void set_AT_ref_external (dw_attr_ref, int);
4986 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
4987 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
4988 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
4989 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
4990                              dw_loc_list_ref);
4991 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
4992 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
4993 static inline rtx AT_addr (dw_attr_ref);
4994 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
4995 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
4996 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
4997 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
4998                            unsigned HOST_WIDE_INT);
4999 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
5000                                unsigned long);
5001 static inline const char *AT_lbl (dw_attr_ref);
5002 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
5003 static const char *get_AT_low_pc (dw_die_ref);
5004 static const char *get_AT_hi_pc (dw_die_ref);
5005 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
5006 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
5007 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
5008 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
5009 static bool is_c_family (void);
5010 static bool is_cxx (void);
5011 static bool is_java (void);
5012 static bool is_fortran (void);
5013 static bool is_ada (void);
5014 static void remove_AT (dw_die_ref, enum dwarf_attribute);
5015 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
5016 static void add_child_die (dw_die_ref, dw_die_ref);
5017 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
5018 static dw_die_ref lookup_type_die (tree);
5019 static void equate_type_number_to_die (tree, dw_die_ref);
5020 static hashval_t decl_die_table_hash (const void *);
5021 static int decl_die_table_eq (const void *, const void *);
5022 static dw_die_ref lookup_decl_die (tree);
5023 static hashval_t common_block_die_table_hash (const void *);
5024 static int common_block_die_table_eq (const void *, const void *);
5025 static hashval_t decl_loc_table_hash (const void *);
5026 static int decl_loc_table_eq (const void *, const void *);
5027 static var_loc_list *lookup_decl_loc (const_tree);
5028 static void equate_decl_number_to_die (tree, dw_die_ref);
5029 static void add_var_loc_to_decl (tree, struct var_loc_node *);
5030 static void print_spaces (FILE *);
5031 static void print_die (dw_die_ref, FILE *);
5032 static void print_dwarf_line_table (FILE *);
5033 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
5034 static dw_die_ref pop_compile_unit (dw_die_ref);
5035 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
5036 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
5037 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
5038 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
5039 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
5040 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
5041 static int same_die_p (dw_die_ref, dw_die_ref, int *);
5042 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
5043 static void compute_section_prefix (dw_die_ref);
5044 static int is_type_die (dw_die_ref);
5045 static int is_comdat_die (dw_die_ref);
5046 static int is_symbol_die (dw_die_ref);
5047 static void assign_symbol_names (dw_die_ref);
5048 static void break_out_includes (dw_die_ref);
5049 static hashval_t htab_cu_hash (const void *);
5050 static int htab_cu_eq (const void *, const void *);
5051 static void htab_cu_del (void *);
5052 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
5053 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
5054 static void add_sibling_attributes (dw_die_ref);
5055 static void build_abbrev_table (dw_die_ref);
5056 static void output_location_lists (dw_die_ref);
5057 static int constant_size (unsigned HOST_WIDE_INT);
5058 static unsigned long size_of_die (dw_die_ref);
5059 static void calc_die_sizes (dw_die_ref);
5060 static void mark_dies (dw_die_ref);
5061 static void unmark_dies (dw_die_ref);
5062 static void unmark_all_dies (dw_die_ref);
5063 static unsigned long size_of_pubnames (VEC (pubname_entry,gc) *);
5064 static unsigned long size_of_aranges (void);
5065 static enum dwarf_form value_format (dw_attr_ref);
5066 static void output_value_format (dw_attr_ref);
5067 static void output_abbrev_section (void);
5068 static void output_die_symbol (dw_die_ref);
5069 static void output_die (dw_die_ref);
5070 static void output_compilation_unit_header (void);
5071 static void output_comp_unit (dw_die_ref, int);
5072 static const char *dwarf2_name (tree, int);
5073 static void add_pubname (tree, dw_die_ref);
5074 static void add_pubname_string (const char *, dw_die_ref);
5075 static void add_pubtype (tree, dw_die_ref);
5076 static void output_pubnames (VEC (pubname_entry,gc) *);
5077 static void add_arange (tree, dw_die_ref);
5078 static void output_aranges (void);
5079 static unsigned int add_ranges_num (int);
5080 static unsigned int add_ranges (const_tree);
5081 static unsigned int add_ranges_by_labels (const char *, const char *);
5082 static void output_ranges (void);
5083 static void output_line_info (void);
5084 static void output_file_names (void);
5085 static dw_die_ref base_type_die (tree);
5086 static int is_base_type (tree);
5087 static bool is_subrange_type (const_tree);
5088 static dw_die_ref subrange_type_die (tree, dw_die_ref);
5089 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
5090 static int type_is_enum (const_tree);
5091 static unsigned int dbx_reg_number (const_rtx);
5092 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
5093 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
5094 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
5095                                                 enum var_init_status);
5096 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
5097                                                      enum var_init_status);
5098 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
5099                                          enum var_init_status);
5100 static int is_based_loc (const_rtx);
5101 static dw_loc_descr_ref mem_loc_descriptor (rtx, enum machine_mode mode,
5102                                             enum var_init_status);
5103 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
5104                                                enum var_init_status);
5105 static dw_loc_descr_ref loc_descriptor (rtx, enum var_init_status);
5106 static dw_loc_descr_ref loc_descriptor_from_tree_1 (tree, int);
5107 static dw_loc_descr_ref loc_descriptor_from_tree (tree);
5108 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
5109 static tree field_type (const_tree);
5110 static unsigned int simple_type_align_in_bits (const_tree);
5111 static unsigned int simple_decl_align_in_bits (const_tree);
5112 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
5113 static HOST_WIDE_INT field_byte_offset (const_tree);
5114 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
5115                                          dw_loc_descr_ref);
5116 static void add_data_member_location_attribute (dw_die_ref, tree);
5117 static void add_const_value_attribute (dw_die_ref, rtx);
5118 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
5119 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
5120 static void insert_float (const_rtx, unsigned char *);
5121 static rtx rtl_for_decl_location (tree);
5122 static void add_location_or_const_value_attribute (dw_die_ref, tree,
5123                                                    enum dwarf_attribute);
5124 static void tree_add_const_value_attribute (dw_die_ref, tree);
5125 static void add_name_attribute (dw_die_ref, const char *);
5126 static void add_comp_dir_attribute (dw_die_ref);
5127 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
5128 static void add_subscript_info (dw_die_ref, tree, bool);
5129 static void add_byte_size_attribute (dw_die_ref, tree);
5130 static void add_bit_offset_attribute (dw_die_ref, tree);
5131 static void add_bit_size_attribute (dw_die_ref, tree);
5132 static void add_prototyped_attribute (dw_die_ref, tree);
5133 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
5134 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
5135 static void add_src_coords_attributes (dw_die_ref, tree);
5136 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
5137 static void push_decl_scope (tree);
5138 static void pop_decl_scope (void);
5139 static dw_die_ref scope_die_for (tree, dw_die_ref);
5140 static inline int local_scope_p (dw_die_ref);
5141 static inline int class_scope_p (dw_die_ref);
5142 static inline int class_or_namespace_scope_p (dw_die_ref);
5143 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
5144 static void add_calling_convention_attribute (dw_die_ref, tree);
5145 static const char *type_tag (const_tree);
5146 static tree member_declared_type (const_tree);
5147 #if 0
5148 static const char *decl_start_label (tree);
5149 #endif
5150 static void gen_array_type_die (tree, dw_die_ref);
5151 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
5152 #if 0
5153 static void gen_entry_point_die (tree, dw_die_ref);
5154 #endif
5155 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
5156 static dw_die_ref gen_formal_parameter_die (tree, tree, dw_die_ref);
5157 static void gen_unspecified_parameters_die (tree, dw_die_ref);
5158 static void gen_formal_types_die (tree, dw_die_ref);
5159 static void gen_subprogram_die (tree, dw_die_ref);
5160 static void gen_variable_die (tree, tree, dw_die_ref);
5161 static void gen_const_die (tree, dw_die_ref);
5162 static void gen_label_die (tree, dw_die_ref);
5163 static void gen_lexical_block_die (tree, dw_die_ref, int);
5164 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
5165 static void gen_field_die (tree, dw_die_ref);
5166 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
5167 static dw_die_ref gen_compile_unit_die (const char *);
5168 static void gen_inheritance_die (tree, tree, dw_die_ref);
5169 static void gen_member_die (tree, dw_die_ref);
5170 static void gen_struct_or_union_type_die (tree, dw_die_ref,
5171                                                 enum debug_info_usage);
5172 static void gen_subroutine_type_die (tree, dw_die_ref);
5173 static void gen_typedef_die (tree, dw_die_ref);
5174 static void gen_type_die (tree, dw_die_ref);
5175 static void gen_block_die (tree, dw_die_ref, int);
5176 static void decls_for_scope (tree, dw_die_ref, int);
5177 static int is_redundant_typedef (const_tree);
5178 static void gen_namespace_die (tree, dw_die_ref);
5179 static void gen_decl_die (tree, tree, dw_die_ref);
5180 static dw_die_ref force_decl_die (tree);
5181 static dw_die_ref force_type_die (tree);
5182 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
5183 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
5184 static struct dwarf_file_data * lookup_filename (const char *);
5185 static void retry_incomplete_types (void);
5186 static void gen_type_die_for_member (tree, tree, dw_die_ref);
5187 static void splice_child_die (dw_die_ref, dw_die_ref);
5188 static int file_info_cmp (const void *, const void *);
5189 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
5190                                      const char *, const char *, unsigned);
5191 static void add_loc_descr_to_loc_list (dw_loc_list_ref *, dw_loc_descr_ref,
5192                                        const char *, const char *,
5193                                        const char *);
5194 static void output_loc_list (dw_loc_list_ref);
5195 static char *gen_internal_sym (const char *);
5196
5197 static void prune_unmark_dies (dw_die_ref);
5198 static void prune_unused_types_mark (dw_die_ref, int);
5199 static void prune_unused_types_walk (dw_die_ref);
5200 static void prune_unused_types_walk_attribs (dw_die_ref);
5201 static void prune_unused_types_prune (dw_die_ref);
5202 static void prune_unused_types (void);
5203 static int maybe_emit_file (struct dwarf_file_data *fd);
5204
5205 /* Section names used to hold DWARF debugging information.  */
5206 #ifndef DEBUG_INFO_SECTION
5207 #define DEBUG_INFO_SECTION      ".debug_info"
5208 #endif
5209 #ifndef DEBUG_ABBREV_SECTION
5210 #define DEBUG_ABBREV_SECTION    ".debug_abbrev"
5211 #endif
5212 #ifndef DEBUG_ARANGES_SECTION
5213 #define DEBUG_ARANGES_SECTION   ".debug_aranges"
5214 #endif
5215 #ifndef DEBUG_MACINFO_SECTION
5216 #define DEBUG_MACINFO_SECTION   ".debug_macinfo"
5217 #endif
5218 #ifndef DEBUG_LINE_SECTION
5219 #define DEBUG_LINE_SECTION      ".debug_line"
5220 #endif
5221 #ifndef DEBUG_LOC_SECTION
5222 #define DEBUG_LOC_SECTION       ".debug_loc"
5223 #endif
5224 #ifndef DEBUG_PUBNAMES_SECTION
5225 #define DEBUG_PUBNAMES_SECTION  ".debug_pubnames"
5226 #endif
5227 #ifndef DEBUG_STR_SECTION
5228 #define DEBUG_STR_SECTION       ".debug_str"
5229 #endif
5230 #ifndef DEBUG_RANGES_SECTION
5231 #define DEBUG_RANGES_SECTION    ".debug_ranges"
5232 #endif
5233
5234 /* Standard ELF section names for compiled code and data.  */
5235 #ifndef TEXT_SECTION_NAME
5236 #define TEXT_SECTION_NAME       ".text"
5237 #endif
5238
5239 /* Section flags for .debug_str section.  */
5240 #define DEBUG_STR_SECTION_FLAGS \
5241   (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings               \
5242    ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1        \
5243    : SECTION_DEBUG)
5244
5245 /* Labels we insert at beginning sections we can reference instead of
5246    the section names themselves.  */
5247
5248 #ifndef TEXT_SECTION_LABEL
5249 #define TEXT_SECTION_LABEL              "Ltext"
5250 #endif
5251 #ifndef COLD_TEXT_SECTION_LABEL
5252 #define COLD_TEXT_SECTION_LABEL         "Ltext_cold"
5253 #endif
5254 #ifndef DEBUG_LINE_SECTION_LABEL
5255 #define DEBUG_LINE_SECTION_LABEL        "Ldebug_line"
5256 #endif
5257 #ifndef DEBUG_INFO_SECTION_LABEL
5258 #define DEBUG_INFO_SECTION_LABEL        "Ldebug_info"
5259 #endif
5260 #ifndef DEBUG_ABBREV_SECTION_LABEL
5261 #define DEBUG_ABBREV_SECTION_LABEL      "Ldebug_abbrev"
5262 #endif
5263 #ifndef DEBUG_LOC_SECTION_LABEL
5264 #define DEBUG_LOC_SECTION_LABEL         "Ldebug_loc"
5265 #endif
5266 #ifndef DEBUG_RANGES_SECTION_LABEL
5267 #define DEBUG_RANGES_SECTION_LABEL      "Ldebug_ranges"
5268 #endif
5269 #ifndef DEBUG_MACINFO_SECTION_LABEL
5270 #define DEBUG_MACINFO_SECTION_LABEL     "Ldebug_macinfo"
5271 #endif
5272
5273 /* Definitions of defaults for formats and names of various special
5274    (artificial) labels which may be generated within this file (when the -g
5275    options is used and DWARF2_DEBUGGING_INFO is in effect.
5276    If necessary, these may be overridden from within the tm.h file, but
5277    typically, overriding these defaults is unnecessary.  */
5278
5279 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
5280 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5281 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5282 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
5283 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5284 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5285 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5286 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5287 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5288 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
5289
5290 #ifndef TEXT_END_LABEL
5291 #define TEXT_END_LABEL          "Letext"
5292 #endif
5293 #ifndef COLD_END_LABEL
5294 #define COLD_END_LABEL          "Letext_cold"
5295 #endif
5296 #ifndef BLOCK_BEGIN_LABEL
5297 #define BLOCK_BEGIN_LABEL       "LBB"
5298 #endif
5299 #ifndef BLOCK_END_LABEL
5300 #define BLOCK_END_LABEL         "LBE"
5301 #endif
5302 #ifndef LINE_CODE_LABEL
5303 #define LINE_CODE_LABEL         "LM"
5304 #endif
5305 #ifndef SEPARATE_LINE_CODE_LABEL
5306 #define SEPARATE_LINE_CODE_LABEL        "LSM"
5307 #endif
5308
5309 \f
5310 /* We allow a language front-end to designate a function that is to be
5311    called to "demangle" any name before it is put into a DIE.  */
5312
5313 static const char *(*demangle_name_func) (const char *);
5314
5315 void
5316 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
5317 {
5318   demangle_name_func = func;
5319 }
5320
5321 /* Test if rtl node points to a pseudo register.  */
5322
5323 static inline int
5324 is_pseudo_reg (const_rtx rtl)
5325 {
5326   return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
5327           || (GET_CODE (rtl) == SUBREG
5328               && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
5329 }
5330
5331 /* Return a reference to a type, with its const and volatile qualifiers
5332    removed.  */
5333
5334 static inline tree
5335 type_main_variant (tree type)
5336 {
5337   type = TYPE_MAIN_VARIANT (type);
5338
5339   /* ??? There really should be only one main variant among any group of
5340      variants of a given type (and all of the MAIN_VARIANT values for all
5341      members of the group should point to that one type) but sometimes the C
5342      front-end messes this up for array types, so we work around that bug
5343      here.  */
5344   if (TREE_CODE (type) == ARRAY_TYPE)
5345     while (type != TYPE_MAIN_VARIANT (type))
5346       type = TYPE_MAIN_VARIANT (type);
5347
5348   return type;
5349 }
5350
5351 /* Return nonzero if the given type node represents a tagged type.  */
5352
5353 static inline int
5354 is_tagged_type (const_tree type)
5355 {
5356   enum tree_code code = TREE_CODE (type);
5357
5358   return (code == RECORD_TYPE || code == UNION_TYPE
5359           || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
5360 }
5361
5362 /* Convert a DIE tag into its string name.  */
5363
5364 static const char *
5365 dwarf_tag_name (unsigned int tag)
5366 {
5367   switch (tag)
5368     {
5369     case DW_TAG_padding:
5370       return "DW_TAG_padding";
5371     case DW_TAG_array_type:
5372       return "DW_TAG_array_type";
5373     case DW_TAG_class_type:
5374       return "DW_TAG_class_type";
5375     case DW_TAG_entry_point:
5376       return "DW_TAG_entry_point";
5377     case DW_TAG_enumeration_type:
5378       return "DW_TAG_enumeration_type";
5379     case DW_TAG_formal_parameter:
5380       return "DW_TAG_formal_parameter";
5381     case DW_TAG_imported_declaration:
5382       return "DW_TAG_imported_declaration";
5383     case DW_TAG_label:
5384       return "DW_TAG_label";
5385     case DW_TAG_lexical_block:
5386       return "DW_TAG_lexical_block";
5387     case DW_TAG_member:
5388       return "DW_TAG_member";
5389     case DW_TAG_pointer_type:
5390       return "DW_TAG_pointer_type";
5391     case DW_TAG_reference_type:
5392       return "DW_TAG_reference_type";
5393     case DW_TAG_compile_unit:
5394       return "DW_TAG_compile_unit";
5395     case DW_TAG_string_type:
5396       return "DW_TAG_string_type";
5397     case DW_TAG_structure_type:
5398       return "DW_TAG_structure_type";
5399     case DW_TAG_subroutine_type:
5400       return "DW_TAG_subroutine_type";
5401     case DW_TAG_typedef:
5402       return "DW_TAG_typedef";
5403     case DW_TAG_union_type:
5404       return "DW_TAG_union_type";
5405     case DW_TAG_unspecified_parameters:
5406       return "DW_TAG_unspecified_parameters";
5407     case DW_TAG_variant:
5408       return "DW_TAG_variant";
5409     case DW_TAG_common_block:
5410       return "DW_TAG_common_block";
5411     case DW_TAG_common_inclusion:
5412       return "DW_TAG_common_inclusion";
5413     case DW_TAG_inheritance:
5414       return "DW_TAG_inheritance";
5415     case DW_TAG_inlined_subroutine:
5416       return "DW_TAG_inlined_subroutine";
5417     case DW_TAG_module:
5418       return "DW_TAG_module";
5419     case DW_TAG_ptr_to_member_type:
5420       return "DW_TAG_ptr_to_member_type";
5421     case DW_TAG_set_type:
5422       return "DW_TAG_set_type";
5423     case DW_TAG_subrange_type:
5424       return "DW_TAG_subrange_type";
5425     case DW_TAG_with_stmt:
5426       return "DW_TAG_with_stmt";
5427     case DW_TAG_access_declaration:
5428       return "DW_TAG_access_declaration";
5429     case DW_TAG_base_type:
5430       return "DW_TAG_base_type";
5431     case DW_TAG_catch_block:
5432       return "DW_TAG_catch_block";
5433     case DW_TAG_const_type:
5434       return "DW_TAG_const_type";
5435     case DW_TAG_constant:
5436       return "DW_TAG_constant";
5437     case DW_TAG_enumerator:
5438       return "DW_TAG_enumerator";
5439     case DW_TAG_file_type:
5440       return "DW_TAG_file_type";
5441     case DW_TAG_friend:
5442       return "DW_TAG_friend";
5443     case DW_TAG_namelist:
5444       return "DW_TAG_namelist";
5445     case DW_TAG_namelist_item:
5446       return "DW_TAG_namelist_item";
5447     case DW_TAG_packed_type:
5448       return "DW_TAG_packed_type";
5449     case DW_TAG_subprogram:
5450       return "DW_TAG_subprogram";
5451     case DW_TAG_template_type_param:
5452       return "DW_TAG_template_type_param";
5453     case DW_TAG_template_value_param:
5454       return "DW_TAG_template_value_param";
5455     case DW_TAG_thrown_type:
5456       return "DW_TAG_thrown_type";
5457     case DW_TAG_try_block:
5458       return "DW_TAG_try_block";
5459     case DW_TAG_variant_part:
5460       return "DW_TAG_variant_part";
5461     case DW_TAG_variable:
5462       return "DW_TAG_variable";
5463     case DW_TAG_volatile_type:
5464       return "DW_TAG_volatile_type";
5465     case DW_TAG_dwarf_procedure:
5466       return "DW_TAG_dwarf_procedure";
5467     case DW_TAG_restrict_type:
5468       return "DW_TAG_restrict_type";
5469     case DW_TAG_interface_type:
5470       return "DW_TAG_interface_type";
5471     case DW_TAG_namespace:
5472       return "DW_TAG_namespace";
5473     case DW_TAG_imported_module:
5474       return "DW_TAG_imported_module";
5475     case DW_TAG_unspecified_type:
5476       return "DW_TAG_unspecified_type";
5477     case DW_TAG_partial_unit:
5478       return "DW_TAG_partial_unit";
5479     case DW_TAG_imported_unit:
5480       return "DW_TAG_imported_unit";
5481     case DW_TAG_condition:
5482       return "DW_TAG_condition";
5483     case DW_TAG_shared_type:
5484       return "DW_TAG_shared_type";
5485     case DW_TAG_MIPS_loop:
5486       return "DW_TAG_MIPS_loop";
5487     case DW_TAG_format_label:
5488       return "DW_TAG_format_label";
5489     case DW_TAG_function_template:
5490       return "DW_TAG_function_template";
5491     case DW_TAG_class_template:
5492       return "DW_TAG_class_template";
5493     case DW_TAG_GNU_BINCL:
5494       return "DW_TAG_GNU_BINCL";
5495     case DW_TAG_GNU_EINCL:
5496       return "DW_TAG_GNU_EINCL";
5497     default:
5498       return "DW_TAG_<unknown>";
5499     }
5500 }
5501
5502 /* Convert a DWARF attribute code into its string name.  */
5503
5504 static const char *
5505 dwarf_attr_name (unsigned int attr)
5506 {
5507   switch (attr)
5508     {
5509     case DW_AT_sibling:
5510       return "DW_AT_sibling";
5511     case DW_AT_location:
5512       return "DW_AT_location";
5513     case DW_AT_name:
5514       return "DW_AT_name";
5515     case DW_AT_ordering:
5516       return "DW_AT_ordering";
5517     case DW_AT_subscr_data:
5518       return "DW_AT_subscr_data";
5519     case DW_AT_byte_size:
5520       return "DW_AT_byte_size";
5521     case DW_AT_bit_offset:
5522       return "DW_AT_bit_offset";
5523     case DW_AT_bit_size:
5524       return "DW_AT_bit_size";
5525     case DW_AT_element_list:
5526       return "DW_AT_element_list";
5527     case DW_AT_stmt_list:
5528       return "DW_AT_stmt_list";
5529     case DW_AT_low_pc:
5530       return "DW_AT_low_pc";
5531     case DW_AT_high_pc:
5532       return "DW_AT_high_pc";
5533     case DW_AT_language:
5534       return "DW_AT_language";
5535     case DW_AT_member:
5536       return "DW_AT_member";
5537     case DW_AT_discr:
5538       return "DW_AT_discr";
5539     case DW_AT_discr_value:
5540       return "DW_AT_discr_value";
5541     case DW_AT_visibility:
5542       return "DW_AT_visibility";
5543     case DW_AT_import:
5544       return "DW_AT_import";
5545     case DW_AT_string_length:
5546       return "DW_AT_string_length";
5547     case DW_AT_common_reference:
5548       return "DW_AT_common_reference";
5549     case DW_AT_comp_dir:
5550       return "DW_AT_comp_dir";
5551     case DW_AT_const_value:
5552       return "DW_AT_const_value";
5553     case DW_AT_containing_type:
5554       return "DW_AT_containing_type";
5555     case DW_AT_default_value:
5556       return "DW_AT_default_value";
5557     case DW_AT_inline:
5558       return "DW_AT_inline";
5559     case DW_AT_is_optional:
5560       return "DW_AT_is_optional";
5561     case DW_AT_lower_bound:
5562       return "DW_AT_lower_bound";
5563     case DW_AT_producer:
5564       return "DW_AT_producer";
5565     case DW_AT_prototyped:
5566       return "DW_AT_prototyped";
5567     case DW_AT_return_addr:
5568       return "DW_AT_return_addr";
5569     case DW_AT_start_scope:
5570       return "DW_AT_start_scope";
5571     case DW_AT_bit_stride:
5572       return "DW_AT_bit_stride";
5573     case DW_AT_upper_bound:
5574       return "DW_AT_upper_bound";
5575     case DW_AT_abstract_origin:
5576       return "DW_AT_abstract_origin";
5577     case DW_AT_accessibility:
5578       return "DW_AT_accessibility";
5579     case DW_AT_address_class:
5580       return "DW_AT_address_class";
5581     case DW_AT_artificial:
5582       return "DW_AT_artificial";
5583     case DW_AT_base_types:
5584       return "DW_AT_base_types";
5585     case DW_AT_calling_convention:
5586       return "DW_AT_calling_convention";
5587     case DW_AT_count:
5588       return "DW_AT_count";
5589     case DW_AT_data_member_location:
5590       return "DW_AT_data_member_location";
5591     case DW_AT_decl_column:
5592       return "DW_AT_decl_column";
5593     case DW_AT_decl_file:
5594       return "DW_AT_decl_file";
5595     case DW_AT_decl_line:
5596       return "DW_AT_decl_line";
5597     case DW_AT_declaration:
5598       return "DW_AT_declaration";
5599     case DW_AT_discr_list:
5600       return "DW_AT_discr_list";
5601     case DW_AT_encoding:
5602       return "DW_AT_encoding";
5603     case DW_AT_external:
5604       return "DW_AT_external";
5605     case DW_AT_explicit:
5606       return "DW_AT_explicit";
5607     case DW_AT_frame_base:
5608       return "DW_AT_frame_base";
5609     case DW_AT_friend:
5610       return "DW_AT_friend";
5611     case DW_AT_identifier_case:
5612       return "DW_AT_identifier_case";
5613     case DW_AT_macro_info:
5614       return "DW_AT_macro_info";
5615     case DW_AT_namelist_items:
5616       return "DW_AT_namelist_items";
5617     case DW_AT_priority:
5618       return "DW_AT_priority";
5619     case DW_AT_segment:
5620       return "DW_AT_segment";
5621     case DW_AT_specification:
5622       return "DW_AT_specification";
5623     case DW_AT_static_link:
5624       return "DW_AT_static_link";
5625     case DW_AT_type:
5626       return "DW_AT_type";
5627     case DW_AT_use_location:
5628       return "DW_AT_use_location";
5629     case DW_AT_variable_parameter:
5630       return "DW_AT_variable_parameter";
5631     case DW_AT_virtuality:
5632       return "DW_AT_virtuality";
5633     case DW_AT_vtable_elem_location:
5634       return "DW_AT_vtable_elem_location";
5635
5636     case DW_AT_allocated:
5637       return "DW_AT_allocated";
5638     case DW_AT_associated:
5639       return "DW_AT_associated";
5640     case DW_AT_data_location:
5641       return "DW_AT_data_location";
5642     case DW_AT_byte_stride:
5643       return "DW_AT_byte_stride";
5644     case DW_AT_entry_pc:
5645       return "DW_AT_entry_pc";
5646     case DW_AT_use_UTF8:
5647       return "DW_AT_use_UTF8";
5648     case DW_AT_extension:
5649       return "DW_AT_extension";
5650     case DW_AT_ranges:
5651       return "DW_AT_ranges";
5652     case DW_AT_trampoline:
5653       return "DW_AT_trampoline";
5654     case DW_AT_call_column:
5655       return "DW_AT_call_column";
5656     case DW_AT_call_file:
5657       return "DW_AT_call_file";
5658     case DW_AT_call_line:
5659       return "DW_AT_call_line";
5660
5661     case DW_AT_MIPS_fde:
5662       return "DW_AT_MIPS_fde";
5663     case DW_AT_MIPS_loop_begin:
5664       return "DW_AT_MIPS_loop_begin";
5665     case DW_AT_MIPS_tail_loop_begin:
5666       return "DW_AT_MIPS_tail_loop_begin";
5667     case DW_AT_MIPS_epilog_begin:
5668       return "DW_AT_MIPS_epilog_begin";
5669     case DW_AT_MIPS_loop_unroll_factor:
5670       return "DW_AT_MIPS_loop_unroll_factor";
5671     case DW_AT_MIPS_software_pipeline_depth:
5672       return "DW_AT_MIPS_software_pipeline_depth";
5673     case DW_AT_MIPS_linkage_name:
5674       return "DW_AT_MIPS_linkage_name";
5675     case DW_AT_MIPS_stride:
5676       return "DW_AT_MIPS_stride";
5677     case DW_AT_MIPS_abstract_name:
5678       return "DW_AT_MIPS_abstract_name";
5679     case DW_AT_MIPS_clone_origin:
5680       return "DW_AT_MIPS_clone_origin";
5681     case DW_AT_MIPS_has_inlines:
5682       return "DW_AT_MIPS_has_inlines";
5683
5684     case DW_AT_sf_names:
5685       return "DW_AT_sf_names";
5686     case DW_AT_src_info:
5687       return "DW_AT_src_info";
5688     case DW_AT_mac_info:
5689       return "DW_AT_mac_info";
5690     case DW_AT_src_coords:
5691       return "DW_AT_src_coords";
5692     case DW_AT_body_begin:
5693       return "DW_AT_body_begin";
5694     case DW_AT_body_end:
5695       return "DW_AT_body_end";
5696     case DW_AT_GNU_vector:
5697       return "DW_AT_GNU_vector";
5698
5699     case DW_AT_VMS_rtnbeg_pd_address:
5700       return "DW_AT_VMS_rtnbeg_pd_address";
5701
5702     default:
5703       return "DW_AT_<unknown>";
5704     }
5705 }
5706
5707 /* Convert a DWARF value form code into its string name.  */
5708
5709 static const char *
5710 dwarf_form_name (unsigned int form)
5711 {
5712   switch (form)
5713     {
5714     case DW_FORM_addr:
5715       return "DW_FORM_addr";
5716     case DW_FORM_block2:
5717       return "DW_FORM_block2";
5718     case DW_FORM_block4:
5719       return "DW_FORM_block4";
5720     case DW_FORM_data2:
5721       return "DW_FORM_data2";
5722     case DW_FORM_data4:
5723       return "DW_FORM_data4";
5724     case DW_FORM_data8:
5725       return "DW_FORM_data8";
5726     case DW_FORM_string:
5727       return "DW_FORM_string";
5728     case DW_FORM_block:
5729       return "DW_FORM_block";
5730     case DW_FORM_block1:
5731       return "DW_FORM_block1";
5732     case DW_FORM_data1:
5733       return "DW_FORM_data1";
5734     case DW_FORM_flag:
5735       return "DW_FORM_flag";
5736     case DW_FORM_sdata:
5737       return "DW_FORM_sdata";
5738     case DW_FORM_strp:
5739       return "DW_FORM_strp";
5740     case DW_FORM_udata:
5741       return "DW_FORM_udata";
5742     case DW_FORM_ref_addr:
5743       return "DW_FORM_ref_addr";
5744     case DW_FORM_ref1:
5745       return "DW_FORM_ref1";
5746     case DW_FORM_ref2:
5747       return "DW_FORM_ref2";
5748     case DW_FORM_ref4:
5749       return "DW_FORM_ref4";
5750     case DW_FORM_ref8:
5751       return "DW_FORM_ref8";
5752     case DW_FORM_ref_udata:
5753       return "DW_FORM_ref_udata";
5754     case DW_FORM_indirect:
5755       return "DW_FORM_indirect";
5756     default:
5757       return "DW_FORM_<unknown>";
5758     }
5759 }
5760 \f
5761 /* Determine the "ultimate origin" of a decl.  The decl may be an inlined
5762    instance of an inlined instance of a decl which is local to an inline
5763    function, so we have to trace all of the way back through the origin chain
5764    to find out what sort of node actually served as the original seed for the
5765    given block.  */
5766
5767 static tree
5768 decl_ultimate_origin (const_tree decl)
5769 {
5770   if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
5771     return NULL_TREE;
5772
5773   /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
5774      nodes in the function to point to themselves; ignore that if
5775      we're trying to output the abstract instance of this function.  */
5776   if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
5777     return NULL_TREE;
5778
5779   /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
5780      most distant ancestor, this should never happen.  */
5781   gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
5782
5783   return DECL_ABSTRACT_ORIGIN (decl);
5784 }
5785
5786 /* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
5787    of a virtual function may refer to a base class, so we check the 'this'
5788    parameter.  */
5789
5790 static tree
5791 decl_class_context (tree decl)
5792 {
5793   tree context = NULL_TREE;
5794
5795   if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
5796     context = DECL_CONTEXT (decl);
5797   else
5798     context = TYPE_MAIN_VARIANT
5799       (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
5800
5801   if (context && !TYPE_P (context))
5802     context = NULL_TREE;
5803
5804   return context;
5805 }
5806 \f
5807 /* Add an attribute/value pair to a DIE.  */
5808
5809 static inline void
5810 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
5811 {
5812   /* Maybe this should be an assert?  */
5813   if (die == NULL)
5814     return;
5815
5816   if (die->die_attr == NULL)
5817     die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
5818   VEC_safe_push (dw_attr_node, gc, die->die_attr, attr);
5819 }
5820
5821 static inline enum dw_val_class
5822 AT_class (dw_attr_ref a)
5823 {
5824   return a->dw_attr_val.val_class;
5825 }
5826
5827 /* Add a flag value attribute to a DIE.  */
5828
5829 static inline void
5830 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
5831 {
5832   dw_attr_node attr;
5833
5834   attr.dw_attr = attr_kind;
5835   attr.dw_attr_val.val_class = dw_val_class_flag;
5836   attr.dw_attr_val.v.val_flag = flag;
5837   add_dwarf_attr (die, &attr);
5838 }
5839
5840 static inline unsigned
5841 AT_flag (dw_attr_ref a)
5842 {
5843   gcc_assert (a && AT_class (a) == dw_val_class_flag);
5844   return a->dw_attr_val.v.val_flag;
5845 }
5846
5847 /* Add a signed integer attribute value to a DIE.  */
5848
5849 static inline void
5850 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
5851 {
5852   dw_attr_node attr;
5853
5854   attr.dw_attr = attr_kind;
5855   attr.dw_attr_val.val_class = dw_val_class_const;
5856   attr.dw_attr_val.v.val_int = int_val;
5857   add_dwarf_attr (die, &attr);
5858 }
5859
5860 static inline HOST_WIDE_INT
5861 AT_int (dw_attr_ref a)
5862 {
5863   gcc_assert (a && AT_class (a) == dw_val_class_const);
5864   return a->dw_attr_val.v.val_int;
5865 }
5866
5867 /* Add an unsigned integer attribute value to a DIE.  */
5868
5869 static inline void
5870 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
5871                  unsigned HOST_WIDE_INT unsigned_val)
5872 {
5873   dw_attr_node attr;
5874
5875   attr.dw_attr = attr_kind;
5876   attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
5877   attr.dw_attr_val.v.val_unsigned = unsigned_val;
5878   add_dwarf_attr (die, &attr);
5879 }
5880
5881 static inline unsigned HOST_WIDE_INT
5882 AT_unsigned (dw_attr_ref a)
5883 {
5884   gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
5885   return a->dw_attr_val.v.val_unsigned;
5886 }
5887
5888 /* Add an unsigned double integer attribute value to a DIE.  */
5889
5890 static inline void
5891 add_AT_long_long (dw_die_ref die, enum dwarf_attribute attr_kind,
5892                   long unsigned int val_hi, long unsigned int val_low)
5893 {
5894   dw_attr_node attr;
5895
5896   attr.dw_attr = attr_kind;
5897   attr.dw_attr_val.val_class = dw_val_class_long_long;
5898   attr.dw_attr_val.v.val_long_long.hi = val_hi;
5899   attr.dw_attr_val.v.val_long_long.low = val_low;
5900   add_dwarf_attr (die, &attr);
5901 }
5902
5903 /* Add a floating point attribute value to a DIE and return it.  */
5904
5905 static inline void
5906 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
5907             unsigned int length, unsigned int elt_size, unsigned char *array)
5908 {
5909   dw_attr_node attr;
5910
5911   attr.dw_attr = attr_kind;
5912   attr.dw_attr_val.val_class = dw_val_class_vec;
5913   attr.dw_attr_val.v.val_vec.length = length;
5914   attr.dw_attr_val.v.val_vec.elt_size = elt_size;
5915   attr.dw_attr_val.v.val_vec.array = array;
5916   add_dwarf_attr (die, &attr);
5917 }
5918
5919 /* Hash and equality functions for debug_str_hash.  */
5920
5921 static hashval_t
5922 debug_str_do_hash (const void *x)
5923 {
5924   return htab_hash_string (((const struct indirect_string_node *)x)->str);
5925 }
5926
5927 static int
5928 debug_str_eq (const void *x1, const void *x2)
5929 {
5930   return strcmp ((((const struct indirect_string_node *)x1)->str),
5931                  (const char *)x2) == 0;
5932 }
5933
5934 static struct indirect_string_node *
5935 find_AT_string (const char *str)
5936 {
5937   struct indirect_string_node *node;
5938   void **slot;
5939
5940   if (! debug_str_hash)
5941     debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
5942                                       debug_str_eq, NULL);
5943
5944   slot = htab_find_slot_with_hash (debug_str_hash, str,
5945                                    htab_hash_string (str), INSERT);
5946   if (*slot == NULL)
5947     {
5948       node = (struct indirect_string_node *)
5949                ggc_alloc_cleared (sizeof (struct indirect_string_node));
5950       node->str = ggc_strdup (str);
5951       *slot = node;
5952     }
5953   else
5954     node = (struct indirect_string_node *) *slot;
5955
5956   node->refcount++;
5957   return node;
5958 }
5959
5960 /* Add a string attribute value to a DIE.  */
5961
5962 static inline void
5963 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
5964 {
5965   dw_attr_node attr;
5966   struct indirect_string_node *node;
5967
5968   node = find_AT_string (str);
5969
5970   attr.dw_attr = attr_kind;
5971   attr.dw_attr_val.val_class = dw_val_class_str;
5972   attr.dw_attr_val.v.val_str = node;
5973   add_dwarf_attr (die, &attr);
5974 }
5975
5976 static inline const char *
5977 AT_string (dw_attr_ref a)
5978 {
5979   gcc_assert (a && AT_class (a) == dw_val_class_str);
5980   return a->dw_attr_val.v.val_str->str;
5981 }
5982
5983 /* Find out whether a string should be output inline in DIE
5984    or out-of-line in .debug_str section.  */
5985
5986 static int
5987 AT_string_form (dw_attr_ref a)
5988 {
5989   struct indirect_string_node *node;
5990   unsigned int len;
5991   char label[32];
5992
5993   gcc_assert (a && AT_class (a) == dw_val_class_str);
5994
5995   node = a->dw_attr_val.v.val_str;
5996   if (node->form)
5997     return node->form;
5998
5999   len = strlen (node->str) + 1;
6000
6001   /* If the string is shorter or equal to the size of the reference, it is
6002      always better to put it inline.  */
6003   if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
6004     return node->form = DW_FORM_string;
6005
6006   /* If we cannot expect the linker to merge strings in .debug_str
6007      section, only put it into .debug_str if it is worth even in this
6008      single module.  */
6009   if ((debug_str_section->common.flags & SECTION_MERGE) == 0
6010       && (len - DWARF_OFFSET_SIZE) * node->refcount <= len)
6011     return node->form = DW_FORM_string;
6012
6013   ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
6014   ++dw2_string_counter;
6015   node->label = xstrdup (label);
6016
6017   return node->form = DW_FORM_strp;
6018 }
6019
6020 /* Add a DIE reference attribute value to a DIE.  */
6021
6022 static inline void
6023 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
6024 {
6025   dw_attr_node attr;
6026
6027   attr.dw_attr = attr_kind;
6028   attr.dw_attr_val.val_class = dw_val_class_die_ref;
6029   attr.dw_attr_val.v.val_die_ref.die = targ_die;
6030   attr.dw_attr_val.v.val_die_ref.external = 0;
6031   add_dwarf_attr (die, &attr);
6032 }
6033
6034 /* Add an AT_specification attribute to a DIE, and also make the back
6035    pointer from the specification to the definition.  */
6036
6037 static inline void
6038 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
6039 {
6040   add_AT_die_ref (die, DW_AT_specification, targ_die);
6041   gcc_assert (!targ_die->die_definition);
6042   targ_die->die_definition = die;
6043 }
6044
6045 static inline dw_die_ref
6046 AT_ref (dw_attr_ref a)
6047 {
6048   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
6049   return a->dw_attr_val.v.val_die_ref.die;
6050 }
6051
6052 static inline int
6053 AT_ref_external (dw_attr_ref a)
6054 {
6055   if (a && AT_class (a) == dw_val_class_die_ref)
6056     return a->dw_attr_val.v.val_die_ref.external;
6057
6058   return 0;
6059 }
6060
6061 static inline void
6062 set_AT_ref_external (dw_attr_ref a, int i)
6063 {
6064   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
6065   a->dw_attr_val.v.val_die_ref.external = i;
6066 }
6067
6068 /* Add an FDE reference attribute value to a DIE.  */
6069
6070 static inline void
6071 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
6072 {
6073   dw_attr_node attr;
6074
6075   attr.dw_attr = attr_kind;
6076   attr.dw_attr_val.val_class = dw_val_class_fde_ref;
6077   attr.dw_attr_val.v.val_fde_index = targ_fde;
6078   add_dwarf_attr (die, &attr);
6079 }
6080
6081 /* Add a location description attribute value to a DIE.  */
6082
6083 static inline void
6084 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
6085 {
6086   dw_attr_node attr;
6087
6088   attr.dw_attr = attr_kind;
6089   attr.dw_attr_val.val_class = dw_val_class_loc;
6090   attr.dw_attr_val.v.val_loc = loc;
6091   add_dwarf_attr (die, &attr);
6092 }
6093
6094 static inline dw_loc_descr_ref
6095 AT_loc (dw_attr_ref a)
6096 {
6097   gcc_assert (a && AT_class (a) == dw_val_class_loc);
6098   return a->dw_attr_val.v.val_loc;
6099 }
6100
6101 static inline void
6102 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
6103 {
6104   dw_attr_node attr;
6105
6106   attr.dw_attr = attr_kind;
6107   attr.dw_attr_val.val_class = dw_val_class_loc_list;
6108   attr.dw_attr_val.v.val_loc_list = loc_list;
6109   add_dwarf_attr (die, &attr);
6110   have_location_lists = true;
6111 }
6112
6113 static inline dw_loc_list_ref
6114 AT_loc_list (dw_attr_ref a)
6115 {
6116   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
6117   return a->dw_attr_val.v.val_loc_list;
6118 }
6119
6120 /* Add an address constant attribute value to a DIE.  */
6121
6122 static inline void
6123 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
6124 {
6125   dw_attr_node attr;
6126
6127   attr.dw_attr = attr_kind;
6128   attr.dw_attr_val.val_class = dw_val_class_addr;
6129   attr.dw_attr_val.v.val_addr = addr;
6130   add_dwarf_attr (die, &attr);
6131 }
6132
6133 /* Get the RTX from to an address DIE attribute.  */
6134
6135 static inline rtx
6136 AT_addr (dw_attr_ref a)
6137 {
6138   gcc_assert (a && AT_class (a) == dw_val_class_addr);
6139   return a->dw_attr_val.v.val_addr;
6140 }
6141
6142 /* Add a file attribute value to a DIE.  */
6143
6144 static inline void
6145 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
6146              struct dwarf_file_data *fd)
6147 {
6148   dw_attr_node attr;
6149
6150   attr.dw_attr = attr_kind;
6151   attr.dw_attr_val.val_class = dw_val_class_file;
6152   attr.dw_attr_val.v.val_file = fd;
6153   add_dwarf_attr (die, &attr);
6154 }
6155
6156 /* Get the dwarf_file_data from a file DIE attribute.  */
6157
6158 static inline struct dwarf_file_data *
6159 AT_file (dw_attr_ref a)
6160 {
6161   gcc_assert (a && AT_class (a) == dw_val_class_file);
6162   return a->dw_attr_val.v.val_file;
6163 }
6164
6165 /* Add a label identifier attribute value to a DIE.  */
6166
6167 static inline void
6168 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
6169 {
6170   dw_attr_node attr;
6171
6172   attr.dw_attr = attr_kind;
6173   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
6174   attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
6175   add_dwarf_attr (die, &attr);
6176 }
6177
6178 /* Add a section offset attribute value to a DIE, an offset into the
6179    debug_line section.  */
6180
6181 static inline void
6182 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
6183                 const char *label)
6184 {
6185   dw_attr_node attr;
6186
6187   attr.dw_attr = attr_kind;
6188   attr.dw_attr_val.val_class = dw_val_class_lineptr;
6189   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
6190   add_dwarf_attr (die, &attr);
6191 }
6192
6193 /* Add a section offset attribute value to a DIE, an offset into the
6194    debug_macinfo section.  */
6195
6196 static inline void
6197 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
6198                const char *label)
6199 {
6200   dw_attr_node attr;
6201
6202   attr.dw_attr = attr_kind;
6203   attr.dw_attr_val.val_class = dw_val_class_macptr;
6204   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
6205   add_dwarf_attr (die, &attr);
6206 }
6207
6208 /* Add an offset attribute value to a DIE.  */
6209
6210 static inline void
6211 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
6212                unsigned HOST_WIDE_INT offset)
6213 {
6214   dw_attr_node attr;
6215
6216   attr.dw_attr = attr_kind;
6217   attr.dw_attr_val.val_class = dw_val_class_offset;
6218   attr.dw_attr_val.v.val_offset = offset;
6219   add_dwarf_attr (die, &attr);
6220 }
6221
6222 /* Add an range_list attribute value to a DIE.  */
6223
6224 static void
6225 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
6226                    long unsigned int offset)
6227 {
6228   dw_attr_node attr;
6229
6230   attr.dw_attr = attr_kind;
6231   attr.dw_attr_val.val_class = dw_val_class_range_list;
6232   attr.dw_attr_val.v.val_offset = offset;
6233   add_dwarf_attr (die, &attr);
6234 }
6235
6236 static inline const char *
6237 AT_lbl (dw_attr_ref a)
6238 {
6239   gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
6240                     || AT_class (a) == dw_val_class_lineptr
6241                     || AT_class (a) == dw_val_class_macptr));
6242   return a->dw_attr_val.v.val_lbl_id;
6243 }
6244
6245 /* Get the attribute of type attr_kind.  */
6246
6247 static dw_attr_ref
6248 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
6249 {
6250   dw_attr_ref a;
6251   unsigned ix;
6252   dw_die_ref spec = NULL;
6253
6254   if (! die)
6255     return NULL;
6256
6257   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6258     if (a->dw_attr == attr_kind)
6259       return a;
6260     else if (a->dw_attr == DW_AT_specification
6261              || a->dw_attr == DW_AT_abstract_origin)
6262       spec = AT_ref (a);
6263
6264   if (spec)
6265     return get_AT (spec, attr_kind);
6266
6267   return NULL;
6268 }
6269
6270 /* Return the "low pc" attribute value, typically associated with a subprogram
6271    DIE.  Return null if the "low pc" attribute is either not present, or if it
6272    cannot be represented as an assembler label identifier.  */
6273
6274 static inline const char *
6275 get_AT_low_pc (dw_die_ref die)
6276 {
6277   dw_attr_ref a = get_AT (die, DW_AT_low_pc);
6278
6279   return a ? AT_lbl (a) : NULL;
6280 }
6281
6282 /* Return the "high pc" attribute value, typically associated with a subprogram
6283    DIE.  Return null if the "high pc" attribute is either not present, or if it
6284    cannot be represented as an assembler label identifier.  */
6285
6286 static inline const char *
6287 get_AT_hi_pc (dw_die_ref die)
6288 {
6289   dw_attr_ref a = get_AT (die, DW_AT_high_pc);
6290
6291   return a ? AT_lbl (a) : NULL;
6292 }
6293
6294 /* Return the value of the string attribute designated by ATTR_KIND, or
6295    NULL if it is not present.  */
6296
6297 static inline const char *
6298 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
6299 {
6300   dw_attr_ref a = get_AT (die, attr_kind);
6301
6302   return a ? AT_string (a) : NULL;
6303 }
6304
6305 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
6306    if it is not present.  */
6307
6308 static inline int
6309 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
6310 {
6311   dw_attr_ref a = get_AT (die, attr_kind);
6312
6313   return a ? AT_flag (a) : 0;
6314 }
6315
6316 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
6317    if it is not present.  */
6318
6319 static inline unsigned
6320 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
6321 {
6322   dw_attr_ref a = get_AT (die, attr_kind);
6323
6324   return a ? AT_unsigned (a) : 0;
6325 }
6326
6327 static inline dw_die_ref
6328 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
6329 {
6330   dw_attr_ref a = get_AT (die, attr_kind);
6331
6332   return a ? AT_ref (a) : NULL;
6333 }
6334
6335 static inline struct dwarf_file_data *
6336 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
6337 {
6338   dw_attr_ref a = get_AT (die, attr_kind);
6339
6340   return a ? AT_file (a) : NULL;
6341 }
6342
6343 /* Return TRUE if the language is C or C++.  */
6344
6345 static inline bool
6346 is_c_family (void)
6347 {
6348   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
6349
6350   return (lang == DW_LANG_C || lang == DW_LANG_C89 || lang == DW_LANG_ObjC
6351           || lang == DW_LANG_C99
6352           || lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus);
6353 }
6354
6355 /* Return TRUE if the language is C++.  */
6356
6357 static inline bool
6358 is_cxx (void)
6359 {
6360   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
6361
6362   return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
6363 }
6364
6365 /* Return TRUE if the language is Fortran.  */
6366
6367 static inline bool
6368 is_fortran (void)
6369 {
6370   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
6371
6372   return (lang == DW_LANG_Fortran77
6373           || lang == DW_LANG_Fortran90
6374           || lang == DW_LANG_Fortran95);
6375 }
6376
6377 /* Return TRUE if the language is Java.  */
6378
6379 static inline bool
6380 is_java (void)
6381 {
6382   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
6383
6384   return lang == DW_LANG_Java;
6385 }
6386
6387 /* Return TRUE if the language is Ada.  */
6388
6389 static inline bool
6390 is_ada (void)
6391 {
6392   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
6393
6394   return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
6395 }
6396
6397 /* Remove the specified attribute if present.  */
6398
6399 static void
6400 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
6401 {
6402   dw_attr_ref a;
6403   unsigned ix;
6404
6405   if (! die)
6406     return;
6407
6408   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6409     if (a->dw_attr == attr_kind)
6410       {
6411         if (AT_class (a) == dw_val_class_str)
6412           if (a->dw_attr_val.v.val_str->refcount)
6413             a->dw_attr_val.v.val_str->refcount--;
6414
6415         /* VEC_ordered_remove should help reduce the number of abbrevs
6416            that are needed.  */
6417         VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
6418         return;
6419       }
6420 }
6421
6422 /* Remove CHILD from its parent.  PREV must have the property that
6423    PREV->DIE_SIB == CHILD.  Does not alter CHILD.  */
6424
6425 static void
6426 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
6427 {
6428   gcc_assert (child->die_parent == prev->die_parent);
6429   gcc_assert (prev->die_sib == child);
6430   if (prev == child)
6431     {
6432       gcc_assert (child->die_parent->die_child == child);
6433       prev = NULL;
6434     }
6435   else
6436     prev->die_sib = child->die_sib;
6437   if (child->die_parent->die_child == child)
6438     child->die_parent->die_child = prev;
6439 }
6440
6441 /* Remove child DIE whose die_tag is TAG.  Do nothing if no child
6442    matches TAG.  */
6443
6444 static void
6445 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
6446 {
6447   dw_die_ref c;
6448
6449   c = die->die_child;
6450   if (c) do {
6451     dw_die_ref prev = c;
6452     c = c->die_sib;
6453     while (c->die_tag == tag)
6454       {
6455         remove_child_with_prev (c, prev);
6456         /* Might have removed every child.  */
6457         if (c == c->die_sib)
6458           return;
6459         c = c->die_sib;
6460       }
6461   } while (c != die->die_child);
6462 }
6463
6464 /* Add a CHILD_DIE as the last child of DIE.  */
6465
6466 static void
6467 add_child_die (dw_die_ref die, dw_die_ref child_die)
6468 {
6469   /* FIXME this should probably be an assert.  */
6470   if (! die || ! child_die)
6471     return;
6472   gcc_assert (die != child_die);
6473
6474   child_die->die_parent = die;
6475   if (die->die_child)
6476     {
6477       child_die->die_sib = die->die_child->die_sib;
6478       die->die_child->die_sib = child_die;
6479     }
6480   else
6481     child_die->die_sib = child_die;
6482   die->die_child = child_die;
6483 }
6484
6485 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
6486    is the specification, to the end of PARENT's list of children.
6487    This is done by removing and re-adding it.  */
6488
6489 static void
6490 splice_child_die (dw_die_ref parent, dw_die_ref child)
6491 {
6492   dw_die_ref p;
6493
6494   /* We want the declaration DIE from inside the class, not the
6495      specification DIE at toplevel.  */
6496   if (child->die_parent != parent)
6497     {
6498       dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
6499
6500       if (tmp)
6501         child = tmp;
6502     }
6503
6504   gcc_assert (child->die_parent == parent
6505               || (child->die_parent
6506                   == get_AT_ref (parent, DW_AT_specification)));
6507
6508   for (p = child->die_parent->die_child; ; p = p->die_sib)
6509     if (p->die_sib == child)
6510       {
6511         remove_child_with_prev (child, p);
6512         break;
6513       }
6514
6515   add_child_die (parent, child);
6516 }
6517
6518 /* Return a pointer to a newly created DIE node.  */
6519
6520 static inline dw_die_ref
6521 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
6522 {
6523   dw_die_ref die = GGC_CNEW (die_node);
6524
6525   die->die_tag = tag_value;
6526
6527   if (parent_die != NULL)
6528     add_child_die (parent_die, die);
6529   else
6530     {
6531       limbo_die_node *limbo_node;
6532
6533       limbo_node = GGC_CNEW (limbo_die_node);
6534       limbo_node->die = die;
6535       limbo_node->created_for = t;
6536       limbo_node->next = limbo_die_list;
6537       limbo_die_list = limbo_node;
6538     }
6539
6540   return die;
6541 }
6542
6543 /* Return the DIE associated with the given type specifier.  */
6544
6545 static inline dw_die_ref
6546 lookup_type_die (tree type)
6547 {
6548   return TYPE_SYMTAB_DIE (type);
6549 }
6550
6551 /* Equate a DIE to a given type specifier.  */
6552
6553 static inline void
6554 equate_type_number_to_die (tree type, dw_die_ref type_die)
6555 {
6556   TYPE_SYMTAB_DIE (type) = type_die;
6557 }
6558
6559 /* Returns a hash value for X (which really is a die_struct).  */
6560
6561 static hashval_t
6562 decl_die_table_hash (const void *x)
6563 {
6564   return (hashval_t) ((const_dw_die_ref) x)->decl_id;
6565 }
6566
6567 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
6568
6569 static int
6570 decl_die_table_eq (const void *x, const void *y)
6571 {
6572   return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
6573 }
6574
6575 /* Return the DIE associated with a given declaration.  */
6576
6577 static inline dw_die_ref
6578 lookup_decl_die (tree decl)
6579 {
6580   return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
6581 }
6582
6583 /* Returns a hash value for X (which really is a var_loc_list).  */
6584
6585 static hashval_t
6586 decl_loc_table_hash (const void *x)
6587 {
6588   return (hashval_t) ((const var_loc_list *) x)->decl_id;
6589 }
6590
6591 /* Return nonzero if decl_id of var_loc_list X is the same as
6592    UID of decl *Y.  */
6593
6594 static int
6595 decl_loc_table_eq (const void *x, const void *y)
6596 {
6597   return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
6598 }
6599
6600 /* Return the var_loc list associated with a given declaration.  */
6601
6602 static inline var_loc_list *
6603 lookup_decl_loc (const_tree decl)
6604 {
6605   return (var_loc_list *)
6606     htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
6607 }
6608
6609 /* Equate a DIE to a particular declaration.  */
6610
6611 static void
6612 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
6613 {
6614   unsigned int decl_id = DECL_UID (decl);
6615   void **slot;
6616
6617   slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
6618   *slot = decl_die;
6619   decl_die->decl_id = decl_id;
6620 }
6621
6622 /* Add a variable location node to the linked list for DECL.  */
6623
6624 static void
6625 add_var_loc_to_decl (tree decl, struct var_loc_node *loc)
6626 {
6627   unsigned int decl_id = DECL_UID (decl);
6628   var_loc_list *temp;
6629   void **slot;
6630
6631   slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
6632   if (*slot == NULL)
6633     {
6634       temp = GGC_CNEW (var_loc_list);
6635       temp->decl_id = decl_id;
6636       *slot = temp;
6637     }
6638   else
6639     temp = (var_loc_list *) *slot;
6640
6641   if (temp->last)
6642     {
6643       /* If the current location is the same as the end of the list,
6644          and either both or neither of the locations is uninitialized,
6645          we have nothing to do.  */
6646       if ((!rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->last->var_loc_note),
6647                          NOTE_VAR_LOCATION_LOC (loc->var_loc_note)))
6648           || ((NOTE_VAR_LOCATION_STATUS (temp->last->var_loc_note)
6649                != NOTE_VAR_LOCATION_STATUS (loc->var_loc_note))
6650               && ((NOTE_VAR_LOCATION_STATUS (temp->last->var_loc_note)
6651                    == VAR_INIT_STATUS_UNINITIALIZED)
6652                   || (NOTE_VAR_LOCATION_STATUS (loc->var_loc_note)
6653                       == VAR_INIT_STATUS_UNINITIALIZED))))
6654         {
6655           /* Add LOC to the end of list and update LAST.  */
6656           temp->last->next = loc;
6657           temp->last = loc;
6658         }
6659     }
6660   /* Do not add empty location to the beginning of the list.  */
6661   else if (NOTE_VAR_LOCATION_LOC (loc->var_loc_note) != NULL_RTX)
6662     {
6663       temp->first = loc;
6664       temp->last = loc;
6665     }
6666 }
6667 \f
6668 /* Keep track of the number of spaces used to indent the
6669    output of the debugging routines that print the structure of
6670    the DIE internal representation.  */
6671 static int print_indent;
6672
6673 /* Indent the line the number of spaces given by print_indent.  */
6674
6675 static inline void
6676 print_spaces (FILE *outfile)
6677 {
6678   fprintf (outfile, "%*s", print_indent, "");
6679 }
6680
6681 /* Print the information associated with a given DIE, and its children.
6682    This routine is a debugging aid only.  */
6683
6684 static void
6685 print_die (dw_die_ref die, FILE *outfile)
6686 {
6687   dw_attr_ref a;
6688   dw_die_ref c;
6689   unsigned ix;
6690
6691   print_spaces (outfile);
6692   fprintf (outfile, "DIE %4ld: %s\n",
6693            die->die_offset, dwarf_tag_name (die->die_tag));
6694   print_spaces (outfile);
6695   fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
6696   fprintf (outfile, " offset: %ld\n", die->die_offset);
6697
6698   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6699     {
6700       print_spaces (outfile);
6701       fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
6702
6703       switch (AT_class (a))
6704         {
6705         case dw_val_class_addr:
6706           fprintf (outfile, "address");
6707           break;
6708         case dw_val_class_offset:
6709           fprintf (outfile, "offset");
6710           break;
6711         case dw_val_class_loc:
6712           fprintf (outfile, "location descriptor");
6713           break;
6714         case dw_val_class_loc_list:
6715           fprintf (outfile, "location list -> label:%s",
6716                    AT_loc_list (a)->ll_symbol);
6717           break;
6718         case dw_val_class_range_list:
6719           fprintf (outfile, "range list");
6720           break;
6721         case dw_val_class_const:
6722           fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
6723           break;
6724         case dw_val_class_unsigned_const:
6725           fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
6726           break;
6727         case dw_val_class_long_long:
6728           fprintf (outfile, "constant (%lu,%lu)",
6729                    a->dw_attr_val.v.val_long_long.hi,
6730                    a->dw_attr_val.v.val_long_long.low);
6731           break;
6732         case dw_val_class_vec:
6733           fprintf (outfile, "floating-point or vector constant");
6734           break;
6735         case dw_val_class_flag:
6736           fprintf (outfile, "%u", AT_flag (a));
6737           break;
6738         case dw_val_class_die_ref:
6739           if (AT_ref (a) != NULL)
6740             {
6741               if (AT_ref (a)->die_symbol)
6742                 fprintf (outfile, "die -> label: %s", AT_ref (a)->die_symbol);
6743               else
6744                 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
6745             }
6746           else
6747             fprintf (outfile, "die -> <null>");
6748           break;
6749         case dw_val_class_lbl_id:
6750         case dw_val_class_lineptr:
6751         case dw_val_class_macptr:
6752           fprintf (outfile, "label: %s", AT_lbl (a));
6753           break;
6754         case dw_val_class_str:
6755           if (AT_string (a) != NULL)
6756             fprintf (outfile, "\"%s\"", AT_string (a));
6757           else
6758             fprintf (outfile, "<null>");
6759           break;
6760         case dw_val_class_file:
6761           fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
6762                    AT_file (a)->emitted_number);
6763           break;
6764         default:
6765           break;
6766         }
6767
6768       fprintf (outfile, "\n");
6769     }
6770
6771   if (die->die_child != NULL)
6772     {
6773       print_indent += 4;
6774       FOR_EACH_CHILD (die, c, print_die (c, outfile));
6775       print_indent -= 4;
6776     }
6777   if (print_indent == 0)
6778     fprintf (outfile, "\n");
6779 }
6780
6781 /* Print the contents of the source code line number correspondence table.
6782    This routine is a debugging aid only.  */
6783
6784 static void
6785 print_dwarf_line_table (FILE *outfile)
6786 {
6787   unsigned i;
6788   dw_line_info_ref line_info;
6789
6790   fprintf (outfile, "\n\nDWARF source line information\n");
6791   for (i = 1; i < line_info_table_in_use; i++)
6792     {
6793       line_info = &line_info_table[i];
6794       fprintf (outfile, "%5d: %4ld %6ld\n", i,
6795                line_info->dw_file_num,
6796                line_info->dw_line_num);
6797     }
6798
6799   fprintf (outfile, "\n\n");
6800 }
6801
6802 /* Print the information collected for a given DIE.  */
6803
6804 void
6805 debug_dwarf_die (dw_die_ref die)
6806 {
6807   print_die (die, stderr);
6808 }
6809
6810 /* Print all DWARF information collected for the compilation unit.
6811    This routine is a debugging aid only.  */
6812
6813 void
6814 debug_dwarf (void)
6815 {
6816   print_indent = 0;
6817   print_die (comp_unit_die, stderr);
6818   if (! DWARF2_ASM_LINE_DEBUG_INFO)
6819     print_dwarf_line_table (stderr);
6820 }
6821 \f
6822 /* Start a new compilation unit DIE for an include file.  OLD_UNIT is the CU
6823    for the enclosing include file, if any.  BINCL_DIE is the DW_TAG_GNU_BINCL
6824    DIE that marks the start of the DIEs for this include file.  */
6825
6826 static dw_die_ref
6827 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
6828 {
6829   const char *filename = get_AT_string (bincl_die, DW_AT_name);
6830   dw_die_ref new_unit = gen_compile_unit_die (filename);
6831
6832   new_unit->die_sib = old_unit;
6833   return new_unit;
6834 }
6835
6836 /* Close an include-file CU and reopen the enclosing one.  */
6837
6838 static dw_die_ref
6839 pop_compile_unit (dw_die_ref old_unit)
6840 {
6841   dw_die_ref new_unit = old_unit->die_sib;
6842
6843   old_unit->die_sib = NULL;
6844   return new_unit;
6845 }
6846
6847 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6848 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
6849
6850 /* Calculate the checksum of a location expression.  */
6851
6852 static inline void
6853 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6854 {
6855   CHECKSUM (loc->dw_loc_opc);
6856   CHECKSUM (loc->dw_loc_oprnd1);
6857   CHECKSUM (loc->dw_loc_oprnd2);
6858 }
6859
6860 /* Calculate the checksum of an attribute.  */
6861
6862 static void
6863 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
6864 {
6865   dw_loc_descr_ref loc;
6866   rtx r;
6867
6868   CHECKSUM (at->dw_attr);
6869
6870   /* We don't care that this was compiled with a different compiler
6871      snapshot; if the output is the same, that's what matters.  */
6872   if (at->dw_attr == DW_AT_producer)
6873     return;
6874
6875   switch (AT_class (at))
6876     {
6877     case dw_val_class_const:
6878       CHECKSUM (at->dw_attr_val.v.val_int);
6879       break;
6880     case dw_val_class_unsigned_const:
6881       CHECKSUM (at->dw_attr_val.v.val_unsigned);
6882       break;
6883     case dw_val_class_long_long:
6884       CHECKSUM (at->dw_attr_val.v.val_long_long);
6885       break;
6886     case dw_val_class_vec:
6887       CHECKSUM (at->dw_attr_val.v.val_vec);
6888       break;
6889     case dw_val_class_flag:
6890       CHECKSUM (at->dw_attr_val.v.val_flag);
6891       break;
6892     case dw_val_class_str:
6893       CHECKSUM_STRING (AT_string (at));
6894       break;
6895
6896     case dw_val_class_addr:
6897       r = AT_addr (at);
6898       gcc_assert (GET_CODE (r) == SYMBOL_REF);
6899       CHECKSUM_STRING (XSTR (r, 0));
6900       break;
6901
6902     case dw_val_class_offset:
6903       CHECKSUM (at->dw_attr_val.v.val_offset);
6904       break;
6905
6906     case dw_val_class_loc:
6907       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6908         loc_checksum (loc, ctx);
6909       break;
6910
6911     case dw_val_class_die_ref:
6912       die_checksum (AT_ref (at), ctx, mark);
6913       break;
6914
6915     case dw_val_class_fde_ref:
6916     case dw_val_class_lbl_id:
6917     case dw_val_class_lineptr:
6918     case dw_val_class_macptr:
6919       break;
6920
6921     case dw_val_class_file:
6922       CHECKSUM_STRING (AT_file (at)->filename);
6923       break;
6924
6925     default:
6926       break;
6927     }
6928 }
6929
6930 /* Calculate the checksum of a DIE.  */
6931
6932 static void
6933 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6934 {
6935   dw_die_ref c;
6936   dw_attr_ref a;
6937   unsigned ix;
6938
6939   /* To avoid infinite recursion.  */
6940   if (die->die_mark)
6941     {
6942       CHECKSUM (die->die_mark);
6943       return;
6944     }
6945   die->die_mark = ++(*mark);
6946
6947   CHECKSUM (die->die_tag);
6948
6949   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6950     attr_checksum (a, ctx, mark);
6951
6952   FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
6953 }
6954
6955 #undef CHECKSUM
6956 #undef CHECKSUM_STRING
6957
6958 /* Do the location expressions look same?  */
6959 static inline int
6960 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6961 {
6962   return loc1->dw_loc_opc == loc2->dw_loc_opc
6963          && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6964          && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6965 }
6966
6967 /* Do the values look the same?  */
6968 static int
6969 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6970 {
6971   dw_loc_descr_ref loc1, loc2;
6972   rtx r1, r2;
6973
6974   if (v1->val_class != v2->val_class)
6975     return 0;
6976
6977   switch (v1->val_class)
6978     {
6979     case dw_val_class_const:
6980       return v1->v.val_int == v2->v.val_int;
6981     case dw_val_class_unsigned_const:
6982       return v1->v.val_unsigned == v2->v.val_unsigned;
6983     case dw_val_class_long_long:
6984       return v1->v.val_long_long.hi == v2->v.val_long_long.hi
6985              && v1->v.val_long_long.low == v2->v.val_long_long.low;
6986     case dw_val_class_vec:
6987       if (v1->v.val_vec.length != v2->v.val_vec.length
6988           || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6989         return 0;
6990       if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6991                   v1->v.val_vec.length * v1->v.val_vec.elt_size))
6992         return 0;
6993       return 1;
6994     case dw_val_class_flag:
6995       return v1->v.val_flag == v2->v.val_flag;
6996     case dw_val_class_str:
6997       return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
6998
6999     case dw_val_class_addr:
7000       r1 = v1->v.val_addr;
7001       r2 = v2->v.val_addr;
7002       if (GET_CODE (r1) != GET_CODE (r2))
7003         return 0;
7004       gcc_assert (GET_CODE (r1) == SYMBOL_REF);
7005       return !strcmp (XSTR (r1, 0), XSTR (r2, 0));
7006
7007     case dw_val_class_offset:
7008       return v1->v.val_offset == v2->v.val_offset;
7009
7010     case dw_val_class_loc:
7011       for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
7012            loc1 && loc2;
7013            loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
7014         if (!same_loc_p (loc1, loc2, mark))
7015           return 0;
7016       return !loc1 && !loc2;
7017
7018     case dw_val_class_die_ref:
7019       return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
7020
7021     case dw_val_class_fde_ref:
7022     case dw_val_class_lbl_id:
7023     case dw_val_class_lineptr:
7024     case dw_val_class_macptr:
7025       return 1;
7026
7027     case dw_val_class_file:
7028       return v1->v.val_file == v2->v.val_file;
7029
7030     default:
7031       return 1;
7032     }
7033 }
7034
7035 /* Do the attributes look the same?  */
7036
7037 static int
7038 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
7039 {
7040   if (at1->dw_attr != at2->dw_attr)
7041     return 0;
7042
7043   /* We don't care that this was compiled with a different compiler
7044      snapshot; if the output is the same, that's what matters. */
7045   if (at1->dw_attr == DW_AT_producer)
7046     return 1;
7047
7048   return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
7049 }
7050
7051 /* Do the dies look the same?  */
7052
7053 static int
7054 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
7055 {
7056   dw_die_ref c1, c2;
7057   dw_attr_ref a1;
7058   unsigned ix;
7059
7060   /* To avoid infinite recursion.  */
7061   if (die1->die_mark)
7062     return die1->die_mark == die2->die_mark;
7063   die1->die_mark = die2->die_mark = ++(*mark);
7064
7065   if (die1->die_tag != die2->die_tag)
7066     return 0;
7067
7068   if (VEC_length (dw_attr_node, die1->die_attr)
7069       != VEC_length (dw_attr_node, die2->die_attr))
7070     return 0;
7071
7072   for (ix = 0; VEC_iterate (dw_attr_node, die1->die_attr, ix, a1); ix++)
7073     if (!same_attr_p (a1, VEC_index (dw_attr_node, die2->die_attr, ix), mark))
7074       return 0;
7075
7076   c1 = die1->die_child;
7077   c2 = die2->die_child;
7078   if (! c1)
7079     {
7080       if (c2)
7081         return 0;
7082     }
7083   else
7084     for (;;)
7085       {
7086         if (!same_die_p (c1, c2, mark))
7087           return 0;
7088         c1 = c1->die_sib;
7089         c2 = c2->die_sib;
7090         if (c1 == die1->die_child)
7091           {
7092             if (c2 == die2->die_child)
7093               break;
7094             else
7095               return 0;
7096           }
7097     }
7098
7099   return 1;
7100 }
7101
7102 /* Do the dies look the same?  Wrapper around same_die_p.  */
7103
7104 static int
7105 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
7106 {
7107   int mark = 0;
7108   int ret = same_die_p (die1, die2, &mark);
7109
7110   unmark_all_dies (die1);
7111   unmark_all_dies (die2);
7112
7113   return ret;
7114 }
7115
7116 /* The prefix to attach to symbols on DIEs in the current comdat debug
7117    info section.  */
7118 static char *comdat_symbol_id;
7119
7120 /* The index of the current symbol within the current comdat CU.  */
7121 static unsigned int comdat_symbol_number;
7122
7123 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
7124    children, and set comdat_symbol_id accordingly.  */
7125
7126 static void
7127 compute_section_prefix (dw_die_ref unit_die)
7128 {
7129   const char *die_name = get_AT_string (unit_die, DW_AT_name);
7130   const char *base = die_name ? lbasename (die_name) : "anonymous";
7131   char *name = XALLOCAVEC (char, strlen (base) + 64);
7132   char *p;
7133   int i, mark;
7134   unsigned char checksum[16];
7135   struct md5_ctx ctx;
7136
7137   /* Compute the checksum of the DIE, then append part of it as hex digits to
7138      the name filename of the unit.  */
7139
7140   md5_init_ctx (&ctx);
7141   mark = 0;
7142   die_checksum (unit_die, &ctx, &mark);
7143   unmark_all_dies (unit_die);
7144   md5_finish_ctx (&ctx, checksum);
7145
7146   sprintf (name, "%s.", base);
7147   clean_symbol_name (name);
7148
7149   p = name + strlen (name);
7150   for (i = 0; i < 4; i++)
7151     {
7152       sprintf (p, "%.2x", checksum[i]);
7153       p += 2;
7154     }
7155
7156   comdat_symbol_id = unit_die->die_symbol = xstrdup (name);
7157   comdat_symbol_number = 0;
7158 }
7159
7160 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P.  */
7161
7162 static int
7163 is_type_die (dw_die_ref die)
7164 {
7165   switch (die->die_tag)
7166     {
7167     case DW_TAG_array_type:
7168     case DW_TAG_class_type:
7169     case DW_TAG_interface_type:
7170     case DW_TAG_enumeration_type:
7171     case DW_TAG_pointer_type:
7172     case DW_TAG_reference_type:
7173     case DW_TAG_string_type:
7174     case DW_TAG_structure_type:
7175     case DW_TAG_subroutine_type:
7176     case DW_TAG_union_type:
7177     case DW_TAG_ptr_to_member_type:
7178     case DW_TAG_set_type:
7179     case DW_TAG_subrange_type:
7180     case DW_TAG_base_type:
7181     case DW_TAG_const_type:
7182     case DW_TAG_file_type:
7183     case DW_TAG_packed_type:
7184     case DW_TAG_volatile_type:
7185     case DW_TAG_typedef:
7186       return 1;
7187     default:
7188       return 0;
7189     }
7190 }
7191
7192 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
7193    Basically, we want to choose the bits that are likely to be shared between
7194    compilations (types) and leave out the bits that are specific to individual
7195    compilations (functions).  */
7196
7197 static int
7198 is_comdat_die (dw_die_ref c)
7199 {
7200   /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
7201      we do for stabs.  The advantage is a greater likelihood of sharing between
7202      objects that don't include headers in the same order (and therefore would
7203      put the base types in a different comdat).  jason 8/28/00 */
7204
7205   if (c->die_tag == DW_TAG_base_type)
7206     return 0;
7207
7208   if (c->die_tag == DW_TAG_pointer_type
7209       || c->die_tag == DW_TAG_reference_type
7210       || c->die_tag == DW_TAG_const_type
7211       || c->die_tag == DW_TAG_volatile_type)
7212     {
7213       dw_die_ref t = get_AT_ref (c, DW_AT_type);
7214
7215       return t ? is_comdat_die (t) : 0;
7216     }
7217
7218   return is_type_die (c);
7219 }
7220
7221 /* Returns 1 iff C is the sort of DIE that might be referred to from another
7222    compilation unit.  */
7223
7224 static int
7225 is_symbol_die (dw_die_ref c)
7226 {
7227   return (is_type_die (c)
7228           || (get_AT (c, DW_AT_declaration)
7229               && !get_AT (c, DW_AT_specification))
7230           || c->die_tag == DW_TAG_namespace
7231           || c->die_tag == DW_TAG_module);
7232 }
7233
7234 static char *
7235 gen_internal_sym (const char *prefix)
7236 {
7237   char buf[256];
7238
7239   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
7240   return xstrdup (buf);
7241 }
7242
7243 /* Assign symbols to all worthy DIEs under DIE.  */
7244
7245 static void
7246 assign_symbol_names (dw_die_ref die)
7247 {
7248   dw_die_ref c;
7249
7250   if (is_symbol_die (die))
7251     {
7252       if (comdat_symbol_id)
7253         {
7254           char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
7255
7256           sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
7257                    comdat_symbol_id, comdat_symbol_number++);
7258           die->die_symbol = xstrdup (p);
7259         }
7260       else
7261         die->die_symbol = gen_internal_sym ("LDIE");
7262     }
7263
7264   FOR_EACH_CHILD (die, c, assign_symbol_names (c));
7265 }
7266
7267 struct cu_hash_table_entry
7268 {
7269   dw_die_ref cu;
7270   unsigned min_comdat_num, max_comdat_num;
7271   struct cu_hash_table_entry *next;
7272 };
7273
7274 /* Routines to manipulate hash table of CUs.  */
7275 static hashval_t
7276 htab_cu_hash (const void *of)
7277 {
7278   const struct cu_hash_table_entry *const entry =
7279     (const struct cu_hash_table_entry *) of;
7280
7281   return htab_hash_string (entry->cu->die_symbol);
7282 }
7283
7284 static int
7285 htab_cu_eq (const void *of1, const void *of2)
7286 {
7287   const struct cu_hash_table_entry *const entry1 =
7288     (const struct cu_hash_table_entry *) of1;
7289   const struct die_struct *const entry2 = (const struct die_struct *) of2;
7290
7291   return !strcmp (entry1->cu->die_symbol, entry2->die_symbol);
7292 }
7293
7294 static void
7295 htab_cu_del (void *what)
7296 {
7297   struct cu_hash_table_entry *next,
7298     *entry = (struct cu_hash_table_entry *) what;
7299
7300   while (entry)
7301     {
7302       next = entry->next;
7303       free (entry);
7304       entry = next;
7305     }
7306 }
7307
7308 /* Check whether we have already seen this CU and set up SYM_NUM
7309    accordingly.  */
7310 static int
7311 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
7312 {
7313   struct cu_hash_table_entry dummy;
7314   struct cu_hash_table_entry **slot, *entry, *last = &dummy;
7315
7316   dummy.max_comdat_num = 0;
7317
7318   slot = (struct cu_hash_table_entry **)
7319     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
7320         INSERT);
7321   entry = *slot;
7322
7323   for (; entry; last = entry, entry = entry->next)
7324     {
7325       if (same_die_p_wrap (cu, entry->cu))
7326         break;
7327     }
7328
7329   if (entry)
7330     {
7331       *sym_num = entry->min_comdat_num;
7332       return 1;
7333     }
7334
7335   entry = XCNEW (struct cu_hash_table_entry);
7336   entry->cu = cu;
7337   entry->min_comdat_num = *sym_num = last->max_comdat_num;
7338   entry->next = *slot;
7339   *slot = entry;
7340
7341   return 0;
7342 }
7343
7344 /* Record SYM_NUM to record of CU in HTABLE.  */
7345 static void
7346 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
7347 {
7348   struct cu_hash_table_entry **slot, *entry;
7349
7350   slot = (struct cu_hash_table_entry **)
7351     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
7352         NO_INSERT);
7353   entry = *slot;
7354
7355   entry->max_comdat_num = sym_num;
7356 }
7357
7358 /* Traverse the DIE (which is always comp_unit_die), and set up
7359    additional compilation units for each of the include files we see
7360    bracketed by BINCL/EINCL.  */
7361
7362 static void
7363 break_out_includes (dw_die_ref die)
7364 {
7365   dw_die_ref c;
7366   dw_die_ref unit = NULL;
7367   limbo_die_node *node, **pnode;
7368   htab_t cu_hash_table;
7369
7370   c = die->die_child;
7371   if (c) do {
7372     dw_die_ref prev = c;
7373     c = c->die_sib;
7374     while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
7375            || (unit && is_comdat_die (c)))
7376       {
7377         dw_die_ref next = c->die_sib;
7378
7379         /* This DIE is for a secondary CU; remove it from the main one.  */
7380         remove_child_with_prev (c, prev);
7381
7382         if (c->die_tag == DW_TAG_GNU_BINCL)
7383           unit = push_new_compile_unit (unit, c);
7384         else if (c->die_tag == DW_TAG_GNU_EINCL)
7385           unit = pop_compile_unit (unit);
7386         else
7387           add_child_die (unit, c);
7388         c = next;
7389         if (c == die->die_child)
7390           break;
7391       }
7392   } while (c != die->die_child);
7393
7394 #if 0
7395   /* We can only use this in debugging, since the frontend doesn't check
7396      to make sure that we leave every include file we enter.  */
7397   gcc_assert (!unit);
7398 #endif
7399
7400   assign_symbol_names (die);
7401   cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
7402   for (node = limbo_die_list, pnode = &limbo_die_list;
7403        node;
7404        node = node->next)
7405     {
7406       int is_dupl;
7407
7408       compute_section_prefix (node->die);
7409       is_dupl = check_duplicate_cu (node->die, cu_hash_table,
7410                         &comdat_symbol_number);
7411       assign_symbol_names (node->die);
7412       if (is_dupl)
7413         *pnode = node->next;
7414       else
7415         {
7416           pnode = &node->next;
7417           record_comdat_symbol_number (node->die, cu_hash_table,
7418                 comdat_symbol_number);
7419         }
7420     }
7421   htab_delete (cu_hash_table);
7422 }
7423
7424 /* Traverse the DIE and add a sibling attribute if it may have the
7425    effect of speeding up access to siblings.  To save some space,
7426    avoid generating sibling attributes for DIE's without children.  */
7427
7428 static void
7429 add_sibling_attributes (dw_die_ref die)
7430 {
7431   dw_die_ref c;
7432
7433   if (! die->die_child)
7434     return;
7435
7436   if (die->die_parent && die != die->die_parent->die_child)
7437     add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
7438
7439   FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
7440 }
7441
7442 /* Output all location lists for the DIE and its children.  */
7443
7444 static void
7445 output_location_lists (dw_die_ref die)
7446 {
7447   dw_die_ref c;
7448   dw_attr_ref a;
7449   unsigned ix;
7450
7451   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7452     if (AT_class (a) == dw_val_class_loc_list)
7453       output_loc_list (AT_loc_list (a));
7454
7455   FOR_EACH_CHILD (die, c, output_location_lists (c));
7456 }
7457
7458 /* The format of each DIE (and its attribute value pairs) is encoded in an
7459    abbreviation table.  This routine builds the abbreviation table and assigns
7460    a unique abbreviation id for each abbreviation entry.  The children of each
7461    die are visited recursively.  */
7462
7463 static void
7464 build_abbrev_table (dw_die_ref die)
7465 {
7466   unsigned long abbrev_id;
7467   unsigned int n_alloc;
7468   dw_die_ref c;
7469   dw_attr_ref a;
7470   unsigned ix;
7471
7472   /* Scan the DIE references, and mark as external any that refer to
7473      DIEs from other CUs (i.e. those which are not marked).  */
7474   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7475     if (AT_class (a) == dw_val_class_die_ref
7476         && AT_ref (a)->die_mark == 0)
7477       {
7478         gcc_assert (AT_ref (a)->die_symbol);
7479
7480         set_AT_ref_external (a, 1);
7481       }
7482
7483   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7484     {
7485       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7486       dw_attr_ref die_a, abbrev_a;
7487       unsigned ix;
7488       bool ok = true;
7489
7490       if (abbrev->die_tag != die->die_tag)
7491         continue;
7492       if ((abbrev->die_child != NULL) != (die->die_child != NULL))
7493         continue;
7494
7495       if (VEC_length (dw_attr_node, abbrev->die_attr)
7496           != VEC_length (dw_attr_node, die->die_attr))
7497         continue;
7498
7499       for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, die_a); ix++)
7500         {
7501           abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
7502           if ((abbrev_a->dw_attr != die_a->dw_attr)
7503               || (value_format (abbrev_a) != value_format (die_a)))
7504             {
7505               ok = false;
7506               break;
7507             }
7508         }
7509       if (ok)
7510         break;
7511     }
7512
7513   if (abbrev_id >= abbrev_die_table_in_use)
7514     {
7515       if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
7516         {
7517           n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
7518           abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
7519                                             n_alloc);
7520
7521           memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
7522                  (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
7523           abbrev_die_table_allocated = n_alloc;
7524         }
7525
7526       ++abbrev_die_table_in_use;
7527       abbrev_die_table[abbrev_id] = die;
7528     }
7529
7530   die->die_abbrev = abbrev_id;
7531   FOR_EACH_CHILD (die, c, build_abbrev_table (c));
7532 }
7533 \f
7534 /* Return the power-of-two number of bytes necessary to represent VALUE.  */
7535
7536 static int
7537 constant_size (unsigned HOST_WIDE_INT value)
7538 {
7539   int log;
7540
7541   if (value == 0)
7542     log = 0;
7543   else
7544     log = floor_log2 (value);
7545
7546   log = log / 8;
7547   log = 1 << (floor_log2 (log) + 1);
7548
7549   return log;
7550 }
7551
7552 /* Return the size of a DIE as it is represented in the
7553    .debug_info section.  */
7554
7555 static unsigned long
7556 size_of_die (dw_die_ref die)
7557 {
7558   unsigned long size = 0;
7559   dw_attr_ref a;
7560   unsigned ix;
7561
7562   size += size_of_uleb128 (die->die_abbrev);
7563   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7564     {
7565       switch (AT_class (a))
7566         {
7567         case dw_val_class_addr:
7568           size += DWARF2_ADDR_SIZE;
7569           break;
7570         case dw_val_class_offset:
7571           size += DWARF_OFFSET_SIZE;
7572           break;
7573         case dw_val_class_loc:
7574           {
7575             unsigned long lsize = size_of_locs (AT_loc (a));
7576
7577             /* Block length.  */
7578             size += constant_size (lsize);
7579             size += lsize;
7580           }
7581           break;
7582         case dw_val_class_loc_list:
7583           size += DWARF_OFFSET_SIZE;
7584           break;
7585         case dw_val_class_range_list:
7586           size += DWARF_OFFSET_SIZE;
7587           break;
7588         case dw_val_class_const:
7589           size += size_of_sleb128 (AT_int (a));
7590           break;
7591         case dw_val_class_unsigned_const:
7592           size += constant_size (AT_unsigned (a));
7593           break;
7594         case dw_val_class_long_long:
7595           size += 1 + 2*HOST_BITS_PER_LONG/HOST_BITS_PER_CHAR; /* block */
7596           break;
7597         case dw_val_class_vec:
7598           size += constant_size (a->dw_attr_val.v.val_vec.length
7599                                  * a->dw_attr_val.v.val_vec.elt_size)
7600                   + a->dw_attr_val.v.val_vec.length
7601                     * a->dw_attr_val.v.val_vec.elt_size; /* block */
7602           break;
7603         case dw_val_class_flag:
7604           size += 1;
7605           break;
7606         case dw_val_class_die_ref:
7607           if (AT_ref_external (a))
7608             size += DWARF2_ADDR_SIZE;
7609           else
7610             size += DWARF_OFFSET_SIZE;
7611           break;
7612         case dw_val_class_fde_ref:
7613           size += DWARF_OFFSET_SIZE;
7614           break;
7615         case dw_val_class_lbl_id:
7616           size += DWARF2_ADDR_SIZE;
7617           break;
7618         case dw_val_class_lineptr:
7619         case dw_val_class_macptr:
7620           size += DWARF_OFFSET_SIZE;
7621           break;
7622         case dw_val_class_str:
7623           if (AT_string_form (a) == DW_FORM_strp)
7624             size += DWARF_OFFSET_SIZE;
7625           else
7626             size += strlen (a->dw_attr_val.v.val_str->str) + 1;
7627           break;
7628         case dw_val_class_file:
7629           size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
7630           break;
7631         default:
7632           gcc_unreachable ();
7633         }
7634     }
7635
7636   return size;
7637 }
7638
7639 /* Size the debugging information associated with a given DIE.  Visits the
7640    DIE's children recursively.  Updates the global variable next_die_offset, on
7641    each time through.  Uses the current value of next_die_offset to update the
7642    die_offset field in each DIE.  */
7643
7644 static void
7645 calc_die_sizes (dw_die_ref die)
7646 {
7647   dw_die_ref c;
7648
7649   die->die_offset = next_die_offset;
7650   next_die_offset += size_of_die (die);
7651
7652   FOR_EACH_CHILD (die, c, calc_die_sizes (c));
7653
7654   if (die->die_child != NULL)
7655     /* Count the null byte used to terminate sibling lists.  */
7656     next_die_offset += 1;
7657 }
7658
7659 /* Set the marks for a die and its children.  We do this so
7660    that we know whether or not a reference needs to use FORM_ref_addr; only
7661    DIEs in the same CU will be marked.  We used to clear out the offset
7662    and use that as the flag, but ran into ordering problems.  */
7663
7664 static void
7665 mark_dies (dw_die_ref die)
7666 {
7667   dw_die_ref c;
7668
7669   gcc_assert (!die->die_mark);
7670
7671   die->die_mark = 1;
7672   FOR_EACH_CHILD (die, c, mark_dies (c));
7673 }
7674
7675 /* Clear the marks for a die and its children.  */
7676
7677 static void
7678 unmark_dies (dw_die_ref die)
7679 {
7680   dw_die_ref c;
7681
7682   gcc_assert (die->die_mark);
7683
7684   die->die_mark = 0;
7685   FOR_EACH_CHILD (die, c, unmark_dies (c));
7686 }
7687
7688 /* Clear the marks for a die, its children and referred dies.  */
7689
7690 static void
7691 unmark_all_dies (dw_die_ref die)
7692 {
7693   dw_die_ref c;
7694   dw_attr_ref a;
7695   unsigned ix;
7696
7697   if (!die->die_mark)
7698     return;
7699   die->die_mark = 0;
7700
7701   FOR_EACH_CHILD (die, c, unmark_all_dies (c));
7702
7703   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7704     if (AT_class (a) == dw_val_class_die_ref)
7705       unmark_all_dies (AT_ref (a));
7706 }
7707
7708 /* Return the size of the .debug_pubnames or .debug_pubtypes table
7709    generated for the compilation unit.  */
7710
7711 static unsigned long
7712 size_of_pubnames (VEC (pubname_entry, gc) * names)
7713 {
7714   unsigned long size;
7715   unsigned i;
7716   pubname_ref p;
7717
7718   size = DWARF_PUBNAMES_HEADER_SIZE;
7719   for (i = 0; VEC_iterate (pubname_entry, names, i, p); i++)
7720     if (names != pubtype_table
7721         || p->die->die_offset != 0
7722         || !flag_eliminate_unused_debug_types)
7723       size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
7724
7725   size += DWARF_OFFSET_SIZE;
7726   return size;
7727 }
7728
7729 /* Return the size of the information in the .debug_aranges section.  */
7730
7731 static unsigned long
7732 size_of_aranges (void)
7733 {
7734   unsigned long size;
7735
7736   size = DWARF_ARANGES_HEADER_SIZE;
7737
7738   /* Count the address/length pair for this compilation unit.  */
7739   if (text_section_used)
7740     size += 2 * DWARF2_ADDR_SIZE;
7741   if (cold_text_section_used)
7742     size += 2 * DWARF2_ADDR_SIZE;
7743   size += 2 * DWARF2_ADDR_SIZE * arange_table_in_use;
7744
7745   /* Count the two zero words used to terminated the address range table.  */
7746   size += 2 * DWARF2_ADDR_SIZE;
7747   return size;
7748 }
7749 \f
7750 /* Select the encoding of an attribute value.  */
7751
7752 static enum dwarf_form
7753 value_format (dw_attr_ref a)
7754 {
7755   switch (a->dw_attr_val.val_class)
7756     {
7757     case dw_val_class_addr:
7758       return DW_FORM_addr;
7759     case dw_val_class_range_list:
7760     case dw_val_class_offset:
7761     case dw_val_class_loc_list:
7762       switch (DWARF_OFFSET_SIZE)
7763         {
7764         case 4:
7765           return DW_FORM_data4;
7766         case 8:
7767           return DW_FORM_data8;
7768         default:
7769           gcc_unreachable ();
7770         }
7771     case dw_val_class_loc:
7772       switch (constant_size (size_of_locs (AT_loc (a))))
7773         {
7774         case 1:
7775           return DW_FORM_block1;
7776         case 2:
7777           return DW_FORM_block2;
7778         default:
7779           gcc_unreachable ();
7780         }
7781     case dw_val_class_const:
7782       return DW_FORM_sdata;
7783     case dw_val_class_unsigned_const:
7784       switch (constant_size (AT_unsigned (a)))
7785         {
7786         case 1:
7787           return DW_FORM_data1;
7788         case 2:
7789           return DW_FORM_data2;
7790         case 4:
7791           return DW_FORM_data4;
7792         case 8:
7793           return DW_FORM_data8;
7794         default:
7795           gcc_unreachable ();
7796         }
7797     case dw_val_class_long_long:
7798       return DW_FORM_block1;
7799     case dw_val_class_vec:
7800       switch (constant_size (a->dw_attr_val.v.val_vec.length
7801                              * a->dw_attr_val.v.val_vec.elt_size))
7802         {
7803         case 1:
7804           return DW_FORM_block1;
7805         case 2:
7806           return DW_FORM_block2;
7807         case 4:
7808           return DW_FORM_block4;
7809         default:
7810           gcc_unreachable ();
7811         }
7812     case dw_val_class_flag:
7813       return DW_FORM_flag;
7814     case dw_val_class_die_ref:
7815       if (AT_ref_external (a))
7816         return DW_FORM_ref_addr;
7817       else
7818         return DW_FORM_ref;
7819     case dw_val_class_fde_ref:
7820       return DW_FORM_data;
7821     case dw_val_class_lbl_id:
7822       return DW_FORM_addr;
7823     case dw_val_class_lineptr:
7824     case dw_val_class_macptr:
7825       return DW_FORM_data;
7826     case dw_val_class_str:
7827       return AT_string_form (a);
7828     case dw_val_class_file:
7829       switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
7830         {
7831         case 1:
7832           return DW_FORM_data1;
7833         case 2:
7834           return DW_FORM_data2;
7835         case 4:
7836           return DW_FORM_data4;
7837         default:
7838           gcc_unreachable ();
7839         }
7840
7841     default:
7842       gcc_unreachable ();
7843     }
7844 }
7845
7846 /* Output the encoding of an attribute value.  */
7847
7848 static void
7849 output_value_format (dw_attr_ref a)
7850 {
7851   enum dwarf_form form = value_format (a);
7852
7853   dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
7854 }
7855
7856 /* Output the .debug_abbrev section which defines the DIE abbreviation
7857    table.  */
7858
7859 static void
7860 output_abbrev_section (void)
7861 {
7862   unsigned long abbrev_id;
7863
7864   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7865     {
7866       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7867       unsigned ix;
7868       dw_attr_ref a_attr;
7869
7870       dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
7871       dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
7872                                    dwarf_tag_name (abbrev->die_tag));
7873
7874       if (abbrev->die_child != NULL)
7875         dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
7876       else
7877         dw2_asm_output_data (1, DW_children_no, "DW_children_no");
7878
7879       for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
7880            ix++)
7881         {
7882           dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
7883                                        dwarf_attr_name (a_attr->dw_attr));
7884           output_value_format (a_attr);
7885         }
7886
7887       dw2_asm_output_data (1, 0, NULL);
7888       dw2_asm_output_data (1, 0, NULL);
7889     }
7890
7891   /* Terminate the table.  */
7892   dw2_asm_output_data (1, 0, NULL);
7893 }
7894
7895 /* Output a symbol we can use to refer to this DIE from another CU.  */
7896
7897 static inline void
7898 output_die_symbol (dw_die_ref die)
7899 {
7900   char *sym = die->die_symbol;
7901
7902   if (sym == 0)
7903     return;
7904
7905   if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
7906     /* We make these global, not weak; if the target doesn't support
7907        .linkonce, it doesn't support combining the sections, so debugging
7908        will break.  */
7909     targetm.asm_out.globalize_label (asm_out_file, sym);
7910
7911   ASM_OUTPUT_LABEL (asm_out_file, sym);
7912 }
7913
7914 /* Return a new location list, given the begin and end range, and the
7915    expression. gensym tells us whether to generate a new internal symbol for
7916    this location list node, which is done for the head of the list only.  */
7917
7918 static inline dw_loc_list_ref
7919 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
7920               const char *section, unsigned int gensym)
7921 {
7922   dw_loc_list_ref retlist = GGC_CNEW (dw_loc_list_node);
7923
7924   retlist->begin = begin;
7925   retlist->end = end;
7926   retlist->expr = expr;
7927   retlist->section = section;
7928   if (gensym)
7929     retlist->ll_symbol = gen_internal_sym ("LLST");
7930
7931   return retlist;
7932 }
7933
7934 /* Add a location description expression to a location list.  */
7935
7936 static inline void
7937 add_loc_descr_to_loc_list (dw_loc_list_ref *list_head, dw_loc_descr_ref descr,
7938                            const char *begin, const char *end,
7939                            const char *section)
7940 {
7941   dw_loc_list_ref *d;
7942
7943   /* Find the end of the chain.  */
7944   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
7945     ;
7946
7947   /* Add a new location list node to the list.  */
7948   *d = new_loc_list (descr, begin, end, section, 0);
7949 }
7950
7951 /* Output the location list given to us.  */
7952
7953 static void
7954 output_loc_list (dw_loc_list_ref list_head)
7955 {
7956   dw_loc_list_ref curr = list_head;
7957
7958   ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
7959
7960   /* Walk the location list, and output each range + expression.  */
7961   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
7962     {
7963       unsigned long size;
7964       /* Don't output an entry that starts and ends at the same address.  */
7965       if (strcmp (curr->begin, curr->end) == 0)
7966         continue;
7967       if (!have_multiple_function_sections)
7968         {
7969           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
7970                                 "Location list begin address (%s)",
7971                                 list_head->ll_symbol);
7972           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
7973                                 "Location list end address (%s)",
7974                                 list_head->ll_symbol);
7975         }
7976       else
7977         {
7978           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
7979                                "Location list begin address (%s)",
7980                                list_head->ll_symbol);
7981           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
7982                                "Location list end address (%s)",
7983                                list_head->ll_symbol);
7984         }
7985       size = size_of_locs (curr->expr);
7986
7987       /* Output the block length for this list of location operations.  */
7988       gcc_assert (size <= 0xffff);
7989       dw2_asm_output_data (2, size, "%s", "Location expression size");
7990
7991       output_loc_sequence (curr->expr);
7992     }
7993
7994   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
7995                        "Location list terminator begin (%s)",
7996                        list_head->ll_symbol);
7997   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
7998                        "Location list terminator end (%s)",
7999                        list_head->ll_symbol);
8000 }
8001
8002 /* Output the DIE and its attributes.  Called recursively to generate
8003    the definitions of each child DIE.  */
8004
8005 static void
8006 output_die (dw_die_ref die)
8007 {
8008   dw_attr_ref a;
8009   dw_die_ref c;
8010   unsigned long size;
8011   unsigned ix;
8012
8013   /* If someone in another CU might refer to us, set up a symbol for
8014      them to point to.  */
8015   if (die->die_symbol)
8016     output_die_symbol (die);
8017
8018   dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (0x%lx) %s)",
8019                                (unsigned long)die->die_offset,
8020                                dwarf_tag_name (die->die_tag));
8021
8022   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
8023     {
8024       const char *name = dwarf_attr_name (a->dw_attr);
8025
8026       switch (AT_class (a))
8027         {
8028         case dw_val_class_addr:
8029           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8030           break;
8031
8032         case dw_val_class_offset:
8033           dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8034                                "%s", name);
8035           break;
8036
8037         case dw_val_class_range_list:
8038           {
8039             char *p = strchr (ranges_section_label, '\0');
8040
8041             sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
8042                      a->dw_attr_val.v.val_offset);
8043             dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8044                                    debug_ranges_section, "%s", name);
8045             *p = '\0';
8046           }
8047           break;
8048
8049         case dw_val_class_loc:
8050           size = size_of_locs (AT_loc (a));
8051
8052           /* Output the block length for this list of location operations.  */
8053           dw2_asm_output_data (constant_size (size), size, "%s", name);
8054
8055           output_loc_sequence (AT_loc (a));
8056           break;
8057
8058         case dw_val_class_const:
8059           /* ??? It would be slightly more efficient to use a scheme like is
8060              used for unsigned constants below, but gdb 4.x does not sign
8061              extend.  Gdb 5.x does sign extend.  */
8062           dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8063           break;
8064
8065         case dw_val_class_unsigned_const:
8066           dw2_asm_output_data (constant_size (AT_unsigned (a)),
8067                                AT_unsigned (a), "%s", name);
8068           break;
8069
8070         case dw_val_class_long_long:
8071           {
8072             unsigned HOST_WIDE_INT first, second;
8073
8074             dw2_asm_output_data (1,
8075                                  2 * HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
8076                                  "%s", name);
8077
8078             if (WORDS_BIG_ENDIAN)
8079               {
8080                 first = a->dw_attr_val.v.val_long_long.hi;
8081                 second = a->dw_attr_val.v.val_long_long.low;
8082               }
8083             else
8084               {
8085                 first = a->dw_attr_val.v.val_long_long.low;
8086                 second = a->dw_attr_val.v.val_long_long.hi;
8087               }
8088
8089             dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
8090                                  first, "long long constant");
8091             dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
8092                                  second, NULL);
8093           }
8094           break;
8095
8096         case dw_val_class_vec:
8097           {
8098             unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
8099             unsigned int len = a->dw_attr_val.v.val_vec.length;
8100             unsigned int i;
8101             unsigned char *p;
8102
8103             dw2_asm_output_data (constant_size (len * elt_size),
8104                                  len * elt_size, "%s", name);
8105             if (elt_size > sizeof (HOST_WIDE_INT))
8106               {
8107                 elt_size /= 2;
8108                 len *= 2;
8109               }
8110             for (i = 0, p = a->dw_attr_val.v.val_vec.array;
8111                  i < len;
8112                  i++, p += elt_size)
8113               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
8114                                    "fp or vector constant word %u", i);
8115             break;
8116           }
8117
8118         case dw_val_class_flag:
8119           dw2_asm_output_data (1, AT_flag (a), "%s", name);
8120           break;
8121
8122         case dw_val_class_loc_list:
8123           {
8124             char *sym = AT_loc_list (a)->ll_symbol;
8125
8126             gcc_assert (sym);
8127             dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8128                                    "%s", name);
8129           }
8130           break;
8131
8132         case dw_val_class_die_ref:
8133           if (AT_ref_external (a))
8134             {
8135               char *sym = AT_ref (a)->die_symbol;
8136
8137               gcc_assert (sym);
8138               dw2_asm_output_offset (DWARF2_ADDR_SIZE, sym, debug_info_section,
8139                                      "%s", name);
8140             }
8141           else
8142             {
8143               gcc_assert (AT_ref (a)->die_offset);
8144               dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
8145                                    "%s", name);
8146             }
8147           break;
8148
8149         case dw_val_class_fde_ref:
8150           {
8151             char l1[20];
8152
8153             ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
8154                                          a->dw_attr_val.v.val_fde_index * 2);
8155             dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
8156                                    "%s", name);
8157           }
8158           break;
8159
8160         case dw_val_class_lbl_id:
8161           dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8162           break;
8163
8164         case dw_val_class_lineptr:
8165           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8166                                  debug_line_section, "%s", name);
8167           break;
8168
8169         case dw_val_class_macptr:
8170           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8171                                  debug_macinfo_section, "%s", name);
8172           break;
8173
8174         case dw_val_class_str:
8175           if (AT_string_form (a) == DW_FORM_strp)
8176             dw2_asm_output_offset (DWARF_OFFSET_SIZE,
8177                                    a->dw_attr_val.v.val_str->label,
8178                                    debug_str_section,
8179                                    "%s: \"%s\"", name, AT_string (a));
8180           else
8181             dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
8182           break;
8183
8184         case dw_val_class_file:
8185           {
8186             int f = maybe_emit_file (a->dw_attr_val.v.val_file);
8187
8188             dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
8189                                  a->dw_attr_val.v.val_file->filename);
8190             break;
8191           }
8192
8193         default:
8194           gcc_unreachable ();
8195         }
8196     }
8197
8198   FOR_EACH_CHILD (die, c, output_die (c));
8199
8200   /* Add null byte to terminate sibling list.  */
8201   if (die->die_child != NULL)
8202     dw2_asm_output_data (1, 0, "end of children of DIE 0x%lx",
8203                          (unsigned long) die->die_offset);
8204 }
8205
8206 /* Output the compilation unit that appears at the beginning of the
8207    .debug_info section, and precedes the DIE descriptions.  */
8208
8209 static void
8210 output_compilation_unit_header (void)
8211 {
8212   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8213     dw2_asm_output_data (4, 0xffffffff,
8214       "Initial length escape value indicating 64-bit DWARF extension");
8215   dw2_asm_output_data (DWARF_OFFSET_SIZE,
8216                        next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
8217                        "Length of Compilation Unit Info");
8218   dw2_asm_output_data (2, DWARF_VERSION, "DWARF version number");
8219   dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
8220                          debug_abbrev_section,
8221                          "Offset Into Abbrev. Section");
8222   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8223 }
8224
8225 /* Output the compilation unit DIE and its children.  */
8226
8227 static void
8228 output_comp_unit (dw_die_ref die, int output_if_empty)
8229 {
8230   const char *secname;
8231   char *oldsym, *tmp;
8232
8233   /* Unless we are outputting main CU, we may throw away empty ones.  */
8234   if (!output_if_empty && die->die_child == NULL)
8235     return;
8236
8237   /* Even if there are no children of this DIE, we must output the information
8238      about the compilation unit.  Otherwise, on an empty translation unit, we
8239      will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
8240      will then complain when examining the file.  First mark all the DIEs in
8241      this CU so we know which get local refs.  */
8242   mark_dies (die);
8243
8244   build_abbrev_table (die);
8245
8246   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
8247   next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8248   calc_die_sizes (die);
8249
8250   oldsym = die->die_symbol;
8251   if (oldsym)
8252     {
8253       tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
8254
8255       sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
8256       secname = tmp;
8257       die->die_symbol = NULL;
8258       switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
8259     }
8260   else
8261     switch_to_section (debug_info_section);
8262
8263   /* Output debugging information.  */
8264   output_compilation_unit_header ();
8265   output_die (die);
8266
8267   /* Leave the marks on the main CU, so we can check them in
8268      output_pubnames.  */
8269   if (oldsym)
8270     {
8271       unmark_dies (die);
8272       die->die_symbol = oldsym;
8273     }
8274 }
8275
8276 /* Return the DWARF2/3 pubname associated with a decl.  */
8277
8278 static const char *
8279 dwarf2_name (tree decl, int scope)
8280 {
8281   return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
8282 }
8283
8284 /* Add a new entry to .debug_pubnames if appropriate.  */
8285
8286 static void
8287 add_pubname_string (const char *str, dw_die_ref die)
8288 {
8289   pubname_entry e;
8290
8291   e.die = die;
8292   e.name = xstrdup (str);
8293   VEC_safe_push (pubname_entry, gc, pubname_table, &e);
8294 }
8295
8296 static void
8297 add_pubname (tree decl, dw_die_ref die)
8298 {
8299
8300   if (TREE_PUBLIC (decl))
8301     add_pubname_string (dwarf2_name (decl, 1), die);
8302 }
8303
8304 /* Add a new entry to .debug_pubtypes if appropriate.  */
8305
8306 static void
8307 add_pubtype (tree decl, dw_die_ref die)
8308 {
8309   pubname_entry e;
8310
8311   e.name = NULL;
8312   if ((TREE_PUBLIC (decl)
8313        || die->die_parent == comp_unit_die)
8314       && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
8315     {
8316       e.die = die;
8317       if (TYPE_P (decl))
8318         {
8319           if (TYPE_NAME (decl))
8320             {
8321               if (TREE_CODE (TYPE_NAME (decl)) == IDENTIFIER_NODE)
8322                 e.name = IDENTIFIER_POINTER (TYPE_NAME (decl));
8323               else if (TREE_CODE (TYPE_NAME (decl)) == TYPE_DECL
8324                        && DECL_NAME (TYPE_NAME (decl)))
8325                 e.name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (decl)));
8326               else
8327                e.name = xstrdup ((const char *) get_AT_string (die, DW_AT_name));
8328             }
8329         }
8330       else
8331         e.name = xstrdup (dwarf2_name (decl, 1));
8332
8333       /* If we don't have a name for the type, there's no point in adding
8334          it to the table.  */
8335       if (e.name && e.name[0] != '\0')
8336         VEC_safe_push (pubname_entry, gc, pubtype_table, &e);
8337     }
8338 }
8339
8340 /* Output the public names table used to speed up access to externally
8341    visible names; or the public types table used to find type definitions.  */
8342
8343 static void
8344 output_pubnames (VEC (pubname_entry, gc) * names)
8345 {
8346   unsigned i;
8347   unsigned long pubnames_length = size_of_pubnames (names);
8348   pubname_ref pub;
8349
8350   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8351     dw2_asm_output_data (4, 0xffffffff,
8352       "Initial length escape value indicating 64-bit DWARF extension");
8353   if (names == pubname_table)
8354     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
8355                          "Length of Public Names Info");
8356   else
8357     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
8358                          "Length of Public Type Names Info");
8359   dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
8360   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
8361                          debug_info_section,
8362                          "Offset of Compilation Unit Info");
8363   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
8364                        "Compilation Unit Length");
8365
8366   for (i = 0; VEC_iterate (pubname_entry, names, i, pub); i++)
8367     {
8368       /* We shouldn't see pubnames for DIEs outside of the main CU.  */
8369       if (names == pubname_table)
8370         gcc_assert (pub->die->die_mark);
8371
8372       if (names != pubtype_table
8373           || pub->die->die_offset != 0
8374           || !flag_eliminate_unused_debug_types)
8375         {
8376           dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
8377                                "DIE offset");
8378
8379           dw2_asm_output_nstring (pub->name, -1, "external name");
8380         }
8381     }
8382
8383   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
8384 }
8385
8386 /* Add a new entry to .debug_aranges if appropriate.  */
8387
8388 static void
8389 add_arange (tree decl, dw_die_ref die)
8390 {
8391   if (! DECL_SECTION_NAME (decl))
8392     return;
8393
8394   if (arange_table_in_use == arange_table_allocated)
8395     {
8396       arange_table_allocated += ARANGE_TABLE_INCREMENT;
8397       arange_table = GGC_RESIZEVEC (dw_die_ref, arange_table,
8398                                     arange_table_allocated);
8399       memset (arange_table + arange_table_in_use, 0,
8400               ARANGE_TABLE_INCREMENT * sizeof (dw_die_ref));
8401     }
8402
8403   arange_table[arange_table_in_use++] = die;
8404 }
8405
8406 /* Output the information that goes into the .debug_aranges table.
8407    Namely, define the beginning and ending address range of the
8408    text section generated for this compilation unit.  */
8409
8410 static void
8411 output_aranges (void)
8412 {
8413   unsigned i;
8414   unsigned long aranges_length = size_of_aranges ();
8415
8416   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8417     dw2_asm_output_data (4, 0xffffffff,
8418       "Initial length escape value indicating 64-bit DWARF extension");
8419   dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
8420                        "Length of Address Ranges Info");
8421   dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
8422   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
8423                          debug_info_section,
8424                          "Offset of Compilation Unit Info");
8425   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
8426   dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
8427
8428   /* We need to align to twice the pointer size here.  */
8429   if (DWARF_ARANGES_PAD_SIZE)
8430     {
8431       /* Pad using a 2 byte words so that padding is correct for any
8432          pointer size.  */
8433       dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
8434                            2 * DWARF2_ADDR_SIZE);
8435       for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
8436         dw2_asm_output_data (2, 0, NULL);
8437     }
8438
8439   /* It is necessary not to output these entries if the sections were
8440      not used; if the sections were not used, the length will be 0 and
8441      the address may end up as 0 if the section is discarded by ld
8442      --gc-sections, leaving an invalid (0, 0) entry that can be
8443      confused with the terminator.  */
8444   if (text_section_used)
8445     {
8446       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
8447       dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
8448                             text_section_label, "Length");
8449     }
8450   if (cold_text_section_used)
8451     {
8452       dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
8453                            "Address");
8454       dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
8455                             cold_text_section_label, "Length");
8456     }
8457
8458   for (i = 0; i < arange_table_in_use; i++)
8459     {
8460       dw_die_ref die = arange_table[i];
8461
8462       /* We shouldn't see aranges for DIEs outside of the main CU.  */
8463       gcc_assert (die->die_mark);
8464
8465       if (die->die_tag == DW_TAG_subprogram)
8466         {
8467           dw2_asm_output_addr (DWARF2_ADDR_SIZE, get_AT_low_pc (die),
8468                                "Address");
8469           dw2_asm_output_delta (DWARF2_ADDR_SIZE, get_AT_hi_pc (die),
8470                                 get_AT_low_pc (die), "Length");
8471         }
8472       else
8473         {
8474           /* A static variable; extract the symbol from DW_AT_location.
8475              Note that this code isn't currently hit, as we only emit
8476              aranges for functions (jason 9/23/99).  */
8477           dw_attr_ref a = get_AT (die, DW_AT_location);
8478           dw_loc_descr_ref loc;
8479
8480           gcc_assert (a && AT_class (a) == dw_val_class_loc);
8481
8482           loc = AT_loc (a);
8483           gcc_assert (loc->dw_loc_opc == DW_OP_addr);
8484
8485           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE,
8486                                    loc->dw_loc_oprnd1.v.val_addr, "Address");
8487           dw2_asm_output_data (DWARF2_ADDR_SIZE,
8488                                get_AT_unsigned (die, DW_AT_byte_size),
8489                                "Length");
8490         }
8491     }
8492
8493   /* Output the terminator words.  */
8494   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8495   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8496 }
8497
8498 /* Add a new entry to .debug_ranges.  Return the offset at which it
8499    was placed.  */
8500
8501 static unsigned int
8502 add_ranges_num (int num)
8503 {
8504   unsigned int in_use = ranges_table_in_use;
8505
8506   if (in_use == ranges_table_allocated)
8507     {
8508       ranges_table_allocated += RANGES_TABLE_INCREMENT;
8509       ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
8510                                     ranges_table_allocated);
8511       memset (ranges_table + ranges_table_in_use, 0,
8512               RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
8513     }
8514
8515   ranges_table[in_use].num = num;
8516   ranges_table_in_use = in_use + 1;
8517
8518   return in_use * 2 * DWARF2_ADDR_SIZE;
8519 }
8520
8521 /* Add a new entry to .debug_ranges corresponding to a block, or a
8522    range terminator if BLOCK is NULL.  */
8523
8524 static unsigned int
8525 add_ranges (const_tree block)
8526 {
8527   return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
8528 }
8529
8530 /* Add a new entry to .debug_ranges corresponding to a pair of
8531    labels.  */
8532
8533 static unsigned int
8534 add_ranges_by_labels (const char *begin, const char *end)
8535 {
8536   unsigned int in_use = ranges_by_label_in_use;
8537
8538   if (in_use == ranges_by_label_allocated)
8539     {
8540       ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
8541       ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
8542                                        ranges_by_label,
8543                                        ranges_by_label_allocated);
8544       memset (ranges_by_label + ranges_by_label_in_use, 0,
8545               RANGES_TABLE_INCREMENT
8546               * sizeof (struct dw_ranges_by_label_struct));
8547     }
8548
8549   ranges_by_label[in_use].begin = begin;
8550   ranges_by_label[in_use].end = end;
8551   ranges_by_label_in_use = in_use + 1;
8552
8553   return add_ranges_num (-(int)in_use - 1);
8554 }
8555
8556 static void
8557 output_ranges (void)
8558 {
8559   unsigned i;
8560   static const char *const start_fmt = "Offset 0x%x";
8561   const char *fmt = start_fmt;
8562
8563   for (i = 0; i < ranges_table_in_use; i++)
8564     {
8565       int block_num = ranges_table[i].num;
8566
8567       if (block_num > 0)
8568         {
8569           char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
8570           char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
8571
8572           ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
8573           ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
8574
8575           /* If all code is in the text section, then the compilation
8576              unit base address defaults to DW_AT_low_pc, which is the
8577              base of the text section.  */
8578           if (!have_multiple_function_sections)
8579             {
8580               dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
8581                                     text_section_label,
8582                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
8583               dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
8584                                     text_section_label, NULL);
8585             }
8586
8587           /* Otherwise, the compilation unit base address is zero,
8588              which allows us to use absolute addresses, and not worry
8589              about whether the target supports cross-section
8590              arithmetic.  */
8591           else
8592             {
8593               dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
8594                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
8595               dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
8596             }
8597
8598           fmt = NULL;
8599         }
8600
8601       /* Negative block_num stands for an index into ranges_by_label.  */
8602       else if (block_num < 0)
8603         {
8604           int lab_idx = - block_num - 1;
8605
8606           if (!have_multiple_function_sections)
8607             {
8608               gcc_unreachable ();
8609 #if 0
8610               /* If we ever use add_ranges_by_labels () for a single
8611                  function section, all we have to do is to take out
8612                  the #if 0 above.  */
8613               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
8614                                     ranges_by_label[lab_idx].begin,
8615                                     text_section_label,
8616                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
8617               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
8618                                     ranges_by_label[lab_idx].end,
8619                                     text_section_label, NULL);
8620 #endif
8621             }
8622           else
8623             {
8624               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
8625                                    ranges_by_label[lab_idx].begin,
8626                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
8627               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
8628                                    ranges_by_label[lab_idx].end,
8629                                    NULL);
8630             }
8631         }
8632       else
8633         {
8634           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8635           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8636           fmt = start_fmt;
8637         }
8638     }
8639 }
8640
8641 /* Data structure containing information about input files.  */
8642 struct file_info
8643 {
8644   const char *path;     /* Complete file name.  */
8645   const char *fname;    /* File name part.  */
8646   int length;           /* Length of entire string.  */
8647   struct dwarf_file_data * file_idx;    /* Index in input file table.  */
8648   int dir_idx;          /* Index in directory table.  */
8649 };
8650
8651 /* Data structure containing information about directories with source
8652    files.  */
8653 struct dir_info
8654 {
8655   const char *path;     /* Path including directory name.  */
8656   int length;           /* Path length.  */
8657   int prefix;           /* Index of directory entry which is a prefix.  */
8658   int count;            /* Number of files in this directory.  */
8659   int dir_idx;          /* Index of directory used as base.  */
8660 };
8661
8662 /* Callback function for file_info comparison.  We sort by looking at
8663    the directories in the path.  */
8664
8665 static int
8666 file_info_cmp (const void *p1, const void *p2)
8667 {
8668   const struct file_info *const s1 = (const struct file_info *) p1;
8669   const struct file_info *const s2 = (const struct file_info *) p2;
8670   const unsigned char *cp1;
8671   const unsigned char *cp2;
8672
8673   /* Take care of file names without directories.  We need to make sure that
8674      we return consistent values to qsort since some will get confused if
8675      we return the same value when identical operands are passed in opposite
8676      orders.  So if neither has a directory, return 0 and otherwise return
8677      1 or -1 depending on which one has the directory.  */
8678   if ((s1->path == s1->fname || s2->path == s2->fname))
8679     return (s2->path == s2->fname) - (s1->path == s1->fname);
8680
8681   cp1 = (const unsigned char *) s1->path;
8682   cp2 = (const unsigned char *) s2->path;
8683
8684   while (1)
8685     {
8686       ++cp1;
8687       ++cp2;
8688       /* Reached the end of the first path?  If so, handle like above.  */
8689       if ((cp1 == (const unsigned char *) s1->fname)
8690           || (cp2 == (const unsigned char *) s2->fname))
8691         return ((cp2 == (const unsigned char *) s2->fname)
8692                 - (cp1 == (const unsigned char *) s1->fname));
8693
8694       /* Character of current path component the same?  */
8695       else if (*cp1 != *cp2)
8696         return *cp1 - *cp2;
8697     }
8698 }
8699
8700 struct file_name_acquire_data
8701 {
8702   struct file_info *files;
8703   int used_files;
8704   int max_files;
8705 };
8706
8707 /* Traversal function for the hash table.  */
8708
8709 static int
8710 file_name_acquire (void ** slot, void *data)
8711 {
8712   struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
8713   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
8714   struct file_info *fi;
8715   const char *f;
8716
8717   gcc_assert (fnad->max_files >= d->emitted_number);
8718
8719   if (! d->emitted_number)
8720     return 1;
8721
8722   gcc_assert (fnad->max_files != fnad->used_files);
8723
8724   fi = fnad->files + fnad->used_files++;
8725
8726   /* Skip all leading "./".  */
8727   f = d->filename;
8728   while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
8729     f += 2;
8730
8731   /* Create a new array entry.  */
8732   fi->path = f;
8733   fi->length = strlen (f);
8734   fi->file_idx = d;
8735
8736   /* Search for the file name part.  */
8737   f = strrchr (f, DIR_SEPARATOR);
8738 #if defined (DIR_SEPARATOR_2)
8739   {
8740     char *g = strrchr (fi->path, DIR_SEPARATOR_2);
8741
8742     if (g != NULL)
8743       {
8744         if (f == NULL || f < g)
8745           f = g;
8746       }
8747   }
8748 #endif
8749
8750   fi->fname = f == NULL ? fi->path : f + 1;
8751   return 1;
8752 }
8753
8754 /* Output the directory table and the file name table.  We try to minimize
8755    the total amount of memory needed.  A heuristic is used to avoid large
8756    slowdowns with many input files.  */
8757
8758 static void
8759 output_file_names (void)
8760 {
8761   struct file_name_acquire_data fnad;
8762   int numfiles;
8763   struct file_info *files;
8764   struct dir_info *dirs;
8765   int *saved;
8766   int *savehere;
8767   int *backmap;
8768   int ndirs;
8769   int idx_offset;
8770   int i;
8771   int idx;
8772
8773   if (!last_emitted_file)
8774     {
8775       dw2_asm_output_data (1, 0, "End directory table");
8776       dw2_asm_output_data (1, 0, "End file name table");
8777       return;
8778     }
8779
8780   numfiles = last_emitted_file->emitted_number;
8781
8782   /* Allocate the various arrays we need.  */
8783   files = XALLOCAVEC (struct file_info, numfiles);
8784   dirs = XALLOCAVEC (struct dir_info, numfiles);
8785
8786   fnad.files = files;
8787   fnad.used_files = 0;
8788   fnad.max_files = numfiles;
8789   htab_traverse (file_table, file_name_acquire, &fnad);
8790   gcc_assert (fnad.used_files == fnad.max_files);
8791
8792   qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
8793
8794   /* Find all the different directories used.  */
8795   dirs[0].path = files[0].path;
8796   dirs[0].length = files[0].fname - files[0].path;
8797   dirs[0].prefix = -1;
8798   dirs[0].count = 1;
8799   dirs[0].dir_idx = 0;
8800   files[0].dir_idx = 0;
8801   ndirs = 1;
8802
8803   for (i = 1; i < numfiles; i++)
8804     if (files[i].fname - files[i].path == dirs[ndirs - 1].length
8805         && memcmp (dirs[ndirs - 1].path, files[i].path,
8806                    dirs[ndirs - 1].length) == 0)
8807       {
8808         /* Same directory as last entry.  */
8809         files[i].dir_idx = ndirs - 1;
8810         ++dirs[ndirs - 1].count;
8811       }
8812     else
8813       {
8814         int j;
8815
8816         /* This is a new directory.  */
8817         dirs[ndirs].path = files[i].path;
8818         dirs[ndirs].length = files[i].fname - files[i].path;
8819         dirs[ndirs].count = 1;
8820         dirs[ndirs].dir_idx = ndirs;
8821         files[i].dir_idx = ndirs;
8822
8823         /* Search for a prefix.  */
8824         dirs[ndirs].prefix = -1;
8825         for (j = 0; j < ndirs; j++)
8826           if (dirs[j].length < dirs[ndirs].length
8827               && dirs[j].length > 1
8828               && (dirs[ndirs].prefix == -1
8829                   || dirs[j].length > dirs[dirs[ndirs].prefix].length)
8830               && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
8831             dirs[ndirs].prefix = j;
8832
8833         ++ndirs;
8834       }
8835
8836   /* Now to the actual work.  We have to find a subset of the directories which
8837      allow expressing the file name using references to the directory table
8838      with the least amount of characters.  We do not do an exhaustive search
8839      where we would have to check out every combination of every single
8840      possible prefix.  Instead we use a heuristic which provides nearly optimal
8841      results in most cases and never is much off.  */
8842   saved = XALLOCAVEC (int, ndirs);
8843   savehere = XALLOCAVEC (int, ndirs);
8844
8845   memset (saved, '\0', ndirs * sizeof (saved[0]));
8846   for (i = 0; i < ndirs; i++)
8847     {
8848       int j;
8849       int total;
8850
8851       /* We can always save some space for the current directory.  But this
8852          does not mean it will be enough to justify adding the directory.  */
8853       savehere[i] = dirs[i].length;
8854       total = (savehere[i] - saved[i]) * dirs[i].count;
8855
8856       for (j = i + 1; j < ndirs; j++)
8857         {
8858           savehere[j] = 0;
8859           if (saved[j] < dirs[i].length)
8860             {
8861               /* Determine whether the dirs[i] path is a prefix of the
8862                  dirs[j] path.  */
8863               int k;
8864
8865               k = dirs[j].prefix;
8866               while (k != -1 && k != (int) i)
8867                 k = dirs[k].prefix;
8868
8869               if (k == (int) i)
8870                 {
8871                   /* Yes it is.  We can possibly save some memory by
8872                      writing the filenames in dirs[j] relative to
8873                      dirs[i].  */
8874                   savehere[j] = dirs[i].length;
8875                   total += (savehere[j] - saved[j]) * dirs[j].count;
8876                 }
8877             }
8878         }
8879
8880       /* Check whether we can save enough to justify adding the dirs[i]
8881          directory.  */
8882       if (total > dirs[i].length + 1)
8883         {
8884           /* It's worthwhile adding.  */
8885           for (j = i; j < ndirs; j++)
8886             if (savehere[j] > 0)
8887               {
8888                 /* Remember how much we saved for this directory so far.  */
8889                 saved[j] = savehere[j];
8890
8891                 /* Remember the prefix directory.  */
8892                 dirs[j].dir_idx = i;
8893               }
8894         }
8895     }
8896
8897   /* Emit the directory name table.  */
8898   idx = 1;
8899   idx_offset = dirs[0].length > 0 ? 1 : 0;
8900   for (i = 1 - idx_offset; i < ndirs; i++)
8901     dw2_asm_output_nstring (dirs[i].path, dirs[i].length - 1,
8902                             "Directory Entry: 0x%x", i + idx_offset);
8903
8904   dw2_asm_output_data (1, 0, "End directory table");
8905
8906   /* We have to emit them in the order of emitted_number since that's
8907      used in the debug info generation.  To do this efficiently we
8908      generate a back-mapping of the indices first.  */
8909   backmap = XALLOCAVEC (int, numfiles);
8910   for (i = 0; i < numfiles; i++)
8911     backmap[files[i].file_idx->emitted_number - 1] = i;
8912
8913   /* Now write all the file names.  */
8914   for (i = 0; i < numfiles; i++)
8915     {
8916       int file_idx = backmap[i];
8917       int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
8918
8919       dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
8920                               "File Entry: 0x%x", (unsigned) i + 1);
8921
8922       /* Include directory index.  */
8923       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
8924
8925       /* Modification time.  */
8926       dw2_asm_output_data_uleb128 (0, NULL);
8927
8928       /* File length in bytes.  */
8929       dw2_asm_output_data_uleb128 (0, NULL);
8930     }
8931
8932   dw2_asm_output_data (1, 0, "End file name table");
8933 }
8934
8935
8936 /* Output the source line number correspondence information.  This
8937    information goes into the .debug_line section.  */
8938
8939 static void
8940 output_line_info (void)
8941 {
8942   char l1[20], l2[20], p1[20], p2[20];
8943   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
8944   char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
8945   unsigned opc;
8946   unsigned n_op_args;
8947   unsigned long lt_index;
8948   unsigned long current_line;
8949   long line_offset;
8950   long line_delta;
8951   unsigned long current_file;
8952   unsigned long function;
8953
8954   ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
8955   ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
8956   ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
8957   ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
8958
8959   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8960     dw2_asm_output_data (4, 0xffffffff,
8961       "Initial length escape value indicating 64-bit DWARF extension");
8962   dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
8963                         "Length of Source Line Info");
8964   ASM_OUTPUT_LABEL (asm_out_file, l1);
8965
8966   dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
8967   dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
8968   ASM_OUTPUT_LABEL (asm_out_file, p1);
8969
8970   /* Define the architecture-dependent minimum instruction length (in
8971    bytes).  In this implementation of DWARF, this field is used for
8972    information purposes only.  Since GCC generates assembly language,
8973    we have no a priori knowledge of how many instruction bytes are
8974    generated for each source line, and therefore can use only the
8975    DW_LNE_set_address and DW_LNS_fixed_advance_pc line information
8976    commands.  Accordingly, we fix this as `1', which is "correct
8977    enough" for all architectures, and don't let the target override.  */
8978   dw2_asm_output_data (1, 1,
8979                        "Minimum Instruction Length");
8980
8981   dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
8982                        "Default is_stmt_start flag");
8983   dw2_asm_output_data (1, DWARF_LINE_BASE,
8984                        "Line Base Value (Special Opcodes)");
8985   dw2_asm_output_data (1, DWARF_LINE_RANGE,
8986                        "Line Range Value (Special Opcodes)");
8987   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
8988                        "Special Opcode Base");
8989
8990   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
8991     {
8992       switch (opc)
8993         {
8994         case DW_LNS_advance_pc:
8995         case DW_LNS_advance_line:
8996         case DW_LNS_set_file:
8997         case DW_LNS_set_column:
8998         case DW_LNS_fixed_advance_pc:
8999           n_op_args = 1;
9000           break;
9001         default:
9002           n_op_args = 0;
9003           break;
9004         }
9005
9006       dw2_asm_output_data (1, n_op_args, "opcode: 0x%x has %d args",
9007                            opc, n_op_args);
9008     }
9009
9010   /* Write out the information about the files we use.  */
9011   output_file_names ();
9012   ASM_OUTPUT_LABEL (asm_out_file, p2);
9013
9014   /* We used to set the address register to the first location in the text
9015      section here, but that didn't accomplish anything since we already
9016      have a line note for the opening brace of the first function.  */
9017
9018   /* Generate the line number to PC correspondence table, encoded as
9019      a series of state machine operations.  */
9020   current_file = 1;
9021   current_line = 1;
9022
9023   if (cfun && in_cold_section_p)
9024     strcpy (prev_line_label, crtl->subsections.cold_section_label);
9025   else
9026     strcpy (prev_line_label, text_section_label);
9027   for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
9028     {
9029       dw_line_info_ref line_info = &line_info_table[lt_index];
9030
9031 #if 0
9032       /* Disable this optimization for now; GDB wants to see two line notes
9033          at the beginning of a function so it can find the end of the
9034          prologue.  */
9035
9036       /* Don't emit anything for redundant notes.  Just updating the
9037          address doesn't accomplish anything, because we already assume
9038          that anything after the last address is this line.  */
9039       if (line_info->dw_line_num == current_line
9040           && line_info->dw_file_num == current_file)
9041         continue;
9042 #endif
9043
9044       /* Emit debug info for the address of the current line.
9045
9046          Unfortunately, we have little choice here currently, and must always
9047          use the most general form.  GCC does not know the address delta
9048          itself, so we can't use DW_LNS_advance_pc.  Many ports do have length
9049          attributes which will give an upper bound on the address range.  We
9050          could perhaps use length attributes to determine when it is safe to
9051          use DW_LNS_fixed_advance_pc.  */
9052
9053       ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
9054       if (0)
9055         {
9056           /* This can handle deltas up to 0xffff.  This takes 3 bytes.  */
9057           dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
9058                                "DW_LNS_fixed_advance_pc");
9059           dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
9060         }
9061       else
9062         {
9063           /* This can handle any delta.  This takes
9064              4+DWARF2_ADDR_SIZE bytes.  */
9065           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9066           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9067           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9068           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9069         }
9070
9071       strcpy (prev_line_label, line_label);
9072
9073       /* Emit debug info for the source file of the current line, if
9074          different from the previous line.  */
9075       if (line_info->dw_file_num != current_file)
9076         {
9077           current_file = line_info->dw_file_num;
9078           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
9079           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
9080         }
9081
9082       /* Emit debug info for the current line number, choosing the encoding
9083          that uses the least amount of space.  */
9084       if (line_info->dw_line_num != current_line)
9085         {
9086           line_offset = line_info->dw_line_num - current_line;
9087           line_delta = line_offset - DWARF_LINE_BASE;
9088           current_line = line_info->dw_line_num;
9089           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
9090             /* This can handle deltas from -10 to 234, using the current
9091                definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.  This
9092                takes 1 byte.  */
9093             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
9094                                  "line %lu", current_line);
9095           else
9096             {
9097               /* This can handle any delta.  This takes at least 4 bytes,
9098                  depending on the value being encoded.  */
9099               dw2_asm_output_data (1, DW_LNS_advance_line,
9100                                    "advance to line %lu", current_line);
9101               dw2_asm_output_data_sleb128 (line_offset, NULL);
9102               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
9103             }
9104         }
9105       else
9106         /* We still need to start a new row, so output a copy insn.  */
9107         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
9108     }
9109
9110   /* Emit debug info for the address of the end of the function.  */
9111   if (0)
9112     {
9113       dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
9114                            "DW_LNS_fixed_advance_pc");
9115       dw2_asm_output_delta (2, text_end_label, prev_line_label, NULL);
9116     }
9117   else
9118     {
9119       dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9120       dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9121       dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9122       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_end_label, NULL);
9123     }
9124
9125   dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
9126   dw2_asm_output_data_uleb128 (1, NULL);
9127   dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
9128
9129   function = 0;
9130   current_file = 1;
9131   current_line = 1;
9132   for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
9133     {
9134       dw_separate_line_info_ref line_info
9135         = &separate_line_info_table[lt_index];
9136
9137 #if 0
9138       /* Don't emit anything for redundant notes.  */
9139       if (line_info->dw_line_num == current_line
9140           && line_info->dw_file_num == current_file
9141           && line_info->function == function)
9142         goto cont;
9143 #endif
9144
9145       /* Emit debug info for the address of the current line.  If this is
9146          a new function, or the first line of a function, then we need
9147          to handle it differently.  */
9148       ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
9149                                    lt_index);
9150       if (function != line_info->function)
9151         {
9152           function = line_info->function;
9153
9154           /* Set the address register to the first line in the function.  */
9155           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9156           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9157           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9158           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9159         }
9160       else
9161         {
9162           /* ??? See the DW_LNS_advance_pc comment above.  */
9163           if (0)
9164             {
9165               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
9166                                    "DW_LNS_fixed_advance_pc");
9167               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
9168             }
9169           else
9170             {
9171               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9172               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9173               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9174               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9175             }
9176         }
9177
9178       strcpy (prev_line_label, line_label);
9179
9180       /* Emit debug info for the source file of the current line, if
9181          different from the previous line.  */
9182       if (line_info->dw_file_num != current_file)
9183         {
9184           current_file = line_info->dw_file_num;
9185           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
9186           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
9187         }
9188
9189       /* Emit debug info for the current line number, choosing the encoding
9190          that uses the least amount of space.  */
9191       if (line_info->dw_line_num != current_line)
9192         {
9193           line_offset = line_info->dw_line_num - current_line;
9194           line_delta = line_offset - DWARF_LINE_BASE;
9195           current_line = line_info->dw_line_num;
9196           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
9197             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
9198                                  "line %lu", current_line);
9199           else
9200             {
9201               dw2_asm_output_data (1, DW_LNS_advance_line,
9202                                    "advance to line %lu", current_line);
9203               dw2_asm_output_data_sleb128 (line_offset, NULL);
9204               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
9205             }
9206         }
9207       else
9208         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
9209
9210 #if 0
9211     cont:
9212 #endif
9213
9214       lt_index++;
9215
9216       /* If we're done with a function, end its sequence.  */
9217       if (lt_index == separate_line_info_table_in_use
9218           || separate_line_info_table[lt_index].function != function)
9219         {
9220           current_file = 1;
9221           current_line = 1;
9222
9223           /* Emit debug info for the address of the end of the function.  */
9224           ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
9225           if (0)
9226             {
9227               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
9228                                    "DW_LNS_fixed_advance_pc");
9229               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
9230             }
9231           else
9232             {
9233               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9234               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9235               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9236               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9237             }
9238
9239           /* Output the marker for the end of this sequence.  */
9240           dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
9241           dw2_asm_output_data_uleb128 (1, NULL);
9242           dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
9243         }
9244     }
9245
9246   /* Output the marker for the end of the line number info.  */
9247   ASM_OUTPUT_LABEL (asm_out_file, l2);
9248 }
9249 \f
9250 /* Given a pointer to a tree node for some base type, return a pointer to
9251    a DIE that describes the given type.
9252
9253    This routine must only be called for GCC type nodes that correspond to
9254    Dwarf base (fundamental) types.  */
9255
9256 static dw_die_ref
9257 base_type_die (tree type)
9258 {
9259   dw_die_ref base_type_result;
9260   enum dwarf_type encoding;
9261
9262   if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
9263     return 0;
9264
9265   switch (TREE_CODE (type))
9266     {
9267     case INTEGER_TYPE:
9268       if (TYPE_STRING_FLAG (type))
9269         {
9270           if (TYPE_UNSIGNED (type))
9271             encoding = DW_ATE_unsigned_char;
9272           else
9273             encoding = DW_ATE_signed_char;
9274         }
9275       else if (TYPE_UNSIGNED (type))
9276         encoding = DW_ATE_unsigned;
9277       else
9278         encoding = DW_ATE_signed;
9279       break;
9280
9281     case REAL_TYPE:
9282       if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
9283         encoding = DW_ATE_decimal_float;
9284       else
9285         encoding = DW_ATE_float;
9286       break;
9287
9288     case FIXED_POINT_TYPE:
9289       if (TYPE_UNSIGNED (type))
9290         encoding = DW_ATE_unsigned_fixed;
9291       else
9292         encoding = DW_ATE_signed_fixed;
9293       break;
9294
9295       /* Dwarf2 doesn't know anything about complex ints, so use
9296          a user defined type for it.  */
9297     case COMPLEX_TYPE:
9298       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
9299         encoding = DW_ATE_complex_float;
9300       else
9301         encoding = DW_ATE_lo_user;
9302       break;
9303
9304     case BOOLEAN_TYPE:
9305       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
9306       encoding = DW_ATE_boolean;
9307       break;
9308
9309     default:
9310       /* No other TREE_CODEs are Dwarf fundamental types.  */
9311       gcc_unreachable ();
9312     }
9313
9314   base_type_result = new_die (DW_TAG_base_type, comp_unit_die, type);
9315
9316   /* This probably indicates a bug.  */
9317   if (! TYPE_NAME (type))
9318     add_name_attribute (base_type_result, "__unknown__");
9319
9320   add_AT_unsigned (base_type_result, DW_AT_byte_size,
9321                    int_size_in_bytes (type));
9322   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
9323
9324   return base_type_result;
9325 }
9326
9327 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
9328    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
9329
9330 static inline int
9331 is_base_type (tree type)
9332 {
9333   switch (TREE_CODE (type))
9334     {
9335     case ERROR_MARK:
9336     case VOID_TYPE:
9337     case INTEGER_TYPE:
9338     case REAL_TYPE:
9339     case FIXED_POINT_TYPE:
9340     case COMPLEX_TYPE:
9341     case BOOLEAN_TYPE:
9342       return 1;
9343
9344     case ARRAY_TYPE:
9345     case RECORD_TYPE:
9346     case UNION_TYPE:
9347     case QUAL_UNION_TYPE:
9348     case ENUMERAL_TYPE:
9349     case FUNCTION_TYPE:
9350     case METHOD_TYPE:
9351     case POINTER_TYPE:
9352     case REFERENCE_TYPE:
9353     case OFFSET_TYPE:
9354     case LANG_TYPE:
9355     case VECTOR_TYPE:
9356       return 0;
9357
9358     default:
9359       gcc_unreachable ();
9360     }
9361
9362   return 0;
9363 }
9364
9365 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
9366    node, return the size in bits for the type if it is a constant, or else
9367    return the alignment for the type if the type's size is not constant, or
9368    else return BITS_PER_WORD if the type actually turns out to be an
9369    ERROR_MARK node.  */
9370
9371 static inline unsigned HOST_WIDE_INT
9372 simple_type_size_in_bits (const_tree type)
9373 {
9374   if (TREE_CODE (type) == ERROR_MARK)
9375     return BITS_PER_WORD;
9376   else if (TYPE_SIZE (type) == NULL_TREE)
9377     return 0;
9378   else if (host_integerp (TYPE_SIZE (type), 1))
9379     return tree_low_cst (TYPE_SIZE (type), 1);
9380   else
9381     return TYPE_ALIGN (type);
9382 }
9383
9384 /* Return true if the debug information for the given type should be
9385    emitted as a subrange type.  */
9386
9387 static inline bool
9388 is_subrange_type (const_tree type)
9389 {
9390   tree subtype = TREE_TYPE (type);
9391
9392   /* Subrange types are identified by the fact that they are integer
9393      types, and that they have a subtype which is either an integer type
9394      or an enumeral type.  */
9395
9396   if (TREE_CODE (type) != INTEGER_TYPE
9397       || subtype == NULL_TREE)
9398     return false;
9399
9400   if (TREE_CODE (subtype) != INTEGER_TYPE
9401       && TREE_CODE (subtype) != ENUMERAL_TYPE
9402       && TREE_CODE (subtype) != BOOLEAN_TYPE)
9403     return false;
9404
9405   if (TREE_CODE (type) == TREE_CODE (subtype)
9406       && int_size_in_bytes (type) == int_size_in_bytes (subtype)
9407       && TYPE_MIN_VALUE (type) != NULL
9408       && TYPE_MIN_VALUE (subtype) != NULL
9409       && tree_int_cst_equal (TYPE_MIN_VALUE (type), TYPE_MIN_VALUE (subtype))
9410       && TYPE_MAX_VALUE (type) != NULL
9411       && TYPE_MAX_VALUE (subtype) != NULL
9412       && tree_int_cst_equal (TYPE_MAX_VALUE (type), TYPE_MAX_VALUE (subtype)))
9413     {
9414       /* The type and its subtype have the same representation.  If in
9415          addition the two types also have the same name, then the given
9416          type is not a subrange type, but rather a plain base type.  */
9417       /* FIXME: brobecker/2004-03-22:
9418          Sizetype INTEGER_CSTs nodes are canonicalized.  It should
9419          therefore be sufficient to check the TYPE_SIZE node pointers
9420          rather than checking the actual size.  Unfortunately, we have
9421          found some cases, such as in the Ada "integer" type, where
9422          this is not the case.  Until this problem is solved, we need to
9423          keep checking the actual size.  */
9424       tree type_name = TYPE_NAME (type);
9425       tree subtype_name = TYPE_NAME (subtype);
9426
9427       if (type_name != NULL && TREE_CODE (type_name) == TYPE_DECL)
9428         type_name = DECL_NAME (type_name);
9429
9430       if (subtype_name != NULL && TREE_CODE (subtype_name) == TYPE_DECL)
9431         subtype_name = DECL_NAME (subtype_name);
9432
9433       if (type_name == subtype_name)
9434         return false;
9435     }
9436
9437   return true;
9438 }
9439
9440 /*  Given a pointer to a tree node for a subrange type, return a pointer
9441     to a DIE that describes the given type.  */
9442
9443 static dw_die_ref
9444 subrange_type_die (tree type, dw_die_ref context_die)
9445 {
9446   dw_die_ref subrange_die;
9447   const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
9448
9449   if (context_die == NULL)
9450     context_die = comp_unit_die;
9451
9452   subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
9453
9454   if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
9455     {
9456       /* The size of the subrange type and its base type do not match,
9457          so we need to generate a size attribute for the subrange type.  */
9458       add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
9459     }
9460
9461   if (TYPE_MIN_VALUE (type) != NULL)
9462     add_bound_info (subrange_die, DW_AT_lower_bound,
9463                     TYPE_MIN_VALUE (type));
9464   if (TYPE_MAX_VALUE (type) != NULL)
9465     add_bound_info (subrange_die, DW_AT_upper_bound,
9466                     TYPE_MAX_VALUE (type));
9467
9468   return subrange_die;
9469 }
9470
9471 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
9472    entry that chains various modifiers in front of the given type.  */
9473
9474 static dw_die_ref
9475 modified_type_die (tree type, int is_const_type, int is_volatile_type,
9476                    dw_die_ref context_die)
9477 {
9478   enum tree_code code = TREE_CODE (type);
9479   dw_die_ref mod_type_die;
9480   dw_die_ref sub_die = NULL;
9481   tree item_type = NULL;
9482   tree qualified_type;
9483   tree name;
9484
9485   if (code == ERROR_MARK)
9486     return NULL;
9487
9488   /* See if we already have the appropriately qualified variant of
9489      this type.  */
9490   qualified_type
9491     = get_qualified_type (type,
9492                           ((is_const_type ? TYPE_QUAL_CONST : 0)
9493                            | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
9494
9495   /* If we do, then we can just use its DIE, if it exists.  */
9496   if (qualified_type)
9497     {
9498       mod_type_die = lookup_type_die (qualified_type);
9499       if (mod_type_die)
9500         return mod_type_die;
9501     }
9502
9503   name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
9504
9505   /* Handle C typedef types.  */
9506   if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name))
9507     {
9508       tree dtype = TREE_TYPE (name);
9509
9510       if (qualified_type == dtype)
9511         {
9512           /* For a named type, use the typedef.  */
9513           gen_type_die (qualified_type, context_die);
9514           return lookup_type_die (qualified_type);
9515         }
9516       else if (is_const_type < TYPE_READONLY (dtype)
9517                || is_volatile_type < TYPE_VOLATILE (dtype)
9518                || (is_const_type <= TYPE_READONLY (dtype)
9519                    && is_volatile_type <= TYPE_VOLATILE (dtype)
9520                    && DECL_ORIGINAL_TYPE (name) != type))
9521         /* cv-unqualified version of named type.  Just use the unnamed
9522            type to which it refers.  */
9523         return modified_type_die (DECL_ORIGINAL_TYPE (name),
9524                                   is_const_type, is_volatile_type,
9525                                   context_die);
9526       /* Else cv-qualified version of named type; fall through.  */
9527     }
9528
9529   if (is_const_type)
9530     {
9531       mod_type_die = new_die (DW_TAG_const_type, comp_unit_die, type);
9532       sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
9533     }
9534   else if (is_volatile_type)
9535     {
9536       mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die, type);
9537       sub_die = modified_type_die (type, 0, 0, context_die);
9538     }
9539   else if (code == POINTER_TYPE)
9540     {
9541       mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die, type);
9542       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
9543                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
9544       item_type = TREE_TYPE (type);
9545     }
9546   else if (code == REFERENCE_TYPE)
9547     {
9548       mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die, type);
9549       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
9550                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
9551       item_type = TREE_TYPE (type);
9552     }
9553   else if (is_subrange_type (type))
9554     {
9555       mod_type_die = subrange_type_die (type, context_die);
9556       item_type = TREE_TYPE (type);
9557     }
9558   else if (is_base_type (type))
9559     mod_type_die = base_type_die (type);
9560   else
9561     {
9562       gen_type_die (type, context_die);
9563
9564       /* We have to get the type_main_variant here (and pass that to the
9565          `lookup_type_die' routine) because the ..._TYPE node we have
9566          might simply be a *copy* of some original type node (where the
9567          copy was created to help us keep track of typedef names) and
9568          that copy might have a different TYPE_UID from the original
9569          ..._TYPE node.  */
9570       if (TREE_CODE (type) != VECTOR_TYPE)
9571         return lookup_type_die (type_main_variant (type));
9572       else
9573         /* Vectors have the debugging information in the type,
9574            not the main variant.  */
9575         return lookup_type_die (type);
9576     }
9577
9578   /* Builtin types don't have a DECL_ORIGINAL_TYPE.  For those,
9579      don't output a DW_TAG_typedef, since there isn't one in the
9580      user's program; just attach a DW_AT_name to the type.  */
9581   if (name
9582       && (TREE_CODE (name) != TYPE_DECL
9583           || (TREE_TYPE (name) == qualified_type && DECL_NAME (name))))
9584     {
9585       if (TREE_CODE (name) == TYPE_DECL)
9586         /* Could just call add_name_and_src_coords_attributes here,
9587            but since this is a builtin type it doesn't have any
9588            useful source coordinates anyway.  */
9589         name = DECL_NAME (name);
9590       add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
9591     }
9592
9593   if (qualified_type)
9594     equate_type_number_to_die (qualified_type, mod_type_die);
9595
9596   if (item_type)
9597     /* We must do this after the equate_type_number_to_die call, in case
9598        this is a recursive type.  This ensures that the modified_type_die
9599        recursion will terminate even if the type is recursive.  Recursive
9600        types are possible in Ada.  */
9601     sub_die = modified_type_die (item_type,
9602                                  TYPE_READONLY (item_type),
9603                                  TYPE_VOLATILE (item_type),
9604                                  context_die);
9605
9606   if (sub_die != NULL)
9607     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
9608
9609   return mod_type_die;
9610 }
9611
9612 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
9613    an enumerated type.  */
9614
9615 static inline int
9616 type_is_enum (const_tree type)
9617 {
9618   return TREE_CODE (type) == ENUMERAL_TYPE;
9619 }
9620
9621 /* Return the DBX register number described by a given RTL node.  */
9622
9623 static unsigned int
9624 dbx_reg_number (const_rtx rtl)
9625 {
9626   unsigned regno = REGNO (rtl);
9627
9628   gcc_assert (regno < FIRST_PSEUDO_REGISTER);
9629
9630 #ifdef LEAF_REG_REMAP
9631   if (current_function_uses_only_leaf_regs)
9632     {
9633       int leaf_reg = LEAF_REG_REMAP (regno);
9634       if (leaf_reg != -1)
9635         regno = (unsigned) leaf_reg;
9636     }
9637 #endif
9638
9639   return DBX_REGISTER_NUMBER (regno);
9640 }
9641
9642 /* Optionally add a DW_OP_piece term to a location description expression.
9643    DW_OP_piece is only added if the location description expression already
9644    doesn't end with DW_OP_piece.  */
9645
9646 static void
9647 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
9648 {
9649   dw_loc_descr_ref loc;
9650
9651   if (*list_head != NULL)
9652     {
9653       /* Find the end of the chain.  */
9654       for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
9655         ;
9656
9657       if (loc->dw_loc_opc != DW_OP_piece)
9658         loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
9659     }
9660 }
9661
9662 /* Return a location descriptor that designates a machine register or
9663    zero if there is none.  */
9664
9665 static dw_loc_descr_ref
9666 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
9667 {
9668   rtx regs;
9669
9670   if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
9671     return 0;
9672
9673   regs = targetm.dwarf_register_span (rtl);
9674
9675   if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
9676     return multiple_reg_loc_descriptor (rtl, regs, initialized);
9677   else
9678     return one_reg_loc_descriptor (dbx_reg_number (rtl), initialized);
9679 }
9680
9681 /* Return a location descriptor that designates a machine register for
9682    a given hard register number.  */
9683
9684 static dw_loc_descr_ref
9685 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
9686 {
9687   dw_loc_descr_ref reg_loc_descr = new_reg_loc_descr (regno, 0);
9688
9689   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
9690     add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9691
9692   return reg_loc_descr;
9693 }
9694
9695 /* Given an RTL of a register, return a location descriptor that
9696    designates a value that spans more than one register.  */
9697
9698 static dw_loc_descr_ref
9699 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
9700                              enum var_init_status initialized)
9701 {
9702   int nregs, size, i;
9703   unsigned reg;
9704   dw_loc_descr_ref loc_result = NULL;
9705
9706   reg = REGNO (rtl);
9707 #ifdef LEAF_REG_REMAP
9708   if (current_function_uses_only_leaf_regs)
9709     {
9710       int leaf_reg = LEAF_REG_REMAP (reg);
9711       if (leaf_reg != -1)
9712         reg = (unsigned) leaf_reg;
9713     }
9714 #endif
9715   gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
9716   nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
9717
9718   /* Simple, contiguous registers.  */
9719   if (regs == NULL_RTX)
9720     {
9721       size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
9722
9723       loc_result = NULL;
9724       while (nregs--)
9725         {
9726           dw_loc_descr_ref t;
9727
9728           t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
9729                                       VAR_INIT_STATUS_INITIALIZED);
9730           add_loc_descr (&loc_result, t);
9731           add_loc_descr_op_piece (&loc_result, size);
9732           ++reg;
9733         }
9734       return loc_result;
9735     }
9736
9737   /* Now onto stupid register sets in non contiguous locations.  */
9738
9739   gcc_assert (GET_CODE (regs) == PARALLEL);
9740
9741   size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
9742   loc_result = NULL;
9743
9744   for (i = 0; i < XVECLEN (regs, 0); ++i)
9745     {
9746       dw_loc_descr_ref t;
9747
9748       t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
9749                                   VAR_INIT_STATUS_INITIALIZED);
9750       add_loc_descr (&loc_result, t);
9751       size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
9752       add_loc_descr_op_piece (&loc_result, size);
9753     }
9754
9755   if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
9756     add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9757   return loc_result;
9758 }
9759
9760 #endif /* DWARF2_DEBUGGING_INFO */
9761
9762 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
9763
9764 /* Return a location descriptor that designates a constant.  */
9765
9766 static dw_loc_descr_ref
9767 int_loc_descriptor (HOST_WIDE_INT i)
9768 {
9769   enum dwarf_location_atom op;
9770
9771   /* Pick the smallest representation of a constant, rather than just
9772      defaulting to the LEB encoding.  */
9773   if (i >= 0)
9774     {
9775       if (i <= 31)
9776         op = DW_OP_lit0 + i;
9777       else if (i <= 0xff)
9778         op = DW_OP_const1u;
9779       else if (i <= 0xffff)
9780         op = DW_OP_const2u;
9781       else if (HOST_BITS_PER_WIDE_INT == 32
9782                || i <= 0xffffffff)
9783         op = DW_OP_const4u;
9784       else
9785         op = DW_OP_constu;
9786     }
9787   else
9788     {
9789       if (i >= -0x80)
9790         op = DW_OP_const1s;
9791       else if (i >= -0x8000)
9792         op = DW_OP_const2s;
9793       else if (HOST_BITS_PER_WIDE_INT == 32
9794                || i >= -0x80000000)
9795         op = DW_OP_const4s;
9796       else
9797         op = DW_OP_consts;
9798     }
9799
9800   return new_loc_descr (op, i, 0);
9801 }
9802 #endif
9803
9804 #ifdef DWARF2_DEBUGGING_INFO
9805
9806 /* Return a location descriptor that designates a base+offset location.  */
9807
9808 static dw_loc_descr_ref
9809 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
9810                  enum var_init_status initialized)
9811 {
9812   unsigned int regno;
9813   dw_loc_descr_ref result;
9814   dw_fde_ref fde = current_fde ();
9815
9816   /* We only use "frame base" when we're sure we're talking about the
9817      post-prologue local stack frame.  We do this by *not* running
9818      register elimination until this point, and recognizing the special
9819      argument pointer and soft frame pointer rtx's.  */
9820   if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
9821     {
9822       rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
9823
9824       if (elim != reg)
9825         {
9826           if (GET_CODE (elim) == PLUS)
9827             {
9828               offset += INTVAL (XEXP (elim, 1));
9829               elim = XEXP (elim, 0);
9830             }
9831           gcc_assert ((SUPPORTS_STACK_ALIGNMENT
9832                        && (elim == hard_frame_pointer_rtx
9833                            || elim == stack_pointer_rtx))
9834                       || elim == (frame_pointer_needed
9835                                   ? hard_frame_pointer_rtx
9836                                   : stack_pointer_rtx));
9837
9838           /* If drap register is used to align stack, use frame
9839              pointer + offset to access stack variables.  If stack
9840              is aligned without drap, use stack pointer + offset to
9841              access stack variables.  */
9842           if (crtl->stack_realign_tried
9843               && cfa.reg == HARD_FRAME_POINTER_REGNUM
9844               && reg == frame_pointer_rtx)
9845             {
9846               int base_reg
9847                 = DWARF_FRAME_REGNUM (cfa.indirect
9848                                       ? HARD_FRAME_POINTER_REGNUM
9849                                       : STACK_POINTER_REGNUM);
9850               return new_reg_loc_descr (base_reg, offset);
9851             }
9852
9853           offset += frame_pointer_fb_offset;
9854           return new_loc_descr (DW_OP_fbreg, offset, 0);
9855         }
9856     }
9857   else if (fde
9858            && fde->drap_reg != INVALID_REGNUM
9859            && (fde->drap_reg == REGNO (reg)
9860                || fde->vdrap_reg == REGNO (reg)))
9861     {
9862       /* Use cfa+offset to represent the location of arguments passed
9863          on stack when drap is used to align stack.  */
9864       return new_loc_descr (DW_OP_fbreg, offset, 0);
9865     }
9866
9867   regno = dbx_reg_number (reg);
9868   if (regno <= 31)
9869     result = new_loc_descr (DW_OP_breg0 + regno, offset, 0);
9870   else
9871     result = new_loc_descr (DW_OP_bregx, regno, offset);
9872
9873   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
9874     add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9875
9876   return result;
9877 }
9878
9879 /* Return true if this RTL expression describes a base+offset calculation.  */
9880
9881 static inline int
9882 is_based_loc (const_rtx rtl)
9883 {
9884   return (GET_CODE (rtl) == PLUS
9885           && ((REG_P (XEXP (rtl, 0))
9886                && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
9887                && GET_CODE (XEXP (rtl, 1)) == CONST_INT)));
9888 }
9889
9890 /* Return a descriptor that describes the concatenation of N locations
9891    used to form the address of a memory location.  */
9892
9893 static dw_loc_descr_ref
9894 concatn_mem_loc_descriptor (rtx concatn, enum machine_mode mode,
9895                             enum var_init_status initialized)
9896 {
9897   unsigned int i;
9898   dw_loc_descr_ref cc_loc_result = NULL;
9899   unsigned int n = XVECLEN (concatn, 0);
9900
9901   for (i = 0; i < n; ++i)
9902     {
9903       dw_loc_descr_ref ref;
9904       rtx x = XVECEXP (concatn, 0, i);
9905
9906       ref = mem_loc_descriptor (x, mode, VAR_INIT_STATUS_INITIALIZED);
9907       if (ref == NULL)
9908         return NULL;
9909
9910       add_loc_descr (&cc_loc_result, ref);
9911       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
9912     }
9913
9914   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
9915     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9916
9917   return cc_loc_result;
9918 }
9919
9920 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
9921    failed.  */
9922
9923 static dw_loc_descr_ref
9924 tls_mem_loc_descriptor (rtx mem)
9925 {
9926   tree base;
9927   dw_loc_descr_ref loc_result, loc_result2;
9928
9929   if (MEM_EXPR (mem) == NULL_TREE || MEM_OFFSET (mem) == NULL_RTX)
9930     return NULL;
9931
9932   base = get_base_address (MEM_EXPR (mem));
9933   if (base == NULL
9934       || TREE_CODE (base) != VAR_DECL
9935       || !DECL_THREAD_LOCAL_P (base))
9936     return NULL;
9937
9938   loc_result = loc_descriptor_from_tree_1 (MEM_EXPR (mem), 2);
9939   if (loc_result == NULL)
9940     return NULL;
9941
9942   if (INTVAL (MEM_OFFSET (mem)))
9943     {
9944       if (INTVAL (MEM_OFFSET (mem)) >= 0)
9945         add_loc_descr (&loc_result,
9946                        new_loc_descr (DW_OP_plus_uconst,
9947                                       INTVAL (MEM_OFFSET (mem)), 0));
9948       else
9949         {
9950           loc_result2 = mem_loc_descriptor (MEM_OFFSET (mem), GET_MODE (mem),
9951                                             VAR_INIT_STATUS_INITIALIZED);
9952           if (loc_result2 == 0)
9953             return NULL;
9954           add_loc_descr (&loc_result, loc_result2);
9955           add_loc_descr (&loc_result, new_loc_descr (DW_OP_plus, 0, 0));
9956         }
9957     }
9958
9959   return loc_result;
9960 }
9961
9962 /* The following routine converts the RTL for a variable or parameter
9963    (resident in memory) into an equivalent Dwarf representation of a
9964    mechanism for getting the address of that same variable onto the top of a
9965    hypothetical "address evaluation" stack.
9966
9967    When creating memory location descriptors, we are effectively transforming
9968    the RTL for a memory-resident object into its Dwarf postfix expression
9969    equivalent.  This routine recursively descends an RTL tree, turning
9970    it into Dwarf postfix code as it goes.
9971
9972    MODE is the mode of the memory reference, needed to handle some
9973    autoincrement addressing modes.
9974
9975    CAN_USE_FBREG is a flag whether we can use DW_AT_frame_base in the
9976    location list for RTL.
9977
9978    Return 0 if we can't represent the location.  */
9979
9980 static dw_loc_descr_ref
9981 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
9982                     enum var_init_status initialized)
9983 {
9984   dw_loc_descr_ref mem_loc_result = NULL;
9985   enum dwarf_location_atom op;
9986
9987   /* Note that for a dynamically sized array, the location we will generate a
9988      description of here will be the lowest numbered location which is
9989      actually within the array.  That's *not* necessarily the same as the
9990      zeroth element of the array.  */
9991
9992   rtl = targetm.delegitimize_address (rtl);
9993
9994   switch (GET_CODE (rtl))
9995     {
9996     case POST_INC:
9997     case POST_DEC:
9998     case POST_MODIFY:
9999       /* POST_INC and POST_DEC can be handled just like a SUBREG.  So we
10000          just fall into the SUBREG code.  */
10001
10002       /* ... fall through ...  */
10003
10004     case SUBREG:
10005       /* The case of a subreg may arise when we have a local (register)
10006          variable or a formal (register) parameter which doesn't quite fill
10007          up an entire register.  For now, just assume that it is
10008          legitimate to make the Dwarf info refer to the whole register which
10009          contains the given subreg.  */
10010       rtl = XEXP (rtl, 0);
10011
10012       /* ... fall through ...  */
10013
10014     case REG:
10015       /* Whenever a register number forms a part of the description of the
10016          method for calculating the (dynamic) address of a memory resident
10017          object, DWARF rules require the register number be referred to as
10018          a "base register".  This distinction is not based in any way upon
10019          what category of register the hardware believes the given register
10020          belongs to.  This is strictly DWARF terminology we're dealing with
10021          here. Note that in cases where the location of a memory-resident
10022          data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
10023          OP_CONST (0)) the actual DWARF location descriptor that we generate
10024          may just be OP_BASEREG (basereg).  This may look deceptively like
10025          the object in question was allocated to a register (rather than in
10026          memory) so DWARF consumers need to be aware of the subtle
10027          distinction between OP_REG and OP_BASEREG.  */
10028       if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
10029         mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
10030       else if (stack_realign_drap
10031                && crtl->drap_reg
10032                && crtl->args.internal_arg_pointer == rtl
10033                && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
10034         {
10035           /* If RTL is internal_arg_pointer, which has been optimized
10036              out, use DRAP instead.  */
10037           mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
10038                                             VAR_INIT_STATUS_INITIALIZED);
10039         }
10040       break;
10041
10042     case MEM:
10043       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
10044                                            VAR_INIT_STATUS_INITIALIZED);
10045       if (mem_loc_result == NULL)
10046         mem_loc_result = tls_mem_loc_descriptor (rtl);
10047       if (mem_loc_result != 0)
10048         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
10049       break;
10050
10051     case LO_SUM:
10052          rtl = XEXP (rtl, 1);
10053
10054       /* ... fall through ...  */
10055
10056     case LABEL_REF:
10057       /* Some ports can transform a symbol ref into a label ref, because
10058          the symbol ref is too far away and has to be dumped into a constant
10059          pool.  */
10060     case CONST:
10061     case SYMBOL_REF:
10062       /* Alternatively, the symbol in the constant pool might be referenced
10063          by a different symbol.  */
10064       if (GET_CODE (rtl) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (rtl))
10065         {
10066           bool marked;
10067           rtx tmp = get_pool_constant_mark (rtl, &marked);
10068
10069           if (GET_CODE (tmp) == SYMBOL_REF)
10070             {
10071               rtl = tmp;
10072               if (CONSTANT_POOL_ADDRESS_P (tmp))
10073                 get_pool_constant_mark (tmp, &marked);
10074               else
10075                 marked = true;
10076             }
10077
10078           /* If all references to this pool constant were optimized away,
10079              it was not output and thus we can't represent it.
10080              FIXME: might try to use DW_OP_const_value here, though
10081              DW_OP_piece complicates it.  */
10082           if (!marked)
10083             return 0;
10084         }
10085
10086       mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
10087       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
10088       mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
10089       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
10090       break;
10091
10092     case PRE_MODIFY:
10093       /* Extract the PLUS expression nested inside and fall into
10094          PLUS code below.  */
10095       rtl = XEXP (rtl, 1);
10096       goto plus;
10097
10098     case PRE_INC:
10099     case PRE_DEC:
10100       /* Turn these into a PLUS expression and fall into the PLUS code
10101          below.  */
10102       rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
10103                           GEN_INT (GET_CODE (rtl) == PRE_INC
10104                                    ? GET_MODE_UNIT_SIZE (mode)
10105                                    : -GET_MODE_UNIT_SIZE (mode)));
10106
10107       /* ... fall through ...  */
10108
10109     case PLUS:
10110     plus:
10111       if (is_based_loc (rtl))
10112         mem_loc_result = based_loc_descr (XEXP (rtl, 0),
10113                                           INTVAL (XEXP (rtl, 1)),
10114                                           VAR_INIT_STATUS_INITIALIZED);
10115       else
10116         {
10117           mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode,
10118                                                VAR_INIT_STATUS_INITIALIZED);
10119           if (mem_loc_result == 0)
10120             break;
10121
10122           if (GET_CODE (XEXP (rtl, 1)) == CONST_INT
10123               && INTVAL (XEXP (rtl, 1)) >= 0)
10124             add_loc_descr (&mem_loc_result,
10125                            new_loc_descr (DW_OP_plus_uconst,
10126                                           INTVAL (XEXP (rtl, 1)), 0));
10127           else
10128             {
10129               dw_loc_descr_ref mem_loc_result2
10130                 = mem_loc_descriptor (XEXP (rtl, 1), mode,
10131                                       VAR_INIT_STATUS_INITIALIZED);
10132               if (mem_loc_result2 == 0)
10133                 break;
10134               add_loc_descr (&mem_loc_result, mem_loc_result2);
10135               add_loc_descr (&mem_loc_result,
10136                              new_loc_descr (DW_OP_plus, 0, 0));
10137             }
10138         }
10139       break;
10140
10141     /* If a pseudo-reg is optimized away, it is possible for it to
10142        be replaced with a MEM containing a multiply or shift.  */
10143     case MULT:
10144       op = DW_OP_mul;
10145       goto do_binop;
10146
10147     case ASHIFT:
10148       op = DW_OP_shl;
10149       goto do_binop;
10150
10151     case ASHIFTRT:
10152       op = DW_OP_shra;
10153       goto do_binop;
10154
10155     case LSHIFTRT:
10156       op = DW_OP_shr;
10157       goto do_binop;
10158
10159     do_binop:
10160       {
10161         dw_loc_descr_ref op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
10162                                                    VAR_INIT_STATUS_INITIALIZED);
10163         dw_loc_descr_ref op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
10164                                                    VAR_INIT_STATUS_INITIALIZED);
10165
10166         if (op0 == 0 || op1 == 0)
10167           break;
10168
10169         mem_loc_result = op0;
10170         add_loc_descr (&mem_loc_result, op1);
10171         add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
10172         break;
10173       }
10174
10175     case CONST_INT:
10176       mem_loc_result = int_loc_descriptor (INTVAL (rtl));
10177       break;
10178
10179     case CONCATN:
10180       mem_loc_result = concatn_mem_loc_descriptor (rtl, mode,
10181                                                    VAR_INIT_STATUS_INITIALIZED);
10182       break;
10183
10184     case UNSPEC:
10185       /* If delegitimize_address couldn't do anything with the UNSPEC, we
10186          can't express it in the debug info.  This can happen e.g. with some
10187          TLS UNSPECs.  */
10188       break;
10189
10190     default:
10191       gcc_unreachable ();
10192     }
10193
10194   if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10195     add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10196
10197   return mem_loc_result;
10198 }
10199
10200 /* Return a descriptor that describes the concatenation of two locations.
10201    This is typically a complex variable.  */
10202
10203 static dw_loc_descr_ref
10204 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
10205 {
10206   dw_loc_descr_ref cc_loc_result = NULL;
10207   dw_loc_descr_ref x0_ref = loc_descriptor (x0, VAR_INIT_STATUS_INITIALIZED);
10208   dw_loc_descr_ref x1_ref = loc_descriptor (x1, VAR_INIT_STATUS_INITIALIZED);
10209
10210   if (x0_ref == 0 || x1_ref == 0)
10211     return 0;
10212
10213   cc_loc_result = x0_ref;
10214   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
10215
10216   add_loc_descr (&cc_loc_result, x1_ref);
10217   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
10218
10219   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10220     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10221
10222   return cc_loc_result;
10223 }
10224
10225 /* Return a descriptor that describes the concatenation of N
10226    locations.  */
10227
10228 static dw_loc_descr_ref
10229 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
10230 {
10231   unsigned int i;
10232   dw_loc_descr_ref cc_loc_result = NULL;
10233   unsigned int n = XVECLEN (concatn, 0);
10234
10235   for (i = 0; i < n; ++i)
10236     {
10237       dw_loc_descr_ref ref;
10238       rtx x = XVECEXP (concatn, 0, i);
10239
10240       ref = loc_descriptor (x, VAR_INIT_STATUS_INITIALIZED);
10241       if (ref == NULL)
10242         return NULL;
10243
10244       add_loc_descr (&cc_loc_result, ref);
10245       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
10246     }
10247
10248   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10249     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10250
10251   return cc_loc_result;
10252 }
10253
10254 /* Output a proper Dwarf location descriptor for a variable or parameter
10255    which is either allocated in a register or in a memory location.  For a
10256    register, we just generate an OP_REG and the register number.  For a
10257    memory location we provide a Dwarf postfix expression describing how to
10258    generate the (dynamic) address of the object onto the address stack.
10259
10260    If we don't know how to describe it, return 0.  */
10261
10262 static dw_loc_descr_ref
10263 loc_descriptor (rtx rtl, enum var_init_status initialized)
10264 {
10265   dw_loc_descr_ref loc_result = NULL;
10266
10267   switch (GET_CODE (rtl))
10268     {
10269     case SUBREG:
10270       /* The case of a subreg may arise when we have a local (register)
10271          variable or a formal (register) parameter which doesn't quite fill
10272          up an entire register.  For now, just assume that it is
10273          legitimate to make the Dwarf info refer to the whole register which
10274          contains the given subreg.  */
10275       rtl = SUBREG_REG (rtl);
10276
10277       /* ... fall through ...  */
10278
10279     case REG:
10280       loc_result = reg_loc_descriptor (rtl, initialized);
10281       break;
10282
10283     case MEM:
10284       loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
10285                                        initialized);
10286       if (loc_result == NULL)
10287         loc_result = tls_mem_loc_descriptor (rtl);
10288       break;
10289
10290     case CONCAT:
10291       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
10292                                           initialized);
10293       break;
10294
10295     case CONCATN:
10296       loc_result = concatn_loc_descriptor (rtl, initialized);
10297       break;
10298
10299     case VAR_LOCATION:
10300       /* Single part.  */
10301       if (GET_CODE (XEXP (rtl, 1)) != PARALLEL)
10302         {
10303           loc_result = loc_descriptor (XEXP (XEXP (rtl, 1), 0), initialized);
10304           break;
10305         }
10306
10307       rtl = XEXP (rtl, 1);
10308       /* FALLTHRU */
10309
10310     case PARALLEL:
10311       {
10312         rtvec par_elems = XVEC (rtl, 0);
10313         int num_elem = GET_NUM_ELEM (par_elems);
10314         enum machine_mode mode;
10315         int i;
10316
10317         /* Create the first one, so we have something to add to.  */
10318         loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
10319                                      initialized);
10320         if (loc_result == NULL)
10321           return NULL;
10322         mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
10323         add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
10324         for (i = 1; i < num_elem; i++)
10325           {
10326             dw_loc_descr_ref temp;
10327
10328             temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
10329                                    initialized);
10330             if (temp == NULL)
10331               return NULL;
10332             add_loc_descr (&loc_result, temp);
10333             mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
10334             add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
10335           }
10336       }
10337       break;
10338
10339     default:
10340       gcc_unreachable ();
10341     }
10342
10343   return loc_result;
10344 }
10345
10346 /* Similar, but generate the descriptor from trees instead of rtl.  This comes
10347    up particularly with variable length arrays.  WANT_ADDRESS is 2 if this is
10348    a top-level invocation of loc_descriptor_from_tree; is 1 if this is not a
10349    top-level invocation, and we require the address of LOC; is 0 if we require
10350    the value of LOC.  */
10351
10352 static dw_loc_descr_ref
10353 loc_descriptor_from_tree_1 (tree loc, int want_address)
10354 {
10355   dw_loc_descr_ref ret, ret1;
10356   int have_address = 0;
10357   enum dwarf_location_atom op;
10358
10359   /* ??? Most of the time we do not take proper care for sign/zero
10360      extending the values properly.  Hopefully this won't be a real
10361      problem...  */
10362
10363   switch (TREE_CODE (loc))
10364     {
10365     case ERROR_MARK:
10366       return 0;
10367
10368     case PLACEHOLDER_EXPR:
10369       /* This case involves extracting fields from an object to determine the
10370          position of other fields.  We don't try to encode this here.  The
10371          only user of this is Ada, which encodes the needed information using
10372          the names of types.  */
10373       return 0;
10374
10375     case CALL_EXPR:
10376       return 0;
10377
10378     case PREINCREMENT_EXPR:
10379     case PREDECREMENT_EXPR:
10380     case POSTINCREMENT_EXPR:
10381     case POSTDECREMENT_EXPR:
10382       /* There are no opcodes for these operations.  */
10383       return 0;
10384
10385     case ADDR_EXPR:
10386       /* If we already want an address, there's nothing we can do.  */
10387       if (want_address)
10388         return 0;
10389
10390       /* Otherwise, process the argument and look for the address.  */
10391       return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 1);
10392
10393     case VAR_DECL:
10394       if (DECL_THREAD_LOCAL_P (loc))
10395         {
10396           rtx rtl;
10397           unsigned first_op;
10398           unsigned second_op;
10399
10400           if (targetm.have_tls)
10401             {
10402               /* If this is not defined, we have no way to emit the
10403                  data.  */
10404               if (!targetm.asm_out.output_dwarf_dtprel)
10405                 return 0;
10406
10407                /* The way DW_OP_GNU_push_tls_address is specified, we
10408                   can only look up addresses of objects in the current
10409                   module.  */
10410               if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
10411                 return 0;
10412               first_op = INTERNAL_DW_OP_tls_addr;
10413               second_op = DW_OP_GNU_push_tls_address;
10414             }
10415           else
10416             {
10417               if (!targetm.emutls.debug_form_tls_address)
10418                 return 0;
10419               loc = emutls_decl (loc);
10420               first_op = DW_OP_addr;
10421               second_op = DW_OP_form_tls_address;
10422             }
10423
10424           rtl = rtl_for_decl_location (loc);
10425           if (rtl == NULL_RTX)
10426             return 0;
10427
10428           if (!MEM_P (rtl))
10429             return 0;
10430           rtl = XEXP (rtl, 0);
10431           if (! CONSTANT_P (rtl))
10432             return 0;
10433
10434           ret = new_loc_descr (first_op, 0, 0);
10435           ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
10436           ret->dw_loc_oprnd1.v.val_addr = rtl;
10437
10438           ret1 = new_loc_descr (second_op, 0, 0);
10439           add_loc_descr (&ret, ret1);
10440
10441           have_address = 1;
10442           break;
10443         }
10444       /* FALLTHRU */
10445
10446     case PARM_DECL:
10447       if (DECL_HAS_VALUE_EXPR_P (loc))
10448         return loc_descriptor_from_tree_1 (DECL_VALUE_EXPR (loc),
10449                                            want_address);
10450       /* FALLTHRU */
10451
10452     case RESULT_DECL:
10453     case FUNCTION_DECL:
10454       {
10455         rtx rtl = rtl_for_decl_location (loc);
10456
10457         if (rtl == NULL_RTX)
10458           return 0;
10459         else if (GET_CODE (rtl) == CONST_INT)
10460           {
10461             HOST_WIDE_INT val = INTVAL (rtl);
10462             if (TYPE_UNSIGNED (TREE_TYPE (loc)))
10463               val &= GET_MODE_MASK (DECL_MODE (loc));
10464             ret = int_loc_descriptor (val);
10465           }
10466         else if (GET_CODE (rtl) == CONST_STRING)
10467           return 0;
10468         else if (CONSTANT_P (rtl))
10469           {
10470             ret = new_loc_descr (DW_OP_addr, 0, 0);
10471             ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
10472             ret->dw_loc_oprnd1.v.val_addr = rtl;
10473           }
10474         else
10475           {
10476             enum machine_mode mode;
10477
10478             /* Certain constructs can only be represented at top-level.  */
10479             if (want_address == 2)
10480               return loc_descriptor (rtl, VAR_INIT_STATUS_INITIALIZED);
10481
10482             mode = GET_MODE (rtl);
10483             if (MEM_P (rtl))
10484               {
10485                 rtl = XEXP (rtl, 0);
10486                 have_address = 1;
10487               }
10488             ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
10489           }
10490       }
10491       break;
10492
10493     case INDIRECT_REF:
10494       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
10495       have_address = 1;
10496       break;
10497
10498     case COMPOUND_EXPR:
10499       return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), want_address);
10500
10501     CASE_CONVERT:
10502     case VIEW_CONVERT_EXPR:
10503     case SAVE_EXPR:
10504     case MODIFY_EXPR:
10505       return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), want_address);
10506
10507     case COMPONENT_REF:
10508     case BIT_FIELD_REF:
10509     case ARRAY_REF:
10510     case ARRAY_RANGE_REF:
10511       {
10512         tree obj, offset;
10513         HOST_WIDE_INT bitsize, bitpos, bytepos;
10514         enum machine_mode mode;
10515         int volatilep;
10516         int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
10517
10518         obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
10519                                    &unsignedp, &volatilep, false);
10520
10521         if (obj == loc)
10522           return 0;
10523
10524         ret = loc_descriptor_from_tree_1 (obj, 1);
10525         if (ret == 0
10526             || bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
10527           return 0;
10528
10529         if (offset != NULL_TREE)
10530           {
10531             /* Variable offset.  */
10532             ret1 = loc_descriptor_from_tree_1 (offset, 0);
10533             if (ret1 == 0)
10534               return 0;
10535             add_loc_descr (&ret, ret1);
10536             add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
10537           }
10538
10539         bytepos = bitpos / BITS_PER_UNIT;
10540         if (bytepos > 0)
10541           add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
10542         else if (bytepos < 0)
10543           {
10544             add_loc_descr (&ret, int_loc_descriptor (bytepos));
10545             add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
10546           }
10547
10548         have_address = 1;
10549         break;
10550       }
10551
10552     case INTEGER_CST:
10553       if (host_integerp (loc, 0))
10554         ret = int_loc_descriptor (tree_low_cst (loc, 0));
10555       else
10556         return 0;
10557       break;
10558
10559     case CONSTRUCTOR:
10560       {
10561         /* Get an RTL for this, if something has been emitted.  */
10562         rtx rtl = lookup_constant_def (loc);
10563         enum machine_mode mode;
10564
10565         if (!rtl || !MEM_P (rtl))
10566           return 0;
10567         mode = GET_MODE (rtl);
10568         rtl = XEXP (rtl, 0);
10569         ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
10570         have_address = 1;
10571         break;
10572       }
10573
10574     case TRUTH_AND_EXPR:
10575     case TRUTH_ANDIF_EXPR:
10576     case BIT_AND_EXPR:
10577       op = DW_OP_and;
10578       goto do_binop;
10579
10580     case TRUTH_XOR_EXPR:
10581     case BIT_XOR_EXPR:
10582       op = DW_OP_xor;
10583       goto do_binop;
10584
10585     case TRUTH_OR_EXPR:
10586     case TRUTH_ORIF_EXPR:
10587     case BIT_IOR_EXPR:
10588       op = DW_OP_or;
10589       goto do_binop;
10590
10591     case FLOOR_DIV_EXPR:
10592     case CEIL_DIV_EXPR:
10593     case ROUND_DIV_EXPR:
10594     case TRUNC_DIV_EXPR:
10595       op = DW_OP_div;
10596       goto do_binop;
10597
10598     case MINUS_EXPR:
10599       op = DW_OP_minus;
10600       goto do_binop;
10601
10602     case FLOOR_MOD_EXPR:
10603     case CEIL_MOD_EXPR:
10604     case ROUND_MOD_EXPR:
10605     case TRUNC_MOD_EXPR:
10606       op = DW_OP_mod;
10607       goto do_binop;
10608
10609     case MULT_EXPR:
10610       op = DW_OP_mul;
10611       goto do_binop;
10612
10613     case LSHIFT_EXPR:
10614       op = DW_OP_shl;
10615       goto do_binop;
10616
10617     case RSHIFT_EXPR:
10618       op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
10619       goto do_binop;
10620
10621     case POINTER_PLUS_EXPR:
10622     case PLUS_EXPR:
10623       if (TREE_CODE (TREE_OPERAND (loc, 1)) == INTEGER_CST
10624           && host_integerp (TREE_OPERAND (loc, 1), 0))
10625         {
10626           ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
10627           if (ret == 0)
10628             return 0;
10629
10630           add_loc_descr (&ret,
10631                          new_loc_descr (DW_OP_plus_uconst,
10632                                         tree_low_cst (TREE_OPERAND (loc, 1),
10633                                                       0),
10634                                         0));
10635           break;
10636         }
10637
10638       op = DW_OP_plus;
10639       goto do_binop;
10640
10641     case LE_EXPR:
10642       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
10643         return 0;
10644
10645       op = DW_OP_le;
10646       goto do_binop;
10647
10648     case GE_EXPR:
10649       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
10650         return 0;
10651
10652       op = DW_OP_ge;
10653       goto do_binop;
10654
10655     case LT_EXPR:
10656       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
10657         return 0;
10658
10659       op = DW_OP_lt;
10660       goto do_binop;
10661
10662     case GT_EXPR:
10663       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
10664         return 0;
10665
10666       op = DW_OP_gt;
10667       goto do_binop;
10668
10669     case EQ_EXPR:
10670       op = DW_OP_eq;
10671       goto do_binop;
10672
10673     case NE_EXPR:
10674       op = DW_OP_ne;
10675       goto do_binop;
10676
10677     do_binop:
10678       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
10679       ret1 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
10680       if (ret == 0 || ret1 == 0)
10681         return 0;
10682
10683       add_loc_descr (&ret, ret1);
10684       add_loc_descr (&ret, new_loc_descr (op, 0, 0));
10685       break;
10686
10687     case TRUTH_NOT_EXPR:
10688     case BIT_NOT_EXPR:
10689       op = DW_OP_not;
10690       goto do_unop;
10691
10692     case ABS_EXPR:
10693       op = DW_OP_abs;
10694       goto do_unop;
10695
10696     case NEGATE_EXPR:
10697       op = DW_OP_neg;
10698       goto do_unop;
10699
10700     do_unop:
10701       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
10702       if (ret == 0)
10703         return 0;
10704
10705       add_loc_descr (&ret, new_loc_descr (op, 0, 0));
10706       break;
10707
10708     case MIN_EXPR:
10709     case MAX_EXPR:
10710       {
10711         const enum tree_code code =
10712           TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
10713
10714         loc = build3 (COND_EXPR, TREE_TYPE (loc),
10715                       build2 (code, integer_type_node,
10716                               TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
10717                       TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
10718       }
10719
10720       /* ... fall through ...  */
10721
10722     case COND_EXPR:
10723       {
10724         dw_loc_descr_ref lhs
10725           = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
10726         dw_loc_descr_ref rhs
10727           = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 2), 0);
10728         dw_loc_descr_ref bra_node, jump_node, tmp;
10729
10730         ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
10731         if (ret == 0 || lhs == 0 || rhs == 0)
10732           return 0;
10733
10734         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
10735         add_loc_descr (&ret, bra_node);
10736
10737         add_loc_descr (&ret, rhs);
10738         jump_node = new_loc_descr (DW_OP_skip, 0, 0);
10739         add_loc_descr (&ret, jump_node);
10740
10741         add_loc_descr (&ret, lhs);
10742         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
10743         bra_node->dw_loc_oprnd1.v.val_loc = lhs;
10744
10745         /* ??? Need a node to point the skip at.  Use a nop.  */
10746         tmp = new_loc_descr (DW_OP_nop, 0, 0);
10747         add_loc_descr (&ret, tmp);
10748         jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
10749         jump_node->dw_loc_oprnd1.v.val_loc = tmp;
10750       }
10751       break;
10752
10753     case FIX_TRUNC_EXPR:
10754       return 0;
10755
10756     default:
10757       /* Leave front-end specific codes as simply unknown.  This comes
10758          up, for instance, with the C STMT_EXPR.  */
10759       if ((unsigned int) TREE_CODE (loc)
10760           >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
10761         return 0;
10762
10763 #ifdef ENABLE_CHECKING
10764       /* Otherwise this is a generic code; we should just lists all of
10765          these explicitly.  We forgot one.  */
10766       gcc_unreachable ();
10767 #else
10768       /* In a release build, we want to degrade gracefully: better to
10769          generate incomplete debugging information than to crash.  */
10770       return NULL;
10771 #endif
10772     }
10773
10774   /* Show if we can't fill the request for an address.  */
10775   if (want_address && !have_address)
10776     return 0;
10777
10778   /* If we've got an address and don't want one, dereference.  */
10779   if (!want_address && have_address && ret)
10780     {
10781       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
10782
10783       if (size > DWARF2_ADDR_SIZE || size == -1)
10784         return 0;
10785       else if (size == DWARF2_ADDR_SIZE)
10786         op = DW_OP_deref;
10787       else
10788         op = DW_OP_deref_size;
10789
10790       add_loc_descr (&ret, new_loc_descr (op, size, 0));
10791     }
10792
10793   return ret;
10794 }
10795
10796 static inline dw_loc_descr_ref
10797 loc_descriptor_from_tree (tree loc)
10798 {
10799   return loc_descriptor_from_tree_1 (loc, 2);
10800 }
10801
10802 /* Given a value, round it up to the lowest multiple of `boundary'
10803    which is not less than the value itself.  */
10804
10805 static inline HOST_WIDE_INT
10806 ceiling (HOST_WIDE_INT value, unsigned int boundary)
10807 {
10808   return (((value + boundary - 1) / boundary) * boundary);
10809 }
10810
10811 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
10812    pointer to the declared type for the relevant field variable, or return
10813    `integer_type_node' if the given node turns out to be an
10814    ERROR_MARK node.  */
10815
10816 static inline tree
10817 field_type (const_tree decl)
10818 {
10819   tree type;
10820
10821   if (TREE_CODE (decl) == ERROR_MARK)
10822     return integer_type_node;
10823
10824   type = DECL_BIT_FIELD_TYPE (decl);
10825   if (type == NULL_TREE)
10826     type = TREE_TYPE (decl);
10827
10828   return type;
10829 }
10830
10831 /* Given a pointer to a tree node, return the alignment in bits for
10832    it, or else return BITS_PER_WORD if the node actually turns out to
10833    be an ERROR_MARK node.  */
10834
10835 static inline unsigned
10836 simple_type_align_in_bits (const_tree type)
10837 {
10838   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
10839 }
10840
10841 static inline unsigned
10842 simple_decl_align_in_bits (const_tree decl)
10843 {
10844   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
10845 }
10846
10847 /* Return the result of rounding T up to ALIGN.  */
10848
10849 static inline HOST_WIDE_INT
10850 round_up_to_align (HOST_WIDE_INT t, unsigned int align)
10851 {
10852   /* We must be careful if T is negative because HOST_WIDE_INT can be
10853      either "above" or "below" unsigned int as per the C promotion
10854      rules, depending on the host, thus making the signedness of the
10855      direct multiplication and division unpredictable.  */
10856   unsigned HOST_WIDE_INT u = (unsigned HOST_WIDE_INT) t;
10857
10858   u += align - 1;
10859   u /= align;
10860   u *= align;
10861
10862   return (HOST_WIDE_INT) u;
10863 }
10864
10865 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
10866    lowest addressed byte of the "containing object" for the given FIELD_DECL,
10867    or return 0 if we are unable to determine what that offset is, either
10868    because the argument turns out to be a pointer to an ERROR_MARK node, or
10869    because the offset is actually variable.  (We can't handle the latter case
10870    just yet).  */
10871
10872 static HOST_WIDE_INT
10873 field_byte_offset (const_tree decl)
10874 {
10875   HOST_WIDE_INT object_offset_in_bits;
10876   HOST_WIDE_INT bitpos_int;
10877
10878   if (TREE_CODE (decl) == ERROR_MARK)
10879     return 0;
10880
10881   gcc_assert (TREE_CODE (decl) == FIELD_DECL);
10882
10883   /* We cannot yet cope with fields whose positions are variable, so
10884      for now, when we see such things, we simply return 0.  Someday, we may
10885      be able to handle such cases, but it will be damn difficult.  */
10886   if (! host_integerp (bit_position (decl), 0))
10887     return 0;
10888
10889   bitpos_int = int_bit_position (decl);
10890
10891 #ifdef PCC_BITFIELD_TYPE_MATTERS
10892   if (PCC_BITFIELD_TYPE_MATTERS)
10893     {
10894       tree type;
10895       tree field_size_tree;
10896       HOST_WIDE_INT deepest_bitpos;
10897       unsigned HOST_WIDE_INT field_size_in_bits;
10898       unsigned int type_align_in_bits;
10899       unsigned int decl_align_in_bits;
10900       unsigned HOST_WIDE_INT type_size_in_bits;
10901
10902       type = field_type (decl);
10903       type_size_in_bits = simple_type_size_in_bits (type);
10904       type_align_in_bits = simple_type_align_in_bits (type);
10905
10906       field_size_tree = DECL_SIZE (decl);
10907
10908       /* The size could be unspecified if there was an error, or for
10909          a flexible array member.  */
10910       if (!field_size_tree)
10911         field_size_tree = bitsize_zero_node;
10912
10913       /* If the size of the field is not constant, use the type size.  */
10914       if (host_integerp (field_size_tree, 1))
10915         field_size_in_bits = tree_low_cst (field_size_tree, 1);
10916       else
10917         field_size_in_bits = type_size_in_bits;
10918
10919       decl_align_in_bits = simple_decl_align_in_bits (decl);
10920
10921       /* The GCC front-end doesn't make any attempt to keep track of the
10922          starting bit offset (relative to the start of the containing
10923          structure type) of the hypothetical "containing object" for a
10924          bit-field.  Thus, when computing the byte offset value for the
10925          start of the "containing object" of a bit-field, we must deduce
10926          this information on our own. This can be rather tricky to do in
10927          some cases.  For example, handling the following structure type
10928          definition when compiling for an i386/i486 target (which only
10929          aligns long long's to 32-bit boundaries) can be very tricky:
10930
10931          struct S { int field1; long long field2:31; };
10932
10933          Fortunately, there is a simple rule-of-thumb which can be used
10934          in such cases.  When compiling for an i386/i486, GCC will
10935          allocate 8 bytes for the structure shown above.  It decides to
10936          do this based upon one simple rule for bit-field allocation.
10937          GCC allocates each "containing object" for each bit-field at
10938          the first (i.e. lowest addressed) legitimate alignment boundary
10939          (based upon the required minimum alignment for the declared
10940          type of the field) which it can possibly use, subject to the
10941          condition that there is still enough available space remaining
10942          in the containing object (when allocated at the selected point)
10943          to fully accommodate all of the bits of the bit-field itself.
10944
10945          This simple rule makes it obvious why GCC allocates 8 bytes for
10946          each object of the structure type shown above.  When looking
10947          for a place to allocate the "containing object" for `field2',
10948          the compiler simply tries to allocate a 64-bit "containing
10949          object" at each successive 32-bit boundary (starting at zero)
10950          until it finds a place to allocate that 64- bit field such that
10951          at least 31 contiguous (and previously unallocated) bits remain
10952          within that selected 64 bit field.  (As it turns out, for the
10953          example above, the compiler finds it is OK to allocate the
10954          "containing object" 64-bit field at bit-offset zero within the
10955          structure type.)
10956
10957          Here we attempt to work backwards from the limited set of facts
10958          we're given, and we try to deduce from those facts, where GCC
10959          must have believed that the containing object started (within
10960          the structure type). The value we deduce is then used (by the
10961          callers of this routine) to generate DW_AT_location and
10962          DW_AT_bit_offset attributes for fields (both bit-fields and, in
10963          the case of DW_AT_location, regular fields as well).  */
10964
10965       /* Figure out the bit-distance from the start of the structure to
10966          the "deepest" bit of the bit-field.  */
10967       deepest_bitpos = bitpos_int + field_size_in_bits;
10968
10969       /* This is the tricky part.  Use some fancy footwork to deduce
10970          where the lowest addressed bit of the containing object must
10971          be.  */
10972       object_offset_in_bits = deepest_bitpos - type_size_in_bits;
10973
10974       /* Round up to type_align by default.  This works best for
10975          bitfields.  */
10976       object_offset_in_bits
10977         = round_up_to_align (object_offset_in_bits, type_align_in_bits);
10978
10979       if (object_offset_in_bits > bitpos_int)
10980         {
10981           object_offset_in_bits = deepest_bitpos - type_size_in_bits;
10982
10983           /* Round up to decl_align instead.  */
10984           object_offset_in_bits
10985             = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
10986         }
10987     }
10988   else
10989 #endif
10990     object_offset_in_bits = bitpos_int;
10991
10992   return object_offset_in_bits / BITS_PER_UNIT;
10993 }
10994 \f
10995 /* The following routines define various Dwarf attributes and any data
10996    associated with them.  */
10997
10998 /* Add a location description attribute value to a DIE.
10999
11000    This emits location attributes suitable for whole variables and
11001    whole parameters.  Note that the location attributes for struct fields are
11002    generated by the routine `data_member_location_attribute' below.  */
11003
11004 static inline void
11005 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
11006                              dw_loc_descr_ref descr)
11007 {
11008   if (descr != 0)
11009     add_AT_loc (die, attr_kind, descr);
11010 }
11011
11012 /* Attach the specialized form of location attribute used for data members of
11013    struct and union types.  In the special case of a FIELD_DECL node which
11014    represents a bit-field, the "offset" part of this special location
11015    descriptor must indicate the distance in bytes from the lowest-addressed
11016    byte of the containing struct or union type to the lowest-addressed byte of
11017    the "containing object" for the bit-field.  (See the `field_byte_offset'
11018    function above).
11019
11020    For any given bit-field, the "containing object" is a hypothetical object
11021    (of some integral or enum type) within which the given bit-field lives.  The
11022    type of this hypothetical "containing object" is always the same as the
11023    declared type of the individual bit-field itself (for GCC anyway... the
11024    DWARF spec doesn't actually mandate this).  Note that it is the size (in
11025    bytes) of the hypothetical "containing object" which will be given in the
11026    DW_AT_byte_size attribute for this bit-field.  (See the
11027    `byte_size_attribute' function below.)  It is also used when calculating the
11028    value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
11029    function below.)  */
11030
11031 static void
11032 add_data_member_location_attribute (dw_die_ref die, tree decl)
11033 {
11034   HOST_WIDE_INT offset;
11035   dw_loc_descr_ref loc_descr = 0;
11036
11037   if (TREE_CODE (decl) == TREE_BINFO)
11038     {
11039       /* We're working on the TAG_inheritance for a base class.  */
11040       if (BINFO_VIRTUAL_P (decl) && is_cxx ())
11041         {
11042           /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
11043              aren't at a fixed offset from all (sub)objects of the same
11044              type.  We need to extract the appropriate offset from our
11045              vtable.  The following dwarf expression means
11046
11047                BaseAddr = ObAddr + *((*ObAddr) - Offset)
11048
11049              This is specific to the V3 ABI, of course.  */
11050
11051           dw_loc_descr_ref tmp;
11052
11053           /* Make a copy of the object address.  */
11054           tmp = new_loc_descr (DW_OP_dup, 0, 0);
11055           add_loc_descr (&loc_descr, tmp);
11056
11057           /* Extract the vtable address.  */
11058           tmp = new_loc_descr (DW_OP_deref, 0, 0);
11059           add_loc_descr (&loc_descr, tmp);
11060
11061           /* Calculate the address of the offset.  */
11062           offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
11063           gcc_assert (offset < 0);
11064
11065           tmp = int_loc_descriptor (-offset);
11066           add_loc_descr (&loc_descr, tmp);
11067           tmp = new_loc_descr (DW_OP_minus, 0, 0);
11068           add_loc_descr (&loc_descr, tmp);
11069
11070           /* Extract the offset.  */
11071           tmp = new_loc_descr (DW_OP_deref, 0, 0);
11072           add_loc_descr (&loc_descr, tmp);
11073
11074           /* Add it to the object address.  */
11075           tmp = new_loc_descr (DW_OP_plus, 0, 0);
11076           add_loc_descr (&loc_descr, tmp);
11077         }
11078       else
11079         offset = tree_low_cst (BINFO_OFFSET (decl), 0);
11080     }
11081   else
11082     offset = field_byte_offset (decl);
11083
11084   if (! loc_descr)
11085     {
11086       enum dwarf_location_atom op;
11087
11088       /* The DWARF2 standard says that we should assume that the structure
11089          address is already on the stack, so we can specify a structure field
11090          address by using DW_OP_plus_uconst.  */
11091
11092 #ifdef MIPS_DEBUGGING_INFO
11093       /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
11094          operator correctly.  It works only if we leave the offset on the
11095          stack.  */
11096       op = DW_OP_constu;
11097 #else
11098       op = DW_OP_plus_uconst;
11099 #endif
11100
11101       loc_descr = new_loc_descr (op, offset, 0);
11102     }
11103
11104   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
11105 }
11106
11107 /* Writes integer values to dw_vec_const array.  */
11108
11109 static void
11110 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
11111 {
11112   while (size != 0)
11113     {
11114       *dest++ = val & 0xff;
11115       val >>= 8;
11116       --size;
11117     }
11118 }
11119
11120 /* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
11121
11122 static HOST_WIDE_INT
11123 extract_int (const unsigned char *src, unsigned int size)
11124 {
11125   HOST_WIDE_INT val = 0;
11126
11127   src += size;
11128   while (size != 0)
11129     {
11130       val <<= 8;
11131       val |= *--src & 0xff;
11132       --size;
11133     }
11134   return val;
11135 }
11136
11137 /* Writes floating point values to dw_vec_const array.  */
11138
11139 static void
11140 insert_float (const_rtx rtl, unsigned char *array)
11141 {
11142   REAL_VALUE_TYPE rv;
11143   long val[4];
11144   int i;
11145
11146   REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
11147   real_to_target (val, &rv, GET_MODE (rtl));
11148
11149   /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
11150   for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
11151     {
11152       insert_int (val[i], 4, array);
11153       array += 4;
11154     }
11155 }
11156
11157 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
11158    does not have a "location" either in memory or in a register.  These
11159    things can arise in GNU C when a constant is passed as an actual parameter
11160    to an inlined function.  They can also arise in C++ where declared
11161    constants do not necessarily get memory "homes".  */
11162
11163 static void
11164 add_const_value_attribute (dw_die_ref die, rtx rtl)
11165 {
11166   switch (GET_CODE (rtl))
11167     {
11168     case CONST_INT:
11169       {
11170         HOST_WIDE_INT val = INTVAL (rtl);
11171
11172         if (val < 0)
11173           add_AT_int (die, DW_AT_const_value, val);
11174         else
11175           add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
11176       }
11177       break;
11178
11179     case CONST_DOUBLE:
11180       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
11181          floating-point constant.  A CONST_DOUBLE is used whenever the
11182          constant requires more than one word in order to be adequately
11183          represented.  We output CONST_DOUBLEs as blocks.  */
11184       {
11185         enum machine_mode mode = GET_MODE (rtl);
11186
11187         if (SCALAR_FLOAT_MODE_P (mode))
11188           {
11189             unsigned int length = GET_MODE_SIZE (mode);
11190             unsigned char *array = GGC_NEWVEC (unsigned char, length);
11191
11192             insert_float (rtl, array);
11193             add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
11194           }
11195         else
11196           {
11197             /* ??? We really should be using HOST_WIDE_INT throughout.  */
11198             gcc_assert (HOST_BITS_PER_LONG == HOST_BITS_PER_WIDE_INT);
11199
11200             add_AT_long_long (die, DW_AT_const_value,
11201                               CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
11202           }
11203       }
11204       break;
11205
11206     case CONST_VECTOR:
11207       {
11208         enum machine_mode mode = GET_MODE (rtl);
11209         unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
11210         unsigned int length = CONST_VECTOR_NUNITS (rtl);
11211         unsigned char *array = GGC_NEWVEC (unsigned char, length * elt_size);
11212         unsigned int i;
11213         unsigned char *p;
11214
11215         switch (GET_MODE_CLASS (mode))
11216           {
11217           case MODE_VECTOR_INT:
11218             for (i = 0, p = array; i < length; i++, p += elt_size)
11219               {
11220                 rtx elt = CONST_VECTOR_ELT (rtl, i);
11221                 HOST_WIDE_INT lo, hi;
11222
11223                 switch (GET_CODE (elt))
11224                   {
11225                   case CONST_INT:
11226                     lo = INTVAL (elt);
11227                     hi = -(lo < 0);
11228                     break;
11229
11230                   case CONST_DOUBLE:
11231                     lo = CONST_DOUBLE_LOW (elt);
11232                     hi = CONST_DOUBLE_HIGH (elt);
11233                     break;
11234
11235                   default:
11236                     gcc_unreachable ();
11237                   }
11238
11239                 if (elt_size <= sizeof (HOST_WIDE_INT))
11240                   insert_int (lo, elt_size, p);
11241                 else
11242                   {
11243                     unsigned char *p0 = p;
11244                     unsigned char *p1 = p + sizeof (HOST_WIDE_INT);
11245
11246                     gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
11247                     if (WORDS_BIG_ENDIAN)
11248                       {
11249                         p0 = p1;
11250                         p1 = p;
11251                       }
11252                     insert_int (lo, sizeof (HOST_WIDE_INT), p0);
11253                     insert_int (hi, sizeof (HOST_WIDE_INT), p1);
11254                   }
11255               }
11256             break;
11257
11258           case MODE_VECTOR_FLOAT:
11259             for (i = 0, p = array; i < length; i++, p += elt_size)
11260               {
11261                 rtx elt = CONST_VECTOR_ELT (rtl, i);
11262                 insert_float (elt, p);
11263               }
11264             break;
11265
11266           default:
11267             gcc_unreachable ();
11268           }
11269
11270         add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
11271       }
11272       break;
11273
11274     case CONST_STRING:
11275       add_AT_string (die, DW_AT_const_value, XSTR (rtl, 0));
11276       break;
11277
11278     case SYMBOL_REF:
11279     case LABEL_REF:
11280     case CONST:
11281       add_AT_addr (die, DW_AT_const_value, rtl);
11282       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
11283       break;
11284
11285     case PLUS:
11286       /* In cases where an inlined instance of an inline function is passed
11287          the address of an `auto' variable (which is local to the caller) we
11288          can get a situation where the DECL_RTL of the artificial local
11289          variable (for the inlining) which acts as a stand-in for the
11290          corresponding formal parameter (of the inline function) will look
11291          like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
11292          exactly a compile-time constant expression, but it isn't the address
11293          of the (artificial) local variable either.  Rather, it represents the
11294          *value* which the artificial local variable always has during its
11295          lifetime.  We currently have no way to represent such quasi-constant
11296          values in Dwarf, so for now we just punt and generate nothing.  */
11297       break;
11298
11299     default:
11300       /* No other kinds of rtx should be possible here.  */
11301       gcc_unreachable ();
11302     }
11303
11304 }
11305
11306 /* Determine whether the evaluation of EXPR references any variables
11307    or functions which aren't otherwise used (and therefore may not be
11308    output).  */
11309 static tree
11310 reference_to_unused (tree * tp, int * walk_subtrees,
11311                      void * data ATTRIBUTE_UNUSED)
11312 {
11313   if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
11314     *walk_subtrees = 0;
11315
11316   if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
11317       && ! TREE_ASM_WRITTEN (*tp))
11318     return *tp;
11319   /* ???  The C++ FE emits debug information for using decls, so
11320      putting gcc_unreachable here falls over.  See PR31899.  For now
11321      be conservative.  */
11322   else if (!cgraph_global_info_ready
11323            && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
11324     return *tp;
11325   else if (DECL_P (*tp) && TREE_CODE (*tp) == VAR_DECL)
11326     {
11327       struct varpool_node *node = varpool_node (*tp);
11328       if (!node->needed)
11329         return *tp;
11330     }
11331   else if (DECL_P (*tp) && TREE_CODE (*tp) == FUNCTION_DECL
11332            && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
11333     {
11334       struct cgraph_node *node = cgraph_node (*tp);
11335       if (node->process || TREE_ASM_WRITTEN (*tp))
11336         return *tp;
11337     }
11338   else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
11339     return *tp;
11340
11341   return NULL_TREE;
11342 }
11343
11344 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
11345    for use in a later add_const_value_attribute call.  */
11346
11347 static rtx
11348 rtl_for_decl_init (tree init, tree type)
11349 {
11350   rtx rtl = NULL_RTX;
11351
11352   /* If a variable is initialized with a string constant without embedded
11353      zeros, build CONST_STRING.  */
11354   if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
11355     {
11356       tree enttype = TREE_TYPE (type);
11357       tree domain = TYPE_DOMAIN (type);
11358       enum machine_mode mode = TYPE_MODE (enttype);
11359
11360       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
11361           && domain
11362           && integer_zerop (TYPE_MIN_VALUE (domain))
11363           && compare_tree_int (TYPE_MAX_VALUE (domain),
11364                                TREE_STRING_LENGTH (init) - 1) == 0
11365           && ((size_t) TREE_STRING_LENGTH (init)
11366               == strlen (TREE_STRING_POINTER (init)) + 1))
11367         rtl = gen_rtx_CONST_STRING (VOIDmode,
11368                                     ggc_strdup (TREE_STRING_POINTER (init)));
11369     }
11370   /* Other aggregates, and complex values, could be represented using
11371      CONCAT: FIXME!  */
11372   else if (AGGREGATE_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
11373     ;
11374   /* Vectors only work if their mode is supported by the target.
11375      FIXME: generic vectors ought to work too.  */
11376   else if (TREE_CODE (type) == VECTOR_TYPE && TYPE_MODE (type) == BLKmode)
11377     ;
11378   /* If the initializer is something that we know will expand into an
11379      immediate RTL constant, expand it now.  We must be careful not to
11380      reference variables which won't be output.  */
11381   else if (initializer_constant_valid_p (init, type)
11382            && ! walk_tree (&init, reference_to_unused, NULL, NULL))
11383     {
11384       /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
11385          possible.  */
11386       if (TREE_CODE (type) == VECTOR_TYPE)
11387         switch (TREE_CODE (init))
11388           {
11389           case VECTOR_CST:
11390             break;
11391           case CONSTRUCTOR:
11392             if (TREE_CONSTANT (init))
11393               {
11394                 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
11395                 bool constant_p = true;
11396                 tree value;
11397                 unsigned HOST_WIDE_INT ix;
11398
11399                 /* Even when ctor is constant, it might contain non-*_CST
11400                    elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
11401                    belong into VECTOR_CST nodes.  */
11402                 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
11403                   if (!CONSTANT_CLASS_P (value))
11404                     {
11405                       constant_p = false;
11406                       break;
11407                     }
11408
11409                 if (constant_p)
11410                   {
11411                     init = build_vector_from_ctor (type, elts);
11412                     break;
11413                   }
11414               }
11415             /* FALLTHRU */
11416
11417           default:
11418             return NULL;
11419           }
11420
11421       rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
11422
11423       /* If expand_expr returns a MEM, it wasn't immediate.  */
11424       gcc_assert (!rtl || !MEM_P (rtl));
11425     }
11426
11427   return rtl;
11428 }
11429
11430 /* Generate RTL for the variable DECL to represent its location.  */
11431
11432 static rtx
11433 rtl_for_decl_location (tree decl)
11434 {
11435   rtx rtl;
11436
11437   /* Here we have to decide where we are going to say the parameter "lives"
11438      (as far as the debugger is concerned).  We only have a couple of
11439      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
11440
11441      DECL_RTL normally indicates where the parameter lives during most of the
11442      activation of the function.  If optimization is enabled however, this
11443      could be either NULL or else a pseudo-reg.  Both of those cases indicate
11444      that the parameter doesn't really live anywhere (as far as the code
11445      generation parts of GCC are concerned) during most of the function's
11446      activation.  That will happen (for example) if the parameter is never
11447      referenced within the function.
11448
11449      We could just generate a location descriptor here for all non-NULL
11450      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
11451      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
11452      where DECL_RTL is NULL or is a pseudo-reg.
11453
11454      Note however that we can only get away with using DECL_INCOMING_RTL as
11455      a backup substitute for DECL_RTL in certain limited cases.  In cases
11456      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
11457      we can be sure that the parameter was passed using the same type as it is
11458      declared to have within the function, and that its DECL_INCOMING_RTL
11459      points us to a place where a value of that type is passed.
11460
11461      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
11462      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
11463      because in these cases DECL_INCOMING_RTL points us to a value of some
11464      type which is *different* from the type of the parameter itself.  Thus,
11465      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
11466      such cases, the debugger would end up (for example) trying to fetch a
11467      `float' from a place which actually contains the first part of a
11468      `double'.  That would lead to really incorrect and confusing
11469      output at debug-time.
11470
11471      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
11472      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
11473      are a couple of exceptions however.  On little-endian machines we can
11474      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
11475      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
11476      an integral type that is smaller than TREE_TYPE (decl). These cases arise
11477      when (on a little-endian machine) a non-prototyped function has a
11478      parameter declared to be of type `short' or `char'.  In such cases,
11479      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
11480      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
11481      passed `int' value.  If the debugger then uses that address to fetch
11482      a `short' or a `char' (on a little-endian machine) the result will be
11483      the correct data, so we allow for such exceptional cases below.
11484
11485      Note that our goal here is to describe the place where the given formal
11486      parameter lives during most of the function's activation (i.e. between the
11487      end of the prologue and the start of the epilogue).  We'll do that as best
11488      as we can. Note however that if the given formal parameter is modified
11489      sometime during the execution of the function, then a stack backtrace (at
11490      debug-time) will show the function as having been called with the *new*
11491      value rather than the value which was originally passed in.  This happens
11492      rarely enough that it is not a major problem, but it *is* a problem, and
11493      I'd like to fix it.
11494
11495      A future version of dwarf2out.c may generate two additional attributes for
11496      any given DW_TAG_formal_parameter DIE which will describe the "passed
11497      type" and the "passed location" for the given formal parameter in addition
11498      to the attributes we now generate to indicate the "declared type" and the
11499      "active location" for each parameter.  This additional set of attributes
11500      could be used by debuggers for stack backtraces. Separately, note that
11501      sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
11502      This happens (for example) for inlined-instances of inline function formal
11503      parameters which are never referenced.  This really shouldn't be
11504      happening.  All PARM_DECL nodes should get valid non-NULL
11505      DECL_INCOMING_RTL values.  FIXME.  */
11506
11507   /* Use DECL_RTL as the "location" unless we find something better.  */
11508   rtl = DECL_RTL_IF_SET (decl);
11509
11510   /* When generating abstract instances, ignore everything except
11511      constants, symbols living in memory, and symbols living in
11512      fixed registers.  */
11513   if (! reload_completed)
11514     {
11515       if (rtl
11516           && (CONSTANT_P (rtl)
11517               || (MEM_P (rtl)
11518                   && CONSTANT_P (XEXP (rtl, 0)))
11519               || (REG_P (rtl)
11520                   && TREE_CODE (decl) == VAR_DECL
11521                   && TREE_STATIC (decl))))
11522         {
11523           rtl = targetm.delegitimize_address (rtl);
11524           return rtl;
11525         }
11526       rtl = NULL_RTX;
11527     }
11528   else if (TREE_CODE (decl) == PARM_DECL)
11529     {
11530       if (rtl == NULL_RTX || is_pseudo_reg (rtl))
11531         {
11532           tree declared_type = TREE_TYPE (decl);
11533           tree passed_type = DECL_ARG_TYPE (decl);
11534           enum machine_mode dmode = TYPE_MODE (declared_type);
11535           enum machine_mode pmode = TYPE_MODE (passed_type);
11536
11537           /* This decl represents a formal parameter which was optimized out.
11538              Note that DECL_INCOMING_RTL may be NULL in here, but we handle
11539              all cases where (rtl == NULL_RTX) just below.  */
11540           if (dmode == pmode)
11541             rtl = DECL_INCOMING_RTL (decl);
11542           else if (SCALAR_INT_MODE_P (dmode)
11543                    && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
11544                    && DECL_INCOMING_RTL (decl))
11545             {
11546               rtx inc = DECL_INCOMING_RTL (decl);
11547               if (REG_P (inc))
11548                 rtl = inc;
11549               else if (MEM_P (inc))
11550                 {
11551                   if (BYTES_BIG_ENDIAN)
11552                     rtl = adjust_address_nv (inc, dmode,
11553                                              GET_MODE_SIZE (pmode)
11554                                              - GET_MODE_SIZE (dmode));
11555                   else
11556                     rtl = inc;
11557                 }
11558             }
11559         }
11560
11561       /* If the parm was passed in registers, but lives on the stack, then
11562          make a big endian correction if the mode of the type of the
11563          parameter is not the same as the mode of the rtl.  */
11564       /* ??? This is the same series of checks that are made in dbxout.c before
11565          we reach the big endian correction code there.  It isn't clear if all
11566          of these checks are necessary here, but keeping them all is the safe
11567          thing to do.  */
11568       else if (MEM_P (rtl)
11569                && XEXP (rtl, 0) != const0_rtx
11570                && ! CONSTANT_P (XEXP (rtl, 0))
11571                /* Not passed in memory.  */
11572                && !MEM_P (DECL_INCOMING_RTL (decl))
11573                /* Not passed by invisible reference.  */
11574                && (!REG_P (XEXP (rtl, 0))
11575                    || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
11576                    || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
11577 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
11578                    || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
11579 #endif
11580                      )
11581                /* Big endian correction check.  */
11582                && BYTES_BIG_ENDIAN
11583                && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
11584                && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
11585                    < UNITS_PER_WORD))
11586         {
11587           int offset = (UNITS_PER_WORD
11588                         - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
11589
11590           rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
11591                              plus_constant (XEXP (rtl, 0), offset));
11592         }
11593     }
11594   else if (TREE_CODE (decl) == VAR_DECL
11595            && rtl
11596            && MEM_P (rtl)
11597            && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
11598            && BYTES_BIG_ENDIAN)
11599     {
11600       int rsize = GET_MODE_SIZE (GET_MODE (rtl));
11601       int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
11602
11603       /* If a variable is declared "register" yet is smaller than
11604          a register, then if we store the variable to memory, it
11605          looks like we're storing a register-sized value, when in
11606          fact we are not.  We need to adjust the offset of the
11607          storage location to reflect the actual value's bytes,
11608          else gdb will not be able to display it.  */
11609       if (rsize > dsize)
11610         rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
11611                            plus_constant (XEXP (rtl, 0), rsize-dsize));
11612     }
11613
11614   /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
11615      and will have been substituted directly into all expressions that use it.
11616      C does not have such a concept, but C++ and other languages do.  */
11617   if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
11618     rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
11619
11620   if (rtl)
11621     rtl = targetm.delegitimize_address (rtl);
11622
11623   /* If we don't look past the constant pool, we risk emitting a
11624      reference to a constant pool entry that isn't referenced from
11625      code, and thus is not emitted.  */
11626   if (rtl)
11627     rtl = avoid_constant_pool_reference (rtl);
11628
11629   return rtl;
11630 }
11631
11632 /* We need to figure out what section we should use as the base for the
11633    address ranges where a given location is valid.
11634    1. If this particular DECL has a section associated with it, use that.
11635    2. If this function has a section associated with it, use that.
11636    3. Otherwise, use the text section.
11637    XXX: If you split a variable across multiple sections, we won't notice.  */
11638
11639 static const char *
11640 secname_for_decl (const_tree decl)
11641 {
11642   const char *secname;
11643
11644   if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
11645     {
11646       tree sectree = DECL_SECTION_NAME (decl);
11647       secname = TREE_STRING_POINTER (sectree);
11648     }
11649   else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
11650     {
11651       tree sectree = DECL_SECTION_NAME (current_function_decl);
11652       secname = TREE_STRING_POINTER (sectree);
11653     }
11654   else if (cfun && in_cold_section_p)
11655     secname = crtl->subsections.cold_section_label;
11656   else
11657     secname = text_section_label;
11658
11659   return secname;
11660 }
11661
11662 /* Check whether decl is a Fortran COMMON symbol.  If not, NULL_TREE is
11663    returned.  If so, the decl for the COMMON block is returned, and the
11664    value is the offset into the common block for the symbol.  */
11665
11666 static tree
11667 fortran_common (tree decl, HOST_WIDE_INT *value)
11668 {
11669   tree val_expr, cvar;
11670   enum machine_mode mode;
11671   HOST_WIDE_INT bitsize, bitpos;
11672   tree offset;
11673   int volatilep = 0, unsignedp = 0;
11674
11675   /* If the decl isn't a VAR_DECL, or if it isn't public or static, or if
11676      it does not have a value (the offset into the common area), or if it
11677      is thread local (as opposed to global) then it isn't common, and shouldn't
11678      be handled as such.  */
11679   if (TREE_CODE (decl) != VAR_DECL
11680       || !TREE_PUBLIC (decl)
11681       || !TREE_STATIC (decl)
11682       || !DECL_HAS_VALUE_EXPR_P (decl)
11683       || !is_fortran ())
11684     return NULL_TREE;
11685
11686   val_expr = DECL_VALUE_EXPR (decl);
11687   if (TREE_CODE (val_expr) != COMPONENT_REF)
11688     return NULL_TREE;
11689
11690   cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
11691                               &mode, &unsignedp, &volatilep, true);
11692
11693   if (cvar == NULL_TREE
11694       || TREE_CODE (cvar) != VAR_DECL
11695       || DECL_ARTIFICIAL (cvar)
11696       || !TREE_PUBLIC (cvar))
11697     return NULL_TREE;
11698
11699   *value = 0;
11700   if (offset != NULL)
11701     {
11702       if (!host_integerp (offset, 0))
11703         return NULL_TREE;
11704       *value = tree_low_cst (offset, 0);
11705     }
11706   if (bitpos != 0)
11707     *value += bitpos / BITS_PER_UNIT;
11708
11709   return cvar;
11710 }
11711
11712 /* Dereference a location expression LOC if DECL is passed by invisible
11713    reference.  */
11714
11715 static dw_loc_descr_ref
11716 loc_by_reference (dw_loc_descr_ref loc, tree decl)
11717 {
11718   HOST_WIDE_INT size;
11719   enum dwarf_location_atom op;
11720
11721   if (loc == NULL)
11722     return NULL;
11723
11724   if ((TREE_CODE (decl) != PARM_DECL && TREE_CODE (decl) != RESULT_DECL)
11725       || !DECL_BY_REFERENCE (decl))
11726     return loc;
11727
11728   size = int_size_in_bytes (TREE_TYPE (decl));
11729   if (size > DWARF2_ADDR_SIZE || size == -1)
11730     return 0;
11731   else if (size == DWARF2_ADDR_SIZE)
11732     op = DW_OP_deref;
11733   else
11734     op = DW_OP_deref_size;
11735   add_loc_descr (&loc, new_loc_descr (op, size, 0));
11736   return loc;
11737 }
11738
11739 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
11740    data attribute for a variable or a parameter.  We generate the
11741    DW_AT_const_value attribute only in those cases where the given variable
11742    or parameter does not have a true "location" either in memory or in a
11743    register.  This can happen (for example) when a constant is passed as an
11744    actual argument in a call to an inline function.  (It's possible that
11745    these things can crop up in other ways also.)  Note that one type of
11746    constant value which can be passed into an inlined function is a constant
11747    pointer.  This can happen for example if an actual argument in an inlined
11748    function call evaluates to a compile-time constant address.  */
11749
11750 static void
11751 add_location_or_const_value_attribute (dw_die_ref die, tree decl,
11752                                        enum dwarf_attribute attr)
11753 {
11754   rtx rtl;
11755   dw_loc_descr_ref descr;
11756   var_loc_list *loc_list;
11757   struct var_loc_node *node;
11758   if (TREE_CODE (decl) == ERROR_MARK)
11759     return;
11760
11761   gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
11762               || TREE_CODE (decl) == RESULT_DECL);
11763
11764   /* See if we possibly have multiple locations for this variable.  */
11765   loc_list = lookup_decl_loc (decl);
11766
11767   /* If it truly has multiple locations, the first and last node will
11768      differ.  */
11769   if (loc_list && loc_list->first != loc_list->last)
11770     {
11771       const char *endname, *secname;
11772       dw_loc_list_ref list;
11773       rtx varloc;
11774       enum var_init_status initialized;
11775
11776       /* Now that we know what section we are using for a base,
11777          actually construct the list of locations.
11778          The first location information is what is passed to the
11779          function that creates the location list, and the remaining
11780          locations just get added on to that list.
11781          Note that we only know the start address for a location
11782          (IE location changes), so to build the range, we use
11783          the range [current location start, next location start].
11784          This means we have to special case the last node, and generate
11785          a range of [last location start, end of function label].  */
11786
11787       node = loc_list->first;
11788       varloc = NOTE_VAR_LOCATION (node->var_loc_note);
11789       secname = secname_for_decl (decl);
11790
11791       if (NOTE_VAR_LOCATION_LOC (node->var_loc_note))
11792         initialized = NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
11793       else
11794         initialized = VAR_INIT_STATUS_INITIALIZED;
11795
11796       descr = loc_by_reference (loc_descriptor (varloc, initialized), decl);
11797       list = new_loc_list (descr, node->label, node->next->label, secname, 1);
11798       node = node->next;
11799
11800       for (; node->next; node = node->next)
11801         if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
11802           {
11803             /* The variable has a location between NODE->LABEL and
11804                NODE->NEXT->LABEL.  */
11805             enum var_init_status initialized =
11806               NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
11807             varloc = NOTE_VAR_LOCATION (node->var_loc_note);
11808             descr = loc_by_reference (loc_descriptor (varloc, initialized),
11809                                       decl);
11810             add_loc_descr_to_loc_list (&list, descr,
11811                                        node->label, node->next->label, secname);
11812           }
11813
11814       /* If the variable has a location at the last label
11815          it keeps its location until the end of function.  */
11816       if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
11817         {
11818           char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
11819           enum var_init_status initialized =
11820             NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
11821
11822           varloc = NOTE_VAR_LOCATION (node->var_loc_note);
11823           if (!current_function_decl)
11824             endname = text_end_label;
11825           else
11826             {
11827               ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
11828                                            current_function_funcdef_no);
11829               endname = ggc_strdup (label_id);
11830             }
11831           descr = loc_by_reference (loc_descriptor (varloc, initialized),
11832                                     decl);
11833           add_loc_descr_to_loc_list (&list, descr,
11834                                      node->label, endname, secname);
11835         }
11836
11837       /* Finally, add the location list to the DIE, and we are done.  */
11838       add_AT_loc_list (die, attr, list);
11839       return;
11840     }
11841
11842   /* Try to get some constant RTL for this decl, and use that as the value of
11843      the location.  */
11844
11845   rtl = rtl_for_decl_location (decl);
11846   if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING))
11847     {
11848       add_const_value_attribute (die, rtl);
11849       return;
11850     }
11851
11852   /* If we have tried to generate the location otherwise, and it
11853      didn't work out (we wouldn't be here if we did), and we have a one entry
11854      location list, try generating a location from that.  */
11855   if (loc_list && loc_list->first)
11856     {
11857       enum var_init_status status;
11858       node = loc_list->first;
11859       status = NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
11860       descr = loc_descriptor (NOTE_VAR_LOCATION (node->var_loc_note), status);
11861       if (descr)
11862         {
11863           descr = loc_by_reference (descr, decl);
11864           add_AT_location_description (die, attr, descr);
11865           return;
11866         }
11867     }
11868
11869   /* We couldn't get any rtl, so try directly generating the location
11870      description from the tree.  */
11871   descr = loc_descriptor_from_tree (decl);
11872   if (descr)
11873     {
11874       descr = loc_by_reference (descr, decl);
11875       add_AT_location_description (die, attr, descr);
11876       return;
11877     }
11878   /* None of that worked, so it must not really have a location;
11879      try adding a constant value attribute from the DECL_INITIAL.  */
11880   tree_add_const_value_attribute (die, decl);
11881 }
11882
11883 /* Add VARIABLE and DIE into deferred locations list.  */
11884
11885 static void
11886 defer_location (tree variable, dw_die_ref die)
11887 {
11888   deferred_locations entry;
11889   entry.variable = variable;
11890   entry.die = die;
11891   VEC_safe_push (deferred_locations, gc, deferred_locations_list, &entry);
11892 }
11893
11894 /* Helper function for tree_add_const_value_attribute.  Natively encode
11895    initializer INIT into an array.  Return true if successful.  */
11896
11897 static bool
11898 native_encode_initializer (tree init, unsigned char *array, int size)
11899 {
11900   tree type;
11901
11902   if (init == NULL_TREE)
11903     return false;
11904
11905   STRIP_NOPS (init);
11906   switch (TREE_CODE (init))
11907     {
11908     case STRING_CST:
11909       type = TREE_TYPE (init);
11910       if (TREE_CODE (type) == ARRAY_TYPE)
11911         {
11912           tree enttype = TREE_TYPE (type);
11913           enum machine_mode mode = TYPE_MODE (enttype);
11914
11915           if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
11916             return false;
11917           if (int_size_in_bytes (type) != size)
11918             return false;
11919           if (size > TREE_STRING_LENGTH (init))
11920             {
11921               memcpy (array, TREE_STRING_POINTER (init),
11922                       TREE_STRING_LENGTH (init));
11923               memset (array + TREE_STRING_LENGTH (init),
11924                       '\0', size - TREE_STRING_LENGTH (init));
11925             }
11926           else
11927             memcpy (array, TREE_STRING_POINTER (init), size);
11928           return true;
11929         }
11930       return false;
11931     case CONSTRUCTOR:
11932       type = TREE_TYPE (init);
11933       if (int_size_in_bytes (type) != size)
11934         return false;
11935       if (TREE_CODE (type) == ARRAY_TYPE)
11936         {
11937           HOST_WIDE_INT min_index;
11938           unsigned HOST_WIDE_INT cnt;
11939           int curpos = 0, fieldsize;
11940           constructor_elt *ce;
11941
11942           if (TYPE_DOMAIN (type) == NULL_TREE
11943               || !host_integerp (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0))
11944             return false;
11945
11946           fieldsize = int_size_in_bytes (TREE_TYPE (type));
11947           if (fieldsize <= 0)
11948             return false;
11949
11950           min_index = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0);
11951           memset (array, '\0', size);
11952           for (cnt = 0;
11953                VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce);
11954                cnt++)
11955             {
11956               tree val = ce->value;
11957               tree index = ce->index;
11958               int pos = curpos;
11959               if (index && TREE_CODE (index) == RANGE_EXPR)
11960                 pos = (tree_low_cst (TREE_OPERAND (index, 0), 0) - min_index)
11961                       * fieldsize;
11962               else if (index)
11963                 pos = (tree_low_cst (index, 0) - min_index) * fieldsize;
11964
11965               if (val)
11966                 {
11967                   STRIP_NOPS (val);
11968                   if (!native_encode_initializer (val, array + pos, fieldsize))
11969                     return false;
11970                 }
11971               curpos = pos + fieldsize;
11972               if (index && TREE_CODE (index) == RANGE_EXPR)
11973                 {
11974                   int count = tree_low_cst (TREE_OPERAND (index, 1), 0)
11975                               - tree_low_cst (TREE_OPERAND (index, 0), 0);
11976                   while (count > 0)
11977                     {
11978                       if (val)
11979                         memcpy (array + curpos, array + pos, fieldsize);
11980                       curpos += fieldsize;
11981                     }
11982                 }
11983               gcc_assert (curpos <= size);
11984             }
11985           return true;
11986         }
11987       else if (TREE_CODE (type) == RECORD_TYPE
11988                || TREE_CODE (type) == UNION_TYPE)
11989         {
11990           tree field = NULL_TREE;
11991           unsigned HOST_WIDE_INT cnt;
11992           constructor_elt *ce;
11993
11994           if (int_size_in_bytes (type) != size)
11995             return false;
11996
11997           if (TREE_CODE (type) == RECORD_TYPE)
11998             field = TYPE_FIELDS (type);
11999
12000           for (cnt = 0;
12001                VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce);
12002                cnt++, field = field ? TREE_CHAIN (field) : 0)
12003             {
12004               tree val = ce->value;
12005               int pos, fieldsize;
12006
12007               if (ce->index != 0)
12008                 field = ce->index;
12009
12010               if (val)
12011                 STRIP_NOPS (val);
12012
12013               if (field == NULL_TREE || DECL_BIT_FIELD (field))
12014                 return false;
12015
12016               if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
12017                   && TYPE_DOMAIN (TREE_TYPE (field))
12018                   && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
12019                 return false;
12020               else if (DECL_SIZE_UNIT (field) == NULL_TREE
12021                        || !host_integerp (DECL_SIZE_UNIT (field), 0))
12022                 return false;
12023               fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 0);
12024               pos = int_byte_position (field);
12025               gcc_assert (pos + fieldsize <= size);
12026               if (val
12027                   && !native_encode_initializer (val, array + pos, fieldsize))
12028                 return false;
12029             }
12030           return true;
12031         }
12032       return false;
12033     case VIEW_CONVERT_EXPR:
12034     case NON_LVALUE_EXPR:
12035       return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
12036     default:
12037       return native_encode_expr (init, array, size) == size;
12038     }
12039 }
12040
12041 /* If we don't have a copy of this variable in memory for some reason (such
12042    as a C++ member constant that doesn't have an out-of-line definition),
12043    we should tell the debugger about the constant value.  */
12044
12045 static void
12046 tree_add_const_value_attribute (dw_die_ref var_die, tree decl)
12047 {
12048   tree init;
12049   tree type = TREE_TYPE (decl);
12050   rtx rtl;
12051
12052   if (TREE_CODE (decl) != VAR_DECL && TREE_CODE (decl) != CONST_DECL)
12053     return;
12054
12055   init = DECL_INITIAL (decl);
12056   if (TREE_READONLY (decl) && ! TREE_THIS_VOLATILE (decl) && init)
12057     /* OK */;
12058   else
12059     return;
12060
12061   rtl = rtl_for_decl_init (init, type);
12062   if (rtl)
12063     add_const_value_attribute (var_die, rtl);
12064   /* If the host and target are sane, try harder.  */
12065   else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
12066            && initializer_constant_valid_p (init, type))
12067     {
12068       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
12069       if (size > 0 && (int) size == size)
12070         {
12071           unsigned char *array = GGC_CNEWVEC (unsigned char, size);
12072
12073           if (native_encode_initializer (init, array, size))
12074             add_AT_vec (var_die, DW_AT_const_value, size, 1, array);
12075         }
12076     }
12077 }
12078
12079 /* Convert the CFI instructions for the current function into a
12080    location list.  This is used for DW_AT_frame_base when we targeting
12081    a dwarf2 consumer that does not support the dwarf3
12082    DW_OP_call_frame_cfa.  OFFSET is a constant to be added to all CFA
12083    expressions.  */
12084
12085 static dw_loc_list_ref
12086 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
12087 {
12088   dw_fde_ref fde;
12089   dw_loc_list_ref list, *list_tail;
12090   dw_cfi_ref cfi;
12091   dw_cfa_location last_cfa, next_cfa;
12092   const char *start_label, *last_label, *section;
12093
12094   fde = current_fde ();
12095   gcc_assert (fde != NULL);
12096
12097   section = secname_for_decl (current_function_decl);
12098   list_tail = &list;
12099   list = NULL;
12100
12101   next_cfa.reg = INVALID_REGNUM;
12102   next_cfa.offset = 0;
12103   next_cfa.indirect = 0;
12104   next_cfa.base_offset = 0;
12105
12106   start_label = fde->dw_fde_begin;
12107
12108   /* ??? Bald assumption that the CIE opcode list does not contain
12109      advance opcodes.  */
12110   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
12111     lookup_cfa_1 (cfi, &next_cfa);
12112
12113   last_cfa = next_cfa;
12114   last_label = start_label;
12115
12116   for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
12117     switch (cfi->dw_cfi_opc)
12118       {
12119       case DW_CFA_set_loc:
12120       case DW_CFA_advance_loc1:
12121       case DW_CFA_advance_loc2:
12122       case DW_CFA_advance_loc4:
12123         if (!cfa_equal_p (&last_cfa, &next_cfa))
12124           {
12125             *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
12126                                        start_label, last_label, section,
12127                                        list == NULL);
12128
12129             list_tail = &(*list_tail)->dw_loc_next;
12130             last_cfa = next_cfa;
12131             start_label = last_label;
12132           }
12133         last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
12134         break;
12135
12136       case DW_CFA_advance_loc:
12137         /* The encoding is complex enough that we should never emit this.  */
12138       case DW_CFA_remember_state:
12139       case DW_CFA_restore_state:
12140         /* We don't handle these two in this function.  It would be possible
12141            if it were to be required.  */
12142         gcc_unreachable ();
12143
12144       default:
12145         lookup_cfa_1 (cfi, &next_cfa);
12146         break;
12147       }
12148
12149   if (!cfa_equal_p (&last_cfa, &next_cfa))
12150     {
12151       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
12152                                  start_label, last_label, section,
12153                                  list == NULL);
12154       list_tail = &(*list_tail)->dw_loc_next;
12155       start_label = last_label;
12156     }
12157   *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
12158                              start_label, fde->dw_fde_end, section,
12159                              list == NULL);
12160
12161   return list;
12162 }
12163
12164 /* Compute a displacement from the "steady-state frame pointer" to the
12165    frame base (often the same as the CFA), and store it in
12166    frame_pointer_fb_offset.  OFFSET is added to the displacement
12167    before the latter is negated.  */
12168
12169 static void
12170 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
12171 {
12172   rtx reg, elim;
12173
12174 #ifdef FRAME_POINTER_CFA_OFFSET
12175   reg = frame_pointer_rtx;
12176   offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
12177 #else
12178   reg = arg_pointer_rtx;
12179   offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
12180 #endif
12181
12182   elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
12183   if (GET_CODE (elim) == PLUS)
12184     {
12185       offset += INTVAL (XEXP (elim, 1));
12186       elim = XEXP (elim, 0);
12187     }
12188
12189   gcc_assert ((SUPPORTS_STACK_ALIGNMENT
12190                && (elim == hard_frame_pointer_rtx
12191                    || elim == stack_pointer_rtx))
12192               || elim == (frame_pointer_needed
12193                           ? hard_frame_pointer_rtx
12194                           : stack_pointer_rtx));
12195
12196   frame_pointer_fb_offset = -offset;
12197 }
12198
12199 /* Generate a DW_AT_name attribute given some string value to be included as
12200    the value of the attribute.  */
12201
12202 static void
12203 add_name_attribute (dw_die_ref die, const char *name_string)
12204 {
12205   if (name_string != NULL && *name_string != 0)
12206     {
12207       if (demangle_name_func)
12208         name_string = (*demangle_name_func) (name_string);
12209
12210       add_AT_string (die, DW_AT_name, name_string);
12211     }
12212 }
12213
12214 /* Generate a DW_AT_comp_dir attribute for DIE.  */
12215
12216 static void
12217 add_comp_dir_attribute (dw_die_ref die)
12218 {
12219   const char *wd = get_src_pwd ();
12220   if (wd != NULL)
12221     add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
12222 }
12223
12224 /* Given a tree node describing an array bound (either lower or upper) output
12225    a representation for that bound.  */
12226
12227 static void
12228 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
12229 {
12230   switch (TREE_CODE (bound))
12231     {
12232     case ERROR_MARK:
12233       return;
12234
12235     /* All fixed-bounds are represented by INTEGER_CST nodes.  */
12236     case INTEGER_CST:
12237       if (! host_integerp (bound, 0)
12238           || (bound_attr == DW_AT_lower_bound
12239               && (((is_c_family () || is_java ()) &&  integer_zerop (bound))
12240                   || (is_fortran () && integer_onep (bound)))))
12241         /* Use the default.  */
12242         ;
12243       else
12244         add_AT_unsigned (subrange_die, bound_attr, tree_low_cst (bound, 0));
12245       break;
12246
12247     CASE_CONVERT:
12248     case VIEW_CONVERT_EXPR:
12249       add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
12250       break;
12251
12252     case SAVE_EXPR:
12253       break;
12254
12255     case VAR_DECL:
12256     case PARM_DECL:
12257     case RESULT_DECL:
12258       {
12259         dw_die_ref decl_die = lookup_decl_die (bound);
12260         dw_loc_descr_ref loc;
12261
12262         /* ??? Can this happen, or should the variable have been bound
12263            first?  Probably it can, since I imagine that we try to create
12264            the types of parameters in the order in which they exist in
12265            the list, and won't have created a forward reference to a
12266            later parameter.  */
12267         if (decl_die != NULL)
12268           add_AT_die_ref (subrange_die, bound_attr, decl_die);
12269         else
12270           {
12271             loc = loc_descriptor_from_tree_1 (bound, 0);
12272             add_AT_location_description (subrange_die, bound_attr, loc);
12273           }
12274         break;
12275       }
12276
12277     default:
12278       {
12279         /* Otherwise try to create a stack operation procedure to
12280            evaluate the value of the array bound.  */
12281
12282         dw_die_ref ctx, decl_die;
12283         dw_loc_descr_ref loc;
12284
12285         loc = loc_descriptor_from_tree (bound);
12286         if (loc == NULL)
12287           break;
12288
12289         if (current_function_decl == 0)
12290           ctx = comp_unit_die;
12291         else
12292           ctx = lookup_decl_die (current_function_decl);
12293
12294         decl_die = new_die (DW_TAG_variable, ctx, bound);
12295         add_AT_flag (decl_die, DW_AT_artificial, 1);
12296         add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
12297         add_AT_loc (decl_die, DW_AT_location, loc);
12298
12299         add_AT_die_ref (subrange_die, bound_attr, decl_die);
12300         break;
12301       }
12302     }
12303 }
12304
12305 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
12306    possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
12307    Note that the block of subscript information for an array type also
12308    includes information about the element type of the given array type.  */
12309
12310 static void
12311 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
12312 {
12313   unsigned dimension_number;
12314   tree lower, upper;
12315   dw_die_ref subrange_die;
12316
12317   for (dimension_number = 0;
12318        TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
12319        type = TREE_TYPE (type), dimension_number++)
12320     {
12321       tree domain = TYPE_DOMAIN (type);
12322
12323       if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
12324         break;
12325
12326       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
12327          and (in GNU C only) variable bounds.  Handle all three forms
12328          here.  */
12329       subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
12330       if (domain)
12331         {
12332           /* We have an array type with specified bounds.  */
12333           lower = TYPE_MIN_VALUE (domain);
12334           upper = TYPE_MAX_VALUE (domain);
12335
12336           /* Define the index type.  */
12337           if (TREE_TYPE (domain))
12338             {
12339               /* ??? This is probably an Ada unnamed subrange type.  Ignore the
12340                  TREE_TYPE field.  We can't emit debug info for this
12341                  because it is an unnamed integral type.  */
12342               if (TREE_CODE (domain) == INTEGER_TYPE
12343                   && TYPE_NAME (domain) == NULL_TREE
12344                   && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
12345                   && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
12346                 ;
12347               else
12348                 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
12349                                     type_die);
12350             }
12351
12352           /* ??? If upper is NULL, the array has unspecified length,
12353              but it does have a lower bound.  This happens with Fortran
12354                dimension arr(N:*)
12355              Since the debugger is definitely going to need to know N
12356              to produce useful results, go ahead and output the lower
12357              bound solo, and hope the debugger can cope.  */
12358
12359           add_bound_info (subrange_die, DW_AT_lower_bound, lower);
12360           if (upper)
12361             add_bound_info (subrange_die, DW_AT_upper_bound, upper);
12362         }
12363
12364       /* Otherwise we have an array type with an unspecified length.  The
12365          DWARF-2 spec does not say how to handle this; let's just leave out the
12366          bounds.  */
12367     }
12368 }
12369
12370 static void
12371 add_byte_size_attribute (dw_die_ref die, tree tree_node)
12372 {
12373   unsigned size;
12374
12375   switch (TREE_CODE (tree_node))
12376     {
12377     case ERROR_MARK:
12378       size = 0;
12379       break;
12380     case ENUMERAL_TYPE:
12381     case RECORD_TYPE:
12382     case UNION_TYPE:
12383     case QUAL_UNION_TYPE:
12384       size = int_size_in_bytes (tree_node);
12385       break;
12386     case FIELD_DECL:
12387       /* For a data member of a struct or union, the DW_AT_byte_size is
12388          generally given as the number of bytes normally allocated for an
12389          object of the *declared* type of the member itself.  This is true
12390          even for bit-fields.  */
12391       size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
12392       break;
12393     default:
12394       gcc_unreachable ();
12395     }
12396
12397   /* Note that `size' might be -1 when we get to this point.  If it is, that
12398      indicates that the byte size of the entity in question is variable.  We
12399      have no good way of expressing this fact in Dwarf at the present time,
12400      so just let the -1 pass on through.  */
12401   add_AT_unsigned (die, DW_AT_byte_size, size);
12402 }
12403
12404 /* For a FIELD_DECL node which represents a bit-field, output an attribute
12405    which specifies the distance in bits from the highest order bit of the
12406    "containing object" for the bit-field to the highest order bit of the
12407    bit-field itself.
12408
12409    For any given bit-field, the "containing object" is a hypothetical object
12410    (of some integral or enum type) within which the given bit-field lives.  The
12411    type of this hypothetical "containing object" is always the same as the
12412    declared type of the individual bit-field itself.  The determination of the
12413    exact location of the "containing object" for a bit-field is rather
12414    complicated.  It's handled by the `field_byte_offset' function (above).
12415
12416    Note that it is the size (in bytes) of the hypothetical "containing object"
12417    which will be given in the DW_AT_byte_size attribute for this bit-field.
12418    (See `byte_size_attribute' above).  */
12419
12420 static inline void
12421 add_bit_offset_attribute (dw_die_ref die, tree decl)
12422 {
12423   HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
12424   tree type = DECL_BIT_FIELD_TYPE (decl);
12425   HOST_WIDE_INT bitpos_int;
12426   HOST_WIDE_INT highest_order_object_bit_offset;
12427   HOST_WIDE_INT highest_order_field_bit_offset;
12428   HOST_WIDE_INT unsigned bit_offset;
12429
12430   /* Must be a field and a bit field.  */
12431   gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
12432
12433   /* We can't yet handle bit-fields whose offsets are variable, so if we
12434      encounter such things, just return without generating any attribute
12435      whatsoever.  Likewise for variable or too large size.  */
12436   if (! host_integerp (bit_position (decl), 0)
12437       || ! host_integerp (DECL_SIZE (decl), 1))
12438     return;
12439
12440   bitpos_int = int_bit_position (decl);
12441
12442   /* Note that the bit offset is always the distance (in bits) from the
12443      highest-order bit of the "containing object" to the highest-order bit of
12444      the bit-field itself.  Since the "high-order end" of any object or field
12445      is different on big-endian and little-endian machines, the computation
12446      below must take account of these differences.  */
12447   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
12448   highest_order_field_bit_offset = bitpos_int;
12449
12450   if (! BYTES_BIG_ENDIAN)
12451     {
12452       highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
12453       highest_order_object_bit_offset += simple_type_size_in_bits (type);
12454     }
12455
12456   bit_offset
12457     = (! BYTES_BIG_ENDIAN
12458        ? highest_order_object_bit_offset - highest_order_field_bit_offset
12459        : highest_order_field_bit_offset - highest_order_object_bit_offset);
12460
12461   add_AT_unsigned (die, DW_AT_bit_offset, bit_offset);
12462 }
12463
12464 /* For a FIELD_DECL node which represents a bit field, output an attribute
12465    which specifies the length in bits of the given field.  */
12466
12467 static inline void
12468 add_bit_size_attribute (dw_die_ref die, tree decl)
12469 {
12470   /* Must be a field and a bit field.  */
12471   gcc_assert (TREE_CODE (decl) == FIELD_DECL
12472               && DECL_BIT_FIELD_TYPE (decl));
12473
12474   if (host_integerp (DECL_SIZE (decl), 1))
12475     add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
12476 }
12477
12478 /* If the compiled language is ANSI C, then add a 'prototyped'
12479    attribute, if arg types are given for the parameters of a function.  */
12480
12481 static inline void
12482 add_prototyped_attribute (dw_die_ref die, tree func_type)
12483 {
12484   if (get_AT_unsigned (comp_unit_die, DW_AT_language) == DW_LANG_C89
12485       && TYPE_ARG_TYPES (func_type) != NULL)
12486     add_AT_flag (die, DW_AT_prototyped, 1);
12487 }
12488
12489 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
12490    by looking in either the type declaration or object declaration
12491    equate table.  */
12492
12493 static inline dw_die_ref
12494 add_abstract_origin_attribute (dw_die_ref die, tree origin)
12495 {
12496   dw_die_ref origin_die = NULL;
12497
12498   if (TREE_CODE (origin) != FUNCTION_DECL)
12499     {
12500       /* We may have gotten separated from the block for the inlined
12501          function, if we're in an exception handler or some such; make
12502          sure that the abstract function has been written out.
12503
12504          Doing this for nested functions is wrong, however; functions are
12505          distinct units, and our context might not even be inline.  */
12506       tree fn = origin;
12507
12508       if (TYPE_P (fn))
12509         fn = TYPE_STUB_DECL (fn);
12510
12511       fn = decl_function_context (fn);
12512       if (fn)
12513         dwarf2out_abstract_function (fn);
12514     }
12515
12516   if (DECL_P (origin))
12517     origin_die = lookup_decl_die (origin);
12518   else if (TYPE_P (origin))
12519     origin_die = lookup_type_die (origin);
12520
12521   /* XXX: Functions that are never lowered don't always have correct block
12522      trees (in the case of java, they simply have no block tree, in some other
12523      languages).  For these functions, there is nothing we can really do to
12524      output correct debug info for inlined functions in all cases.  Rather
12525      than die, we'll just produce deficient debug info now, in that we will
12526      have variables without a proper abstract origin.  In the future, when all
12527      functions are lowered, we should re-add a gcc_assert (origin_die)
12528      here.  */
12529
12530   if (origin_die)
12531     add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
12532   return origin_die;
12533 }
12534
12535 /* We do not currently support the pure_virtual attribute.  */
12536
12537 static inline void
12538 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
12539 {
12540   if (DECL_VINDEX (func_decl))
12541     {
12542       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
12543
12544       if (host_integerp (DECL_VINDEX (func_decl), 0))
12545         add_AT_loc (die, DW_AT_vtable_elem_location,
12546                     new_loc_descr (DW_OP_constu,
12547                                    tree_low_cst (DECL_VINDEX (func_decl), 0),
12548                                    0));
12549
12550       /* GNU extension: Record what type this method came from originally.  */
12551       if (debug_info_level > DINFO_LEVEL_TERSE)
12552         add_AT_die_ref (die, DW_AT_containing_type,
12553                         lookup_type_die (DECL_CONTEXT (func_decl)));
12554     }
12555 }
12556 \f
12557 /* Add source coordinate attributes for the given decl.  */
12558
12559 static void
12560 add_src_coords_attributes (dw_die_ref die, tree decl)
12561 {
12562   expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
12563
12564   add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
12565   add_AT_unsigned (die, DW_AT_decl_line, s.line);
12566 }
12567
12568 /* Add a DW_AT_name attribute and source coordinate attribute for the
12569    given decl, but only if it actually has a name.  */
12570
12571 static void
12572 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
12573 {
12574   tree decl_name;
12575
12576   decl_name = DECL_NAME (decl);
12577   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
12578     {
12579       add_name_attribute (die, dwarf2_name (decl, 0));
12580       if (! DECL_ARTIFICIAL (decl))
12581         add_src_coords_attributes (die, decl);
12582
12583       if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
12584           && TREE_PUBLIC (decl)
12585           && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
12586           && !DECL_ABSTRACT (decl)
12587           && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
12588           && !is_fortran ())
12589         add_AT_string (die, DW_AT_MIPS_linkage_name,
12590                        IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
12591     }
12592
12593 #ifdef VMS_DEBUGGING_INFO
12594   /* Get the function's name, as described by its RTL.  This may be different
12595      from the DECL_NAME name used in the source file.  */
12596   if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
12597     {
12598       add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
12599                    XEXP (DECL_RTL (decl), 0));
12600       VEC_safe_push (tree, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
12601     }
12602 #endif
12603 }
12604
12605 /* Push a new declaration scope.  */
12606
12607 static void
12608 push_decl_scope (tree scope)
12609 {
12610   VEC_safe_push (tree, gc, decl_scope_table, scope);
12611 }
12612
12613 /* Pop a declaration scope.  */
12614
12615 static inline void
12616 pop_decl_scope (void)
12617 {
12618   VEC_pop (tree, decl_scope_table);
12619 }
12620
12621 /* Return the DIE for the scope that immediately contains this type.
12622    Non-named types get global scope.  Named types nested in other
12623    types get their containing scope if it's open, or global scope
12624    otherwise.  All other types (i.e. function-local named types) get
12625    the current active scope.  */
12626
12627 static dw_die_ref
12628 scope_die_for (tree t, dw_die_ref context_die)
12629 {
12630   dw_die_ref scope_die = NULL;
12631   tree containing_scope;
12632   int i;
12633
12634   /* Non-types always go in the current scope.  */
12635   gcc_assert (TYPE_P (t));
12636
12637   containing_scope = TYPE_CONTEXT (t);
12638
12639   /* Use the containing namespace if it was passed in (for a declaration).  */
12640   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
12641     {
12642       if (context_die == lookup_decl_die (containing_scope))
12643         /* OK */;
12644       else
12645         containing_scope = NULL_TREE;
12646     }
12647
12648   /* Ignore function type "scopes" from the C frontend.  They mean that
12649      a tagged type is local to a parmlist of a function declarator, but
12650      that isn't useful to DWARF.  */
12651   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
12652     containing_scope = NULL_TREE;
12653
12654   if (containing_scope == NULL_TREE)
12655     scope_die = comp_unit_die;
12656   else if (TYPE_P (containing_scope))
12657     {
12658       /* For types, we can just look up the appropriate DIE.  But
12659          first we check to see if we're in the middle of emitting it
12660          so we know where the new DIE should go.  */
12661       for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
12662         if (VEC_index (tree, decl_scope_table, i) == containing_scope)
12663           break;
12664
12665       if (i < 0)
12666         {
12667           gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
12668                       || TREE_ASM_WRITTEN (containing_scope));
12669
12670           /* If none of the current dies are suitable, we get file scope.  */
12671           scope_die = comp_unit_die;
12672         }
12673       else
12674         scope_die = lookup_type_die (containing_scope);
12675     }
12676   else
12677     scope_die = context_die;
12678
12679   return scope_die;
12680 }
12681
12682 /* Returns nonzero if CONTEXT_DIE is internal to a function.  */
12683
12684 static inline int
12685 local_scope_p (dw_die_ref context_die)
12686 {
12687   for (; context_die; context_die = context_die->die_parent)
12688     if (context_die->die_tag == DW_TAG_inlined_subroutine
12689         || context_die->die_tag == DW_TAG_subprogram)
12690       return 1;
12691
12692   return 0;
12693 }
12694
12695 /* Returns nonzero if CONTEXT_DIE is a class.  */
12696
12697 static inline int
12698 class_scope_p (dw_die_ref context_die)
12699 {
12700   return (context_die
12701           && (context_die->die_tag == DW_TAG_structure_type
12702               || context_die->die_tag == DW_TAG_class_type
12703               || context_die->die_tag == DW_TAG_interface_type
12704               || context_die->die_tag == DW_TAG_union_type));
12705 }
12706
12707 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
12708    whether or not to treat a DIE in this context as a declaration.  */
12709
12710 static inline int
12711 class_or_namespace_scope_p (dw_die_ref context_die)
12712 {
12713   return (class_scope_p (context_die)
12714           || (context_die && context_die->die_tag == DW_TAG_namespace));
12715 }
12716
12717 /* Many forms of DIEs require a "type description" attribute.  This
12718    routine locates the proper "type descriptor" die for the type given
12719    by 'type', and adds a DW_AT_type attribute below the given die.  */
12720
12721 static void
12722 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
12723                     int decl_volatile, dw_die_ref context_die)
12724 {
12725   enum tree_code code  = TREE_CODE (type);
12726   dw_die_ref type_die  = NULL;
12727
12728   /* ??? If this type is an unnamed subrange type of an integral, floating-point
12729      or fixed-point type, use the inner type.  This is because we have no
12730      support for unnamed types in base_type_die.  This can happen if this is
12731      an Ada subrange type.  Correct solution is emit a subrange type die.  */
12732   if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
12733       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
12734     type = TREE_TYPE (type), code = TREE_CODE (type);
12735
12736   if (code == ERROR_MARK
12737       /* Handle a special case.  For functions whose return type is void, we
12738          generate *no* type attribute.  (Note that no object may have type
12739          `void', so this only applies to function return types).  */
12740       || code == VOID_TYPE)
12741     return;
12742
12743   type_die = modified_type_die (type,
12744                                 decl_const || TYPE_READONLY (type),
12745                                 decl_volatile || TYPE_VOLATILE (type),
12746                                 context_die);
12747
12748   if (type_die != NULL)
12749     add_AT_die_ref (object_die, DW_AT_type, type_die);
12750 }
12751
12752 /* Given an object die, add the calling convention attribute for the
12753    function call type.  */
12754 static void
12755 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
12756 {
12757   enum dwarf_calling_convention value = DW_CC_normal;
12758
12759   value = targetm.dwarf_calling_convention (TREE_TYPE (decl));
12760
12761   /* DWARF doesn't provide a way to identify a program's source-level
12762      entry point.  DW_AT_calling_convention attributes are only meant
12763      to describe functions' calling conventions.  However, lacking a
12764      better way to signal the Fortran main program, we use this for the
12765      time being, following existing custom.  */
12766   if (is_fortran ()
12767       && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
12768     value = DW_CC_program;
12769
12770   /* Only add the attribute if the backend requests it, and
12771      is not DW_CC_normal.  */
12772   if (value && (value != DW_CC_normal))
12773     add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
12774 }
12775
12776 /* Given a tree pointer to a struct, class, union, or enum type node, return
12777    a pointer to the (string) tag name for the given type, or zero if the type
12778    was declared without a tag.  */
12779
12780 static const char *
12781 type_tag (const_tree type)
12782 {
12783   const char *name = 0;
12784
12785   if (TYPE_NAME (type) != 0)
12786     {
12787       tree t = 0;
12788
12789       /* Find the IDENTIFIER_NODE for the type name.  */
12790       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
12791         t = TYPE_NAME (type);
12792
12793       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
12794          a TYPE_DECL node, regardless of whether or not a `typedef' was
12795          involved.  */
12796       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12797                && ! DECL_IGNORED_P (TYPE_NAME (type)))
12798         {
12799           /* We want to be extra verbose.  Don't call dwarf_name if
12800              DECL_NAME isn't set.  The default hook for decl_printable_name
12801              doesn't like that, and in this context it's correct to return
12802              0, instead of "<anonymous>" or the like.  */
12803           if (DECL_NAME (TYPE_NAME (type)))
12804             name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
12805         }
12806
12807       /* Now get the name as a string, or invent one.  */
12808       if (!name && t != 0)
12809         name = IDENTIFIER_POINTER (t);
12810     }
12811
12812   return (name == 0 || *name == '\0') ? 0 : name;
12813 }
12814
12815 /* Return the type associated with a data member, make a special check
12816    for bit field types.  */
12817
12818 static inline tree
12819 member_declared_type (const_tree member)
12820 {
12821   return (DECL_BIT_FIELD_TYPE (member)
12822           ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
12823 }
12824
12825 /* Get the decl's label, as described by its RTL. This may be different
12826    from the DECL_NAME name used in the source file.  */
12827
12828 #if 0
12829 static const char *
12830 decl_start_label (tree decl)
12831 {
12832   rtx x;
12833   const char *fnname;
12834
12835   x = DECL_RTL (decl);
12836   gcc_assert (MEM_P (x));
12837
12838   x = XEXP (x, 0);
12839   gcc_assert (GET_CODE (x) == SYMBOL_REF);
12840
12841   fnname = XSTR (x, 0);
12842   return fnname;
12843 }
12844 #endif
12845 \f
12846 /* These routines generate the internal representation of the DIE's for
12847    the compilation unit.  Debugging information is collected by walking
12848    the declaration trees passed in from dwarf2out_decl().  */
12849
12850 static void
12851 gen_array_type_die (tree type, dw_die_ref context_die)
12852 {
12853   dw_die_ref scope_die = scope_die_for (type, context_die);
12854   dw_die_ref array_die;
12855
12856   /* GNU compilers represent multidimensional array types as sequences of one
12857      dimensional array types whose element types are themselves array types.
12858      We sometimes squish that down to a single array_type DIE with multiple
12859      subscripts in the Dwarf debugging info.  The draft Dwarf specification
12860      say that we are allowed to do this kind of compression in C, because
12861      there is no difference between an array of arrays and a multidimensional
12862      array.  We don't do this for Ada to remain as close as possible to the
12863      actual representation, which is especially important against the language
12864      flexibilty wrt arrays of variable size.  */
12865
12866   bool collapse_nested_arrays = !is_ada ();
12867   tree element_type;
12868
12869   /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
12870      DW_TAG_string_type doesn't have DW_AT_type attribute).  */
12871   if (TYPE_STRING_FLAG (type)
12872       && TREE_CODE (type) == ARRAY_TYPE
12873       && is_fortran ()
12874       && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
12875     {
12876       HOST_WIDE_INT size;
12877
12878       array_die = new_die (DW_TAG_string_type, scope_die, type);
12879       add_name_attribute (array_die, type_tag (type));
12880       equate_type_number_to_die (type, array_die);
12881       size = int_size_in_bytes (type);
12882       if (size >= 0)
12883         add_AT_unsigned (array_die, DW_AT_byte_size, size);
12884       else if (TYPE_DOMAIN (type) != NULL_TREE
12885                && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
12886                && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
12887         {
12888           tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
12889           dw_loc_descr_ref loc = loc_descriptor_from_tree (szdecl);
12890
12891           size = int_size_in_bytes (TREE_TYPE (szdecl));
12892           if (loc && size > 0)
12893             {
12894               add_AT_loc (array_die, DW_AT_string_length, loc);
12895               if (size != DWARF2_ADDR_SIZE)
12896                 add_AT_unsigned (array_die, DW_AT_byte_size, size);
12897             }
12898         }
12899       return;
12900     }
12901
12902   /* ??? The SGI dwarf reader fails for array of array of enum types
12903      (e.g. const enum machine_mode insn_operand_mode[2][10]) unless the inner
12904      array type comes before the outer array type.  We thus call gen_type_die
12905      before we new_die and must prevent nested array types collapsing for this
12906      target.  */
12907
12908 #ifdef MIPS_DEBUGGING_INFO
12909   gen_type_die (TREE_TYPE (type), context_die);
12910   collapse_nested_arrays = false;
12911 #endif
12912
12913   array_die = new_die (DW_TAG_array_type, scope_die, type);
12914   add_name_attribute (array_die, type_tag (type));
12915   equate_type_number_to_die (type, array_die);
12916
12917   if (TREE_CODE (type) == VECTOR_TYPE)
12918     {
12919       /* The frontend feeds us a representation for the vector as a struct
12920          containing an array.  Pull out the array type.  */
12921       type = TREE_TYPE (TYPE_FIELDS (TYPE_DEBUG_REPRESENTATION_TYPE (type)));
12922       add_AT_flag (array_die, DW_AT_GNU_vector, 1);
12923     }
12924
12925   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
12926   if (is_fortran ()
12927       && TREE_CODE (type) == ARRAY_TYPE
12928       && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
12929       && !TYPE_STRING_FLAG (TREE_TYPE (type)))
12930     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
12931
12932 #if 0
12933   /* We default the array ordering.  SDB will probably do
12934      the right things even if DW_AT_ordering is not present.  It's not even
12935      an issue until we start to get into multidimensional arrays anyway.  If
12936      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
12937      then we'll have to put the DW_AT_ordering attribute back in.  (But if
12938      and when we find out that we need to put these in, we will only do so
12939      for multidimensional arrays.  */
12940   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
12941 #endif
12942
12943 #ifdef MIPS_DEBUGGING_INFO
12944   /* The SGI compilers handle arrays of unknown bound by setting
12945      AT_declaration and not emitting any subrange DIEs.  */
12946   if (! TYPE_DOMAIN (type))
12947     add_AT_flag (array_die, DW_AT_declaration, 1);
12948   else
12949 #endif
12950     add_subscript_info (array_die, type, collapse_nested_arrays);
12951
12952   /* Add representation of the type of the elements of this array type and
12953      emit the corresponding DIE if we haven't done it already.  */  
12954   element_type = TREE_TYPE (type);
12955   if (collapse_nested_arrays)
12956     while (TREE_CODE (element_type) == ARRAY_TYPE)
12957       {
12958         if (TYPE_STRING_FLAG (element_type) && is_fortran ())
12959           break;
12960         element_type = TREE_TYPE (element_type);
12961       }
12962
12963 #ifndef MIPS_DEBUGGING_INFO
12964   gen_type_die (element_type, context_die);
12965 #endif
12966
12967   add_type_attribute (array_die, element_type, 0, 0, context_die);
12968
12969   if (get_AT (array_die, DW_AT_name))
12970     add_pubtype (type, array_die);
12971 }
12972
12973 static dw_loc_descr_ref
12974 descr_info_loc (tree val, tree base_decl)
12975 {
12976   HOST_WIDE_INT size;
12977   dw_loc_descr_ref loc, loc2;
12978   enum dwarf_location_atom op;
12979
12980   if (val == base_decl)
12981     return new_loc_descr (DW_OP_push_object_address, 0, 0);
12982
12983   switch (TREE_CODE (val))
12984     {
12985     CASE_CONVERT:
12986       return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
12987     case VAR_DECL:
12988       return loc_descriptor_from_tree_1 (val, 0);
12989     case INTEGER_CST:
12990       if (host_integerp (val, 0))
12991         return int_loc_descriptor (tree_low_cst (val, 0));
12992       break;
12993     case INDIRECT_REF:
12994       size = int_size_in_bytes (TREE_TYPE (val));
12995       if (size < 0)
12996         break;
12997       loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
12998       if (!loc)
12999         break;
13000       if (size == DWARF2_ADDR_SIZE)
13001         add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
13002       else
13003         add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
13004       return loc;
13005     case POINTER_PLUS_EXPR:
13006     case PLUS_EXPR:
13007       if (host_integerp (TREE_OPERAND (val, 1), 1)
13008           && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
13009              < 16384)
13010         {
13011           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
13012           if (!loc)
13013             break;
13014           add_loc_descr (&loc,
13015                          new_loc_descr (DW_OP_plus_uconst,
13016                                         tree_low_cst (TREE_OPERAND (val, 1),
13017                                                       1), 0));
13018         }
13019       else
13020         {
13021           op = DW_OP_plus;
13022         do_binop:
13023           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
13024           if (!loc)
13025             break;
13026           loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
13027           if (!loc2)
13028             break;
13029           add_loc_descr (&loc, loc2);
13030           add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
13031         }
13032       return loc;
13033     case MINUS_EXPR:
13034       op = DW_OP_minus;
13035       goto do_binop;
13036     case MULT_EXPR:
13037       op = DW_OP_mul;
13038       goto do_binop;
13039     case EQ_EXPR:
13040       op = DW_OP_eq;
13041       goto do_binop;
13042     case NE_EXPR:
13043       op = DW_OP_ne;
13044       goto do_binop;
13045     default:
13046       break;
13047     }
13048   return NULL;
13049 }
13050
13051 static void
13052 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
13053                       tree val, tree base_decl)
13054 {
13055   dw_loc_descr_ref loc;
13056
13057   if (host_integerp (val, 0))
13058     {
13059       add_AT_unsigned (die, attr, tree_low_cst (val, 0));
13060       return;
13061     }
13062
13063   loc = descr_info_loc (val, base_decl);
13064   if (!loc)
13065     return;
13066
13067   add_AT_loc (die, attr, loc);
13068 }
13069
13070 /* This routine generates DIE for array with hidden descriptor, details
13071    are filled into *info by a langhook.  */
13072
13073 static void
13074 gen_descr_array_type_die (tree type, struct array_descr_info *info,
13075                           dw_die_ref context_die)
13076 {
13077   dw_die_ref scope_die = scope_die_for (type, context_die);
13078   dw_die_ref array_die;
13079   int dim;
13080
13081   array_die = new_die (DW_TAG_array_type, scope_die, type);
13082   add_name_attribute (array_die, type_tag (type));
13083   equate_type_number_to_die (type, array_die);
13084
13085   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
13086   if (is_fortran ()
13087       && info->ndimensions >= 2)
13088     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
13089
13090   if (info->data_location)
13091     add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
13092                           info->base_decl);
13093   if (info->associated)
13094     add_descr_info_field (array_die, DW_AT_associated, info->associated,
13095                           info->base_decl);
13096   if (info->allocated)
13097     add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
13098                           info->base_decl);
13099
13100   for (dim = 0; dim < info->ndimensions; dim++)
13101     {
13102       dw_die_ref subrange_die
13103         = new_die (DW_TAG_subrange_type, array_die, NULL);
13104
13105       if (info->dimen[dim].lower_bound)
13106         {
13107           /* If it is the default value, omit it.  */
13108           if ((is_c_family () || is_java ())
13109               && integer_zerop (info->dimen[dim].lower_bound))
13110             ;
13111           else if (is_fortran ()
13112                    && integer_onep (info->dimen[dim].lower_bound))
13113             ;
13114           else
13115             add_descr_info_field (subrange_die, DW_AT_lower_bound,
13116                                   info->dimen[dim].lower_bound,
13117                                   info->base_decl);
13118         }
13119       if (info->dimen[dim].upper_bound)
13120         add_descr_info_field (subrange_die, DW_AT_upper_bound,
13121                               info->dimen[dim].upper_bound,
13122                               info->base_decl);
13123       if (info->dimen[dim].stride)
13124         add_descr_info_field (subrange_die, DW_AT_byte_stride,
13125                               info->dimen[dim].stride,
13126                               info->base_decl);
13127     }
13128
13129   gen_type_die (info->element_type, context_die);
13130   add_type_attribute (array_die, info->element_type, 0, 0, context_die);
13131
13132   if (get_AT (array_die, DW_AT_name))
13133     add_pubtype (type, array_die);
13134 }
13135
13136 #if 0
13137 static void
13138 gen_entry_point_die (tree decl, dw_die_ref context_die)
13139 {
13140   tree origin = decl_ultimate_origin (decl);
13141   dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
13142
13143   if (origin != NULL)
13144     add_abstract_origin_attribute (decl_die, origin);
13145   else
13146     {
13147       add_name_and_src_coords_attributes (decl_die, decl);
13148       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
13149                           0, 0, context_die);
13150     }
13151
13152   if (DECL_ABSTRACT (decl))
13153     equate_decl_number_to_die (decl, decl_die);
13154   else
13155     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
13156 }
13157 #endif
13158
13159 /* Walk through the list of incomplete types again, trying once more to
13160    emit full debugging info for them.  */
13161
13162 static void
13163 retry_incomplete_types (void)
13164 {
13165   int i;
13166
13167   for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
13168     gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die);
13169 }
13170
13171 /* Determine what tag to use for a record type.  */
13172
13173 static enum dwarf_tag
13174 record_type_tag (tree type)
13175 {
13176   if (! lang_hooks.types.classify_record)
13177     return DW_TAG_structure_type;
13178
13179   switch (lang_hooks.types.classify_record (type))
13180     {
13181     case RECORD_IS_STRUCT:
13182       return DW_TAG_structure_type;
13183
13184     case RECORD_IS_CLASS:
13185       return DW_TAG_class_type;
13186
13187     case RECORD_IS_INTERFACE:
13188       return DW_TAG_interface_type;
13189
13190     default:
13191       gcc_unreachable ();
13192     }
13193 }
13194
13195 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
13196    include all of the information about the enumeration values also. Each
13197    enumerated type name/value is listed as a child of the enumerated type
13198    DIE.  */
13199
13200 static dw_die_ref
13201 gen_enumeration_type_die (tree type, dw_die_ref context_die)
13202 {
13203   dw_die_ref type_die = lookup_type_die (type);
13204
13205   if (type_die == NULL)
13206     {
13207       type_die = new_die (DW_TAG_enumeration_type,
13208                           scope_die_for (type, context_die), type);
13209       equate_type_number_to_die (type, type_die);
13210       add_name_attribute (type_die, type_tag (type));
13211     }
13212   else if (! TYPE_SIZE (type))
13213     return type_die;
13214   else
13215     remove_AT (type_die, DW_AT_declaration);
13216
13217   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
13218      given enum type is incomplete, do not generate the DW_AT_byte_size
13219      attribute or the DW_AT_element_list attribute.  */
13220   if (TYPE_SIZE (type))
13221     {
13222       tree link;
13223
13224       TREE_ASM_WRITTEN (type) = 1;
13225       add_byte_size_attribute (type_die, type);
13226       if (TYPE_STUB_DECL (type) != NULL_TREE)
13227         add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
13228
13229       /* If the first reference to this type was as the return type of an
13230          inline function, then it may not have a parent.  Fix this now.  */
13231       if (type_die->die_parent == NULL)
13232         add_child_die (scope_die_for (type, context_die), type_die);
13233
13234       for (link = TYPE_VALUES (type);
13235            link != NULL; link = TREE_CHAIN (link))
13236         {
13237           dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
13238           tree value = TREE_VALUE (link);
13239
13240           add_name_attribute (enum_die,
13241                               IDENTIFIER_POINTER (TREE_PURPOSE (link)));
13242
13243           if (TREE_CODE (value) == CONST_DECL)
13244             value = DECL_INITIAL (value);
13245
13246           if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
13247             /* DWARF2 does not provide a way of indicating whether or
13248                not enumeration constants are signed or unsigned.  GDB
13249                always assumes the values are signed, so we output all
13250                values as if they were signed.  That means that
13251                enumeration constants with very large unsigned values
13252                will appear to have negative values in the debugger.  */
13253             add_AT_int (enum_die, DW_AT_const_value,
13254                         tree_low_cst (value, tree_int_cst_sgn (value) > 0));
13255         }
13256     }
13257   else
13258     add_AT_flag (type_die, DW_AT_declaration, 1);
13259
13260   if (get_AT (type_die, DW_AT_name))
13261     add_pubtype (type, type_die);
13262
13263   return type_die;
13264 }
13265
13266 /* Generate a DIE to represent either a real live formal parameter decl or to
13267    represent just the type of some formal parameter position in some function
13268    type.
13269
13270    Note that this routine is a bit unusual because its argument may be a
13271    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
13272    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
13273    node.  If it's the former then this function is being called to output a
13274    DIE to represent a formal parameter object (or some inlining thereof).  If
13275    it's the latter, then this function is only being called to output a
13276    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
13277    argument type of some subprogram type.  */
13278
13279 static dw_die_ref
13280 gen_formal_parameter_die (tree node, tree origin, dw_die_ref context_die)
13281 {
13282   tree node_or_origin = node ? node : origin;
13283   dw_die_ref parm_die
13284     = new_die (DW_TAG_formal_parameter, context_die, node);
13285
13286   switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
13287     {
13288     case tcc_declaration:
13289       if (!origin)
13290         origin = decl_ultimate_origin (node);
13291       if (origin != NULL)
13292         add_abstract_origin_attribute (parm_die, origin);
13293       else
13294         {
13295           tree type = TREE_TYPE (node);
13296           add_name_and_src_coords_attributes (parm_die, node);
13297           if (DECL_BY_REFERENCE (node))
13298             add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
13299                                 context_die);
13300           else
13301             add_type_attribute (parm_die, type,
13302                                 TREE_READONLY (node),
13303                                 TREE_THIS_VOLATILE (node),
13304                                 context_die);
13305           if (DECL_ARTIFICIAL (node))
13306             add_AT_flag (parm_die, DW_AT_artificial, 1);
13307         }
13308
13309       if (node)
13310         equate_decl_number_to_die (node, parm_die);
13311       if (! DECL_ABSTRACT (node_or_origin))
13312         add_location_or_const_value_attribute (parm_die, node_or_origin,
13313                                                DW_AT_location);
13314
13315       break;
13316
13317     case tcc_type:
13318       /* We were called with some kind of a ..._TYPE node.  */
13319       add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
13320       break;
13321
13322     default:
13323       gcc_unreachable ();
13324     }
13325
13326   return parm_die;
13327 }
13328
13329 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
13330    at the end of an (ANSI prototyped) formal parameters list.  */
13331
13332 static void
13333 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
13334 {
13335   new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
13336 }
13337
13338 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
13339    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
13340    parameters as specified in some function type specification (except for
13341    those which appear as part of a function *definition*).  */
13342
13343 static void
13344 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
13345 {
13346   tree link;
13347   tree formal_type = NULL;
13348   tree first_parm_type;
13349   tree arg;
13350
13351   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
13352     {
13353       arg = DECL_ARGUMENTS (function_or_method_type);
13354       function_or_method_type = TREE_TYPE (function_or_method_type);
13355     }
13356   else
13357     arg = NULL_TREE;
13358
13359   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
13360
13361   /* Make our first pass over the list of formal parameter types and output a
13362      DW_TAG_formal_parameter DIE for each one.  */
13363   for (link = first_parm_type; link; )
13364     {
13365       dw_die_ref parm_die;
13366
13367       formal_type = TREE_VALUE (link);
13368       if (formal_type == void_type_node)
13369         break;
13370
13371       /* Output a (nameless) DIE to represent the formal parameter itself.  */
13372       parm_die = gen_formal_parameter_die (formal_type, NULL, context_die);
13373       if ((TREE_CODE (function_or_method_type) == METHOD_TYPE
13374            && link == first_parm_type)
13375           || (arg && DECL_ARTIFICIAL (arg)))
13376         add_AT_flag (parm_die, DW_AT_artificial, 1);
13377
13378       link = TREE_CHAIN (link);
13379       if (arg)
13380         arg = TREE_CHAIN (arg);
13381     }
13382
13383   /* If this function type has an ellipsis, add a
13384      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
13385   if (formal_type != void_type_node)
13386     gen_unspecified_parameters_die (function_or_method_type, context_die);
13387
13388   /* Make our second (and final) pass over the list of formal parameter types
13389      and output DIEs to represent those types (as necessary).  */
13390   for (link = TYPE_ARG_TYPES (function_or_method_type);
13391        link && TREE_VALUE (link);
13392        link = TREE_CHAIN (link))
13393     gen_type_die (TREE_VALUE (link), context_die);
13394 }
13395
13396 /* We want to generate the DIE for TYPE so that we can generate the
13397    die for MEMBER, which has been defined; we will need to refer back
13398    to the member declaration nested within TYPE.  If we're trying to
13399    generate minimal debug info for TYPE, processing TYPE won't do the
13400    trick; we need to attach the member declaration by hand.  */
13401
13402 static void
13403 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
13404 {
13405   gen_type_die (type, context_die);
13406
13407   /* If we're trying to avoid duplicate debug info, we may not have
13408      emitted the member decl for this function.  Emit it now.  */
13409   if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
13410       && ! lookup_decl_die (member))
13411     {
13412       dw_die_ref type_die;
13413       gcc_assert (!decl_ultimate_origin (member));
13414
13415       push_decl_scope (type);
13416       type_die = lookup_type_die (type);
13417       if (TREE_CODE (member) == FUNCTION_DECL)
13418         gen_subprogram_die (member, type_die);
13419       else if (TREE_CODE (member) == FIELD_DECL)
13420         {
13421           /* Ignore the nameless fields that are used to skip bits but handle
13422              C++ anonymous unions and structs.  */
13423           if (DECL_NAME (member) != NULL_TREE
13424               || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
13425               || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
13426             {
13427               gen_type_die (member_declared_type (member), type_die);
13428               gen_field_die (member, type_die);
13429             }
13430         }
13431       else
13432         gen_variable_die (member, NULL_TREE, type_die);
13433
13434       pop_decl_scope ();
13435     }
13436 }
13437
13438 /* Generate the DWARF2 info for the "abstract" instance of a function which we
13439    may later generate inlined and/or out-of-line instances of.  */
13440
13441 static void
13442 dwarf2out_abstract_function (tree decl)
13443 {
13444   dw_die_ref old_die;
13445   tree save_fn;
13446   tree context;
13447   int was_abstract = DECL_ABSTRACT (decl);
13448
13449   /* Make sure we have the actual abstract inline, not a clone.  */
13450   decl = DECL_ORIGIN (decl);
13451
13452   old_die = lookup_decl_die (decl);
13453   if (old_die && get_AT (old_die, DW_AT_inline))
13454     /* We've already generated the abstract instance.  */
13455     return;
13456
13457   /* Be sure we've emitted the in-class declaration DIE (if any) first, so
13458      we don't get confused by DECL_ABSTRACT.  */
13459   if (debug_info_level > DINFO_LEVEL_TERSE)
13460     {
13461       context = decl_class_context (decl);
13462       if (context)
13463         gen_type_die_for_member
13464           (context, decl, decl_function_context (decl) ? NULL : comp_unit_die);
13465     }
13466
13467   /* Pretend we've just finished compiling this function.  */
13468   save_fn = current_function_decl;
13469   current_function_decl = decl;
13470   push_cfun (DECL_STRUCT_FUNCTION (decl));
13471
13472   set_decl_abstract_flags (decl, 1);
13473   dwarf2out_decl (decl);
13474   if (! was_abstract)
13475     set_decl_abstract_flags (decl, 0);
13476
13477   current_function_decl = save_fn;
13478   pop_cfun ();
13479 }
13480
13481 /* Helper function of premark_used_types() which gets called through
13482    htab_traverse_resize().
13483
13484    Marks the DIE of a given type in *SLOT as perennial, so it never gets
13485    marked as unused by prune_unused_types.  */
13486 static int
13487 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
13488 {
13489   tree type;
13490   dw_die_ref die;
13491
13492   type = (tree) *slot;
13493   die = lookup_type_die (type);
13494   if (die != NULL)
13495     die->die_perennial_p = 1;
13496   return 1;
13497 }
13498
13499 /* Mark all members of used_types_hash as perennial.  */
13500 static void
13501 premark_used_types (void)
13502 {
13503   if (cfun && cfun->used_types_hash)
13504     htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
13505 }
13506
13507 /* Generate a DIE to represent a declared function (either file-scope or
13508    block-local).  */
13509
13510 static void
13511 gen_subprogram_die (tree decl, dw_die_ref context_die)
13512 {
13513   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
13514   tree origin = decl_ultimate_origin (decl);
13515   dw_die_ref subr_die;
13516   tree fn_arg_types;
13517   tree outer_scope;
13518   dw_die_ref old_die = lookup_decl_die (decl);
13519   int declaration = (current_function_decl != decl
13520                      || class_or_namespace_scope_p (context_die));
13521
13522   premark_used_types ();
13523
13524   /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
13525      started to generate the abstract instance of an inline, decided to output
13526      its containing class, and proceeded to emit the declaration of the inline
13527      from the member list for the class.  If so, DECLARATION takes priority;
13528      we'll get back to the abstract instance when done with the class.  */
13529
13530   /* The class-scope declaration DIE must be the primary DIE.  */
13531   if (origin && declaration && class_or_namespace_scope_p (context_die))
13532     {
13533       origin = NULL;
13534       gcc_assert (!old_die);
13535     }
13536
13537   /* Now that the C++ front end lazily declares artificial member fns, we
13538      might need to retrofit the declaration into its class.  */
13539   if (!declaration && !origin && !old_die
13540       && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
13541       && !class_or_namespace_scope_p (context_die)
13542       && debug_info_level > DINFO_LEVEL_TERSE)
13543     old_die = force_decl_die (decl);
13544
13545   if (origin != NULL)
13546     {
13547       gcc_assert (!declaration || local_scope_p (context_die));
13548
13549       /* Fixup die_parent for the abstract instance of a nested
13550          inline function.  */
13551       if (old_die && old_die->die_parent == NULL)
13552         add_child_die (context_die, old_die);
13553
13554       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
13555       add_abstract_origin_attribute (subr_die, origin);
13556     }
13557   else if (old_die)
13558     {
13559       expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
13560       struct dwarf_file_data * file_index = lookup_filename (s.file);
13561
13562       if (!get_AT_flag (old_die, DW_AT_declaration)
13563           /* We can have a normal definition following an inline one in the
13564              case of redefinition of GNU C extern inlines.
13565              It seems reasonable to use AT_specification in this case.  */
13566           && !get_AT (old_die, DW_AT_inline))
13567         {
13568           /* Detect and ignore this case, where we are trying to output
13569              something we have already output.  */
13570           return;
13571         }
13572
13573       /* If the definition comes from the same place as the declaration,
13574          maybe use the old DIE.  We always want the DIE for this function
13575          that has the *_pc attributes to be under comp_unit_die so the
13576          debugger can find it.  We also need to do this for abstract
13577          instances of inlines, since the spec requires the out-of-line copy
13578          to have the same parent.  For local class methods, this doesn't
13579          apply; we just use the old DIE.  */
13580       if ((old_die->die_parent == comp_unit_die || context_die == NULL)
13581           && (DECL_ARTIFICIAL (decl)
13582               || (get_AT_file (old_die, DW_AT_decl_file) == file_index
13583                   && (get_AT_unsigned (old_die, DW_AT_decl_line)
13584                       == (unsigned) s.line))))
13585         {
13586           subr_die = old_die;
13587
13588           /* Clear out the declaration attribute and the formal parameters.
13589              Do not remove all children, because it is possible that this
13590              declaration die was forced using force_decl_die(). In such
13591              cases die that forced declaration die (e.g. TAG_imported_module)
13592              is one of the children that we do not want to remove.  */
13593           remove_AT (subr_die, DW_AT_declaration);
13594           remove_child_TAG (subr_die, DW_TAG_formal_parameter);
13595         }
13596       else
13597         {
13598           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
13599           add_AT_specification (subr_die, old_die);
13600           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
13601             add_AT_file (subr_die, DW_AT_decl_file, file_index);
13602           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
13603             add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
13604         }
13605     }
13606   else
13607     {
13608       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
13609
13610       if (TREE_PUBLIC (decl))
13611         add_AT_flag (subr_die, DW_AT_external, 1);
13612
13613       add_name_and_src_coords_attributes (subr_die, decl);
13614       if (debug_info_level > DINFO_LEVEL_TERSE)
13615         {
13616           add_prototyped_attribute (subr_die, TREE_TYPE (decl));
13617           add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
13618                               0, 0, context_die);
13619         }
13620
13621       add_pure_or_virtual_attribute (subr_die, decl);
13622       if (DECL_ARTIFICIAL (decl))
13623         add_AT_flag (subr_die, DW_AT_artificial, 1);
13624
13625       if (TREE_PROTECTED (decl))
13626         add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_protected);
13627       else if (TREE_PRIVATE (decl))
13628         add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_private);
13629     }
13630
13631   if (declaration)
13632     {
13633       if (!old_die || !get_AT (old_die, DW_AT_inline))
13634         {
13635           add_AT_flag (subr_die, DW_AT_declaration, 1);
13636
13637           /* If this is an explicit function declaration then generate
13638              a DW_AT_explicit attribute.  */
13639           if (lang_hooks.decls.function_decl_explicit_p (decl))
13640             add_AT_flag (subr_die, DW_AT_explicit, 1);
13641
13642           /* The first time we see a member function, it is in the context of
13643              the class to which it belongs.  We make sure of this by emitting
13644              the class first.  The next time is the definition, which is
13645              handled above.  The two may come from the same source text.
13646
13647              Note that force_decl_die() forces function declaration die. It is
13648              later reused to represent definition.  */
13649           equate_decl_number_to_die (decl, subr_die);
13650         }
13651     }
13652   else if (DECL_ABSTRACT (decl))
13653     {
13654       if (DECL_DECLARED_INLINE_P (decl))
13655         {
13656           if (cgraph_function_possibly_inlined_p (decl))
13657             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
13658           else
13659             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
13660         }
13661       else
13662         {
13663           if (cgraph_function_possibly_inlined_p (decl))
13664             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
13665           else
13666             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
13667         }
13668
13669       if (DECL_DECLARED_INLINE_P (decl)
13670           && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
13671         add_AT_flag (subr_die, DW_AT_artificial, 1);
13672
13673       equate_decl_number_to_die (decl, subr_die);
13674     }
13675   else if (!DECL_EXTERNAL (decl))
13676     {
13677       HOST_WIDE_INT cfa_fb_offset;
13678
13679       if (!old_die || !get_AT (old_die, DW_AT_inline))
13680         equate_decl_number_to_die (decl, subr_die);
13681
13682       if (!flag_reorder_blocks_and_partition)
13683         {
13684           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
13685                                        current_function_funcdef_no);
13686           add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
13687           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
13688                                        current_function_funcdef_no);
13689           add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
13690
13691           add_pubname (decl, subr_die);
13692           add_arange (decl, subr_die);
13693         }
13694       else
13695         {  /* Do nothing for now; maybe need to duplicate die, one for
13696               hot section and one for cold section, then use the hot/cold
13697               section begin/end labels to generate the aranges...  */
13698           /*
13699             add_AT_lbl_id (subr_die, DW_AT_low_pc, hot_section_label);
13700             add_AT_lbl_id (subr_die, DW_AT_high_pc, hot_section_end_label);
13701             add_AT_lbl_id (subr_die, DW_AT_lo_user, unlikely_section_label);
13702             add_AT_lbl_id (subr_die, DW_AT_hi_user, cold_section_end_label);
13703
13704             add_pubname (decl, subr_die);
13705             add_arange (decl, subr_die);
13706             add_arange (decl, subr_die);
13707            */
13708         }
13709
13710 #ifdef MIPS_DEBUGGING_INFO
13711       /* Add a reference to the FDE for this routine.  */
13712       add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
13713 #endif
13714
13715       cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
13716
13717       /* We define the "frame base" as the function's CFA.  This is more
13718          convenient for several reasons: (1) It's stable across the prologue
13719          and epilogue, which makes it better than just a frame pointer,
13720          (2) With dwarf3, there exists a one-byte encoding that allows us
13721          to reference the .debug_frame data by proxy, but failing that,
13722          (3) We can at least reuse the code inspection and interpretation
13723          code that determines the CFA position at various points in the
13724          function.  */
13725       /* ??? Use some command-line or configury switch to enable the use
13726          of dwarf3 DW_OP_call_frame_cfa.  At present there are no dwarf
13727          consumers that understand it; fall back to "pure" dwarf2 and
13728          convert the CFA data into a location list.  */
13729       {
13730         dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
13731         if (list->dw_loc_next)
13732           add_AT_loc_list (subr_die, DW_AT_frame_base, list);
13733         else
13734           add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
13735       }
13736
13737       /* Compute a displacement from the "steady-state frame pointer" to
13738          the CFA.  The former is what all stack slots and argument slots
13739          will reference in the rtl; the later is what we've told the
13740          debugger about.  We'll need to adjust all frame_base references
13741          by this displacement.  */
13742       compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
13743
13744       if (cfun->static_chain_decl)
13745         add_AT_location_description (subr_die, DW_AT_static_link,
13746                  loc_descriptor_from_tree (cfun->static_chain_decl));
13747     }
13748
13749   /* Now output descriptions of the arguments for this function. This gets
13750      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
13751      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
13752      `...' at the end of the formal parameter list.  In order to find out if
13753      there was a trailing ellipsis or not, we must instead look at the type
13754      associated with the FUNCTION_DECL.  This will be a node of type
13755      FUNCTION_TYPE. If the chain of type nodes hanging off of this
13756      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
13757      an ellipsis at the end.  */
13758
13759   /* In the case where we are describing a mere function declaration, all we
13760      need to do here (and all we *can* do here) is to describe the *types* of
13761      its formal parameters.  */
13762   if (debug_info_level <= DINFO_LEVEL_TERSE)
13763     ;
13764   else if (declaration)
13765     gen_formal_types_die (decl, subr_die);
13766   else
13767     {
13768       /* Generate DIEs to represent all known formal parameters.  */
13769       tree arg_decls = DECL_ARGUMENTS (decl);
13770       tree parm;
13771
13772       /* When generating DIEs, generate the unspecified_parameters DIE
13773          instead if we come across the arg "__builtin_va_alist" */
13774       for (parm = arg_decls; parm; parm = TREE_CHAIN (parm))
13775         if (TREE_CODE (parm) == PARM_DECL)
13776           {
13777             if (DECL_NAME (parm)
13778                 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (parm)),
13779                             "__builtin_va_alist"))
13780               gen_unspecified_parameters_die (parm, subr_die);
13781             else
13782               gen_decl_die (parm, NULL, subr_die);
13783           }
13784
13785       /* Decide whether we need an unspecified_parameters DIE at the end.
13786          There are 2 more cases to do this for: 1) the ansi ... declaration -
13787          this is detectable when the end of the arg list is not a
13788          void_type_node 2) an unprototyped function declaration (not a
13789          definition).  This just means that we have no info about the
13790          parameters at all.  */
13791       fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
13792       if (fn_arg_types != NULL)
13793         {
13794           /* This is the prototyped case, check for....  */
13795           if (TREE_VALUE (tree_last (fn_arg_types)) != void_type_node)
13796             gen_unspecified_parameters_die (decl, subr_die);
13797         }
13798       else if (DECL_INITIAL (decl) == NULL_TREE)
13799         gen_unspecified_parameters_die (decl, subr_die);
13800     }
13801
13802   /* Output Dwarf info for all of the stuff within the body of the function
13803      (if it has one - it may be just a declaration).  */
13804   outer_scope = DECL_INITIAL (decl);
13805
13806   /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
13807      a function.  This BLOCK actually represents the outermost binding contour
13808      for the function, i.e. the contour in which the function's formal
13809      parameters and labels get declared. Curiously, it appears that the front
13810      end doesn't actually put the PARM_DECL nodes for the current function onto
13811      the BLOCK_VARS list for this outer scope, but are strung off of the
13812      DECL_ARGUMENTS list for the function instead.
13813
13814      The BLOCK_VARS list for the `outer_scope' does provide us with a list of
13815      the LABEL_DECL nodes for the function however, and we output DWARF info
13816      for those in decls_for_scope.  Just within the `outer_scope' there will be
13817      a BLOCK node representing the function's outermost pair of curly braces,
13818      and any blocks used for the base and member initializers of a C++
13819      constructor function.  */
13820   if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
13821     {
13822       /* Emit a DW_TAG_variable DIE for a named return value.  */
13823       if (DECL_NAME (DECL_RESULT (decl)))
13824         gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
13825
13826       current_function_has_inlines = 0;
13827       decls_for_scope (outer_scope, subr_die, 0);
13828
13829 #if 0 && defined (MIPS_DEBUGGING_INFO)
13830       if (current_function_has_inlines)
13831         {
13832           add_AT_flag (subr_die, DW_AT_MIPS_has_inlines, 1);
13833           if (! comp_unit_has_inlines)
13834             {
13835               add_AT_flag (comp_unit_die, DW_AT_MIPS_has_inlines, 1);
13836               comp_unit_has_inlines = 1;
13837             }
13838         }
13839 #endif
13840     }
13841   /* Add the calling convention attribute if requested.  */
13842   add_calling_convention_attribute (subr_die, decl);
13843
13844 }
13845
13846 /* Returns a hash value for X (which really is a die_struct).  */
13847
13848 static hashval_t
13849 common_block_die_table_hash (const void *x)
13850 {
13851   const_dw_die_ref d = (const_dw_die_ref) x;
13852   return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
13853 }
13854
13855 /* Return nonzero if decl_id and die_parent of die_struct X is the same
13856    as decl_id and die_parent of die_struct Y.  */
13857
13858 static int
13859 common_block_die_table_eq (const void *x, const void *y)
13860 {
13861   const_dw_die_ref d = (const_dw_die_ref) x;
13862   const_dw_die_ref e = (const_dw_die_ref) y;
13863   return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
13864 }
13865
13866 /* Generate a DIE to represent a declared data object.
13867    Either DECL or ORIGIN must be non-null.  */
13868
13869 static void
13870 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
13871 {
13872   HOST_WIDE_INT off;
13873   tree com_decl;
13874   tree decl_or_origin = decl ? decl : origin;
13875   dw_die_ref var_die;
13876   dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
13877   dw_die_ref origin_die;
13878   int declaration = (DECL_EXTERNAL (decl_or_origin)
13879                      /* If DECL is COMDAT and has not actually been
13880                         emitted, we cannot take its address; there
13881                         might end up being no definition anywhere in
13882                         the program.  For example, consider the C++
13883                         test case:
13884
13885                           template <class T>
13886                           struct S { static const int i = 7; };
13887
13888                           template <class T>
13889                           const int S<T>::i;
13890
13891                           int f() { return S<int>::i; }
13892
13893                         Here, S<int>::i is not DECL_EXTERNAL, but no
13894                         definition is required, so the compiler will
13895                         not emit a definition.  */
13896                      || (TREE_CODE (decl_or_origin) == VAR_DECL
13897                          && DECL_COMDAT (decl_or_origin)
13898                          && !TREE_ASM_WRITTEN (decl_or_origin))
13899                      || class_or_namespace_scope_p (context_die));
13900
13901   if (!origin)
13902     origin = decl_ultimate_origin (decl);
13903
13904   com_decl = fortran_common (decl_or_origin, &off);
13905
13906   /* Symbol in common gets emitted as a child of the common block, in the form
13907      of a data member.  */
13908   if (com_decl)
13909     {
13910       tree field;
13911       dw_die_ref com_die;
13912       dw_loc_descr_ref loc;
13913       die_node com_die_arg;
13914
13915       var_die = lookup_decl_die (decl_or_origin);
13916       if (var_die)
13917         {
13918           if (get_AT (var_die, DW_AT_location) == NULL)
13919             {
13920               loc = loc_descriptor_from_tree (com_decl);
13921               if (loc)
13922                 {
13923                   if (off)
13924                     {
13925                       /* Optimize the common case.  */
13926                       if (loc->dw_loc_opc == DW_OP_addr
13927                           && loc->dw_loc_next == NULL
13928                           && GET_CODE (loc->dw_loc_oprnd1.v.val_addr)
13929                              == SYMBOL_REF)
13930                         loc->dw_loc_oprnd1.v.val_addr
13931                           = plus_constant (loc->dw_loc_oprnd1.v.val_addr, off);
13932                         else
13933                           add_loc_descr (&loc,
13934                                          new_loc_descr (DW_OP_plus_uconst,
13935                                                         off, 0));
13936                     }
13937                   add_AT_loc (var_die, DW_AT_location, loc);
13938                   remove_AT (var_die, DW_AT_declaration);
13939                 }
13940             }
13941           return;
13942         }
13943
13944       if (common_block_die_table == NULL)
13945         common_block_die_table
13946           = htab_create_ggc (10, common_block_die_table_hash,
13947                              common_block_die_table_eq, NULL);
13948
13949       field = TREE_OPERAND (DECL_VALUE_EXPR (decl), 0);
13950       com_die_arg.decl_id = DECL_UID (com_decl);
13951       com_die_arg.die_parent = context_die;
13952       com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
13953       loc = loc_descriptor_from_tree (com_decl);
13954       if (com_die == NULL)
13955         {
13956           const char *cnam
13957             = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
13958           void **slot;
13959
13960           com_die = new_die (DW_TAG_common_block, context_die, decl);
13961           add_name_and_src_coords_attributes (com_die, com_decl);
13962           if (loc)
13963             {
13964               add_AT_loc (com_die, DW_AT_location, loc);
13965               /* Avoid sharing the same loc descriptor between
13966                  DW_TAG_common_block and DW_TAG_variable.  */
13967               loc = loc_descriptor_from_tree (com_decl);
13968             }
13969           else if (DECL_EXTERNAL (decl))
13970             add_AT_flag (com_die, DW_AT_declaration, 1);
13971           add_pubname_string (cnam, com_die); /* ??? needed? */
13972           com_die->decl_id = DECL_UID (com_decl);
13973           slot = htab_find_slot (common_block_die_table, com_die, INSERT);
13974           *slot = (void *) com_die;
13975         }
13976       else if (get_AT (com_die, DW_AT_location) == NULL && loc)
13977         {
13978           add_AT_loc (com_die, DW_AT_location, loc);
13979           loc = loc_descriptor_from_tree (com_decl);
13980           remove_AT (com_die, DW_AT_declaration);
13981         }
13982       var_die = new_die (DW_TAG_variable, com_die, decl);
13983       add_name_and_src_coords_attributes (var_die, decl);
13984       add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
13985                           TREE_THIS_VOLATILE (decl), context_die);
13986       add_AT_flag (var_die, DW_AT_external, 1);
13987       if (loc)
13988         {
13989           if (off)
13990             {
13991               /* Optimize the common case.  */
13992               if (loc->dw_loc_opc == DW_OP_addr
13993                   && loc->dw_loc_next == NULL
13994                   && GET_CODE (loc->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
13995                 loc->dw_loc_oprnd1.v.val_addr
13996                   = plus_constant (loc->dw_loc_oprnd1.v.val_addr, off);
13997               else
13998                 add_loc_descr (&loc, new_loc_descr (DW_OP_plus_uconst,
13999                                                     off, 0));
14000             }
14001           add_AT_loc (var_die, DW_AT_location, loc);
14002         }
14003       else if (DECL_EXTERNAL (decl))
14004         add_AT_flag (var_die, DW_AT_declaration, 1);
14005       equate_decl_number_to_die (decl, var_die);
14006       return;
14007     }
14008
14009   /* If the compiler emitted a definition for the DECL declaration
14010      and if we already emitted a DIE for it, don't emit a second
14011      DIE for it again.  */
14012   if (old_die
14013       && declaration
14014       && old_die->die_parent == context_die)
14015     return;
14016
14017   /* For static data members, the declaration in the class is supposed
14018      to have DW_TAG_member tag; the specification should still be
14019      DW_TAG_variable referencing the DW_TAG_member DIE.  */
14020   if (declaration && class_scope_p (context_die))
14021     var_die = new_die (DW_TAG_member, context_die, decl);
14022   else
14023     var_die = new_die (DW_TAG_variable, context_die, decl);
14024
14025   origin_die = NULL;
14026   if (origin != NULL)
14027     origin_die = add_abstract_origin_attribute (var_die, origin);
14028
14029   /* Loop unrolling can create multiple blocks that refer to the same
14030      static variable, so we must test for the DW_AT_declaration flag.
14031
14032      ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
14033      copy decls and set the DECL_ABSTRACT flag on them instead of
14034      sharing them.
14035
14036      ??? Duplicated blocks have been rewritten to use .debug_ranges.
14037
14038      ??? The declare_in_namespace support causes us to get two DIEs for one
14039      variable, both of which are declarations.  We want to avoid considering
14040      one to be a specification, so we must test that this DIE is not a
14041      declaration.  */
14042   else if (old_die && TREE_STATIC (decl) && ! declaration
14043            && get_AT_flag (old_die, DW_AT_declaration) == 1)
14044     {
14045       /* This is a definition of a C++ class level static.  */
14046       add_AT_specification (var_die, old_die);
14047       if (DECL_NAME (decl))
14048         {
14049           expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
14050           struct dwarf_file_data * file_index = lookup_filename (s.file);
14051
14052           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
14053             add_AT_file (var_die, DW_AT_decl_file, file_index);
14054
14055           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
14056             add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
14057         }
14058     }
14059   else
14060     {
14061       tree type = TREE_TYPE (decl);
14062
14063       add_name_and_src_coords_attributes (var_die, decl);
14064       if ((TREE_CODE (decl) == PARM_DECL
14065            || TREE_CODE (decl) == RESULT_DECL)
14066           && DECL_BY_REFERENCE (decl))
14067         add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
14068       else
14069         add_type_attribute (var_die, type, TREE_READONLY (decl),
14070                             TREE_THIS_VOLATILE (decl), context_die);
14071
14072       if (TREE_PUBLIC (decl))
14073         add_AT_flag (var_die, DW_AT_external, 1);
14074
14075       if (DECL_ARTIFICIAL (decl))
14076         add_AT_flag (var_die, DW_AT_artificial, 1);
14077
14078       if (TREE_PROTECTED (decl))
14079         add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_protected);
14080       else if (TREE_PRIVATE (decl))
14081         add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_private);
14082     }
14083
14084   if (declaration)
14085     add_AT_flag (var_die, DW_AT_declaration, 1);
14086
14087   if (decl && (DECL_ABSTRACT (decl) || declaration))
14088     equate_decl_number_to_die (decl, var_die);
14089
14090   if (! declaration
14091       && (! DECL_ABSTRACT (decl_or_origin)
14092           /* Local static vars are shared between all clones/inlines,
14093              so emit DW_AT_location on the abstract DIE if DECL_RTL is
14094              already set.  */
14095           || (TREE_CODE (decl_or_origin) == VAR_DECL
14096               && TREE_STATIC (decl_or_origin)
14097               && DECL_RTL_SET_P (decl_or_origin)))
14098       /* When abstract origin already has DW_AT_location attribute, no need
14099          to add it again.  */
14100       && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
14101     {
14102       if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
14103           && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
14104         defer_location (decl_or_origin, var_die);
14105       else
14106         add_location_or_const_value_attribute (var_die,
14107                                                decl_or_origin,
14108                                                DW_AT_location);
14109       add_pubname (decl_or_origin, var_die);
14110     }
14111   else
14112     tree_add_const_value_attribute (var_die, decl_or_origin);
14113 }
14114
14115 /* Generate a DIE to represent a named constant.  */
14116
14117 static void
14118 gen_const_die (tree decl, dw_die_ref context_die)
14119 {
14120   dw_die_ref const_die;
14121   tree type = TREE_TYPE (decl);
14122
14123   const_die = new_die (DW_TAG_constant, context_die, decl);
14124   add_name_and_src_coords_attributes (const_die, decl);
14125   add_type_attribute (const_die, type, 1, 0, context_die);
14126   if (TREE_PUBLIC (decl))
14127     add_AT_flag (const_die, DW_AT_external, 1);
14128   if (DECL_ARTIFICIAL (decl))
14129     add_AT_flag (const_die, DW_AT_artificial, 1);
14130   tree_add_const_value_attribute (const_die, decl);
14131 }
14132
14133 /* Generate a DIE to represent a label identifier.  */
14134
14135 static void
14136 gen_label_die (tree decl, dw_die_ref context_die)
14137 {
14138   tree origin = decl_ultimate_origin (decl);
14139   dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
14140   rtx insn;
14141   char label[MAX_ARTIFICIAL_LABEL_BYTES];
14142
14143   if (origin != NULL)
14144     add_abstract_origin_attribute (lbl_die, origin);
14145   else
14146     add_name_and_src_coords_attributes (lbl_die, decl);
14147
14148   if (DECL_ABSTRACT (decl))
14149     equate_decl_number_to_die (decl, lbl_die);
14150   else
14151     {
14152       insn = DECL_RTL_IF_SET (decl);
14153
14154       /* Deleted labels are programmer specified labels which have been
14155          eliminated because of various optimizations.  We still emit them
14156          here so that it is possible to put breakpoints on them.  */
14157       if (insn
14158           && (LABEL_P (insn)
14159               || ((NOTE_P (insn)
14160                    && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
14161         {
14162           /* When optimization is enabled (via -O) some parts of the compiler
14163              (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
14164              represent source-level labels which were explicitly declared by
14165              the user.  This really shouldn't be happening though, so catch
14166              it if it ever does happen.  */
14167           gcc_assert (!INSN_DELETED_P (insn));
14168
14169           ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
14170           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
14171         }
14172     }
14173 }
14174
14175 /* A helper function for gen_inlined_subroutine_die.  Add source coordinate
14176    attributes to the DIE for a block STMT, to describe where the inlined
14177    function was called from.  This is similar to add_src_coords_attributes.  */
14178
14179 static inline void
14180 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
14181 {
14182   expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
14183
14184   add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
14185   add_AT_unsigned (die, DW_AT_call_line, s.line);
14186 }
14187
14188
14189 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
14190    Add low_pc and high_pc attributes to the DIE for a block STMT.  */
14191
14192 static inline void
14193 add_high_low_attributes (tree stmt, dw_die_ref die)
14194 {
14195   char label[MAX_ARTIFICIAL_LABEL_BYTES];
14196
14197   if (BLOCK_FRAGMENT_CHAIN (stmt))
14198     {
14199       tree chain;
14200
14201       if (inlined_function_outer_scope_p (stmt))
14202         {
14203           ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
14204                                        BLOCK_NUMBER (stmt));
14205           add_AT_lbl_id (die, DW_AT_entry_pc, label);
14206         }
14207
14208       add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
14209
14210       chain = BLOCK_FRAGMENT_CHAIN (stmt);
14211       do
14212         {
14213           add_ranges (chain);
14214           chain = BLOCK_FRAGMENT_CHAIN (chain);
14215         }
14216       while (chain);
14217       add_ranges (NULL);
14218     }
14219   else
14220     {
14221       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
14222                                    BLOCK_NUMBER (stmt));
14223       add_AT_lbl_id (die, DW_AT_low_pc, label);
14224       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
14225                                    BLOCK_NUMBER (stmt));
14226       add_AT_lbl_id (die, DW_AT_high_pc, label);
14227     }
14228 }
14229
14230 /* Generate a DIE for a lexical block.  */
14231
14232 static void
14233 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
14234 {
14235   dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
14236
14237   if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
14238     add_high_low_attributes (stmt, stmt_die);
14239
14240   decls_for_scope (stmt, stmt_die, depth);
14241 }
14242
14243 /* Generate a DIE for an inlined subprogram.  */
14244
14245 static void
14246 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
14247 {
14248   tree decl = block_ultimate_origin (stmt);
14249
14250   /* Emit info for the abstract instance first, if we haven't yet.  We
14251      must emit this even if the block is abstract, otherwise when we
14252      emit the block below (or elsewhere), we may end up trying to emit
14253      a die whose origin die hasn't been emitted, and crashing.  */
14254   dwarf2out_abstract_function (decl);
14255
14256   if (! BLOCK_ABSTRACT (stmt))
14257     {
14258       dw_die_ref subr_die
14259         = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
14260
14261       add_abstract_origin_attribute (subr_die, decl);
14262       if (TREE_ASM_WRITTEN (stmt))
14263         add_high_low_attributes (stmt, subr_die);
14264       add_call_src_coords_attributes (stmt, subr_die);
14265
14266       decls_for_scope (stmt, subr_die, depth);
14267       current_function_has_inlines = 1;
14268     }
14269   else
14270     /* We may get here if we're the outer block of function A that was
14271        inlined into function B that was inlined into function C.  When
14272        generating debugging info for C, dwarf2out_abstract_function(B)
14273        would mark all inlined blocks as abstract, including this one.
14274        So, we wouldn't (and shouldn't) expect labels to be generated
14275        for this one.  Instead, just emit debugging info for
14276        declarations within the block.  This is particularly important
14277        in the case of initializers of arguments passed from B to us:
14278        if they're statement expressions containing declarations, we
14279        wouldn't generate dies for their abstract variables, and then,
14280        when generating dies for the real variables, we'd die (pun
14281        intended :-)  */
14282     gen_lexical_block_die (stmt, context_die, depth);
14283 }
14284
14285 /* Generate a DIE for a field in a record, or structure.  */
14286
14287 static void
14288 gen_field_die (tree decl, dw_die_ref context_die)
14289 {
14290   dw_die_ref decl_die;
14291
14292   if (TREE_TYPE (decl) == error_mark_node)
14293     return;
14294
14295   decl_die = new_die (DW_TAG_member, context_die, decl);
14296   add_name_and_src_coords_attributes (decl_die, decl);
14297   add_type_attribute (decl_die, member_declared_type (decl),
14298                       TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
14299                       context_die);
14300
14301   if (DECL_BIT_FIELD_TYPE (decl))
14302     {
14303       add_byte_size_attribute (decl_die, decl);
14304       add_bit_size_attribute (decl_die, decl);
14305       add_bit_offset_attribute (decl_die, decl);
14306     }
14307
14308   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
14309     add_data_member_location_attribute (decl_die, decl);
14310
14311   if (DECL_ARTIFICIAL (decl))
14312     add_AT_flag (decl_die, DW_AT_artificial, 1);
14313
14314   if (TREE_PROTECTED (decl))
14315     add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_protected);
14316   else if (TREE_PRIVATE (decl))
14317     add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_private);
14318
14319   /* Equate decl number to die, so that we can look up this decl later on.  */
14320   equate_decl_number_to_die (decl, decl_die);
14321 }
14322
14323 #if 0
14324 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
14325    Use modified_type_die instead.
14326    We keep this code here just in case these types of DIEs may be needed to
14327    represent certain things in other languages (e.g. Pascal) someday.  */
14328
14329 static void
14330 gen_pointer_type_die (tree type, dw_die_ref context_die)
14331 {
14332   dw_die_ref ptr_die
14333     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
14334
14335   equate_type_number_to_die (type, ptr_die);
14336   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
14337   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
14338 }
14339
14340 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
14341    Use modified_type_die instead.
14342    We keep this code here just in case these types of DIEs may be needed to
14343    represent certain things in other languages (e.g. Pascal) someday.  */
14344
14345 static void
14346 gen_reference_type_die (tree type, dw_die_ref context_die)
14347 {
14348   dw_die_ref ref_die
14349     = new_die (DW_TAG_reference_type, scope_die_for (type, context_die), type);
14350
14351   equate_type_number_to_die (type, ref_die);
14352   add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
14353   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
14354 }
14355 #endif
14356
14357 /* Generate a DIE for a pointer to a member type.  */
14358
14359 static void
14360 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
14361 {
14362   dw_die_ref ptr_die
14363     = new_die (DW_TAG_ptr_to_member_type,
14364                scope_die_for (type, context_die), type);
14365
14366   equate_type_number_to_die (type, ptr_die);
14367   add_AT_die_ref (ptr_die, DW_AT_containing_type,
14368                   lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
14369   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
14370 }
14371
14372 /* Generate the DIE for the compilation unit.  */
14373
14374 static dw_die_ref
14375 gen_compile_unit_die (const char *filename)
14376 {
14377   dw_die_ref die;
14378   char producer[250];
14379   const char *language_string = lang_hooks.name;
14380   int language;
14381
14382   die = new_die (DW_TAG_compile_unit, NULL, NULL);
14383
14384   if (filename)
14385     {
14386       add_name_attribute (die, filename);
14387       /* Don't add cwd for <built-in>.  */
14388       if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
14389         add_comp_dir_attribute (die);
14390     }
14391
14392   sprintf (producer, "%s %s", language_string, version_string);
14393
14394 #ifdef MIPS_DEBUGGING_INFO
14395   /* The MIPS/SGI compilers place the 'cc' command line options in the producer
14396      string.  The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
14397      not appear in the producer string, the debugger reaches the conclusion
14398      that the object file is stripped and has no debugging information.
14399      To get the MIPS/SGI debugger to believe that there is debugging
14400      information in the object file, we add a -g to the producer string.  */
14401   if (debug_info_level > DINFO_LEVEL_TERSE)
14402     strcat (producer, " -g");
14403 #endif
14404
14405   add_AT_string (die, DW_AT_producer, producer);
14406
14407   if (strcmp (language_string, "GNU C++") == 0)
14408     language = DW_LANG_C_plus_plus;
14409   else if (strcmp (language_string, "GNU Ada") == 0)
14410     language = DW_LANG_Ada95;
14411   else if (strcmp (language_string, "GNU F77") == 0)
14412     language = DW_LANG_Fortran77;
14413   else if (strcmp (language_string, "GNU Fortran") == 0)
14414     language = DW_LANG_Fortran95;
14415   else if (strcmp (language_string, "GNU Pascal") == 0)
14416     language = DW_LANG_Pascal83;
14417   else if (strcmp (language_string, "GNU Java") == 0)
14418     language = DW_LANG_Java;
14419   else if (strcmp (language_string, "GNU Objective-C") == 0)
14420     language = DW_LANG_ObjC;
14421   else if (strcmp (language_string, "GNU Objective-C++") == 0)
14422     language = DW_LANG_ObjC_plus_plus;
14423   else
14424     language = DW_LANG_C89;
14425
14426   add_AT_unsigned (die, DW_AT_language, language);
14427   return die;
14428 }
14429
14430 /* Generate the DIE for a base class.  */
14431
14432 static void
14433 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
14434 {
14435   dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
14436
14437   add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
14438   add_data_member_location_attribute (die, binfo);
14439
14440   if (BINFO_VIRTUAL_P (binfo))
14441     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
14442
14443   if (access == access_public_node)
14444     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
14445   else if (access == access_protected_node)
14446     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
14447 }
14448
14449 /* Generate a DIE for a class member.  */
14450
14451 static void
14452 gen_member_die (tree type, dw_die_ref context_die)
14453 {
14454   tree member;
14455   tree binfo = TYPE_BINFO (type);
14456   dw_die_ref child;
14457
14458   /* If this is not an incomplete type, output descriptions of each of its
14459      members. Note that as we output the DIEs necessary to represent the
14460      members of this record or union type, we will also be trying to output
14461      DIEs to represent the *types* of those members. However the `type'
14462      function (above) will specifically avoid generating type DIEs for member
14463      types *within* the list of member DIEs for this (containing) type except
14464      for those types (of members) which are explicitly marked as also being
14465      members of this (containing) type themselves.  The g++ front- end can
14466      force any given type to be treated as a member of some other (containing)
14467      type by setting the TYPE_CONTEXT of the given (member) type to point to
14468      the TREE node representing the appropriate (containing) type.  */
14469
14470   /* First output info about the base classes.  */
14471   if (binfo)
14472     {
14473       VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
14474       int i;
14475       tree base;
14476
14477       for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
14478         gen_inheritance_die (base,
14479                              (accesses ? VEC_index (tree, accesses, i)
14480                               : access_public_node), context_die);
14481     }
14482
14483   /* Now output info about the data members and type members.  */
14484   for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member))
14485     {
14486       /* If we thought we were generating minimal debug info for TYPE
14487          and then changed our minds, some of the member declarations
14488          may have already been defined.  Don't define them again, but
14489          do put them in the right order.  */
14490
14491       child = lookup_decl_die (member);
14492       if (child)
14493         splice_child_die (context_die, child);
14494       else
14495         gen_decl_die (member, NULL, context_die);
14496     }
14497
14498   /* Now output info about the function members (if any).  */
14499   for (member = TYPE_METHODS (type); member; member = TREE_CHAIN (member))
14500     {
14501       /* Don't include clones in the member list.  */
14502       if (DECL_ABSTRACT_ORIGIN (member))
14503         continue;
14504
14505       child = lookup_decl_die (member);
14506       if (child)
14507         splice_child_die (context_die, child);
14508       else
14509         gen_decl_die (member, NULL, context_die);
14510     }
14511 }
14512
14513 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
14514    is set, we pretend that the type was never defined, so we only get the
14515    member DIEs needed by later specification DIEs.  */
14516
14517 static void
14518 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
14519                                 enum debug_info_usage usage)
14520 {
14521   dw_die_ref type_die = lookup_type_die (type);
14522   dw_die_ref scope_die = 0;
14523   int nested = 0;
14524   int complete = (TYPE_SIZE (type)
14525                   && (! TYPE_STUB_DECL (type)
14526                       || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
14527   int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
14528   complete = complete && should_emit_struct_debug (type, usage);
14529
14530   if (type_die && ! complete)
14531     return;
14532
14533   if (TYPE_CONTEXT (type) != NULL_TREE
14534       && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
14535           || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
14536     nested = 1;
14537
14538   scope_die = scope_die_for (type, context_die);
14539
14540   if (! type_die || (nested && scope_die == comp_unit_die))
14541     /* First occurrence of type or toplevel definition of nested class.  */
14542     {
14543       dw_die_ref old_die = type_die;
14544
14545       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
14546                           ? record_type_tag (type) : DW_TAG_union_type,
14547                           scope_die, type);
14548       equate_type_number_to_die (type, type_die);
14549       if (old_die)
14550         add_AT_specification (type_die, old_die);
14551       else
14552         add_name_attribute (type_die, type_tag (type));
14553     }
14554   else
14555     remove_AT (type_die, DW_AT_declaration);
14556
14557   /* If this type has been completed, then give it a byte_size attribute and
14558      then give a list of members.  */
14559   if (complete && !ns_decl)
14560     {
14561       /* Prevent infinite recursion in cases where the type of some member of
14562          this type is expressed in terms of this type itself.  */
14563       TREE_ASM_WRITTEN (type) = 1;
14564       add_byte_size_attribute (type_die, type);
14565       if (TYPE_STUB_DECL (type) != NULL_TREE)
14566         add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
14567
14568       /* If the first reference to this type was as the return type of an
14569          inline function, then it may not have a parent.  Fix this now.  */
14570       if (type_die->die_parent == NULL)
14571         add_child_die (scope_die, type_die);
14572
14573       push_decl_scope (type);
14574       gen_member_die (type, type_die);
14575       pop_decl_scope ();
14576
14577       /* GNU extension: Record what type our vtable lives in.  */
14578       if (TYPE_VFIELD (type))
14579         {
14580           tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
14581
14582           gen_type_die (vtype, context_die);
14583           add_AT_die_ref (type_die, DW_AT_containing_type,
14584                           lookup_type_die (vtype));
14585         }
14586     }
14587   else
14588     {
14589       add_AT_flag (type_die, DW_AT_declaration, 1);
14590
14591       /* We don't need to do this for function-local types.  */
14592       if (TYPE_STUB_DECL (type)
14593           && ! decl_function_context (TYPE_STUB_DECL (type)))
14594         VEC_safe_push (tree, gc, incomplete_types, type);
14595     }
14596
14597   if (get_AT (type_die, DW_AT_name))
14598     add_pubtype (type, type_die);
14599 }
14600
14601 /* Generate a DIE for a subroutine _type_.  */
14602
14603 static void
14604 gen_subroutine_type_die (tree type, dw_die_ref context_die)
14605 {
14606   tree return_type = TREE_TYPE (type);
14607   dw_die_ref subr_die
14608     = new_die (DW_TAG_subroutine_type,
14609                scope_die_for (type, context_die), type);
14610
14611   equate_type_number_to_die (type, subr_die);
14612   add_prototyped_attribute (subr_die, type);
14613   add_type_attribute (subr_die, return_type, 0, 0, context_die);
14614   gen_formal_types_die (type, subr_die);
14615
14616   if (get_AT (subr_die, DW_AT_name))
14617     add_pubtype (type, subr_die);
14618 }
14619
14620 /* Generate a DIE for a type definition.  */
14621
14622 static void
14623 gen_typedef_die (tree decl, dw_die_ref context_die)
14624 {
14625   dw_die_ref type_die;
14626   tree origin;
14627
14628   if (TREE_ASM_WRITTEN (decl))
14629     return;
14630
14631   TREE_ASM_WRITTEN (decl) = 1;
14632   type_die = new_die (DW_TAG_typedef, context_die, decl);
14633   origin = decl_ultimate_origin (decl);
14634   if (origin != NULL)
14635     add_abstract_origin_attribute (type_die, origin);
14636   else
14637     {
14638       tree type;
14639
14640       add_name_and_src_coords_attributes (type_die, decl);
14641       if (DECL_ORIGINAL_TYPE (decl))
14642         {
14643           type = DECL_ORIGINAL_TYPE (decl);
14644
14645           gcc_assert (type != TREE_TYPE (decl));
14646           equate_type_number_to_die (TREE_TYPE (decl), type_die);
14647         }
14648       else
14649         type = TREE_TYPE (decl);
14650
14651       add_type_attribute (type_die, type, TREE_READONLY (decl),
14652                           TREE_THIS_VOLATILE (decl), context_die);
14653     }
14654
14655   if (DECL_ABSTRACT (decl))
14656     equate_decl_number_to_die (decl, type_die);
14657
14658   if (get_AT (type_die, DW_AT_name))
14659     add_pubtype (decl, type_die);
14660 }
14661
14662 /* Generate a type description DIE.  */
14663
14664 static void
14665 gen_type_die_with_usage (tree type, dw_die_ref context_die,
14666                                 enum debug_info_usage usage)
14667 {
14668   int need_pop;
14669   struct array_descr_info info;
14670
14671   if (type == NULL_TREE || type == error_mark_node)
14672     return;
14673
14674   if (TYPE_NAME (type) && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
14675       && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
14676     {
14677       if (TREE_ASM_WRITTEN (type))
14678         return;
14679
14680       /* Prevent broken recursion; we can't hand off to the same type.  */
14681       gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
14682
14683       TREE_ASM_WRITTEN (type) = 1;
14684       gen_decl_die (TYPE_NAME (type), NULL, context_die);
14685       return;
14686     }
14687
14688   /* If this is an array type with hidden descriptor, handle it first.  */
14689   if (!TREE_ASM_WRITTEN (type)
14690       && lang_hooks.types.get_array_descr_info
14691       && lang_hooks.types.get_array_descr_info (type, &info))
14692     {
14693       gen_descr_array_type_die (type, &info, context_die);
14694       TREE_ASM_WRITTEN (type) = 1;
14695       return;
14696     }
14697
14698   /* We are going to output a DIE to represent the unqualified version
14699      of this type (i.e. without any const or volatile qualifiers) so
14700      get the main variant (i.e. the unqualified version) of this type
14701      now.  (Vectors are special because the debugging info is in the
14702      cloned type itself).  */
14703   if (TREE_CODE (type) != VECTOR_TYPE)
14704     type = type_main_variant (type);
14705
14706   if (TREE_ASM_WRITTEN (type))
14707     return;
14708
14709   switch (TREE_CODE (type))
14710     {
14711     case ERROR_MARK:
14712       break;
14713
14714     case POINTER_TYPE:
14715     case REFERENCE_TYPE:
14716       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
14717          ensures that the gen_type_die recursion will terminate even if the
14718          type is recursive.  Recursive types are possible in Ada.  */
14719       /* ??? We could perhaps do this for all types before the switch
14720          statement.  */
14721       TREE_ASM_WRITTEN (type) = 1;
14722
14723       /* For these types, all that is required is that we output a DIE (or a
14724          set of DIEs) to represent the "basis" type.  */
14725       gen_type_die_with_usage (TREE_TYPE (type), context_die,
14726                                 DINFO_USAGE_IND_USE);
14727       break;
14728
14729     case OFFSET_TYPE:
14730       /* This code is used for C++ pointer-to-data-member types.
14731          Output a description of the relevant class type.  */
14732       gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
14733                                         DINFO_USAGE_IND_USE);
14734
14735       /* Output a description of the type of the object pointed to.  */
14736       gen_type_die_with_usage (TREE_TYPE (type), context_die,
14737                                         DINFO_USAGE_IND_USE);
14738
14739       /* Now output a DIE to represent this pointer-to-data-member type
14740          itself.  */
14741       gen_ptr_to_mbr_type_die (type, context_die);
14742       break;
14743
14744     case FUNCTION_TYPE:
14745       /* Force out return type (in case it wasn't forced out already).  */
14746       gen_type_die_with_usage (TREE_TYPE (type), context_die,
14747                                         DINFO_USAGE_DIR_USE);
14748       gen_subroutine_type_die (type, context_die);
14749       break;
14750
14751     case METHOD_TYPE:
14752       /* Force out return type (in case it wasn't forced out already).  */
14753       gen_type_die_with_usage (TREE_TYPE (type), context_die,
14754                                         DINFO_USAGE_DIR_USE);
14755       gen_subroutine_type_die (type, context_die);
14756       break;
14757
14758     case ARRAY_TYPE:
14759       gen_array_type_die (type, context_die);
14760       break;
14761
14762     case VECTOR_TYPE:
14763       gen_array_type_die (type, context_die);
14764       break;
14765
14766     case ENUMERAL_TYPE:
14767     case RECORD_TYPE:
14768     case UNION_TYPE:
14769     case QUAL_UNION_TYPE:
14770       /* If this is a nested type whose containing class hasn't been written
14771          out yet, writing it out will cover this one, too.  This does not apply
14772          to instantiations of member class templates; they need to be added to
14773          the containing class as they are generated.  FIXME: This hurts the
14774          idea of combining type decls from multiple TUs, since we can't predict
14775          what set of template instantiations we'll get.  */
14776       if (TYPE_CONTEXT (type)
14777           && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
14778           && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
14779         {
14780           gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
14781
14782           if (TREE_ASM_WRITTEN (type))
14783             return;
14784
14785           /* If that failed, attach ourselves to the stub.  */
14786           push_decl_scope (TYPE_CONTEXT (type));
14787           context_die = lookup_type_die (TYPE_CONTEXT (type));
14788           need_pop = 1;
14789         }
14790       else
14791         {
14792           context_die = declare_in_namespace (type, context_die);
14793           need_pop = 0;
14794         }
14795
14796       if (TREE_CODE (type) == ENUMERAL_TYPE)
14797         {
14798           /* This might have been written out by the call to
14799              declare_in_namespace.  */
14800           if (!TREE_ASM_WRITTEN (type))
14801             gen_enumeration_type_die (type, context_die);
14802         }
14803       else
14804         gen_struct_or_union_type_die (type, context_die, usage);
14805
14806       if (need_pop)
14807         pop_decl_scope ();
14808
14809       /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
14810          it up if it is ever completed.  gen_*_type_die will set it for us
14811          when appropriate.  */
14812       return;
14813
14814     case VOID_TYPE:
14815     case INTEGER_TYPE:
14816     case REAL_TYPE:
14817     case FIXED_POINT_TYPE:
14818     case COMPLEX_TYPE:
14819     case BOOLEAN_TYPE:
14820       /* No DIEs needed for fundamental types.  */
14821       break;
14822
14823     case LANG_TYPE:
14824       /* No Dwarf representation currently defined.  */
14825       break;
14826
14827     default:
14828       gcc_unreachable ();
14829     }
14830
14831   TREE_ASM_WRITTEN (type) = 1;
14832 }
14833
14834 static void
14835 gen_type_die (tree type, dw_die_ref context_die)
14836 {
14837   gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
14838 }
14839
14840 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
14841    things which are local to the given block.  */
14842
14843 static void
14844 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
14845 {
14846   int must_output_die = 0;
14847   bool inlined_func;
14848
14849   /* Ignore blocks that are NULL.  */
14850   if (stmt == NULL_TREE)
14851     return;
14852
14853   inlined_func = inlined_function_outer_scope_p (stmt);
14854
14855   /* If the block is one fragment of a non-contiguous block, do not
14856      process the variables, since they will have been done by the
14857      origin block.  Do process subblocks.  */
14858   if (BLOCK_FRAGMENT_ORIGIN (stmt))
14859     {
14860       tree sub;
14861
14862       for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
14863         gen_block_die (sub, context_die, depth + 1);
14864
14865       return;
14866     }
14867
14868   /* Determine if we need to output any Dwarf DIEs at all to represent this
14869      block.  */
14870   if (inlined_func)
14871     /* The outer scopes for inlinings *must* always be represented.  We
14872        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
14873     must_output_die = 1;
14874   else
14875     {
14876       /* Determine if this block directly contains any "significant"
14877          local declarations which we will need to output DIEs for.  */
14878       if (debug_info_level > DINFO_LEVEL_TERSE)
14879         /* We are not in terse mode so *any* local declaration counts
14880            as being a "significant" one.  */
14881         must_output_die = ((BLOCK_VARS (stmt) != NULL
14882                             || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
14883                            && (TREE_USED (stmt)
14884                                || TREE_ASM_WRITTEN (stmt)
14885                                || BLOCK_ABSTRACT (stmt)));
14886       else if ((TREE_USED (stmt)
14887                 || TREE_ASM_WRITTEN (stmt)
14888                 || BLOCK_ABSTRACT (stmt))
14889                && !dwarf2out_ignore_block (stmt))
14890         must_output_die = 1;
14891     }
14892
14893   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
14894      DIE for any block which contains no significant local declarations at
14895      all.  Rather, in such cases we just call `decls_for_scope' so that any
14896      needed Dwarf info for any sub-blocks will get properly generated. Note
14897      that in terse mode, our definition of what constitutes a "significant"
14898      local declaration gets restricted to include only inlined function
14899      instances and local (nested) function definitions.  */
14900   if (must_output_die)
14901     {
14902       if (inlined_func)
14903         gen_inlined_subroutine_die (stmt, context_die, depth);
14904       else
14905         gen_lexical_block_die (stmt, context_die, depth);
14906     }
14907   else
14908     decls_for_scope (stmt, context_die, depth);
14909 }
14910
14911 /* Process variable DECL (or variable with origin ORIGIN) within
14912    block STMT and add it to CONTEXT_DIE.  */
14913 static void
14914 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
14915 {
14916   dw_die_ref die;
14917   tree decl_or_origin = decl ? decl : origin;
14918   tree ultimate_origin = origin ? decl_ultimate_origin (origin) : NULL;
14919
14920   if (ultimate_origin)
14921     origin = ultimate_origin;
14922
14923   if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
14924     die = lookup_decl_die (decl_or_origin);
14925   else if (TREE_CODE (decl_or_origin) == TYPE_DECL
14926            && TYPE_DECL_IS_STUB (decl_or_origin))
14927     die = lookup_type_die (TREE_TYPE (decl_or_origin));
14928   else
14929     die = NULL;
14930
14931   if (die != NULL && die->die_parent == NULL)
14932     add_child_die (context_die, die);
14933   else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
14934     dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
14935                                          stmt, context_die);
14936   else
14937     gen_decl_die (decl, origin, context_die);
14938 }
14939
14940 /* Generate all of the decls declared within a given scope and (recursively)
14941    all of its sub-blocks.  */
14942
14943 static void
14944 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
14945 {
14946   tree decl;
14947   unsigned int i;
14948   tree subblocks;
14949
14950   /* Ignore NULL blocks.  */
14951   if (stmt == NULL_TREE)
14952     return;
14953
14954   /* Output the DIEs to represent all of the data objects and typedefs
14955      declared directly within this block but not within any nested
14956      sub-blocks.  Also, nested function and tag DIEs have been
14957      generated with a parent of NULL; fix that up now.  */
14958   for (decl = BLOCK_VARS (stmt); decl != NULL; decl = TREE_CHAIN (decl))
14959     process_scope_var (stmt, decl, NULL_TREE, context_die);
14960   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
14961     process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
14962                        context_die);
14963
14964   /* If we're at -g1, we're not interested in subblocks.  */
14965   if (debug_info_level <= DINFO_LEVEL_TERSE)
14966     return;
14967
14968   /* Output the DIEs to represent all sub-blocks (and the items declared
14969      therein) of this block.  */
14970   for (subblocks = BLOCK_SUBBLOCKS (stmt);
14971        subblocks != NULL;
14972        subblocks = BLOCK_CHAIN (subblocks))
14973     gen_block_die (subblocks, context_die, depth + 1);
14974 }
14975
14976 /* Is this a typedef we can avoid emitting?  */
14977
14978 static inline int
14979 is_redundant_typedef (const_tree decl)
14980 {
14981   if (TYPE_DECL_IS_STUB (decl))
14982     return 1;
14983
14984   if (DECL_ARTIFICIAL (decl)
14985       && DECL_CONTEXT (decl)
14986       && is_tagged_type (DECL_CONTEXT (decl))
14987       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
14988       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
14989     /* Also ignore the artificial member typedef for the class name.  */
14990     return 1;
14991
14992   return 0;
14993 }
14994
14995 /* Returns the DIE for a context.  */
14996
14997 static inline dw_die_ref
14998 get_context_die (tree context)
14999 {
15000   if (context)
15001     {
15002       /* Find die that represents this context.  */
15003       if (TYPE_P (context))
15004         return force_type_die (context);
15005       else
15006         return force_decl_die (context);
15007     }
15008   return comp_unit_die;
15009 }
15010
15011 /* Returns the DIE for decl.  A DIE will always be returned.  */
15012
15013 static dw_die_ref
15014 force_decl_die (tree decl)
15015 {
15016   dw_die_ref decl_die;
15017   unsigned saved_external_flag;
15018   tree save_fn = NULL_TREE;
15019   decl_die = lookup_decl_die (decl);
15020   if (!decl_die)
15021     {
15022       dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
15023
15024       decl_die = lookup_decl_die (decl);
15025       if (decl_die)
15026         return decl_die;
15027
15028       switch (TREE_CODE (decl))
15029         {
15030         case FUNCTION_DECL:
15031           /* Clear current_function_decl, so that gen_subprogram_die thinks
15032              that this is a declaration. At this point, we just want to force
15033              declaration die.  */
15034           save_fn = current_function_decl;
15035           current_function_decl = NULL_TREE;
15036           gen_subprogram_die (decl, context_die);
15037           current_function_decl = save_fn;
15038           break;
15039
15040         case VAR_DECL:
15041           /* Set external flag to force declaration die. Restore it after
15042            gen_decl_die() call.  */
15043           saved_external_flag = DECL_EXTERNAL (decl);
15044           DECL_EXTERNAL (decl) = 1;
15045           gen_decl_die (decl, NULL, context_die);
15046           DECL_EXTERNAL (decl) = saved_external_flag;
15047           break;
15048
15049         case NAMESPACE_DECL:
15050           dwarf2out_decl (decl);
15051           break;
15052
15053         default:
15054           gcc_unreachable ();
15055         }
15056
15057       /* We should be able to find the DIE now.  */
15058       if (!decl_die)
15059         decl_die = lookup_decl_die (decl);
15060       gcc_assert (decl_die);
15061     }
15062
15063   return decl_die;
15064 }
15065
15066 /* Returns the DIE for TYPE, that must not be a base type.  A DIE is
15067    always returned.  */
15068
15069 static dw_die_ref
15070 force_type_die (tree type)
15071 {
15072   dw_die_ref type_die;
15073
15074   type_die = lookup_type_die (type);
15075   if (!type_die)
15076     {
15077       dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
15078
15079       type_die = modified_type_die (type, TYPE_READONLY (type),
15080                                     TYPE_VOLATILE (type), context_die);
15081       gcc_assert (type_die);
15082     }
15083   return type_die;
15084 }
15085
15086 /* Force out any required namespaces to be able to output DECL,
15087    and return the new context_die for it, if it's changed.  */
15088
15089 static dw_die_ref
15090 setup_namespace_context (tree thing, dw_die_ref context_die)
15091 {
15092   tree context = (DECL_P (thing)
15093                   ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
15094   if (context && TREE_CODE (context) == NAMESPACE_DECL)
15095     /* Force out the namespace.  */
15096     context_die = force_decl_die (context);
15097
15098   return context_die;
15099 }
15100
15101 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
15102    type) within its namespace, if appropriate.
15103
15104    For compatibility with older debuggers, namespace DIEs only contain
15105    declarations; all definitions are emitted at CU scope.  */
15106
15107 static dw_die_ref
15108 declare_in_namespace (tree thing, dw_die_ref context_die)
15109 {
15110   dw_die_ref ns_context;
15111
15112   if (debug_info_level <= DINFO_LEVEL_TERSE)
15113     return context_die;
15114
15115   /* If this decl is from an inlined function, then don't try to emit it in its
15116      namespace, as we will get confused.  It would have already been emitted
15117      when the abstract instance of the inline function was emitted anyways.  */
15118   if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
15119     return context_die;
15120
15121   ns_context = setup_namespace_context (thing, context_die);
15122
15123   if (ns_context != context_die)
15124     {
15125       if (is_fortran ())
15126         return ns_context;
15127       if (DECL_P (thing))
15128         gen_decl_die (thing, NULL, ns_context);
15129       else
15130         gen_type_die (thing, ns_context);
15131     }
15132   return context_die;
15133 }
15134
15135 /* Generate a DIE for a namespace or namespace alias.  */
15136
15137 static void
15138 gen_namespace_die (tree decl, dw_die_ref context_die)
15139 {
15140   dw_die_ref namespace_die;
15141
15142   /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
15143      they are an alias of.  */
15144   if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
15145     {
15146       /* Output a real namespace or module.  */
15147       context_die = setup_namespace_context (decl, comp_unit_die);
15148       namespace_die = new_die (is_fortran ()
15149                                ? DW_TAG_module : DW_TAG_namespace,
15150                                context_die, decl);
15151       /* For Fortran modules defined in different CU don't add src coords.  */
15152       if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
15153         add_name_attribute (namespace_die, dwarf2_name (decl, 0));
15154       else
15155         add_name_and_src_coords_attributes (namespace_die, decl);
15156       if (DECL_EXTERNAL (decl))
15157         add_AT_flag (namespace_die, DW_AT_declaration, 1);
15158       equate_decl_number_to_die (decl, namespace_die);
15159     }
15160   else
15161     {
15162       /* Output a namespace alias.  */
15163
15164       /* Force out the namespace we are an alias of, if necessary.  */
15165       dw_die_ref origin_die
15166         = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
15167
15168       if (DECL_CONTEXT (decl) == NULL_TREE
15169           || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
15170         context_die = setup_namespace_context (decl, comp_unit_die);
15171       /* Now create the namespace alias DIE.  */
15172       namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
15173       add_name_and_src_coords_attributes (namespace_die, decl);
15174       add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
15175       equate_decl_number_to_die (decl, namespace_die);
15176     }
15177 }
15178
15179 /* Generate Dwarf debug information for a decl described by DECL.  */
15180
15181 static void
15182 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
15183 {
15184   tree decl_or_origin = decl ? decl : origin;
15185   tree class_origin = NULL;
15186
15187   if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
15188     return;
15189
15190   switch (TREE_CODE (decl_or_origin))
15191     {
15192     case ERROR_MARK:
15193       break;
15194
15195     case CONST_DECL:
15196       if (!is_fortran ())
15197         {
15198           /* The individual enumerators of an enum type get output when we output
15199              the Dwarf representation of the relevant enum type itself.  */
15200           break;
15201         }
15202
15203       /* Emit its type.  */
15204       gen_type_die (TREE_TYPE (decl), context_die);
15205
15206       /* And its containing namespace.  */
15207       context_die = declare_in_namespace (decl, context_die);
15208
15209       gen_const_die (decl, context_die);
15210       break;
15211
15212     case FUNCTION_DECL:
15213       /* Don't output any DIEs to represent mere function declarations,
15214          unless they are class members or explicit block externs.  */
15215       if (DECL_INITIAL (decl_or_origin) == NULL_TREE
15216           && DECL_CONTEXT (decl_or_origin) == NULL_TREE
15217           && (current_function_decl == NULL_TREE
15218               || DECL_ARTIFICIAL (decl_or_origin)))
15219         break;
15220
15221 #if 0
15222       /* FIXME */
15223       /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
15224          on local redeclarations of global functions.  That seems broken.  */
15225       if (current_function_decl != decl)
15226         /* This is only a declaration.  */;
15227 #endif
15228
15229       /* If we're emitting a clone, emit info for the abstract instance.  */
15230       if (origin || DECL_ORIGIN (decl) != decl)
15231         dwarf2out_abstract_function (origin ? origin : DECL_ABSTRACT_ORIGIN (decl));
15232
15233       /* If we're emitting an out-of-line copy of an inline function,
15234          emit info for the abstract instance and set up to refer to it.  */
15235       else if (cgraph_function_possibly_inlined_p (decl)
15236                && ! DECL_ABSTRACT (decl)
15237                && ! class_or_namespace_scope_p (context_die)
15238                /* dwarf2out_abstract_function won't emit a die if this is just
15239                   a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
15240                   that case, because that works only if we have a die.  */
15241                && DECL_INITIAL (decl) != NULL_TREE)
15242         {
15243           dwarf2out_abstract_function (decl);
15244           set_decl_origin_self (decl);
15245         }
15246
15247       /* Otherwise we're emitting the primary DIE for this decl.  */
15248       else if (debug_info_level > DINFO_LEVEL_TERSE)
15249         {
15250           /* Before we describe the FUNCTION_DECL itself, make sure that we
15251              have described its return type.  */
15252           gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
15253
15254           /* And its virtual context.  */
15255           if (DECL_VINDEX (decl) != NULL_TREE)
15256             gen_type_die (DECL_CONTEXT (decl), context_die);
15257
15258           /* And its containing type.  */
15259           if (!origin)
15260             origin = decl_class_context (decl);
15261           if (origin != NULL_TREE)
15262             gen_type_die_for_member (origin, decl, context_die);
15263
15264           /* And its containing namespace.  */
15265           context_die = declare_in_namespace (decl, context_die);
15266         }
15267
15268       /* Now output a DIE to represent the function itself.  */
15269       if (decl)
15270         gen_subprogram_die (decl, context_die);
15271       break;
15272
15273     case TYPE_DECL:
15274       /* If we are in terse mode, don't generate any DIEs to represent any
15275          actual typedefs.  */
15276       if (debug_info_level <= DINFO_LEVEL_TERSE)
15277         break;
15278
15279       /* In the special case of a TYPE_DECL node representing the declaration
15280          of some type tag, if the given TYPE_DECL is marked as having been
15281          instantiated from some other (original) TYPE_DECL node (e.g. one which
15282          was generated within the original definition of an inline function) we
15283          used to generate a special (abbreviated) DW_TAG_structure_type,
15284          DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  But nothing
15285          should be actually referencing those DIEs, as variable DIEs with that
15286          type would be emitted already in the abstract origin, so it was always
15287          removed during unused type prunning.  Don't add anything in this
15288          case.  */
15289       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
15290         break;
15291
15292       if (is_redundant_typedef (decl))
15293         gen_type_die (TREE_TYPE (decl), context_die);
15294       else
15295         /* Output a DIE to represent the typedef itself.  */
15296         gen_typedef_die (decl, context_die);
15297       break;
15298
15299     case LABEL_DECL:
15300       if (debug_info_level >= DINFO_LEVEL_NORMAL)
15301         gen_label_die (decl, context_die);
15302       break;
15303
15304     case VAR_DECL:
15305     case RESULT_DECL:
15306       /* If we are in terse mode, don't generate any DIEs to represent any
15307          variable declarations or definitions.  */
15308       if (debug_info_level <= DINFO_LEVEL_TERSE)
15309         break;
15310
15311       /* Output any DIEs that are needed to specify the type of this data
15312          object.  */
15313       if (TREE_CODE (decl_or_origin) == RESULT_DECL
15314           && DECL_BY_REFERENCE (decl_or_origin))
15315         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
15316       else
15317         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
15318
15319       /* And its containing type.  */
15320       class_origin = decl_class_context (decl_or_origin);
15321       if (class_origin != NULL_TREE)
15322         gen_type_die_for_member (class_origin, decl_or_origin, context_die);
15323
15324       /* And its containing namespace.  */
15325       context_die = declare_in_namespace (decl_or_origin, context_die);
15326
15327       /* Now output the DIE to represent the data object itself.  This gets
15328          complicated because of the possibility that the VAR_DECL really
15329          represents an inlined instance of a formal parameter for an inline
15330          function.  */
15331       if (!origin)
15332         origin = decl_ultimate_origin (decl);
15333       if (origin != NULL_TREE && TREE_CODE (origin) == PARM_DECL)
15334         gen_formal_parameter_die (decl, origin, context_die);
15335       else
15336         gen_variable_die (decl, origin, context_die);
15337       break;
15338
15339     case FIELD_DECL:
15340       /* Ignore the nameless fields that are used to skip bits but handle C++
15341          anonymous unions and structs.  */
15342       if (DECL_NAME (decl) != NULL_TREE
15343           || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
15344           || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
15345         {
15346           gen_type_die (member_declared_type (decl), context_die);
15347           gen_field_die (decl, context_die);
15348         }
15349       break;
15350
15351     case PARM_DECL:
15352       if (DECL_BY_REFERENCE (decl_or_origin))
15353         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
15354       else
15355         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
15356       gen_formal_parameter_die (decl, origin, context_die);
15357       break;
15358
15359     case NAMESPACE_DECL:
15360     case IMPORTED_DECL:
15361       gen_namespace_die (decl, context_die);
15362       break;
15363
15364     default:
15365       /* Probably some frontend-internal decl.  Assume we don't care.  */
15366       gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
15367       break;
15368     }
15369 }
15370 \f
15371 /* Output debug information for global decl DECL.  Called from toplev.c after
15372    compilation proper has finished.  */
15373
15374 static void
15375 dwarf2out_global_decl (tree decl)
15376 {
15377   /* Output DWARF2 information for file-scope tentative data object
15378      declarations, file-scope (extern) function declarations (which
15379      had no corresponding body) and file-scope tagged type declarations
15380      and definitions which have not yet been forced out.  */
15381   if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
15382     dwarf2out_decl (decl);
15383 }
15384
15385 /* Output debug information for type decl DECL.  Called from toplev.c
15386    and from language front ends (to record built-in types).  */
15387 static void
15388 dwarf2out_type_decl (tree decl, int local)
15389 {
15390   if (!local)
15391     dwarf2out_decl (decl);
15392 }
15393
15394 /* Output debug information for imported module or decl DECL.
15395    NAME is non-NULL name in the lexical block if the decl has been renamed.
15396    LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
15397    that DECL belongs to.
15398    LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK.  */
15399 static void
15400 dwarf2out_imported_module_or_decl_1 (tree decl,
15401                                      tree name,
15402                                      tree lexical_block,
15403                                      dw_die_ref lexical_block_die)
15404 {
15405   expanded_location xloc;
15406   dw_die_ref imported_die = NULL;
15407   dw_die_ref at_import_die;
15408
15409   if (TREE_CODE (decl) == IMPORTED_DECL)
15410     {
15411       xloc = expand_location (DECL_SOURCE_LOCATION (decl));
15412       decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
15413       gcc_assert (decl);
15414     }
15415   else
15416     xloc = expand_location (input_location);
15417
15418   if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
15419     {
15420       if (is_base_type (TREE_TYPE (decl)))
15421         at_import_die = base_type_die (TREE_TYPE (decl));
15422       else
15423         at_import_die = force_type_die (TREE_TYPE (decl));
15424       /* For namespace N { typedef void T; } using N::T; base_type_die
15425          returns NULL, but DW_TAG_imported_declaration requires
15426          the DW_AT_import tag.  Force creation of DW_TAG_typedef.  */
15427       if (!at_import_die)
15428         {
15429           gcc_assert (TREE_CODE (decl) == TYPE_DECL);
15430           gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
15431           at_import_die = lookup_type_die (TREE_TYPE (decl));
15432           gcc_assert (at_import_die);
15433         }
15434     }
15435   else
15436     {
15437       at_import_die = lookup_decl_die (decl);
15438       if (!at_import_die)
15439         {
15440           /* If we're trying to avoid duplicate debug info, we may not have
15441              emitted the member decl for this field.  Emit it now.  */
15442           if (TREE_CODE (decl) == FIELD_DECL)
15443             {
15444               tree type = DECL_CONTEXT (decl);
15445
15446               if (TYPE_CONTEXT (type)
15447                   && TYPE_P (TYPE_CONTEXT (type))
15448                   && !should_emit_struct_debug (TYPE_CONTEXT (type),
15449                                                 DINFO_USAGE_DIR_USE))
15450                 return;
15451               gen_type_die_for_member (type, decl,
15452                                        get_context_die (TYPE_CONTEXT (type)));
15453             }
15454           at_import_die = force_decl_die (decl);
15455         }
15456     }
15457
15458   if (TREE_CODE (decl) == NAMESPACE_DECL)
15459     imported_die = new_die (DW_TAG_imported_module,
15460                             lexical_block_die,
15461                             lexical_block);
15462   else
15463     imported_die = new_die (DW_TAG_imported_declaration,
15464                             lexical_block_die,
15465                             lexical_block);
15466
15467   add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
15468   add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
15469   if (name)
15470     add_AT_string (imported_die, DW_AT_name,
15471                    IDENTIFIER_POINTER (name));
15472   add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
15473 }
15474
15475 /* Output debug information for imported module or decl DECL.
15476    NAME is non-NULL name in context if the decl has been renamed.
15477    CHILD is true if decl is one of the renamed decls as part of
15478    importing whole module.  */
15479
15480 static void
15481 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
15482                                    bool child)
15483 {
15484   /* dw_die_ref at_import_die;  */
15485   dw_die_ref scope_die;
15486
15487   if (debug_info_level <= DINFO_LEVEL_TERSE)
15488     return;
15489
15490   gcc_assert (decl);
15491
15492   /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
15493      We need decl DIE for reference and scope die. First, get DIE for the decl
15494      itself.  */
15495
15496   /* Get the scope die for decl context. Use comp_unit_die for global module
15497      or decl. If die is not found for non globals, force new die.  */
15498   if (context
15499       && TYPE_P (context)
15500       && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
15501     return;
15502   scope_die = get_context_die (context);
15503
15504   if (child)
15505     {
15506       gcc_assert (scope_die->die_child);
15507       gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
15508       gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
15509       scope_die = scope_die->die_child;
15510     }
15511
15512   /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
15513   dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
15514
15515 }
15516
15517 /* Write the debugging output for DECL.  */
15518
15519 void
15520 dwarf2out_decl (tree decl)
15521 {
15522   dw_die_ref context_die = comp_unit_die;
15523
15524   switch (TREE_CODE (decl))
15525     {
15526     case ERROR_MARK:
15527       return;
15528
15529     case FUNCTION_DECL:
15530       /* What we would really like to do here is to filter out all mere
15531          file-scope declarations of file-scope functions which are never
15532          referenced later within this translation unit (and keep all of ones
15533          that *are* referenced later on) but we aren't clairvoyant, so we have
15534          no idea which functions will be referenced in the future (i.e. later
15535          on within the current translation unit). So here we just ignore all
15536          file-scope function declarations which are not also definitions.  If
15537          and when the debugger needs to know something about these functions,
15538          it will have to hunt around and find the DWARF information associated
15539          with the definition of the function.
15540
15541          We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
15542          nodes represent definitions and which ones represent mere
15543          declarations.  We have to check DECL_INITIAL instead. That's because
15544          the C front-end supports some weird semantics for "extern inline"
15545          function definitions.  These can get inlined within the current
15546          translation unit (and thus, we need to generate Dwarf info for their
15547          abstract instances so that the Dwarf info for the concrete inlined
15548          instances can have something to refer to) but the compiler never
15549          generates any out-of-lines instances of such things (despite the fact
15550          that they *are* definitions).
15551
15552          The important point is that the C front-end marks these "extern
15553          inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
15554          them anyway. Note that the C++ front-end also plays some similar games
15555          for inline function definitions appearing within include files which
15556          also contain `#pragma interface' pragmas.  */
15557       if (DECL_INITIAL (decl) == NULL_TREE)
15558         return;
15559
15560       /* If we're a nested function, initially use a parent of NULL; if we're
15561          a plain function, this will be fixed up in decls_for_scope.  If
15562          we're a method, it will be ignored, since we already have a DIE.  */
15563       if (decl_function_context (decl)
15564           /* But if we're in terse mode, we don't care about scope.  */
15565           && debug_info_level > DINFO_LEVEL_TERSE)
15566         context_die = NULL;
15567       break;
15568
15569     case VAR_DECL:
15570       /* Ignore this VAR_DECL if it refers to a file-scope extern data object
15571          declaration and if the declaration was never even referenced from
15572          within this entire compilation unit.  We suppress these DIEs in
15573          order to save space in the .debug section (by eliminating entries
15574          which are probably useless).  Note that we must not suppress
15575          block-local extern declarations (whether used or not) because that
15576          would screw-up the debugger's name lookup mechanism and cause it to
15577          miss things which really ought to be in scope at a given point.  */
15578       if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
15579         return;
15580
15581       /* For local statics lookup proper context die.  */
15582       if (TREE_STATIC (decl) && decl_function_context (decl))
15583         context_die = lookup_decl_die (DECL_CONTEXT (decl));
15584
15585       /* If we are in terse mode, don't generate any DIEs to represent any
15586          variable declarations or definitions.  */
15587       if (debug_info_level <= DINFO_LEVEL_TERSE)
15588         return;
15589       break;
15590
15591     case CONST_DECL:
15592       if (debug_info_level <= DINFO_LEVEL_TERSE)
15593         return;
15594       if (!is_fortran ())
15595         return;
15596       if (TREE_STATIC (decl) && decl_function_context (decl))
15597         context_die = lookup_decl_die (DECL_CONTEXT (decl));
15598       break;
15599
15600     case NAMESPACE_DECL:
15601     case IMPORTED_DECL:
15602       if (debug_info_level <= DINFO_LEVEL_TERSE)
15603         return;
15604       if (lookup_decl_die (decl) != NULL)
15605         return;
15606       break;
15607
15608     case TYPE_DECL:
15609       /* Don't emit stubs for types unless they are needed by other DIEs.  */
15610       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
15611         return;
15612
15613       /* Don't bother trying to generate any DIEs to represent any of the
15614          normal built-in types for the language we are compiling.  */
15615       if (DECL_IS_BUILTIN (decl))
15616         {
15617           /* OK, we need to generate one for `bool' so GDB knows what type
15618              comparisons have.  */
15619           if (is_cxx ()
15620               && TREE_CODE (TREE_TYPE (decl)) == BOOLEAN_TYPE
15621               && ! DECL_IGNORED_P (decl))
15622             modified_type_die (TREE_TYPE (decl), 0, 0, NULL);
15623
15624           return;
15625         }
15626
15627       /* If we are in terse mode, don't generate any DIEs for types.  */
15628       if (debug_info_level <= DINFO_LEVEL_TERSE)
15629         return;
15630
15631       /* If we're a function-scope tag, initially use a parent of NULL;
15632          this will be fixed up in decls_for_scope.  */
15633       if (decl_function_context (decl))
15634         context_die = NULL;
15635
15636       break;
15637
15638     default:
15639       return;
15640     }
15641
15642   gen_decl_die (decl, NULL, context_die);
15643 }
15644
15645 /* Output a marker (i.e. a label) for the beginning of the generated code for
15646    a lexical block.  */
15647
15648 static void
15649 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
15650                        unsigned int blocknum)
15651 {
15652   switch_to_section (current_function_section ());
15653   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
15654 }
15655
15656 /* Output a marker (i.e. a label) for the end of the generated code for a
15657    lexical block.  */
15658
15659 static void
15660 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
15661 {
15662   switch_to_section (current_function_section ());
15663   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
15664 }
15665
15666 /* Returns nonzero if it is appropriate not to emit any debugging
15667    information for BLOCK, because it doesn't contain any instructions.
15668
15669    Don't allow this for blocks with nested functions or local classes
15670    as we would end up with orphans, and in the presence of scheduling
15671    we may end up calling them anyway.  */
15672
15673 static bool
15674 dwarf2out_ignore_block (const_tree block)
15675 {
15676   tree decl;
15677   unsigned int i;
15678
15679   for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
15680     if (TREE_CODE (decl) == FUNCTION_DECL
15681         || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
15682       return 0;
15683   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
15684     {
15685       decl = BLOCK_NONLOCALIZED_VAR (block, i);
15686       if (TREE_CODE (decl) == FUNCTION_DECL
15687           || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
15688       return 0;
15689     }
15690
15691   return 1;
15692 }
15693
15694 /* Hash table routines for file_hash.  */
15695
15696 static int
15697 file_table_eq (const void *p1_p, const void *p2_p)
15698 {
15699   const struct dwarf_file_data *const p1 =
15700     (const struct dwarf_file_data *) p1_p;
15701   const char *const p2 = (const char *) p2_p;
15702   return strcmp (p1->filename, p2) == 0;
15703 }
15704
15705 static hashval_t
15706 file_table_hash (const void *p_p)
15707 {
15708   const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
15709   return htab_hash_string (p->filename);
15710 }
15711
15712 /* Lookup FILE_NAME (in the list of filenames that we know about here in
15713    dwarf2out.c) and return its "index".  The index of each (known) filename is
15714    just a unique number which is associated with only that one filename.  We
15715    need such numbers for the sake of generating labels (in the .debug_sfnames
15716    section) and references to those files numbers (in the .debug_srcinfo
15717    and.debug_macinfo sections).  If the filename given as an argument is not
15718    found in our current list, add it to the list and assign it the next
15719    available unique index number.  In order to speed up searches, we remember
15720    the index of the filename was looked up last.  This handles the majority of
15721    all searches.  */
15722
15723 static struct dwarf_file_data *
15724 lookup_filename (const char *file_name)
15725 {
15726   void ** slot;
15727   struct dwarf_file_data * created;
15728
15729   /* Check to see if the file name that was searched on the previous
15730      call matches this file name.  If so, return the index.  */
15731   if (file_table_last_lookup
15732       && (file_name == file_table_last_lookup->filename
15733           || strcmp (file_table_last_lookup->filename, file_name) == 0))
15734     return file_table_last_lookup;
15735
15736   /* Didn't match the previous lookup, search the table.  */
15737   slot = htab_find_slot_with_hash (file_table, file_name,
15738                                    htab_hash_string (file_name), INSERT);
15739   if (*slot)
15740     return (struct dwarf_file_data *) *slot;
15741
15742   created = GGC_NEW (struct dwarf_file_data);
15743   created->filename = file_name;
15744   created->emitted_number = 0;
15745   *slot = created;
15746   return created;
15747 }
15748
15749 /* If the assembler will construct the file table, then translate the compiler
15750    internal file table number into the assembler file table number, and emit
15751    a .file directive if we haven't already emitted one yet.  The file table
15752    numbers are different because we prune debug info for unused variables and
15753    types, which may include filenames.  */
15754
15755 static int
15756 maybe_emit_file (struct dwarf_file_data * fd)
15757 {
15758   if (! fd->emitted_number)
15759     {
15760       if (last_emitted_file)
15761         fd->emitted_number = last_emitted_file->emitted_number + 1;
15762       else
15763         fd->emitted_number = 1;
15764       last_emitted_file = fd;
15765
15766       if (DWARF2_ASM_LINE_DEBUG_INFO)
15767         {
15768           fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
15769           output_quoted_string (asm_out_file,
15770                                 remap_debug_filename (fd->filename));
15771           fputc ('\n', asm_out_file);
15772         }
15773     }
15774
15775   return fd->emitted_number;
15776 }
15777
15778 /* Replace DW_AT_name for the decl with name.  */
15779  
15780 static void
15781 dwarf2out_set_name (tree decl, tree name)
15782 {
15783   dw_die_ref die;
15784   dw_attr_ref attr;
15785
15786   die = TYPE_SYMTAB_DIE (decl);
15787   if (!die)
15788     return;
15789
15790   attr = get_AT (die, DW_AT_name);
15791   if (attr)
15792     {
15793       struct indirect_string_node *node;
15794
15795       node = find_AT_string (dwarf2_name (name, 0));
15796       /* replace the string.  */
15797       attr->dw_attr_val.v.val_str = node;
15798     }
15799
15800   else
15801     add_name_attribute (die, dwarf2_name (name, 0));
15802 }
15803 /* Called by the final INSN scan whenever we see a var location.  We
15804    use it to drop labels in the right places, and throw the location in
15805    our lookup table.  */
15806
15807 static void
15808 dwarf2out_var_location (rtx loc_note)
15809 {
15810   char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
15811   struct var_loc_node *newloc;
15812   rtx prev_insn;
15813   static rtx last_insn;
15814   static const char *last_label;
15815   tree decl;
15816
15817   if (!DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
15818     return;
15819   prev_insn = PREV_INSN (loc_note);
15820
15821   newloc = GGC_CNEW (struct var_loc_node);
15822   /* If the insn we processed last time is the previous insn
15823      and it is also a var location note, use the label we emitted
15824      last time.  */
15825   if (last_insn != NULL_RTX
15826       && last_insn == prev_insn
15827       && NOTE_P (prev_insn)
15828       && NOTE_KIND (prev_insn) == NOTE_INSN_VAR_LOCATION)
15829     {
15830       newloc->label = last_label;
15831     }
15832   else
15833     {
15834       ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
15835       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
15836       loclabel_num++;
15837       newloc->label = ggc_strdup (loclabel);
15838     }
15839   newloc->var_loc_note = loc_note;
15840   newloc->next = NULL;
15841
15842   if (cfun && in_cold_section_p)
15843     newloc->section_label = crtl->subsections.cold_section_label;
15844   else
15845     newloc->section_label = text_section_label;
15846
15847   last_insn = loc_note;
15848   last_label = newloc->label;
15849   decl = NOTE_VAR_LOCATION_DECL (loc_note);
15850   add_var_loc_to_decl (decl, newloc);
15851 }
15852
15853 /* We need to reset the locations at the beginning of each
15854    function. We can't do this in the end_function hook, because the
15855    declarations that use the locations won't have been output when
15856    that hook is called.  Also compute have_multiple_function_sections here.  */
15857
15858 static void
15859 dwarf2out_begin_function (tree fun)
15860 {
15861   htab_empty (decl_loc_table);
15862
15863   if (function_section (fun) != text_section)
15864     have_multiple_function_sections = true;
15865
15866   dwarf2out_note_section_used ();
15867 }
15868
15869 /* Output a label to mark the beginning of a source code line entry
15870    and record information relating to this source line, in
15871    'line_info_table' for later output of the .debug_line section.  */
15872
15873 static void
15874 dwarf2out_source_line (unsigned int line, const char *filename)
15875 {
15876   if (debug_info_level >= DINFO_LEVEL_NORMAL
15877       && line != 0)
15878     {
15879       int file_num = maybe_emit_file (lookup_filename (filename));
15880
15881       switch_to_section (current_function_section ());
15882
15883       /* If requested, emit something human-readable.  */
15884       if (flag_debug_asm)
15885         fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
15886                  filename, line);
15887
15888       if (DWARF2_ASM_LINE_DEBUG_INFO)
15889         {
15890           /* Emit the .loc directive understood by GNU as.  */
15891           fprintf (asm_out_file, "\t.loc %d %d 0\n", file_num, line);
15892
15893           /* Indicate that line number info exists.  */
15894           line_info_table_in_use++;
15895         }
15896       else if (function_section (current_function_decl) != text_section)
15897         {
15898           dw_separate_line_info_ref line_info;
15899           targetm.asm_out.internal_label (asm_out_file,
15900                                           SEPARATE_LINE_CODE_LABEL,
15901                                           separate_line_info_table_in_use);
15902
15903           /* Expand the line info table if necessary.  */
15904           if (separate_line_info_table_in_use
15905               == separate_line_info_table_allocated)
15906             {
15907               separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
15908               separate_line_info_table
15909                 = GGC_RESIZEVEC (dw_separate_line_info_entry,
15910                                  separate_line_info_table,
15911                                  separate_line_info_table_allocated);
15912               memset (separate_line_info_table
15913                        + separate_line_info_table_in_use,
15914                       0,
15915                       (LINE_INFO_TABLE_INCREMENT
15916                        * sizeof (dw_separate_line_info_entry)));
15917             }
15918
15919           /* Add the new entry at the end of the line_info_table.  */
15920           line_info
15921             = &separate_line_info_table[separate_line_info_table_in_use++];
15922           line_info->dw_file_num = file_num;
15923           line_info->dw_line_num = line;
15924           line_info->function = current_function_funcdef_no;
15925         }
15926       else
15927         {
15928           dw_line_info_ref line_info;
15929
15930           targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL,
15931                                      line_info_table_in_use);
15932
15933           /* Expand the line info table if necessary.  */
15934           if (line_info_table_in_use == line_info_table_allocated)
15935             {
15936               line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
15937               line_info_table
15938                 = GGC_RESIZEVEC (dw_line_info_entry, line_info_table,
15939                                  line_info_table_allocated);
15940               memset (line_info_table + line_info_table_in_use, 0,
15941                       LINE_INFO_TABLE_INCREMENT * sizeof (dw_line_info_entry));
15942             }
15943
15944           /* Add the new entry at the end of the line_info_table.  */
15945           line_info = &line_info_table[line_info_table_in_use++];
15946           line_info->dw_file_num = file_num;
15947           line_info->dw_line_num = line;
15948         }
15949     }
15950 }
15951
15952 /* Record the beginning of a new source file.  */
15953
15954 static void
15955 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
15956 {
15957   if (flag_eliminate_dwarf2_dups)
15958     {
15959       /* Record the beginning of the file for break_out_includes.  */
15960       dw_die_ref bincl_die;
15961
15962       bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die, NULL);
15963       add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
15964     }
15965
15966   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
15967     {
15968       int file_num = maybe_emit_file (lookup_filename (filename));
15969
15970       switch_to_section (debug_macinfo_section);
15971       dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
15972       dw2_asm_output_data_uleb128 (lineno, "Included from line number %d",
15973                                    lineno);
15974
15975       dw2_asm_output_data_uleb128 (file_num, "file %s", filename);
15976     }
15977 }
15978
15979 /* Record the end of a source file.  */
15980
15981 static void
15982 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
15983 {
15984   if (flag_eliminate_dwarf2_dups)
15985     /* Record the end of the file for break_out_includes.  */
15986     new_die (DW_TAG_GNU_EINCL, comp_unit_die, NULL);
15987
15988   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
15989     {
15990       switch_to_section (debug_macinfo_section);
15991       dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
15992     }
15993 }
15994
15995 /* Called from debug_define in toplev.c.  The `buffer' parameter contains
15996    the tail part of the directive line, i.e. the part which is past the
15997    initial whitespace, #, whitespace, directive-name, whitespace part.  */
15998
15999 static void
16000 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
16001                   const char *buffer ATTRIBUTE_UNUSED)
16002 {
16003   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
16004     {
16005       switch_to_section (debug_macinfo_section);
16006       dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
16007       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
16008       dw2_asm_output_nstring (buffer, -1, "The macro");
16009     }
16010 }
16011
16012 /* Called from debug_undef in toplev.c.  The `buffer' parameter contains
16013    the tail part of the directive line, i.e. the part which is past the
16014    initial whitespace, #, whitespace, directive-name, whitespace part.  */
16015
16016 static void
16017 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
16018                  const char *buffer ATTRIBUTE_UNUSED)
16019 {
16020   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
16021     {
16022       switch_to_section (debug_macinfo_section);
16023       dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
16024       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
16025       dw2_asm_output_nstring (buffer, -1, "The macro");
16026     }
16027 }
16028
16029 /* Set up for Dwarf output at the start of compilation.  */
16030
16031 static void
16032 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
16033 {
16034   /* Allocate the file_table.  */
16035   file_table = htab_create_ggc (50, file_table_hash,
16036                                 file_table_eq, NULL);
16037
16038   /* Allocate the decl_die_table.  */
16039   decl_die_table = htab_create_ggc (10, decl_die_table_hash,
16040                                     decl_die_table_eq, NULL);
16041
16042   /* Allocate the decl_loc_table.  */
16043   decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
16044                                     decl_loc_table_eq, NULL);
16045
16046   /* Allocate the initial hunk of the decl_scope_table.  */
16047   decl_scope_table = VEC_alloc (tree, gc, 256);
16048
16049   /* Allocate the initial hunk of the abbrev_die_table.  */
16050   abbrev_die_table = GGC_CNEWVEC (dw_die_ref, ABBREV_DIE_TABLE_INCREMENT);
16051   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
16052   /* Zero-th entry is allocated, but unused.  */
16053   abbrev_die_table_in_use = 1;
16054
16055   /* Allocate the initial hunk of the line_info_table.  */
16056   line_info_table = GGC_CNEWVEC (dw_line_info_entry, LINE_INFO_TABLE_INCREMENT);
16057   line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
16058
16059   /* Zero-th entry is allocated, but unused.  */
16060   line_info_table_in_use = 1;
16061
16062   /* Allocate the pubtypes and pubnames vectors.  */
16063   pubname_table = VEC_alloc (pubname_entry, gc, 32);
16064   pubtype_table = VEC_alloc (pubname_entry, gc, 32);
16065
16066   /* Generate the initial DIE for the .debug section.  Note that the (string)
16067      value given in the DW_AT_name attribute of the DW_TAG_compile_unit DIE
16068      will (typically) be a relative pathname and that this pathname should be
16069      taken as being relative to the directory from which the compiler was
16070      invoked when the given (base) source file was compiled.  We will fill
16071      in this value in dwarf2out_finish.  */
16072   comp_unit_die = gen_compile_unit_die (NULL);
16073
16074   incomplete_types = VEC_alloc (tree, gc, 64);
16075
16076   used_rtx_array = VEC_alloc (rtx, gc, 32);
16077
16078   debug_info_section = get_section (DEBUG_INFO_SECTION,
16079                                     SECTION_DEBUG, NULL);
16080   debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
16081                                       SECTION_DEBUG, NULL);
16082   debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
16083                                        SECTION_DEBUG, NULL);
16084   debug_macinfo_section = get_section (DEBUG_MACINFO_SECTION,
16085                                        SECTION_DEBUG, NULL);
16086   debug_line_section = get_section (DEBUG_LINE_SECTION,
16087                                     SECTION_DEBUG, NULL);
16088   debug_loc_section = get_section (DEBUG_LOC_SECTION,
16089                                    SECTION_DEBUG, NULL);
16090   debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
16091                                         SECTION_DEBUG, NULL);
16092 #ifdef DEBUG_PUBTYPES_SECTION
16093   debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
16094                                         SECTION_DEBUG, NULL);
16095 #endif
16096   debug_str_section = get_section (DEBUG_STR_SECTION,
16097                                    DEBUG_STR_SECTION_FLAGS, NULL);
16098   debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
16099                                       SECTION_DEBUG, NULL);
16100   debug_frame_section = get_section (DEBUG_FRAME_SECTION,
16101                                      SECTION_DEBUG, NULL);
16102
16103   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
16104   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
16105                                DEBUG_ABBREV_SECTION_LABEL, 0);
16106   ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
16107   ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
16108                                COLD_TEXT_SECTION_LABEL, 0);
16109   ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
16110
16111   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
16112                                DEBUG_INFO_SECTION_LABEL, 0);
16113   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
16114                                DEBUG_LINE_SECTION_LABEL, 0);
16115   ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
16116                                DEBUG_RANGES_SECTION_LABEL, 0);
16117   switch_to_section (debug_abbrev_section);
16118   ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
16119   switch_to_section (debug_info_section);
16120   ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
16121   switch_to_section (debug_line_section);
16122   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
16123
16124   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
16125     {
16126       switch_to_section (debug_macinfo_section);
16127       ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
16128                                    DEBUG_MACINFO_SECTION_LABEL, 0);
16129       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
16130     }
16131
16132   switch_to_section (text_section);
16133   ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
16134   if (flag_reorder_blocks_and_partition)
16135     {
16136       cold_text_section = unlikely_text_section ();
16137       switch_to_section (cold_text_section);
16138       ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
16139     }
16140 }
16141
16142 /* A helper function for dwarf2out_finish called through
16143    ht_forall.  Emit one queued .debug_str string.  */
16144
16145 static int
16146 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
16147 {
16148   struct indirect_string_node *node = (struct indirect_string_node *) *h;
16149
16150   if (node->form == DW_FORM_strp)
16151     {
16152       switch_to_section (debug_str_section);
16153       ASM_OUTPUT_LABEL (asm_out_file, node->label);
16154       assemble_string (node->str, strlen (node->str) + 1);
16155     }
16156
16157   return 1;
16158 }
16159
16160 #if ENABLE_ASSERT_CHECKING
16161 /* Verify that all marks are clear.  */
16162
16163 static void
16164 verify_marks_clear (dw_die_ref die)
16165 {
16166   dw_die_ref c;
16167
16168   gcc_assert (! die->die_mark);
16169   FOR_EACH_CHILD (die, c, verify_marks_clear (c));
16170 }
16171 #endif /* ENABLE_ASSERT_CHECKING */
16172
16173 /* Clear the marks for a die and its children.
16174    Be cool if the mark isn't set.  */
16175
16176 static void
16177 prune_unmark_dies (dw_die_ref die)
16178 {
16179   dw_die_ref c;
16180
16181   if (die->die_mark)
16182     die->die_mark = 0;
16183   FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
16184 }
16185
16186 /* Given DIE that we're marking as used, find any other dies
16187    it references as attributes and mark them as used.  */
16188
16189 static void
16190 prune_unused_types_walk_attribs (dw_die_ref die)
16191 {
16192   dw_attr_ref a;
16193   unsigned ix;
16194
16195   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
16196     {
16197       if (a->dw_attr_val.val_class == dw_val_class_die_ref)
16198         {
16199           /* A reference to another DIE.
16200              Make sure that it will get emitted.  */
16201           prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
16202         }
16203       /* Set the string's refcount to 0 so that prune_unused_types_mark
16204          accounts properly for it.  */
16205       if (AT_class (a) == dw_val_class_str)
16206         a->dw_attr_val.v.val_str->refcount = 0;
16207     }
16208 }
16209
16210
16211 /* Mark DIE as being used.  If DOKIDS is true, then walk down
16212    to DIE's children.  */
16213
16214 static void
16215 prune_unused_types_mark (dw_die_ref die, int dokids)
16216 {
16217   dw_die_ref c;
16218
16219   if (die->die_mark == 0)
16220     {
16221       /* We haven't done this node yet.  Mark it as used.  */
16222       die->die_mark = 1;
16223
16224       /* We also have to mark its parents as used.
16225          (But we don't want to mark our parents' kids due to this.)  */
16226       if (die->die_parent)
16227         prune_unused_types_mark (die->die_parent, 0);
16228
16229       /* Mark any referenced nodes.  */
16230       prune_unused_types_walk_attribs (die);
16231
16232       /* If this node is a specification,
16233          also mark the definition, if it exists.  */
16234       if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
16235         prune_unused_types_mark (die->die_definition, 1);
16236     }
16237
16238   if (dokids && die->die_mark != 2)
16239     {
16240       /* We need to walk the children, but haven't done so yet.
16241          Remember that we've walked the kids.  */
16242       die->die_mark = 2;
16243
16244       /* If this is an array type, we need to make sure our
16245          kids get marked, even if they're types.  */
16246       if (die->die_tag == DW_TAG_array_type)
16247         FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
16248       else
16249         FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
16250     }
16251 }
16252
16253 /* For local classes, look if any static member functions were emitted
16254    and if so, mark them.  */
16255
16256 static void
16257 prune_unused_types_walk_local_classes (dw_die_ref die)
16258 {
16259   dw_die_ref c;
16260
16261   if (die->die_mark == 2)
16262     return;
16263
16264   switch (die->die_tag)
16265     {
16266     case DW_TAG_structure_type:
16267     case DW_TAG_union_type:
16268     case DW_TAG_class_type:
16269       break;
16270
16271     case DW_TAG_subprogram:
16272       if (!get_AT_flag (die, DW_AT_declaration)
16273           || die->die_definition != NULL)
16274         prune_unused_types_mark (die, 1);
16275       return;
16276
16277     default:
16278       return;
16279     }
16280
16281   /* Mark children.  */
16282   FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
16283 }
16284
16285 /* Walk the tree DIE and mark types that we actually use.  */
16286
16287 static void
16288 prune_unused_types_walk (dw_die_ref die)
16289 {
16290   dw_die_ref c;
16291
16292   /* Don't do anything if this node is already marked and
16293      children have been marked as well.  */
16294   if (die->die_mark == 2)
16295     return;
16296
16297   switch (die->die_tag)
16298     {
16299     case DW_TAG_structure_type:
16300     case DW_TAG_union_type:
16301     case DW_TAG_class_type:
16302       if (die->die_perennial_p)
16303         break;
16304
16305       for (c = die->die_parent; c; c = c->die_parent)
16306         if (c->die_tag == DW_TAG_subprogram)
16307           break;
16308
16309       /* Finding used static member functions inside of classes
16310          is needed just for local classes, because for other classes
16311          static member function DIEs with DW_AT_specification
16312          are emitted outside of the DW_TAG_*_type.  If we ever change
16313          it, we'd need to call this even for non-local classes.  */
16314       if (c)
16315         prune_unused_types_walk_local_classes (die);
16316
16317       /* It's a type node --- don't mark it.  */
16318       return;
16319
16320     case DW_TAG_const_type:
16321     case DW_TAG_packed_type:
16322     case DW_TAG_pointer_type:
16323     case DW_TAG_reference_type:
16324     case DW_TAG_volatile_type:
16325     case DW_TAG_typedef:
16326     case DW_TAG_array_type:
16327     case DW_TAG_interface_type:
16328     case DW_TAG_friend:
16329     case DW_TAG_variant_part:
16330     case DW_TAG_enumeration_type:
16331     case DW_TAG_subroutine_type:
16332     case DW_TAG_string_type:
16333     case DW_TAG_set_type:
16334     case DW_TAG_subrange_type:
16335     case DW_TAG_ptr_to_member_type:
16336     case DW_TAG_file_type:
16337       if (die->die_perennial_p)
16338         break;
16339
16340       /* It's a type node --- don't mark it.  */
16341       return;
16342
16343     default:
16344       /* Mark everything else.  */
16345       break;
16346   }
16347
16348   if (die->die_mark == 0)
16349     {
16350       die->die_mark = 1;
16351
16352       /* Now, mark any dies referenced from here.  */
16353       prune_unused_types_walk_attribs (die);
16354     }
16355
16356   die->die_mark = 2;
16357
16358   /* Mark children.  */
16359   FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
16360 }
16361
16362 /* Increment the string counts on strings referred to from DIE's
16363    attributes.  */
16364
16365 static void
16366 prune_unused_types_update_strings (dw_die_ref die)
16367 {
16368   dw_attr_ref a;
16369   unsigned ix;
16370
16371   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
16372     if (AT_class (a) == dw_val_class_str)
16373       {
16374         struct indirect_string_node *s = a->dw_attr_val.v.val_str;
16375         s->refcount++;
16376         /* Avoid unnecessarily putting strings that are used less than
16377            twice in the hash table.  */
16378         if (s->refcount
16379             == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
16380           {
16381             void ** slot;
16382             slot = htab_find_slot_with_hash (debug_str_hash, s->str,
16383                                              htab_hash_string (s->str),
16384                                              INSERT);
16385             gcc_assert (*slot == NULL);
16386             *slot = s;
16387           }
16388       }
16389 }
16390
16391 /* Remove from the tree DIE any dies that aren't marked.  */
16392
16393 static void
16394 prune_unused_types_prune (dw_die_ref die)
16395 {
16396   dw_die_ref c;
16397
16398   gcc_assert (die->die_mark);
16399   prune_unused_types_update_strings (die);
16400
16401   if (! die->die_child)
16402     return;
16403
16404   c = die->die_child;
16405   do {
16406     dw_die_ref prev = c;
16407     for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
16408       if (c == die->die_child)
16409         {
16410           /* No marked children between 'prev' and the end of the list.  */
16411           if (prev == c)
16412             /* No marked children at all.  */
16413             die->die_child = NULL;
16414           else
16415             {
16416               prev->die_sib = c->die_sib;
16417               die->die_child = prev;
16418             }
16419           return;
16420         }
16421
16422     if (c != prev->die_sib)
16423       prev->die_sib = c;
16424     prune_unused_types_prune (c);
16425   } while (c != die->die_child);
16426 }
16427
16428
16429 /* Remove dies representing declarations that we never use.  */
16430
16431 static void
16432 prune_unused_types (void)
16433 {
16434   unsigned int i;
16435   limbo_die_node *node;
16436   pubname_ref pub;
16437
16438 #if ENABLE_ASSERT_CHECKING
16439   /* All the marks should already be clear.  */
16440   verify_marks_clear (comp_unit_die);
16441   for (node = limbo_die_list; node; node = node->next)
16442     verify_marks_clear (node->die);
16443 #endif /* ENABLE_ASSERT_CHECKING */
16444
16445   /* Set the mark on nodes that are actually used.  */
16446   prune_unused_types_walk (comp_unit_die);
16447   for (node = limbo_die_list; node; node = node->next)
16448     prune_unused_types_walk (node->die);
16449
16450   /* Also set the mark on nodes referenced from the
16451      pubname_table or arange_table.  */
16452   for (i = 0; VEC_iterate (pubname_entry, pubname_table, i, pub); i++)
16453     prune_unused_types_mark (pub->die, 1);
16454   for (i = 0; i < arange_table_in_use; i++)
16455     prune_unused_types_mark (arange_table[i], 1);
16456
16457   /* Get rid of nodes that aren't marked; and update the string counts.  */
16458   if (debug_str_hash)
16459     htab_empty (debug_str_hash);
16460   prune_unused_types_prune (comp_unit_die);
16461   for (node = limbo_die_list; node; node = node->next)
16462     prune_unused_types_prune (node->die);
16463
16464   /* Leave the marks clear.  */
16465   prune_unmark_dies (comp_unit_die);
16466   for (node = limbo_die_list; node; node = node->next)
16467     prune_unmark_dies (node->die);
16468 }
16469
16470 /* Set the parameter to true if there are any relative pathnames in
16471    the file table.  */
16472 static int
16473 file_table_relative_p (void ** slot, void *param)
16474 {
16475   bool *p = (bool *) param;
16476   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
16477   if (!IS_ABSOLUTE_PATH (d->filename))
16478     {
16479       *p = true;
16480       return 0;
16481     }
16482   return 1;
16483 }
16484
16485 /* Output stuff that dwarf requires at the end of every file,
16486    and generate the DWARF-2 debugging info.  */
16487
16488 static void
16489 dwarf2out_finish (const char *filename)
16490 {
16491   limbo_die_node *node, *next_node;
16492   dw_die_ref die = 0;
16493   unsigned int i;
16494
16495   /* Add the name for the main input file now.  We delayed this from
16496      dwarf2out_init to avoid complications with PCH.  */
16497   add_name_attribute (comp_unit_die, remap_debug_filename (filename));
16498   if (!IS_ABSOLUTE_PATH (filename))
16499     add_comp_dir_attribute (comp_unit_die);
16500   else if (get_AT (comp_unit_die, DW_AT_comp_dir) == NULL)
16501     {
16502       bool p = false;
16503       htab_traverse (file_table, file_table_relative_p, &p);
16504       if (p)
16505         add_comp_dir_attribute (comp_unit_die);
16506     }
16507
16508   for (i = 0; i < VEC_length (deferred_locations, deferred_locations_list); i++)
16509     {
16510       add_location_or_const_value_attribute (
16511         VEC_index (deferred_locations, deferred_locations_list, i)->die,
16512         VEC_index (deferred_locations, deferred_locations_list, i)->variable,
16513         DW_AT_location);
16514     }
16515
16516   /* Traverse the limbo die list, and add parent/child links.  The only
16517      dies without parents that should be here are concrete instances of
16518      inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
16519      For concrete instances, we can get the parent die from the abstract
16520      instance.  */
16521   for (node = limbo_die_list; node; node = next_node)
16522     {
16523       next_node = node->next;
16524       die = node->die;
16525
16526       if (die->die_parent == NULL)
16527         {
16528           dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
16529
16530           if (origin)
16531             add_child_die (origin->die_parent, die);
16532           else if (die == comp_unit_die)
16533             ;
16534           else if (errorcount > 0 || sorrycount > 0)
16535             /* It's OK to be confused by errors in the input.  */
16536             add_child_die (comp_unit_die, die);
16537           else
16538             {
16539               /* In certain situations, the lexical block containing a
16540                  nested function can be optimized away, which results
16541                  in the nested function die being orphaned.  Likewise
16542                  with the return type of that nested function.  Force
16543                  this to be a child of the containing function.
16544
16545                  It may happen that even the containing function got fully
16546                  inlined and optimized out.  In that case we are lost and
16547                  assign the empty child.  This should not be big issue as
16548                  the function is likely unreachable too.  */
16549               tree context = NULL_TREE;
16550
16551               gcc_assert (node->created_for);
16552
16553               if (DECL_P (node->created_for))
16554                 context = DECL_CONTEXT (node->created_for);
16555               else if (TYPE_P (node->created_for))
16556                 context = TYPE_CONTEXT (node->created_for);
16557
16558               gcc_assert (context
16559                           && (TREE_CODE (context) == FUNCTION_DECL
16560                               || TREE_CODE (context) == NAMESPACE_DECL));
16561
16562               origin = lookup_decl_die (context);
16563               if (origin)
16564                 add_child_die (origin, die);
16565               else
16566                 add_child_die (comp_unit_die, die);
16567             }
16568         }
16569     }
16570
16571   limbo_die_list = NULL;
16572
16573   /* Walk through the list of incomplete types again, trying once more to
16574      emit full debugging info for them.  */
16575   retry_incomplete_types ();
16576
16577   if (flag_eliminate_unused_debug_types)
16578     prune_unused_types ();
16579
16580   /* Generate separate CUs for each of the include files we've seen.
16581      They will go into limbo_die_list.  */
16582   if (flag_eliminate_dwarf2_dups)
16583     break_out_includes (comp_unit_die);
16584
16585   /* Traverse the DIE's and add add sibling attributes to those DIE's
16586      that have children.  */
16587   add_sibling_attributes (comp_unit_die);
16588   for (node = limbo_die_list; node; node = node->next)
16589     add_sibling_attributes (node->die);
16590
16591   /* Output a terminator label for the .text section.  */
16592   switch_to_section (text_section);
16593   targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
16594   if (flag_reorder_blocks_and_partition)
16595     {
16596       switch_to_section (unlikely_text_section ());
16597       targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
16598     }
16599
16600   /* We can only use the low/high_pc attributes if all of the code was
16601      in .text.  */
16602   if (!have_multiple_function_sections)
16603     {
16604       add_AT_lbl_id (comp_unit_die, DW_AT_low_pc, text_section_label);
16605       add_AT_lbl_id (comp_unit_die, DW_AT_high_pc, text_end_label);
16606     }
16607
16608   else
16609     {
16610       unsigned fde_idx = 0;
16611
16612       /* We need to give .debug_loc and .debug_ranges an appropriate
16613          "base address".  Use zero so that these addresses become
16614          absolute.  Historically, we've emitted the unexpected
16615          DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
16616          Emit both to give time for other tools to adapt.  */
16617       add_AT_addr (comp_unit_die, DW_AT_low_pc, const0_rtx);
16618       add_AT_addr (comp_unit_die, DW_AT_entry_pc, const0_rtx);
16619
16620       add_AT_range_list (comp_unit_die, DW_AT_ranges,
16621                          add_ranges_by_labels (text_section_label,
16622                                                text_end_label));
16623       if (flag_reorder_blocks_and_partition)
16624         add_ranges_by_labels (cold_text_section_label,
16625                               cold_end_label);
16626
16627       for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
16628         {
16629           dw_fde_ref fde = &fde_table[fde_idx];
16630
16631           if (fde->dw_fde_switched_sections)
16632             {
16633               add_ranges_by_labels (fde->dw_fde_hot_section_label,
16634                                     fde->dw_fde_hot_section_end_label);
16635               add_ranges_by_labels (fde->dw_fde_unlikely_section_label,
16636                                     fde->dw_fde_unlikely_section_end_label);
16637             }
16638           else
16639             add_ranges_by_labels (fde->dw_fde_begin,
16640                                   fde->dw_fde_end);
16641         }
16642
16643       add_ranges (NULL);
16644     }
16645
16646   /* Output location list section if necessary.  */
16647   if (have_location_lists)
16648     {
16649       /* Output the location lists info.  */
16650       switch_to_section (debug_loc_section);
16651       ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
16652                                    DEBUG_LOC_SECTION_LABEL, 0);
16653       ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
16654       output_location_lists (die);
16655     }
16656
16657   if (debug_info_level >= DINFO_LEVEL_NORMAL)
16658     add_AT_lineptr (comp_unit_die, DW_AT_stmt_list,
16659                     debug_line_section_label);
16660
16661   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
16662     add_AT_macptr (comp_unit_die, DW_AT_macro_info, macinfo_section_label);
16663
16664   /* Output all of the compilation units.  We put the main one last so that
16665      the offsets are available to output_pubnames.  */
16666   for (node = limbo_die_list; node; node = node->next)
16667     output_comp_unit (node->die, 0);
16668
16669   /* Output the main compilation unit if non-empty or if .debug_macinfo
16670      has been emitted.  */
16671   output_comp_unit (comp_unit_die, debug_info_level >= DINFO_LEVEL_VERBOSE);
16672
16673   /* Output the abbreviation table.  */
16674   switch_to_section (debug_abbrev_section);
16675   output_abbrev_section ();
16676
16677   /* Output public names table if necessary.  */
16678   if (!VEC_empty (pubname_entry, pubname_table))
16679     {
16680       switch_to_section (debug_pubnames_section);
16681       output_pubnames (pubname_table);
16682     }
16683
16684 #ifdef DEBUG_PUBTYPES_SECTION
16685   /* Output public types table if necessary.  */
16686   if (!VEC_empty (pubname_entry, pubtype_table))
16687     {
16688       switch_to_section (debug_pubtypes_section);
16689       output_pubnames (pubtype_table);
16690     }
16691 #endif
16692
16693   /* Output the address range information.  We only put functions in the arange
16694      table, so don't write it out if we don't have any.  */
16695   if (fde_table_in_use)
16696     {
16697       switch_to_section (debug_aranges_section);
16698       output_aranges ();
16699     }
16700
16701   /* Output ranges section if necessary.  */
16702   if (ranges_table_in_use)
16703     {
16704       switch_to_section (debug_ranges_section);
16705       ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
16706       output_ranges ();
16707     }
16708
16709   /* Output the source line correspondence table.  We must do this
16710      even if there is no line information.  Otherwise, on an empty
16711      translation unit, we will generate a present, but empty,
16712      .debug_info section.  IRIX 6.5 `nm' will then complain when
16713      examining the file.  This is done late so that any filenames
16714      used by the debug_info section are marked as 'used'.  */
16715   if (! DWARF2_ASM_LINE_DEBUG_INFO)
16716     {
16717       switch_to_section (debug_line_section);
16718       output_line_info ();
16719     }
16720
16721   /* Have to end the macro section.  */
16722   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
16723     {
16724       switch_to_section (debug_macinfo_section);
16725       dw2_asm_output_data (1, 0, "End compilation unit");
16726     }
16727
16728   /* If we emitted any DW_FORM_strp form attribute, output the string
16729      table too.  */
16730   if (debug_str_hash)
16731     htab_traverse (debug_str_hash, output_indirect_string, NULL);
16732 }
16733 #else
16734
16735 /* This should never be used, but its address is needed for comparisons.  */
16736 const struct gcc_debug_hooks dwarf2_debug_hooks;
16737
16738 #endif /* DWARF2_DEBUGGING_INFO */
16739
16740 #include "gt-dwarf2out.h"