OSDN Git Service

PR debug/39267
[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 = TREE_NOTHROW (current_function_decl);
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   dw_val_node dw_loc_oprnd1;
3503   dw_val_node dw_loc_oprnd2;
3504   int dw_loc_addr;
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
4553 /* The debug hooks structure.  */
4554
4555 const struct gcc_debug_hooks dwarf2_debug_hooks =
4556 {
4557   dwarf2out_init,
4558   dwarf2out_finish,
4559   dwarf2out_define,
4560   dwarf2out_undef,
4561   dwarf2out_start_source_file,
4562   dwarf2out_end_source_file,
4563   dwarf2out_begin_block,
4564   dwarf2out_end_block,
4565   dwarf2out_ignore_block,
4566   dwarf2out_source_line,
4567   dwarf2out_begin_prologue,
4568   debug_nothing_int_charstar,   /* end_prologue */
4569   dwarf2out_end_epilogue,
4570   dwarf2out_begin_function,
4571   debug_nothing_int,            /* end_function */
4572   dwarf2out_decl,               /* function_decl */
4573   dwarf2out_global_decl,
4574   dwarf2out_type_decl,          /* type_decl */
4575   dwarf2out_imported_module_or_decl,
4576   debug_nothing_tree,           /* deferred_inline_function */
4577   /* The DWARF 2 backend tries to reduce debugging bloat by not
4578      emitting the abstract description of inline functions until
4579      something tries to reference them.  */
4580   dwarf2out_abstract_function,  /* outlining_inline_function */
4581   debug_nothing_rtx,            /* label */
4582   debug_nothing_int,            /* handle_pch */
4583   dwarf2out_var_location,
4584   dwarf2out_switch_text_section,
4585   1                             /* start_end_main_source_file */
4586 };
4587 #endif
4588 \f
4589 /* NOTE: In the comments in this file, many references are made to
4590    "Debugging Information Entries".  This term is abbreviated as `DIE'
4591    throughout the remainder of this file.  */
4592
4593 /* An internal representation of the DWARF output is built, and then
4594    walked to generate the DWARF debugging info.  The walk of the internal
4595    representation is done after the entire program has been compiled.
4596    The types below are used to describe the internal representation.  */
4597
4598 /* Various DIE's use offsets relative to the beginning of the
4599    .debug_info section to refer to each other.  */
4600
4601 typedef long int dw_offset;
4602
4603 /* Define typedefs here to avoid circular dependencies.  */
4604
4605 typedef struct dw_attr_struct *dw_attr_ref;
4606 typedef struct dw_line_info_struct *dw_line_info_ref;
4607 typedef struct dw_separate_line_info_struct *dw_separate_line_info_ref;
4608 typedef struct pubname_struct *pubname_ref;
4609 typedef struct dw_ranges_struct *dw_ranges_ref;
4610 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
4611
4612 /* Each entry in the line_info_table maintains the file and
4613    line number associated with the label generated for that
4614    entry.  The label gives the PC value associated with
4615    the line number entry.  */
4616
4617 typedef struct dw_line_info_struct GTY(())
4618 {
4619   unsigned long dw_file_num;
4620   unsigned long dw_line_num;
4621 }
4622 dw_line_info_entry;
4623
4624 /* Line information for functions in separate sections; each one gets its
4625    own sequence.  */
4626 typedef struct dw_separate_line_info_struct GTY(())
4627 {
4628   unsigned long dw_file_num;
4629   unsigned long dw_line_num;
4630   unsigned long function;
4631 }
4632 dw_separate_line_info_entry;
4633
4634 /* Each DIE attribute has a field specifying the attribute kind,
4635    a link to the next attribute in the chain, and an attribute value.
4636    Attributes are typically linked below the DIE they modify.  */
4637
4638 typedef struct dw_attr_struct GTY(())
4639 {
4640   enum dwarf_attribute dw_attr;
4641   dw_val_node dw_attr_val;
4642 }
4643 dw_attr_node;
4644
4645 DEF_VEC_O(dw_attr_node);
4646 DEF_VEC_ALLOC_O(dw_attr_node,gc);
4647
4648 /* The Debugging Information Entry (DIE) structure.  DIEs form a tree.
4649    The children of each node form a circular list linked by
4650    die_sib.  die_child points to the node *before* the "first" child node.  */
4651
4652 typedef struct die_struct GTY((chain_circular ("%h.die_sib")))
4653 {
4654   enum dwarf_tag die_tag;
4655   char *die_symbol;
4656   VEC(dw_attr_node,gc) * die_attr;
4657   dw_die_ref die_parent;
4658   dw_die_ref die_child;
4659   dw_die_ref die_sib;
4660   dw_die_ref die_definition; /* ref from a specification to its definition */
4661   dw_offset die_offset;
4662   unsigned long die_abbrev;
4663   int die_mark;
4664   /* Die is used and must not be pruned as unused.  */
4665   int die_perennial_p;
4666   unsigned int decl_id;
4667 }
4668 die_node;
4669
4670 /* Evaluate 'expr' while 'c' is set to each child of DIE in order.  */
4671 #define FOR_EACH_CHILD(die, c, expr) do {       \
4672   c = die->die_child;                           \
4673   if (c) do {                                   \
4674     c = c->die_sib;                             \
4675     expr;                                       \
4676   } while (c != die->die_child);                \
4677 } while (0)
4678
4679 /* The pubname structure */
4680
4681 typedef struct pubname_struct GTY(())
4682 {
4683   dw_die_ref die;
4684   const char *name;
4685 }
4686 pubname_entry;
4687
4688 DEF_VEC_O(pubname_entry);
4689 DEF_VEC_ALLOC_O(pubname_entry, gc);
4690
4691 struct dw_ranges_struct GTY(())
4692 {
4693   /* If this is positive, it's a block number, otherwise it's a
4694      bitwise-negated index into dw_ranges_by_label.  */
4695   int num;
4696 };
4697
4698 struct dw_ranges_by_label_struct GTY(())
4699 {
4700   const char *begin;
4701   const char *end;
4702 };
4703
4704 /* The limbo die list structure.  */
4705 typedef struct limbo_die_struct GTY(())
4706 {
4707   dw_die_ref die;
4708   tree created_for;
4709   struct limbo_die_struct *next;
4710 }
4711 limbo_die_node;
4712
4713 /* How to start an assembler comment.  */
4714 #ifndef ASM_COMMENT_START
4715 #define ASM_COMMENT_START ";#"
4716 #endif
4717
4718 /* Define a macro which returns nonzero for a TYPE_DECL which was
4719    implicitly generated for a tagged type.
4720
4721    Note that unlike the gcc front end (which generates a NULL named
4722    TYPE_DECL node for each complete tagged type, each array type, and
4723    each function type node created) the g++ front end generates a
4724    _named_ TYPE_DECL node for each tagged type node created.
4725    These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
4726    generate a DW_TAG_typedef DIE for them.  */
4727
4728 #define TYPE_DECL_IS_STUB(decl)                         \
4729   (DECL_NAME (decl) == NULL_TREE                        \
4730    || (DECL_ARTIFICIAL (decl)                           \
4731        && is_tagged_type (TREE_TYPE (decl))             \
4732        && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl)))  \
4733            /* This is necessary for stub decls that     \
4734               appear in nested inline functions.  */    \
4735            || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
4736                && (decl_ultimate_origin (decl)          \
4737                    == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
4738
4739 /* Information concerning the compilation unit's programming
4740    language, and compiler version.  */
4741
4742 /* Fixed size portion of the DWARF compilation unit header.  */
4743 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
4744   (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
4745
4746 /* Fixed size portion of public names info.  */
4747 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
4748
4749 /* Fixed size portion of the address range info.  */
4750 #define DWARF_ARANGES_HEADER_SIZE                                       \
4751   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4,      \
4752                 DWARF2_ADDR_SIZE * 2)                                   \
4753    - DWARF_INITIAL_LENGTH_SIZE)
4754
4755 /* Size of padding portion in the address range info.  It must be
4756    aligned to twice the pointer size.  */
4757 #define DWARF_ARANGES_PAD_SIZE \
4758   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
4759                 DWARF2_ADDR_SIZE * 2)                              \
4760    - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
4761
4762 /* Use assembler line directives if available.  */
4763 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
4764 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
4765 #define DWARF2_ASM_LINE_DEBUG_INFO 1
4766 #else
4767 #define DWARF2_ASM_LINE_DEBUG_INFO 0
4768 #endif
4769 #endif
4770
4771 /* Minimum line offset in a special line info. opcode.
4772    This value was chosen to give a reasonable range of values.  */
4773 #define DWARF_LINE_BASE  -10
4774
4775 /* First special line opcode - leave room for the standard opcodes.  */
4776 #define DWARF_LINE_OPCODE_BASE  10
4777
4778 /* Range of line offsets in a special line info. opcode.  */
4779 #define DWARF_LINE_RANGE  (254-DWARF_LINE_OPCODE_BASE+1)
4780
4781 /* Flag that indicates the initial value of the is_stmt_start flag.
4782    In the present implementation, we do not mark any lines as
4783    the beginning of a source statement, because that information
4784    is not made available by the GCC front-end.  */
4785 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
4786
4787 #ifdef DWARF2_DEBUGGING_INFO
4788 /* This location is used by calc_die_sizes() to keep track
4789    the offset of each DIE within the .debug_info section.  */
4790 static unsigned long next_die_offset;
4791 #endif
4792
4793 /* Record the root of the DIE's built for the current compilation unit.  */
4794 static GTY(()) dw_die_ref comp_unit_die;
4795
4796 /* A list of DIEs with a NULL parent waiting to be relocated.  */
4797 static GTY(()) limbo_die_node *limbo_die_list;
4798
4799 /* Filenames referenced by this compilation unit.  */
4800 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
4801
4802 /* A hash table of references to DIE's that describe declarations.
4803    The key is a DECL_UID() which is a unique number identifying each decl.  */
4804 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
4805
4806 /* A hash table of references to DIE's that describe COMMON blocks.
4807    The key is DECL_UID() ^ die_parent.  */
4808 static GTY ((param_is (struct die_struct))) htab_t common_block_die_table;
4809
4810 /* Node of the variable location list.  */
4811 struct var_loc_node GTY ((chain_next ("%h.next")))
4812 {
4813   rtx GTY (()) var_loc_note;
4814   const char * GTY (()) label;
4815   const char * GTY (()) section_label;
4816   struct var_loc_node * GTY (()) next;
4817 };
4818
4819 /* Variable location list.  */
4820 struct var_loc_list_def GTY (())
4821 {
4822   struct var_loc_node * GTY (()) first;
4823
4824   /* Do not mark the last element of the chained list because
4825      it is marked through the chain.  */
4826   struct var_loc_node * GTY ((skip ("%h"))) last;
4827
4828   /* DECL_UID of the variable decl.  */
4829   unsigned int decl_id;
4830 };
4831 typedef struct var_loc_list_def var_loc_list;
4832
4833
4834 /* Table of decl location linked lists.  */
4835 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
4836
4837 /* A pointer to the base of a list of references to DIE's that
4838    are uniquely identified by their tag, presence/absence of
4839    children DIE's, and list of attribute/value pairs.  */
4840 static GTY((length ("abbrev_die_table_allocated")))
4841   dw_die_ref *abbrev_die_table;
4842
4843 /* Number of elements currently allocated for abbrev_die_table.  */
4844 static GTY(()) unsigned abbrev_die_table_allocated;
4845
4846 /* Number of elements in type_die_table currently in use.  */
4847 static GTY(()) unsigned abbrev_die_table_in_use;
4848
4849 /* Size (in elements) of increments by which we may expand the
4850    abbrev_die_table.  */
4851 #define ABBREV_DIE_TABLE_INCREMENT 256
4852
4853 /* A pointer to the base of a table that contains line information
4854    for each source code line in .text in the compilation unit.  */
4855 static GTY((length ("line_info_table_allocated")))
4856      dw_line_info_ref line_info_table;
4857
4858 /* Number of elements currently allocated for line_info_table.  */
4859 static GTY(()) unsigned line_info_table_allocated;
4860
4861 /* Number of elements in line_info_table currently in use.  */
4862 static GTY(()) unsigned line_info_table_in_use;
4863
4864 /* A pointer to the base of a table that contains line information
4865    for each source code line outside of .text in the compilation unit.  */
4866 static GTY ((length ("separate_line_info_table_allocated")))
4867      dw_separate_line_info_ref separate_line_info_table;
4868
4869 /* Number of elements currently allocated for separate_line_info_table.  */
4870 static GTY(()) unsigned separate_line_info_table_allocated;
4871
4872 /* Number of elements in separate_line_info_table currently in use.  */
4873 static GTY(()) unsigned separate_line_info_table_in_use;
4874
4875 /* Size (in elements) of increments by which we may expand the
4876    line_info_table.  */
4877 #define LINE_INFO_TABLE_INCREMENT 1024
4878
4879 /* A pointer to the base of a table that contains a list of publicly
4880    accessible names.  */
4881 static GTY (()) VEC (pubname_entry, gc) *  pubname_table;
4882
4883 /* A pointer to the base of a table that contains a list of publicly
4884    accessible types.  */
4885 static GTY (()) VEC (pubname_entry, gc) * pubtype_table;
4886
4887 /* Array of dies for which we should generate .debug_arange info.  */
4888 static GTY((length ("arange_table_allocated"))) dw_die_ref *arange_table;
4889
4890 /* Number of elements currently allocated for arange_table.  */
4891 static GTY(()) unsigned arange_table_allocated;
4892
4893 /* Number of elements in arange_table currently in use.  */
4894 static GTY(()) unsigned arange_table_in_use;
4895
4896 /* Size (in elements) of increments by which we may expand the
4897    arange_table.  */
4898 #define ARANGE_TABLE_INCREMENT 64
4899
4900 /* Array of dies for which we should generate .debug_ranges info.  */
4901 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
4902
4903 /* Number of elements currently allocated for ranges_table.  */
4904 static GTY(()) unsigned ranges_table_allocated;
4905
4906 /* Number of elements in ranges_table currently in use.  */
4907 static GTY(()) unsigned ranges_table_in_use;
4908
4909 /* Array of pairs of labels referenced in ranges_table.  */
4910 static GTY ((length ("ranges_by_label_allocated")))
4911      dw_ranges_by_label_ref ranges_by_label;
4912
4913 /* Number of elements currently allocated for ranges_by_label.  */
4914 static GTY(()) unsigned ranges_by_label_allocated;
4915
4916 /* Number of elements in ranges_by_label currently in use.  */
4917 static GTY(()) unsigned ranges_by_label_in_use;
4918
4919 /* Size (in elements) of increments by which we may expand the
4920    ranges_table.  */
4921 #define RANGES_TABLE_INCREMENT 64
4922
4923 /* Whether we have location lists that need outputting */
4924 static GTY(()) bool have_location_lists;
4925
4926 /* Unique label counter.  */
4927 static GTY(()) unsigned int loclabel_num;
4928
4929 #ifdef DWARF2_DEBUGGING_INFO
4930 /* Record whether the function being analyzed contains inlined functions.  */
4931 static int current_function_has_inlines;
4932 #endif
4933 #if 0 && defined (MIPS_DEBUGGING_INFO)
4934 static int comp_unit_has_inlines;
4935 #endif
4936
4937 /* The last file entry emitted by maybe_emit_file().  */
4938 static GTY(()) struct dwarf_file_data * last_emitted_file;
4939
4940 /* Number of internal labels generated by gen_internal_sym().  */
4941 static GTY(()) int label_num;
4942
4943 /* Cached result of previous call to lookup_filename.  */
4944 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
4945
4946 #ifdef DWARF2_DEBUGGING_INFO
4947
4948 /* Offset from the "steady-state frame pointer" to the frame base,
4949    within the current function.  */
4950 static HOST_WIDE_INT frame_pointer_fb_offset;
4951
4952 /* Forward declarations for functions defined in this file.  */
4953
4954 static int is_pseudo_reg (const_rtx);
4955 static tree type_main_variant (tree);
4956 static int is_tagged_type (const_tree);
4957 static const char *dwarf_tag_name (unsigned);
4958 static const char *dwarf_attr_name (unsigned);
4959 static const char *dwarf_form_name (unsigned);
4960 static tree decl_ultimate_origin (const_tree);
4961 static tree decl_class_context (tree);
4962 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
4963 static inline enum dw_val_class AT_class (dw_attr_ref);
4964 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
4965 static inline unsigned AT_flag (dw_attr_ref);
4966 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
4967 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
4968 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
4969 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
4970 static void add_AT_long_long (dw_die_ref, enum dwarf_attribute, unsigned long,
4971                               unsigned long);
4972 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
4973                                unsigned int, unsigned char *);
4974 static hashval_t debug_str_do_hash (const void *);
4975 static int debug_str_eq (const void *, const void *);
4976 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
4977 static inline const char *AT_string (dw_attr_ref);
4978 static int AT_string_form (dw_attr_ref);
4979 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
4980 static void add_AT_specification (dw_die_ref, dw_die_ref);
4981 static inline dw_die_ref AT_ref (dw_attr_ref);
4982 static inline int AT_ref_external (dw_attr_ref);
4983 static inline void set_AT_ref_external (dw_attr_ref, int);
4984 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
4985 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
4986 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
4987 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
4988                              dw_loc_list_ref);
4989 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
4990 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
4991 static inline rtx AT_addr (dw_attr_ref);
4992 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
4993 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
4994 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
4995 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
4996                            unsigned HOST_WIDE_INT);
4997 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
4998                                unsigned long);
4999 static inline const char *AT_lbl (dw_attr_ref);
5000 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
5001 static const char *get_AT_low_pc (dw_die_ref);
5002 static const char *get_AT_hi_pc (dw_die_ref);
5003 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
5004 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
5005 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
5006 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
5007 static bool is_c_family (void);
5008 static bool is_cxx (void);
5009 static bool is_java (void);
5010 static bool is_fortran (void);
5011 static bool is_ada (void);
5012 static void remove_AT (dw_die_ref, enum dwarf_attribute);
5013 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
5014 static void add_child_die (dw_die_ref, dw_die_ref);
5015 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
5016 static dw_die_ref lookup_type_die (tree);
5017 static void equate_type_number_to_die (tree, dw_die_ref);
5018 static hashval_t decl_die_table_hash (const void *);
5019 static int decl_die_table_eq (const void *, const void *);
5020 static dw_die_ref lookup_decl_die (tree);
5021 static hashval_t common_block_die_table_hash (const void *);
5022 static int common_block_die_table_eq (const void *, const void *);
5023 static hashval_t decl_loc_table_hash (const void *);
5024 static int decl_loc_table_eq (const void *, const void *);
5025 static var_loc_list *lookup_decl_loc (const_tree);
5026 static void equate_decl_number_to_die (tree, dw_die_ref);
5027 static void add_var_loc_to_decl (tree, struct var_loc_node *);
5028 static void print_spaces (FILE *);
5029 static void print_die (dw_die_ref, FILE *);
5030 static void print_dwarf_line_table (FILE *);
5031 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
5032 static dw_die_ref pop_compile_unit (dw_die_ref);
5033 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
5034 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
5035 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
5036 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
5037 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
5038 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
5039 static int same_die_p (dw_die_ref, dw_die_ref, int *);
5040 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
5041 static void compute_section_prefix (dw_die_ref);
5042 static int is_type_die (dw_die_ref);
5043 static int is_comdat_die (dw_die_ref);
5044 static int is_symbol_die (dw_die_ref);
5045 static void assign_symbol_names (dw_die_ref);
5046 static void break_out_includes (dw_die_ref);
5047 static hashval_t htab_cu_hash (const void *);
5048 static int htab_cu_eq (const void *, const void *);
5049 static void htab_cu_del (void *);
5050 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
5051 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
5052 static void add_sibling_attributes (dw_die_ref);
5053 static void build_abbrev_table (dw_die_ref);
5054 static void output_location_lists (dw_die_ref);
5055 static int constant_size (unsigned HOST_WIDE_INT);
5056 static unsigned long size_of_die (dw_die_ref);
5057 static void calc_die_sizes (dw_die_ref);
5058 static void mark_dies (dw_die_ref);
5059 static void unmark_dies (dw_die_ref);
5060 static void unmark_all_dies (dw_die_ref);
5061 static unsigned long size_of_pubnames (VEC (pubname_entry,gc) *);
5062 static unsigned long size_of_aranges (void);
5063 static enum dwarf_form value_format (dw_attr_ref);
5064 static void output_value_format (dw_attr_ref);
5065 static void output_abbrev_section (void);
5066 static void output_die_symbol (dw_die_ref);
5067 static void output_die (dw_die_ref);
5068 static void output_compilation_unit_header (void);
5069 static void output_comp_unit (dw_die_ref, int);
5070 static const char *dwarf2_name (tree, int);
5071 static void add_pubname (tree, dw_die_ref);
5072 static void add_pubname_string (const char *, dw_die_ref);
5073 static void add_pubtype (tree, dw_die_ref);
5074 static void output_pubnames (VEC (pubname_entry,gc) *);
5075 static void add_arange (tree, dw_die_ref);
5076 static void output_aranges (void);
5077 static unsigned int add_ranges_num (int);
5078 static unsigned int add_ranges (const_tree);
5079 static unsigned int add_ranges_by_labels (const char *, const char *);
5080 static void output_ranges (void);
5081 static void output_line_info (void);
5082 static void output_file_names (void);
5083 static dw_die_ref base_type_die (tree);
5084 static int is_base_type (tree);
5085 static bool is_subrange_type (const_tree);
5086 static dw_die_ref subrange_type_die (tree, dw_die_ref);
5087 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
5088 static int type_is_enum (const_tree);
5089 static unsigned int dbx_reg_number (const_rtx);
5090 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
5091 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
5092 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
5093                                                 enum var_init_status);
5094 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
5095                                                      enum var_init_status);
5096 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
5097                                          enum var_init_status);
5098 static int is_based_loc (const_rtx);
5099 static dw_loc_descr_ref mem_loc_descriptor (rtx, enum machine_mode mode,
5100                                             enum var_init_status);
5101 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
5102                                                enum var_init_status);
5103 static dw_loc_descr_ref loc_descriptor (rtx, enum var_init_status);
5104 static dw_loc_descr_ref loc_descriptor_from_tree_1 (tree, int);
5105 static dw_loc_descr_ref loc_descriptor_from_tree (tree);
5106 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
5107 static tree field_type (const_tree);
5108 static unsigned int simple_type_align_in_bits (const_tree);
5109 static unsigned int simple_decl_align_in_bits (const_tree);
5110 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
5111 static HOST_WIDE_INT field_byte_offset (const_tree);
5112 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
5113                                          dw_loc_descr_ref);
5114 static void add_data_member_location_attribute (dw_die_ref, tree);
5115 static void add_const_value_attribute (dw_die_ref, rtx);
5116 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
5117 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
5118 static void insert_float (const_rtx, unsigned char *);
5119 static rtx rtl_for_decl_location (tree);
5120 static void add_location_or_const_value_attribute (dw_die_ref, tree,
5121                                                    enum dwarf_attribute);
5122 static void tree_add_const_value_attribute (dw_die_ref, tree);
5123 static void add_name_attribute (dw_die_ref, const char *);
5124 static void add_comp_dir_attribute (dw_die_ref);
5125 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
5126 static void add_subscript_info (dw_die_ref, tree, bool);
5127 static void add_byte_size_attribute (dw_die_ref, tree);
5128 static void add_bit_offset_attribute (dw_die_ref, tree);
5129 static void add_bit_size_attribute (dw_die_ref, tree);
5130 static void add_prototyped_attribute (dw_die_ref, tree);
5131 static void add_abstract_origin_attribute (dw_die_ref, tree);
5132 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
5133 static void add_src_coords_attributes (dw_die_ref, tree);
5134 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
5135 static void push_decl_scope (tree);
5136 static void pop_decl_scope (void);
5137 static dw_die_ref scope_die_for (tree, dw_die_ref);
5138 static inline int local_scope_p (dw_die_ref);
5139 static inline int class_or_namespace_scope_p (dw_die_ref);
5140 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
5141 static void add_calling_convention_attribute (dw_die_ref, tree);
5142 static const char *type_tag (const_tree);
5143 static tree member_declared_type (const_tree);
5144 #if 0
5145 static const char *decl_start_label (tree);
5146 #endif
5147 static void gen_array_type_die (tree, dw_die_ref);
5148 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
5149 #if 0
5150 static void gen_entry_point_die (tree, dw_die_ref);
5151 #endif
5152 static void gen_inlined_enumeration_type_die (tree, dw_die_ref);
5153 static void gen_inlined_structure_type_die (tree, dw_die_ref);
5154 static void gen_inlined_union_type_die (tree, dw_die_ref);
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_tagged_type_instantiation_die (tree, dw_die_ref);
5176 static void gen_block_die (tree, dw_die_ref, int);
5177 static void decls_for_scope (tree, dw_die_ref, int);
5178 static int is_redundant_typedef (const_tree);
5179 static void gen_namespace_die (tree);
5180 static void gen_decl_die (tree, tree, dw_die_ref);
5181 static dw_die_ref force_decl_die (tree);
5182 static dw_die_ref force_type_die (tree);
5183 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
5184 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
5185 static struct dwarf_file_data * lookup_filename (const char *);
5186 static void retry_incomplete_types (void);
5187 static void gen_type_die_for_member (tree, tree, dw_die_ref);
5188 static void splice_child_die (dw_die_ref, dw_die_ref);
5189 static int file_info_cmp (const void *, const void *);
5190 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
5191                                      const char *, const char *, unsigned);
5192 static void add_loc_descr_to_loc_list (dw_loc_list_ref *, dw_loc_descr_ref,
5193                                        const char *, const char *,
5194                                        const char *);
5195 static void output_loc_list (dw_loc_list_ref);
5196 static char *gen_internal_sym (const char *);
5197
5198 static void prune_unmark_dies (dw_die_ref);
5199 static void prune_unused_types_mark (dw_die_ref, int);
5200 static void prune_unused_types_walk (dw_die_ref);
5201 static void prune_unused_types_walk_attribs (dw_die_ref);
5202 static void prune_unused_types_prune (dw_die_ref);
5203 static void prune_unused_types (void);
5204 static int maybe_emit_file (struct dwarf_file_data *fd);
5205
5206 /* Section names used to hold DWARF debugging information.  */
5207 #ifndef DEBUG_INFO_SECTION
5208 #define DEBUG_INFO_SECTION      ".debug_info"
5209 #endif
5210 #ifndef DEBUG_ABBREV_SECTION
5211 #define DEBUG_ABBREV_SECTION    ".debug_abbrev"
5212 #endif
5213 #ifndef DEBUG_ARANGES_SECTION
5214 #define DEBUG_ARANGES_SECTION   ".debug_aranges"
5215 #endif
5216 #ifndef DEBUG_MACINFO_SECTION
5217 #define DEBUG_MACINFO_SECTION   ".debug_macinfo"
5218 #endif
5219 #ifndef DEBUG_LINE_SECTION
5220 #define DEBUG_LINE_SECTION      ".debug_line"
5221 #endif
5222 #ifndef DEBUG_LOC_SECTION
5223 #define DEBUG_LOC_SECTION       ".debug_loc"
5224 #endif
5225 #ifndef DEBUG_PUBNAMES_SECTION
5226 #define DEBUG_PUBNAMES_SECTION  ".debug_pubnames"
5227 #endif
5228 #ifndef DEBUG_STR_SECTION
5229 #define DEBUG_STR_SECTION       ".debug_str"
5230 #endif
5231 #ifndef DEBUG_RANGES_SECTION
5232 #define DEBUG_RANGES_SECTION    ".debug_ranges"
5233 #endif
5234
5235 /* Standard ELF section names for compiled code and data.  */
5236 #ifndef TEXT_SECTION_NAME
5237 #define TEXT_SECTION_NAME       ".text"
5238 #endif
5239
5240 /* Section flags for .debug_str section.  */
5241 #define DEBUG_STR_SECTION_FLAGS \
5242   (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings               \
5243    ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1        \
5244    : SECTION_DEBUG)
5245
5246 /* Labels we insert at beginning sections we can reference instead of
5247    the section names themselves.  */
5248
5249 #ifndef TEXT_SECTION_LABEL
5250 #define TEXT_SECTION_LABEL              "Ltext"
5251 #endif
5252 #ifndef COLD_TEXT_SECTION_LABEL
5253 #define COLD_TEXT_SECTION_LABEL         "Ltext_cold"
5254 #endif
5255 #ifndef DEBUG_LINE_SECTION_LABEL
5256 #define DEBUG_LINE_SECTION_LABEL        "Ldebug_line"
5257 #endif
5258 #ifndef DEBUG_INFO_SECTION_LABEL
5259 #define DEBUG_INFO_SECTION_LABEL        "Ldebug_info"
5260 #endif
5261 #ifndef DEBUG_ABBREV_SECTION_LABEL
5262 #define DEBUG_ABBREV_SECTION_LABEL      "Ldebug_abbrev"
5263 #endif
5264 #ifndef DEBUG_LOC_SECTION_LABEL
5265 #define DEBUG_LOC_SECTION_LABEL         "Ldebug_loc"
5266 #endif
5267 #ifndef DEBUG_RANGES_SECTION_LABEL
5268 #define DEBUG_RANGES_SECTION_LABEL      "Ldebug_ranges"
5269 #endif
5270 #ifndef DEBUG_MACINFO_SECTION_LABEL
5271 #define DEBUG_MACINFO_SECTION_LABEL     "Ldebug_macinfo"
5272 #endif
5273
5274 /* Definitions of defaults for formats and names of various special
5275    (artificial) labels which may be generated within this file (when the -g
5276    options is used and DWARF2_DEBUGGING_INFO is in effect.
5277    If necessary, these may be overridden from within the tm.h file, but
5278    typically, overriding these defaults is unnecessary.  */
5279
5280 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
5281 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5282 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5283 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
5284 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5285 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5286 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5287 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5288 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5289 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
5290
5291 #ifndef TEXT_END_LABEL
5292 #define TEXT_END_LABEL          "Letext"
5293 #endif
5294 #ifndef COLD_END_LABEL
5295 #define COLD_END_LABEL          "Letext_cold"
5296 #endif
5297 #ifndef BLOCK_BEGIN_LABEL
5298 #define BLOCK_BEGIN_LABEL       "LBB"
5299 #endif
5300 #ifndef BLOCK_END_LABEL
5301 #define BLOCK_END_LABEL         "LBE"
5302 #endif
5303 #ifndef LINE_CODE_LABEL
5304 #define LINE_CODE_LABEL         "LM"
5305 #endif
5306 #ifndef SEPARATE_LINE_CODE_LABEL
5307 #define SEPARATE_LINE_CODE_LABEL        "LSM"
5308 #endif
5309
5310 \f
5311 /* We allow a language front-end to designate a function that is to be
5312    called to "demangle" any name before it is put into a DIE.  */
5313
5314 static const char *(*demangle_name_func) (const char *);
5315
5316 void
5317 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
5318 {
5319   demangle_name_func = func;
5320 }
5321
5322 /* Test if rtl node points to a pseudo register.  */
5323
5324 static inline int
5325 is_pseudo_reg (const_rtx rtl)
5326 {
5327   return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
5328           || (GET_CODE (rtl) == SUBREG
5329               && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
5330 }
5331
5332 /* Return a reference to a type, with its const and volatile qualifiers
5333    removed.  */
5334
5335 static inline tree
5336 type_main_variant (tree type)
5337 {
5338   type = TYPE_MAIN_VARIANT (type);
5339
5340   /* ??? There really should be only one main variant among any group of
5341      variants of a given type (and all of the MAIN_VARIANT values for all
5342      members of the group should point to that one type) but sometimes the C
5343      front-end messes this up for array types, so we work around that bug
5344      here.  */
5345   if (TREE_CODE (type) == ARRAY_TYPE)
5346     while (type != TYPE_MAIN_VARIANT (type))
5347       type = TYPE_MAIN_VARIANT (type);
5348
5349   return type;
5350 }
5351
5352 /* Return nonzero if the given type node represents a tagged type.  */
5353
5354 static inline int
5355 is_tagged_type (const_tree type)
5356 {
5357   enum tree_code code = TREE_CODE (type);
5358
5359   return (code == RECORD_TYPE || code == UNION_TYPE
5360           || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
5361 }
5362
5363 /* Convert a DIE tag into its string name.  */
5364
5365 static const char *
5366 dwarf_tag_name (unsigned int tag)
5367 {
5368   switch (tag)
5369     {
5370     case DW_TAG_padding:
5371       return "DW_TAG_padding";
5372     case DW_TAG_array_type:
5373       return "DW_TAG_array_type";
5374     case DW_TAG_class_type:
5375       return "DW_TAG_class_type";
5376     case DW_TAG_entry_point:
5377       return "DW_TAG_entry_point";
5378     case DW_TAG_enumeration_type:
5379       return "DW_TAG_enumeration_type";
5380     case DW_TAG_formal_parameter:
5381       return "DW_TAG_formal_parameter";
5382     case DW_TAG_imported_declaration:
5383       return "DW_TAG_imported_declaration";
5384     case DW_TAG_label:
5385       return "DW_TAG_label";
5386     case DW_TAG_lexical_block:
5387       return "DW_TAG_lexical_block";
5388     case DW_TAG_member:
5389       return "DW_TAG_member";
5390     case DW_TAG_pointer_type:
5391       return "DW_TAG_pointer_type";
5392     case DW_TAG_reference_type:
5393       return "DW_TAG_reference_type";
5394     case DW_TAG_compile_unit:
5395       return "DW_TAG_compile_unit";
5396     case DW_TAG_string_type:
5397       return "DW_TAG_string_type";
5398     case DW_TAG_structure_type:
5399       return "DW_TAG_structure_type";
5400     case DW_TAG_subroutine_type:
5401       return "DW_TAG_subroutine_type";
5402     case DW_TAG_typedef:
5403       return "DW_TAG_typedef";
5404     case DW_TAG_union_type:
5405       return "DW_TAG_union_type";
5406     case DW_TAG_unspecified_parameters:
5407       return "DW_TAG_unspecified_parameters";
5408     case DW_TAG_variant:
5409       return "DW_TAG_variant";
5410     case DW_TAG_common_block:
5411       return "DW_TAG_common_block";
5412     case DW_TAG_common_inclusion:
5413       return "DW_TAG_common_inclusion";
5414     case DW_TAG_inheritance:
5415       return "DW_TAG_inheritance";
5416     case DW_TAG_inlined_subroutine:
5417       return "DW_TAG_inlined_subroutine";
5418     case DW_TAG_module:
5419       return "DW_TAG_module";
5420     case DW_TAG_ptr_to_member_type:
5421       return "DW_TAG_ptr_to_member_type";
5422     case DW_TAG_set_type:
5423       return "DW_TAG_set_type";
5424     case DW_TAG_subrange_type:
5425       return "DW_TAG_subrange_type";
5426     case DW_TAG_with_stmt:
5427       return "DW_TAG_with_stmt";
5428     case DW_TAG_access_declaration:
5429       return "DW_TAG_access_declaration";
5430     case DW_TAG_base_type:
5431       return "DW_TAG_base_type";
5432     case DW_TAG_catch_block:
5433       return "DW_TAG_catch_block";
5434     case DW_TAG_const_type:
5435       return "DW_TAG_const_type";
5436     case DW_TAG_constant:
5437       return "DW_TAG_constant";
5438     case DW_TAG_enumerator:
5439       return "DW_TAG_enumerator";
5440     case DW_TAG_file_type:
5441       return "DW_TAG_file_type";
5442     case DW_TAG_friend:
5443       return "DW_TAG_friend";
5444     case DW_TAG_namelist:
5445       return "DW_TAG_namelist";
5446     case DW_TAG_namelist_item:
5447       return "DW_TAG_namelist_item";
5448     case DW_TAG_packed_type:
5449       return "DW_TAG_packed_type";
5450     case DW_TAG_subprogram:
5451       return "DW_TAG_subprogram";
5452     case DW_TAG_template_type_param:
5453       return "DW_TAG_template_type_param";
5454     case DW_TAG_template_value_param:
5455       return "DW_TAG_template_value_param";
5456     case DW_TAG_thrown_type:
5457       return "DW_TAG_thrown_type";
5458     case DW_TAG_try_block:
5459       return "DW_TAG_try_block";
5460     case DW_TAG_variant_part:
5461       return "DW_TAG_variant_part";
5462     case DW_TAG_variable:
5463       return "DW_TAG_variable";
5464     case DW_TAG_volatile_type:
5465       return "DW_TAG_volatile_type";
5466     case DW_TAG_dwarf_procedure:
5467       return "DW_TAG_dwarf_procedure";
5468     case DW_TAG_restrict_type:
5469       return "DW_TAG_restrict_type";
5470     case DW_TAG_interface_type:
5471       return "DW_TAG_interface_type";
5472     case DW_TAG_namespace:
5473       return "DW_TAG_namespace";
5474     case DW_TAG_imported_module:
5475       return "DW_TAG_imported_module";
5476     case DW_TAG_unspecified_type:
5477       return "DW_TAG_unspecified_type";
5478     case DW_TAG_partial_unit:
5479       return "DW_TAG_partial_unit";
5480     case DW_TAG_imported_unit:
5481       return "DW_TAG_imported_unit";
5482     case DW_TAG_condition:
5483       return "DW_TAG_condition";
5484     case DW_TAG_shared_type:
5485       return "DW_TAG_shared_type";
5486     case DW_TAG_MIPS_loop:
5487       return "DW_TAG_MIPS_loop";
5488     case DW_TAG_format_label:
5489       return "DW_TAG_format_label";
5490     case DW_TAG_function_template:
5491       return "DW_TAG_function_template";
5492     case DW_TAG_class_template:
5493       return "DW_TAG_class_template";
5494     case DW_TAG_GNU_BINCL:
5495       return "DW_TAG_GNU_BINCL";
5496     case DW_TAG_GNU_EINCL:
5497       return "DW_TAG_GNU_EINCL";
5498     default:
5499       return "DW_TAG_<unknown>";
5500     }
5501 }
5502
5503 /* Convert a DWARF attribute code into its string name.  */
5504
5505 static const char *
5506 dwarf_attr_name (unsigned int attr)
5507 {
5508   switch (attr)
5509     {
5510     case DW_AT_sibling:
5511       return "DW_AT_sibling";
5512     case DW_AT_location:
5513       return "DW_AT_location";
5514     case DW_AT_name:
5515       return "DW_AT_name";
5516     case DW_AT_ordering:
5517       return "DW_AT_ordering";
5518     case DW_AT_subscr_data:
5519       return "DW_AT_subscr_data";
5520     case DW_AT_byte_size:
5521       return "DW_AT_byte_size";
5522     case DW_AT_bit_offset:
5523       return "DW_AT_bit_offset";
5524     case DW_AT_bit_size:
5525       return "DW_AT_bit_size";
5526     case DW_AT_element_list:
5527       return "DW_AT_element_list";
5528     case DW_AT_stmt_list:
5529       return "DW_AT_stmt_list";
5530     case DW_AT_low_pc:
5531       return "DW_AT_low_pc";
5532     case DW_AT_high_pc:
5533       return "DW_AT_high_pc";
5534     case DW_AT_language:
5535       return "DW_AT_language";
5536     case DW_AT_member:
5537       return "DW_AT_member";
5538     case DW_AT_discr:
5539       return "DW_AT_discr";
5540     case DW_AT_discr_value:
5541       return "DW_AT_discr_value";
5542     case DW_AT_visibility:
5543       return "DW_AT_visibility";
5544     case DW_AT_import:
5545       return "DW_AT_import";
5546     case DW_AT_string_length:
5547       return "DW_AT_string_length";
5548     case DW_AT_common_reference:
5549       return "DW_AT_common_reference";
5550     case DW_AT_comp_dir:
5551       return "DW_AT_comp_dir";
5552     case DW_AT_const_value:
5553       return "DW_AT_const_value";
5554     case DW_AT_containing_type:
5555       return "DW_AT_containing_type";
5556     case DW_AT_default_value:
5557       return "DW_AT_default_value";
5558     case DW_AT_inline:
5559       return "DW_AT_inline";
5560     case DW_AT_is_optional:
5561       return "DW_AT_is_optional";
5562     case DW_AT_lower_bound:
5563       return "DW_AT_lower_bound";
5564     case DW_AT_producer:
5565       return "DW_AT_producer";
5566     case DW_AT_prototyped:
5567       return "DW_AT_prototyped";
5568     case DW_AT_return_addr:
5569       return "DW_AT_return_addr";
5570     case DW_AT_start_scope:
5571       return "DW_AT_start_scope";
5572     case DW_AT_bit_stride:
5573       return "DW_AT_bit_stride";
5574     case DW_AT_upper_bound:
5575       return "DW_AT_upper_bound";
5576     case DW_AT_abstract_origin:
5577       return "DW_AT_abstract_origin";
5578     case DW_AT_accessibility:
5579       return "DW_AT_accessibility";
5580     case DW_AT_address_class:
5581       return "DW_AT_address_class";
5582     case DW_AT_artificial:
5583       return "DW_AT_artificial";
5584     case DW_AT_base_types:
5585       return "DW_AT_base_types";
5586     case DW_AT_calling_convention:
5587       return "DW_AT_calling_convention";
5588     case DW_AT_count:
5589       return "DW_AT_count";
5590     case DW_AT_data_member_location:
5591       return "DW_AT_data_member_location";
5592     case DW_AT_decl_column:
5593       return "DW_AT_decl_column";
5594     case DW_AT_decl_file:
5595       return "DW_AT_decl_file";
5596     case DW_AT_decl_line:
5597       return "DW_AT_decl_line";
5598     case DW_AT_declaration:
5599       return "DW_AT_declaration";
5600     case DW_AT_discr_list:
5601       return "DW_AT_discr_list";
5602     case DW_AT_encoding:
5603       return "DW_AT_encoding";
5604     case DW_AT_external:
5605       return "DW_AT_external";
5606     case DW_AT_frame_base:
5607       return "DW_AT_frame_base";
5608     case DW_AT_friend:
5609       return "DW_AT_friend";
5610     case DW_AT_identifier_case:
5611       return "DW_AT_identifier_case";
5612     case DW_AT_macro_info:
5613       return "DW_AT_macro_info";
5614     case DW_AT_namelist_items:
5615       return "DW_AT_namelist_items";
5616     case DW_AT_priority:
5617       return "DW_AT_priority";
5618     case DW_AT_segment:
5619       return "DW_AT_segment";
5620     case DW_AT_specification:
5621       return "DW_AT_specification";
5622     case DW_AT_static_link:
5623       return "DW_AT_static_link";
5624     case DW_AT_type:
5625       return "DW_AT_type";
5626     case DW_AT_use_location:
5627       return "DW_AT_use_location";
5628     case DW_AT_variable_parameter:
5629       return "DW_AT_variable_parameter";
5630     case DW_AT_virtuality:
5631       return "DW_AT_virtuality";
5632     case DW_AT_vtable_elem_location:
5633       return "DW_AT_vtable_elem_location";
5634
5635     case DW_AT_allocated:
5636       return "DW_AT_allocated";
5637     case DW_AT_associated:
5638       return "DW_AT_associated";
5639     case DW_AT_data_location:
5640       return "DW_AT_data_location";
5641     case DW_AT_byte_stride:
5642       return "DW_AT_byte_stride";
5643     case DW_AT_entry_pc:
5644       return "DW_AT_entry_pc";
5645     case DW_AT_use_UTF8:
5646       return "DW_AT_use_UTF8";
5647     case DW_AT_extension:
5648       return "DW_AT_extension";
5649     case DW_AT_ranges:
5650       return "DW_AT_ranges";
5651     case DW_AT_trampoline:
5652       return "DW_AT_trampoline";
5653     case DW_AT_call_column:
5654       return "DW_AT_call_column";
5655     case DW_AT_call_file:
5656       return "DW_AT_call_file";
5657     case DW_AT_call_line:
5658       return "DW_AT_call_line";
5659
5660     case DW_AT_MIPS_fde:
5661       return "DW_AT_MIPS_fde";
5662     case DW_AT_MIPS_loop_begin:
5663       return "DW_AT_MIPS_loop_begin";
5664     case DW_AT_MIPS_tail_loop_begin:
5665       return "DW_AT_MIPS_tail_loop_begin";
5666     case DW_AT_MIPS_epilog_begin:
5667       return "DW_AT_MIPS_epilog_begin";
5668     case DW_AT_MIPS_loop_unroll_factor:
5669       return "DW_AT_MIPS_loop_unroll_factor";
5670     case DW_AT_MIPS_software_pipeline_depth:
5671       return "DW_AT_MIPS_software_pipeline_depth";
5672     case DW_AT_MIPS_linkage_name:
5673       return "DW_AT_MIPS_linkage_name";
5674     case DW_AT_MIPS_stride:
5675       return "DW_AT_MIPS_stride";
5676     case DW_AT_MIPS_abstract_name:
5677       return "DW_AT_MIPS_abstract_name";
5678     case DW_AT_MIPS_clone_origin:
5679       return "DW_AT_MIPS_clone_origin";
5680     case DW_AT_MIPS_has_inlines:
5681       return "DW_AT_MIPS_has_inlines";
5682
5683     case DW_AT_sf_names:
5684       return "DW_AT_sf_names";
5685     case DW_AT_src_info:
5686       return "DW_AT_src_info";
5687     case DW_AT_mac_info:
5688       return "DW_AT_mac_info";
5689     case DW_AT_src_coords:
5690       return "DW_AT_src_coords";
5691     case DW_AT_body_begin:
5692       return "DW_AT_body_begin";
5693     case DW_AT_body_end:
5694       return "DW_AT_body_end";
5695     case DW_AT_GNU_vector:
5696       return "DW_AT_GNU_vector";
5697
5698     case DW_AT_VMS_rtnbeg_pd_address:
5699       return "DW_AT_VMS_rtnbeg_pd_address";
5700
5701     default:
5702       return "DW_AT_<unknown>";
5703     }
5704 }
5705
5706 /* Convert a DWARF value form code into its string name.  */
5707
5708 static const char *
5709 dwarf_form_name (unsigned int form)
5710 {
5711   switch (form)
5712     {
5713     case DW_FORM_addr:
5714       return "DW_FORM_addr";
5715     case DW_FORM_block2:
5716       return "DW_FORM_block2";
5717     case DW_FORM_block4:
5718       return "DW_FORM_block4";
5719     case DW_FORM_data2:
5720       return "DW_FORM_data2";
5721     case DW_FORM_data4:
5722       return "DW_FORM_data4";
5723     case DW_FORM_data8:
5724       return "DW_FORM_data8";
5725     case DW_FORM_string:
5726       return "DW_FORM_string";
5727     case DW_FORM_block:
5728       return "DW_FORM_block";
5729     case DW_FORM_block1:
5730       return "DW_FORM_block1";
5731     case DW_FORM_data1:
5732       return "DW_FORM_data1";
5733     case DW_FORM_flag:
5734       return "DW_FORM_flag";
5735     case DW_FORM_sdata:
5736       return "DW_FORM_sdata";
5737     case DW_FORM_strp:
5738       return "DW_FORM_strp";
5739     case DW_FORM_udata:
5740       return "DW_FORM_udata";
5741     case DW_FORM_ref_addr:
5742       return "DW_FORM_ref_addr";
5743     case DW_FORM_ref1:
5744       return "DW_FORM_ref1";
5745     case DW_FORM_ref2:
5746       return "DW_FORM_ref2";
5747     case DW_FORM_ref4:
5748       return "DW_FORM_ref4";
5749     case DW_FORM_ref8:
5750       return "DW_FORM_ref8";
5751     case DW_FORM_ref_udata:
5752       return "DW_FORM_ref_udata";
5753     case DW_FORM_indirect:
5754       return "DW_FORM_indirect";
5755     default:
5756       return "DW_FORM_<unknown>";
5757     }
5758 }
5759 \f
5760 /* Determine the "ultimate origin" of a decl.  The decl may be an inlined
5761    instance of an inlined instance of a decl which is local to an inline
5762    function, so we have to trace all of the way back through the origin chain
5763    to find out what sort of node actually served as the original seed for the
5764    given block.  */
5765
5766 static tree
5767 decl_ultimate_origin (const_tree decl)
5768 {
5769   if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
5770     return NULL_TREE;
5771
5772   /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
5773      nodes in the function to point to themselves; ignore that if
5774      we're trying to output the abstract instance of this function.  */
5775   if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
5776     return NULL_TREE;
5777
5778   /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
5779      most distant ancestor, this should never happen.  */
5780   gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
5781
5782   return DECL_ABSTRACT_ORIGIN (decl);
5783 }
5784
5785 /* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
5786    of a virtual function may refer to a base class, so we check the 'this'
5787    parameter.  */
5788
5789 static tree
5790 decl_class_context (tree decl)
5791 {
5792   tree context = NULL_TREE;
5793
5794   if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
5795     context = DECL_CONTEXT (decl);
5796   else
5797     context = TYPE_MAIN_VARIANT
5798       (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
5799
5800   if (context && !TYPE_P (context))
5801     context = NULL_TREE;
5802
5803   return context;
5804 }
5805 \f
5806 /* Add an attribute/value pair to a DIE.  */
5807
5808 static inline void
5809 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
5810 {
5811   /* Maybe this should be an assert?  */
5812   if (die == NULL)
5813     return;
5814
5815   if (die->die_attr == NULL)
5816     die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
5817   VEC_safe_push (dw_attr_node, gc, die->die_attr, attr);
5818 }
5819
5820 static inline enum dw_val_class
5821 AT_class (dw_attr_ref a)
5822 {
5823   return a->dw_attr_val.val_class;
5824 }
5825
5826 /* Add a flag value attribute to a DIE.  */
5827
5828 static inline void
5829 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
5830 {
5831   dw_attr_node attr;
5832
5833   attr.dw_attr = attr_kind;
5834   attr.dw_attr_val.val_class = dw_val_class_flag;
5835   attr.dw_attr_val.v.val_flag = flag;
5836   add_dwarf_attr (die, &attr);
5837 }
5838
5839 static inline unsigned
5840 AT_flag (dw_attr_ref a)
5841 {
5842   gcc_assert (a && AT_class (a) == dw_val_class_flag);
5843   return a->dw_attr_val.v.val_flag;
5844 }
5845
5846 /* Add a signed integer attribute value to a DIE.  */
5847
5848 static inline void
5849 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
5850 {
5851   dw_attr_node attr;
5852
5853   attr.dw_attr = attr_kind;
5854   attr.dw_attr_val.val_class = dw_val_class_const;
5855   attr.dw_attr_val.v.val_int = int_val;
5856   add_dwarf_attr (die, &attr);
5857 }
5858
5859 static inline HOST_WIDE_INT
5860 AT_int (dw_attr_ref a)
5861 {
5862   gcc_assert (a && AT_class (a) == dw_val_class_const);
5863   return a->dw_attr_val.v.val_int;
5864 }
5865
5866 /* Add an unsigned integer attribute value to a DIE.  */
5867
5868 static inline void
5869 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
5870                  unsigned HOST_WIDE_INT unsigned_val)
5871 {
5872   dw_attr_node attr;
5873
5874   attr.dw_attr = attr_kind;
5875   attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
5876   attr.dw_attr_val.v.val_unsigned = unsigned_val;
5877   add_dwarf_attr (die, &attr);
5878 }
5879
5880 static inline unsigned HOST_WIDE_INT
5881 AT_unsigned (dw_attr_ref a)
5882 {
5883   gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
5884   return a->dw_attr_val.v.val_unsigned;
5885 }
5886
5887 /* Add an unsigned double integer attribute value to a DIE.  */
5888
5889 static inline void
5890 add_AT_long_long (dw_die_ref die, enum dwarf_attribute attr_kind,
5891                   long unsigned int val_hi, long unsigned int val_low)
5892 {
5893   dw_attr_node attr;
5894
5895   attr.dw_attr = attr_kind;
5896   attr.dw_attr_val.val_class = dw_val_class_long_long;
5897   attr.dw_attr_val.v.val_long_long.hi = val_hi;
5898   attr.dw_attr_val.v.val_long_long.low = val_low;
5899   add_dwarf_attr (die, &attr);
5900 }
5901
5902 /* Add a floating point attribute value to a DIE and return it.  */
5903
5904 static inline void
5905 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
5906             unsigned int length, unsigned int elt_size, unsigned char *array)
5907 {
5908   dw_attr_node attr;
5909
5910   attr.dw_attr = attr_kind;
5911   attr.dw_attr_val.val_class = dw_val_class_vec;
5912   attr.dw_attr_val.v.val_vec.length = length;
5913   attr.dw_attr_val.v.val_vec.elt_size = elt_size;
5914   attr.dw_attr_val.v.val_vec.array = array;
5915   add_dwarf_attr (die, &attr);
5916 }
5917
5918 /* Hash and equality functions for debug_str_hash.  */
5919
5920 static hashval_t
5921 debug_str_do_hash (const void *x)
5922 {
5923   return htab_hash_string (((const struct indirect_string_node *)x)->str);
5924 }
5925
5926 static int
5927 debug_str_eq (const void *x1, const void *x2)
5928 {
5929   return strcmp ((((const struct indirect_string_node *)x1)->str),
5930                  (const char *)x2) == 0;
5931 }
5932
5933 /* Add a string attribute value to a DIE.  */
5934
5935 static inline void
5936 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
5937 {
5938   dw_attr_node attr;
5939   struct indirect_string_node *node;
5940   void **slot;
5941
5942   if (! debug_str_hash)
5943     debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
5944                                       debug_str_eq, NULL);
5945
5946   slot = htab_find_slot_with_hash (debug_str_hash, str,
5947                                    htab_hash_string (str), INSERT);
5948   if (*slot == NULL)
5949     {
5950       node = (struct indirect_string_node *)
5951                ggc_alloc_cleared (sizeof (struct indirect_string_node));
5952       node->str = ggc_strdup (str);
5953       *slot = node;
5954     }
5955   else
5956     node = (struct indirect_string_node *) *slot;
5957
5958   node->refcount++;
5959
5960   attr.dw_attr = attr_kind;
5961   attr.dw_attr_val.val_class = dw_val_class_str;
5962   attr.dw_attr_val.v.val_str = node;
5963   add_dwarf_attr (die, &attr);
5964 }
5965
5966 static inline const char *
5967 AT_string (dw_attr_ref a)
5968 {
5969   gcc_assert (a && AT_class (a) == dw_val_class_str);
5970   return a->dw_attr_val.v.val_str->str;
5971 }
5972
5973 /* Find out whether a string should be output inline in DIE
5974    or out-of-line in .debug_str section.  */
5975
5976 static int
5977 AT_string_form (dw_attr_ref a)
5978 {
5979   struct indirect_string_node *node;
5980   unsigned int len;
5981   char label[32];
5982
5983   gcc_assert (a && AT_class (a) == dw_val_class_str);
5984
5985   node = a->dw_attr_val.v.val_str;
5986   if (node->form)
5987     return node->form;
5988
5989   len = strlen (node->str) + 1;
5990
5991   /* If the string is shorter or equal to the size of the reference, it is
5992      always better to put it inline.  */
5993   if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
5994     return node->form = DW_FORM_string;
5995
5996   /* If we cannot expect the linker to merge strings in .debug_str
5997      section, only put it into .debug_str if it is worth even in this
5998      single module.  */
5999   if ((debug_str_section->common.flags & SECTION_MERGE) == 0
6000       && (len - DWARF_OFFSET_SIZE) * node->refcount <= len)
6001     return node->form = DW_FORM_string;
6002
6003   ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
6004   ++dw2_string_counter;
6005   node->label = xstrdup (label);
6006
6007   return node->form = DW_FORM_strp;
6008 }
6009
6010 /* Add a DIE reference attribute value to a DIE.  */
6011
6012 static inline void
6013 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
6014 {
6015   dw_attr_node attr;
6016
6017   attr.dw_attr = attr_kind;
6018   attr.dw_attr_val.val_class = dw_val_class_die_ref;
6019   attr.dw_attr_val.v.val_die_ref.die = targ_die;
6020   attr.dw_attr_val.v.val_die_ref.external = 0;
6021   add_dwarf_attr (die, &attr);
6022 }
6023
6024 /* Add an AT_specification attribute to a DIE, and also make the back
6025    pointer from the specification to the definition.  */
6026
6027 static inline void
6028 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
6029 {
6030   add_AT_die_ref (die, DW_AT_specification, targ_die);
6031   gcc_assert (!targ_die->die_definition);
6032   targ_die->die_definition = die;
6033 }
6034
6035 static inline dw_die_ref
6036 AT_ref (dw_attr_ref a)
6037 {
6038   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
6039   return a->dw_attr_val.v.val_die_ref.die;
6040 }
6041
6042 static inline int
6043 AT_ref_external (dw_attr_ref a)
6044 {
6045   if (a && AT_class (a) == dw_val_class_die_ref)
6046     return a->dw_attr_val.v.val_die_ref.external;
6047
6048   return 0;
6049 }
6050
6051 static inline void
6052 set_AT_ref_external (dw_attr_ref a, int i)
6053 {
6054   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
6055   a->dw_attr_val.v.val_die_ref.external = i;
6056 }
6057
6058 /* Add an FDE reference attribute value to a DIE.  */
6059
6060 static inline void
6061 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
6062 {
6063   dw_attr_node attr;
6064
6065   attr.dw_attr = attr_kind;
6066   attr.dw_attr_val.val_class = dw_val_class_fde_ref;
6067   attr.dw_attr_val.v.val_fde_index = targ_fde;
6068   add_dwarf_attr (die, &attr);
6069 }
6070
6071 /* Add a location description attribute value to a DIE.  */
6072
6073 static inline void
6074 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
6075 {
6076   dw_attr_node attr;
6077
6078   attr.dw_attr = attr_kind;
6079   attr.dw_attr_val.val_class = dw_val_class_loc;
6080   attr.dw_attr_val.v.val_loc = loc;
6081   add_dwarf_attr (die, &attr);
6082 }
6083
6084 static inline dw_loc_descr_ref
6085 AT_loc (dw_attr_ref a)
6086 {
6087   gcc_assert (a && AT_class (a) == dw_val_class_loc);
6088   return a->dw_attr_val.v.val_loc;
6089 }
6090
6091 static inline void
6092 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
6093 {
6094   dw_attr_node attr;
6095
6096   attr.dw_attr = attr_kind;
6097   attr.dw_attr_val.val_class = dw_val_class_loc_list;
6098   attr.dw_attr_val.v.val_loc_list = loc_list;
6099   add_dwarf_attr (die, &attr);
6100   have_location_lists = true;
6101 }
6102
6103 static inline dw_loc_list_ref
6104 AT_loc_list (dw_attr_ref a)
6105 {
6106   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
6107   return a->dw_attr_val.v.val_loc_list;
6108 }
6109
6110 /* Add an address constant attribute value to a DIE.  */
6111
6112 static inline void
6113 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
6114 {
6115   dw_attr_node attr;
6116
6117   attr.dw_attr = attr_kind;
6118   attr.dw_attr_val.val_class = dw_val_class_addr;
6119   attr.dw_attr_val.v.val_addr = addr;
6120   add_dwarf_attr (die, &attr);
6121 }
6122
6123 /* Get the RTX from to an address DIE attribute.  */
6124
6125 static inline rtx
6126 AT_addr (dw_attr_ref a)
6127 {
6128   gcc_assert (a && AT_class (a) == dw_val_class_addr);
6129   return a->dw_attr_val.v.val_addr;
6130 }
6131
6132 /* Add a file attribute value to a DIE.  */
6133
6134 static inline void
6135 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
6136              struct dwarf_file_data *fd)
6137 {
6138   dw_attr_node attr;
6139
6140   attr.dw_attr = attr_kind;
6141   attr.dw_attr_val.val_class = dw_val_class_file;
6142   attr.dw_attr_val.v.val_file = fd;
6143   add_dwarf_attr (die, &attr);
6144 }
6145
6146 /* Get the dwarf_file_data from a file DIE attribute.  */
6147
6148 static inline struct dwarf_file_data *
6149 AT_file (dw_attr_ref a)
6150 {
6151   gcc_assert (a && AT_class (a) == dw_val_class_file);
6152   return a->dw_attr_val.v.val_file;
6153 }
6154
6155 /* Add a label identifier attribute value to a DIE.  */
6156
6157 static inline void
6158 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
6159 {
6160   dw_attr_node attr;
6161
6162   attr.dw_attr = attr_kind;
6163   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
6164   attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
6165   add_dwarf_attr (die, &attr);
6166 }
6167
6168 /* Add a section offset attribute value to a DIE, an offset into the
6169    debug_line section.  */
6170
6171 static inline void
6172 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
6173                 const char *label)
6174 {
6175   dw_attr_node attr;
6176
6177   attr.dw_attr = attr_kind;
6178   attr.dw_attr_val.val_class = dw_val_class_lineptr;
6179   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
6180   add_dwarf_attr (die, &attr);
6181 }
6182
6183 /* Add a section offset attribute value to a DIE, an offset into the
6184    debug_macinfo section.  */
6185
6186 static inline void
6187 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
6188                const char *label)
6189 {
6190   dw_attr_node attr;
6191
6192   attr.dw_attr = attr_kind;
6193   attr.dw_attr_val.val_class = dw_val_class_macptr;
6194   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
6195   add_dwarf_attr (die, &attr);
6196 }
6197
6198 /* Add an offset attribute value to a DIE.  */
6199
6200 static inline void
6201 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
6202                unsigned HOST_WIDE_INT offset)
6203 {
6204   dw_attr_node attr;
6205
6206   attr.dw_attr = attr_kind;
6207   attr.dw_attr_val.val_class = dw_val_class_offset;
6208   attr.dw_attr_val.v.val_offset = offset;
6209   add_dwarf_attr (die, &attr);
6210 }
6211
6212 /* Add an range_list attribute value to a DIE.  */
6213
6214 static void
6215 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
6216                    long unsigned int offset)
6217 {
6218   dw_attr_node attr;
6219
6220   attr.dw_attr = attr_kind;
6221   attr.dw_attr_val.val_class = dw_val_class_range_list;
6222   attr.dw_attr_val.v.val_offset = offset;
6223   add_dwarf_attr (die, &attr);
6224 }
6225
6226 static inline const char *
6227 AT_lbl (dw_attr_ref a)
6228 {
6229   gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
6230                     || AT_class (a) == dw_val_class_lineptr
6231                     || AT_class (a) == dw_val_class_macptr));
6232   return a->dw_attr_val.v.val_lbl_id;
6233 }
6234
6235 /* Get the attribute of type attr_kind.  */
6236
6237 static dw_attr_ref
6238 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
6239 {
6240   dw_attr_ref a;
6241   unsigned ix;
6242   dw_die_ref spec = NULL;
6243
6244   if (! die)
6245     return NULL;
6246
6247   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6248     if (a->dw_attr == attr_kind)
6249       return a;
6250     else if (a->dw_attr == DW_AT_specification
6251              || a->dw_attr == DW_AT_abstract_origin)
6252       spec = AT_ref (a);
6253
6254   if (spec)
6255     return get_AT (spec, attr_kind);
6256
6257   return NULL;
6258 }
6259
6260 /* Return the "low pc" attribute value, typically associated with a subprogram
6261    DIE.  Return null if the "low pc" attribute is either not present, or if it
6262    cannot be represented as an assembler label identifier.  */
6263
6264 static inline const char *
6265 get_AT_low_pc (dw_die_ref die)
6266 {
6267   dw_attr_ref a = get_AT (die, DW_AT_low_pc);
6268
6269   return a ? AT_lbl (a) : NULL;
6270 }
6271
6272 /* Return the "high pc" attribute value, typically associated with a subprogram
6273    DIE.  Return null if the "high pc" attribute is either not present, or if it
6274    cannot be represented as an assembler label identifier.  */
6275
6276 static inline const char *
6277 get_AT_hi_pc (dw_die_ref die)
6278 {
6279   dw_attr_ref a = get_AT (die, DW_AT_high_pc);
6280
6281   return a ? AT_lbl (a) : NULL;
6282 }
6283
6284 /* Return the value of the string attribute designated by ATTR_KIND, or
6285    NULL if it is not present.  */
6286
6287 static inline const char *
6288 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
6289 {
6290   dw_attr_ref a = get_AT (die, attr_kind);
6291
6292   return a ? AT_string (a) : NULL;
6293 }
6294
6295 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
6296    if it is not present.  */
6297
6298 static inline int
6299 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
6300 {
6301   dw_attr_ref a = get_AT (die, attr_kind);
6302
6303   return a ? AT_flag (a) : 0;
6304 }
6305
6306 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
6307    if it is not present.  */
6308
6309 static inline unsigned
6310 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
6311 {
6312   dw_attr_ref a = get_AT (die, attr_kind);
6313
6314   return a ? AT_unsigned (a) : 0;
6315 }
6316
6317 static inline dw_die_ref
6318 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
6319 {
6320   dw_attr_ref a = get_AT (die, attr_kind);
6321
6322   return a ? AT_ref (a) : NULL;
6323 }
6324
6325 static inline struct dwarf_file_data *
6326 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
6327 {
6328   dw_attr_ref a = get_AT (die, attr_kind);
6329
6330   return a ? AT_file (a) : NULL;
6331 }
6332
6333 /* Return TRUE if the language is C or C++.  */
6334
6335 static inline bool
6336 is_c_family (void)
6337 {
6338   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
6339
6340   return (lang == DW_LANG_C || lang == DW_LANG_C89 || lang == DW_LANG_ObjC
6341           || lang == DW_LANG_C99
6342           || lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus);
6343 }
6344
6345 /* Return TRUE if the language is C++.  */
6346
6347 static inline bool
6348 is_cxx (void)
6349 {
6350   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
6351
6352   return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
6353 }
6354
6355 /* Return TRUE if the language is Fortran.  */
6356
6357 static inline bool
6358 is_fortran (void)
6359 {
6360   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
6361
6362   return (lang == DW_LANG_Fortran77
6363           || lang == DW_LANG_Fortran90
6364           || lang == DW_LANG_Fortran95);
6365 }
6366
6367 /* Return TRUE if the language is Java.  */
6368
6369 static inline bool
6370 is_java (void)
6371 {
6372   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
6373
6374   return lang == DW_LANG_Java;
6375 }
6376
6377 /* Return TRUE if the language is Ada.  */
6378
6379 static inline bool
6380 is_ada (void)
6381 {
6382   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
6383
6384   return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
6385 }
6386
6387 /* Remove the specified attribute if present.  */
6388
6389 static void
6390 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
6391 {
6392   dw_attr_ref a;
6393   unsigned ix;
6394
6395   if (! die)
6396     return;
6397
6398   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6399     if (a->dw_attr == attr_kind)
6400       {
6401         if (AT_class (a) == dw_val_class_str)
6402           if (a->dw_attr_val.v.val_str->refcount)
6403             a->dw_attr_val.v.val_str->refcount--;
6404
6405         /* VEC_ordered_remove should help reduce the number of abbrevs
6406            that are needed.  */
6407         VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
6408         return;
6409       }
6410 }
6411
6412 /* Remove CHILD from its parent.  PREV must have the property that
6413    PREV->DIE_SIB == CHILD.  Does not alter CHILD.  */
6414
6415 static void
6416 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
6417 {
6418   gcc_assert (child->die_parent == prev->die_parent);
6419   gcc_assert (prev->die_sib == child);
6420   if (prev == child)
6421     {
6422       gcc_assert (child->die_parent->die_child == child);
6423       prev = NULL;
6424     }
6425   else
6426     prev->die_sib = child->die_sib;
6427   if (child->die_parent->die_child == child)
6428     child->die_parent->die_child = prev;
6429 }
6430
6431 /* Remove child DIE whose die_tag is TAG.  Do nothing if no child
6432    matches TAG.  */
6433
6434 static void
6435 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
6436 {
6437   dw_die_ref c;
6438
6439   c = die->die_child;
6440   if (c) do {
6441     dw_die_ref prev = c;
6442     c = c->die_sib;
6443     while (c->die_tag == tag)
6444       {
6445         remove_child_with_prev (c, prev);
6446         /* Might have removed every child.  */
6447         if (c == c->die_sib)
6448           return;
6449         c = c->die_sib;
6450       }
6451   } while (c != die->die_child);
6452 }
6453
6454 /* Add a CHILD_DIE as the last child of DIE.  */
6455
6456 static void
6457 add_child_die (dw_die_ref die, dw_die_ref child_die)
6458 {
6459   /* FIXME this should probably be an assert.  */
6460   if (! die || ! child_die)
6461     return;
6462   gcc_assert (die != child_die);
6463
6464   child_die->die_parent = die;
6465   if (die->die_child)
6466     {
6467       child_die->die_sib = die->die_child->die_sib;
6468       die->die_child->die_sib = child_die;
6469     }
6470   else
6471     child_die->die_sib = child_die;
6472   die->die_child = child_die;
6473 }
6474
6475 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
6476    is the specification, to the end of PARENT's list of children.
6477    This is done by removing and re-adding it.  */
6478
6479 static void
6480 splice_child_die (dw_die_ref parent, dw_die_ref child)
6481 {
6482   dw_die_ref p;
6483
6484   /* We want the declaration DIE from inside the class, not the
6485      specification DIE at toplevel.  */
6486   if (child->die_parent != parent)
6487     {
6488       dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
6489
6490       if (tmp)
6491         child = tmp;
6492     }
6493
6494   gcc_assert (child->die_parent == parent
6495               || (child->die_parent
6496                   == get_AT_ref (parent, DW_AT_specification)));
6497
6498   for (p = child->die_parent->die_child; ; p = p->die_sib)
6499     if (p->die_sib == child)
6500       {
6501         remove_child_with_prev (child, p);
6502         break;
6503       }
6504
6505   add_child_die (parent, child);
6506 }
6507
6508 /* Return a pointer to a newly created DIE node.  */
6509
6510 static inline dw_die_ref
6511 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
6512 {
6513   dw_die_ref die = GGC_CNEW (die_node);
6514
6515   die->die_tag = tag_value;
6516
6517   if (parent_die != NULL)
6518     add_child_die (parent_die, die);
6519   else
6520     {
6521       limbo_die_node *limbo_node;
6522
6523       limbo_node = GGC_CNEW (limbo_die_node);
6524       limbo_node->die = die;
6525       limbo_node->created_for = t;
6526       limbo_node->next = limbo_die_list;
6527       limbo_die_list = limbo_node;
6528     }
6529
6530   return die;
6531 }
6532
6533 /* Return the DIE associated with the given type specifier.  */
6534
6535 static inline dw_die_ref
6536 lookup_type_die (tree type)
6537 {
6538   return TYPE_SYMTAB_DIE (type);
6539 }
6540
6541 /* Equate a DIE to a given type specifier.  */
6542
6543 static inline void
6544 equate_type_number_to_die (tree type, dw_die_ref type_die)
6545 {
6546   TYPE_SYMTAB_DIE (type) = type_die;
6547 }
6548
6549 /* Returns a hash value for X (which really is a die_struct).  */
6550
6551 static hashval_t
6552 decl_die_table_hash (const void *x)
6553 {
6554   return (hashval_t) ((const_dw_die_ref) x)->decl_id;
6555 }
6556
6557 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
6558
6559 static int
6560 decl_die_table_eq (const void *x, const void *y)
6561 {
6562   return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
6563 }
6564
6565 /* Return the DIE associated with a given declaration.  */
6566
6567 static inline dw_die_ref
6568 lookup_decl_die (tree decl)
6569 {
6570   return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
6571 }
6572
6573 /* Returns a hash value for X (which really is a var_loc_list).  */
6574
6575 static hashval_t
6576 decl_loc_table_hash (const void *x)
6577 {
6578   return (hashval_t) ((const var_loc_list *) x)->decl_id;
6579 }
6580
6581 /* Return nonzero if decl_id of var_loc_list X is the same as
6582    UID of decl *Y.  */
6583
6584 static int
6585 decl_loc_table_eq (const void *x, const void *y)
6586 {
6587   return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
6588 }
6589
6590 /* Return the var_loc list associated with a given declaration.  */
6591
6592 static inline var_loc_list *
6593 lookup_decl_loc (const_tree decl)
6594 {
6595   return (var_loc_list *)
6596     htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
6597 }
6598
6599 /* Equate a DIE to a particular declaration.  */
6600
6601 static void
6602 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
6603 {
6604   unsigned int decl_id = DECL_UID (decl);
6605   void **slot;
6606
6607   slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
6608   *slot = decl_die;
6609   decl_die->decl_id = decl_id;
6610 }
6611
6612 /* Add a variable location node to the linked list for DECL.  */
6613
6614 static void
6615 add_var_loc_to_decl (tree decl, struct var_loc_node *loc)
6616 {
6617   unsigned int decl_id = DECL_UID (decl);
6618   var_loc_list *temp;
6619   void **slot;
6620
6621   slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
6622   if (*slot == NULL)
6623     {
6624       temp = GGC_CNEW (var_loc_list);
6625       temp->decl_id = decl_id;
6626       *slot = temp;
6627     }
6628   else
6629     temp = (var_loc_list *) *slot;
6630
6631   if (temp->last)
6632     {
6633       /* If the current location is the same as the end of the list,
6634          and either both or neither of the locations is uninitialized,
6635          we have nothing to do.  */
6636       if ((!rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->last->var_loc_note),
6637                          NOTE_VAR_LOCATION_LOC (loc->var_loc_note)))
6638           || ((NOTE_VAR_LOCATION_STATUS (temp->last->var_loc_note)
6639                != NOTE_VAR_LOCATION_STATUS (loc->var_loc_note))
6640               && ((NOTE_VAR_LOCATION_STATUS (temp->last->var_loc_note)
6641                    == VAR_INIT_STATUS_UNINITIALIZED)
6642                   || (NOTE_VAR_LOCATION_STATUS (loc->var_loc_note)
6643                       == VAR_INIT_STATUS_UNINITIALIZED))))
6644         {
6645           /* Add LOC to the end of list and update LAST.  */
6646           temp->last->next = loc;
6647           temp->last = loc;
6648         }
6649     }
6650   /* Do not add empty location to the beginning of the list.  */
6651   else if (NOTE_VAR_LOCATION_LOC (loc->var_loc_note) != NULL_RTX)
6652     {
6653       temp->first = loc;
6654       temp->last = loc;
6655     }
6656 }
6657 \f
6658 /* Keep track of the number of spaces used to indent the
6659    output of the debugging routines that print the structure of
6660    the DIE internal representation.  */
6661 static int print_indent;
6662
6663 /* Indent the line the number of spaces given by print_indent.  */
6664
6665 static inline void
6666 print_spaces (FILE *outfile)
6667 {
6668   fprintf (outfile, "%*s", print_indent, "");
6669 }
6670
6671 /* Print the information associated with a given DIE, and its children.
6672    This routine is a debugging aid only.  */
6673
6674 static void
6675 print_die (dw_die_ref die, FILE *outfile)
6676 {
6677   dw_attr_ref a;
6678   dw_die_ref c;
6679   unsigned ix;
6680
6681   print_spaces (outfile);
6682   fprintf (outfile, "DIE %4ld: %s\n",
6683            die->die_offset, dwarf_tag_name (die->die_tag));
6684   print_spaces (outfile);
6685   fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
6686   fprintf (outfile, " offset: %ld\n", die->die_offset);
6687
6688   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6689     {
6690       print_spaces (outfile);
6691       fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
6692
6693       switch (AT_class (a))
6694         {
6695         case dw_val_class_addr:
6696           fprintf (outfile, "address");
6697           break;
6698         case dw_val_class_offset:
6699           fprintf (outfile, "offset");
6700           break;
6701         case dw_val_class_loc:
6702           fprintf (outfile, "location descriptor");
6703           break;
6704         case dw_val_class_loc_list:
6705           fprintf (outfile, "location list -> label:%s",
6706                    AT_loc_list (a)->ll_symbol);
6707           break;
6708         case dw_val_class_range_list:
6709           fprintf (outfile, "range list");
6710           break;
6711         case dw_val_class_const:
6712           fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
6713           break;
6714         case dw_val_class_unsigned_const:
6715           fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
6716           break;
6717         case dw_val_class_long_long:
6718           fprintf (outfile, "constant (%lu,%lu)",
6719                    a->dw_attr_val.v.val_long_long.hi,
6720                    a->dw_attr_val.v.val_long_long.low);
6721           break;
6722         case dw_val_class_vec:
6723           fprintf (outfile, "floating-point or vector constant");
6724           break;
6725         case dw_val_class_flag:
6726           fprintf (outfile, "%u", AT_flag (a));
6727           break;
6728         case dw_val_class_die_ref:
6729           if (AT_ref (a) != NULL)
6730             {
6731               if (AT_ref (a)->die_symbol)
6732                 fprintf (outfile, "die -> label: %s", AT_ref (a)->die_symbol);
6733               else
6734                 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
6735             }
6736           else
6737             fprintf (outfile, "die -> <null>");
6738           break;
6739         case dw_val_class_lbl_id:
6740         case dw_val_class_lineptr:
6741         case dw_val_class_macptr:
6742           fprintf (outfile, "label: %s", AT_lbl (a));
6743           break;
6744         case dw_val_class_str:
6745           if (AT_string (a) != NULL)
6746             fprintf (outfile, "\"%s\"", AT_string (a));
6747           else
6748             fprintf (outfile, "<null>");
6749           break;
6750         case dw_val_class_file:
6751           fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
6752                    AT_file (a)->emitted_number);
6753           break;
6754         default:
6755           break;
6756         }
6757
6758       fprintf (outfile, "\n");
6759     }
6760
6761   if (die->die_child != NULL)
6762     {
6763       print_indent += 4;
6764       FOR_EACH_CHILD (die, c, print_die (c, outfile));
6765       print_indent -= 4;
6766     }
6767   if (print_indent == 0)
6768     fprintf (outfile, "\n");
6769 }
6770
6771 /* Print the contents of the source code line number correspondence table.
6772    This routine is a debugging aid only.  */
6773
6774 static void
6775 print_dwarf_line_table (FILE *outfile)
6776 {
6777   unsigned i;
6778   dw_line_info_ref line_info;
6779
6780   fprintf (outfile, "\n\nDWARF source line information\n");
6781   for (i = 1; i < line_info_table_in_use; i++)
6782     {
6783       line_info = &line_info_table[i];
6784       fprintf (outfile, "%5d: %4ld %6ld\n", i,
6785                line_info->dw_file_num,
6786                line_info->dw_line_num);
6787     }
6788
6789   fprintf (outfile, "\n\n");
6790 }
6791
6792 /* Print the information collected for a given DIE.  */
6793
6794 void
6795 debug_dwarf_die (dw_die_ref die)
6796 {
6797   print_die (die, stderr);
6798 }
6799
6800 /* Print all DWARF information collected for the compilation unit.
6801    This routine is a debugging aid only.  */
6802
6803 void
6804 debug_dwarf (void)
6805 {
6806   print_indent = 0;
6807   print_die (comp_unit_die, stderr);
6808   if (! DWARF2_ASM_LINE_DEBUG_INFO)
6809     print_dwarf_line_table (stderr);
6810 }
6811 \f
6812 /* Start a new compilation unit DIE for an include file.  OLD_UNIT is the CU
6813    for the enclosing include file, if any.  BINCL_DIE is the DW_TAG_GNU_BINCL
6814    DIE that marks the start of the DIEs for this include file.  */
6815
6816 static dw_die_ref
6817 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
6818 {
6819   const char *filename = get_AT_string (bincl_die, DW_AT_name);
6820   dw_die_ref new_unit = gen_compile_unit_die (filename);
6821
6822   new_unit->die_sib = old_unit;
6823   return new_unit;
6824 }
6825
6826 /* Close an include-file CU and reopen the enclosing one.  */
6827
6828 static dw_die_ref
6829 pop_compile_unit (dw_die_ref old_unit)
6830 {
6831   dw_die_ref new_unit = old_unit->die_sib;
6832
6833   old_unit->die_sib = NULL;
6834   return new_unit;
6835 }
6836
6837 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6838 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
6839
6840 /* Calculate the checksum of a location expression.  */
6841
6842 static inline void
6843 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6844 {
6845   CHECKSUM (loc->dw_loc_opc);
6846   CHECKSUM (loc->dw_loc_oprnd1);
6847   CHECKSUM (loc->dw_loc_oprnd2);
6848 }
6849
6850 /* Calculate the checksum of an attribute.  */
6851
6852 static void
6853 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
6854 {
6855   dw_loc_descr_ref loc;
6856   rtx r;
6857
6858   CHECKSUM (at->dw_attr);
6859
6860   /* We don't care that this was compiled with a different compiler
6861      snapshot; if the output is the same, that's what matters.  */
6862   if (at->dw_attr == DW_AT_producer)
6863     return;
6864
6865   switch (AT_class (at))
6866     {
6867     case dw_val_class_const:
6868       CHECKSUM (at->dw_attr_val.v.val_int);
6869       break;
6870     case dw_val_class_unsigned_const:
6871       CHECKSUM (at->dw_attr_val.v.val_unsigned);
6872       break;
6873     case dw_val_class_long_long:
6874       CHECKSUM (at->dw_attr_val.v.val_long_long);
6875       break;
6876     case dw_val_class_vec:
6877       CHECKSUM (at->dw_attr_val.v.val_vec);
6878       break;
6879     case dw_val_class_flag:
6880       CHECKSUM (at->dw_attr_val.v.val_flag);
6881       break;
6882     case dw_val_class_str:
6883       CHECKSUM_STRING (AT_string (at));
6884       break;
6885
6886     case dw_val_class_addr:
6887       r = AT_addr (at);
6888       gcc_assert (GET_CODE (r) == SYMBOL_REF);
6889       CHECKSUM_STRING (XSTR (r, 0));
6890       break;
6891
6892     case dw_val_class_offset:
6893       CHECKSUM (at->dw_attr_val.v.val_offset);
6894       break;
6895
6896     case dw_val_class_loc:
6897       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6898         loc_checksum (loc, ctx);
6899       break;
6900
6901     case dw_val_class_die_ref:
6902       die_checksum (AT_ref (at), ctx, mark);
6903       break;
6904
6905     case dw_val_class_fde_ref:
6906     case dw_val_class_lbl_id:
6907     case dw_val_class_lineptr:
6908     case dw_val_class_macptr:
6909       break;
6910
6911     case dw_val_class_file:
6912       CHECKSUM_STRING (AT_file (at)->filename);
6913       break;
6914
6915     default:
6916       break;
6917     }
6918 }
6919
6920 /* Calculate the checksum of a DIE.  */
6921
6922 static void
6923 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6924 {
6925   dw_die_ref c;
6926   dw_attr_ref a;
6927   unsigned ix;
6928
6929   /* To avoid infinite recursion.  */
6930   if (die->die_mark)
6931     {
6932       CHECKSUM (die->die_mark);
6933       return;
6934     }
6935   die->die_mark = ++(*mark);
6936
6937   CHECKSUM (die->die_tag);
6938
6939   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6940     attr_checksum (a, ctx, mark);
6941
6942   FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
6943 }
6944
6945 #undef CHECKSUM
6946 #undef CHECKSUM_STRING
6947
6948 /* Do the location expressions look same?  */
6949 static inline int
6950 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6951 {
6952   return loc1->dw_loc_opc == loc2->dw_loc_opc
6953          && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6954          && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6955 }
6956
6957 /* Do the values look the same?  */
6958 static int
6959 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6960 {
6961   dw_loc_descr_ref loc1, loc2;
6962   rtx r1, r2;
6963
6964   if (v1->val_class != v2->val_class)
6965     return 0;
6966
6967   switch (v1->val_class)
6968     {
6969     case dw_val_class_const:
6970       return v1->v.val_int == v2->v.val_int;
6971     case dw_val_class_unsigned_const:
6972       return v1->v.val_unsigned == v2->v.val_unsigned;
6973     case dw_val_class_long_long:
6974       return v1->v.val_long_long.hi == v2->v.val_long_long.hi
6975              && v1->v.val_long_long.low == v2->v.val_long_long.low;
6976     case dw_val_class_vec:
6977       if (v1->v.val_vec.length != v2->v.val_vec.length
6978           || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6979         return 0;
6980       if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6981                   v1->v.val_vec.length * v1->v.val_vec.elt_size))
6982         return 0;
6983       return 1;
6984     case dw_val_class_flag:
6985       return v1->v.val_flag == v2->v.val_flag;
6986     case dw_val_class_str:
6987       return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
6988
6989     case dw_val_class_addr:
6990       r1 = v1->v.val_addr;
6991       r2 = v2->v.val_addr;
6992       if (GET_CODE (r1) != GET_CODE (r2))
6993         return 0;
6994       gcc_assert (GET_CODE (r1) == SYMBOL_REF);
6995       return !strcmp (XSTR (r1, 0), XSTR (r2, 0));
6996
6997     case dw_val_class_offset:
6998       return v1->v.val_offset == v2->v.val_offset;
6999
7000     case dw_val_class_loc:
7001       for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
7002            loc1 && loc2;
7003            loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
7004         if (!same_loc_p (loc1, loc2, mark))
7005           return 0;
7006       return !loc1 && !loc2;
7007
7008     case dw_val_class_die_ref:
7009       return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
7010
7011     case dw_val_class_fde_ref:
7012     case dw_val_class_lbl_id:
7013     case dw_val_class_lineptr:
7014     case dw_val_class_macptr:
7015       return 1;
7016
7017     case dw_val_class_file:
7018       return v1->v.val_file == v2->v.val_file;
7019
7020     default:
7021       return 1;
7022     }
7023 }
7024
7025 /* Do the attributes look the same?  */
7026
7027 static int
7028 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
7029 {
7030   if (at1->dw_attr != at2->dw_attr)
7031     return 0;
7032
7033   /* We don't care that this was compiled with a different compiler
7034      snapshot; if the output is the same, that's what matters. */
7035   if (at1->dw_attr == DW_AT_producer)
7036     return 1;
7037
7038   return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
7039 }
7040
7041 /* Do the dies look the same?  */
7042
7043 static int
7044 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
7045 {
7046   dw_die_ref c1, c2;
7047   dw_attr_ref a1;
7048   unsigned ix;
7049
7050   /* To avoid infinite recursion.  */
7051   if (die1->die_mark)
7052     return die1->die_mark == die2->die_mark;
7053   die1->die_mark = die2->die_mark = ++(*mark);
7054
7055   if (die1->die_tag != die2->die_tag)
7056     return 0;
7057
7058   if (VEC_length (dw_attr_node, die1->die_attr)
7059       != VEC_length (dw_attr_node, die2->die_attr))
7060     return 0;
7061
7062   for (ix = 0; VEC_iterate (dw_attr_node, die1->die_attr, ix, a1); ix++)
7063     if (!same_attr_p (a1, VEC_index (dw_attr_node, die2->die_attr, ix), mark))
7064       return 0;
7065
7066   c1 = die1->die_child;
7067   c2 = die2->die_child;
7068   if (! c1)
7069     {
7070       if (c2)
7071         return 0;
7072     }
7073   else
7074     for (;;)
7075       {
7076         if (!same_die_p (c1, c2, mark))
7077           return 0;
7078         c1 = c1->die_sib;
7079         c2 = c2->die_sib;
7080         if (c1 == die1->die_child)
7081           {
7082             if (c2 == die2->die_child)
7083               break;
7084             else
7085               return 0;
7086           }
7087     }
7088
7089   return 1;
7090 }
7091
7092 /* Do the dies look the same?  Wrapper around same_die_p.  */
7093
7094 static int
7095 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
7096 {
7097   int mark = 0;
7098   int ret = same_die_p (die1, die2, &mark);
7099
7100   unmark_all_dies (die1);
7101   unmark_all_dies (die2);
7102
7103   return ret;
7104 }
7105
7106 /* The prefix to attach to symbols on DIEs in the current comdat debug
7107    info section.  */
7108 static char *comdat_symbol_id;
7109
7110 /* The index of the current symbol within the current comdat CU.  */
7111 static unsigned int comdat_symbol_number;
7112
7113 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
7114    children, and set comdat_symbol_id accordingly.  */
7115
7116 static void
7117 compute_section_prefix (dw_die_ref unit_die)
7118 {
7119   const char *die_name = get_AT_string (unit_die, DW_AT_name);
7120   const char *base = die_name ? lbasename (die_name) : "anonymous";
7121   char *name = XALLOCAVEC (char, strlen (base) + 64);
7122   char *p;
7123   int i, mark;
7124   unsigned char checksum[16];
7125   struct md5_ctx ctx;
7126
7127   /* Compute the checksum of the DIE, then append part of it as hex digits to
7128      the name filename of the unit.  */
7129
7130   md5_init_ctx (&ctx);
7131   mark = 0;
7132   die_checksum (unit_die, &ctx, &mark);
7133   unmark_all_dies (unit_die);
7134   md5_finish_ctx (&ctx, checksum);
7135
7136   sprintf (name, "%s.", base);
7137   clean_symbol_name (name);
7138
7139   p = name + strlen (name);
7140   for (i = 0; i < 4; i++)
7141     {
7142       sprintf (p, "%.2x", checksum[i]);
7143       p += 2;
7144     }
7145
7146   comdat_symbol_id = unit_die->die_symbol = xstrdup (name);
7147   comdat_symbol_number = 0;
7148 }
7149
7150 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P.  */
7151
7152 static int
7153 is_type_die (dw_die_ref die)
7154 {
7155   switch (die->die_tag)
7156     {
7157     case DW_TAG_array_type:
7158     case DW_TAG_class_type:
7159     case DW_TAG_interface_type:
7160     case DW_TAG_enumeration_type:
7161     case DW_TAG_pointer_type:
7162     case DW_TAG_reference_type:
7163     case DW_TAG_string_type:
7164     case DW_TAG_structure_type:
7165     case DW_TAG_subroutine_type:
7166     case DW_TAG_union_type:
7167     case DW_TAG_ptr_to_member_type:
7168     case DW_TAG_set_type:
7169     case DW_TAG_subrange_type:
7170     case DW_TAG_base_type:
7171     case DW_TAG_const_type:
7172     case DW_TAG_file_type:
7173     case DW_TAG_packed_type:
7174     case DW_TAG_volatile_type:
7175     case DW_TAG_typedef:
7176       return 1;
7177     default:
7178       return 0;
7179     }
7180 }
7181
7182 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
7183    Basically, we want to choose the bits that are likely to be shared between
7184    compilations (types) and leave out the bits that are specific to individual
7185    compilations (functions).  */
7186
7187 static int
7188 is_comdat_die (dw_die_ref c)
7189 {
7190   /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
7191      we do for stabs.  The advantage is a greater likelihood of sharing between
7192      objects that don't include headers in the same order (and therefore would
7193      put the base types in a different comdat).  jason 8/28/00 */
7194
7195   if (c->die_tag == DW_TAG_base_type)
7196     return 0;
7197
7198   if (c->die_tag == DW_TAG_pointer_type
7199       || c->die_tag == DW_TAG_reference_type
7200       || c->die_tag == DW_TAG_const_type
7201       || c->die_tag == DW_TAG_volatile_type)
7202     {
7203       dw_die_ref t = get_AT_ref (c, DW_AT_type);
7204
7205       return t ? is_comdat_die (t) : 0;
7206     }
7207
7208   return is_type_die (c);
7209 }
7210
7211 /* Returns 1 iff C is the sort of DIE that might be referred to from another
7212    compilation unit.  */
7213
7214 static int
7215 is_symbol_die (dw_die_ref c)
7216 {
7217   return (is_type_die (c)
7218           || (get_AT (c, DW_AT_declaration)
7219               && !get_AT (c, DW_AT_specification))
7220           || c->die_tag == DW_TAG_namespace
7221           || c->die_tag == DW_TAG_module);
7222 }
7223
7224 static char *
7225 gen_internal_sym (const char *prefix)
7226 {
7227   char buf[256];
7228
7229   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
7230   return xstrdup (buf);
7231 }
7232
7233 /* Assign symbols to all worthy DIEs under DIE.  */
7234
7235 static void
7236 assign_symbol_names (dw_die_ref die)
7237 {
7238   dw_die_ref c;
7239
7240   if (is_symbol_die (die))
7241     {
7242       if (comdat_symbol_id)
7243         {
7244           char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
7245
7246           sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
7247                    comdat_symbol_id, comdat_symbol_number++);
7248           die->die_symbol = xstrdup (p);
7249         }
7250       else
7251         die->die_symbol = gen_internal_sym ("LDIE");
7252     }
7253
7254   FOR_EACH_CHILD (die, c, assign_symbol_names (c));
7255 }
7256
7257 struct cu_hash_table_entry
7258 {
7259   dw_die_ref cu;
7260   unsigned min_comdat_num, max_comdat_num;
7261   struct cu_hash_table_entry *next;
7262 };
7263
7264 /* Routines to manipulate hash table of CUs.  */
7265 static hashval_t
7266 htab_cu_hash (const void *of)
7267 {
7268   const struct cu_hash_table_entry *const entry =
7269     (const struct cu_hash_table_entry *) of;
7270
7271   return htab_hash_string (entry->cu->die_symbol);
7272 }
7273
7274 static int
7275 htab_cu_eq (const void *of1, const void *of2)
7276 {
7277   const struct cu_hash_table_entry *const entry1 =
7278     (const struct cu_hash_table_entry *) of1;
7279   const struct die_struct *const entry2 = (const struct die_struct *) of2;
7280
7281   return !strcmp (entry1->cu->die_symbol, entry2->die_symbol);
7282 }
7283
7284 static void
7285 htab_cu_del (void *what)
7286 {
7287   struct cu_hash_table_entry *next,
7288     *entry = (struct cu_hash_table_entry *) what;
7289
7290   while (entry)
7291     {
7292       next = entry->next;
7293       free (entry);
7294       entry = next;
7295     }
7296 }
7297
7298 /* Check whether we have already seen this CU and set up SYM_NUM
7299    accordingly.  */
7300 static int
7301 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
7302 {
7303   struct cu_hash_table_entry dummy;
7304   struct cu_hash_table_entry **slot, *entry, *last = &dummy;
7305
7306   dummy.max_comdat_num = 0;
7307
7308   slot = (struct cu_hash_table_entry **)
7309     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
7310         INSERT);
7311   entry = *slot;
7312
7313   for (; entry; last = entry, entry = entry->next)
7314     {
7315       if (same_die_p_wrap (cu, entry->cu))
7316         break;
7317     }
7318
7319   if (entry)
7320     {
7321       *sym_num = entry->min_comdat_num;
7322       return 1;
7323     }
7324
7325   entry = XCNEW (struct cu_hash_table_entry);
7326   entry->cu = cu;
7327   entry->min_comdat_num = *sym_num = last->max_comdat_num;
7328   entry->next = *slot;
7329   *slot = entry;
7330
7331   return 0;
7332 }
7333
7334 /* Record SYM_NUM to record of CU in HTABLE.  */
7335 static void
7336 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
7337 {
7338   struct cu_hash_table_entry **slot, *entry;
7339
7340   slot = (struct cu_hash_table_entry **)
7341     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
7342         NO_INSERT);
7343   entry = *slot;
7344
7345   entry->max_comdat_num = sym_num;
7346 }
7347
7348 /* Traverse the DIE (which is always comp_unit_die), and set up
7349    additional compilation units for each of the include files we see
7350    bracketed by BINCL/EINCL.  */
7351
7352 static void
7353 break_out_includes (dw_die_ref die)
7354 {
7355   dw_die_ref c;
7356   dw_die_ref unit = NULL;
7357   limbo_die_node *node, **pnode;
7358   htab_t cu_hash_table;
7359
7360   c = die->die_child;
7361   if (c) do {
7362     dw_die_ref prev = c;
7363     c = c->die_sib;
7364     while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
7365            || (unit && is_comdat_die (c)))
7366       {
7367         dw_die_ref next = c->die_sib;
7368
7369         /* This DIE is for a secondary CU; remove it from the main one.  */
7370         remove_child_with_prev (c, prev);
7371
7372         if (c->die_tag == DW_TAG_GNU_BINCL)
7373           unit = push_new_compile_unit (unit, c);
7374         else if (c->die_tag == DW_TAG_GNU_EINCL)
7375           unit = pop_compile_unit (unit);
7376         else
7377           add_child_die (unit, c);
7378         c = next;
7379         if (c == die->die_child)
7380           break;
7381       }
7382   } while (c != die->die_child);
7383
7384 #if 0
7385   /* We can only use this in debugging, since the frontend doesn't check
7386      to make sure that we leave every include file we enter.  */
7387   gcc_assert (!unit);
7388 #endif
7389
7390   assign_symbol_names (die);
7391   cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
7392   for (node = limbo_die_list, pnode = &limbo_die_list;
7393        node;
7394        node = node->next)
7395     {
7396       int is_dupl;
7397
7398       compute_section_prefix (node->die);
7399       is_dupl = check_duplicate_cu (node->die, cu_hash_table,
7400                         &comdat_symbol_number);
7401       assign_symbol_names (node->die);
7402       if (is_dupl)
7403         *pnode = node->next;
7404       else
7405         {
7406           pnode = &node->next;
7407           record_comdat_symbol_number (node->die, cu_hash_table,
7408                 comdat_symbol_number);
7409         }
7410     }
7411   htab_delete (cu_hash_table);
7412 }
7413
7414 /* Traverse the DIE and add a sibling attribute if it may have the
7415    effect of speeding up access to siblings.  To save some space,
7416    avoid generating sibling attributes for DIE's without children.  */
7417
7418 static void
7419 add_sibling_attributes (dw_die_ref die)
7420 {
7421   dw_die_ref c;
7422
7423   if (! die->die_child)
7424     return;
7425
7426   if (die->die_parent && die != die->die_parent->die_child)
7427     add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
7428
7429   FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
7430 }
7431
7432 /* Output all location lists for the DIE and its children.  */
7433
7434 static void
7435 output_location_lists (dw_die_ref die)
7436 {
7437   dw_die_ref c;
7438   dw_attr_ref a;
7439   unsigned ix;
7440
7441   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7442     if (AT_class (a) == dw_val_class_loc_list)
7443       output_loc_list (AT_loc_list (a));
7444
7445   FOR_EACH_CHILD (die, c, output_location_lists (c));
7446 }
7447
7448 /* The format of each DIE (and its attribute value pairs) is encoded in an
7449    abbreviation table.  This routine builds the abbreviation table and assigns
7450    a unique abbreviation id for each abbreviation entry.  The children of each
7451    die are visited recursively.  */
7452
7453 static void
7454 build_abbrev_table (dw_die_ref die)
7455 {
7456   unsigned long abbrev_id;
7457   unsigned int n_alloc;
7458   dw_die_ref c;
7459   dw_attr_ref a;
7460   unsigned ix;
7461
7462   /* Scan the DIE references, and mark as external any that refer to
7463      DIEs from other CUs (i.e. those which are not marked).  */
7464   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7465     if (AT_class (a) == dw_val_class_die_ref
7466         && AT_ref (a)->die_mark == 0)
7467       {
7468         gcc_assert (AT_ref (a)->die_symbol);
7469
7470         set_AT_ref_external (a, 1);
7471       }
7472
7473   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7474     {
7475       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7476       dw_attr_ref die_a, abbrev_a;
7477       unsigned ix;
7478       bool ok = true;
7479
7480       if (abbrev->die_tag != die->die_tag)
7481         continue;
7482       if ((abbrev->die_child != NULL) != (die->die_child != NULL))
7483         continue;
7484
7485       if (VEC_length (dw_attr_node, abbrev->die_attr)
7486           != VEC_length (dw_attr_node, die->die_attr))
7487         continue;
7488
7489       for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, die_a); ix++)
7490         {
7491           abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
7492           if ((abbrev_a->dw_attr != die_a->dw_attr)
7493               || (value_format (abbrev_a) != value_format (die_a)))
7494             {
7495               ok = false;
7496               break;
7497             }
7498         }
7499       if (ok)
7500         break;
7501     }
7502
7503   if (abbrev_id >= abbrev_die_table_in_use)
7504     {
7505       if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
7506         {
7507           n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
7508           abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
7509                                             n_alloc);
7510
7511           memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
7512                  (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
7513           abbrev_die_table_allocated = n_alloc;
7514         }
7515
7516       ++abbrev_die_table_in_use;
7517       abbrev_die_table[abbrev_id] = die;
7518     }
7519
7520   die->die_abbrev = abbrev_id;
7521   FOR_EACH_CHILD (die, c, build_abbrev_table (c));
7522 }
7523 \f
7524 /* Return the power-of-two number of bytes necessary to represent VALUE.  */
7525
7526 static int
7527 constant_size (unsigned HOST_WIDE_INT value)
7528 {
7529   int log;
7530
7531   if (value == 0)
7532     log = 0;
7533   else
7534     log = floor_log2 (value);
7535
7536   log = log / 8;
7537   log = 1 << (floor_log2 (log) + 1);
7538
7539   return log;
7540 }
7541
7542 /* Return the size of a DIE as it is represented in the
7543    .debug_info section.  */
7544
7545 static unsigned long
7546 size_of_die (dw_die_ref die)
7547 {
7548   unsigned long size = 0;
7549   dw_attr_ref a;
7550   unsigned ix;
7551
7552   size += size_of_uleb128 (die->die_abbrev);
7553   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7554     {
7555       switch (AT_class (a))
7556         {
7557         case dw_val_class_addr:
7558           size += DWARF2_ADDR_SIZE;
7559           break;
7560         case dw_val_class_offset:
7561           size += DWARF_OFFSET_SIZE;
7562           break;
7563         case dw_val_class_loc:
7564           {
7565             unsigned long lsize = size_of_locs (AT_loc (a));
7566
7567             /* Block length.  */
7568             size += constant_size (lsize);
7569             size += lsize;
7570           }
7571           break;
7572         case dw_val_class_loc_list:
7573           size += DWARF_OFFSET_SIZE;
7574           break;
7575         case dw_val_class_range_list:
7576           size += DWARF_OFFSET_SIZE;
7577           break;
7578         case dw_val_class_const:
7579           size += size_of_sleb128 (AT_int (a));
7580           break;
7581         case dw_val_class_unsigned_const:
7582           size += constant_size (AT_unsigned (a));
7583           break;
7584         case dw_val_class_long_long:
7585           size += 1 + 2*HOST_BITS_PER_LONG/HOST_BITS_PER_CHAR; /* block */
7586           break;
7587         case dw_val_class_vec:
7588           size += constant_size (a->dw_attr_val.v.val_vec.length
7589                                  * a->dw_attr_val.v.val_vec.elt_size)
7590                   + a->dw_attr_val.v.val_vec.length
7591                     * a->dw_attr_val.v.val_vec.elt_size; /* block */
7592           break;
7593         case dw_val_class_flag:
7594           size += 1;
7595           break;
7596         case dw_val_class_die_ref:
7597           if (AT_ref_external (a))
7598             size += DWARF2_ADDR_SIZE;
7599           else
7600             size += DWARF_OFFSET_SIZE;
7601           break;
7602         case dw_val_class_fde_ref:
7603           size += DWARF_OFFSET_SIZE;
7604           break;
7605         case dw_val_class_lbl_id:
7606           size += DWARF2_ADDR_SIZE;
7607           break;
7608         case dw_val_class_lineptr:
7609         case dw_val_class_macptr:
7610           size += DWARF_OFFSET_SIZE;
7611           break;
7612         case dw_val_class_str:
7613           if (AT_string_form (a) == DW_FORM_strp)
7614             size += DWARF_OFFSET_SIZE;
7615           else
7616             size += strlen (a->dw_attr_val.v.val_str->str) + 1;
7617           break;
7618         case dw_val_class_file:
7619           size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
7620           break;
7621         default:
7622           gcc_unreachable ();
7623         }
7624     }
7625
7626   return size;
7627 }
7628
7629 /* Size the debugging information associated with a given DIE.  Visits the
7630    DIE's children recursively.  Updates the global variable next_die_offset, on
7631    each time through.  Uses the current value of next_die_offset to update the
7632    die_offset field in each DIE.  */
7633
7634 static void
7635 calc_die_sizes (dw_die_ref die)
7636 {
7637   dw_die_ref c;
7638
7639   die->die_offset = next_die_offset;
7640   next_die_offset += size_of_die (die);
7641
7642   FOR_EACH_CHILD (die, c, calc_die_sizes (c));
7643
7644   if (die->die_child != NULL)
7645     /* Count the null byte used to terminate sibling lists.  */
7646     next_die_offset += 1;
7647 }
7648
7649 /* Set the marks for a die and its children.  We do this so
7650    that we know whether or not a reference needs to use FORM_ref_addr; only
7651    DIEs in the same CU will be marked.  We used to clear out the offset
7652    and use that as the flag, but ran into ordering problems.  */
7653
7654 static void
7655 mark_dies (dw_die_ref die)
7656 {
7657   dw_die_ref c;
7658
7659   gcc_assert (!die->die_mark);
7660
7661   die->die_mark = 1;
7662   FOR_EACH_CHILD (die, c, mark_dies (c));
7663 }
7664
7665 /* Clear the marks for a die and its children.  */
7666
7667 static void
7668 unmark_dies (dw_die_ref die)
7669 {
7670   dw_die_ref c;
7671
7672   gcc_assert (die->die_mark);
7673
7674   die->die_mark = 0;
7675   FOR_EACH_CHILD (die, c, unmark_dies (c));
7676 }
7677
7678 /* Clear the marks for a die, its children and referred dies.  */
7679
7680 static void
7681 unmark_all_dies (dw_die_ref die)
7682 {
7683   dw_die_ref c;
7684   dw_attr_ref a;
7685   unsigned ix;
7686
7687   if (!die->die_mark)
7688     return;
7689   die->die_mark = 0;
7690
7691   FOR_EACH_CHILD (die, c, unmark_all_dies (c));
7692
7693   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7694     if (AT_class (a) == dw_val_class_die_ref)
7695       unmark_all_dies (AT_ref (a));
7696 }
7697
7698 /* Return the size of the .debug_pubnames or .debug_pubtypes table
7699    generated for the compilation unit.  */
7700
7701 static unsigned long
7702 size_of_pubnames (VEC (pubname_entry, gc) * names)
7703 {
7704   unsigned long size;
7705   unsigned i;
7706   pubname_ref p;
7707
7708   size = DWARF_PUBNAMES_HEADER_SIZE;
7709   for (i = 0; VEC_iterate (pubname_entry, names, i, p); i++)
7710     if (names != pubtype_table
7711         || p->die->die_offset != 0
7712         || !flag_eliminate_unused_debug_types)
7713       size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
7714
7715   size += DWARF_OFFSET_SIZE;
7716   return size;
7717 }
7718
7719 /* Return the size of the information in the .debug_aranges section.  */
7720
7721 static unsigned long
7722 size_of_aranges (void)
7723 {
7724   unsigned long size;
7725
7726   size = DWARF_ARANGES_HEADER_SIZE;
7727
7728   /* Count the address/length pair for this compilation unit.  */
7729   if (text_section_used)
7730     size += 2 * DWARF2_ADDR_SIZE;
7731   if (cold_text_section_used)
7732     size += 2 * DWARF2_ADDR_SIZE;
7733   size += 2 * DWARF2_ADDR_SIZE * arange_table_in_use;
7734
7735   /* Count the two zero words used to terminated the address range table.  */
7736   size += 2 * DWARF2_ADDR_SIZE;
7737   return size;
7738 }
7739 \f
7740 /* Select the encoding of an attribute value.  */
7741
7742 static enum dwarf_form
7743 value_format (dw_attr_ref a)
7744 {
7745   switch (a->dw_attr_val.val_class)
7746     {
7747     case dw_val_class_addr:
7748       return DW_FORM_addr;
7749     case dw_val_class_range_list:
7750     case dw_val_class_offset:
7751     case dw_val_class_loc_list:
7752       switch (DWARF_OFFSET_SIZE)
7753         {
7754         case 4:
7755           return DW_FORM_data4;
7756         case 8:
7757           return DW_FORM_data8;
7758         default:
7759           gcc_unreachable ();
7760         }
7761     case dw_val_class_loc:
7762       switch (constant_size (size_of_locs (AT_loc (a))))
7763         {
7764         case 1:
7765           return DW_FORM_block1;
7766         case 2:
7767           return DW_FORM_block2;
7768         default:
7769           gcc_unreachable ();
7770         }
7771     case dw_val_class_const:
7772       return DW_FORM_sdata;
7773     case dw_val_class_unsigned_const:
7774       switch (constant_size (AT_unsigned (a)))
7775         {
7776         case 1:
7777           return DW_FORM_data1;
7778         case 2:
7779           return DW_FORM_data2;
7780         case 4:
7781           return DW_FORM_data4;
7782         case 8:
7783           return DW_FORM_data8;
7784         default:
7785           gcc_unreachable ();
7786         }
7787     case dw_val_class_long_long:
7788       return DW_FORM_block1;
7789     case dw_val_class_vec:
7790       switch (constant_size (a->dw_attr_val.v.val_vec.length
7791                              * a->dw_attr_val.v.val_vec.elt_size))
7792         {
7793         case 1:
7794           return DW_FORM_block1;
7795         case 2:
7796           return DW_FORM_block2;
7797         case 4:
7798           return DW_FORM_block4;
7799         default:
7800           gcc_unreachable ();
7801         }
7802     case dw_val_class_flag:
7803       return DW_FORM_flag;
7804     case dw_val_class_die_ref:
7805       if (AT_ref_external (a))
7806         return DW_FORM_ref_addr;
7807       else
7808         return DW_FORM_ref;
7809     case dw_val_class_fde_ref:
7810       return DW_FORM_data;
7811     case dw_val_class_lbl_id:
7812       return DW_FORM_addr;
7813     case dw_val_class_lineptr:
7814     case dw_val_class_macptr:
7815       return DW_FORM_data;
7816     case dw_val_class_str:
7817       return AT_string_form (a);
7818     case dw_val_class_file:
7819       switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
7820         {
7821         case 1:
7822           return DW_FORM_data1;
7823         case 2:
7824           return DW_FORM_data2;
7825         case 4:
7826           return DW_FORM_data4;
7827         default:
7828           gcc_unreachable ();
7829         }
7830
7831     default:
7832       gcc_unreachable ();
7833     }
7834 }
7835
7836 /* Output the encoding of an attribute value.  */
7837
7838 static void
7839 output_value_format (dw_attr_ref a)
7840 {
7841   enum dwarf_form form = value_format (a);
7842
7843   dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
7844 }
7845
7846 /* Output the .debug_abbrev section which defines the DIE abbreviation
7847    table.  */
7848
7849 static void
7850 output_abbrev_section (void)
7851 {
7852   unsigned long abbrev_id;
7853
7854   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7855     {
7856       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7857       unsigned ix;
7858       dw_attr_ref a_attr;
7859
7860       dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
7861       dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
7862                                    dwarf_tag_name (abbrev->die_tag));
7863
7864       if (abbrev->die_child != NULL)
7865         dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
7866       else
7867         dw2_asm_output_data (1, DW_children_no, "DW_children_no");
7868
7869       for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
7870            ix++)
7871         {
7872           dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
7873                                        dwarf_attr_name (a_attr->dw_attr));
7874           output_value_format (a_attr);
7875         }
7876
7877       dw2_asm_output_data (1, 0, NULL);
7878       dw2_asm_output_data (1, 0, NULL);
7879     }
7880
7881   /* Terminate the table.  */
7882   dw2_asm_output_data (1, 0, NULL);
7883 }
7884
7885 /* Output a symbol we can use to refer to this DIE from another CU.  */
7886
7887 static inline void
7888 output_die_symbol (dw_die_ref die)
7889 {
7890   char *sym = die->die_symbol;
7891
7892   if (sym == 0)
7893     return;
7894
7895   if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
7896     /* We make these global, not weak; if the target doesn't support
7897        .linkonce, it doesn't support combining the sections, so debugging
7898        will break.  */
7899     targetm.asm_out.globalize_label (asm_out_file, sym);
7900
7901   ASM_OUTPUT_LABEL (asm_out_file, sym);
7902 }
7903
7904 /* Return a new location list, given the begin and end range, and the
7905    expression. gensym tells us whether to generate a new internal symbol for
7906    this location list node, which is done for the head of the list only.  */
7907
7908 static inline dw_loc_list_ref
7909 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
7910               const char *section, unsigned int gensym)
7911 {
7912   dw_loc_list_ref retlist = GGC_CNEW (dw_loc_list_node);
7913
7914   retlist->begin = begin;
7915   retlist->end = end;
7916   retlist->expr = expr;
7917   retlist->section = section;
7918   if (gensym)
7919     retlist->ll_symbol = gen_internal_sym ("LLST");
7920
7921   return retlist;
7922 }
7923
7924 /* Add a location description expression to a location list.  */
7925
7926 static inline void
7927 add_loc_descr_to_loc_list (dw_loc_list_ref *list_head, dw_loc_descr_ref descr,
7928                            const char *begin, const char *end,
7929                            const char *section)
7930 {
7931   dw_loc_list_ref *d;
7932
7933   /* Find the end of the chain.  */
7934   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
7935     ;
7936
7937   /* Add a new location list node to the list.  */
7938   *d = new_loc_list (descr, begin, end, section, 0);
7939 }
7940
7941 /* Output the location list given to us.  */
7942
7943 static void
7944 output_loc_list (dw_loc_list_ref list_head)
7945 {
7946   dw_loc_list_ref curr = list_head;
7947
7948   ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
7949
7950   /* Walk the location list, and output each range + expression.  */
7951   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
7952     {
7953       unsigned long size;
7954       /* Don't output an entry that starts and ends at the same address.  */
7955       if (strcmp (curr->begin, curr->end) == 0)
7956         continue;
7957       if (!have_multiple_function_sections)
7958         {
7959           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
7960                                 "Location list begin address (%s)",
7961                                 list_head->ll_symbol);
7962           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
7963                                 "Location list end address (%s)",
7964                                 list_head->ll_symbol);
7965         }
7966       else
7967         {
7968           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
7969                                "Location list begin address (%s)",
7970                                list_head->ll_symbol);
7971           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
7972                                "Location list end address (%s)",
7973                                list_head->ll_symbol);
7974         }
7975       size = size_of_locs (curr->expr);
7976
7977       /* Output the block length for this list of location operations.  */
7978       gcc_assert (size <= 0xffff);
7979       dw2_asm_output_data (2, size, "%s", "Location expression size");
7980
7981       output_loc_sequence (curr->expr);
7982     }
7983
7984   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
7985                        "Location list terminator begin (%s)",
7986                        list_head->ll_symbol);
7987   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
7988                        "Location list terminator end (%s)",
7989                        list_head->ll_symbol);
7990 }
7991
7992 /* Output the DIE and its attributes.  Called recursively to generate
7993    the definitions of each child DIE.  */
7994
7995 static void
7996 output_die (dw_die_ref die)
7997 {
7998   dw_attr_ref a;
7999   dw_die_ref c;
8000   unsigned long size;
8001   unsigned ix;
8002
8003   /* If someone in another CU might refer to us, set up a symbol for
8004      them to point to.  */
8005   if (die->die_symbol)
8006     output_die_symbol (die);
8007
8008   dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (0x%lx) %s)",
8009                                (unsigned long)die->die_offset,
8010                                dwarf_tag_name (die->die_tag));
8011
8012   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
8013     {
8014       const char *name = dwarf_attr_name (a->dw_attr);
8015
8016       switch (AT_class (a))
8017         {
8018         case dw_val_class_addr:
8019           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8020           break;
8021
8022         case dw_val_class_offset:
8023           dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8024                                "%s", name);
8025           break;
8026
8027         case dw_val_class_range_list:
8028           {
8029             char *p = strchr (ranges_section_label, '\0');
8030
8031             sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
8032                      a->dw_attr_val.v.val_offset);
8033             dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8034                                    debug_ranges_section, "%s", name);
8035             *p = '\0';
8036           }
8037           break;
8038
8039         case dw_val_class_loc:
8040           size = size_of_locs (AT_loc (a));
8041
8042           /* Output the block length for this list of location operations.  */
8043           dw2_asm_output_data (constant_size (size), size, "%s", name);
8044
8045           output_loc_sequence (AT_loc (a));
8046           break;
8047
8048         case dw_val_class_const:
8049           /* ??? It would be slightly more efficient to use a scheme like is
8050              used for unsigned constants below, but gdb 4.x does not sign
8051              extend.  Gdb 5.x does sign extend.  */
8052           dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8053           break;
8054
8055         case dw_val_class_unsigned_const:
8056           dw2_asm_output_data (constant_size (AT_unsigned (a)),
8057                                AT_unsigned (a), "%s", name);
8058           break;
8059
8060         case dw_val_class_long_long:
8061           {
8062             unsigned HOST_WIDE_INT first, second;
8063
8064             dw2_asm_output_data (1,
8065                                  2 * HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
8066                                  "%s", name);
8067
8068             if (WORDS_BIG_ENDIAN)
8069               {
8070                 first = a->dw_attr_val.v.val_long_long.hi;
8071                 second = a->dw_attr_val.v.val_long_long.low;
8072               }
8073             else
8074               {
8075                 first = a->dw_attr_val.v.val_long_long.low;
8076                 second = a->dw_attr_val.v.val_long_long.hi;
8077               }
8078
8079             dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
8080                                  first, "long long constant");
8081             dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
8082                                  second, NULL);
8083           }
8084           break;
8085
8086         case dw_val_class_vec:
8087           {
8088             unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
8089             unsigned int len = a->dw_attr_val.v.val_vec.length;
8090             unsigned int i;
8091             unsigned char *p;
8092
8093             dw2_asm_output_data (constant_size (len * elt_size),
8094                                  len * elt_size, "%s", name);
8095             if (elt_size > sizeof (HOST_WIDE_INT))
8096               {
8097                 elt_size /= 2;
8098                 len *= 2;
8099               }
8100             for (i = 0, p = a->dw_attr_val.v.val_vec.array;
8101                  i < len;
8102                  i++, p += elt_size)
8103               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
8104                                    "fp or vector constant word %u", i);
8105             break;
8106           }
8107
8108         case dw_val_class_flag:
8109           dw2_asm_output_data (1, AT_flag (a), "%s", name);
8110           break;
8111
8112         case dw_val_class_loc_list:
8113           {
8114             char *sym = AT_loc_list (a)->ll_symbol;
8115
8116             gcc_assert (sym);
8117             dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8118                                    "%s", name);
8119           }
8120           break;
8121
8122         case dw_val_class_die_ref:
8123           if (AT_ref_external (a))
8124             {
8125               char *sym = AT_ref (a)->die_symbol;
8126
8127               gcc_assert (sym);
8128               dw2_asm_output_offset (DWARF2_ADDR_SIZE, sym, debug_info_section,
8129                                      "%s", name);
8130             }
8131           else
8132             {
8133               gcc_assert (AT_ref (a)->die_offset);
8134               dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
8135                                    "%s", name);
8136             }
8137           break;
8138
8139         case dw_val_class_fde_ref:
8140           {
8141             char l1[20];
8142
8143             ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
8144                                          a->dw_attr_val.v.val_fde_index * 2);
8145             dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
8146                                    "%s", name);
8147           }
8148           break;
8149
8150         case dw_val_class_lbl_id:
8151           dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8152           break;
8153
8154         case dw_val_class_lineptr:
8155           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8156                                  debug_line_section, "%s", name);
8157           break;
8158
8159         case dw_val_class_macptr:
8160           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8161                                  debug_macinfo_section, "%s", name);
8162           break;
8163
8164         case dw_val_class_str:
8165           if (AT_string_form (a) == DW_FORM_strp)
8166             dw2_asm_output_offset (DWARF_OFFSET_SIZE,
8167                                    a->dw_attr_val.v.val_str->label,
8168                                    debug_str_section,
8169                                    "%s: \"%s\"", name, AT_string (a));
8170           else
8171             dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
8172           break;
8173
8174         case dw_val_class_file:
8175           {
8176             int f = maybe_emit_file (a->dw_attr_val.v.val_file);
8177
8178             dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
8179                                  a->dw_attr_val.v.val_file->filename);
8180             break;
8181           }
8182
8183         default:
8184           gcc_unreachable ();
8185         }
8186     }
8187
8188   FOR_EACH_CHILD (die, c, output_die (c));
8189
8190   /* Add null byte to terminate sibling list.  */
8191   if (die->die_child != NULL)
8192     dw2_asm_output_data (1, 0, "end of children of DIE 0x%lx",
8193                          (unsigned long) die->die_offset);
8194 }
8195
8196 /* Output the compilation unit that appears at the beginning of the
8197    .debug_info section, and precedes the DIE descriptions.  */
8198
8199 static void
8200 output_compilation_unit_header (void)
8201 {
8202   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8203     dw2_asm_output_data (4, 0xffffffff,
8204       "Initial length escape value indicating 64-bit DWARF extension");
8205   dw2_asm_output_data (DWARF_OFFSET_SIZE,
8206                        next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
8207                        "Length of Compilation Unit Info");
8208   dw2_asm_output_data (2, DWARF_VERSION, "DWARF version number");
8209   dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
8210                          debug_abbrev_section,
8211                          "Offset Into Abbrev. Section");
8212   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8213 }
8214
8215 /* Output the compilation unit DIE and its children.  */
8216
8217 static void
8218 output_comp_unit (dw_die_ref die, int output_if_empty)
8219 {
8220   const char *secname;
8221   char *oldsym, *tmp;
8222
8223   /* Unless we are outputting main CU, we may throw away empty ones.  */
8224   if (!output_if_empty && die->die_child == NULL)
8225     return;
8226
8227   /* Even if there are no children of this DIE, we must output the information
8228      about the compilation unit.  Otherwise, on an empty translation unit, we
8229      will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
8230      will then complain when examining the file.  First mark all the DIEs in
8231      this CU so we know which get local refs.  */
8232   mark_dies (die);
8233
8234   build_abbrev_table (die);
8235
8236   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
8237   next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8238   calc_die_sizes (die);
8239
8240   oldsym = die->die_symbol;
8241   if (oldsym)
8242     {
8243       tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
8244
8245       sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
8246       secname = tmp;
8247       die->die_symbol = NULL;
8248       switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
8249     }
8250   else
8251     switch_to_section (debug_info_section);
8252
8253   /* Output debugging information.  */
8254   output_compilation_unit_header ();
8255   output_die (die);
8256
8257   /* Leave the marks on the main CU, so we can check them in
8258      output_pubnames.  */
8259   if (oldsym)
8260     {
8261       unmark_dies (die);
8262       die->die_symbol = oldsym;
8263     }
8264 }
8265
8266 /* Return the DWARF2/3 pubname associated with a decl.  */
8267
8268 static const char *
8269 dwarf2_name (tree decl, int scope)
8270 {
8271   return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
8272 }
8273
8274 /* Add a new entry to .debug_pubnames if appropriate.  */
8275
8276 static void
8277 add_pubname_string (const char *str, dw_die_ref die)
8278 {
8279   pubname_entry e;
8280
8281   e.die = die;
8282   e.name = xstrdup (str);
8283   VEC_safe_push (pubname_entry, gc, pubname_table, &e);
8284 }
8285
8286 static void
8287 add_pubname (tree decl, dw_die_ref die)
8288 {
8289
8290   if (TREE_PUBLIC (decl))
8291     add_pubname_string (dwarf2_name (decl, 1), die);
8292 }
8293
8294 /* Add a new entry to .debug_pubtypes if appropriate.  */
8295
8296 static void
8297 add_pubtype (tree decl, dw_die_ref die)
8298 {
8299   pubname_entry e;
8300
8301   e.name = NULL;
8302   if ((TREE_PUBLIC (decl)
8303        || die->die_parent == comp_unit_die)
8304       && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
8305     {
8306       e.die = die;
8307       if (TYPE_P (decl))
8308         {
8309           if (TYPE_NAME (decl))
8310             {
8311               if (TREE_CODE (TYPE_NAME (decl)) == IDENTIFIER_NODE)
8312                 e.name = IDENTIFIER_POINTER (TYPE_NAME (decl));
8313               else if (TREE_CODE (TYPE_NAME (decl)) == TYPE_DECL
8314                        && DECL_NAME (TYPE_NAME (decl)))
8315                 e.name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (decl)));
8316               else
8317                e.name = xstrdup ((const char *) get_AT_string (die, DW_AT_name));
8318             }
8319         }
8320       else
8321         e.name = xstrdup (dwarf2_name (decl, 1));
8322
8323       /* If we don't have a name for the type, there's no point in adding
8324          it to the table.  */
8325       if (e.name && e.name[0] != '\0')
8326         VEC_safe_push (pubname_entry, gc, pubtype_table, &e);
8327     }
8328 }
8329
8330 /* Output the public names table used to speed up access to externally
8331    visible names; or the public types table used to find type definitions.  */
8332
8333 static void
8334 output_pubnames (VEC (pubname_entry, gc) * names)
8335 {
8336   unsigned i;
8337   unsigned long pubnames_length = size_of_pubnames (names);
8338   pubname_ref pub;
8339
8340   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8341     dw2_asm_output_data (4, 0xffffffff,
8342       "Initial length escape value indicating 64-bit DWARF extension");
8343   if (names == pubname_table)
8344     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
8345                          "Length of Public Names Info");
8346   else
8347     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
8348                          "Length of Public Type Names Info");
8349   dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
8350   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
8351                          debug_info_section,
8352                          "Offset of Compilation Unit Info");
8353   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
8354                        "Compilation Unit Length");
8355
8356   for (i = 0; VEC_iterate (pubname_entry, names, i, pub); i++)
8357     {
8358       /* We shouldn't see pubnames for DIEs outside of the main CU.  */
8359       if (names == pubname_table)
8360         gcc_assert (pub->die->die_mark);
8361
8362       if (names != pubtype_table
8363           || pub->die->die_offset != 0
8364           || !flag_eliminate_unused_debug_types)
8365         {
8366           dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
8367                                "DIE offset");
8368
8369           dw2_asm_output_nstring (pub->name, -1, "external name");
8370         }
8371     }
8372
8373   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
8374 }
8375
8376 /* Add a new entry to .debug_aranges if appropriate.  */
8377
8378 static void
8379 add_arange (tree decl, dw_die_ref die)
8380 {
8381   if (! DECL_SECTION_NAME (decl))
8382     return;
8383
8384   if (arange_table_in_use == arange_table_allocated)
8385     {
8386       arange_table_allocated += ARANGE_TABLE_INCREMENT;
8387       arange_table = GGC_RESIZEVEC (dw_die_ref, arange_table,
8388                                     arange_table_allocated);
8389       memset (arange_table + arange_table_in_use, 0,
8390               ARANGE_TABLE_INCREMENT * sizeof (dw_die_ref));
8391     }
8392
8393   arange_table[arange_table_in_use++] = die;
8394 }
8395
8396 /* Output the information that goes into the .debug_aranges table.
8397    Namely, define the beginning and ending address range of the
8398    text section generated for this compilation unit.  */
8399
8400 static void
8401 output_aranges (void)
8402 {
8403   unsigned i;
8404   unsigned long aranges_length = size_of_aranges ();
8405
8406   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8407     dw2_asm_output_data (4, 0xffffffff,
8408       "Initial length escape value indicating 64-bit DWARF extension");
8409   dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
8410                        "Length of Address Ranges Info");
8411   dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
8412   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
8413                          debug_info_section,
8414                          "Offset of Compilation Unit Info");
8415   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
8416   dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
8417
8418   /* We need to align to twice the pointer size here.  */
8419   if (DWARF_ARANGES_PAD_SIZE)
8420     {
8421       /* Pad using a 2 byte words so that padding is correct for any
8422          pointer size.  */
8423       dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
8424                            2 * DWARF2_ADDR_SIZE);
8425       for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
8426         dw2_asm_output_data (2, 0, NULL);
8427     }
8428
8429   /* It is necessary not to output these entries if the sections were
8430      not used; if the sections were not used, the length will be 0 and
8431      the address may end up as 0 if the section is discarded by ld
8432      --gc-sections, leaving an invalid (0, 0) entry that can be
8433      confused with the terminator.  */
8434   if (text_section_used)
8435     {
8436       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
8437       dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
8438                             text_section_label, "Length");
8439     }
8440   if (cold_text_section_used)
8441     {
8442       dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
8443                            "Address");
8444       dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
8445                             cold_text_section_label, "Length");
8446     }
8447
8448   for (i = 0; i < arange_table_in_use; i++)
8449     {
8450       dw_die_ref die = arange_table[i];
8451
8452       /* We shouldn't see aranges for DIEs outside of the main CU.  */
8453       gcc_assert (die->die_mark);
8454
8455       if (die->die_tag == DW_TAG_subprogram)
8456         {
8457           dw2_asm_output_addr (DWARF2_ADDR_SIZE, get_AT_low_pc (die),
8458                                "Address");
8459           dw2_asm_output_delta (DWARF2_ADDR_SIZE, get_AT_hi_pc (die),
8460                                 get_AT_low_pc (die), "Length");
8461         }
8462       else
8463         {
8464           /* A static variable; extract the symbol from DW_AT_location.
8465              Note that this code isn't currently hit, as we only emit
8466              aranges for functions (jason 9/23/99).  */
8467           dw_attr_ref a = get_AT (die, DW_AT_location);
8468           dw_loc_descr_ref loc;
8469
8470           gcc_assert (a && AT_class (a) == dw_val_class_loc);
8471
8472           loc = AT_loc (a);
8473           gcc_assert (loc->dw_loc_opc == DW_OP_addr);
8474
8475           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE,
8476                                    loc->dw_loc_oprnd1.v.val_addr, "Address");
8477           dw2_asm_output_data (DWARF2_ADDR_SIZE,
8478                                get_AT_unsigned (die, DW_AT_byte_size),
8479                                "Length");
8480         }
8481     }
8482
8483   /* Output the terminator words.  */
8484   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8485   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8486 }
8487
8488 /* Add a new entry to .debug_ranges.  Return the offset at which it
8489    was placed.  */
8490
8491 static unsigned int
8492 add_ranges_num (int num)
8493 {
8494   unsigned int in_use = ranges_table_in_use;
8495
8496   if (in_use == ranges_table_allocated)
8497     {
8498       ranges_table_allocated += RANGES_TABLE_INCREMENT;
8499       ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
8500                                     ranges_table_allocated);
8501       memset (ranges_table + ranges_table_in_use, 0,
8502               RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
8503     }
8504
8505   ranges_table[in_use].num = num;
8506   ranges_table_in_use = in_use + 1;
8507
8508   return in_use * 2 * DWARF2_ADDR_SIZE;
8509 }
8510
8511 /* Add a new entry to .debug_ranges corresponding to a block, or a
8512    range terminator if BLOCK is NULL.  */
8513
8514 static unsigned int
8515 add_ranges (const_tree block)
8516 {
8517   return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
8518 }
8519
8520 /* Add a new entry to .debug_ranges corresponding to a pair of
8521    labels.  */
8522
8523 static unsigned int
8524 add_ranges_by_labels (const char *begin, const char *end)
8525 {
8526   unsigned int in_use = ranges_by_label_in_use;
8527
8528   if (in_use == ranges_by_label_allocated)
8529     {
8530       ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
8531       ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
8532                                        ranges_by_label,
8533                                        ranges_by_label_allocated);
8534       memset (ranges_by_label + ranges_by_label_in_use, 0,
8535               RANGES_TABLE_INCREMENT
8536               * sizeof (struct dw_ranges_by_label_struct));
8537     }
8538
8539   ranges_by_label[in_use].begin = begin;
8540   ranges_by_label[in_use].end = end;
8541   ranges_by_label_in_use = in_use + 1;
8542
8543   return add_ranges_num (-(int)in_use - 1);
8544 }
8545
8546 static void
8547 output_ranges (void)
8548 {
8549   unsigned i;
8550   static const char *const start_fmt = "Offset 0x%x";
8551   const char *fmt = start_fmt;
8552
8553   for (i = 0; i < ranges_table_in_use; i++)
8554     {
8555       int block_num = ranges_table[i].num;
8556
8557       if (block_num > 0)
8558         {
8559           char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
8560           char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
8561
8562           ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
8563           ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
8564
8565           /* If all code is in the text section, then the compilation
8566              unit base address defaults to DW_AT_low_pc, which is the
8567              base of the text section.  */
8568           if (!have_multiple_function_sections)
8569             {
8570               dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
8571                                     text_section_label,
8572                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
8573               dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
8574                                     text_section_label, NULL);
8575             }
8576
8577           /* Otherwise, the compilation unit base address is zero,
8578              which allows us to use absolute addresses, and not worry
8579              about whether the target supports cross-section
8580              arithmetic.  */
8581           else
8582             {
8583               dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
8584                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
8585               dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
8586             }
8587
8588           fmt = NULL;
8589         }
8590
8591       /* Negative block_num stands for an index into ranges_by_label.  */
8592       else if (block_num < 0)
8593         {
8594           int lab_idx = - block_num - 1;
8595
8596           if (!have_multiple_function_sections)
8597             {
8598               gcc_unreachable ();
8599 #if 0
8600               /* If we ever use add_ranges_by_labels () for a single
8601                  function section, all we have to do is to take out
8602                  the #if 0 above.  */
8603               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
8604                                     ranges_by_label[lab_idx].begin,
8605                                     text_section_label,
8606                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
8607               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
8608                                     ranges_by_label[lab_idx].end,
8609                                     text_section_label, NULL);
8610 #endif
8611             }
8612           else
8613             {
8614               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
8615                                    ranges_by_label[lab_idx].begin,
8616                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
8617               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
8618                                    ranges_by_label[lab_idx].end,
8619                                    NULL);
8620             }
8621         }
8622       else
8623         {
8624           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8625           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8626           fmt = start_fmt;
8627         }
8628     }
8629 }
8630
8631 /* Data structure containing information about input files.  */
8632 struct file_info
8633 {
8634   const char *path;     /* Complete file name.  */
8635   const char *fname;    /* File name part.  */
8636   int length;           /* Length of entire string.  */
8637   struct dwarf_file_data * file_idx;    /* Index in input file table.  */
8638   int dir_idx;          /* Index in directory table.  */
8639 };
8640
8641 /* Data structure containing information about directories with source
8642    files.  */
8643 struct dir_info
8644 {
8645   const char *path;     /* Path including directory name.  */
8646   int length;           /* Path length.  */
8647   int prefix;           /* Index of directory entry which is a prefix.  */
8648   int count;            /* Number of files in this directory.  */
8649   int dir_idx;          /* Index of directory used as base.  */
8650 };
8651
8652 /* Callback function for file_info comparison.  We sort by looking at
8653    the directories in the path.  */
8654
8655 static int
8656 file_info_cmp (const void *p1, const void *p2)
8657 {
8658   const struct file_info *const s1 = (const struct file_info *) p1;
8659   const struct file_info *const s2 = (const struct file_info *) p2;
8660   const unsigned char *cp1;
8661   const unsigned char *cp2;
8662
8663   /* Take care of file names without directories.  We need to make sure that
8664      we return consistent values to qsort since some will get confused if
8665      we return the same value when identical operands are passed in opposite
8666      orders.  So if neither has a directory, return 0 and otherwise return
8667      1 or -1 depending on which one has the directory.  */
8668   if ((s1->path == s1->fname || s2->path == s2->fname))
8669     return (s2->path == s2->fname) - (s1->path == s1->fname);
8670
8671   cp1 = (const unsigned char *) s1->path;
8672   cp2 = (const unsigned char *) s2->path;
8673
8674   while (1)
8675     {
8676       ++cp1;
8677       ++cp2;
8678       /* Reached the end of the first path?  If so, handle like above.  */
8679       if ((cp1 == (const unsigned char *) s1->fname)
8680           || (cp2 == (const unsigned char *) s2->fname))
8681         return ((cp2 == (const unsigned char *) s2->fname)
8682                 - (cp1 == (const unsigned char *) s1->fname));
8683
8684       /* Character of current path component the same?  */
8685       else if (*cp1 != *cp2)
8686         return *cp1 - *cp2;
8687     }
8688 }
8689
8690 struct file_name_acquire_data
8691 {
8692   struct file_info *files;
8693   int used_files;
8694   int max_files;
8695 };
8696
8697 /* Traversal function for the hash table.  */
8698
8699 static int
8700 file_name_acquire (void ** slot, void *data)
8701 {
8702   struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
8703   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
8704   struct file_info *fi;
8705   const char *f;
8706
8707   gcc_assert (fnad->max_files >= d->emitted_number);
8708
8709   if (! d->emitted_number)
8710     return 1;
8711
8712   gcc_assert (fnad->max_files != fnad->used_files);
8713
8714   fi = fnad->files + fnad->used_files++;
8715
8716   /* Skip all leading "./".  */
8717   f = d->filename;
8718   while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
8719     f += 2;
8720
8721   /* Create a new array entry.  */
8722   fi->path = f;
8723   fi->length = strlen (f);
8724   fi->file_idx = d;
8725
8726   /* Search for the file name part.  */
8727   f = strrchr (f, DIR_SEPARATOR);
8728 #if defined (DIR_SEPARATOR_2)
8729   {
8730     char *g = strrchr (fi->path, DIR_SEPARATOR_2);
8731
8732     if (g != NULL)
8733       {
8734         if (f == NULL || f < g)
8735           f = g;
8736       }
8737   }
8738 #endif
8739
8740   fi->fname = f == NULL ? fi->path : f + 1;
8741   return 1;
8742 }
8743
8744 /* Output the directory table and the file name table.  We try to minimize
8745    the total amount of memory needed.  A heuristic is used to avoid large
8746    slowdowns with many input files.  */
8747
8748 static void
8749 output_file_names (void)
8750 {
8751   struct file_name_acquire_data fnad;
8752   int numfiles;
8753   struct file_info *files;
8754   struct dir_info *dirs;
8755   int *saved;
8756   int *savehere;
8757   int *backmap;
8758   int ndirs;
8759   int idx_offset;
8760   int i;
8761   int idx;
8762
8763   if (!last_emitted_file)
8764     {
8765       dw2_asm_output_data (1, 0, "End directory table");
8766       dw2_asm_output_data (1, 0, "End file name table");
8767       return;
8768     }
8769
8770   numfiles = last_emitted_file->emitted_number;
8771
8772   /* Allocate the various arrays we need.  */
8773   files = XALLOCAVEC (struct file_info, numfiles);
8774   dirs = XALLOCAVEC (struct dir_info, numfiles);
8775
8776   fnad.files = files;
8777   fnad.used_files = 0;
8778   fnad.max_files = numfiles;
8779   htab_traverse (file_table, file_name_acquire, &fnad);
8780   gcc_assert (fnad.used_files == fnad.max_files);
8781
8782   qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
8783
8784   /* Find all the different directories used.  */
8785   dirs[0].path = files[0].path;
8786   dirs[0].length = files[0].fname - files[0].path;
8787   dirs[0].prefix = -1;
8788   dirs[0].count = 1;
8789   dirs[0].dir_idx = 0;
8790   files[0].dir_idx = 0;
8791   ndirs = 1;
8792
8793   for (i = 1; i < numfiles; i++)
8794     if (files[i].fname - files[i].path == dirs[ndirs - 1].length
8795         && memcmp (dirs[ndirs - 1].path, files[i].path,
8796                    dirs[ndirs - 1].length) == 0)
8797       {
8798         /* Same directory as last entry.  */
8799         files[i].dir_idx = ndirs - 1;
8800         ++dirs[ndirs - 1].count;
8801       }
8802     else
8803       {
8804         int j;
8805
8806         /* This is a new directory.  */
8807         dirs[ndirs].path = files[i].path;
8808         dirs[ndirs].length = files[i].fname - files[i].path;
8809         dirs[ndirs].count = 1;
8810         dirs[ndirs].dir_idx = ndirs;
8811         files[i].dir_idx = ndirs;
8812
8813         /* Search for a prefix.  */
8814         dirs[ndirs].prefix = -1;
8815         for (j = 0; j < ndirs; j++)
8816           if (dirs[j].length < dirs[ndirs].length
8817               && dirs[j].length > 1
8818               && (dirs[ndirs].prefix == -1
8819                   || dirs[j].length > dirs[dirs[ndirs].prefix].length)
8820               && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
8821             dirs[ndirs].prefix = j;
8822
8823         ++ndirs;
8824       }
8825
8826   /* Now to the actual work.  We have to find a subset of the directories which
8827      allow expressing the file name using references to the directory table
8828      with the least amount of characters.  We do not do an exhaustive search
8829      where we would have to check out every combination of every single
8830      possible prefix.  Instead we use a heuristic which provides nearly optimal
8831      results in most cases and never is much off.  */
8832   saved = XALLOCAVEC (int, ndirs);
8833   savehere = XALLOCAVEC (int, ndirs);
8834
8835   memset (saved, '\0', ndirs * sizeof (saved[0]));
8836   for (i = 0; i < ndirs; i++)
8837     {
8838       int j;
8839       int total;
8840
8841       /* We can always save some space for the current directory.  But this
8842          does not mean it will be enough to justify adding the directory.  */
8843       savehere[i] = dirs[i].length;
8844       total = (savehere[i] - saved[i]) * dirs[i].count;
8845
8846       for (j = i + 1; j < ndirs; j++)
8847         {
8848           savehere[j] = 0;
8849           if (saved[j] < dirs[i].length)
8850             {
8851               /* Determine whether the dirs[i] path is a prefix of the
8852                  dirs[j] path.  */
8853               int k;
8854
8855               k = dirs[j].prefix;
8856               while (k != -1 && k != (int) i)
8857                 k = dirs[k].prefix;
8858
8859               if (k == (int) i)
8860                 {
8861                   /* Yes it is.  We can possibly save some memory by
8862                      writing the filenames in dirs[j] relative to
8863                      dirs[i].  */
8864                   savehere[j] = dirs[i].length;
8865                   total += (savehere[j] - saved[j]) * dirs[j].count;
8866                 }
8867             }
8868         }
8869
8870       /* Check whether we can save enough to justify adding the dirs[i]
8871          directory.  */
8872       if (total > dirs[i].length + 1)
8873         {
8874           /* It's worthwhile adding.  */
8875           for (j = i; j < ndirs; j++)
8876             if (savehere[j] > 0)
8877               {
8878                 /* Remember how much we saved for this directory so far.  */
8879                 saved[j] = savehere[j];
8880
8881                 /* Remember the prefix directory.  */
8882                 dirs[j].dir_idx = i;
8883               }
8884         }
8885     }
8886
8887   /* Emit the directory name table.  */
8888   idx = 1;
8889   idx_offset = dirs[0].length > 0 ? 1 : 0;
8890   for (i = 1 - idx_offset; i < ndirs; i++)
8891     dw2_asm_output_nstring (dirs[i].path, dirs[i].length - 1,
8892                             "Directory Entry: 0x%x", i + idx_offset);
8893
8894   dw2_asm_output_data (1, 0, "End directory table");
8895
8896   /* We have to emit them in the order of emitted_number since that's
8897      used in the debug info generation.  To do this efficiently we
8898      generate a back-mapping of the indices first.  */
8899   backmap = XALLOCAVEC (int, numfiles);
8900   for (i = 0; i < numfiles; i++)
8901     backmap[files[i].file_idx->emitted_number - 1] = i;
8902
8903   /* Now write all the file names.  */
8904   for (i = 0; i < numfiles; i++)
8905     {
8906       int file_idx = backmap[i];
8907       int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
8908
8909       dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
8910                               "File Entry: 0x%x", (unsigned) i + 1);
8911
8912       /* Include directory index.  */
8913       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
8914
8915       /* Modification time.  */
8916       dw2_asm_output_data_uleb128 (0, NULL);
8917
8918       /* File length in bytes.  */
8919       dw2_asm_output_data_uleb128 (0, NULL);
8920     }
8921
8922   dw2_asm_output_data (1, 0, "End file name table");
8923 }
8924
8925
8926 /* Output the source line number correspondence information.  This
8927    information goes into the .debug_line section.  */
8928
8929 static void
8930 output_line_info (void)
8931 {
8932   char l1[20], l2[20], p1[20], p2[20];
8933   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
8934   char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
8935   unsigned opc;
8936   unsigned n_op_args;
8937   unsigned long lt_index;
8938   unsigned long current_line;
8939   long line_offset;
8940   long line_delta;
8941   unsigned long current_file;
8942   unsigned long function;
8943
8944   ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
8945   ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
8946   ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
8947   ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
8948
8949   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8950     dw2_asm_output_data (4, 0xffffffff,
8951       "Initial length escape value indicating 64-bit DWARF extension");
8952   dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
8953                         "Length of Source Line Info");
8954   ASM_OUTPUT_LABEL (asm_out_file, l1);
8955
8956   dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
8957   dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
8958   ASM_OUTPUT_LABEL (asm_out_file, p1);
8959
8960   /* Define the architecture-dependent minimum instruction length (in
8961    bytes).  In this implementation of DWARF, this field is used for
8962    information purposes only.  Since GCC generates assembly language,
8963    we have no a priori knowledge of how many instruction bytes are
8964    generated for each source line, and therefore can use only the
8965    DW_LNE_set_address and DW_LNS_fixed_advance_pc line information
8966    commands.  Accordingly, we fix this as `1', which is "correct
8967    enough" for all architectures, and don't let the target override.  */
8968   dw2_asm_output_data (1, 1,
8969                        "Minimum Instruction Length");
8970
8971   dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
8972                        "Default is_stmt_start flag");
8973   dw2_asm_output_data (1, DWARF_LINE_BASE,
8974                        "Line Base Value (Special Opcodes)");
8975   dw2_asm_output_data (1, DWARF_LINE_RANGE,
8976                        "Line Range Value (Special Opcodes)");
8977   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
8978                        "Special Opcode Base");
8979
8980   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
8981     {
8982       switch (opc)
8983         {
8984         case DW_LNS_advance_pc:
8985         case DW_LNS_advance_line:
8986         case DW_LNS_set_file:
8987         case DW_LNS_set_column:
8988         case DW_LNS_fixed_advance_pc:
8989           n_op_args = 1;
8990           break;
8991         default:
8992           n_op_args = 0;
8993           break;
8994         }
8995
8996       dw2_asm_output_data (1, n_op_args, "opcode: 0x%x has %d args",
8997                            opc, n_op_args);
8998     }
8999
9000   /* Write out the information about the files we use.  */
9001   output_file_names ();
9002   ASM_OUTPUT_LABEL (asm_out_file, p2);
9003
9004   /* We used to set the address register to the first location in the text
9005      section here, but that didn't accomplish anything since we already
9006      have a line note for the opening brace of the first function.  */
9007
9008   /* Generate the line number to PC correspondence table, encoded as
9009      a series of state machine operations.  */
9010   current_file = 1;
9011   current_line = 1;
9012
9013   if (cfun && in_cold_section_p)
9014     strcpy (prev_line_label, crtl->subsections.cold_section_label);
9015   else
9016     strcpy (prev_line_label, text_section_label);
9017   for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
9018     {
9019       dw_line_info_ref line_info = &line_info_table[lt_index];
9020
9021 #if 0
9022       /* Disable this optimization for now; GDB wants to see two line notes
9023          at the beginning of a function so it can find the end of the
9024          prologue.  */
9025
9026       /* Don't emit anything for redundant notes.  Just updating the
9027          address doesn't accomplish anything, because we already assume
9028          that anything after the last address is this line.  */
9029       if (line_info->dw_line_num == current_line
9030           && line_info->dw_file_num == current_file)
9031         continue;
9032 #endif
9033
9034       /* Emit debug info for the address of the current line.
9035
9036          Unfortunately, we have little choice here currently, and must always
9037          use the most general form.  GCC does not know the address delta
9038          itself, so we can't use DW_LNS_advance_pc.  Many ports do have length
9039          attributes which will give an upper bound on the address range.  We
9040          could perhaps use length attributes to determine when it is safe to
9041          use DW_LNS_fixed_advance_pc.  */
9042
9043       ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
9044       if (0)
9045         {
9046           /* This can handle deltas up to 0xffff.  This takes 3 bytes.  */
9047           dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
9048                                "DW_LNS_fixed_advance_pc");
9049           dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
9050         }
9051       else
9052         {
9053           /* This can handle any delta.  This takes
9054              4+DWARF2_ADDR_SIZE bytes.  */
9055           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9056           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9057           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9058           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9059         }
9060
9061       strcpy (prev_line_label, line_label);
9062
9063       /* Emit debug info for the source file of the current line, if
9064          different from the previous line.  */
9065       if (line_info->dw_file_num != current_file)
9066         {
9067           current_file = line_info->dw_file_num;
9068           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
9069           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
9070         }
9071
9072       /* Emit debug info for the current line number, choosing the encoding
9073          that uses the least amount of space.  */
9074       if (line_info->dw_line_num != current_line)
9075         {
9076           line_offset = line_info->dw_line_num - current_line;
9077           line_delta = line_offset - DWARF_LINE_BASE;
9078           current_line = line_info->dw_line_num;
9079           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
9080             /* This can handle deltas from -10 to 234, using the current
9081                definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.  This
9082                takes 1 byte.  */
9083             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
9084                                  "line %lu", current_line);
9085           else
9086             {
9087               /* This can handle any delta.  This takes at least 4 bytes,
9088                  depending on the value being encoded.  */
9089               dw2_asm_output_data (1, DW_LNS_advance_line,
9090                                    "advance to line %lu", current_line);
9091               dw2_asm_output_data_sleb128 (line_offset, NULL);
9092               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
9093             }
9094         }
9095       else
9096         /* We still need to start a new row, so output a copy insn.  */
9097         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
9098     }
9099
9100   /* Emit debug info for the address of the end of the function.  */
9101   if (0)
9102     {
9103       dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
9104                            "DW_LNS_fixed_advance_pc");
9105       dw2_asm_output_delta (2, text_end_label, prev_line_label, NULL);
9106     }
9107   else
9108     {
9109       dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9110       dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9111       dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9112       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_end_label, NULL);
9113     }
9114
9115   dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
9116   dw2_asm_output_data_uleb128 (1, NULL);
9117   dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
9118
9119   function = 0;
9120   current_file = 1;
9121   current_line = 1;
9122   for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
9123     {
9124       dw_separate_line_info_ref line_info
9125         = &separate_line_info_table[lt_index];
9126
9127 #if 0
9128       /* Don't emit anything for redundant notes.  */
9129       if (line_info->dw_line_num == current_line
9130           && line_info->dw_file_num == current_file
9131           && line_info->function == function)
9132         goto cont;
9133 #endif
9134
9135       /* Emit debug info for the address of the current line.  If this is
9136          a new function, or the first line of a function, then we need
9137          to handle it differently.  */
9138       ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
9139                                    lt_index);
9140       if (function != line_info->function)
9141         {
9142           function = line_info->function;
9143
9144           /* Set the address register to the first line in the function.  */
9145           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9146           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9147           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9148           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9149         }
9150       else
9151         {
9152           /* ??? See the DW_LNS_advance_pc comment above.  */
9153           if (0)
9154             {
9155               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
9156                                    "DW_LNS_fixed_advance_pc");
9157               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
9158             }
9159           else
9160             {
9161               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9162               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9163               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9164               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9165             }
9166         }
9167
9168       strcpy (prev_line_label, line_label);
9169
9170       /* Emit debug info for the source file of the current line, if
9171          different from the previous line.  */
9172       if (line_info->dw_file_num != current_file)
9173         {
9174           current_file = line_info->dw_file_num;
9175           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
9176           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
9177         }
9178
9179       /* Emit debug info for the current line number, choosing the encoding
9180          that uses the least amount of space.  */
9181       if (line_info->dw_line_num != current_line)
9182         {
9183           line_offset = line_info->dw_line_num - current_line;
9184           line_delta = line_offset - DWARF_LINE_BASE;
9185           current_line = line_info->dw_line_num;
9186           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
9187             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
9188                                  "line %lu", current_line);
9189           else
9190             {
9191               dw2_asm_output_data (1, DW_LNS_advance_line,
9192                                    "advance to line %lu", current_line);
9193               dw2_asm_output_data_sleb128 (line_offset, NULL);
9194               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
9195             }
9196         }
9197       else
9198         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
9199
9200 #if 0
9201     cont:
9202 #endif
9203
9204       lt_index++;
9205
9206       /* If we're done with a function, end its sequence.  */
9207       if (lt_index == separate_line_info_table_in_use
9208           || separate_line_info_table[lt_index].function != function)
9209         {
9210           current_file = 1;
9211           current_line = 1;
9212
9213           /* Emit debug info for the address of the end of the function.  */
9214           ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
9215           if (0)
9216             {
9217               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
9218                                    "DW_LNS_fixed_advance_pc");
9219               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
9220             }
9221           else
9222             {
9223               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9224               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9225               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9226               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9227             }
9228
9229           /* Output the marker for the end of this sequence.  */
9230           dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
9231           dw2_asm_output_data_uleb128 (1, NULL);
9232           dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
9233         }
9234     }
9235
9236   /* Output the marker for the end of the line number info.  */
9237   ASM_OUTPUT_LABEL (asm_out_file, l2);
9238 }
9239 \f
9240 /* Given a pointer to a tree node for some base type, return a pointer to
9241    a DIE that describes the given type.
9242
9243    This routine must only be called for GCC type nodes that correspond to
9244    Dwarf base (fundamental) types.  */
9245
9246 static dw_die_ref
9247 base_type_die (tree type)
9248 {
9249   dw_die_ref base_type_result;
9250   enum dwarf_type encoding;
9251
9252   if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
9253     return 0;
9254
9255   switch (TREE_CODE (type))
9256     {
9257     case INTEGER_TYPE:
9258       if (TYPE_STRING_FLAG (type))
9259         {
9260           if (TYPE_UNSIGNED (type))
9261             encoding = DW_ATE_unsigned_char;
9262           else
9263             encoding = DW_ATE_signed_char;
9264         }
9265       else if (TYPE_UNSIGNED (type))
9266         encoding = DW_ATE_unsigned;
9267       else
9268         encoding = DW_ATE_signed;
9269       break;
9270
9271     case REAL_TYPE:
9272       if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
9273         encoding = DW_ATE_decimal_float;
9274       else
9275         encoding = DW_ATE_float;
9276       break;
9277
9278     case FIXED_POINT_TYPE:
9279       if (TYPE_UNSIGNED (type))
9280         encoding = DW_ATE_unsigned_fixed;
9281       else
9282         encoding = DW_ATE_signed_fixed;
9283       break;
9284
9285       /* Dwarf2 doesn't know anything about complex ints, so use
9286          a user defined type for it.  */
9287     case COMPLEX_TYPE:
9288       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
9289         encoding = DW_ATE_complex_float;
9290       else
9291         encoding = DW_ATE_lo_user;
9292       break;
9293
9294     case BOOLEAN_TYPE:
9295       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
9296       encoding = DW_ATE_boolean;
9297       break;
9298
9299     default:
9300       /* No other TREE_CODEs are Dwarf fundamental types.  */
9301       gcc_unreachable ();
9302     }
9303
9304   base_type_result = new_die (DW_TAG_base_type, comp_unit_die, type);
9305
9306   /* This probably indicates a bug.  */
9307   if (! TYPE_NAME (type))
9308     add_name_attribute (base_type_result, "__unknown__");
9309
9310   add_AT_unsigned (base_type_result, DW_AT_byte_size,
9311                    int_size_in_bytes (type));
9312   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
9313
9314   return base_type_result;
9315 }
9316
9317 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
9318    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
9319
9320 static inline int
9321 is_base_type (tree type)
9322 {
9323   switch (TREE_CODE (type))
9324     {
9325     case ERROR_MARK:
9326     case VOID_TYPE:
9327     case INTEGER_TYPE:
9328     case REAL_TYPE:
9329     case FIXED_POINT_TYPE:
9330     case COMPLEX_TYPE:
9331     case BOOLEAN_TYPE:
9332       return 1;
9333
9334     case ARRAY_TYPE:
9335     case RECORD_TYPE:
9336     case UNION_TYPE:
9337     case QUAL_UNION_TYPE:
9338     case ENUMERAL_TYPE:
9339     case FUNCTION_TYPE:
9340     case METHOD_TYPE:
9341     case POINTER_TYPE:
9342     case REFERENCE_TYPE:
9343     case OFFSET_TYPE:
9344     case LANG_TYPE:
9345     case VECTOR_TYPE:
9346       return 0;
9347
9348     default:
9349       gcc_unreachable ();
9350     }
9351
9352   return 0;
9353 }
9354
9355 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
9356    node, return the size in bits for the type if it is a constant, or else
9357    return the alignment for the type if the type's size is not constant, or
9358    else return BITS_PER_WORD if the type actually turns out to be an
9359    ERROR_MARK node.  */
9360
9361 static inline unsigned HOST_WIDE_INT
9362 simple_type_size_in_bits (const_tree type)
9363 {
9364   if (TREE_CODE (type) == ERROR_MARK)
9365     return BITS_PER_WORD;
9366   else if (TYPE_SIZE (type) == NULL_TREE)
9367     return 0;
9368   else if (host_integerp (TYPE_SIZE (type), 1))
9369     return tree_low_cst (TYPE_SIZE (type), 1);
9370   else
9371     return TYPE_ALIGN (type);
9372 }
9373
9374 /* Return true if the debug information for the given type should be
9375    emitted as a subrange type.  */
9376
9377 static inline bool
9378 is_subrange_type (const_tree type)
9379 {
9380   tree subtype = TREE_TYPE (type);
9381
9382   /* Subrange types are identified by the fact that they are integer
9383      types, and that they have a subtype which is either an integer type
9384      or an enumeral type.  */
9385
9386   if (TREE_CODE (type) != INTEGER_TYPE
9387       || subtype == NULL_TREE)
9388     return false;
9389
9390   if (TREE_CODE (subtype) != INTEGER_TYPE
9391       && TREE_CODE (subtype) != ENUMERAL_TYPE
9392       && TREE_CODE (subtype) != BOOLEAN_TYPE)
9393     return false;
9394
9395   if (TREE_CODE (type) == TREE_CODE (subtype)
9396       && int_size_in_bytes (type) == int_size_in_bytes (subtype)
9397       && TYPE_MIN_VALUE (type) != NULL
9398       && TYPE_MIN_VALUE (subtype) != NULL
9399       && tree_int_cst_equal (TYPE_MIN_VALUE (type), TYPE_MIN_VALUE (subtype))
9400       && TYPE_MAX_VALUE (type) != NULL
9401       && TYPE_MAX_VALUE (subtype) != NULL
9402       && tree_int_cst_equal (TYPE_MAX_VALUE (type), TYPE_MAX_VALUE (subtype)))
9403     {
9404       /* The type and its subtype have the same representation.  If in
9405          addition the two types also have the same name, then the given
9406          type is not a subrange type, but rather a plain base type.  */
9407       /* FIXME: brobecker/2004-03-22:
9408          Sizetype INTEGER_CSTs nodes are canonicalized.  It should
9409          therefore be sufficient to check the TYPE_SIZE node pointers
9410          rather than checking the actual size.  Unfortunately, we have
9411          found some cases, such as in the Ada "integer" type, where
9412          this is not the case.  Until this problem is solved, we need to
9413          keep checking the actual size.  */
9414       tree type_name = TYPE_NAME (type);
9415       tree subtype_name = TYPE_NAME (subtype);
9416
9417       if (type_name != NULL && TREE_CODE (type_name) == TYPE_DECL)
9418         type_name = DECL_NAME (type_name);
9419
9420       if (subtype_name != NULL && TREE_CODE (subtype_name) == TYPE_DECL)
9421         subtype_name = DECL_NAME (subtype_name);
9422
9423       if (type_name == subtype_name)
9424         return false;
9425     }
9426
9427   return true;
9428 }
9429
9430 /*  Given a pointer to a tree node for a subrange type, return a pointer
9431     to a DIE that describes the given type.  */
9432
9433 static dw_die_ref
9434 subrange_type_die (tree type, dw_die_ref context_die)
9435 {
9436   dw_die_ref subrange_die;
9437   const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
9438
9439   if (context_die == NULL)
9440     context_die = comp_unit_die;
9441
9442   subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
9443
9444   if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
9445     {
9446       /* The size of the subrange type and its base type do not match,
9447          so we need to generate a size attribute for the subrange type.  */
9448       add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
9449     }
9450
9451   if (TYPE_MIN_VALUE (type) != NULL)
9452     add_bound_info (subrange_die, DW_AT_lower_bound,
9453                     TYPE_MIN_VALUE (type));
9454   if (TYPE_MAX_VALUE (type) != NULL)
9455     add_bound_info (subrange_die, DW_AT_upper_bound,
9456                     TYPE_MAX_VALUE (type));
9457
9458   return subrange_die;
9459 }
9460
9461 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
9462    entry that chains various modifiers in front of the given type.  */
9463
9464 static dw_die_ref
9465 modified_type_die (tree type, int is_const_type, int is_volatile_type,
9466                    dw_die_ref context_die)
9467 {
9468   enum tree_code code = TREE_CODE (type);
9469   dw_die_ref mod_type_die;
9470   dw_die_ref sub_die = NULL;
9471   tree item_type = NULL;
9472   tree qualified_type;
9473   tree name;
9474
9475   if (code == ERROR_MARK)
9476     return NULL;
9477
9478   /* See if we already have the appropriately qualified variant of
9479      this type.  */
9480   qualified_type
9481     = get_qualified_type (type,
9482                           ((is_const_type ? TYPE_QUAL_CONST : 0)
9483                            | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
9484
9485   /* If we do, then we can just use its DIE, if it exists.  */
9486   if (qualified_type)
9487     {
9488       mod_type_die = lookup_type_die (qualified_type);
9489       if (mod_type_die)
9490         return mod_type_die;
9491     }
9492
9493   name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
9494
9495   /* Handle C typedef types.  */
9496   if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name))
9497     {
9498       tree dtype = TREE_TYPE (name);
9499
9500       if (qualified_type == dtype)
9501         {
9502           /* For a named type, use the typedef.  */
9503           gen_type_die (qualified_type, context_die);
9504           return lookup_type_die (qualified_type);
9505         }
9506       else if (is_const_type < TYPE_READONLY (dtype)
9507                || is_volatile_type < TYPE_VOLATILE (dtype)
9508                || (is_const_type <= TYPE_READONLY (dtype)
9509                    && is_volatile_type <= TYPE_VOLATILE (dtype)
9510                    && DECL_ORIGINAL_TYPE (name) != type))
9511         /* cv-unqualified version of named type.  Just use the unnamed
9512            type to which it refers.  */
9513         return modified_type_die (DECL_ORIGINAL_TYPE (name),
9514                                   is_const_type, is_volatile_type,
9515                                   context_die);
9516       /* Else cv-qualified version of named type; fall through.  */
9517     }
9518
9519   if (is_const_type)
9520     {
9521       mod_type_die = new_die (DW_TAG_const_type, comp_unit_die, type);
9522       sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
9523     }
9524   else if (is_volatile_type)
9525     {
9526       mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die, type);
9527       sub_die = modified_type_die (type, 0, 0, context_die);
9528     }
9529   else if (code == POINTER_TYPE)
9530     {
9531       mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die, type);
9532       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
9533                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
9534       item_type = TREE_TYPE (type);
9535     }
9536   else if (code == REFERENCE_TYPE)
9537     {
9538       mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die, type);
9539       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
9540                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
9541       item_type = TREE_TYPE (type);
9542     }
9543   else if (is_subrange_type (type))
9544     {
9545       mod_type_die = subrange_type_die (type, context_die);
9546       item_type = TREE_TYPE (type);
9547     }
9548   else if (is_base_type (type))
9549     mod_type_die = base_type_die (type);
9550   else
9551     {
9552       gen_type_die (type, context_die);
9553
9554       /* We have to get the type_main_variant here (and pass that to the
9555          `lookup_type_die' routine) because the ..._TYPE node we have
9556          might simply be a *copy* of some original type node (where the
9557          copy was created to help us keep track of typedef names) and
9558          that copy might have a different TYPE_UID from the original
9559          ..._TYPE node.  */
9560       if (TREE_CODE (type) != VECTOR_TYPE)
9561         return lookup_type_die (type_main_variant (type));
9562       else
9563         /* Vectors have the debugging information in the type,
9564            not the main variant.  */
9565         return lookup_type_die (type);
9566     }
9567
9568   /* Builtin types don't have a DECL_ORIGINAL_TYPE.  For those,
9569      don't output a DW_TAG_typedef, since there isn't one in the
9570      user's program; just attach a DW_AT_name to the type.  */
9571   if (name
9572       && (TREE_CODE (name) != TYPE_DECL
9573           || (TREE_TYPE (name) == qualified_type && DECL_NAME (name))))
9574     {
9575       if (TREE_CODE (name) == TYPE_DECL)
9576         /* Could just call add_name_and_src_coords_attributes here,
9577            but since this is a builtin type it doesn't have any
9578            useful source coordinates anyway.  */
9579         name = DECL_NAME (name);
9580       add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
9581     }
9582
9583   if (qualified_type)
9584     equate_type_number_to_die (qualified_type, mod_type_die);
9585
9586   if (item_type)
9587     /* We must do this after the equate_type_number_to_die call, in case
9588        this is a recursive type.  This ensures that the modified_type_die
9589        recursion will terminate even if the type is recursive.  Recursive
9590        types are possible in Ada.  */
9591     sub_die = modified_type_die (item_type,
9592                                  TYPE_READONLY (item_type),
9593                                  TYPE_VOLATILE (item_type),
9594                                  context_die);
9595
9596   if (sub_die != NULL)
9597     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
9598
9599   return mod_type_die;
9600 }
9601
9602 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
9603    an enumerated type.  */
9604
9605 static inline int
9606 type_is_enum (const_tree type)
9607 {
9608   return TREE_CODE (type) == ENUMERAL_TYPE;
9609 }
9610
9611 /* Return the DBX register number described by a given RTL node.  */
9612
9613 static unsigned int
9614 dbx_reg_number (const_rtx rtl)
9615 {
9616   unsigned regno = REGNO (rtl);
9617
9618   gcc_assert (regno < FIRST_PSEUDO_REGISTER);
9619
9620 #ifdef LEAF_REG_REMAP
9621   if (current_function_uses_only_leaf_regs)
9622     {
9623       int leaf_reg = LEAF_REG_REMAP (regno);
9624       if (leaf_reg != -1)
9625         regno = (unsigned) leaf_reg;
9626     }
9627 #endif
9628
9629   return DBX_REGISTER_NUMBER (regno);
9630 }
9631
9632 /* Optionally add a DW_OP_piece term to a location description expression.
9633    DW_OP_piece is only added if the location description expression already
9634    doesn't end with DW_OP_piece.  */
9635
9636 static void
9637 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
9638 {
9639   dw_loc_descr_ref loc;
9640
9641   if (*list_head != NULL)
9642     {
9643       /* Find the end of the chain.  */
9644       for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
9645         ;
9646
9647       if (loc->dw_loc_opc != DW_OP_piece)
9648         loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
9649     }
9650 }
9651
9652 /* Return a location descriptor that designates a machine register or
9653    zero if there is none.  */
9654
9655 static dw_loc_descr_ref
9656 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
9657 {
9658   rtx regs;
9659
9660   if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
9661     return 0;
9662
9663   regs = targetm.dwarf_register_span (rtl);
9664
9665   if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
9666     return multiple_reg_loc_descriptor (rtl, regs, initialized);
9667   else
9668     return one_reg_loc_descriptor (dbx_reg_number (rtl), initialized);
9669 }
9670
9671 /* Return a location descriptor that designates a machine register for
9672    a given hard register number.  */
9673
9674 static dw_loc_descr_ref
9675 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
9676 {
9677   dw_loc_descr_ref reg_loc_descr = new_reg_loc_descr (regno, 0);
9678
9679   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
9680     add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9681
9682   return reg_loc_descr;
9683 }
9684
9685 /* Given an RTL of a register, return a location descriptor that
9686    designates a value that spans more than one register.  */
9687
9688 static dw_loc_descr_ref
9689 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
9690                              enum var_init_status initialized)
9691 {
9692   int nregs, size, i;
9693   unsigned reg;
9694   dw_loc_descr_ref loc_result = NULL;
9695
9696   reg = REGNO (rtl);
9697 #ifdef LEAF_REG_REMAP
9698   if (current_function_uses_only_leaf_regs)
9699     {
9700       int leaf_reg = LEAF_REG_REMAP (reg);
9701       if (leaf_reg != -1)
9702         reg = (unsigned) leaf_reg;
9703     }
9704 #endif
9705   gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
9706   nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
9707
9708   /* Simple, contiguous registers.  */
9709   if (regs == NULL_RTX)
9710     {
9711       size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
9712
9713       loc_result = NULL;
9714       while (nregs--)
9715         {
9716           dw_loc_descr_ref t;
9717
9718           t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
9719                                       VAR_INIT_STATUS_INITIALIZED);
9720           add_loc_descr (&loc_result, t);
9721           add_loc_descr_op_piece (&loc_result, size);
9722           ++reg;
9723         }
9724       return loc_result;
9725     }
9726
9727   /* Now onto stupid register sets in non contiguous locations.  */
9728
9729   gcc_assert (GET_CODE (regs) == PARALLEL);
9730
9731   size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
9732   loc_result = NULL;
9733
9734   for (i = 0; i < XVECLEN (regs, 0); ++i)
9735     {
9736       dw_loc_descr_ref t;
9737
9738       t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
9739                                   VAR_INIT_STATUS_INITIALIZED);
9740       add_loc_descr (&loc_result, t);
9741       size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
9742       add_loc_descr_op_piece (&loc_result, size);
9743     }
9744
9745   if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
9746     add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9747   return loc_result;
9748 }
9749
9750 #endif /* DWARF2_DEBUGGING_INFO */
9751
9752 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
9753
9754 /* Return a location descriptor that designates a constant.  */
9755
9756 static dw_loc_descr_ref
9757 int_loc_descriptor (HOST_WIDE_INT i)
9758 {
9759   enum dwarf_location_atom op;
9760
9761   /* Pick the smallest representation of a constant, rather than just
9762      defaulting to the LEB encoding.  */
9763   if (i >= 0)
9764     {
9765       if (i <= 31)
9766         op = DW_OP_lit0 + i;
9767       else if (i <= 0xff)
9768         op = DW_OP_const1u;
9769       else if (i <= 0xffff)
9770         op = DW_OP_const2u;
9771       else if (HOST_BITS_PER_WIDE_INT == 32
9772                || i <= 0xffffffff)
9773         op = DW_OP_const4u;
9774       else
9775         op = DW_OP_constu;
9776     }
9777   else
9778     {
9779       if (i >= -0x80)
9780         op = DW_OP_const1s;
9781       else if (i >= -0x8000)
9782         op = DW_OP_const2s;
9783       else if (HOST_BITS_PER_WIDE_INT == 32
9784                || i >= -0x80000000)
9785         op = DW_OP_const4s;
9786       else
9787         op = DW_OP_consts;
9788     }
9789
9790   return new_loc_descr (op, i, 0);
9791 }
9792 #endif
9793
9794 #ifdef DWARF2_DEBUGGING_INFO
9795
9796 /* Return a location descriptor that designates a base+offset location.  */
9797
9798 static dw_loc_descr_ref
9799 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
9800                  enum var_init_status initialized)
9801 {
9802   unsigned int regno;
9803   dw_loc_descr_ref result;
9804   dw_fde_ref fde = current_fde ();
9805
9806   /* We only use "frame base" when we're sure we're talking about the
9807      post-prologue local stack frame.  We do this by *not* running
9808      register elimination until this point, and recognizing the special
9809      argument pointer and soft frame pointer rtx's.  */
9810   if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
9811     {
9812       rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
9813
9814       if (elim != reg)
9815         {
9816           if (GET_CODE (elim) == PLUS)
9817             {
9818               offset += INTVAL (XEXP (elim, 1));
9819               elim = XEXP (elim, 0);
9820             }
9821           gcc_assert ((SUPPORTS_STACK_ALIGNMENT
9822                        && (elim == hard_frame_pointer_rtx
9823                            || elim == stack_pointer_rtx))
9824                       || elim == (frame_pointer_needed
9825                                   ? hard_frame_pointer_rtx
9826                                   : stack_pointer_rtx));
9827
9828           /* If drap register is used to align stack, use frame
9829              pointer + offset to access stack variables.  If stack
9830              is aligned without drap, use stack pointer + offset to
9831              access stack variables.  */
9832           if (crtl->stack_realign_tried
9833               && cfa.reg == HARD_FRAME_POINTER_REGNUM
9834               && reg == frame_pointer_rtx)
9835             {
9836               int base_reg
9837                 = DWARF_FRAME_REGNUM (cfa.indirect
9838                                       ? HARD_FRAME_POINTER_REGNUM
9839                                       : STACK_POINTER_REGNUM);
9840               return new_reg_loc_descr (base_reg, offset);
9841             }
9842
9843           offset += frame_pointer_fb_offset;
9844           return new_loc_descr (DW_OP_fbreg, offset, 0);
9845         }
9846     }
9847   else if (fde
9848            && fde->drap_reg != INVALID_REGNUM
9849            && (fde->drap_reg == REGNO (reg)
9850                || fde->vdrap_reg == REGNO (reg)))
9851     {
9852       /* Use cfa+offset to represent the location of arguments passed
9853          on stack when drap is used to align stack.  */
9854       return new_loc_descr (DW_OP_fbreg, offset, 0);
9855     }
9856
9857   regno = dbx_reg_number (reg);
9858   if (regno <= 31)
9859     result = new_loc_descr (DW_OP_breg0 + regno, offset, 0);
9860   else
9861     result = new_loc_descr (DW_OP_bregx, regno, offset);
9862
9863   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
9864     add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9865
9866   return result;
9867 }
9868
9869 /* Return true if this RTL expression describes a base+offset calculation.  */
9870
9871 static inline int
9872 is_based_loc (const_rtx rtl)
9873 {
9874   return (GET_CODE (rtl) == PLUS
9875           && ((REG_P (XEXP (rtl, 0))
9876                && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
9877                && GET_CODE (XEXP (rtl, 1)) == CONST_INT)));
9878 }
9879
9880 /* Return a descriptor that describes the concatenation of N locations
9881    used to form the address of a memory location.  */
9882
9883 static dw_loc_descr_ref
9884 concatn_mem_loc_descriptor (rtx concatn, enum machine_mode mode,
9885                             enum var_init_status initialized)
9886 {
9887   unsigned int i;
9888   dw_loc_descr_ref cc_loc_result = NULL;
9889   unsigned int n = XVECLEN (concatn, 0);
9890
9891   for (i = 0; i < n; ++i)
9892     {
9893       dw_loc_descr_ref ref;
9894       rtx x = XVECEXP (concatn, 0, i);
9895
9896       ref = mem_loc_descriptor (x, mode, VAR_INIT_STATUS_INITIALIZED);
9897       if (ref == NULL)
9898         return NULL;
9899
9900       add_loc_descr (&cc_loc_result, ref);
9901       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
9902     }
9903
9904   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
9905     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9906
9907   return cc_loc_result;
9908 }
9909
9910 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
9911    failed.  */
9912
9913 static dw_loc_descr_ref
9914 tls_mem_loc_descriptor (rtx mem)
9915 {
9916   tree base;
9917   dw_loc_descr_ref loc_result, loc_result2;
9918
9919   if (MEM_EXPR (mem) == NULL_TREE || MEM_OFFSET (mem) == NULL_RTX)
9920     return NULL;
9921
9922   base = get_base_address (MEM_EXPR (mem));
9923   if (base == NULL
9924       || TREE_CODE (base) != VAR_DECL
9925       || !DECL_THREAD_LOCAL_P (base))
9926     return NULL;
9927
9928   loc_result = loc_descriptor_from_tree_1 (MEM_EXPR (mem), 2);
9929   if (loc_result == NULL)
9930     return NULL;
9931
9932   if (INTVAL (MEM_OFFSET (mem)))
9933     {
9934       if (INTVAL (MEM_OFFSET (mem)) >= 0)
9935         add_loc_descr (&loc_result,
9936                        new_loc_descr (DW_OP_plus_uconst,
9937                                       INTVAL (MEM_OFFSET (mem)), 0));
9938       else
9939         {
9940           loc_result2 = mem_loc_descriptor (MEM_OFFSET (mem), GET_MODE (mem),
9941                                             VAR_INIT_STATUS_INITIALIZED);
9942           if (loc_result2 == 0)
9943             return NULL;
9944           add_loc_descr (&loc_result, loc_result2);
9945           add_loc_descr (&loc_result, new_loc_descr (DW_OP_plus, 0, 0));
9946         }
9947     }
9948
9949   return loc_result;
9950 }
9951
9952 /* The following routine converts the RTL for a variable or parameter
9953    (resident in memory) into an equivalent Dwarf representation of a
9954    mechanism for getting the address of that same variable onto the top of a
9955    hypothetical "address evaluation" stack.
9956
9957    When creating memory location descriptors, we are effectively transforming
9958    the RTL for a memory-resident object into its Dwarf postfix expression
9959    equivalent.  This routine recursively descends an RTL tree, turning
9960    it into Dwarf postfix code as it goes.
9961
9962    MODE is the mode of the memory reference, needed to handle some
9963    autoincrement addressing modes.
9964
9965    CAN_USE_FBREG is a flag whether we can use DW_AT_frame_base in the
9966    location list for RTL.
9967
9968    Return 0 if we can't represent the location.  */
9969
9970 static dw_loc_descr_ref
9971 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
9972                     enum var_init_status initialized)
9973 {
9974   dw_loc_descr_ref mem_loc_result = NULL;
9975   enum dwarf_location_atom op;
9976
9977   /* Note that for a dynamically sized array, the location we will generate a
9978      description of here will be the lowest numbered location which is
9979      actually within the array.  That's *not* necessarily the same as the
9980      zeroth element of the array.  */
9981
9982   rtl = targetm.delegitimize_address (rtl);
9983
9984   switch (GET_CODE (rtl))
9985     {
9986     case POST_INC:
9987     case POST_DEC:
9988     case POST_MODIFY:
9989       /* POST_INC and POST_DEC can be handled just like a SUBREG.  So we
9990          just fall into the SUBREG code.  */
9991
9992       /* ... fall through ...  */
9993
9994     case SUBREG:
9995       /* The case of a subreg may arise when we have a local (register)
9996          variable or a formal (register) parameter which doesn't quite fill
9997          up an entire register.  For now, just assume that it is
9998          legitimate to make the Dwarf info refer to the whole register which
9999          contains the given subreg.  */
10000       rtl = XEXP (rtl, 0);
10001
10002       /* ... fall through ...  */
10003
10004     case REG:
10005       /* Whenever a register number forms a part of the description of the
10006          method for calculating the (dynamic) address of a memory resident
10007          object, DWARF rules require the register number be referred to as
10008          a "base register".  This distinction is not based in any way upon
10009          what category of register the hardware believes the given register
10010          belongs to.  This is strictly DWARF terminology we're dealing with
10011          here. Note that in cases where the location of a memory-resident
10012          data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
10013          OP_CONST (0)) the actual DWARF location descriptor that we generate
10014          may just be OP_BASEREG (basereg).  This may look deceptively like
10015          the object in question was allocated to a register (rather than in
10016          memory) so DWARF consumers need to be aware of the subtle
10017          distinction between OP_REG and OP_BASEREG.  */
10018       if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
10019         mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
10020       else if (stack_realign_drap
10021                && crtl->drap_reg
10022                && crtl->args.internal_arg_pointer == rtl
10023                && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
10024         {
10025           /* If RTL is internal_arg_pointer, which has been optimized
10026              out, use DRAP instead.  */
10027           mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
10028                                             VAR_INIT_STATUS_INITIALIZED);
10029         }
10030       break;
10031
10032     case MEM:
10033       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
10034                                            VAR_INIT_STATUS_INITIALIZED);
10035       if (mem_loc_result == NULL)
10036         mem_loc_result = tls_mem_loc_descriptor (rtl);
10037       if (mem_loc_result != 0)
10038         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
10039       break;
10040
10041     case LO_SUM:
10042          rtl = XEXP (rtl, 1);
10043
10044       /* ... fall through ...  */
10045
10046     case LABEL_REF:
10047       /* Some ports can transform a symbol ref into a label ref, because
10048          the symbol ref is too far away and has to be dumped into a constant
10049          pool.  */
10050     case CONST:
10051     case SYMBOL_REF:
10052       /* Alternatively, the symbol in the constant pool might be referenced
10053          by a different symbol.  */
10054       if (GET_CODE (rtl) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (rtl))
10055         {
10056           bool marked;
10057           rtx tmp = get_pool_constant_mark (rtl, &marked);
10058
10059           if (GET_CODE (tmp) == SYMBOL_REF)
10060             {
10061               rtl = tmp;
10062               if (CONSTANT_POOL_ADDRESS_P (tmp))
10063                 get_pool_constant_mark (tmp, &marked);
10064               else
10065                 marked = true;
10066             }
10067
10068           /* If all references to this pool constant were optimized away,
10069              it was not output and thus we can't represent it.
10070              FIXME: might try to use DW_OP_const_value here, though
10071              DW_OP_piece complicates it.  */
10072           if (!marked)
10073             return 0;
10074         }
10075
10076       mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
10077       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
10078       mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
10079       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
10080       break;
10081
10082     case PRE_MODIFY:
10083       /* Extract the PLUS expression nested inside and fall into
10084          PLUS code below.  */
10085       rtl = XEXP (rtl, 1);
10086       goto plus;
10087
10088     case PRE_INC:
10089     case PRE_DEC:
10090       /* Turn these into a PLUS expression and fall into the PLUS code
10091          below.  */
10092       rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
10093                           GEN_INT (GET_CODE (rtl) == PRE_INC
10094                                    ? GET_MODE_UNIT_SIZE (mode)
10095                                    : -GET_MODE_UNIT_SIZE (mode)));
10096
10097       /* ... fall through ...  */
10098
10099     case PLUS:
10100     plus:
10101       if (is_based_loc (rtl))
10102         mem_loc_result = based_loc_descr (XEXP (rtl, 0),
10103                                           INTVAL (XEXP (rtl, 1)),
10104                                           VAR_INIT_STATUS_INITIALIZED);
10105       else
10106         {
10107           mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode,
10108                                                VAR_INIT_STATUS_INITIALIZED);
10109           if (mem_loc_result == 0)
10110             break;
10111
10112           if (GET_CODE (XEXP (rtl, 1)) == CONST_INT
10113               && INTVAL (XEXP (rtl, 1)) >= 0)
10114             add_loc_descr (&mem_loc_result,
10115                            new_loc_descr (DW_OP_plus_uconst,
10116                                           INTVAL (XEXP (rtl, 1)), 0));
10117           else
10118             {
10119               dw_loc_descr_ref mem_loc_result2
10120                 = mem_loc_descriptor (XEXP (rtl, 1), mode,
10121                                       VAR_INIT_STATUS_INITIALIZED);
10122               if (mem_loc_result2 == 0)
10123                 break;
10124               add_loc_descr (&mem_loc_result, mem_loc_result2);
10125               add_loc_descr (&mem_loc_result,
10126                              new_loc_descr (DW_OP_plus, 0, 0));
10127             }
10128         }
10129       break;
10130
10131     /* If a pseudo-reg is optimized away, it is possible for it to
10132        be replaced with a MEM containing a multiply or shift.  */
10133     case MULT:
10134       op = DW_OP_mul;
10135       goto do_binop;
10136
10137     case ASHIFT:
10138       op = DW_OP_shl;
10139       goto do_binop;
10140
10141     case ASHIFTRT:
10142       op = DW_OP_shra;
10143       goto do_binop;
10144
10145     case LSHIFTRT:
10146       op = DW_OP_shr;
10147       goto do_binop;
10148
10149     do_binop:
10150       {
10151         dw_loc_descr_ref op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
10152                                                    VAR_INIT_STATUS_INITIALIZED);
10153         dw_loc_descr_ref op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
10154                                                    VAR_INIT_STATUS_INITIALIZED);
10155
10156         if (op0 == 0 || op1 == 0)
10157           break;
10158
10159         mem_loc_result = op0;
10160         add_loc_descr (&mem_loc_result, op1);
10161         add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
10162         break;
10163       }
10164
10165     case CONST_INT:
10166       mem_loc_result = int_loc_descriptor (INTVAL (rtl));
10167       break;
10168
10169     case CONCATN:
10170       mem_loc_result = concatn_mem_loc_descriptor (rtl, mode,
10171                                                    VAR_INIT_STATUS_INITIALIZED);
10172       break;
10173
10174     case UNSPEC:
10175       /* If delegitimize_address couldn't do anything with the UNSPEC, we
10176          can't express it in the debug info.  This can happen e.g. with some
10177          TLS UNSPECs.  */
10178       break;
10179
10180     default:
10181       gcc_unreachable ();
10182     }
10183
10184   if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10185     add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10186
10187   return mem_loc_result;
10188 }
10189
10190 /* Return a descriptor that describes the concatenation of two locations.
10191    This is typically a complex variable.  */
10192
10193 static dw_loc_descr_ref
10194 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
10195 {
10196   dw_loc_descr_ref cc_loc_result = NULL;
10197   dw_loc_descr_ref x0_ref = loc_descriptor (x0, VAR_INIT_STATUS_INITIALIZED);
10198   dw_loc_descr_ref x1_ref = loc_descriptor (x1, VAR_INIT_STATUS_INITIALIZED);
10199
10200   if (x0_ref == 0 || x1_ref == 0)
10201     return 0;
10202
10203   cc_loc_result = x0_ref;
10204   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
10205
10206   add_loc_descr (&cc_loc_result, x1_ref);
10207   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
10208
10209   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10210     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10211
10212   return cc_loc_result;
10213 }
10214
10215 /* Return a descriptor that describes the concatenation of N
10216    locations.  */
10217
10218 static dw_loc_descr_ref
10219 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
10220 {
10221   unsigned int i;
10222   dw_loc_descr_ref cc_loc_result = NULL;
10223   unsigned int n = XVECLEN (concatn, 0);
10224
10225   for (i = 0; i < n; ++i)
10226     {
10227       dw_loc_descr_ref ref;
10228       rtx x = XVECEXP (concatn, 0, i);
10229
10230       ref = loc_descriptor (x, VAR_INIT_STATUS_INITIALIZED);
10231       if (ref == NULL)
10232         return NULL;
10233
10234       add_loc_descr (&cc_loc_result, ref);
10235       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
10236     }
10237
10238   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10239     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10240
10241   return cc_loc_result;
10242 }
10243
10244 /* Output a proper Dwarf location descriptor for a variable or parameter
10245    which is either allocated in a register or in a memory location.  For a
10246    register, we just generate an OP_REG and the register number.  For a
10247    memory location we provide a Dwarf postfix expression describing how to
10248    generate the (dynamic) address of the object onto the address stack.
10249
10250    If we don't know how to describe it, return 0.  */
10251
10252 static dw_loc_descr_ref
10253 loc_descriptor (rtx rtl, enum var_init_status initialized)
10254 {
10255   dw_loc_descr_ref loc_result = NULL;
10256
10257   switch (GET_CODE (rtl))
10258     {
10259     case SUBREG:
10260       /* The case of a subreg may arise when we have a local (register)
10261          variable or a formal (register) parameter which doesn't quite fill
10262          up an entire register.  For now, just assume that it is
10263          legitimate to make the Dwarf info refer to the whole register which
10264          contains the given subreg.  */
10265       rtl = SUBREG_REG (rtl);
10266
10267       /* ... fall through ...  */
10268
10269     case REG:
10270       loc_result = reg_loc_descriptor (rtl, initialized);
10271       break;
10272
10273     case MEM:
10274       loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
10275                                        initialized);
10276       if (loc_result == NULL)
10277         loc_result = tls_mem_loc_descriptor (rtl);
10278       break;
10279
10280     case CONCAT:
10281       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
10282                                           initialized);
10283       break;
10284
10285     case CONCATN:
10286       loc_result = concatn_loc_descriptor (rtl, initialized);
10287       break;
10288
10289     case VAR_LOCATION:
10290       /* Single part.  */
10291       if (GET_CODE (XEXP (rtl, 1)) != PARALLEL)
10292         {
10293           loc_result = loc_descriptor (XEXP (XEXP (rtl, 1), 0), initialized);
10294           break;
10295         }
10296
10297       rtl = XEXP (rtl, 1);
10298       /* FALLTHRU */
10299
10300     case PARALLEL:
10301       {
10302         rtvec par_elems = XVEC (rtl, 0);
10303         int num_elem = GET_NUM_ELEM (par_elems);
10304         enum machine_mode mode;
10305         int i;
10306
10307         /* Create the first one, so we have something to add to.  */
10308         loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
10309                                      initialized);
10310         if (loc_result == NULL)
10311           return NULL;
10312         mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
10313         add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
10314         for (i = 1; i < num_elem; i++)
10315           {
10316             dw_loc_descr_ref temp;
10317
10318             temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
10319                                    initialized);
10320             if (temp == NULL)
10321               return NULL;
10322             add_loc_descr (&loc_result, temp);
10323             mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
10324             add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
10325           }
10326       }
10327       break;
10328
10329     default:
10330       gcc_unreachable ();
10331     }
10332
10333   return loc_result;
10334 }
10335
10336 /* Similar, but generate the descriptor from trees instead of rtl.  This comes
10337    up particularly with variable length arrays.  WANT_ADDRESS is 2 if this is
10338    a top-level invocation of loc_descriptor_from_tree; is 1 if this is not a
10339    top-level invocation, and we require the address of LOC; is 0 if we require
10340    the value of LOC.  */
10341
10342 static dw_loc_descr_ref
10343 loc_descriptor_from_tree_1 (tree loc, int want_address)
10344 {
10345   dw_loc_descr_ref ret, ret1;
10346   int have_address = 0;
10347   enum dwarf_location_atom op;
10348
10349   /* ??? Most of the time we do not take proper care for sign/zero
10350      extending the values properly.  Hopefully this won't be a real
10351      problem...  */
10352
10353   switch (TREE_CODE (loc))
10354     {
10355     case ERROR_MARK:
10356       return 0;
10357
10358     case PLACEHOLDER_EXPR:
10359       /* This case involves extracting fields from an object to determine the
10360          position of other fields.  We don't try to encode this here.  The
10361          only user of this is Ada, which encodes the needed information using
10362          the names of types.  */
10363       return 0;
10364
10365     case CALL_EXPR:
10366       return 0;
10367
10368     case PREINCREMENT_EXPR:
10369     case PREDECREMENT_EXPR:
10370     case POSTINCREMENT_EXPR:
10371     case POSTDECREMENT_EXPR:
10372       /* There are no opcodes for these operations.  */
10373       return 0;
10374
10375     case ADDR_EXPR:
10376       /* If we already want an address, there's nothing we can do.  */
10377       if (want_address)
10378         return 0;
10379
10380       /* Otherwise, process the argument and look for the address.  */
10381       return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 1);
10382
10383     case VAR_DECL:
10384       if (DECL_THREAD_LOCAL_P (loc))
10385         {
10386           rtx rtl;
10387           unsigned first_op;
10388           unsigned second_op;
10389
10390           if (targetm.have_tls)
10391             {
10392               /* If this is not defined, we have no way to emit the
10393                  data.  */
10394               if (!targetm.asm_out.output_dwarf_dtprel)
10395                 return 0;
10396
10397                /* The way DW_OP_GNU_push_tls_address is specified, we
10398                   can only look up addresses of objects in the current
10399                   module.  */
10400               if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
10401                 return 0;
10402               first_op = INTERNAL_DW_OP_tls_addr;
10403               second_op = DW_OP_GNU_push_tls_address;
10404             }
10405           else
10406             {
10407               if (!targetm.emutls.debug_form_tls_address)
10408                 return 0;
10409               loc = emutls_decl (loc);
10410               first_op = DW_OP_addr;
10411               second_op = DW_OP_form_tls_address;
10412             }
10413
10414           rtl = rtl_for_decl_location (loc);
10415           if (rtl == NULL_RTX)
10416             return 0;
10417
10418           if (!MEM_P (rtl))
10419             return 0;
10420           rtl = XEXP (rtl, 0);
10421           if (! CONSTANT_P (rtl))
10422             return 0;
10423
10424           ret = new_loc_descr (first_op, 0, 0);
10425           ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
10426           ret->dw_loc_oprnd1.v.val_addr = rtl;
10427
10428           ret1 = new_loc_descr (second_op, 0, 0);
10429           add_loc_descr (&ret, ret1);
10430
10431           have_address = 1;
10432           break;
10433         }
10434       /* FALLTHRU */
10435
10436     case PARM_DECL:
10437       if (DECL_HAS_VALUE_EXPR_P (loc))
10438         return loc_descriptor_from_tree_1 (DECL_VALUE_EXPR (loc),
10439                                            want_address);
10440       /* FALLTHRU */
10441
10442     case RESULT_DECL:
10443     case FUNCTION_DECL:
10444       {
10445         rtx rtl = rtl_for_decl_location (loc);
10446
10447         if (rtl == NULL_RTX)
10448           return 0;
10449         else if (GET_CODE (rtl) == CONST_INT)
10450           {
10451             HOST_WIDE_INT val = INTVAL (rtl);
10452             if (TYPE_UNSIGNED (TREE_TYPE (loc)))
10453               val &= GET_MODE_MASK (DECL_MODE (loc));
10454             ret = int_loc_descriptor (val);
10455           }
10456         else if (GET_CODE (rtl) == CONST_STRING)
10457           return 0;
10458         else if (CONSTANT_P (rtl))
10459           {
10460             ret = new_loc_descr (DW_OP_addr, 0, 0);
10461             ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
10462             ret->dw_loc_oprnd1.v.val_addr = rtl;
10463           }
10464         else
10465           {
10466             enum machine_mode mode;
10467
10468             /* Certain constructs can only be represented at top-level.  */
10469             if (want_address == 2)
10470               return loc_descriptor (rtl, VAR_INIT_STATUS_INITIALIZED);
10471
10472             mode = GET_MODE (rtl);
10473             if (MEM_P (rtl))
10474               {
10475                 rtl = XEXP (rtl, 0);
10476                 have_address = 1;
10477               }
10478             ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
10479           }
10480       }
10481       break;
10482
10483     case INDIRECT_REF:
10484       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
10485       have_address = 1;
10486       break;
10487
10488     case COMPOUND_EXPR:
10489       return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), want_address);
10490
10491     CASE_CONVERT:
10492     case VIEW_CONVERT_EXPR:
10493     case SAVE_EXPR:
10494     case MODIFY_EXPR:
10495       return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), want_address);
10496
10497     case COMPONENT_REF:
10498     case BIT_FIELD_REF:
10499     case ARRAY_REF:
10500     case ARRAY_RANGE_REF:
10501       {
10502         tree obj, offset;
10503         HOST_WIDE_INT bitsize, bitpos, bytepos;
10504         enum machine_mode mode;
10505         int volatilep;
10506         int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
10507
10508         obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
10509                                    &unsignedp, &volatilep, false);
10510
10511         if (obj == loc)
10512           return 0;
10513
10514         ret = loc_descriptor_from_tree_1 (obj, 1);
10515         if (ret == 0
10516             || bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
10517           return 0;
10518
10519         if (offset != NULL_TREE)
10520           {
10521             /* Variable offset.  */
10522             ret1 = loc_descriptor_from_tree_1 (offset, 0);
10523             if (ret1 == 0)
10524               return 0;
10525             add_loc_descr (&ret, ret1);
10526             add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
10527           }
10528
10529         bytepos = bitpos / BITS_PER_UNIT;
10530         if (bytepos > 0)
10531           add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
10532         else if (bytepos < 0)
10533           {
10534             add_loc_descr (&ret, int_loc_descriptor (bytepos));
10535             add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
10536           }
10537
10538         have_address = 1;
10539         break;
10540       }
10541
10542     case INTEGER_CST:
10543       if (host_integerp (loc, 0))
10544         ret = int_loc_descriptor (tree_low_cst (loc, 0));
10545       else
10546         return 0;
10547       break;
10548
10549     case CONSTRUCTOR:
10550       {
10551         /* Get an RTL for this, if something has been emitted.  */
10552         rtx rtl = lookup_constant_def (loc);
10553         enum machine_mode mode;
10554
10555         if (!rtl || !MEM_P (rtl))
10556           return 0;
10557         mode = GET_MODE (rtl);
10558         rtl = XEXP (rtl, 0);
10559         ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
10560         have_address = 1;
10561         break;
10562       }
10563
10564     case TRUTH_AND_EXPR:
10565     case TRUTH_ANDIF_EXPR:
10566     case BIT_AND_EXPR:
10567       op = DW_OP_and;
10568       goto do_binop;
10569
10570     case TRUTH_XOR_EXPR:
10571     case BIT_XOR_EXPR:
10572       op = DW_OP_xor;
10573       goto do_binop;
10574
10575     case TRUTH_OR_EXPR:
10576     case TRUTH_ORIF_EXPR:
10577     case BIT_IOR_EXPR:
10578       op = DW_OP_or;
10579       goto do_binop;
10580
10581     case FLOOR_DIV_EXPR:
10582     case CEIL_DIV_EXPR:
10583     case ROUND_DIV_EXPR:
10584     case TRUNC_DIV_EXPR:
10585       op = DW_OP_div;
10586       goto do_binop;
10587
10588     case MINUS_EXPR:
10589       op = DW_OP_minus;
10590       goto do_binop;
10591
10592     case FLOOR_MOD_EXPR:
10593     case CEIL_MOD_EXPR:
10594     case ROUND_MOD_EXPR:
10595     case TRUNC_MOD_EXPR:
10596       op = DW_OP_mod;
10597       goto do_binop;
10598
10599     case MULT_EXPR:
10600       op = DW_OP_mul;
10601       goto do_binop;
10602
10603     case LSHIFT_EXPR:
10604       op = DW_OP_shl;
10605       goto do_binop;
10606
10607     case RSHIFT_EXPR:
10608       op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
10609       goto do_binop;
10610
10611     case POINTER_PLUS_EXPR:
10612     case PLUS_EXPR:
10613       if (TREE_CODE (TREE_OPERAND (loc, 1)) == INTEGER_CST
10614           && host_integerp (TREE_OPERAND (loc, 1), 0))
10615         {
10616           ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
10617           if (ret == 0)
10618             return 0;
10619
10620           add_loc_descr (&ret,
10621                          new_loc_descr (DW_OP_plus_uconst,
10622                                         tree_low_cst (TREE_OPERAND (loc, 1),
10623                                                       0),
10624                                         0));
10625           break;
10626         }
10627
10628       op = DW_OP_plus;
10629       goto do_binop;
10630
10631     case LE_EXPR:
10632       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
10633         return 0;
10634
10635       op = DW_OP_le;
10636       goto do_binop;
10637
10638     case GE_EXPR:
10639       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
10640         return 0;
10641
10642       op = DW_OP_ge;
10643       goto do_binop;
10644
10645     case LT_EXPR:
10646       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
10647         return 0;
10648
10649       op = DW_OP_lt;
10650       goto do_binop;
10651
10652     case GT_EXPR:
10653       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
10654         return 0;
10655
10656       op = DW_OP_gt;
10657       goto do_binop;
10658
10659     case EQ_EXPR:
10660       op = DW_OP_eq;
10661       goto do_binop;
10662
10663     case NE_EXPR:
10664       op = DW_OP_ne;
10665       goto do_binop;
10666
10667     do_binop:
10668       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
10669       ret1 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
10670       if (ret == 0 || ret1 == 0)
10671         return 0;
10672
10673       add_loc_descr (&ret, ret1);
10674       add_loc_descr (&ret, new_loc_descr (op, 0, 0));
10675       break;
10676
10677     case TRUTH_NOT_EXPR:
10678     case BIT_NOT_EXPR:
10679       op = DW_OP_not;
10680       goto do_unop;
10681
10682     case ABS_EXPR:
10683       op = DW_OP_abs;
10684       goto do_unop;
10685
10686     case NEGATE_EXPR:
10687       op = DW_OP_neg;
10688       goto do_unop;
10689
10690     do_unop:
10691       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
10692       if (ret == 0)
10693         return 0;
10694
10695       add_loc_descr (&ret, new_loc_descr (op, 0, 0));
10696       break;
10697
10698     case MIN_EXPR:
10699     case MAX_EXPR:
10700       {
10701         const enum tree_code code =
10702           TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
10703
10704         loc = build3 (COND_EXPR, TREE_TYPE (loc),
10705                       build2 (code, integer_type_node,
10706                               TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
10707                       TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
10708       }
10709
10710       /* ... fall through ...  */
10711
10712     case COND_EXPR:
10713       {
10714         dw_loc_descr_ref lhs
10715           = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
10716         dw_loc_descr_ref rhs
10717           = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 2), 0);
10718         dw_loc_descr_ref bra_node, jump_node, tmp;
10719
10720         ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
10721         if (ret == 0 || lhs == 0 || rhs == 0)
10722           return 0;
10723
10724         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
10725         add_loc_descr (&ret, bra_node);
10726
10727         add_loc_descr (&ret, rhs);
10728         jump_node = new_loc_descr (DW_OP_skip, 0, 0);
10729         add_loc_descr (&ret, jump_node);
10730
10731         add_loc_descr (&ret, lhs);
10732         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
10733         bra_node->dw_loc_oprnd1.v.val_loc = lhs;
10734
10735         /* ??? Need a node to point the skip at.  Use a nop.  */
10736         tmp = new_loc_descr (DW_OP_nop, 0, 0);
10737         add_loc_descr (&ret, tmp);
10738         jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
10739         jump_node->dw_loc_oprnd1.v.val_loc = tmp;
10740       }
10741       break;
10742
10743     case FIX_TRUNC_EXPR:
10744       return 0;
10745
10746     default:
10747       /* Leave front-end specific codes as simply unknown.  This comes
10748          up, for instance, with the C STMT_EXPR.  */
10749       if ((unsigned int) TREE_CODE (loc)
10750           >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
10751         return 0;
10752
10753 #ifdef ENABLE_CHECKING
10754       /* Otherwise this is a generic code; we should just lists all of
10755          these explicitly.  We forgot one.  */
10756       gcc_unreachable ();
10757 #else
10758       /* In a release build, we want to degrade gracefully: better to
10759          generate incomplete debugging information than to crash.  */
10760       return NULL;
10761 #endif
10762     }
10763
10764   /* Show if we can't fill the request for an address.  */
10765   if (want_address && !have_address)
10766     return 0;
10767
10768   /* If we've got an address and don't want one, dereference.  */
10769   if (!want_address && have_address && ret)
10770     {
10771       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
10772
10773       if (size > DWARF2_ADDR_SIZE || size == -1)
10774         return 0;
10775       else if (size == DWARF2_ADDR_SIZE)
10776         op = DW_OP_deref;
10777       else
10778         op = DW_OP_deref_size;
10779
10780       add_loc_descr (&ret, new_loc_descr (op, size, 0));
10781     }
10782
10783   return ret;
10784 }
10785
10786 static inline dw_loc_descr_ref
10787 loc_descriptor_from_tree (tree loc)
10788 {
10789   return loc_descriptor_from_tree_1 (loc, 2);
10790 }
10791
10792 /* Given a value, round it up to the lowest multiple of `boundary'
10793    which is not less than the value itself.  */
10794
10795 static inline HOST_WIDE_INT
10796 ceiling (HOST_WIDE_INT value, unsigned int boundary)
10797 {
10798   return (((value + boundary - 1) / boundary) * boundary);
10799 }
10800
10801 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
10802    pointer to the declared type for the relevant field variable, or return
10803    `integer_type_node' if the given node turns out to be an
10804    ERROR_MARK node.  */
10805
10806 static inline tree
10807 field_type (const_tree decl)
10808 {
10809   tree type;
10810
10811   if (TREE_CODE (decl) == ERROR_MARK)
10812     return integer_type_node;
10813
10814   type = DECL_BIT_FIELD_TYPE (decl);
10815   if (type == NULL_TREE)
10816     type = TREE_TYPE (decl);
10817
10818   return type;
10819 }
10820
10821 /* Given a pointer to a tree node, return the alignment in bits for
10822    it, or else return BITS_PER_WORD if the node actually turns out to
10823    be an ERROR_MARK node.  */
10824
10825 static inline unsigned
10826 simple_type_align_in_bits (const_tree type)
10827 {
10828   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
10829 }
10830
10831 static inline unsigned
10832 simple_decl_align_in_bits (const_tree decl)
10833 {
10834   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
10835 }
10836
10837 /* Return the result of rounding T up to ALIGN.  */
10838
10839 static inline HOST_WIDE_INT
10840 round_up_to_align (HOST_WIDE_INT t, unsigned int align)
10841 {
10842   /* We must be careful if T is negative because HOST_WIDE_INT can be
10843      either "above" or "below" unsigned int as per the C promotion
10844      rules, depending on the host, thus making the signedness of the
10845      direct multiplication and division unpredictable.  */
10846   unsigned HOST_WIDE_INT u = (unsigned HOST_WIDE_INT) t;
10847
10848   u += align - 1;
10849   u /= align;
10850   u *= align;
10851
10852   return (HOST_WIDE_INT) u;
10853 }
10854
10855 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
10856    lowest addressed byte of the "containing object" for the given FIELD_DECL,
10857    or return 0 if we are unable to determine what that offset is, either
10858    because the argument turns out to be a pointer to an ERROR_MARK node, or
10859    because the offset is actually variable.  (We can't handle the latter case
10860    just yet).  */
10861
10862 static HOST_WIDE_INT
10863 field_byte_offset (const_tree decl)
10864 {
10865   HOST_WIDE_INT object_offset_in_bits;
10866   HOST_WIDE_INT bitpos_int;
10867
10868   if (TREE_CODE (decl) == ERROR_MARK)
10869     return 0;
10870
10871   gcc_assert (TREE_CODE (decl) == FIELD_DECL);
10872
10873   /* We cannot yet cope with fields whose positions are variable, so
10874      for now, when we see such things, we simply return 0.  Someday, we may
10875      be able to handle such cases, but it will be damn difficult.  */
10876   if (! host_integerp (bit_position (decl), 0))
10877     return 0;
10878
10879   bitpos_int = int_bit_position (decl);
10880
10881 #ifdef PCC_BITFIELD_TYPE_MATTERS
10882   if (PCC_BITFIELD_TYPE_MATTERS)
10883     {
10884       tree type;
10885       tree field_size_tree;
10886       HOST_WIDE_INT deepest_bitpos;
10887       unsigned HOST_WIDE_INT field_size_in_bits;
10888       unsigned int type_align_in_bits;
10889       unsigned int decl_align_in_bits;
10890       unsigned HOST_WIDE_INT type_size_in_bits;
10891
10892       type = field_type (decl);
10893       field_size_tree = DECL_SIZE (decl);
10894
10895       /* The size could be unspecified if there was an error, or for
10896          a flexible array member.  */
10897       if (! field_size_tree)
10898         field_size_tree = bitsize_zero_node;
10899
10900       /* If we don't know the size of the field, pretend it's a full word.  */
10901       if (host_integerp (field_size_tree, 1))
10902         field_size_in_bits = tree_low_cst (field_size_tree, 1);
10903       else
10904         field_size_in_bits = BITS_PER_WORD;
10905
10906       type_size_in_bits = simple_type_size_in_bits (type);
10907       type_align_in_bits = simple_type_align_in_bits (type);
10908       decl_align_in_bits = simple_decl_align_in_bits (decl);
10909
10910       /* The GCC front-end doesn't make any attempt to keep track of the
10911          starting bit offset (relative to the start of the containing
10912          structure type) of the hypothetical "containing object" for a
10913          bit-field.  Thus, when computing the byte offset value for the
10914          start of the "containing object" of a bit-field, we must deduce
10915          this information on our own. This can be rather tricky to do in
10916          some cases.  For example, handling the following structure type
10917          definition when compiling for an i386/i486 target (which only
10918          aligns long long's to 32-bit boundaries) can be very tricky:
10919
10920          struct S { int field1; long long field2:31; };
10921
10922          Fortunately, there is a simple rule-of-thumb which can be used
10923          in such cases.  When compiling for an i386/i486, GCC will
10924          allocate 8 bytes for the structure shown above.  It decides to
10925          do this based upon one simple rule for bit-field allocation.
10926          GCC allocates each "containing object" for each bit-field at
10927          the first (i.e. lowest addressed) legitimate alignment boundary
10928          (based upon the required minimum alignment for the declared
10929          type of the field) which it can possibly use, subject to the
10930          condition that there is still enough available space remaining
10931          in the containing object (when allocated at the selected point)
10932          to fully accommodate all of the bits of the bit-field itself.
10933
10934          This simple rule makes it obvious why GCC allocates 8 bytes for
10935          each object of the structure type shown above.  When looking
10936          for a place to allocate the "containing object" for `field2',
10937          the compiler simply tries to allocate a 64-bit "containing
10938          object" at each successive 32-bit boundary (starting at zero)
10939          until it finds a place to allocate that 64- bit field such that
10940          at least 31 contiguous (and previously unallocated) bits remain
10941          within that selected 64 bit field.  (As it turns out, for the
10942          example above, the compiler finds it is OK to allocate the
10943          "containing object" 64-bit field at bit-offset zero within the
10944          structure type.)
10945
10946          Here we attempt to work backwards from the limited set of facts
10947          we're given, and we try to deduce from those facts, where GCC
10948          must have believed that the containing object started (within
10949          the structure type). The value we deduce is then used (by the
10950          callers of this routine) to generate DW_AT_location and
10951          DW_AT_bit_offset attributes for fields (both bit-fields and, in
10952          the case of DW_AT_location, regular fields as well).  */
10953
10954       /* Figure out the bit-distance from the start of the structure to
10955          the "deepest" bit of the bit-field.  */
10956       deepest_bitpos = bitpos_int + field_size_in_bits;
10957
10958       /* This is the tricky part.  Use some fancy footwork to deduce
10959          where the lowest addressed bit of the containing object must
10960          be.  */
10961       object_offset_in_bits = deepest_bitpos - type_size_in_bits;
10962
10963       /* Round up to type_align by default.  This works best for
10964          bitfields.  */
10965       object_offset_in_bits
10966         = round_up_to_align (object_offset_in_bits, type_align_in_bits);
10967
10968       if (object_offset_in_bits > bitpos_int)
10969         {
10970           object_offset_in_bits = deepest_bitpos - type_size_in_bits;
10971
10972           /* Round up to decl_align instead.  */
10973           object_offset_in_bits
10974             = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
10975         }
10976     }
10977   else
10978 #endif
10979     object_offset_in_bits = bitpos_int;
10980
10981   return object_offset_in_bits / BITS_PER_UNIT;
10982 }
10983 \f
10984 /* The following routines define various Dwarf attributes and any data
10985    associated with them.  */
10986
10987 /* Add a location description attribute value to a DIE.
10988
10989    This emits location attributes suitable for whole variables and
10990    whole parameters.  Note that the location attributes for struct fields are
10991    generated by the routine `data_member_location_attribute' below.  */
10992
10993 static inline void
10994 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
10995                              dw_loc_descr_ref descr)
10996 {
10997   if (descr != 0)
10998     add_AT_loc (die, attr_kind, descr);
10999 }
11000
11001 /* Attach the specialized form of location attribute used for data members of
11002    struct and union types.  In the special case of a FIELD_DECL node which
11003    represents a bit-field, the "offset" part of this special location
11004    descriptor must indicate the distance in bytes from the lowest-addressed
11005    byte of the containing struct or union type to the lowest-addressed byte of
11006    the "containing object" for the bit-field.  (See the `field_byte_offset'
11007    function above).
11008
11009    For any given bit-field, the "containing object" is a hypothetical object
11010    (of some integral or enum type) within which the given bit-field lives.  The
11011    type of this hypothetical "containing object" is always the same as the
11012    declared type of the individual bit-field itself (for GCC anyway... the
11013    DWARF spec doesn't actually mandate this).  Note that it is the size (in
11014    bytes) of the hypothetical "containing object" which will be given in the
11015    DW_AT_byte_size attribute for this bit-field.  (See the
11016    `byte_size_attribute' function below.)  It is also used when calculating the
11017    value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
11018    function below.)  */
11019
11020 static void
11021 add_data_member_location_attribute (dw_die_ref die, tree decl)
11022 {
11023   HOST_WIDE_INT offset;
11024   dw_loc_descr_ref loc_descr = 0;
11025
11026   if (TREE_CODE (decl) == TREE_BINFO)
11027     {
11028       /* We're working on the TAG_inheritance for a base class.  */
11029       if (BINFO_VIRTUAL_P (decl) && is_cxx ())
11030         {
11031           /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
11032              aren't at a fixed offset from all (sub)objects of the same
11033              type.  We need to extract the appropriate offset from our
11034              vtable.  The following dwarf expression means
11035
11036                BaseAddr = ObAddr + *((*ObAddr) - Offset)
11037
11038              This is specific to the V3 ABI, of course.  */
11039
11040           dw_loc_descr_ref tmp;
11041
11042           /* Make a copy of the object address.  */
11043           tmp = new_loc_descr (DW_OP_dup, 0, 0);
11044           add_loc_descr (&loc_descr, tmp);
11045
11046           /* Extract the vtable address.  */
11047           tmp = new_loc_descr (DW_OP_deref, 0, 0);
11048           add_loc_descr (&loc_descr, tmp);
11049
11050           /* Calculate the address of the offset.  */
11051           offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
11052           gcc_assert (offset < 0);
11053
11054           tmp = int_loc_descriptor (-offset);
11055           add_loc_descr (&loc_descr, tmp);
11056           tmp = new_loc_descr (DW_OP_minus, 0, 0);
11057           add_loc_descr (&loc_descr, tmp);
11058
11059           /* Extract the offset.  */
11060           tmp = new_loc_descr (DW_OP_deref, 0, 0);
11061           add_loc_descr (&loc_descr, tmp);
11062
11063           /* Add it to the object address.  */
11064           tmp = new_loc_descr (DW_OP_plus, 0, 0);
11065           add_loc_descr (&loc_descr, tmp);
11066         }
11067       else
11068         offset = tree_low_cst (BINFO_OFFSET (decl), 0);
11069     }
11070   else
11071     offset = field_byte_offset (decl);
11072
11073   if (! loc_descr)
11074     {
11075       enum dwarf_location_atom op;
11076
11077       /* The DWARF2 standard says that we should assume that the structure
11078          address is already on the stack, so we can specify a structure field
11079          address by using DW_OP_plus_uconst.  */
11080
11081 #ifdef MIPS_DEBUGGING_INFO
11082       /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
11083          operator correctly.  It works only if we leave the offset on the
11084          stack.  */
11085       op = DW_OP_constu;
11086 #else
11087       op = DW_OP_plus_uconst;
11088 #endif
11089
11090       loc_descr = new_loc_descr (op, offset, 0);
11091     }
11092
11093   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
11094 }
11095
11096 /* Writes integer values to dw_vec_const array.  */
11097
11098 static void
11099 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
11100 {
11101   while (size != 0)
11102     {
11103       *dest++ = val & 0xff;
11104       val >>= 8;
11105       --size;
11106     }
11107 }
11108
11109 /* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
11110
11111 static HOST_WIDE_INT
11112 extract_int (const unsigned char *src, unsigned int size)
11113 {
11114   HOST_WIDE_INT val = 0;
11115
11116   src += size;
11117   while (size != 0)
11118     {
11119       val <<= 8;
11120       val |= *--src & 0xff;
11121       --size;
11122     }
11123   return val;
11124 }
11125
11126 /* Writes floating point values to dw_vec_const array.  */
11127
11128 static void
11129 insert_float (const_rtx rtl, unsigned char *array)
11130 {
11131   REAL_VALUE_TYPE rv;
11132   long val[4];
11133   int i;
11134
11135   REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
11136   real_to_target (val, &rv, GET_MODE (rtl));
11137
11138   /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
11139   for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
11140     {
11141       insert_int (val[i], 4, array);
11142       array += 4;
11143     }
11144 }
11145
11146 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
11147    does not have a "location" either in memory or in a register.  These
11148    things can arise in GNU C when a constant is passed as an actual parameter
11149    to an inlined function.  They can also arise in C++ where declared
11150    constants do not necessarily get memory "homes".  */
11151
11152 static void
11153 add_const_value_attribute (dw_die_ref die, rtx rtl)
11154 {
11155   switch (GET_CODE (rtl))
11156     {
11157     case CONST_INT:
11158       {
11159         HOST_WIDE_INT val = INTVAL (rtl);
11160
11161         if (val < 0)
11162           add_AT_int (die, DW_AT_const_value, val);
11163         else
11164           add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
11165       }
11166       break;
11167
11168     case CONST_DOUBLE:
11169       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
11170          floating-point constant.  A CONST_DOUBLE is used whenever the
11171          constant requires more than one word in order to be adequately
11172          represented.  We output CONST_DOUBLEs as blocks.  */
11173       {
11174         enum machine_mode mode = GET_MODE (rtl);
11175
11176         if (SCALAR_FLOAT_MODE_P (mode))
11177           {
11178             unsigned int length = GET_MODE_SIZE (mode);
11179             unsigned char *array = GGC_NEWVEC (unsigned char, length);
11180
11181             insert_float (rtl, array);
11182             add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
11183           }
11184         else
11185           {
11186             /* ??? We really should be using HOST_WIDE_INT throughout.  */
11187             gcc_assert (HOST_BITS_PER_LONG == HOST_BITS_PER_WIDE_INT);
11188
11189             add_AT_long_long (die, DW_AT_const_value,
11190                               CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
11191           }
11192       }
11193       break;
11194
11195     case CONST_VECTOR:
11196       {
11197         enum machine_mode mode = GET_MODE (rtl);
11198         unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
11199         unsigned int length = CONST_VECTOR_NUNITS (rtl);
11200         unsigned char *array = GGC_NEWVEC (unsigned char, length * elt_size);
11201         unsigned int i;
11202         unsigned char *p;
11203
11204         switch (GET_MODE_CLASS (mode))
11205           {
11206           case MODE_VECTOR_INT:
11207             for (i = 0, p = array; i < length; i++, p += elt_size)
11208               {
11209                 rtx elt = CONST_VECTOR_ELT (rtl, i);
11210                 HOST_WIDE_INT lo, hi;
11211
11212                 switch (GET_CODE (elt))
11213                   {
11214                   case CONST_INT:
11215                     lo = INTVAL (elt);
11216                     hi = -(lo < 0);
11217                     break;
11218
11219                   case CONST_DOUBLE:
11220                     lo = CONST_DOUBLE_LOW (elt);
11221                     hi = CONST_DOUBLE_HIGH (elt);
11222                     break;
11223
11224                   default:
11225                     gcc_unreachable ();
11226                   }
11227
11228                 if (elt_size <= sizeof (HOST_WIDE_INT))
11229                   insert_int (lo, elt_size, p);
11230                 else
11231                   {
11232                     unsigned char *p0 = p;
11233                     unsigned char *p1 = p + sizeof (HOST_WIDE_INT);
11234
11235                     gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
11236                     if (WORDS_BIG_ENDIAN)
11237                       {
11238                         p0 = p1;
11239                         p1 = p;
11240                       }
11241                     insert_int (lo, sizeof (HOST_WIDE_INT), p0);
11242                     insert_int (hi, sizeof (HOST_WIDE_INT), p1);
11243                   }
11244               }
11245             break;
11246
11247           case MODE_VECTOR_FLOAT:
11248             for (i = 0, p = array; i < length; i++, p += elt_size)
11249               {
11250                 rtx elt = CONST_VECTOR_ELT (rtl, i);
11251                 insert_float (elt, p);
11252               }
11253             break;
11254
11255           default:
11256             gcc_unreachable ();
11257           }
11258
11259         add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
11260       }
11261       break;
11262
11263     case CONST_STRING:
11264       add_AT_string (die, DW_AT_const_value, XSTR (rtl, 0));
11265       break;
11266
11267     case SYMBOL_REF:
11268     case LABEL_REF:
11269     case CONST:
11270       add_AT_addr (die, DW_AT_const_value, rtl);
11271       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
11272       break;
11273
11274     case PLUS:
11275       /* In cases where an inlined instance of an inline function is passed
11276          the address of an `auto' variable (which is local to the caller) we
11277          can get a situation where the DECL_RTL of the artificial local
11278          variable (for the inlining) which acts as a stand-in for the
11279          corresponding formal parameter (of the inline function) will look
11280          like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
11281          exactly a compile-time constant expression, but it isn't the address
11282          of the (artificial) local variable either.  Rather, it represents the
11283          *value* which the artificial local variable always has during its
11284          lifetime.  We currently have no way to represent such quasi-constant
11285          values in Dwarf, so for now we just punt and generate nothing.  */
11286       break;
11287
11288     default:
11289       /* No other kinds of rtx should be possible here.  */
11290       gcc_unreachable ();
11291     }
11292
11293 }
11294
11295 /* Determine whether the evaluation of EXPR references any variables
11296    or functions which aren't otherwise used (and therefore may not be
11297    output).  */
11298 static tree
11299 reference_to_unused (tree * tp, int * walk_subtrees,
11300                      void * data ATTRIBUTE_UNUSED)
11301 {
11302   if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
11303     *walk_subtrees = 0;
11304
11305   if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
11306       && ! TREE_ASM_WRITTEN (*tp))
11307     return *tp;
11308   /* ???  The C++ FE emits debug information for using decls, so
11309      putting gcc_unreachable here falls over.  See PR31899.  For now
11310      be conservative.  */
11311   else if (!cgraph_global_info_ready
11312            && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
11313     return *tp;
11314   else if (DECL_P (*tp) && TREE_CODE (*tp) == VAR_DECL)
11315     {
11316       struct varpool_node *node = varpool_node (*tp);
11317       if (!node->needed)
11318         return *tp;
11319     }
11320   else if (DECL_P (*tp) && TREE_CODE (*tp) == FUNCTION_DECL
11321            && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
11322     {
11323       struct cgraph_node *node = cgraph_node (*tp);
11324       if (!node->output)
11325         return *tp;
11326     }
11327   else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
11328     return *tp;
11329
11330   return NULL_TREE;
11331 }
11332
11333 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
11334    for use in a later add_const_value_attribute call.  */
11335
11336 static rtx
11337 rtl_for_decl_init (tree init, tree type)
11338 {
11339   rtx rtl = NULL_RTX;
11340
11341   /* If a variable is initialized with a string constant without embedded
11342      zeros, build CONST_STRING.  */
11343   if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
11344     {
11345       tree enttype = TREE_TYPE (type);
11346       tree domain = TYPE_DOMAIN (type);
11347       enum machine_mode mode = TYPE_MODE (enttype);
11348
11349       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
11350           && domain
11351           && integer_zerop (TYPE_MIN_VALUE (domain))
11352           && compare_tree_int (TYPE_MAX_VALUE (domain),
11353                                TREE_STRING_LENGTH (init) - 1) == 0
11354           && ((size_t) TREE_STRING_LENGTH (init)
11355               == strlen (TREE_STRING_POINTER (init)) + 1))
11356         rtl = gen_rtx_CONST_STRING (VOIDmode,
11357                                     ggc_strdup (TREE_STRING_POINTER (init)));
11358     }
11359   /* Other aggregates, and complex values, could be represented using
11360      CONCAT: FIXME!  */
11361   else if (AGGREGATE_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
11362     ;
11363   /* Vectors only work if their mode is supported by the target.
11364      FIXME: generic vectors ought to work too.  */
11365   else if (TREE_CODE (type) == VECTOR_TYPE && TYPE_MODE (type) == BLKmode)
11366     ;
11367   /* If the initializer is something that we know will expand into an
11368      immediate RTL constant, expand it now.  We must be careful not to
11369      reference variables which won't be output.  */
11370   else if (initializer_constant_valid_p (init, type)
11371            && ! walk_tree (&init, reference_to_unused, NULL, NULL))
11372     {
11373       /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
11374          possible.  */
11375       if (TREE_CODE (type) == VECTOR_TYPE)
11376         switch (TREE_CODE (init))
11377           {
11378           case VECTOR_CST:
11379             break;
11380           case CONSTRUCTOR:
11381             if (TREE_CONSTANT (init))
11382               {
11383                 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
11384                 bool constant_p = true;
11385                 tree value;
11386                 unsigned HOST_WIDE_INT ix;
11387
11388                 /* Even when ctor is constant, it might contain non-*_CST
11389                    elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
11390                    belong into VECTOR_CST nodes.  */
11391                 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
11392                   if (!CONSTANT_CLASS_P (value))
11393                     {
11394                       constant_p = false;
11395                       break;
11396                     }
11397
11398                 if (constant_p)
11399                   {
11400                     init = build_vector_from_ctor (type, elts);
11401                     break;
11402                   }
11403               }
11404             /* FALLTHRU */
11405
11406           default:
11407             return NULL;
11408           }
11409
11410       rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
11411
11412       /* If expand_expr returns a MEM, it wasn't immediate.  */
11413       gcc_assert (!rtl || !MEM_P (rtl));
11414     }
11415
11416   return rtl;
11417 }
11418
11419 /* Generate RTL for the variable DECL to represent its location.  */
11420
11421 static rtx
11422 rtl_for_decl_location (tree decl)
11423 {
11424   rtx rtl;
11425
11426   /* Here we have to decide where we are going to say the parameter "lives"
11427      (as far as the debugger is concerned).  We only have a couple of
11428      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
11429
11430      DECL_RTL normally indicates where the parameter lives during most of the
11431      activation of the function.  If optimization is enabled however, this
11432      could be either NULL or else a pseudo-reg.  Both of those cases indicate
11433      that the parameter doesn't really live anywhere (as far as the code
11434      generation parts of GCC are concerned) during most of the function's
11435      activation.  That will happen (for example) if the parameter is never
11436      referenced within the function.
11437
11438      We could just generate a location descriptor here for all non-NULL
11439      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
11440      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
11441      where DECL_RTL is NULL or is a pseudo-reg.
11442
11443      Note however that we can only get away with using DECL_INCOMING_RTL as
11444      a backup substitute for DECL_RTL in certain limited cases.  In cases
11445      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
11446      we can be sure that the parameter was passed using the same type as it is
11447      declared to have within the function, and that its DECL_INCOMING_RTL
11448      points us to a place where a value of that type is passed.
11449
11450      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
11451      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
11452      because in these cases DECL_INCOMING_RTL points us to a value of some
11453      type which is *different* from the type of the parameter itself.  Thus,
11454      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
11455      such cases, the debugger would end up (for example) trying to fetch a
11456      `float' from a place which actually contains the first part of a
11457      `double'.  That would lead to really incorrect and confusing
11458      output at debug-time.
11459
11460      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
11461      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
11462      are a couple of exceptions however.  On little-endian machines we can
11463      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
11464      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
11465      an integral type that is smaller than TREE_TYPE (decl). These cases arise
11466      when (on a little-endian machine) a non-prototyped function has a
11467      parameter declared to be of type `short' or `char'.  In such cases,
11468      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
11469      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
11470      passed `int' value.  If the debugger then uses that address to fetch
11471      a `short' or a `char' (on a little-endian machine) the result will be
11472      the correct data, so we allow for such exceptional cases below.
11473
11474      Note that our goal here is to describe the place where the given formal
11475      parameter lives during most of the function's activation (i.e. between the
11476      end of the prologue and the start of the epilogue).  We'll do that as best
11477      as we can. Note however that if the given formal parameter is modified
11478      sometime during the execution of the function, then a stack backtrace (at
11479      debug-time) will show the function as having been called with the *new*
11480      value rather than the value which was originally passed in.  This happens
11481      rarely enough that it is not a major problem, but it *is* a problem, and
11482      I'd like to fix it.
11483
11484      A future version of dwarf2out.c may generate two additional attributes for
11485      any given DW_TAG_formal_parameter DIE which will describe the "passed
11486      type" and the "passed location" for the given formal parameter in addition
11487      to the attributes we now generate to indicate the "declared type" and the
11488      "active location" for each parameter.  This additional set of attributes
11489      could be used by debuggers for stack backtraces. Separately, note that
11490      sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
11491      This happens (for example) for inlined-instances of inline function formal
11492      parameters which are never referenced.  This really shouldn't be
11493      happening.  All PARM_DECL nodes should get valid non-NULL
11494      DECL_INCOMING_RTL values.  FIXME.  */
11495
11496   /* Use DECL_RTL as the "location" unless we find something better.  */
11497   rtl = DECL_RTL_IF_SET (decl);
11498
11499   /* When generating abstract instances, ignore everything except
11500      constants, symbols living in memory, and symbols living in
11501      fixed registers.  */
11502   if (! reload_completed)
11503     {
11504       if (rtl
11505           && (CONSTANT_P (rtl)
11506               || (MEM_P (rtl)
11507                   && CONSTANT_P (XEXP (rtl, 0)))
11508               || (REG_P (rtl)
11509                   && TREE_CODE (decl) == VAR_DECL
11510                   && TREE_STATIC (decl))))
11511         {
11512           rtl = targetm.delegitimize_address (rtl);
11513           return rtl;
11514         }
11515       rtl = NULL_RTX;
11516     }
11517   else if (TREE_CODE (decl) == PARM_DECL)
11518     {
11519       if (rtl == NULL_RTX || is_pseudo_reg (rtl))
11520         {
11521           tree declared_type = TREE_TYPE (decl);
11522           tree passed_type = DECL_ARG_TYPE (decl);
11523           enum machine_mode dmode = TYPE_MODE (declared_type);
11524           enum machine_mode pmode = TYPE_MODE (passed_type);
11525
11526           /* This decl represents a formal parameter which was optimized out.
11527              Note that DECL_INCOMING_RTL may be NULL in here, but we handle
11528              all cases where (rtl == NULL_RTX) just below.  */
11529           if (dmode == pmode)
11530             rtl = DECL_INCOMING_RTL (decl);
11531           else if (SCALAR_INT_MODE_P (dmode)
11532                    && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
11533                    && DECL_INCOMING_RTL (decl))
11534             {
11535               rtx inc = DECL_INCOMING_RTL (decl);
11536               if (REG_P (inc))
11537                 rtl = inc;
11538               else if (MEM_P (inc))
11539                 {
11540                   if (BYTES_BIG_ENDIAN)
11541                     rtl = adjust_address_nv (inc, dmode,
11542                                              GET_MODE_SIZE (pmode)
11543                                              - GET_MODE_SIZE (dmode));
11544                   else
11545                     rtl = inc;
11546                 }
11547             }
11548         }
11549
11550       /* If the parm was passed in registers, but lives on the stack, then
11551          make a big endian correction if the mode of the type of the
11552          parameter is not the same as the mode of the rtl.  */
11553       /* ??? This is the same series of checks that are made in dbxout.c before
11554          we reach the big endian correction code there.  It isn't clear if all
11555          of these checks are necessary here, but keeping them all is the safe
11556          thing to do.  */
11557       else if (MEM_P (rtl)
11558                && XEXP (rtl, 0) != const0_rtx
11559                && ! CONSTANT_P (XEXP (rtl, 0))
11560                /* Not passed in memory.  */
11561                && !MEM_P (DECL_INCOMING_RTL (decl))
11562                /* Not passed by invisible reference.  */
11563                && (!REG_P (XEXP (rtl, 0))
11564                    || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
11565                    || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
11566 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
11567                    || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
11568 #endif
11569                      )
11570                /* Big endian correction check.  */
11571                && BYTES_BIG_ENDIAN
11572                && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
11573                && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
11574                    < UNITS_PER_WORD))
11575         {
11576           int offset = (UNITS_PER_WORD
11577                         - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
11578
11579           rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
11580                              plus_constant (XEXP (rtl, 0), offset));
11581         }
11582     }
11583   else if (TREE_CODE (decl) == VAR_DECL
11584            && rtl
11585            && MEM_P (rtl)
11586            && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
11587            && BYTES_BIG_ENDIAN)
11588     {
11589       int rsize = GET_MODE_SIZE (GET_MODE (rtl));
11590       int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
11591
11592       /* If a variable is declared "register" yet is smaller than
11593          a register, then if we store the variable to memory, it
11594          looks like we're storing a register-sized value, when in
11595          fact we are not.  We need to adjust the offset of the
11596          storage location to reflect the actual value's bytes,
11597          else gdb will not be able to display it.  */
11598       if (rsize > dsize)
11599         rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
11600                            plus_constant (XEXP (rtl, 0), rsize-dsize));
11601     }
11602
11603   /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
11604      and will have been substituted directly into all expressions that use it.
11605      C does not have such a concept, but C++ and other languages do.  */
11606   if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
11607     rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
11608
11609   if (rtl)
11610     rtl = targetm.delegitimize_address (rtl);
11611
11612   /* If we don't look past the constant pool, we risk emitting a
11613      reference to a constant pool entry that isn't referenced from
11614      code, and thus is not emitted.  */
11615   if (rtl)
11616     rtl = avoid_constant_pool_reference (rtl);
11617
11618   return rtl;
11619 }
11620
11621 /* We need to figure out what section we should use as the base for the
11622    address ranges where a given location is valid.
11623    1. If this particular DECL has a section associated with it, use that.
11624    2. If this function has a section associated with it, use that.
11625    3. Otherwise, use the text section.
11626    XXX: If you split a variable across multiple sections, we won't notice.  */
11627
11628 static const char *
11629 secname_for_decl (const_tree decl)
11630 {
11631   const char *secname;
11632
11633   if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
11634     {
11635       tree sectree = DECL_SECTION_NAME (decl);
11636       secname = TREE_STRING_POINTER (sectree);
11637     }
11638   else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
11639     {
11640       tree sectree = DECL_SECTION_NAME (current_function_decl);
11641       secname = TREE_STRING_POINTER (sectree);
11642     }
11643   else if (cfun && in_cold_section_p)
11644     secname = crtl->subsections.cold_section_label;
11645   else
11646     secname = text_section_label;
11647
11648   return secname;
11649 }
11650
11651 /* Check whether decl is a Fortran COMMON symbol.  If not, NULL_TREE is
11652    returned.  If so, the decl for the COMMON block is returned, and the
11653    value is the offset into the common block for the symbol.  */
11654
11655 static tree
11656 fortran_common (tree decl, HOST_WIDE_INT *value)
11657 {
11658   tree val_expr, cvar;
11659   enum machine_mode mode;
11660   HOST_WIDE_INT bitsize, bitpos;
11661   tree offset;
11662   int volatilep = 0, unsignedp = 0;
11663
11664   /* If the decl isn't a VAR_DECL, or if it isn't public or static, or if
11665      it does not have a value (the offset into the common area), or if it
11666      is thread local (as opposed to global) then it isn't common, and shouldn't
11667      be handled as such.  */
11668   if (TREE_CODE (decl) != VAR_DECL
11669       || !TREE_PUBLIC (decl)
11670       || !TREE_STATIC (decl)
11671       || !DECL_HAS_VALUE_EXPR_P (decl)
11672       || !is_fortran ())
11673     return NULL_TREE;
11674
11675   val_expr = DECL_VALUE_EXPR (decl);
11676   if (TREE_CODE (val_expr) != COMPONENT_REF)
11677     return NULL_TREE;
11678
11679   cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
11680                               &mode, &unsignedp, &volatilep, true);
11681
11682   if (cvar == NULL_TREE
11683       || TREE_CODE (cvar) != VAR_DECL
11684       || DECL_ARTIFICIAL (cvar)
11685       || !TREE_PUBLIC (cvar))
11686     return NULL_TREE;
11687
11688   *value = 0;
11689   if (offset != NULL)
11690     {
11691       if (!host_integerp (offset, 0))
11692         return NULL_TREE;
11693       *value = tree_low_cst (offset, 0);
11694     }
11695   if (bitpos != 0)
11696     *value += bitpos / BITS_PER_UNIT;
11697
11698   return cvar;
11699 }
11700
11701 /* Dereference a location expression LOC if DECL is passed by invisible
11702    reference.  */
11703
11704 static dw_loc_descr_ref
11705 loc_by_reference (dw_loc_descr_ref loc, tree decl)
11706 {
11707   HOST_WIDE_INT size;
11708   enum dwarf_location_atom op;
11709
11710   if (loc == NULL)
11711     return NULL;
11712
11713   if ((TREE_CODE (decl) != PARM_DECL && TREE_CODE (decl) != RESULT_DECL)
11714       || !DECL_BY_REFERENCE (decl))
11715     return loc;
11716
11717   size = int_size_in_bytes (TREE_TYPE (decl));
11718   if (size > DWARF2_ADDR_SIZE || size == -1)
11719     return 0;
11720   else if (size == DWARF2_ADDR_SIZE)
11721     op = DW_OP_deref;
11722   else
11723     op = DW_OP_deref_size;
11724   add_loc_descr (&loc, new_loc_descr (op, size, 0));
11725   return loc;
11726 }
11727
11728 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
11729    data attribute for a variable or a parameter.  We generate the
11730    DW_AT_const_value attribute only in those cases where the given variable
11731    or parameter does not have a true "location" either in memory or in a
11732    register.  This can happen (for example) when a constant is passed as an
11733    actual argument in a call to an inline function.  (It's possible that
11734    these things can crop up in other ways also.)  Note that one type of
11735    constant value which can be passed into an inlined function is a constant
11736    pointer.  This can happen for example if an actual argument in an inlined
11737    function call evaluates to a compile-time constant address.  */
11738
11739 static void
11740 add_location_or_const_value_attribute (dw_die_ref die, tree decl,
11741                                        enum dwarf_attribute attr)
11742 {
11743   rtx rtl;
11744   dw_loc_descr_ref descr;
11745   var_loc_list *loc_list;
11746   struct var_loc_node *node;
11747   if (TREE_CODE (decl) == ERROR_MARK)
11748     return;
11749
11750   gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
11751               || TREE_CODE (decl) == RESULT_DECL);
11752
11753   /* See if we possibly have multiple locations for this variable.  */
11754   loc_list = lookup_decl_loc (decl);
11755
11756   /* If it truly has multiple locations, the first and last node will
11757      differ.  */
11758   if (loc_list && loc_list->first != loc_list->last)
11759     {
11760       const char *endname, *secname;
11761       dw_loc_list_ref list;
11762       rtx varloc;
11763       enum var_init_status initialized;
11764
11765       /* Now that we know what section we are using for a base,
11766          actually construct the list of locations.
11767          The first location information is what is passed to the
11768          function that creates the location list, and the remaining
11769          locations just get added on to that list.
11770          Note that we only know the start address for a location
11771          (IE location changes), so to build the range, we use
11772          the range [current location start, next location start].
11773          This means we have to special case the last node, and generate
11774          a range of [last location start, end of function label].  */
11775
11776       node = loc_list->first;
11777       varloc = NOTE_VAR_LOCATION (node->var_loc_note);
11778       secname = secname_for_decl (decl);
11779
11780       if (NOTE_VAR_LOCATION_LOC (node->var_loc_note))
11781         initialized = NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
11782       else
11783         initialized = VAR_INIT_STATUS_INITIALIZED;
11784
11785       descr = loc_by_reference (loc_descriptor (varloc, initialized), decl);
11786       list = new_loc_list (descr, node->label, node->next->label, secname, 1);
11787       node = node->next;
11788
11789       for (; node->next; node = node->next)
11790         if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
11791           {
11792             /* The variable has a location between NODE->LABEL and
11793                NODE->NEXT->LABEL.  */
11794             enum var_init_status initialized =
11795               NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
11796             varloc = NOTE_VAR_LOCATION (node->var_loc_note);
11797             descr = loc_by_reference (loc_descriptor (varloc, initialized),
11798                                       decl);
11799             add_loc_descr_to_loc_list (&list, descr,
11800                                        node->label, node->next->label, secname);
11801           }
11802
11803       /* If the variable has a location at the last label
11804          it keeps its location until the end of function.  */
11805       if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
11806         {
11807           char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
11808           enum var_init_status initialized =
11809             NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
11810
11811           varloc = NOTE_VAR_LOCATION (node->var_loc_note);
11812           if (!current_function_decl)
11813             endname = text_end_label;
11814           else
11815             {
11816               ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
11817                                            current_function_funcdef_no);
11818               endname = ggc_strdup (label_id);
11819             }
11820           descr = loc_by_reference (loc_descriptor (varloc, initialized),
11821                                     decl);
11822           add_loc_descr_to_loc_list (&list, descr,
11823                                      node->label, endname, secname);
11824         }
11825
11826       /* Finally, add the location list to the DIE, and we are done.  */
11827       add_AT_loc_list (die, attr, list);
11828       return;
11829     }
11830
11831   /* Try to get some constant RTL for this decl, and use that as the value of
11832      the location.  */
11833
11834   rtl = rtl_for_decl_location (decl);
11835   if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING))
11836     {
11837       add_const_value_attribute (die, rtl);
11838       return;
11839     }
11840
11841   /* If we have tried to generate the location otherwise, and it
11842      didn't work out (we wouldn't be here if we did), and we have a one entry
11843      location list, try generating a location from that.  */
11844   if (loc_list && loc_list->first)
11845     {
11846       enum var_init_status status;
11847       node = loc_list->first;
11848       status = NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
11849       descr = loc_descriptor (NOTE_VAR_LOCATION (node->var_loc_note), status);
11850       if (descr)
11851         {
11852           descr = loc_by_reference (descr, decl);
11853           add_AT_location_description (die, attr, descr);
11854           return;
11855         }
11856     }
11857
11858   /* We couldn't get any rtl, so try directly generating the location
11859      description from the tree.  */
11860   descr = loc_descriptor_from_tree (decl);
11861   if (descr)
11862     {
11863       descr = loc_by_reference (descr, decl);
11864       add_AT_location_description (die, attr, descr);
11865       return;
11866     }
11867   /* None of that worked, so it must not really have a location;
11868      try adding a constant value attribute from the DECL_INITIAL.  */
11869   tree_add_const_value_attribute (die, decl);
11870 }
11871
11872 /* Add VARIABLE and DIE into deferred locations list.  */
11873
11874 static void
11875 defer_location (tree variable, dw_die_ref die)
11876 {
11877   deferred_locations entry;
11878   entry.variable = variable;
11879   entry.die = die;
11880   VEC_safe_push (deferred_locations, gc, deferred_locations_list, &entry);
11881 }
11882
11883 /* Helper function for tree_add_const_value_attribute.  Natively encode
11884    initializer INIT into an array.  Return true if successful.  */
11885
11886 static bool
11887 native_encode_initializer (tree init, unsigned char *array, int size)
11888 {
11889   tree type;
11890
11891   if (init == NULL_TREE)
11892     return false;
11893
11894   STRIP_NOPS (init);
11895   switch (TREE_CODE (init))
11896     {
11897     case STRING_CST:
11898       type = TREE_TYPE (init);
11899       if (TREE_CODE (type) == ARRAY_TYPE)
11900         {
11901           tree enttype = TREE_TYPE (type);
11902           enum machine_mode mode = TYPE_MODE (enttype);
11903
11904           if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
11905             return false;
11906           if (int_size_in_bytes (type) != size)
11907             return false;
11908           if (size > TREE_STRING_LENGTH (init))
11909             {
11910               memcpy (array, TREE_STRING_POINTER (init),
11911                       TREE_STRING_LENGTH (init));
11912               memset (array + TREE_STRING_LENGTH (init),
11913                       '\0', size - TREE_STRING_LENGTH (init));
11914             }
11915           else
11916             memcpy (array, TREE_STRING_POINTER (init), size);
11917           return true;
11918         }
11919       return false;
11920     case CONSTRUCTOR:
11921       type = TREE_TYPE (init);
11922       if (int_size_in_bytes (type) != size)
11923         return false;
11924       if (TREE_CODE (type) == ARRAY_TYPE)
11925         {
11926           HOST_WIDE_INT min_index;
11927           unsigned HOST_WIDE_INT cnt;
11928           int curpos = 0, fieldsize;
11929           constructor_elt *ce;
11930
11931           if (TYPE_DOMAIN (type) == NULL_TREE
11932               || !host_integerp (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0))
11933             return false;
11934
11935           fieldsize = int_size_in_bytes (TREE_TYPE (type));
11936           if (fieldsize <= 0)
11937             return false;
11938
11939           min_index = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0);
11940           memset (array, '\0', size);
11941           for (cnt = 0;
11942                VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce);
11943                cnt++)
11944             {
11945               tree val = ce->value;
11946               tree index = ce->index;
11947               int pos = curpos;
11948               if (index && TREE_CODE (index) == RANGE_EXPR)
11949                 pos = (tree_low_cst (TREE_OPERAND (index, 0), 0) - min_index)
11950                       * fieldsize;
11951               else if (index)
11952                 pos = (tree_low_cst (index, 0) - min_index) * fieldsize;
11953
11954               if (val)
11955                 {
11956                   STRIP_NOPS (val);
11957                   if (!native_encode_initializer (val, array + pos, fieldsize))
11958                     return false;
11959                 }
11960               curpos = pos + fieldsize;
11961               if (index && TREE_CODE (index) == RANGE_EXPR)
11962                 {
11963                   int count = tree_low_cst (TREE_OPERAND (index, 1), 0)
11964                               - tree_low_cst (TREE_OPERAND (index, 0), 0);
11965                   while (count > 0)
11966                     {
11967                       if (val)
11968                         memcpy (array + curpos, array + pos, fieldsize);
11969                       curpos += fieldsize;
11970                     }
11971                 }
11972               gcc_assert (curpos <= size);
11973             }
11974           return true;
11975         }
11976       else if (TREE_CODE (type) == RECORD_TYPE
11977                || TREE_CODE (type) == UNION_TYPE)
11978         {
11979           tree field = NULL_TREE;
11980           unsigned HOST_WIDE_INT cnt;
11981           constructor_elt *ce;
11982
11983           if (int_size_in_bytes (type) != size)
11984             return false;
11985
11986           if (TREE_CODE (type) == RECORD_TYPE)
11987             field = TYPE_FIELDS (type);
11988
11989           for (cnt = 0;
11990                VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce);
11991                cnt++, field = field ? TREE_CHAIN (field) : 0)
11992             {
11993               tree val = ce->value;
11994               int pos, fieldsize;
11995
11996               if (ce->index != 0)
11997                 field = ce->index;
11998
11999               if (val)
12000                 STRIP_NOPS (val);
12001
12002               if (field == NULL_TREE || DECL_BIT_FIELD (field))
12003                 return false;
12004
12005               if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
12006                   && TYPE_DOMAIN (TREE_TYPE (field))
12007                   && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
12008                 return false;
12009               else if (DECL_SIZE_UNIT (field) == NULL_TREE
12010                        || !host_integerp (DECL_SIZE_UNIT (field), 0))
12011                 return false;
12012               fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 0);
12013               pos = int_byte_position (field);
12014               gcc_assert (pos + fieldsize <= size);
12015               if (val
12016                   && !native_encode_initializer (val, array + pos, fieldsize))
12017                 return false;
12018             }
12019           return true;
12020         }
12021       return false;
12022     case VIEW_CONVERT_EXPR:
12023     case NON_LVALUE_EXPR:
12024       return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
12025     default:
12026       return native_encode_expr (init, array, size) == size;
12027     }
12028 }
12029
12030 /* If we don't have a copy of this variable in memory for some reason (such
12031    as a C++ member constant that doesn't have an out-of-line definition),
12032    we should tell the debugger about the constant value.  */
12033
12034 static void
12035 tree_add_const_value_attribute (dw_die_ref var_die, tree decl)
12036 {
12037   tree init;
12038   tree type = TREE_TYPE (decl);
12039   rtx rtl;
12040
12041   if (TREE_CODE (decl) != VAR_DECL && TREE_CODE (decl) != CONST_DECL)
12042     return;
12043
12044   init = DECL_INITIAL (decl);
12045   if (TREE_READONLY (decl) && ! TREE_THIS_VOLATILE (decl) && init)
12046     /* OK */;
12047   else
12048     return;
12049
12050   rtl = rtl_for_decl_init (init, type);
12051   if (rtl)
12052     add_const_value_attribute (var_die, rtl);
12053   /* If the host and target are sane, try harder.  */
12054   else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
12055            && initializer_constant_valid_p (init, type))
12056     {
12057       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
12058       if (size > 0 && (int) size == size)
12059         {
12060           unsigned char *array = GGC_CNEWVEC (unsigned char, size);
12061
12062           if (native_encode_initializer (init, array, size))
12063             add_AT_vec (var_die, DW_AT_const_value, size, 1, array);
12064         }
12065     }
12066 }
12067
12068 /* Convert the CFI instructions for the current function into a
12069    location list.  This is used for DW_AT_frame_base when we targeting
12070    a dwarf2 consumer that does not support the dwarf3
12071    DW_OP_call_frame_cfa.  OFFSET is a constant to be added to all CFA
12072    expressions.  */
12073
12074 static dw_loc_list_ref
12075 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
12076 {
12077   dw_fde_ref fde;
12078   dw_loc_list_ref list, *list_tail;
12079   dw_cfi_ref cfi;
12080   dw_cfa_location last_cfa, next_cfa;
12081   const char *start_label, *last_label, *section;
12082
12083   fde = current_fde ();
12084   gcc_assert (fde != NULL);
12085
12086   section = secname_for_decl (current_function_decl);
12087   list_tail = &list;
12088   list = NULL;
12089
12090   next_cfa.reg = INVALID_REGNUM;
12091   next_cfa.offset = 0;
12092   next_cfa.indirect = 0;
12093   next_cfa.base_offset = 0;
12094
12095   start_label = fde->dw_fde_begin;
12096
12097   /* ??? Bald assumption that the CIE opcode list does not contain
12098      advance opcodes.  */
12099   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
12100     lookup_cfa_1 (cfi, &next_cfa);
12101
12102   last_cfa = next_cfa;
12103   last_label = start_label;
12104
12105   for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
12106     switch (cfi->dw_cfi_opc)
12107       {
12108       case DW_CFA_set_loc:
12109       case DW_CFA_advance_loc1:
12110       case DW_CFA_advance_loc2:
12111       case DW_CFA_advance_loc4:
12112         if (!cfa_equal_p (&last_cfa, &next_cfa))
12113           {
12114             *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
12115                                        start_label, last_label, section,
12116                                        list == NULL);
12117
12118             list_tail = &(*list_tail)->dw_loc_next;
12119             last_cfa = next_cfa;
12120             start_label = last_label;
12121           }
12122         last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
12123         break;
12124
12125       case DW_CFA_advance_loc:
12126         /* The encoding is complex enough that we should never emit this.  */
12127       case DW_CFA_remember_state:
12128       case DW_CFA_restore_state:
12129         /* We don't handle these two in this function.  It would be possible
12130            if it were to be required.  */
12131         gcc_unreachable ();
12132
12133       default:
12134         lookup_cfa_1 (cfi, &next_cfa);
12135         break;
12136       }
12137
12138   if (!cfa_equal_p (&last_cfa, &next_cfa))
12139     {
12140       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
12141                                  start_label, last_label, section,
12142                                  list == NULL);
12143       list_tail = &(*list_tail)->dw_loc_next;
12144       start_label = last_label;
12145     }
12146   *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
12147                              start_label, fde->dw_fde_end, section,
12148                              list == NULL);
12149
12150   return list;
12151 }
12152
12153 /* Compute a displacement from the "steady-state frame pointer" to the
12154    frame base (often the same as the CFA), and store it in
12155    frame_pointer_fb_offset.  OFFSET is added to the displacement
12156    before the latter is negated.  */
12157
12158 static void
12159 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
12160 {
12161   rtx reg, elim;
12162
12163 #ifdef FRAME_POINTER_CFA_OFFSET
12164   reg = frame_pointer_rtx;
12165   offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
12166 #else
12167   reg = arg_pointer_rtx;
12168   offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
12169 #endif
12170
12171   elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
12172   if (GET_CODE (elim) == PLUS)
12173     {
12174       offset += INTVAL (XEXP (elim, 1));
12175       elim = XEXP (elim, 0);
12176     }
12177
12178   gcc_assert ((SUPPORTS_STACK_ALIGNMENT
12179                && (elim == hard_frame_pointer_rtx
12180                    || elim == stack_pointer_rtx))
12181               || elim == (frame_pointer_needed
12182                           ? hard_frame_pointer_rtx
12183                           : stack_pointer_rtx));
12184
12185   frame_pointer_fb_offset = -offset;
12186 }
12187
12188 /* Generate a DW_AT_name attribute given some string value to be included as
12189    the value of the attribute.  */
12190
12191 static void
12192 add_name_attribute (dw_die_ref die, const char *name_string)
12193 {
12194   if (name_string != NULL && *name_string != 0)
12195     {
12196       if (demangle_name_func)
12197         name_string = (*demangle_name_func) (name_string);
12198
12199       add_AT_string (die, DW_AT_name, name_string);
12200     }
12201 }
12202
12203 /* Generate a DW_AT_comp_dir attribute for DIE.  */
12204
12205 static void
12206 add_comp_dir_attribute (dw_die_ref die)
12207 {
12208   const char *wd = get_src_pwd ();
12209   if (wd != NULL)
12210     add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
12211 }
12212
12213 /* Given a tree node describing an array bound (either lower or upper) output
12214    a representation for that bound.  */
12215
12216 static void
12217 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
12218 {
12219   switch (TREE_CODE (bound))
12220     {
12221     case ERROR_MARK:
12222       return;
12223
12224     /* All fixed-bounds are represented by INTEGER_CST nodes.  */
12225     case INTEGER_CST:
12226       if (! host_integerp (bound, 0)
12227           || (bound_attr == DW_AT_lower_bound
12228               && (((is_c_family () || is_java ()) &&  integer_zerop (bound))
12229                   || (is_fortran () && integer_onep (bound)))))
12230         /* Use the default.  */
12231         ;
12232       else
12233         add_AT_unsigned (subrange_die, bound_attr, tree_low_cst (bound, 0));
12234       break;
12235
12236     CASE_CONVERT:
12237     case VIEW_CONVERT_EXPR:
12238       add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
12239       break;
12240
12241     case SAVE_EXPR:
12242       break;
12243
12244     case VAR_DECL:
12245     case PARM_DECL:
12246     case RESULT_DECL:
12247       {
12248         dw_die_ref decl_die = lookup_decl_die (bound);
12249         dw_loc_descr_ref loc;
12250
12251         /* ??? Can this happen, or should the variable have been bound
12252            first?  Probably it can, since I imagine that we try to create
12253            the types of parameters in the order in which they exist in
12254            the list, and won't have created a forward reference to a
12255            later parameter.  */
12256         if (decl_die != NULL)
12257           add_AT_die_ref (subrange_die, bound_attr, decl_die);
12258         else
12259           {
12260             loc = loc_descriptor_from_tree_1 (bound, 0);
12261             add_AT_location_description (subrange_die, bound_attr, loc);
12262           }
12263         break;
12264       }
12265
12266     default:
12267       {
12268         /* Otherwise try to create a stack operation procedure to
12269            evaluate the value of the array bound.  */
12270
12271         dw_die_ref ctx, decl_die;
12272         dw_loc_descr_ref loc;
12273
12274         loc = loc_descriptor_from_tree (bound);
12275         if (loc == NULL)
12276           break;
12277
12278         if (current_function_decl == 0)
12279           ctx = comp_unit_die;
12280         else
12281           ctx = lookup_decl_die (current_function_decl);
12282
12283         decl_die = new_die (DW_TAG_variable, ctx, bound);
12284         add_AT_flag (decl_die, DW_AT_artificial, 1);
12285         add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
12286         add_AT_loc (decl_die, DW_AT_location, loc);
12287
12288         add_AT_die_ref (subrange_die, bound_attr, decl_die);
12289         break;
12290       }
12291     }
12292 }
12293
12294 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
12295    possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
12296    Note that the block of subscript information for an array type also
12297    includes information about the element type of the given array type.  */
12298
12299 static void
12300 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
12301 {
12302   unsigned dimension_number;
12303   tree lower, upper;
12304   dw_die_ref subrange_die;
12305
12306   for (dimension_number = 0;
12307        TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
12308        type = TREE_TYPE (type), dimension_number++)
12309     {
12310       tree domain = TYPE_DOMAIN (type);
12311
12312       if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
12313         break;
12314
12315       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
12316          and (in GNU C only) variable bounds.  Handle all three forms
12317          here.  */
12318       subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
12319       if (domain)
12320         {
12321           /* We have an array type with specified bounds.  */
12322           lower = TYPE_MIN_VALUE (domain);
12323           upper = TYPE_MAX_VALUE (domain);
12324
12325           /* Define the index type.  */
12326           if (TREE_TYPE (domain))
12327             {
12328               /* ??? This is probably an Ada unnamed subrange type.  Ignore the
12329                  TREE_TYPE field.  We can't emit debug info for this
12330                  because it is an unnamed integral type.  */
12331               if (TREE_CODE (domain) == INTEGER_TYPE
12332                   && TYPE_NAME (domain) == NULL_TREE
12333                   && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
12334                   && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
12335                 ;
12336               else
12337                 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
12338                                     type_die);
12339             }
12340
12341           /* ??? If upper is NULL, the array has unspecified length,
12342              but it does have a lower bound.  This happens with Fortran
12343                dimension arr(N:*)
12344              Since the debugger is definitely going to need to know N
12345              to produce useful results, go ahead and output the lower
12346              bound solo, and hope the debugger can cope.  */
12347
12348           add_bound_info (subrange_die, DW_AT_lower_bound, lower);
12349           if (upper)
12350             add_bound_info (subrange_die, DW_AT_upper_bound, upper);
12351         }
12352
12353       /* Otherwise we have an array type with an unspecified length.  The
12354          DWARF-2 spec does not say how to handle this; let's just leave out the
12355          bounds.  */
12356     }
12357 }
12358
12359 static void
12360 add_byte_size_attribute (dw_die_ref die, tree tree_node)
12361 {
12362   unsigned size;
12363
12364   switch (TREE_CODE (tree_node))
12365     {
12366     case ERROR_MARK:
12367       size = 0;
12368       break;
12369     case ENUMERAL_TYPE:
12370     case RECORD_TYPE:
12371     case UNION_TYPE:
12372     case QUAL_UNION_TYPE:
12373       size = int_size_in_bytes (tree_node);
12374       break;
12375     case FIELD_DECL:
12376       /* For a data member of a struct or union, the DW_AT_byte_size is
12377          generally given as the number of bytes normally allocated for an
12378          object of the *declared* type of the member itself.  This is true
12379          even for bit-fields.  */
12380       size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
12381       break;
12382     default:
12383       gcc_unreachable ();
12384     }
12385
12386   /* Note that `size' might be -1 when we get to this point.  If it is, that
12387      indicates that the byte size of the entity in question is variable.  We
12388      have no good way of expressing this fact in Dwarf at the present time,
12389      so just let the -1 pass on through.  */
12390   add_AT_unsigned (die, DW_AT_byte_size, size);
12391 }
12392
12393 /* For a FIELD_DECL node which represents a bit-field, output an attribute
12394    which specifies the distance in bits from the highest order bit of the
12395    "containing object" for the bit-field to the highest order bit of the
12396    bit-field itself.
12397
12398    For any given bit-field, the "containing object" is a hypothetical object
12399    (of some integral or enum type) within which the given bit-field lives.  The
12400    type of this hypothetical "containing object" is always the same as the
12401    declared type of the individual bit-field itself.  The determination of the
12402    exact location of the "containing object" for a bit-field is rather
12403    complicated.  It's handled by the `field_byte_offset' function (above).
12404
12405    Note that it is the size (in bytes) of the hypothetical "containing object"
12406    which will be given in the DW_AT_byte_size attribute for this bit-field.
12407    (See `byte_size_attribute' above).  */
12408
12409 static inline void
12410 add_bit_offset_attribute (dw_die_ref die, tree decl)
12411 {
12412   HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
12413   tree type = DECL_BIT_FIELD_TYPE (decl);
12414   HOST_WIDE_INT bitpos_int;
12415   HOST_WIDE_INT highest_order_object_bit_offset;
12416   HOST_WIDE_INT highest_order_field_bit_offset;
12417   HOST_WIDE_INT unsigned bit_offset;
12418
12419   /* Must be a field and a bit field.  */
12420   gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
12421
12422   /* We can't yet handle bit-fields whose offsets are variable, so if we
12423      encounter such things, just return without generating any attribute
12424      whatsoever.  Likewise for variable or too large size.  */
12425   if (! host_integerp (bit_position (decl), 0)
12426       || ! host_integerp (DECL_SIZE (decl), 1))
12427     return;
12428
12429   bitpos_int = int_bit_position (decl);
12430
12431   /* Note that the bit offset is always the distance (in bits) from the
12432      highest-order bit of the "containing object" to the highest-order bit of
12433      the bit-field itself.  Since the "high-order end" of any object or field
12434      is different on big-endian and little-endian machines, the computation
12435      below must take account of these differences.  */
12436   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
12437   highest_order_field_bit_offset = bitpos_int;
12438
12439   if (! BYTES_BIG_ENDIAN)
12440     {
12441       highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
12442       highest_order_object_bit_offset += simple_type_size_in_bits (type);
12443     }
12444
12445   bit_offset
12446     = (! BYTES_BIG_ENDIAN
12447        ? highest_order_object_bit_offset - highest_order_field_bit_offset
12448        : highest_order_field_bit_offset - highest_order_object_bit_offset);
12449
12450   add_AT_unsigned (die, DW_AT_bit_offset, bit_offset);
12451 }
12452
12453 /* For a FIELD_DECL node which represents a bit field, output an attribute
12454    which specifies the length in bits of the given field.  */
12455
12456 static inline void
12457 add_bit_size_attribute (dw_die_ref die, tree decl)
12458 {
12459   /* Must be a field and a bit field.  */
12460   gcc_assert (TREE_CODE (decl) == FIELD_DECL
12461               && DECL_BIT_FIELD_TYPE (decl));
12462
12463   if (host_integerp (DECL_SIZE (decl), 1))
12464     add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
12465 }
12466
12467 /* If the compiled language is ANSI C, then add a 'prototyped'
12468    attribute, if arg types are given for the parameters of a function.  */
12469
12470 static inline void
12471 add_prototyped_attribute (dw_die_ref die, tree func_type)
12472 {
12473   if (get_AT_unsigned (comp_unit_die, DW_AT_language) == DW_LANG_C89
12474       && TYPE_ARG_TYPES (func_type) != NULL)
12475     add_AT_flag (die, DW_AT_prototyped, 1);
12476 }
12477
12478 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
12479    by looking in either the type declaration or object declaration
12480    equate table.  */
12481
12482 static inline void
12483 add_abstract_origin_attribute (dw_die_ref die, tree origin)
12484 {
12485   dw_die_ref origin_die = NULL;
12486
12487   if (TREE_CODE (origin) != FUNCTION_DECL)
12488     {
12489       /* We may have gotten separated from the block for the inlined
12490          function, if we're in an exception handler or some such; make
12491          sure that the abstract function has been written out.
12492
12493          Doing this for nested functions is wrong, however; functions are
12494          distinct units, and our context might not even be inline.  */
12495       tree fn = origin;
12496
12497       if (TYPE_P (fn))
12498         fn = TYPE_STUB_DECL (fn);
12499
12500       fn = decl_function_context (fn);
12501       if (fn)
12502         dwarf2out_abstract_function (fn);
12503     }
12504
12505   if (DECL_P (origin))
12506     origin_die = lookup_decl_die (origin);
12507   else if (TYPE_P (origin))
12508     origin_die = lookup_type_die (origin);
12509
12510   /* XXX: Functions that are never lowered don't always have correct block
12511      trees (in the case of java, they simply have no block tree, in some other
12512      languages).  For these functions, there is nothing we can really do to
12513      output correct debug info for inlined functions in all cases.  Rather
12514      than die, we'll just produce deficient debug info now, in that we will
12515      have variables without a proper abstract origin.  In the future, when all
12516      functions are lowered, we should re-add a gcc_assert (origin_die)
12517      here.  */
12518
12519   if (origin_die)
12520       add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
12521 }
12522
12523 /* We do not currently support the pure_virtual attribute.  */
12524
12525 static inline void
12526 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
12527 {
12528   if (DECL_VINDEX (func_decl))
12529     {
12530       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
12531
12532       if (host_integerp (DECL_VINDEX (func_decl), 0))
12533         add_AT_loc (die, DW_AT_vtable_elem_location,
12534                     new_loc_descr (DW_OP_constu,
12535                                    tree_low_cst (DECL_VINDEX (func_decl), 0),
12536                                    0));
12537
12538       /* GNU extension: Record what type this method came from originally.  */
12539       if (debug_info_level > DINFO_LEVEL_TERSE)
12540         add_AT_die_ref (die, DW_AT_containing_type,
12541                         lookup_type_die (DECL_CONTEXT (func_decl)));
12542     }
12543 }
12544 \f
12545 /* Add source coordinate attributes for the given decl.  */
12546
12547 static void
12548 add_src_coords_attributes (dw_die_ref die, tree decl)
12549 {
12550   expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
12551
12552   add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
12553   add_AT_unsigned (die, DW_AT_decl_line, s.line);
12554 }
12555
12556 /* Add a DW_AT_name attribute and source coordinate attribute for the
12557    given decl, but only if it actually has a name.  */
12558
12559 static void
12560 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
12561 {
12562   tree decl_name;
12563
12564   decl_name = DECL_NAME (decl);
12565   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
12566     {
12567       add_name_attribute (die, dwarf2_name (decl, 0));
12568       if (! DECL_ARTIFICIAL (decl))
12569         add_src_coords_attributes (die, decl);
12570
12571       if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
12572           && TREE_PUBLIC (decl)
12573           && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
12574           && !DECL_ABSTRACT (decl)
12575           && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
12576           && !is_fortran ())
12577         add_AT_string (die, DW_AT_MIPS_linkage_name,
12578                        IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
12579     }
12580
12581 #ifdef VMS_DEBUGGING_INFO
12582   /* Get the function's name, as described by its RTL.  This may be different
12583      from the DECL_NAME name used in the source file.  */
12584   if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
12585     {
12586       add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
12587                    XEXP (DECL_RTL (decl), 0));
12588       VEC_safe_push (tree, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
12589     }
12590 #endif
12591 }
12592
12593 /* Push a new declaration scope.  */
12594
12595 static void
12596 push_decl_scope (tree scope)
12597 {
12598   VEC_safe_push (tree, gc, decl_scope_table, scope);
12599 }
12600
12601 /* Pop a declaration scope.  */
12602
12603 static inline void
12604 pop_decl_scope (void)
12605 {
12606   VEC_pop (tree, decl_scope_table);
12607 }
12608
12609 /* Return the DIE for the scope that immediately contains this type.
12610    Non-named types get global scope.  Named types nested in other
12611    types get their containing scope if it's open, or global scope
12612    otherwise.  All other types (i.e. function-local named types) get
12613    the current active scope.  */
12614
12615 static dw_die_ref
12616 scope_die_for (tree t, dw_die_ref context_die)
12617 {
12618   dw_die_ref scope_die = NULL;
12619   tree containing_scope;
12620   int i;
12621
12622   /* Non-types always go in the current scope.  */
12623   gcc_assert (TYPE_P (t));
12624
12625   containing_scope = TYPE_CONTEXT (t);
12626
12627   /* Use the containing namespace if it was passed in (for a declaration).  */
12628   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
12629     {
12630       if (context_die == lookup_decl_die (containing_scope))
12631         /* OK */;
12632       else
12633         containing_scope = NULL_TREE;
12634     }
12635
12636   /* Ignore function type "scopes" from the C frontend.  They mean that
12637      a tagged type is local to a parmlist of a function declarator, but
12638      that isn't useful to DWARF.  */
12639   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
12640     containing_scope = NULL_TREE;
12641
12642   if (containing_scope == NULL_TREE)
12643     scope_die = comp_unit_die;
12644   else if (TYPE_P (containing_scope))
12645     {
12646       /* For types, we can just look up the appropriate DIE.  But
12647          first we check to see if we're in the middle of emitting it
12648          so we know where the new DIE should go.  */
12649       for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
12650         if (VEC_index (tree, decl_scope_table, i) == containing_scope)
12651           break;
12652
12653       if (i < 0)
12654         {
12655           gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
12656                       || TREE_ASM_WRITTEN (containing_scope));
12657
12658           /* If none of the current dies are suitable, we get file scope.  */
12659           scope_die = comp_unit_die;
12660         }
12661       else
12662         scope_die = lookup_type_die (containing_scope);
12663     }
12664   else
12665     scope_die = context_die;
12666
12667   return scope_die;
12668 }
12669
12670 /* Returns nonzero if CONTEXT_DIE is internal to a function.  */
12671
12672 static inline int
12673 local_scope_p (dw_die_ref context_die)
12674 {
12675   for (; context_die; context_die = context_die->die_parent)
12676     if (context_die->die_tag == DW_TAG_inlined_subroutine
12677         || context_die->die_tag == DW_TAG_subprogram)
12678       return 1;
12679
12680   return 0;
12681 }
12682
12683 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
12684    whether or not to treat a DIE in this context as a declaration.  */
12685
12686 static inline int
12687 class_or_namespace_scope_p (dw_die_ref context_die)
12688 {
12689   return (context_die
12690           && (context_die->die_tag == DW_TAG_structure_type
12691               || context_die->die_tag == DW_TAG_class_type
12692               || context_die->die_tag == DW_TAG_interface_type
12693               || context_die->die_tag == DW_TAG_union_type
12694               || context_die->die_tag == DW_TAG_namespace));
12695 }
12696
12697 /* Many forms of DIEs require a "type description" attribute.  This
12698    routine locates the proper "type descriptor" die for the type given
12699    by 'type', and adds a DW_AT_type attribute below the given die.  */
12700
12701 static void
12702 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
12703                     int decl_volatile, dw_die_ref context_die)
12704 {
12705   enum tree_code code  = TREE_CODE (type);
12706   dw_die_ref type_die  = NULL;
12707
12708   /* ??? If this type is an unnamed subrange type of an integral, floating-point
12709      or fixed-point type, use the inner type.  This is because we have no
12710      support for unnamed types in base_type_die.  This can happen if this is
12711      an Ada subrange type.  Correct solution is emit a subrange type die.  */
12712   if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
12713       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
12714     type = TREE_TYPE (type), code = TREE_CODE (type);
12715
12716   if (code == ERROR_MARK
12717       /* Handle a special case.  For functions whose return type is void, we
12718          generate *no* type attribute.  (Note that no object may have type
12719          `void', so this only applies to function return types).  */
12720       || code == VOID_TYPE)
12721     return;
12722
12723   type_die = modified_type_die (type,
12724                                 decl_const || TYPE_READONLY (type),
12725                                 decl_volatile || TYPE_VOLATILE (type),
12726                                 context_die);
12727
12728   if (type_die != NULL)
12729     add_AT_die_ref (object_die, DW_AT_type, type_die);
12730 }
12731
12732 /* Given an object die, add the calling convention attribute for the
12733    function call type.  */
12734 static void
12735 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
12736 {
12737   enum dwarf_calling_convention value = DW_CC_normal;
12738
12739   value = targetm.dwarf_calling_convention (TREE_TYPE (decl));
12740
12741   /* DWARF doesn't provide a way to identify a program's source-level
12742      entry point.  DW_AT_calling_convention attributes are only meant
12743      to describe functions' calling conventions.  However, lacking a
12744      better way to signal the Fortran main program, we use this for the
12745      time being, following existing custom.  */
12746   if (is_fortran ()
12747       && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
12748     value = DW_CC_program;
12749
12750   /* Only add the attribute if the backend requests it, and
12751      is not DW_CC_normal.  */
12752   if (value && (value != DW_CC_normal))
12753     add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
12754 }
12755
12756 /* Given a tree pointer to a struct, class, union, or enum type node, return
12757    a pointer to the (string) tag name for the given type, or zero if the type
12758    was declared without a tag.  */
12759
12760 static const char *
12761 type_tag (const_tree type)
12762 {
12763   const char *name = 0;
12764
12765   if (TYPE_NAME (type) != 0)
12766     {
12767       tree t = 0;
12768
12769       /* Find the IDENTIFIER_NODE for the type name.  */
12770       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
12771         t = TYPE_NAME (type);
12772
12773       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
12774          a TYPE_DECL node, regardless of whether or not a `typedef' was
12775          involved.  */
12776       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12777                && ! DECL_IGNORED_P (TYPE_NAME (type)))
12778         {
12779           /* We want to be extra verbose.  Don't call dwarf_name if
12780              DECL_NAME isn't set.  The default hook for decl_printable_name
12781              doesn't like that, and in this context it's correct to return
12782              0, instead of "<anonymous>" or the like.  */
12783           if (DECL_NAME (TYPE_NAME (type)))
12784             name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
12785         }
12786
12787       /* Now get the name as a string, or invent one.  */
12788       if (!name && t != 0)
12789         name = IDENTIFIER_POINTER (t);
12790     }
12791
12792   return (name == 0 || *name == '\0') ? 0 : name;
12793 }
12794
12795 /* Return the type associated with a data member, make a special check
12796    for bit field types.  */
12797
12798 static inline tree
12799 member_declared_type (const_tree member)
12800 {
12801   return (DECL_BIT_FIELD_TYPE (member)
12802           ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
12803 }
12804
12805 /* Get the decl's label, as described by its RTL. This may be different
12806    from the DECL_NAME name used in the source file.  */
12807
12808 #if 0
12809 static const char *
12810 decl_start_label (tree decl)
12811 {
12812   rtx x;
12813   const char *fnname;
12814
12815   x = DECL_RTL (decl);
12816   gcc_assert (MEM_P (x));
12817
12818   x = XEXP (x, 0);
12819   gcc_assert (GET_CODE (x) == SYMBOL_REF);
12820
12821   fnname = XSTR (x, 0);
12822   return fnname;
12823 }
12824 #endif
12825 \f
12826 /* These routines generate the internal representation of the DIE's for
12827    the compilation unit.  Debugging information is collected by walking
12828    the declaration trees passed in from dwarf2out_decl().  */
12829
12830 static void
12831 gen_array_type_die (tree type, dw_die_ref context_die)
12832 {
12833   dw_die_ref scope_die = scope_die_for (type, context_die);
12834   dw_die_ref array_die;
12835
12836   /* GNU compilers represent multidimensional array types as sequences of one
12837      dimensional array types whose element types are themselves array types.
12838      We sometimes squish that down to a single array_type DIE with multiple
12839      subscripts in the Dwarf debugging info.  The draft Dwarf specification
12840      say that we are allowed to do this kind of compression in C, because
12841      there is no difference between an array of arrays and a multidimensional
12842      array.  We don't do this for Ada to remain as close as possible to the
12843      actual representation, which is especially important against the language
12844      flexibilty wrt arrays of variable size.  */
12845
12846   bool collapse_nested_arrays = !is_ada ();
12847   tree element_type;
12848
12849   /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
12850      DW_TAG_string_type doesn't have DW_AT_type attribute).  */
12851   if (TYPE_STRING_FLAG (type)
12852       && TREE_CODE (type) == ARRAY_TYPE
12853       && is_fortran ()
12854       && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
12855     {
12856       HOST_WIDE_INT size;
12857
12858       array_die = new_die (DW_TAG_string_type, scope_die, type);
12859       add_name_attribute (array_die, type_tag (type));
12860       equate_type_number_to_die (type, array_die);
12861       size = int_size_in_bytes (type);
12862       if (size >= 0)
12863         add_AT_unsigned (array_die, DW_AT_byte_size, size);
12864       else if (TYPE_DOMAIN (type) != NULL_TREE
12865                && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
12866                && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
12867         {
12868           tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
12869           dw_loc_descr_ref loc = loc_descriptor_from_tree (szdecl);
12870
12871           size = int_size_in_bytes (TREE_TYPE (szdecl));
12872           if (loc && size > 0)
12873             {
12874               add_AT_loc (array_die, DW_AT_string_length, loc);
12875               if (size != DWARF2_ADDR_SIZE)
12876                 add_AT_unsigned (array_die, DW_AT_byte_size, size);
12877             }
12878         }
12879       return;
12880     }
12881
12882   /* ??? The SGI dwarf reader fails for array of array of enum types
12883      (e.g. const enum machine_mode insn_operand_mode[2][10]) unless the inner
12884      array type comes before the outer array type.  We thus call gen_type_die
12885      before we new_die and must prevent nested array types collapsing for this
12886      target.  */
12887
12888 #ifdef MIPS_DEBUGGING_INFO
12889   gen_type_die (TREE_TYPE (type), context_die);
12890   collapse_nested_arrays = false;
12891 #endif
12892
12893   array_die = new_die (DW_TAG_array_type, scope_die, type);
12894   add_name_attribute (array_die, type_tag (type));
12895   equate_type_number_to_die (type, array_die);
12896
12897   if (TREE_CODE (type) == VECTOR_TYPE)
12898     {
12899       /* The frontend feeds us a representation for the vector as a struct
12900          containing an array.  Pull out the array type.  */
12901       type = TREE_TYPE (TYPE_FIELDS (TYPE_DEBUG_REPRESENTATION_TYPE (type)));
12902       add_AT_flag (array_die, DW_AT_GNU_vector, 1);
12903     }
12904
12905   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
12906   if (is_fortran ()
12907       && TREE_CODE (type) == ARRAY_TYPE
12908       && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
12909       && !TYPE_STRING_FLAG (TREE_TYPE (type)))
12910     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
12911
12912 #if 0
12913   /* We default the array ordering.  SDB will probably do
12914      the right things even if DW_AT_ordering is not present.  It's not even
12915      an issue until we start to get into multidimensional arrays anyway.  If
12916      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
12917      then we'll have to put the DW_AT_ordering attribute back in.  (But if
12918      and when we find out that we need to put these in, we will only do so
12919      for multidimensional arrays.  */
12920   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
12921 #endif
12922
12923 #ifdef MIPS_DEBUGGING_INFO
12924   /* The SGI compilers handle arrays of unknown bound by setting
12925      AT_declaration and not emitting any subrange DIEs.  */
12926   if (! TYPE_DOMAIN (type))
12927     add_AT_flag (array_die, DW_AT_declaration, 1);
12928   else
12929 #endif
12930     add_subscript_info (array_die, type, collapse_nested_arrays);
12931
12932   /* Add representation of the type of the elements of this array type and
12933      emit the corresponding DIE if we haven't done it already.  */  
12934   element_type = TREE_TYPE (type);
12935   if (collapse_nested_arrays)
12936     while (TREE_CODE (element_type) == ARRAY_TYPE)
12937       {
12938         if (TYPE_STRING_FLAG (element_type) && is_fortran ())
12939           break;
12940         element_type = TREE_TYPE (element_type);
12941       }
12942
12943 #ifndef MIPS_DEBUGGING_INFO
12944   gen_type_die (element_type, context_die);
12945 #endif
12946
12947   add_type_attribute (array_die, element_type, 0, 0, context_die);
12948
12949   if (get_AT (array_die, DW_AT_name))
12950     add_pubtype (type, array_die);
12951 }
12952
12953 static dw_loc_descr_ref
12954 descr_info_loc (tree val, tree base_decl)
12955 {
12956   HOST_WIDE_INT size;
12957   dw_loc_descr_ref loc, loc2;
12958   enum dwarf_location_atom op;
12959
12960   if (val == base_decl)
12961     return new_loc_descr (DW_OP_push_object_address, 0, 0);
12962
12963   switch (TREE_CODE (val))
12964     {
12965     CASE_CONVERT:
12966       return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
12967     case VAR_DECL:
12968       return loc_descriptor_from_tree_1 (val, 0);
12969     case INTEGER_CST:
12970       if (host_integerp (val, 0))
12971         return int_loc_descriptor (tree_low_cst (val, 0));
12972       break;
12973     case INDIRECT_REF:
12974       size = int_size_in_bytes (TREE_TYPE (val));
12975       if (size < 0)
12976         break;
12977       loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
12978       if (!loc)
12979         break;
12980       if (size == DWARF2_ADDR_SIZE)
12981         add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
12982       else
12983         add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
12984       return loc;
12985     case POINTER_PLUS_EXPR:
12986     case PLUS_EXPR:
12987       if (host_integerp (TREE_OPERAND (val, 1), 1)
12988           && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
12989              < 16384)
12990         {
12991           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
12992           if (!loc)
12993             break;
12994           add_loc_descr (&loc,
12995                          new_loc_descr (DW_OP_plus_uconst,
12996                                         tree_low_cst (TREE_OPERAND (val, 1),
12997                                                       1), 0));
12998         }
12999       else
13000         {
13001           op = DW_OP_plus;
13002         do_binop:
13003           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
13004           if (!loc)
13005             break;
13006           loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
13007           if (!loc2)
13008             break;
13009           add_loc_descr (&loc, loc2);
13010           add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
13011         }
13012       return loc;
13013     case MINUS_EXPR:
13014       op = DW_OP_minus;
13015       goto do_binop;
13016     case MULT_EXPR:
13017       op = DW_OP_mul;
13018       goto do_binop;
13019     case EQ_EXPR:
13020       op = DW_OP_eq;
13021       goto do_binop;
13022     case NE_EXPR:
13023       op = DW_OP_ne;
13024       goto do_binop;
13025     default:
13026       break;
13027     }
13028   return NULL;
13029 }
13030
13031 static void
13032 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
13033                       tree val, tree base_decl)
13034 {
13035   dw_loc_descr_ref loc;
13036
13037   if (host_integerp (val, 0))
13038     {
13039       add_AT_unsigned (die, attr, tree_low_cst (val, 0));
13040       return;
13041     }
13042
13043   loc = descr_info_loc (val, base_decl);
13044   if (!loc)
13045     return;
13046
13047   add_AT_loc (die, attr, loc);
13048 }
13049
13050 /* This routine generates DIE for array with hidden descriptor, details
13051    are filled into *info by a langhook.  */
13052
13053 static void
13054 gen_descr_array_type_die (tree type, struct array_descr_info *info,
13055                           dw_die_ref context_die)
13056 {
13057   dw_die_ref scope_die = scope_die_for (type, context_die);
13058   dw_die_ref array_die;
13059   int dim;
13060
13061   array_die = new_die (DW_TAG_array_type, scope_die, type);
13062   add_name_attribute (array_die, type_tag (type));
13063   equate_type_number_to_die (type, array_die);
13064
13065   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
13066   if (is_fortran ()
13067       && info->ndimensions >= 2)
13068     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
13069
13070   if (info->data_location)
13071     add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
13072                           info->base_decl);
13073   if (info->associated)
13074     add_descr_info_field (array_die, DW_AT_associated, info->associated,
13075                           info->base_decl);
13076   if (info->allocated)
13077     add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
13078                           info->base_decl);
13079
13080   for (dim = 0; dim < info->ndimensions; dim++)
13081     {
13082       dw_die_ref subrange_die
13083         = new_die (DW_TAG_subrange_type, array_die, NULL);
13084
13085       if (info->dimen[dim].lower_bound)
13086         {
13087           /* If it is the default value, omit it.  */
13088           if ((is_c_family () || is_java ())
13089               && integer_zerop (info->dimen[dim].lower_bound))
13090             ;
13091           else if (is_fortran ()
13092                    && integer_onep (info->dimen[dim].lower_bound))
13093             ;
13094           else
13095             add_descr_info_field (subrange_die, DW_AT_lower_bound,
13096                                   info->dimen[dim].lower_bound,
13097                                   info->base_decl);
13098         }
13099       if (info->dimen[dim].upper_bound)
13100         add_descr_info_field (subrange_die, DW_AT_upper_bound,
13101                               info->dimen[dim].upper_bound,
13102                               info->base_decl);
13103       if (info->dimen[dim].stride)
13104         add_descr_info_field (subrange_die, DW_AT_byte_stride,
13105                               info->dimen[dim].stride,
13106                               info->base_decl);
13107     }
13108
13109   gen_type_die (info->element_type, context_die);
13110   add_type_attribute (array_die, info->element_type, 0, 0, context_die);
13111
13112   if (get_AT (array_die, DW_AT_name))
13113     add_pubtype (type, array_die);
13114 }
13115
13116 #if 0
13117 static void
13118 gen_entry_point_die (tree decl, dw_die_ref context_die)
13119 {
13120   tree origin = decl_ultimate_origin (decl);
13121   dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
13122
13123   if (origin != NULL)
13124     add_abstract_origin_attribute (decl_die, origin);
13125   else
13126     {
13127       add_name_and_src_coords_attributes (decl_die, decl);
13128       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
13129                           0, 0, context_die);
13130     }
13131
13132   if (DECL_ABSTRACT (decl))
13133     equate_decl_number_to_die (decl, decl_die);
13134   else
13135     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
13136 }
13137 #endif
13138
13139 /* Walk through the list of incomplete types again, trying once more to
13140    emit full debugging info for them.  */
13141
13142 static void
13143 retry_incomplete_types (void)
13144 {
13145   int i;
13146
13147   for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
13148     gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die);
13149 }
13150
13151 /* Generate a DIE to represent an inlined instance of an enumeration type.  */
13152
13153 static void
13154 gen_inlined_enumeration_type_die (tree type, dw_die_ref context_die)
13155 {
13156   dw_die_ref type_die = new_die (DW_TAG_enumeration_type, context_die, type);
13157
13158   /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
13159      be incomplete and such types are not marked.  */
13160   add_abstract_origin_attribute (type_die, type);
13161 }
13162
13163 /* Determine what tag to use for a record type.  */
13164
13165 static enum dwarf_tag
13166 record_type_tag (tree type)
13167 {
13168   if (! lang_hooks.types.classify_record)
13169     return DW_TAG_structure_type;
13170
13171   switch (lang_hooks.types.classify_record (type))
13172     {
13173     case RECORD_IS_STRUCT:
13174       return DW_TAG_structure_type;
13175
13176     case RECORD_IS_CLASS:
13177       return DW_TAG_class_type;
13178
13179     case RECORD_IS_INTERFACE:
13180       return DW_TAG_interface_type;
13181
13182     default:
13183       gcc_unreachable ();
13184     }
13185 }
13186
13187 /* Generate a DIE to represent an inlined instance of a structure type.  */
13188
13189 static void
13190 gen_inlined_structure_type_die (tree type, dw_die_ref context_die)
13191 {
13192   dw_die_ref type_die = new_die (record_type_tag (type), context_die, type);
13193
13194   /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
13195      be incomplete and such types are not marked.  */
13196   add_abstract_origin_attribute (type_die, type);
13197 }
13198
13199 /* Generate a DIE to represent an inlined instance of a union type.  */
13200
13201 static void
13202 gen_inlined_union_type_die (tree type, dw_die_ref context_die)
13203 {
13204   dw_die_ref type_die = new_die (DW_TAG_union_type, context_die, type);
13205
13206   /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
13207      be incomplete and such types are not marked.  */
13208   add_abstract_origin_attribute (type_die, type);
13209 }
13210
13211 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
13212    include all of the information about the enumeration values also. Each
13213    enumerated type name/value is listed as a child of the enumerated type
13214    DIE.  */
13215
13216 static dw_die_ref
13217 gen_enumeration_type_die (tree type, dw_die_ref context_die)
13218 {
13219   dw_die_ref type_die = lookup_type_die (type);
13220
13221   if (type_die == NULL)
13222     {
13223       type_die = new_die (DW_TAG_enumeration_type,
13224                           scope_die_for (type, context_die), type);
13225       equate_type_number_to_die (type, type_die);
13226       add_name_attribute (type_die, type_tag (type));
13227     }
13228   else if (! TYPE_SIZE (type))
13229     return type_die;
13230   else
13231     remove_AT (type_die, DW_AT_declaration);
13232
13233   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
13234      given enum type is incomplete, do not generate the DW_AT_byte_size
13235      attribute or the DW_AT_element_list attribute.  */
13236   if (TYPE_SIZE (type))
13237     {
13238       tree link;
13239
13240       TREE_ASM_WRITTEN (type) = 1;
13241       add_byte_size_attribute (type_die, type);
13242       if (TYPE_STUB_DECL (type) != NULL_TREE)
13243         add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
13244
13245       /* If the first reference to this type was as the return type of an
13246          inline function, then it may not have a parent.  Fix this now.  */
13247       if (type_die->die_parent == NULL)
13248         add_child_die (scope_die_for (type, context_die), type_die);
13249
13250       for (link = TYPE_VALUES (type);
13251            link != NULL; link = TREE_CHAIN (link))
13252         {
13253           dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
13254           tree value = TREE_VALUE (link);
13255
13256           add_name_attribute (enum_die,
13257                               IDENTIFIER_POINTER (TREE_PURPOSE (link)));
13258
13259           if (TREE_CODE (value) == CONST_DECL)
13260             value = DECL_INITIAL (value);
13261
13262           if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
13263             /* DWARF2 does not provide a way of indicating whether or
13264                not enumeration constants are signed or unsigned.  GDB
13265                always assumes the values are signed, so we output all
13266                values as if they were signed.  That means that
13267                enumeration constants with very large unsigned values
13268                will appear to have negative values in the debugger.  */
13269             add_AT_int (enum_die, DW_AT_const_value,
13270                         tree_low_cst (value, tree_int_cst_sgn (value) > 0));
13271         }
13272     }
13273   else
13274     add_AT_flag (type_die, DW_AT_declaration, 1);
13275
13276   if (get_AT (type_die, DW_AT_name))
13277     add_pubtype (type, type_die);
13278
13279   return type_die;
13280 }
13281
13282 /* Generate a DIE to represent either a real live formal parameter decl or to
13283    represent just the type of some formal parameter position in some function
13284    type.
13285
13286    Note that this routine is a bit unusual because its argument may be a
13287    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
13288    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
13289    node.  If it's the former then this function is being called to output a
13290    DIE to represent a formal parameter object (or some inlining thereof).  If
13291    it's the latter, then this function is only being called to output a
13292    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
13293    argument type of some subprogram type.  */
13294
13295 static dw_die_ref
13296 gen_formal_parameter_die (tree node, tree origin, dw_die_ref context_die)
13297 {
13298   tree node_or_origin = node ? node : origin;
13299   dw_die_ref parm_die
13300     = new_die (DW_TAG_formal_parameter, context_die, node);
13301
13302   switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
13303     {
13304     case tcc_declaration:
13305       if (!origin)
13306         origin = decl_ultimate_origin (node);
13307       if (origin != NULL)
13308         add_abstract_origin_attribute (parm_die, origin);
13309       else
13310         {
13311           tree type = TREE_TYPE (node);
13312           add_name_and_src_coords_attributes (parm_die, node);
13313           if (DECL_BY_REFERENCE (node))
13314             add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
13315                                 context_die);
13316           else
13317             add_type_attribute (parm_die, type,
13318                                 TREE_READONLY (node),
13319                                 TREE_THIS_VOLATILE (node),
13320                                 context_die);
13321           if (DECL_ARTIFICIAL (node))
13322             add_AT_flag (parm_die, DW_AT_artificial, 1);
13323         }
13324
13325       if (node)
13326         equate_decl_number_to_die (node, parm_die);
13327       if (! DECL_ABSTRACT (node_or_origin))
13328         add_location_or_const_value_attribute (parm_die, node_or_origin,
13329                                                DW_AT_location);
13330
13331       break;
13332
13333     case tcc_type:
13334       /* We were called with some kind of a ..._TYPE node.  */
13335       add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
13336       break;
13337
13338     default:
13339       gcc_unreachable ();
13340     }
13341
13342   return parm_die;
13343 }
13344
13345 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
13346    at the end of an (ANSI prototyped) formal parameters list.  */
13347
13348 static void
13349 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
13350 {
13351   new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
13352 }
13353
13354 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
13355    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
13356    parameters as specified in some function type specification (except for
13357    those which appear as part of a function *definition*).  */
13358
13359 static void
13360 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
13361 {
13362   tree link;
13363   tree formal_type = NULL;
13364   tree first_parm_type;
13365   tree arg;
13366
13367   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
13368     {
13369       arg = DECL_ARGUMENTS (function_or_method_type);
13370       function_or_method_type = TREE_TYPE (function_or_method_type);
13371     }
13372   else
13373     arg = NULL_TREE;
13374
13375   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
13376
13377   /* Make our first pass over the list of formal parameter types and output a
13378      DW_TAG_formal_parameter DIE for each one.  */
13379   for (link = first_parm_type; link; )
13380     {
13381       dw_die_ref parm_die;
13382
13383       formal_type = TREE_VALUE (link);
13384       if (formal_type == void_type_node)
13385         break;
13386
13387       /* Output a (nameless) DIE to represent the formal parameter itself.  */
13388       parm_die = gen_formal_parameter_die (formal_type, NULL, context_die);
13389       if ((TREE_CODE (function_or_method_type) == METHOD_TYPE
13390            && link == first_parm_type)
13391           || (arg && DECL_ARTIFICIAL (arg)))
13392         add_AT_flag (parm_die, DW_AT_artificial, 1);
13393
13394       link = TREE_CHAIN (link);
13395       if (arg)
13396         arg = TREE_CHAIN (arg);
13397     }
13398
13399   /* If this function type has an ellipsis, add a
13400      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
13401   if (formal_type != void_type_node)
13402     gen_unspecified_parameters_die (function_or_method_type, context_die);
13403
13404   /* Make our second (and final) pass over the list of formal parameter types
13405      and output DIEs to represent those types (as necessary).  */
13406   for (link = TYPE_ARG_TYPES (function_or_method_type);
13407        link && TREE_VALUE (link);
13408        link = TREE_CHAIN (link))
13409     gen_type_die (TREE_VALUE (link), context_die);
13410 }
13411
13412 /* We want to generate the DIE for TYPE so that we can generate the
13413    die for MEMBER, which has been defined; we will need to refer back
13414    to the member declaration nested within TYPE.  If we're trying to
13415    generate minimal debug info for TYPE, processing TYPE won't do the
13416    trick; we need to attach the member declaration by hand.  */
13417
13418 static void
13419 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
13420 {
13421   gen_type_die (type, context_die);
13422
13423   /* If we're trying to avoid duplicate debug info, we may not have
13424      emitted the member decl for this function.  Emit it now.  */
13425   if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
13426       && ! lookup_decl_die (member))
13427     {
13428       dw_die_ref type_die;
13429       gcc_assert (!decl_ultimate_origin (member));
13430
13431       push_decl_scope (type);
13432       type_die = lookup_type_die (type);
13433       if (TREE_CODE (member) == FUNCTION_DECL)
13434         gen_subprogram_die (member, type_die);
13435       else if (TREE_CODE (member) == FIELD_DECL)
13436         {
13437           /* Ignore the nameless fields that are used to skip bits but handle
13438              C++ anonymous unions and structs.  */
13439           if (DECL_NAME (member) != NULL_TREE
13440               || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
13441               || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
13442             {
13443               gen_type_die (member_declared_type (member), type_die);
13444               gen_field_die (member, type_die);
13445             }
13446         }
13447       else
13448         gen_variable_die (member, NULL_TREE, type_die);
13449
13450       pop_decl_scope ();
13451     }
13452 }
13453
13454 /* Generate the DWARF2 info for the "abstract" instance of a function which we
13455    may later generate inlined and/or out-of-line instances of.  */
13456
13457 static void
13458 dwarf2out_abstract_function (tree decl)
13459 {
13460   dw_die_ref old_die;
13461   tree save_fn;
13462   tree context;
13463   int was_abstract = DECL_ABSTRACT (decl);
13464
13465   /* Make sure we have the actual abstract inline, not a clone.  */
13466   decl = DECL_ORIGIN (decl);
13467
13468   old_die = lookup_decl_die (decl);
13469   if (old_die && get_AT (old_die, DW_AT_inline))
13470     /* We've already generated the abstract instance.  */
13471     return;
13472
13473   /* Be sure we've emitted the in-class declaration DIE (if any) first, so
13474      we don't get confused by DECL_ABSTRACT.  */
13475   if (debug_info_level > DINFO_LEVEL_TERSE)
13476     {
13477       context = decl_class_context (decl);
13478       if (context)
13479         gen_type_die_for_member
13480           (context, decl, decl_function_context (decl) ? NULL : comp_unit_die);
13481     }
13482
13483   /* Pretend we've just finished compiling this function.  */
13484   save_fn = current_function_decl;
13485   current_function_decl = decl;
13486   push_cfun (DECL_STRUCT_FUNCTION (decl));
13487
13488   set_decl_abstract_flags (decl, 1);
13489   dwarf2out_decl (decl);
13490   if (! was_abstract)
13491     set_decl_abstract_flags (decl, 0);
13492
13493   current_function_decl = save_fn;
13494   pop_cfun ();
13495 }
13496
13497 /* Helper function of premark_used_types() which gets called through
13498    htab_traverse_resize().
13499
13500    Marks the DIE of a given type in *SLOT as perennial, so it never gets
13501    marked as unused by prune_unused_types.  */
13502 static int
13503 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
13504 {
13505   tree type;
13506   dw_die_ref die;
13507
13508   type = (tree) *slot;
13509   die = lookup_type_die (type);
13510   if (die != NULL)
13511     die->die_perennial_p = 1;
13512   return 1;
13513 }
13514
13515 /* Mark all members of used_types_hash as perennial.  */
13516 static void
13517 premark_used_types (void)
13518 {
13519   if (cfun && cfun->used_types_hash)
13520     htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
13521 }
13522
13523 /* Generate a DIE to represent a declared function (either file-scope or
13524    block-local).  */
13525
13526 static void
13527 gen_subprogram_die (tree decl, dw_die_ref context_die)
13528 {
13529   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
13530   tree origin = decl_ultimate_origin (decl);
13531   dw_die_ref subr_die;
13532   tree fn_arg_types;
13533   tree outer_scope;
13534   dw_die_ref old_die = lookup_decl_die (decl);
13535   int declaration = (current_function_decl != decl
13536                      || class_or_namespace_scope_p (context_die));
13537
13538   premark_used_types ();
13539
13540   /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
13541      started to generate the abstract instance of an inline, decided to output
13542      its containing class, and proceeded to emit the declaration of the inline
13543      from the member list for the class.  If so, DECLARATION takes priority;
13544      we'll get back to the abstract instance when done with the class.  */
13545
13546   /* The class-scope declaration DIE must be the primary DIE.  */
13547   if (origin && declaration && class_or_namespace_scope_p (context_die))
13548     {
13549       origin = NULL;
13550       gcc_assert (!old_die);
13551     }
13552
13553   /* Now that the C++ front end lazily declares artificial member fns, we
13554      might need to retrofit the declaration into its class.  */
13555   if (!declaration && !origin && !old_die
13556       && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
13557       && !class_or_namespace_scope_p (context_die)
13558       && debug_info_level > DINFO_LEVEL_TERSE)
13559     old_die = force_decl_die (decl);
13560
13561   if (origin != NULL)
13562     {
13563       gcc_assert (!declaration || local_scope_p (context_die));
13564
13565       /* Fixup die_parent for the abstract instance of a nested
13566          inline function.  */
13567       if (old_die && old_die->die_parent == NULL)
13568         add_child_die (context_die, old_die);
13569
13570       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
13571       add_abstract_origin_attribute (subr_die, origin);
13572     }
13573   else if (old_die)
13574     {
13575       expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
13576       struct dwarf_file_data * file_index = lookup_filename (s.file);
13577
13578       if (!get_AT_flag (old_die, DW_AT_declaration)
13579           /* We can have a normal definition following an inline one in the
13580              case of redefinition of GNU C extern inlines.
13581              It seems reasonable to use AT_specification in this case.  */
13582           && !get_AT (old_die, DW_AT_inline))
13583         {
13584           /* Detect and ignore this case, where we are trying to output
13585              something we have already output.  */
13586           return;
13587         }
13588
13589       /* If the definition comes from the same place as the declaration,
13590          maybe use the old DIE.  We always want the DIE for this function
13591          that has the *_pc attributes to be under comp_unit_die so the
13592          debugger can find it.  We also need to do this for abstract
13593          instances of inlines, since the spec requires the out-of-line copy
13594          to have the same parent.  For local class methods, this doesn't
13595          apply; we just use the old DIE.  */
13596       if ((old_die->die_parent == comp_unit_die || context_die == NULL)
13597           && (DECL_ARTIFICIAL (decl)
13598               || (get_AT_file (old_die, DW_AT_decl_file) == file_index
13599                   && (get_AT_unsigned (old_die, DW_AT_decl_line)
13600                       == (unsigned) s.line))))
13601         {
13602           subr_die = old_die;
13603
13604           /* Clear out the declaration attribute and the formal parameters.
13605              Do not remove all children, because it is possible that this
13606              declaration die was forced using force_decl_die(). In such
13607              cases die that forced declaration die (e.g. TAG_imported_module)
13608              is one of the children that we do not want to remove.  */
13609           remove_AT (subr_die, DW_AT_declaration);
13610           remove_child_TAG (subr_die, DW_TAG_formal_parameter);
13611         }
13612       else
13613         {
13614           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
13615           add_AT_specification (subr_die, old_die);
13616           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
13617             add_AT_file (subr_die, DW_AT_decl_file, file_index);
13618           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
13619             add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
13620         }
13621     }
13622   else
13623     {
13624       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
13625
13626       if (TREE_PUBLIC (decl))
13627         add_AT_flag (subr_die, DW_AT_external, 1);
13628
13629       add_name_and_src_coords_attributes (subr_die, decl);
13630       if (debug_info_level > DINFO_LEVEL_TERSE)
13631         {
13632           add_prototyped_attribute (subr_die, TREE_TYPE (decl));
13633           add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
13634                               0, 0, context_die);
13635         }
13636
13637       add_pure_or_virtual_attribute (subr_die, decl);
13638       if (DECL_ARTIFICIAL (decl))
13639         add_AT_flag (subr_die, DW_AT_artificial, 1);
13640
13641       if (TREE_PROTECTED (decl))
13642         add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_protected);
13643       else if (TREE_PRIVATE (decl))
13644         add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_private);
13645     }
13646
13647   if (declaration)
13648     {
13649       if (!old_die || !get_AT (old_die, DW_AT_inline))
13650         {
13651           add_AT_flag (subr_die, DW_AT_declaration, 1);
13652
13653           /* The first time we see a member function, it is in the context of
13654              the class to which it belongs.  We make sure of this by emitting
13655              the class first.  The next time is the definition, which is
13656              handled above.  The two may come from the same source text.
13657
13658              Note that force_decl_die() forces function declaration die. It is
13659              later reused to represent definition.  */
13660           equate_decl_number_to_die (decl, subr_die);
13661         }
13662     }
13663   else if (DECL_ABSTRACT (decl))
13664     {
13665       if (DECL_DECLARED_INLINE_P (decl))
13666         {
13667           if (cgraph_function_possibly_inlined_p (decl))
13668             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
13669           else
13670             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
13671         }
13672       else
13673         {
13674           if (cgraph_function_possibly_inlined_p (decl))
13675             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
13676           else
13677             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
13678         }
13679
13680       if (DECL_DECLARED_INLINE_P (decl)
13681           && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
13682         add_AT_flag (subr_die, DW_AT_artificial, 1);
13683
13684       equate_decl_number_to_die (decl, subr_die);
13685     }
13686   else if (!DECL_EXTERNAL (decl))
13687     {
13688       HOST_WIDE_INT cfa_fb_offset;
13689
13690       if (!old_die || !get_AT (old_die, DW_AT_inline))
13691         equate_decl_number_to_die (decl, subr_die);
13692
13693       if (!flag_reorder_blocks_and_partition)
13694         {
13695           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
13696                                        current_function_funcdef_no);
13697           add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
13698           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
13699                                        current_function_funcdef_no);
13700           add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
13701
13702           add_pubname (decl, subr_die);
13703           add_arange (decl, subr_die);
13704         }
13705       else
13706         {  /* Do nothing for now; maybe need to duplicate die, one for
13707               hot section and one for cold section, then use the hot/cold
13708               section begin/end labels to generate the aranges...  */
13709           /*
13710             add_AT_lbl_id (subr_die, DW_AT_low_pc, hot_section_label);
13711             add_AT_lbl_id (subr_die, DW_AT_high_pc, hot_section_end_label);
13712             add_AT_lbl_id (subr_die, DW_AT_lo_user, unlikely_section_label);
13713             add_AT_lbl_id (subr_die, DW_AT_hi_user, cold_section_end_label);
13714
13715             add_pubname (decl, subr_die);
13716             add_arange (decl, subr_die);
13717             add_arange (decl, subr_die);
13718            */
13719         }
13720
13721 #ifdef MIPS_DEBUGGING_INFO
13722       /* Add a reference to the FDE for this routine.  */
13723       add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
13724 #endif
13725
13726       cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
13727
13728       /* We define the "frame base" as the function's CFA.  This is more
13729          convenient for several reasons: (1) It's stable across the prologue
13730          and epilogue, which makes it better than just a frame pointer,
13731          (2) With dwarf3, there exists a one-byte encoding that allows us
13732          to reference the .debug_frame data by proxy, but failing that,
13733          (3) We can at least reuse the code inspection and interpretation
13734          code that determines the CFA position at various points in the
13735          function.  */
13736       /* ??? Use some command-line or configury switch to enable the use
13737          of dwarf3 DW_OP_call_frame_cfa.  At present there are no dwarf
13738          consumers that understand it; fall back to "pure" dwarf2 and
13739          convert the CFA data into a location list.  */
13740       {
13741         dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
13742         if (list->dw_loc_next)
13743           add_AT_loc_list (subr_die, DW_AT_frame_base, list);
13744         else
13745           add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
13746       }
13747
13748       /* Compute a displacement from the "steady-state frame pointer" to
13749          the CFA.  The former is what all stack slots and argument slots
13750          will reference in the rtl; the later is what we've told the
13751          debugger about.  We'll need to adjust all frame_base references
13752          by this displacement.  */
13753       compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
13754
13755       if (cfun->static_chain_decl)
13756         add_AT_location_description (subr_die, DW_AT_static_link,
13757                  loc_descriptor_from_tree (cfun->static_chain_decl));
13758     }
13759
13760   /* Now output descriptions of the arguments for this function. This gets
13761      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
13762      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
13763      `...' at the end of the formal parameter list.  In order to find out if
13764      there was a trailing ellipsis or not, we must instead look at the type
13765      associated with the FUNCTION_DECL.  This will be a node of type
13766      FUNCTION_TYPE. If the chain of type nodes hanging off of this
13767      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
13768      an ellipsis at the end.  */
13769
13770   /* In the case where we are describing a mere function declaration, all we
13771      need to do here (and all we *can* do here) is to describe the *types* of
13772      its formal parameters.  */
13773   if (debug_info_level <= DINFO_LEVEL_TERSE)
13774     ;
13775   else if (declaration)
13776     gen_formal_types_die (decl, subr_die);
13777   else
13778     {
13779       /* Generate DIEs to represent all known formal parameters.  */
13780       tree arg_decls = DECL_ARGUMENTS (decl);
13781       tree parm;
13782
13783       /* When generating DIEs, generate the unspecified_parameters DIE
13784          instead if we come across the arg "__builtin_va_alist" */
13785       for (parm = arg_decls; parm; parm = TREE_CHAIN (parm))
13786         if (TREE_CODE (parm) == PARM_DECL)
13787           {
13788             if (DECL_NAME (parm)
13789                 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (parm)),
13790                             "__builtin_va_alist"))
13791               gen_unspecified_parameters_die (parm, subr_die);
13792             else
13793               gen_decl_die (parm, NULL, subr_die);
13794           }
13795
13796       /* Decide whether we need an unspecified_parameters DIE at the end.
13797          There are 2 more cases to do this for: 1) the ansi ... declaration -
13798          this is detectable when the end of the arg list is not a
13799          void_type_node 2) an unprototyped function declaration (not a
13800          definition).  This just means that we have no info about the
13801          parameters at all.  */
13802       fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
13803       if (fn_arg_types != NULL)
13804         {
13805           /* This is the prototyped case, check for....  */
13806           if (TREE_VALUE (tree_last (fn_arg_types)) != void_type_node)
13807             gen_unspecified_parameters_die (decl, subr_die);
13808         }
13809       else if (DECL_INITIAL (decl) == NULL_TREE)
13810         gen_unspecified_parameters_die (decl, subr_die);
13811     }
13812
13813   /* Output Dwarf info for all of the stuff within the body of the function
13814      (if it has one - it may be just a declaration).  */
13815   outer_scope = DECL_INITIAL (decl);
13816
13817   /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
13818      a function.  This BLOCK actually represents the outermost binding contour
13819      for the function, i.e. the contour in which the function's formal
13820      parameters and labels get declared. Curiously, it appears that the front
13821      end doesn't actually put the PARM_DECL nodes for the current function onto
13822      the BLOCK_VARS list for this outer scope, but are strung off of the
13823      DECL_ARGUMENTS list for the function instead.
13824
13825      The BLOCK_VARS list for the `outer_scope' does provide us with a list of
13826      the LABEL_DECL nodes for the function however, and we output DWARF info
13827      for those in decls_for_scope.  Just within the `outer_scope' there will be
13828      a BLOCK node representing the function's outermost pair of curly braces,
13829      and any blocks used for the base and member initializers of a C++
13830      constructor function.  */
13831   if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
13832     {
13833       /* Emit a DW_TAG_variable DIE for a named return value.  */
13834       if (DECL_NAME (DECL_RESULT (decl)))
13835         gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
13836
13837       current_function_has_inlines = 0;
13838       decls_for_scope (outer_scope, subr_die, 0);
13839
13840 #if 0 && defined (MIPS_DEBUGGING_INFO)
13841       if (current_function_has_inlines)
13842         {
13843           add_AT_flag (subr_die, DW_AT_MIPS_has_inlines, 1);
13844           if (! comp_unit_has_inlines)
13845             {
13846               add_AT_flag (comp_unit_die, DW_AT_MIPS_has_inlines, 1);
13847               comp_unit_has_inlines = 1;
13848             }
13849         }
13850 #endif
13851     }
13852   /* Add the calling convention attribute if requested.  */
13853   add_calling_convention_attribute (subr_die, decl);
13854
13855 }
13856
13857 /* Returns a hash value for X (which really is a die_struct).  */
13858
13859 static hashval_t
13860 common_block_die_table_hash (const void *x)
13861 {
13862   const_dw_die_ref d = (const_dw_die_ref) x;
13863   return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
13864 }
13865
13866 /* Return nonzero if decl_id and die_parent of die_struct X is the same
13867    as decl_id and die_parent of die_struct Y.  */
13868
13869 static int
13870 common_block_die_table_eq (const void *x, const void *y)
13871 {
13872   const_dw_die_ref d = (const_dw_die_ref) x;
13873   const_dw_die_ref e = (const_dw_die_ref) y;
13874   return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
13875 }
13876
13877 /* Generate a DIE to represent a declared data object.
13878    Either DECL or ORIGIN must be non-null.  */
13879
13880 static void
13881 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
13882 {
13883   HOST_WIDE_INT off;
13884   tree com_decl;
13885   tree decl_or_origin = decl ? decl : origin;
13886   dw_die_ref var_die;
13887   dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
13888   int declaration = (DECL_EXTERNAL (decl_or_origin)
13889                      /* If DECL is COMDAT and has not actually been
13890                         emitted, we cannot take its address; there
13891                         might end up being no definition anywhere in
13892                         the program.  For example, consider the C++
13893                         test case:
13894
13895                           template <class T>
13896                           struct S { static const int i = 7; };
13897
13898                           template <class T>
13899                           const int S<T>::i;
13900
13901                           int f() { return S<int>::i; }
13902
13903                         Here, S<int>::i is not DECL_EXTERNAL, but no
13904                         definition is required, so the compiler will
13905                         not emit a definition.  */
13906                      || (TREE_CODE (decl_or_origin) == VAR_DECL
13907                          && DECL_COMDAT (decl_or_origin)
13908                          && !TREE_ASM_WRITTEN (decl_or_origin))
13909                      || class_or_namespace_scope_p (context_die));
13910
13911   if (!origin)
13912     origin = decl_ultimate_origin (decl);
13913
13914   com_decl = fortran_common (decl_or_origin, &off);
13915
13916   /* Symbol in common gets emitted as a child of the common block, in the form
13917      of a data member.  */
13918   if (com_decl)
13919     {
13920       tree field;
13921       dw_die_ref com_die;
13922       dw_loc_descr_ref loc;
13923       die_node com_die_arg;
13924
13925       var_die = lookup_decl_die (decl_or_origin);
13926       if (var_die)
13927         {
13928           if (get_AT (var_die, DW_AT_location) == NULL)
13929             {
13930               loc = loc_descriptor_from_tree (com_decl);
13931               if (loc)
13932                 {
13933                   if (off)
13934                     {
13935                       /* Optimize the common case.  */
13936                       if (loc->dw_loc_opc == DW_OP_addr
13937                           && loc->dw_loc_next == NULL
13938                           && GET_CODE (loc->dw_loc_oprnd1.v.val_addr)
13939                              == SYMBOL_REF)
13940                         loc->dw_loc_oprnd1.v.val_addr
13941                           = plus_constant (loc->dw_loc_oprnd1.v.val_addr, off);
13942                         else
13943                           add_loc_descr (&loc,
13944                                          new_loc_descr (DW_OP_plus_uconst,
13945                                                         off, 0));
13946                     }
13947                   add_AT_loc (var_die, DW_AT_location, loc);
13948                   remove_AT (var_die, DW_AT_declaration);
13949                 }
13950             }
13951           return;
13952         }
13953
13954       if (common_block_die_table == NULL)
13955         common_block_die_table
13956           = htab_create_ggc (10, common_block_die_table_hash,
13957                              common_block_die_table_eq, NULL);
13958
13959       field = TREE_OPERAND (DECL_VALUE_EXPR (decl), 0);
13960       com_die_arg.decl_id = DECL_UID (com_decl);
13961       com_die_arg.die_parent = context_die;
13962       com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
13963       loc = loc_descriptor_from_tree (com_decl);
13964       if (com_die == NULL)
13965         {
13966           const char *cnam
13967             = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
13968           void **slot;
13969
13970           com_die = new_die (DW_TAG_common_block, context_die, decl);
13971           add_name_and_src_coords_attributes (com_die, com_decl);
13972           if (loc)
13973             {
13974               add_AT_loc (com_die, DW_AT_location, loc);
13975               /* Avoid sharing the same loc descriptor between
13976                  DW_TAG_common_block and DW_TAG_variable.  */
13977               loc = loc_descriptor_from_tree (com_decl);
13978             }
13979           else if (DECL_EXTERNAL (decl))
13980             add_AT_flag (com_die, DW_AT_declaration, 1);
13981           add_pubname_string (cnam, com_die); /* ??? needed? */
13982           com_die->decl_id = DECL_UID (com_decl);
13983           slot = htab_find_slot (common_block_die_table, com_die, INSERT);
13984           *slot = (void *) com_die;
13985         }
13986       else if (get_AT (com_die, DW_AT_location) == NULL && loc)
13987         {
13988           add_AT_loc (com_die, DW_AT_location, loc);
13989           loc = loc_descriptor_from_tree (com_decl);
13990           remove_AT (com_die, DW_AT_declaration);
13991         }
13992       var_die = new_die (DW_TAG_variable, com_die, decl);
13993       add_name_and_src_coords_attributes (var_die, decl);
13994       add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
13995                           TREE_THIS_VOLATILE (decl), context_die);
13996       add_AT_flag (var_die, DW_AT_external, 1);
13997       if (loc)
13998         {
13999           if (off)
14000             {
14001               /* Optimize the common case.  */
14002               if (loc->dw_loc_opc == DW_OP_addr
14003                   && loc->dw_loc_next == NULL
14004                   && GET_CODE (loc->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
14005                 loc->dw_loc_oprnd1.v.val_addr
14006                   = plus_constant (loc->dw_loc_oprnd1.v.val_addr, off);
14007               else
14008                 add_loc_descr (&loc, new_loc_descr (DW_OP_plus_uconst,
14009                                                     off, 0));
14010             }
14011           add_AT_loc (var_die, DW_AT_location, loc);
14012         }
14013       else if (DECL_EXTERNAL (decl))
14014         add_AT_flag (var_die, DW_AT_declaration, 1);
14015       equate_decl_number_to_die (decl, var_die);
14016       return;
14017     }
14018
14019   var_die = new_die (DW_TAG_variable, context_die, decl);
14020
14021   if (origin != NULL)
14022     add_abstract_origin_attribute (var_die, origin);
14023
14024   /* Loop unrolling can create multiple blocks that refer to the same
14025      static variable, so we must test for the DW_AT_declaration flag.
14026
14027      ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
14028      copy decls and set the DECL_ABSTRACT flag on them instead of
14029      sharing them.
14030
14031      ??? Duplicated blocks have been rewritten to use .debug_ranges.
14032
14033      ??? The declare_in_namespace support causes us to get two DIEs for one
14034      variable, both of which are declarations.  We want to avoid considering
14035      one to be a specification, so we must test that this DIE is not a
14036      declaration.  */
14037   else if (old_die && TREE_STATIC (decl) && ! declaration
14038            && get_AT_flag (old_die, DW_AT_declaration) == 1)
14039     {
14040       /* This is a definition of a C++ class level static.  */
14041       add_AT_specification (var_die, old_die);
14042       if (DECL_NAME (decl))
14043         {
14044           expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
14045           struct dwarf_file_data * file_index = lookup_filename (s.file);
14046
14047           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
14048             add_AT_file (var_die, DW_AT_decl_file, file_index);
14049
14050           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
14051             add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
14052         }
14053     }
14054   else
14055     {
14056       tree type = TREE_TYPE (decl);
14057
14058       add_name_and_src_coords_attributes (var_die, decl);
14059       if ((TREE_CODE (decl) == PARM_DECL
14060            || TREE_CODE (decl) == RESULT_DECL)
14061           && DECL_BY_REFERENCE (decl))
14062         add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
14063       else
14064         add_type_attribute (var_die, type, TREE_READONLY (decl),
14065                             TREE_THIS_VOLATILE (decl), context_die);
14066
14067       if (TREE_PUBLIC (decl))
14068         add_AT_flag (var_die, DW_AT_external, 1);
14069
14070       if (DECL_ARTIFICIAL (decl))
14071         add_AT_flag (var_die, DW_AT_artificial, 1);
14072
14073       if (TREE_PROTECTED (decl))
14074         add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_protected);
14075       else if (TREE_PRIVATE (decl))
14076         add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_private);
14077     }
14078
14079   if (declaration)
14080     add_AT_flag (var_die, DW_AT_declaration, 1);
14081
14082   if (decl && (DECL_ABSTRACT (decl) || declaration))
14083     equate_decl_number_to_die (decl, var_die);
14084
14085   if (! declaration && ! DECL_ABSTRACT (decl_or_origin))
14086     {
14087       if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
14088           && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
14089         defer_location (decl_or_origin, var_die);
14090       else
14091         add_location_or_const_value_attribute (var_die,
14092                                                decl_or_origin,
14093                                                DW_AT_location);
14094       add_pubname (decl_or_origin, var_die);
14095     }
14096   else
14097     tree_add_const_value_attribute (var_die, decl_or_origin);
14098 }
14099
14100 /* Generate a DIE to represent a named constant.  */
14101
14102 static void
14103 gen_const_die (tree decl, dw_die_ref context_die)
14104 {
14105   dw_die_ref const_die;
14106   tree type = TREE_TYPE (decl);
14107
14108   const_die = new_die (DW_TAG_constant, context_die, decl);
14109   add_name_and_src_coords_attributes (const_die, decl);
14110   add_type_attribute (const_die, type, 1, 0, context_die);
14111   if (TREE_PUBLIC (decl))
14112     add_AT_flag (const_die, DW_AT_external, 1);
14113   if (DECL_ARTIFICIAL (decl))
14114     add_AT_flag (const_die, DW_AT_artificial, 1);
14115   tree_add_const_value_attribute (const_die, decl);
14116 }
14117
14118 /* Generate a DIE to represent a label identifier.  */
14119
14120 static void
14121 gen_label_die (tree decl, dw_die_ref context_die)
14122 {
14123   tree origin = decl_ultimate_origin (decl);
14124   dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
14125   rtx insn;
14126   char label[MAX_ARTIFICIAL_LABEL_BYTES];
14127
14128   if (origin != NULL)
14129     add_abstract_origin_attribute (lbl_die, origin);
14130   else
14131     add_name_and_src_coords_attributes (lbl_die, decl);
14132
14133   if (DECL_ABSTRACT (decl))
14134     equate_decl_number_to_die (decl, lbl_die);
14135   else
14136     {
14137       insn = DECL_RTL_IF_SET (decl);
14138
14139       /* Deleted labels are programmer specified labels which have been
14140          eliminated because of various optimizations.  We still emit them
14141          here so that it is possible to put breakpoints on them.  */
14142       if (insn
14143           && (LABEL_P (insn)
14144               || ((NOTE_P (insn)
14145                    && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
14146         {
14147           /* When optimization is enabled (via -O) some parts of the compiler
14148              (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
14149              represent source-level labels which were explicitly declared by
14150              the user.  This really shouldn't be happening though, so catch
14151              it if it ever does happen.  */
14152           gcc_assert (!INSN_DELETED_P (insn));
14153
14154           ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
14155           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
14156         }
14157     }
14158 }
14159
14160 /* A helper function for gen_inlined_subroutine_die.  Add source coordinate
14161    attributes to the DIE for a block STMT, to describe where the inlined
14162    function was called from.  This is similar to add_src_coords_attributes.  */
14163
14164 static inline void
14165 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
14166 {
14167   expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
14168
14169   add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
14170   add_AT_unsigned (die, DW_AT_call_line, s.line);
14171 }
14172
14173
14174 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
14175    Add low_pc and high_pc attributes to the DIE for a block STMT.  */
14176
14177 static inline void
14178 add_high_low_attributes (tree stmt, dw_die_ref die)
14179 {
14180   char label[MAX_ARTIFICIAL_LABEL_BYTES];
14181
14182   if (BLOCK_FRAGMENT_CHAIN (stmt))
14183     {
14184       tree chain;
14185
14186       if (inlined_function_outer_scope_p (stmt))
14187         {
14188           ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
14189                                        BLOCK_NUMBER (stmt));
14190           add_AT_lbl_id (die, DW_AT_entry_pc, label);
14191         }
14192
14193       add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
14194
14195       chain = BLOCK_FRAGMENT_CHAIN (stmt);
14196       do
14197         {
14198           add_ranges (chain);
14199           chain = BLOCK_FRAGMENT_CHAIN (chain);
14200         }
14201       while (chain);
14202       add_ranges (NULL);
14203     }
14204   else
14205     {
14206       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
14207                                    BLOCK_NUMBER (stmt));
14208       add_AT_lbl_id (die, DW_AT_low_pc, label);
14209       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
14210                                    BLOCK_NUMBER (stmt));
14211       add_AT_lbl_id (die, DW_AT_high_pc, label);
14212     }
14213 }
14214
14215 /* Generate a DIE for a lexical block.  */
14216
14217 static void
14218 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
14219 {
14220   dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
14221
14222   if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
14223     add_high_low_attributes (stmt, stmt_die);
14224
14225   decls_for_scope (stmt, stmt_die, depth);
14226 }
14227
14228 /* Generate a DIE for an inlined subprogram.  */
14229
14230 static void
14231 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
14232 {
14233   tree decl = block_ultimate_origin (stmt);
14234
14235   /* Emit info for the abstract instance first, if we haven't yet.  We
14236      must emit this even if the block is abstract, otherwise when we
14237      emit the block below (or elsewhere), we may end up trying to emit
14238      a die whose origin die hasn't been emitted, and crashing.  */
14239   dwarf2out_abstract_function (decl);
14240
14241   if (! BLOCK_ABSTRACT (stmt))
14242     {
14243       dw_die_ref subr_die
14244         = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
14245
14246       add_abstract_origin_attribute (subr_die, decl);
14247       if (TREE_ASM_WRITTEN (stmt))
14248         add_high_low_attributes (stmt, subr_die);
14249       add_call_src_coords_attributes (stmt, subr_die);
14250
14251       decls_for_scope (stmt, subr_die, depth);
14252       current_function_has_inlines = 1;
14253     }
14254   else
14255     /* We may get here if we're the outer block of function A that was
14256        inlined into function B that was inlined into function C.  When
14257        generating debugging info for C, dwarf2out_abstract_function(B)
14258        would mark all inlined blocks as abstract, including this one.
14259        So, we wouldn't (and shouldn't) expect labels to be generated
14260        for this one.  Instead, just emit debugging info for
14261        declarations within the block.  This is particularly important
14262        in the case of initializers of arguments passed from B to us:
14263        if they're statement expressions containing declarations, we
14264        wouldn't generate dies for their abstract variables, and then,
14265        when generating dies for the real variables, we'd die (pun
14266        intended :-)  */
14267     gen_lexical_block_die (stmt, context_die, depth);
14268 }
14269
14270 /* Generate a DIE for a field in a record, or structure.  */
14271
14272 static void
14273 gen_field_die (tree decl, dw_die_ref context_die)
14274 {
14275   dw_die_ref decl_die;
14276
14277   if (TREE_TYPE (decl) == error_mark_node)
14278     return;
14279
14280   decl_die = new_die (DW_TAG_member, context_die, decl);
14281   add_name_and_src_coords_attributes (decl_die, decl);
14282   add_type_attribute (decl_die, member_declared_type (decl),
14283                       TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
14284                       context_die);
14285
14286   if (DECL_BIT_FIELD_TYPE (decl))
14287     {
14288       add_byte_size_attribute (decl_die, decl);
14289       add_bit_size_attribute (decl_die, decl);
14290       add_bit_offset_attribute (decl_die, decl);
14291     }
14292
14293   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
14294     add_data_member_location_attribute (decl_die, decl);
14295
14296   if (DECL_ARTIFICIAL (decl))
14297     add_AT_flag (decl_die, DW_AT_artificial, 1);
14298
14299   if (TREE_PROTECTED (decl))
14300     add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_protected);
14301   else if (TREE_PRIVATE (decl))
14302     add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_private);
14303
14304   /* Equate decl number to die, so that we can look up this decl later on.  */
14305   equate_decl_number_to_die (decl, decl_die);
14306 }
14307
14308 #if 0
14309 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
14310    Use modified_type_die instead.
14311    We keep this code here just in case these types of DIEs may be needed to
14312    represent certain things in other languages (e.g. Pascal) someday.  */
14313
14314 static void
14315 gen_pointer_type_die (tree type, dw_die_ref context_die)
14316 {
14317   dw_die_ref ptr_die
14318     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
14319
14320   equate_type_number_to_die (type, ptr_die);
14321   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
14322   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
14323 }
14324
14325 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
14326    Use modified_type_die instead.
14327    We keep this code here just in case these types of DIEs may be needed to
14328    represent certain things in other languages (e.g. Pascal) someday.  */
14329
14330 static void
14331 gen_reference_type_die (tree type, dw_die_ref context_die)
14332 {
14333   dw_die_ref ref_die
14334     = new_die (DW_TAG_reference_type, scope_die_for (type, context_die), type);
14335
14336   equate_type_number_to_die (type, ref_die);
14337   add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
14338   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
14339 }
14340 #endif
14341
14342 /* Generate a DIE for a pointer to a member type.  */
14343
14344 static void
14345 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
14346 {
14347   dw_die_ref ptr_die
14348     = new_die (DW_TAG_ptr_to_member_type,
14349                scope_die_for (type, context_die), type);
14350
14351   equate_type_number_to_die (type, ptr_die);
14352   add_AT_die_ref (ptr_die, DW_AT_containing_type,
14353                   lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
14354   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
14355 }
14356
14357 /* Generate the DIE for the compilation unit.  */
14358
14359 static dw_die_ref
14360 gen_compile_unit_die (const char *filename)
14361 {
14362   dw_die_ref die;
14363   char producer[250];
14364   const char *language_string = lang_hooks.name;
14365   int language;
14366
14367   die = new_die (DW_TAG_compile_unit, NULL, NULL);
14368
14369   if (filename)
14370     {
14371       add_name_attribute (die, filename);
14372       /* Don't add cwd for <built-in>.  */
14373       if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
14374         add_comp_dir_attribute (die);
14375     }
14376
14377   sprintf (producer, "%s %s", language_string, version_string);
14378
14379 #ifdef MIPS_DEBUGGING_INFO
14380   /* The MIPS/SGI compilers place the 'cc' command line options in the producer
14381      string.  The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
14382      not appear in the producer string, the debugger reaches the conclusion
14383      that the object file is stripped and has no debugging information.
14384      To get the MIPS/SGI debugger to believe that there is debugging
14385      information in the object file, we add a -g to the producer string.  */
14386   if (debug_info_level > DINFO_LEVEL_TERSE)
14387     strcat (producer, " -g");
14388 #endif
14389
14390   add_AT_string (die, DW_AT_producer, producer);
14391
14392   if (strcmp (language_string, "GNU C++") == 0)
14393     language = DW_LANG_C_plus_plus;
14394   else if (strcmp (language_string, "GNU Ada") == 0)
14395     language = DW_LANG_Ada95;
14396   else if (strcmp (language_string, "GNU F77") == 0)
14397     language = DW_LANG_Fortran77;
14398   else if (strcmp (language_string, "GNU Fortran") == 0)
14399     language = DW_LANG_Fortran95;
14400   else if (strcmp (language_string, "GNU Pascal") == 0)
14401     language = DW_LANG_Pascal83;
14402   else if (strcmp (language_string, "GNU Java") == 0)
14403     language = DW_LANG_Java;
14404   else if (strcmp (language_string, "GNU Objective-C") == 0)
14405     language = DW_LANG_ObjC;
14406   else if (strcmp (language_string, "GNU Objective-C++") == 0)
14407     language = DW_LANG_ObjC_plus_plus;
14408   else
14409     language = DW_LANG_C89;
14410
14411   add_AT_unsigned (die, DW_AT_language, language);
14412   return die;
14413 }
14414
14415 /* Generate the DIE for a base class.  */
14416
14417 static void
14418 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
14419 {
14420   dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
14421
14422   add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
14423   add_data_member_location_attribute (die, binfo);
14424
14425   if (BINFO_VIRTUAL_P (binfo))
14426     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
14427
14428   if (access == access_public_node)
14429     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
14430   else if (access == access_protected_node)
14431     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
14432 }
14433
14434 /* Generate a DIE for a class member.  */
14435
14436 static void
14437 gen_member_die (tree type, dw_die_ref context_die)
14438 {
14439   tree member;
14440   tree binfo = TYPE_BINFO (type);
14441   dw_die_ref child;
14442
14443   /* If this is not an incomplete type, output descriptions of each of its
14444      members. Note that as we output the DIEs necessary to represent the
14445      members of this record or union type, we will also be trying to output
14446      DIEs to represent the *types* of those members. However the `type'
14447      function (above) will specifically avoid generating type DIEs for member
14448      types *within* the list of member DIEs for this (containing) type except
14449      for those types (of members) which are explicitly marked as also being
14450      members of this (containing) type themselves.  The g++ front- end can
14451      force any given type to be treated as a member of some other (containing)
14452      type by setting the TYPE_CONTEXT of the given (member) type to point to
14453      the TREE node representing the appropriate (containing) type.  */
14454
14455   /* First output info about the base classes.  */
14456   if (binfo)
14457     {
14458       VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
14459       int i;
14460       tree base;
14461
14462       for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
14463         gen_inheritance_die (base,
14464                              (accesses ? VEC_index (tree, accesses, i)
14465                               : access_public_node), context_die);
14466     }
14467
14468   /* Now output info about the data members and type members.  */
14469   for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member))
14470     {
14471       /* If we thought we were generating minimal debug info for TYPE
14472          and then changed our minds, some of the member declarations
14473          may have already been defined.  Don't define them again, but
14474          do put them in the right order.  */
14475
14476       child = lookup_decl_die (member);
14477       if (child)
14478         splice_child_die (context_die, child);
14479       else
14480         gen_decl_die (member, NULL, context_die);
14481     }
14482
14483   /* Now output info about the function members (if any).  */
14484   for (member = TYPE_METHODS (type); member; member = TREE_CHAIN (member))
14485     {
14486       /* Don't include clones in the member list.  */
14487       if (DECL_ABSTRACT_ORIGIN (member))
14488         continue;
14489
14490       child = lookup_decl_die (member);
14491       if (child)
14492         splice_child_die (context_die, child);
14493       else
14494         gen_decl_die (member, NULL, context_die);
14495     }
14496 }
14497
14498 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
14499    is set, we pretend that the type was never defined, so we only get the
14500    member DIEs needed by later specification DIEs.  */
14501
14502 static void
14503 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
14504                                 enum debug_info_usage usage)
14505 {
14506   dw_die_ref type_die = lookup_type_die (type);
14507   dw_die_ref scope_die = 0;
14508   int nested = 0;
14509   int complete = (TYPE_SIZE (type)
14510                   && (! TYPE_STUB_DECL (type)
14511                       || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
14512   int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
14513   complete = complete && should_emit_struct_debug (type, usage);
14514
14515   if (type_die && ! complete)
14516     return;
14517
14518   if (TYPE_CONTEXT (type) != NULL_TREE
14519       && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
14520           || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
14521     nested = 1;
14522
14523   scope_die = scope_die_for (type, context_die);
14524
14525   if (! type_die || (nested && scope_die == comp_unit_die))
14526     /* First occurrence of type or toplevel definition of nested class.  */
14527     {
14528       dw_die_ref old_die = type_die;
14529
14530       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
14531                           ? record_type_tag (type) : DW_TAG_union_type,
14532                           scope_die, type);
14533       equate_type_number_to_die (type, type_die);
14534       if (old_die)
14535         add_AT_specification (type_die, old_die);
14536       else
14537         add_name_attribute (type_die, type_tag (type));
14538     }
14539   else
14540     remove_AT (type_die, DW_AT_declaration);
14541
14542   /* If this type has been completed, then give it a byte_size attribute and
14543      then give a list of members.  */
14544   if (complete && !ns_decl)
14545     {
14546       /* Prevent infinite recursion in cases where the type of some member of
14547          this type is expressed in terms of this type itself.  */
14548       TREE_ASM_WRITTEN (type) = 1;
14549       add_byte_size_attribute (type_die, type);
14550       if (TYPE_STUB_DECL (type) != NULL_TREE)
14551         add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
14552
14553       /* If the first reference to this type was as the return type of an
14554          inline function, then it may not have a parent.  Fix this now.  */
14555       if (type_die->die_parent == NULL)
14556         add_child_die (scope_die, type_die);
14557
14558       push_decl_scope (type);
14559       gen_member_die (type, type_die);
14560       pop_decl_scope ();
14561
14562       /* GNU extension: Record what type our vtable lives in.  */
14563       if (TYPE_VFIELD (type))
14564         {
14565           tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
14566
14567           gen_type_die (vtype, context_die);
14568           add_AT_die_ref (type_die, DW_AT_containing_type,
14569                           lookup_type_die (vtype));
14570         }
14571     }
14572   else
14573     {
14574       add_AT_flag (type_die, DW_AT_declaration, 1);
14575
14576       /* We don't need to do this for function-local types.  */
14577       if (TYPE_STUB_DECL (type)
14578           && ! decl_function_context (TYPE_STUB_DECL (type)))
14579         VEC_safe_push (tree, gc, incomplete_types, type);
14580     }
14581
14582   if (get_AT (type_die, DW_AT_name))
14583     add_pubtype (type, type_die);
14584 }
14585
14586 /* Generate a DIE for a subroutine _type_.  */
14587
14588 static void
14589 gen_subroutine_type_die (tree type, dw_die_ref context_die)
14590 {
14591   tree return_type = TREE_TYPE (type);
14592   dw_die_ref subr_die
14593     = new_die (DW_TAG_subroutine_type,
14594                scope_die_for (type, context_die), type);
14595
14596   equate_type_number_to_die (type, subr_die);
14597   add_prototyped_attribute (subr_die, type);
14598   add_type_attribute (subr_die, return_type, 0, 0, context_die);
14599   gen_formal_types_die (type, subr_die);
14600
14601   if (get_AT (subr_die, DW_AT_name))
14602     add_pubtype (type, subr_die);
14603 }
14604
14605 /* Generate a DIE for a type definition.  */
14606
14607 static void
14608 gen_typedef_die (tree decl, dw_die_ref context_die)
14609 {
14610   dw_die_ref type_die;
14611   tree origin;
14612
14613   if (TREE_ASM_WRITTEN (decl))
14614     return;
14615
14616   TREE_ASM_WRITTEN (decl) = 1;
14617   type_die = new_die (DW_TAG_typedef, context_die, decl);
14618   origin = decl_ultimate_origin (decl);
14619   if (origin != NULL)
14620     add_abstract_origin_attribute (type_die, origin);
14621   else
14622     {
14623       tree type;
14624
14625       add_name_and_src_coords_attributes (type_die, decl);
14626       if (DECL_ORIGINAL_TYPE (decl))
14627         {
14628           type = DECL_ORIGINAL_TYPE (decl);
14629
14630           gcc_assert (type != TREE_TYPE (decl));
14631           equate_type_number_to_die (TREE_TYPE (decl), type_die);
14632         }
14633       else
14634         type = TREE_TYPE (decl);
14635
14636       add_type_attribute (type_die, type, TREE_READONLY (decl),
14637                           TREE_THIS_VOLATILE (decl), context_die);
14638     }
14639
14640   if (DECL_ABSTRACT (decl))
14641     equate_decl_number_to_die (decl, type_die);
14642
14643   if (get_AT (type_die, DW_AT_name))
14644     add_pubtype (decl, type_die);
14645 }
14646
14647 /* Generate a type description DIE.  */
14648
14649 static void
14650 gen_type_die_with_usage (tree type, dw_die_ref context_die,
14651                                 enum debug_info_usage usage)
14652 {
14653   int need_pop;
14654   struct array_descr_info info;
14655
14656   if (type == NULL_TREE || type == error_mark_node)
14657     return;
14658
14659   if (TYPE_NAME (type) && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
14660       && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
14661     {
14662       if (TREE_ASM_WRITTEN (type))
14663         return;
14664
14665       /* Prevent broken recursion; we can't hand off to the same type.  */
14666       gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
14667
14668       TREE_ASM_WRITTEN (type) = 1;
14669       gen_decl_die (TYPE_NAME (type), NULL, context_die);
14670       return;
14671     }
14672
14673   /* If this is an array type with hidden descriptor, handle it first.  */
14674   if (!TREE_ASM_WRITTEN (type)
14675       && lang_hooks.types.get_array_descr_info
14676       && lang_hooks.types.get_array_descr_info (type, &info))
14677     {
14678       gen_descr_array_type_die (type, &info, context_die);
14679       TREE_ASM_WRITTEN (type) = 1;
14680       return;
14681     }
14682
14683   /* We are going to output a DIE to represent the unqualified version
14684      of this type (i.e. without any const or volatile qualifiers) so
14685      get the main variant (i.e. the unqualified version) of this type
14686      now.  (Vectors are special because the debugging info is in the
14687      cloned type itself).  */
14688   if (TREE_CODE (type) != VECTOR_TYPE)
14689     type = type_main_variant (type);
14690
14691   if (TREE_ASM_WRITTEN (type))
14692     return;
14693
14694   switch (TREE_CODE (type))
14695     {
14696     case ERROR_MARK:
14697       break;
14698
14699     case POINTER_TYPE:
14700     case REFERENCE_TYPE:
14701       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
14702          ensures that the gen_type_die recursion will terminate even if the
14703          type is recursive.  Recursive types are possible in Ada.  */
14704       /* ??? We could perhaps do this for all types before the switch
14705          statement.  */
14706       TREE_ASM_WRITTEN (type) = 1;
14707
14708       /* For these types, all that is required is that we output a DIE (or a
14709          set of DIEs) to represent the "basis" type.  */
14710       gen_type_die_with_usage (TREE_TYPE (type), context_die,
14711                                 DINFO_USAGE_IND_USE);
14712       break;
14713
14714     case OFFSET_TYPE:
14715       /* This code is used for C++ pointer-to-data-member types.
14716          Output a description of the relevant class type.  */
14717       gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
14718                                         DINFO_USAGE_IND_USE);
14719
14720       /* Output a description of the type of the object pointed to.  */
14721       gen_type_die_with_usage (TREE_TYPE (type), context_die,
14722                                         DINFO_USAGE_IND_USE);
14723
14724       /* Now output a DIE to represent this pointer-to-data-member type
14725          itself.  */
14726       gen_ptr_to_mbr_type_die (type, context_die);
14727       break;
14728
14729     case FUNCTION_TYPE:
14730       /* Force out return type (in case it wasn't forced out already).  */
14731       gen_type_die_with_usage (TREE_TYPE (type), context_die,
14732                                         DINFO_USAGE_DIR_USE);
14733       gen_subroutine_type_die (type, context_die);
14734       break;
14735
14736     case METHOD_TYPE:
14737       /* Force out return type (in case it wasn't forced out already).  */
14738       gen_type_die_with_usage (TREE_TYPE (type), context_die,
14739                                         DINFO_USAGE_DIR_USE);
14740       gen_subroutine_type_die (type, context_die);
14741       break;
14742
14743     case ARRAY_TYPE:
14744       gen_array_type_die (type, context_die);
14745       break;
14746
14747     case VECTOR_TYPE:
14748       gen_array_type_die (type, context_die);
14749       break;
14750
14751     case ENUMERAL_TYPE:
14752     case RECORD_TYPE:
14753     case UNION_TYPE:
14754     case QUAL_UNION_TYPE:
14755       /* If this is a nested type whose containing class hasn't been written
14756          out yet, writing it out will cover this one, too.  This does not apply
14757          to instantiations of member class templates; they need to be added to
14758          the containing class as they are generated.  FIXME: This hurts the
14759          idea of combining type decls from multiple TUs, since we can't predict
14760          what set of template instantiations we'll get.  */
14761       if (TYPE_CONTEXT (type)
14762           && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
14763           && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
14764         {
14765           gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
14766
14767           if (TREE_ASM_WRITTEN (type))
14768             return;
14769
14770           /* If that failed, attach ourselves to the stub.  */
14771           push_decl_scope (TYPE_CONTEXT (type));
14772           context_die = lookup_type_die (TYPE_CONTEXT (type));
14773           need_pop = 1;
14774         }
14775       else
14776         {
14777           context_die = declare_in_namespace (type, context_die);
14778           need_pop = 0;
14779         }
14780
14781       if (TREE_CODE (type) == ENUMERAL_TYPE)
14782         {
14783           /* This might have been written out by the call to
14784              declare_in_namespace.  */
14785           if (!TREE_ASM_WRITTEN (type))
14786             gen_enumeration_type_die (type, context_die);
14787         }
14788       else
14789         gen_struct_or_union_type_die (type, context_die, usage);
14790
14791       if (need_pop)
14792         pop_decl_scope ();
14793
14794       /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
14795          it up if it is ever completed.  gen_*_type_die will set it for us
14796          when appropriate.  */
14797       return;
14798
14799     case VOID_TYPE:
14800     case INTEGER_TYPE:
14801     case REAL_TYPE:
14802     case FIXED_POINT_TYPE:
14803     case COMPLEX_TYPE:
14804     case BOOLEAN_TYPE:
14805       /* No DIEs needed for fundamental types.  */
14806       break;
14807
14808     case LANG_TYPE:
14809       /* No Dwarf representation currently defined.  */
14810       break;
14811
14812     default:
14813       gcc_unreachable ();
14814     }
14815
14816   TREE_ASM_WRITTEN (type) = 1;
14817 }
14818
14819 static void
14820 gen_type_die (tree type, dw_die_ref context_die)
14821 {
14822   gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
14823 }
14824
14825 /* Generate a DIE for a tagged type instantiation.  */
14826
14827 static void
14828 gen_tagged_type_instantiation_die (tree type, dw_die_ref context_die)
14829 {
14830   if (type == NULL_TREE || type == error_mark_node)
14831     return;
14832
14833   /* We are going to output a DIE to represent the unqualified version of
14834      this type (i.e. without any const or volatile qualifiers) so make sure
14835      that we have the main variant (i.e. the unqualified version) of this
14836      type now.  */
14837   gcc_assert (type == type_main_variant (type));
14838
14839   /* Do not check TREE_ASM_WRITTEN (type) as it may not be set if this is
14840      an instance of an unresolved type.  */
14841
14842   switch (TREE_CODE (type))
14843     {
14844     case ERROR_MARK:
14845       break;
14846
14847     case ENUMERAL_TYPE:
14848       gen_inlined_enumeration_type_die (type, context_die);
14849       break;
14850
14851     case RECORD_TYPE:
14852       gen_inlined_structure_type_die (type, context_die);
14853       break;
14854
14855     case UNION_TYPE:
14856     case QUAL_UNION_TYPE:
14857       gen_inlined_union_type_die (type, context_die);
14858       break;
14859
14860     default:
14861       gcc_unreachable ();
14862     }
14863 }
14864
14865 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
14866    things which are local to the given block.  */
14867
14868 static void
14869 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
14870 {
14871   int must_output_die = 0;
14872   bool inlined_func;
14873
14874   /* Ignore blocks that are NULL.  */
14875   if (stmt == NULL_TREE)
14876     return;
14877
14878   inlined_func = inlined_function_outer_scope_p (stmt);
14879
14880   /* If the block is one fragment of a non-contiguous block, do not
14881      process the variables, since they will have been done by the
14882      origin block.  Do process subblocks.  */
14883   if (BLOCK_FRAGMENT_ORIGIN (stmt))
14884     {
14885       tree sub;
14886
14887       for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
14888         gen_block_die (sub, context_die, depth + 1);
14889
14890       return;
14891     }
14892
14893   /* Determine if we need to output any Dwarf DIEs at all to represent this
14894      block.  */
14895   if (inlined_func)
14896     /* The outer scopes for inlinings *must* always be represented.  We
14897        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
14898     must_output_die = 1;
14899   else
14900     {
14901       /* Determine if this block directly contains any "significant"
14902          local declarations which we will need to output DIEs for.  */
14903       if (debug_info_level > DINFO_LEVEL_TERSE)
14904         /* We are not in terse mode so *any* local declaration counts
14905            as being a "significant" one.  */
14906         must_output_die = ((BLOCK_VARS (stmt) != NULL
14907                             || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
14908                            && (TREE_USED (stmt)
14909                                || TREE_ASM_WRITTEN (stmt)
14910                                || BLOCK_ABSTRACT (stmt)));
14911       else if ((TREE_USED (stmt)
14912                 || TREE_ASM_WRITTEN (stmt)
14913                 || BLOCK_ABSTRACT (stmt))
14914                && !dwarf2out_ignore_block (stmt))
14915         must_output_die = 1;
14916     }
14917
14918   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
14919      DIE for any block which contains no significant local declarations at
14920      all.  Rather, in such cases we just call `decls_for_scope' so that any
14921      needed Dwarf info for any sub-blocks will get properly generated. Note
14922      that in terse mode, our definition of what constitutes a "significant"
14923      local declaration gets restricted to include only inlined function
14924      instances and local (nested) function definitions.  */
14925   if (must_output_die)
14926     {
14927       if (inlined_func)
14928         gen_inlined_subroutine_die (stmt, context_die, depth);
14929       else
14930         gen_lexical_block_die (stmt, context_die, depth);
14931     }
14932   else
14933     decls_for_scope (stmt, context_die, depth);
14934 }
14935
14936 /* Process variable DECL (or variable with origin ORIGIN) within
14937    block STMT and add it to CONTEXT_DIE.  */
14938 static void
14939 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
14940 {
14941   dw_die_ref die;
14942   tree decl_or_origin = decl ? decl : origin;
14943   tree ultimate_origin = origin ? decl_ultimate_origin (origin) : NULL;
14944
14945   if (ultimate_origin)
14946     origin = ultimate_origin;
14947
14948   if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
14949     die = lookup_decl_die (decl_or_origin);
14950   else if (TREE_CODE (decl_or_origin) == TYPE_DECL
14951            && TYPE_DECL_IS_STUB (decl_or_origin))
14952     die = lookup_type_die (TREE_TYPE (decl_or_origin));
14953   else
14954     die = NULL;
14955
14956   if (die != NULL && die->die_parent == NULL)
14957     add_child_die (context_die, die);
14958   else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
14959     dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
14960                                          stmt, context_die);
14961   else
14962     gen_decl_die (decl, origin, context_die);
14963 }
14964
14965 /* Generate all of the decls declared within a given scope and (recursively)
14966    all of its sub-blocks.  */
14967
14968 static void
14969 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
14970 {
14971   tree decl;
14972   unsigned int i;
14973   tree subblocks;
14974
14975   /* Ignore NULL blocks.  */
14976   if (stmt == NULL_TREE)
14977     return;
14978
14979   /* Output the DIEs to represent all of the data objects and typedefs
14980      declared directly within this block but not within any nested
14981      sub-blocks.  Also, nested function and tag DIEs have been
14982      generated with a parent of NULL; fix that up now.  */
14983   for (decl = BLOCK_VARS (stmt); decl != NULL; decl = TREE_CHAIN (decl))
14984     process_scope_var (stmt, decl, NULL_TREE, context_die);
14985   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
14986     process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
14987                        context_die);
14988
14989   /* If we're at -g1, we're not interested in subblocks.  */
14990   if (debug_info_level <= DINFO_LEVEL_TERSE)
14991     return;
14992
14993   /* Output the DIEs to represent all sub-blocks (and the items declared
14994      therein) of this block.  */
14995   for (subblocks = BLOCK_SUBBLOCKS (stmt);
14996        subblocks != NULL;
14997        subblocks = BLOCK_CHAIN (subblocks))
14998     gen_block_die (subblocks, context_die, depth + 1);
14999 }
15000
15001 /* Is this a typedef we can avoid emitting?  */
15002
15003 static inline int
15004 is_redundant_typedef (const_tree decl)
15005 {
15006   if (TYPE_DECL_IS_STUB (decl))
15007     return 1;
15008
15009   if (DECL_ARTIFICIAL (decl)
15010       && DECL_CONTEXT (decl)
15011       && is_tagged_type (DECL_CONTEXT (decl))
15012       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
15013       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
15014     /* Also ignore the artificial member typedef for the class name.  */
15015     return 1;
15016
15017   return 0;
15018 }
15019
15020 /* Returns the DIE for a context.  */
15021
15022 static inline dw_die_ref
15023 get_context_die (tree context)
15024 {
15025   if (context)
15026     {
15027       /* Find die that represents this context.  */
15028       if (TYPE_P (context))
15029         return force_type_die (context);
15030       else
15031         return force_decl_die (context);
15032     }
15033   return comp_unit_die;
15034 }
15035
15036 /* Returns the DIE for decl.  A DIE will always be returned.  */
15037
15038 static dw_die_ref
15039 force_decl_die (tree decl)
15040 {
15041   dw_die_ref decl_die;
15042   unsigned saved_external_flag;
15043   tree save_fn = NULL_TREE;
15044   decl_die = lookup_decl_die (decl);
15045   if (!decl_die)
15046     {
15047       dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
15048
15049       decl_die = lookup_decl_die (decl);
15050       if (decl_die)
15051         return decl_die;
15052
15053       switch (TREE_CODE (decl))
15054         {
15055         case FUNCTION_DECL:
15056           /* Clear current_function_decl, so that gen_subprogram_die thinks
15057              that this is a declaration. At this point, we just want to force
15058              declaration die.  */
15059           save_fn = current_function_decl;
15060           current_function_decl = NULL_TREE;
15061           gen_subprogram_die (decl, context_die);
15062           current_function_decl = save_fn;
15063           break;
15064
15065         case VAR_DECL:
15066           /* Set external flag to force declaration die. Restore it after
15067            gen_decl_die() call.  */
15068           saved_external_flag = DECL_EXTERNAL (decl);
15069           DECL_EXTERNAL (decl) = 1;
15070           gen_decl_die (decl, NULL, context_die);
15071           DECL_EXTERNAL (decl) = saved_external_flag;
15072           break;
15073
15074         case NAMESPACE_DECL:
15075           dwarf2out_decl (decl);
15076           break;
15077
15078         default:
15079           gcc_unreachable ();
15080         }
15081
15082       /* We should be able to find the DIE now.  */
15083       if (!decl_die)
15084         decl_die = lookup_decl_die (decl);
15085       gcc_assert (decl_die);
15086     }
15087
15088   return decl_die;
15089 }
15090
15091 /* Returns the DIE for TYPE, that must not be a base type.  A DIE is
15092    always returned.  */
15093
15094 static dw_die_ref
15095 force_type_die (tree type)
15096 {
15097   dw_die_ref type_die;
15098
15099   type_die = lookup_type_die (type);
15100   if (!type_die)
15101     {
15102       dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
15103
15104       type_die = modified_type_die (type, TYPE_READONLY (type),
15105                                     TYPE_VOLATILE (type), context_die);
15106       gcc_assert (type_die);
15107     }
15108   return type_die;
15109 }
15110
15111 /* Force out any required namespaces to be able to output DECL,
15112    and return the new context_die for it, if it's changed.  */
15113
15114 static dw_die_ref
15115 setup_namespace_context (tree thing, dw_die_ref context_die)
15116 {
15117   tree context = (DECL_P (thing)
15118                   ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
15119   if (context && TREE_CODE (context) == NAMESPACE_DECL)
15120     /* Force out the namespace.  */
15121     context_die = force_decl_die (context);
15122
15123   return context_die;
15124 }
15125
15126 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
15127    type) within its namespace, if appropriate.
15128
15129    For compatibility with older debuggers, namespace DIEs only contain
15130    declarations; all definitions are emitted at CU scope.  */
15131
15132 static dw_die_ref
15133 declare_in_namespace (tree thing, dw_die_ref context_die)
15134 {
15135   dw_die_ref ns_context;
15136
15137   if (debug_info_level <= DINFO_LEVEL_TERSE)
15138     return context_die;
15139
15140   /* If this decl is from an inlined function, then don't try to emit it in its
15141      namespace, as we will get confused.  It would have already been emitted
15142      when the abstract instance of the inline function was emitted anyways.  */
15143   if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
15144     return context_die;
15145
15146   ns_context = setup_namespace_context (thing, context_die);
15147
15148   if (ns_context != context_die)
15149     {
15150       if (is_fortran ())
15151         return ns_context;
15152       if (DECL_P (thing))
15153         gen_decl_die (thing, NULL, ns_context);
15154       else
15155         gen_type_die (thing, ns_context);
15156     }
15157   return context_die;
15158 }
15159
15160 /* Generate a DIE for a namespace or namespace alias.  */
15161
15162 static void
15163 gen_namespace_die (tree decl)
15164 {
15165   dw_die_ref context_die = setup_namespace_context (decl, comp_unit_die);
15166
15167   /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
15168      they are an alias of.  */
15169   if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
15170     {
15171       /* Output a real namespace or module.  */
15172       dw_die_ref namespace_die
15173         = new_die (is_fortran () ? DW_TAG_module : DW_TAG_namespace,
15174                    context_die, decl);
15175       /* For Fortran modules defined in different CU don't add src coords.  */
15176       if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
15177         add_name_attribute (namespace_die, dwarf2_name (decl, 0));
15178       else
15179         add_name_and_src_coords_attributes (namespace_die, decl);
15180       if (DECL_EXTERNAL (decl))
15181         add_AT_flag (namespace_die, DW_AT_declaration, 1);
15182       equate_decl_number_to_die (decl, namespace_die);
15183     }
15184   else
15185     {
15186       /* Output a namespace alias.  */
15187
15188       /* Force out the namespace we are an alias of, if necessary.  */
15189       dw_die_ref origin_die
15190         = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
15191
15192       /* Now create the namespace alias DIE.  */
15193       dw_die_ref namespace_die
15194         = new_die (DW_TAG_imported_declaration, context_die, decl);
15195       add_name_and_src_coords_attributes (namespace_die, decl);
15196       add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
15197       equate_decl_number_to_die (decl, namespace_die);
15198     }
15199 }
15200
15201 /* Generate Dwarf debug information for a decl described by DECL.  */
15202
15203 static void
15204 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
15205 {
15206   tree decl_or_origin = decl ? decl : origin;
15207   tree class_origin = NULL;
15208
15209   if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
15210     return;
15211
15212   switch (TREE_CODE (decl_or_origin))
15213     {
15214     case ERROR_MARK:
15215       break;
15216
15217     case CONST_DECL:
15218       if (!is_fortran ())
15219         {
15220           /* The individual enumerators of an enum type get output when we output
15221              the Dwarf representation of the relevant enum type itself.  */
15222           break;
15223         }
15224
15225       /* Emit its type.  */
15226       gen_type_die (TREE_TYPE (decl), context_die);
15227
15228       /* And its containing namespace.  */
15229       context_die = declare_in_namespace (decl, context_die);
15230
15231       gen_const_die (decl, context_die);
15232       break;
15233
15234     case FUNCTION_DECL:
15235       /* Don't output any DIEs to represent mere function declarations,
15236          unless they are class members or explicit block externs.  */
15237       if (DECL_INITIAL (decl_or_origin) == NULL_TREE
15238           && DECL_CONTEXT (decl_or_origin) == NULL_TREE
15239           && (current_function_decl == NULL_TREE
15240               || DECL_ARTIFICIAL (decl_or_origin)))
15241         break;
15242
15243 #if 0
15244       /* FIXME */
15245       /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
15246          on local redeclarations of global functions.  That seems broken.  */
15247       if (current_function_decl != decl)
15248         /* This is only a declaration.  */;
15249 #endif
15250
15251       /* If we're emitting a clone, emit info for the abstract instance.  */
15252       if (origin || DECL_ORIGIN (decl) != decl)
15253         dwarf2out_abstract_function (origin ? origin : DECL_ABSTRACT_ORIGIN (decl));
15254
15255       /* If we're emitting an out-of-line copy of an inline function,
15256          emit info for the abstract instance and set up to refer to it.  */
15257       else if (cgraph_function_possibly_inlined_p (decl)
15258                && ! DECL_ABSTRACT (decl)
15259                && ! class_or_namespace_scope_p (context_die)
15260                /* dwarf2out_abstract_function won't emit a die if this is just
15261                   a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
15262                   that case, because that works only if we have a die.  */
15263                && DECL_INITIAL (decl) != NULL_TREE)
15264         {
15265           dwarf2out_abstract_function (decl);
15266           set_decl_origin_self (decl);
15267         }
15268
15269       /* Otherwise we're emitting the primary DIE for this decl.  */
15270       else if (debug_info_level > DINFO_LEVEL_TERSE)
15271         {
15272           /* Before we describe the FUNCTION_DECL itself, make sure that we
15273              have described its return type.  */
15274           gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
15275
15276           /* And its virtual context.  */
15277           if (DECL_VINDEX (decl) != NULL_TREE)
15278             gen_type_die (DECL_CONTEXT (decl), context_die);
15279
15280           /* And its containing type.  */
15281           if (!origin)
15282             origin = decl_class_context (decl);
15283           if (origin != NULL_TREE)
15284             gen_type_die_for_member (origin, decl, context_die);
15285
15286           /* And its containing namespace.  */
15287           context_die = declare_in_namespace (decl, context_die);
15288         }
15289
15290       /* Now output a DIE to represent the function itself.  */
15291       if (decl)
15292         gen_subprogram_die (decl, context_die);
15293       break;
15294
15295     case TYPE_DECL:
15296       /* If we are in terse mode, don't generate any DIEs to represent any
15297          actual typedefs.  */
15298       if (debug_info_level <= DINFO_LEVEL_TERSE)
15299         break;
15300
15301       /* In the special case of a TYPE_DECL node representing the declaration
15302          of some type tag, if the given TYPE_DECL is marked as having been
15303          instantiated from some other (original) TYPE_DECL node (e.g. one which
15304          was generated within the original definition of an inline function) we
15305          have to generate a special (abbreviated) DW_TAG_structure_type,
15306          DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  */
15307       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE
15308           && is_tagged_type (TREE_TYPE (decl)))
15309         {
15310           gen_tagged_type_instantiation_die (TREE_TYPE (decl), context_die);
15311           break;
15312         }
15313
15314       if (is_redundant_typedef (decl))
15315         gen_type_die (TREE_TYPE (decl), context_die);
15316       else
15317         /* Output a DIE to represent the typedef itself.  */
15318         gen_typedef_die (decl, context_die);
15319       break;
15320
15321     case LABEL_DECL:
15322       if (debug_info_level >= DINFO_LEVEL_NORMAL)
15323         gen_label_die (decl, context_die);
15324       break;
15325
15326     case VAR_DECL:
15327     case RESULT_DECL:
15328       /* If we are in terse mode, don't generate any DIEs to represent any
15329          variable declarations or definitions.  */
15330       if (debug_info_level <= DINFO_LEVEL_TERSE)
15331         break;
15332
15333       /* Output any DIEs that are needed to specify the type of this data
15334          object.  */
15335       if (TREE_CODE (decl_or_origin) == RESULT_DECL
15336           && DECL_BY_REFERENCE (decl_or_origin))
15337         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
15338       else
15339         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
15340
15341       /* And its containing type.  */
15342       class_origin = decl_class_context (decl_or_origin);
15343       if (class_origin != NULL_TREE)
15344         gen_type_die_for_member (class_origin, decl_or_origin, context_die);
15345
15346       /* And its containing namespace.  */
15347       context_die = declare_in_namespace (decl_or_origin, context_die);
15348
15349       /* Now output the DIE to represent the data object itself.  This gets
15350          complicated because of the possibility that the VAR_DECL really
15351          represents an inlined instance of a formal parameter for an inline
15352          function.  */
15353       if (!origin)
15354         origin = decl_ultimate_origin (decl);
15355       if (origin != NULL_TREE && TREE_CODE (origin) == PARM_DECL)
15356         gen_formal_parameter_die (decl, origin, context_die);
15357       else
15358         gen_variable_die (decl, origin, context_die);
15359       break;
15360
15361     case FIELD_DECL:
15362       /* Ignore the nameless fields that are used to skip bits but handle C++
15363          anonymous unions and structs.  */
15364       if (DECL_NAME (decl) != NULL_TREE
15365           || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
15366           || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
15367         {
15368           gen_type_die (member_declared_type (decl), context_die);
15369           gen_field_die (decl, context_die);
15370         }
15371       break;
15372
15373     case PARM_DECL:
15374       if (DECL_BY_REFERENCE (decl_or_origin))
15375         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
15376       else
15377         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
15378       gen_formal_parameter_die (decl, origin, context_die);
15379       break;
15380
15381     case NAMESPACE_DECL:
15382     case IMPORTED_DECL:
15383       gen_namespace_die (decl);
15384       break;
15385
15386     default:
15387       /* Probably some frontend-internal decl.  Assume we don't care.  */
15388       gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
15389       break;
15390     }
15391 }
15392 \f
15393 /* Output debug information for global decl DECL.  Called from toplev.c after
15394    compilation proper has finished.  */
15395
15396 static void
15397 dwarf2out_global_decl (tree decl)
15398 {
15399   /* Output DWARF2 information for file-scope tentative data object
15400      declarations, file-scope (extern) function declarations (which
15401      had no corresponding body) and file-scope tagged type declarations
15402      and definitions which have not yet been forced out.  */
15403   if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
15404     dwarf2out_decl (decl);
15405 }
15406
15407 /* Output debug information for type decl DECL.  Called from toplev.c
15408    and from language front ends (to record built-in types).  */
15409 static void
15410 dwarf2out_type_decl (tree decl, int local)
15411 {
15412   if (!local)
15413     dwarf2out_decl (decl);
15414 }
15415
15416 /* Output debug information for imported module or decl DECL.
15417    NAME is non-NULL name in the lexical block if the decl has been renamed.
15418    LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
15419    that DECL belongs to.
15420    LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK.  */
15421 static void
15422 dwarf2out_imported_module_or_decl_1 (tree decl,
15423                                      tree name,
15424                                      tree lexical_block,
15425                                      dw_die_ref lexical_block_die)
15426 {
15427   expanded_location xloc;
15428   dw_die_ref imported_die = NULL;
15429   dw_die_ref at_import_die;
15430
15431   if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
15432     {
15433       if (is_base_type (TREE_TYPE (decl)))
15434         at_import_die = base_type_die (TREE_TYPE (decl));
15435       else
15436         at_import_die = force_type_die (TREE_TYPE (decl));
15437       /* For namespace N { typedef void T; } using N::T; base_type_die
15438          returns NULL, but DW_TAG_imported_declaration requires
15439          the DW_AT_import tag.  Force creation of DW_TAG_typedef.  */
15440       if (!at_import_die)
15441         {
15442           gcc_assert (TREE_CODE (decl) == TYPE_DECL);
15443           gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
15444           at_import_die = lookup_type_die (TREE_TYPE (decl));
15445           gcc_assert (at_import_die);
15446         }
15447     }
15448   else if (TREE_CODE (decl) == IMPORTED_DECL)
15449     {
15450       tree imported_ns_decl;
15451       /* IMPORTED_DECL nodes that are not imported namespace are just not
15452          supported yet.  */
15453       gcc_assert (DECL_INITIAL (decl)
15454                   && TREE_CODE (DECL_INITIAL (decl)) == NAMESPACE_DECL);
15455       imported_ns_decl = DECL_INITIAL (decl);
15456       at_import_die = lookup_decl_die (imported_ns_decl);
15457       if (!at_import_die)
15458         at_import_die = force_decl_die (imported_ns_decl);
15459       gcc_assert (at_import_die);
15460     }
15461   else
15462     {
15463       at_import_die = lookup_decl_die (decl);
15464       if (!at_import_die)
15465         {
15466           /* If we're trying to avoid duplicate debug info, we may not have
15467              emitted the member decl for this field.  Emit it now.  */
15468           if (TREE_CODE (decl) == FIELD_DECL)
15469             {
15470               tree type = DECL_CONTEXT (decl);
15471
15472               if (TYPE_CONTEXT (type)
15473                   && TYPE_P (TYPE_CONTEXT (type))
15474                   && !should_emit_struct_debug (TYPE_CONTEXT (type),
15475                                                 DINFO_USAGE_DIR_USE))
15476                 return;
15477               gen_type_die_for_member (type, decl,
15478                                        get_context_die (TYPE_CONTEXT (type)));
15479             }
15480           at_import_die = force_decl_die (decl);
15481         }
15482     }
15483
15484   if (TREE_CODE (decl) == NAMESPACE_DECL)
15485     imported_die = new_die (DW_TAG_imported_module,
15486                             lexical_block_die,
15487                             lexical_block);
15488   else
15489     imported_die = new_die (DW_TAG_imported_declaration,
15490                             lexical_block_die,
15491                             lexical_block);
15492
15493   xloc = expand_location (input_location);
15494   add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
15495   add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
15496   if (name)
15497     add_AT_string (imported_die, DW_AT_name,
15498                    IDENTIFIER_POINTER (name));
15499   add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
15500 }
15501
15502 /* Output debug information for imported module or decl DECL.
15503    NAME is non-NULL name in context if the decl has been renamed.
15504    CHILD is true if decl is one of the renamed decls as part of
15505    importing whole module.  */
15506
15507 static void
15508 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
15509                                    bool child)
15510 {
15511   /* dw_die_ref at_import_die;  */
15512   dw_die_ref scope_die;
15513
15514   if (debug_info_level <= DINFO_LEVEL_TERSE)
15515     return;
15516
15517   gcc_assert (decl);
15518
15519   /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
15520      We need decl DIE for reference and scope die. First, get DIE for the decl
15521      itself.  */
15522
15523   /* Get the scope die for decl context. Use comp_unit_die for global module
15524      or decl. If die is not found for non globals, force new die.  */
15525   if (context
15526       && TYPE_P (context)
15527       && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
15528     return;
15529   scope_die = get_context_die (context);
15530
15531   if (child)
15532     {
15533       gcc_assert (scope_die->die_child);
15534       gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
15535       gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
15536       scope_die = scope_die->die_child;
15537     }
15538
15539   /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
15540   dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
15541
15542 }
15543
15544 /* Write the debugging output for DECL.  */
15545
15546 void
15547 dwarf2out_decl (tree decl)
15548 {
15549   dw_die_ref context_die = comp_unit_die;
15550
15551   switch (TREE_CODE (decl))
15552     {
15553     case ERROR_MARK:
15554       return;
15555
15556     case FUNCTION_DECL:
15557       /* What we would really like to do here is to filter out all mere
15558          file-scope declarations of file-scope functions which are never
15559          referenced later within this translation unit (and keep all of ones
15560          that *are* referenced later on) but we aren't clairvoyant, so we have
15561          no idea which functions will be referenced in the future (i.e. later
15562          on within the current translation unit). So here we just ignore all
15563          file-scope function declarations which are not also definitions.  If
15564          and when the debugger needs to know something about these functions,
15565          it will have to hunt around and find the DWARF information associated
15566          with the definition of the function.
15567
15568          We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
15569          nodes represent definitions and which ones represent mere
15570          declarations.  We have to check DECL_INITIAL instead. That's because
15571          the C front-end supports some weird semantics for "extern inline"
15572          function definitions.  These can get inlined within the current
15573          translation unit (and thus, we need to generate Dwarf info for their
15574          abstract instances so that the Dwarf info for the concrete inlined
15575          instances can have something to refer to) but the compiler never
15576          generates any out-of-lines instances of such things (despite the fact
15577          that they *are* definitions).
15578
15579          The important point is that the C front-end marks these "extern
15580          inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
15581          them anyway. Note that the C++ front-end also plays some similar games
15582          for inline function definitions appearing within include files which
15583          also contain `#pragma interface' pragmas.  */
15584       if (DECL_INITIAL (decl) == NULL_TREE)
15585         return;
15586
15587       /* If we're a nested function, initially use a parent of NULL; if we're
15588          a plain function, this will be fixed up in decls_for_scope.  If
15589          we're a method, it will be ignored, since we already have a DIE.  */
15590       if (decl_function_context (decl)
15591           /* But if we're in terse mode, we don't care about scope.  */
15592           && debug_info_level > DINFO_LEVEL_TERSE)
15593         context_die = NULL;
15594       break;
15595
15596     case VAR_DECL:
15597       /* Ignore this VAR_DECL if it refers to a file-scope extern data object
15598          declaration and if the declaration was never even referenced from
15599          within this entire compilation unit.  We suppress these DIEs in
15600          order to save space in the .debug section (by eliminating entries
15601          which are probably useless).  Note that we must not suppress
15602          block-local extern declarations (whether used or not) because that
15603          would screw-up the debugger's name lookup mechanism and cause it to
15604          miss things which really ought to be in scope at a given point.  */
15605       if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
15606         return;
15607
15608       /* For local statics lookup proper context die.  */
15609       if (TREE_STATIC (decl) && decl_function_context (decl))
15610         context_die = lookup_decl_die (DECL_CONTEXT (decl));
15611
15612       /* If we are in terse mode, don't generate any DIEs to represent any
15613          variable declarations or definitions.  */
15614       if (debug_info_level <= DINFO_LEVEL_TERSE)
15615         return;
15616       break;
15617
15618     case CONST_DECL:
15619       if (debug_info_level <= DINFO_LEVEL_TERSE)
15620         return;
15621       if (!is_fortran ())
15622         return;
15623       if (TREE_STATIC (decl) && decl_function_context (decl))
15624         context_die = lookup_decl_die (DECL_CONTEXT (decl));
15625       break;
15626
15627     case NAMESPACE_DECL:
15628     case IMPORTED_DECL:
15629       if (debug_info_level <= DINFO_LEVEL_TERSE)
15630         return;
15631       if (lookup_decl_die (decl) != NULL)
15632         return;
15633       break;
15634
15635     case TYPE_DECL:
15636       /* Don't emit stubs for types unless they are needed by other DIEs.  */
15637       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
15638         return;
15639
15640       /* Don't bother trying to generate any DIEs to represent any of the
15641          normal built-in types for the language we are compiling.  */
15642       if (DECL_IS_BUILTIN (decl))
15643         {
15644           /* OK, we need to generate one for `bool' so GDB knows what type
15645              comparisons have.  */
15646           if (is_cxx ()
15647               && TREE_CODE (TREE_TYPE (decl)) == BOOLEAN_TYPE
15648               && ! DECL_IGNORED_P (decl))
15649             modified_type_die (TREE_TYPE (decl), 0, 0, NULL);
15650
15651           return;
15652         }
15653
15654       /* If we are in terse mode, don't generate any DIEs for types.  */
15655       if (debug_info_level <= DINFO_LEVEL_TERSE)
15656         return;
15657
15658       /* If we're a function-scope tag, initially use a parent of NULL;
15659          this will be fixed up in decls_for_scope.  */
15660       if (decl_function_context (decl))
15661         context_die = NULL;
15662
15663       break;
15664
15665     default:
15666       return;
15667     }
15668
15669   gen_decl_die (decl, NULL, context_die);
15670 }
15671
15672 /* Output a marker (i.e. a label) for the beginning of the generated code for
15673    a lexical block.  */
15674
15675 static void
15676 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
15677                        unsigned int blocknum)
15678 {
15679   switch_to_section (current_function_section ());
15680   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
15681 }
15682
15683 /* Output a marker (i.e. a label) for the end of the generated code for a
15684    lexical block.  */
15685
15686 static void
15687 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
15688 {
15689   switch_to_section (current_function_section ());
15690   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
15691 }
15692
15693 /* Returns nonzero if it is appropriate not to emit any debugging
15694    information for BLOCK, because it doesn't contain any instructions.
15695
15696    Don't allow this for blocks with nested functions or local classes
15697    as we would end up with orphans, and in the presence of scheduling
15698    we may end up calling them anyway.  */
15699
15700 static bool
15701 dwarf2out_ignore_block (const_tree block)
15702 {
15703   tree decl;
15704   unsigned int i;
15705
15706   for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
15707     if (TREE_CODE (decl) == FUNCTION_DECL
15708         || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
15709       return 0;
15710   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
15711     {
15712       decl = BLOCK_NONLOCALIZED_VAR (block, i);
15713       if (TREE_CODE (decl) == FUNCTION_DECL
15714           || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
15715       return 0;
15716     }
15717
15718   return 1;
15719 }
15720
15721 /* Hash table routines for file_hash.  */
15722
15723 static int
15724 file_table_eq (const void *p1_p, const void *p2_p)
15725 {
15726   const struct dwarf_file_data *const p1 =
15727     (const struct dwarf_file_data *) p1_p;
15728   const char *const p2 = (const char *) p2_p;
15729   return strcmp (p1->filename, p2) == 0;
15730 }
15731
15732 static hashval_t
15733 file_table_hash (const void *p_p)
15734 {
15735   const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
15736   return htab_hash_string (p->filename);
15737 }
15738
15739 /* Lookup FILE_NAME (in the list of filenames that we know about here in
15740    dwarf2out.c) and return its "index".  The index of each (known) filename is
15741    just a unique number which is associated with only that one filename.  We
15742    need such numbers for the sake of generating labels (in the .debug_sfnames
15743    section) and references to those files numbers (in the .debug_srcinfo
15744    and.debug_macinfo sections).  If the filename given as an argument is not
15745    found in our current list, add it to the list and assign it the next
15746    available unique index number.  In order to speed up searches, we remember
15747    the index of the filename was looked up last.  This handles the majority of
15748    all searches.  */
15749
15750 static struct dwarf_file_data *
15751 lookup_filename (const char *file_name)
15752 {
15753   void ** slot;
15754   struct dwarf_file_data * created;
15755
15756   /* Check to see if the file name that was searched on the previous
15757      call matches this file name.  If so, return the index.  */
15758   if (file_table_last_lookup
15759       && (file_name == file_table_last_lookup->filename
15760           || strcmp (file_table_last_lookup->filename, file_name) == 0))
15761     return file_table_last_lookup;
15762
15763   /* Didn't match the previous lookup, search the table.  */
15764   slot = htab_find_slot_with_hash (file_table, file_name,
15765                                    htab_hash_string (file_name), INSERT);
15766   if (*slot)
15767     return (struct dwarf_file_data *) *slot;
15768
15769   created = GGC_NEW (struct dwarf_file_data);
15770   created->filename = file_name;
15771   created->emitted_number = 0;
15772   *slot = created;
15773   return created;
15774 }
15775
15776 /* If the assembler will construct the file table, then translate the compiler
15777    internal file table number into the assembler file table number, and emit
15778    a .file directive if we haven't already emitted one yet.  The file table
15779    numbers are different because we prune debug info for unused variables and
15780    types, which may include filenames.  */
15781
15782 static int
15783 maybe_emit_file (struct dwarf_file_data * fd)
15784 {
15785   if (! fd->emitted_number)
15786     {
15787       if (last_emitted_file)
15788         fd->emitted_number = last_emitted_file->emitted_number + 1;
15789       else
15790         fd->emitted_number = 1;
15791       last_emitted_file = fd;
15792
15793       if (DWARF2_ASM_LINE_DEBUG_INFO)
15794         {
15795           fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
15796           output_quoted_string (asm_out_file,
15797                                 remap_debug_filename (fd->filename));
15798           fputc ('\n', asm_out_file);
15799         }
15800     }
15801
15802   return fd->emitted_number;
15803 }
15804
15805 /* Called by the final INSN scan whenever we see a var location.  We
15806    use it to drop labels in the right places, and throw the location in
15807    our lookup table.  */
15808
15809 static void
15810 dwarf2out_var_location (rtx loc_note)
15811 {
15812   char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
15813   struct var_loc_node *newloc;
15814   rtx prev_insn;
15815   static rtx last_insn;
15816   static const char *last_label;
15817   tree decl;
15818
15819   if (!DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
15820     return;
15821   prev_insn = PREV_INSN (loc_note);
15822
15823   newloc = GGC_CNEW (struct var_loc_node);
15824   /* If the insn we processed last time is the previous insn
15825      and it is also a var location note, use the label we emitted
15826      last time.  */
15827   if (last_insn != NULL_RTX
15828       && last_insn == prev_insn
15829       && NOTE_P (prev_insn)
15830       && NOTE_KIND (prev_insn) == NOTE_INSN_VAR_LOCATION)
15831     {
15832       newloc->label = last_label;
15833     }
15834   else
15835     {
15836       ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
15837       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
15838       loclabel_num++;
15839       newloc->label = ggc_strdup (loclabel);
15840     }
15841   newloc->var_loc_note = loc_note;
15842   newloc->next = NULL;
15843
15844   if (cfun && in_cold_section_p)
15845     newloc->section_label = crtl->subsections.cold_section_label;
15846   else
15847     newloc->section_label = text_section_label;
15848
15849   last_insn = loc_note;
15850   last_label = newloc->label;
15851   decl = NOTE_VAR_LOCATION_DECL (loc_note);
15852   add_var_loc_to_decl (decl, newloc);
15853 }
15854
15855 /* We need to reset the locations at the beginning of each
15856    function. We can't do this in the end_function hook, because the
15857    declarations that use the locations won't have been output when
15858    that hook is called.  Also compute have_multiple_function_sections here.  */
15859
15860 static void
15861 dwarf2out_begin_function (tree fun)
15862 {
15863   htab_empty (decl_loc_table);
15864
15865   if (function_section (fun) != text_section)
15866     have_multiple_function_sections = true;
15867
15868   dwarf2out_note_section_used ();
15869 }
15870
15871 /* Output a label to mark the beginning of a source code line entry
15872    and record information relating to this source line, in
15873    'line_info_table' for later output of the .debug_line section.  */
15874
15875 static void
15876 dwarf2out_source_line (unsigned int line, const char *filename)
15877 {
15878   if (debug_info_level >= DINFO_LEVEL_NORMAL
15879       && line != 0)
15880     {
15881       int file_num = maybe_emit_file (lookup_filename (filename));
15882
15883       switch_to_section (current_function_section ());
15884
15885       /* If requested, emit something human-readable.  */
15886       if (flag_debug_asm)
15887         fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
15888                  filename, line);
15889
15890       if (DWARF2_ASM_LINE_DEBUG_INFO)
15891         {
15892           /* Emit the .loc directive understood by GNU as.  */
15893           fprintf (asm_out_file, "\t.loc %d %d 0\n", file_num, line);
15894
15895           /* Indicate that line number info exists.  */
15896           line_info_table_in_use++;
15897         }
15898       else if (function_section (current_function_decl) != text_section)
15899         {
15900           dw_separate_line_info_ref line_info;
15901           targetm.asm_out.internal_label (asm_out_file,
15902                                           SEPARATE_LINE_CODE_LABEL,
15903                                           separate_line_info_table_in_use);
15904
15905           /* Expand the line info table if necessary.  */
15906           if (separate_line_info_table_in_use
15907               == separate_line_info_table_allocated)
15908             {
15909               separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
15910               separate_line_info_table
15911                 = GGC_RESIZEVEC (dw_separate_line_info_entry,
15912                                  separate_line_info_table,
15913                                  separate_line_info_table_allocated);
15914               memset (separate_line_info_table
15915                        + separate_line_info_table_in_use,
15916                       0,
15917                       (LINE_INFO_TABLE_INCREMENT
15918                        * sizeof (dw_separate_line_info_entry)));
15919             }
15920
15921           /* Add the new entry at the end of the line_info_table.  */
15922           line_info
15923             = &separate_line_info_table[separate_line_info_table_in_use++];
15924           line_info->dw_file_num = file_num;
15925           line_info->dw_line_num = line;
15926           line_info->function = current_function_funcdef_no;
15927         }
15928       else
15929         {
15930           dw_line_info_ref line_info;
15931
15932           targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL,
15933                                      line_info_table_in_use);
15934
15935           /* Expand the line info table if necessary.  */
15936           if (line_info_table_in_use == line_info_table_allocated)
15937             {
15938               line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
15939               line_info_table
15940                 = GGC_RESIZEVEC (dw_line_info_entry, line_info_table,
15941                                  line_info_table_allocated);
15942               memset (line_info_table + line_info_table_in_use, 0,
15943                       LINE_INFO_TABLE_INCREMENT * sizeof (dw_line_info_entry));
15944             }
15945
15946           /* Add the new entry at the end of the line_info_table.  */
15947           line_info = &line_info_table[line_info_table_in_use++];
15948           line_info->dw_file_num = file_num;
15949           line_info->dw_line_num = line;
15950         }
15951     }
15952 }
15953
15954 /* Record the beginning of a new source file.  */
15955
15956 static void
15957 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
15958 {
15959   if (flag_eliminate_dwarf2_dups)
15960     {
15961       /* Record the beginning of the file for break_out_includes.  */
15962       dw_die_ref bincl_die;
15963
15964       bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die, NULL);
15965       add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
15966     }
15967
15968   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
15969     {
15970       int file_num = maybe_emit_file (lookup_filename (filename));
15971
15972       switch_to_section (debug_macinfo_section);
15973       dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
15974       dw2_asm_output_data_uleb128 (lineno, "Included from line number %d",
15975                                    lineno);
15976
15977       dw2_asm_output_data_uleb128 (file_num, "file %s", filename);
15978     }
15979 }
15980
15981 /* Record the end of a source file.  */
15982
15983 static void
15984 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
15985 {
15986   if (flag_eliminate_dwarf2_dups)
15987     /* Record the end of the file for break_out_includes.  */
15988     new_die (DW_TAG_GNU_EINCL, comp_unit_die, NULL);
15989
15990   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
15991     {
15992       switch_to_section (debug_macinfo_section);
15993       dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
15994     }
15995 }
15996
15997 /* Called from debug_define in toplev.c.  The `buffer' parameter contains
15998    the tail part of the directive line, i.e. the part which is past the
15999    initial whitespace, #, whitespace, directive-name, whitespace part.  */
16000
16001 static void
16002 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
16003                   const char *buffer ATTRIBUTE_UNUSED)
16004 {
16005   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
16006     {
16007       switch_to_section (debug_macinfo_section);
16008       dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
16009       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
16010       dw2_asm_output_nstring (buffer, -1, "The macro");
16011     }
16012 }
16013
16014 /* Called from debug_undef in toplev.c.  The `buffer' parameter contains
16015    the tail part of the directive line, i.e. the part which is past the
16016    initial whitespace, #, whitespace, directive-name, whitespace part.  */
16017
16018 static void
16019 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
16020                  const char *buffer ATTRIBUTE_UNUSED)
16021 {
16022   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
16023     {
16024       switch_to_section (debug_macinfo_section);
16025       dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
16026       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
16027       dw2_asm_output_nstring (buffer, -1, "The macro");
16028     }
16029 }
16030
16031 /* Set up for Dwarf output at the start of compilation.  */
16032
16033 static void
16034 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
16035 {
16036   /* Allocate the file_table.  */
16037   file_table = htab_create_ggc (50, file_table_hash,
16038                                 file_table_eq, NULL);
16039
16040   /* Allocate the decl_die_table.  */
16041   decl_die_table = htab_create_ggc (10, decl_die_table_hash,
16042                                     decl_die_table_eq, NULL);
16043
16044   /* Allocate the decl_loc_table.  */
16045   decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
16046                                     decl_loc_table_eq, NULL);
16047
16048   /* Allocate the initial hunk of the decl_scope_table.  */
16049   decl_scope_table = VEC_alloc (tree, gc, 256);
16050
16051   /* Allocate the initial hunk of the abbrev_die_table.  */
16052   abbrev_die_table = GGC_CNEWVEC (dw_die_ref, ABBREV_DIE_TABLE_INCREMENT);
16053   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
16054   /* Zero-th entry is allocated, but unused.  */
16055   abbrev_die_table_in_use = 1;
16056
16057   /* Allocate the initial hunk of the line_info_table.  */
16058   line_info_table = GGC_CNEWVEC (dw_line_info_entry, LINE_INFO_TABLE_INCREMENT);
16059   line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
16060
16061   /* Zero-th entry is allocated, but unused.  */
16062   line_info_table_in_use = 1;
16063
16064   /* Allocate the pubtypes and pubnames vectors.  */
16065   pubname_table = VEC_alloc (pubname_entry, gc, 32);
16066   pubtype_table = VEC_alloc (pubname_entry, gc, 32);
16067
16068   /* Generate the initial DIE for the .debug section.  Note that the (string)
16069      value given in the DW_AT_name attribute of the DW_TAG_compile_unit DIE
16070      will (typically) be a relative pathname and that this pathname should be
16071      taken as being relative to the directory from which the compiler was
16072      invoked when the given (base) source file was compiled.  We will fill
16073      in this value in dwarf2out_finish.  */
16074   comp_unit_die = gen_compile_unit_die (NULL);
16075
16076   incomplete_types = VEC_alloc (tree, gc, 64);
16077
16078   used_rtx_array = VEC_alloc (rtx, gc, 32);
16079
16080   debug_info_section = get_section (DEBUG_INFO_SECTION,
16081                                     SECTION_DEBUG, NULL);
16082   debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
16083                                       SECTION_DEBUG, NULL);
16084   debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
16085                                        SECTION_DEBUG, NULL);
16086   debug_macinfo_section = get_section (DEBUG_MACINFO_SECTION,
16087                                        SECTION_DEBUG, NULL);
16088   debug_line_section = get_section (DEBUG_LINE_SECTION,
16089                                     SECTION_DEBUG, NULL);
16090   debug_loc_section = get_section (DEBUG_LOC_SECTION,
16091                                    SECTION_DEBUG, NULL);
16092   debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
16093                                         SECTION_DEBUG, NULL);
16094 #ifdef DEBUG_PUBTYPES_SECTION
16095   debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
16096                                         SECTION_DEBUG, NULL);
16097 #endif
16098   debug_str_section = get_section (DEBUG_STR_SECTION,
16099                                    DEBUG_STR_SECTION_FLAGS, NULL);
16100   debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
16101                                       SECTION_DEBUG, NULL);
16102   debug_frame_section = get_section (DEBUG_FRAME_SECTION,
16103                                      SECTION_DEBUG, NULL);
16104
16105   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
16106   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
16107                                DEBUG_ABBREV_SECTION_LABEL, 0);
16108   ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
16109   ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
16110                                COLD_TEXT_SECTION_LABEL, 0);
16111   ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
16112
16113   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
16114                                DEBUG_INFO_SECTION_LABEL, 0);
16115   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
16116                                DEBUG_LINE_SECTION_LABEL, 0);
16117   ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
16118                                DEBUG_RANGES_SECTION_LABEL, 0);
16119   switch_to_section (debug_abbrev_section);
16120   ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
16121   switch_to_section (debug_info_section);
16122   ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
16123   switch_to_section (debug_line_section);
16124   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
16125
16126   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
16127     {
16128       switch_to_section (debug_macinfo_section);
16129       ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
16130                                    DEBUG_MACINFO_SECTION_LABEL, 0);
16131       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
16132     }
16133
16134   switch_to_section (text_section);
16135   ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
16136   if (flag_reorder_blocks_and_partition)
16137     {
16138       cold_text_section = unlikely_text_section ();
16139       switch_to_section (cold_text_section);
16140       ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
16141     }
16142 }
16143
16144 /* A helper function for dwarf2out_finish called through
16145    ht_forall.  Emit one queued .debug_str string.  */
16146
16147 static int
16148 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
16149 {
16150   struct indirect_string_node *node = (struct indirect_string_node *) *h;
16151
16152   if (node->form == DW_FORM_strp)
16153     {
16154       switch_to_section (debug_str_section);
16155       ASM_OUTPUT_LABEL (asm_out_file, node->label);
16156       assemble_string (node->str, strlen (node->str) + 1);
16157     }
16158
16159   return 1;
16160 }
16161
16162 #if ENABLE_ASSERT_CHECKING
16163 /* Verify that all marks are clear.  */
16164
16165 static void
16166 verify_marks_clear (dw_die_ref die)
16167 {
16168   dw_die_ref c;
16169
16170   gcc_assert (! die->die_mark);
16171   FOR_EACH_CHILD (die, c, verify_marks_clear (c));
16172 }
16173 #endif /* ENABLE_ASSERT_CHECKING */
16174
16175 /* Clear the marks for a die and its children.
16176    Be cool if the mark isn't set.  */
16177
16178 static void
16179 prune_unmark_dies (dw_die_ref die)
16180 {
16181   dw_die_ref c;
16182
16183   if (die->die_mark)
16184     die->die_mark = 0;
16185   FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
16186 }
16187
16188 /* Given DIE that we're marking as used, find any other dies
16189    it references as attributes and mark them as used.  */
16190
16191 static void
16192 prune_unused_types_walk_attribs (dw_die_ref die)
16193 {
16194   dw_attr_ref a;
16195   unsigned ix;
16196
16197   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
16198     {
16199       if (a->dw_attr_val.val_class == dw_val_class_die_ref)
16200         {
16201           /* A reference to another DIE.
16202              Make sure that it will get emitted.  */
16203           prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
16204         }
16205       /* Set the string's refcount to 0 so that prune_unused_types_mark
16206          accounts properly for it.  */
16207       if (AT_class (a) == dw_val_class_str)
16208         a->dw_attr_val.v.val_str->refcount = 0;
16209     }
16210 }
16211
16212
16213 /* Mark DIE as being used.  If DOKIDS is true, then walk down
16214    to DIE's children.  */
16215
16216 static void
16217 prune_unused_types_mark (dw_die_ref die, int dokids)
16218 {
16219   dw_die_ref c;
16220
16221   if (die->die_mark == 0)
16222     {
16223       /* We haven't done this node yet.  Mark it as used.  */
16224       die->die_mark = 1;
16225
16226       /* We also have to mark its parents as used.
16227          (But we don't want to mark our parents' kids due to this.)  */
16228       if (die->die_parent)
16229         prune_unused_types_mark (die->die_parent, 0);
16230
16231       /* Mark any referenced nodes.  */
16232       prune_unused_types_walk_attribs (die);
16233
16234       /* If this node is a specification,
16235          also mark the definition, if it exists.  */
16236       if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
16237         prune_unused_types_mark (die->die_definition, 1);
16238     }
16239
16240   if (dokids && die->die_mark != 2)
16241     {
16242       /* We need to walk the children, but haven't done so yet.
16243          Remember that we've walked the kids.  */
16244       die->die_mark = 2;
16245
16246       /* If this is an array type, we need to make sure our
16247          kids get marked, even if they're types.  */
16248       if (die->die_tag == DW_TAG_array_type)
16249         FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
16250       else
16251         FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
16252     }
16253 }
16254
16255 /* For local classes, look if any static member functions were emitted
16256    and if so, mark them.  */
16257
16258 static void
16259 prune_unused_types_walk_local_classes (dw_die_ref die)
16260 {
16261   dw_die_ref c;
16262
16263   if (die->die_mark == 2)
16264     return;
16265
16266   switch (die->die_tag)
16267     {
16268     case DW_TAG_structure_type:
16269     case DW_TAG_union_type:
16270     case DW_TAG_class_type:
16271       break;
16272
16273     case DW_TAG_subprogram:
16274       if (!get_AT_flag (die, DW_AT_declaration)
16275           || die->die_definition != NULL)
16276         prune_unused_types_mark (die, 1);
16277       return;
16278
16279     default:
16280       return;
16281     }
16282
16283   /* Mark children.  */
16284   FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
16285 }
16286
16287 /* Walk the tree DIE and mark types that we actually use.  */
16288
16289 static void
16290 prune_unused_types_walk (dw_die_ref die)
16291 {
16292   dw_die_ref c;
16293
16294   /* Don't do anything if this node is already marked and
16295      children have been marked as well.  */
16296   if (die->die_mark == 2)
16297     return;
16298
16299   switch (die->die_tag)
16300     {
16301     case DW_TAG_structure_type:
16302     case DW_TAG_union_type:
16303     case DW_TAG_class_type:
16304       if (die->die_perennial_p)
16305         break;
16306
16307       for (c = die->die_parent; c; c = c->die_parent)
16308         if (c->die_tag == DW_TAG_subprogram)
16309           break;
16310
16311       /* Finding used static member functions inside of classes
16312          is needed just for local classes, because for other classes
16313          static member function DIEs with DW_AT_specification
16314          are emitted outside of the DW_TAG_*_type.  If we ever change
16315          it, we'd need to call this even for non-local classes.  */
16316       if (c)
16317         prune_unused_types_walk_local_classes (die);
16318
16319       /* It's a type node --- don't mark it.  */
16320       return;
16321
16322     case DW_TAG_const_type:
16323     case DW_TAG_packed_type:
16324     case DW_TAG_pointer_type:
16325     case DW_TAG_reference_type:
16326     case DW_TAG_volatile_type:
16327     case DW_TAG_typedef:
16328     case DW_TAG_array_type:
16329     case DW_TAG_interface_type:
16330     case DW_TAG_friend:
16331     case DW_TAG_variant_part:
16332     case DW_TAG_enumeration_type:
16333     case DW_TAG_subroutine_type:
16334     case DW_TAG_string_type:
16335     case DW_TAG_set_type:
16336     case DW_TAG_subrange_type:
16337     case DW_TAG_ptr_to_member_type:
16338     case DW_TAG_file_type:
16339       if (die->die_perennial_p)
16340         break;
16341
16342       /* It's a type node --- don't mark it.  */
16343       return;
16344
16345     default:
16346       /* Mark everything else.  */
16347       break;
16348   }
16349
16350   if (die->die_mark == 0)
16351     {
16352       die->die_mark = 1;
16353
16354       /* Now, mark any dies referenced from here.  */
16355       prune_unused_types_walk_attribs (die);
16356     }
16357
16358   die->die_mark = 2;
16359
16360   /* Mark children.  */
16361   FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
16362 }
16363
16364 /* Increment the string counts on strings referred to from DIE's
16365    attributes.  */
16366
16367 static void
16368 prune_unused_types_update_strings (dw_die_ref die)
16369 {
16370   dw_attr_ref a;
16371   unsigned ix;
16372
16373   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
16374     if (AT_class (a) == dw_val_class_str)
16375       {
16376         struct indirect_string_node *s = a->dw_attr_val.v.val_str;
16377         s->refcount++;
16378         /* Avoid unnecessarily putting strings that are used less than
16379            twice in the hash table.  */
16380         if (s->refcount
16381             == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
16382           {
16383             void ** slot;
16384             slot = htab_find_slot_with_hash (debug_str_hash, s->str,
16385                                              htab_hash_string (s->str),
16386                                              INSERT);
16387             gcc_assert (*slot == NULL);
16388             *slot = s;
16389           }
16390       }
16391 }
16392
16393 /* Remove from the tree DIE any dies that aren't marked.  */
16394
16395 static void
16396 prune_unused_types_prune (dw_die_ref die)
16397 {
16398   dw_die_ref c;
16399
16400   gcc_assert (die->die_mark);
16401   prune_unused_types_update_strings (die);
16402
16403   if (! die->die_child)
16404     return;
16405
16406   c = die->die_child;
16407   do {
16408     dw_die_ref prev = c;
16409     for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
16410       if (c == die->die_child)
16411         {
16412           /* No marked children between 'prev' and the end of the list.  */
16413           if (prev == c)
16414             /* No marked children at all.  */
16415             die->die_child = NULL;
16416           else
16417             {
16418               prev->die_sib = c->die_sib;
16419               die->die_child = prev;
16420             }
16421           return;
16422         }
16423
16424     if (c != prev->die_sib)
16425       prev->die_sib = c;
16426     prune_unused_types_prune (c);
16427   } while (c != die->die_child);
16428 }
16429
16430
16431 /* Remove dies representing declarations that we never use.  */
16432
16433 static void
16434 prune_unused_types (void)
16435 {
16436   unsigned int i;
16437   limbo_die_node *node;
16438   pubname_ref pub;
16439
16440 #if ENABLE_ASSERT_CHECKING
16441   /* All the marks should already be clear.  */
16442   verify_marks_clear (comp_unit_die);
16443   for (node = limbo_die_list; node; node = node->next)
16444     verify_marks_clear (node->die);
16445 #endif /* ENABLE_ASSERT_CHECKING */
16446
16447   /* Set the mark on nodes that are actually used.  */
16448   prune_unused_types_walk (comp_unit_die);
16449   for (node = limbo_die_list; node; node = node->next)
16450     prune_unused_types_walk (node->die);
16451
16452   /* Also set the mark on nodes referenced from the
16453      pubname_table or arange_table.  */
16454   for (i = 0; VEC_iterate (pubname_entry, pubname_table, i, pub); i++)
16455     prune_unused_types_mark (pub->die, 1);
16456   for (i = 0; i < arange_table_in_use; i++)
16457     prune_unused_types_mark (arange_table[i], 1);
16458
16459   /* Get rid of nodes that aren't marked; and update the string counts.  */
16460   if (debug_str_hash)
16461     htab_empty (debug_str_hash);
16462   prune_unused_types_prune (comp_unit_die);
16463   for (node = limbo_die_list; node; node = node->next)
16464     prune_unused_types_prune (node->die);
16465
16466   /* Leave the marks clear.  */
16467   prune_unmark_dies (comp_unit_die);
16468   for (node = limbo_die_list; node; node = node->next)
16469     prune_unmark_dies (node->die);
16470 }
16471
16472 /* Set the parameter to true if there are any relative pathnames in
16473    the file table.  */
16474 static int
16475 file_table_relative_p (void ** slot, void *param)
16476 {
16477   bool *p = (bool *) param;
16478   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
16479   if (!IS_ABSOLUTE_PATH (d->filename))
16480     {
16481       *p = true;
16482       return 0;
16483     }
16484   return 1;
16485 }
16486
16487 /* Output stuff that dwarf requires at the end of every file,
16488    and generate the DWARF-2 debugging info.  */
16489
16490 static void
16491 dwarf2out_finish (const char *filename)
16492 {
16493   limbo_die_node *node, *next_node;
16494   dw_die_ref die = 0;
16495   unsigned int i;
16496
16497   /* Add the name for the main input file now.  We delayed this from
16498      dwarf2out_init to avoid complications with PCH.  */
16499   add_name_attribute (comp_unit_die, remap_debug_filename (filename));
16500   if (!IS_ABSOLUTE_PATH (filename))
16501     add_comp_dir_attribute (comp_unit_die);
16502   else if (get_AT (comp_unit_die, DW_AT_comp_dir) == NULL)
16503     {
16504       bool p = false;
16505       htab_traverse (file_table, file_table_relative_p, &p);
16506       if (p)
16507         add_comp_dir_attribute (comp_unit_die);
16508     }
16509
16510   for (i = 0; i < VEC_length (deferred_locations, deferred_locations_list); i++)
16511     {
16512       add_location_or_const_value_attribute (
16513         VEC_index (deferred_locations, deferred_locations_list, i)->die,
16514         VEC_index (deferred_locations, deferred_locations_list, i)->variable,
16515         DW_AT_location);
16516     }
16517
16518   /* Traverse the limbo die list, and add parent/child links.  The only
16519      dies without parents that should be here are concrete instances of
16520      inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
16521      For concrete instances, we can get the parent die from the abstract
16522      instance.  */
16523   for (node = limbo_die_list; node; node = next_node)
16524     {
16525       next_node = node->next;
16526       die = node->die;
16527
16528       if (die->die_parent == NULL)
16529         {
16530           dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
16531
16532           if (origin)
16533             add_child_die (origin->die_parent, die);
16534           else if (die == comp_unit_die)
16535             ;
16536           else if (errorcount > 0 || sorrycount > 0)
16537             /* It's OK to be confused by errors in the input.  */
16538             add_child_die (comp_unit_die, die);
16539           else
16540             {
16541               /* In certain situations, the lexical block containing a
16542                  nested function can be optimized away, which results
16543                  in the nested function die being orphaned.  Likewise
16544                  with the return type of that nested function.  Force
16545                  this to be a child of the containing function.
16546
16547                  It may happen that even the containing function got fully
16548                  inlined and optimized out.  In that case we are lost and
16549                  assign the empty child.  This should not be big issue as
16550                  the function is likely unreachable too.  */
16551               tree context = NULL_TREE;
16552
16553               gcc_assert (node->created_for);
16554
16555               if (DECL_P (node->created_for))
16556                 context = DECL_CONTEXT (node->created_for);
16557               else if (TYPE_P (node->created_for))
16558                 context = TYPE_CONTEXT (node->created_for);
16559
16560               gcc_assert (context
16561                           && (TREE_CODE (context) == FUNCTION_DECL
16562                               || TREE_CODE (context) == NAMESPACE_DECL));
16563
16564               origin = lookup_decl_die (context);
16565               if (origin)
16566                 add_child_die (origin, die);
16567               else
16568                 add_child_die (comp_unit_die, die);
16569             }
16570         }
16571     }
16572
16573   limbo_die_list = NULL;
16574
16575   /* Walk through the list of incomplete types again, trying once more to
16576      emit full debugging info for them.  */
16577   retry_incomplete_types ();
16578
16579   if (flag_eliminate_unused_debug_types)
16580     prune_unused_types ();
16581
16582   /* Generate separate CUs for each of the include files we've seen.
16583      They will go into limbo_die_list.  */
16584   if (flag_eliminate_dwarf2_dups)
16585     break_out_includes (comp_unit_die);
16586
16587   /* Traverse the DIE's and add add sibling attributes to those DIE's
16588      that have children.  */
16589   add_sibling_attributes (comp_unit_die);
16590   for (node = limbo_die_list; node; node = node->next)
16591     add_sibling_attributes (node->die);
16592
16593   /* Output a terminator label for the .text section.  */
16594   switch_to_section (text_section);
16595   targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
16596   if (flag_reorder_blocks_and_partition)
16597     {
16598       switch_to_section (unlikely_text_section ());
16599       targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
16600     }
16601
16602   /* We can only use the low/high_pc attributes if all of the code was
16603      in .text.  */
16604   if (!have_multiple_function_sections)
16605     {
16606       add_AT_lbl_id (comp_unit_die, DW_AT_low_pc, text_section_label);
16607       add_AT_lbl_id (comp_unit_die, DW_AT_high_pc, text_end_label);
16608     }
16609
16610   else
16611     {
16612       unsigned fde_idx = 0;
16613
16614       /* We need to give .debug_loc and .debug_ranges an appropriate
16615          "base address".  Use zero so that these addresses become
16616          absolute.  Historically, we've emitted the unexpected
16617          DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
16618          Emit both to give time for other tools to adapt.  */
16619       add_AT_addr (comp_unit_die, DW_AT_low_pc, const0_rtx);
16620       add_AT_addr (comp_unit_die, DW_AT_entry_pc, const0_rtx);
16621
16622       add_AT_range_list (comp_unit_die, DW_AT_ranges,
16623                          add_ranges_by_labels (text_section_label,
16624                                                text_end_label));
16625       if (flag_reorder_blocks_and_partition)
16626         add_ranges_by_labels (cold_text_section_label,
16627                               cold_end_label);
16628
16629       for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
16630         {
16631           dw_fde_ref fde = &fde_table[fde_idx];
16632
16633           if (fde->dw_fde_switched_sections)
16634             {
16635               add_ranges_by_labels (fde->dw_fde_hot_section_label,
16636                                     fde->dw_fde_hot_section_end_label);
16637               add_ranges_by_labels (fde->dw_fde_unlikely_section_label,
16638                                     fde->dw_fde_unlikely_section_end_label);
16639             }
16640           else
16641             add_ranges_by_labels (fde->dw_fde_begin,
16642                                   fde->dw_fde_end);
16643         }
16644
16645       add_ranges (NULL);
16646     }
16647
16648   /* Output location list section if necessary.  */
16649   if (have_location_lists)
16650     {
16651       /* Output the location lists info.  */
16652       switch_to_section (debug_loc_section);
16653       ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
16654                                    DEBUG_LOC_SECTION_LABEL, 0);
16655       ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
16656       output_location_lists (die);
16657     }
16658
16659   if (debug_info_level >= DINFO_LEVEL_NORMAL)
16660     add_AT_lineptr (comp_unit_die, DW_AT_stmt_list,
16661                     debug_line_section_label);
16662
16663   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
16664     add_AT_macptr (comp_unit_die, DW_AT_macro_info, macinfo_section_label);
16665
16666   /* Output all of the compilation units.  We put the main one last so that
16667      the offsets are available to output_pubnames.  */
16668   for (node = limbo_die_list; node; node = node->next)
16669     output_comp_unit (node->die, 0);
16670
16671   /* Output the main compilation unit if non-empty or if .debug_macinfo
16672      has been emitted.  */
16673   output_comp_unit (comp_unit_die, debug_info_level >= DINFO_LEVEL_VERBOSE);
16674
16675   /* Output the abbreviation table.  */
16676   switch_to_section (debug_abbrev_section);
16677   output_abbrev_section ();
16678
16679   /* Output public names table if necessary.  */
16680   if (!VEC_empty (pubname_entry, pubname_table))
16681     {
16682       switch_to_section (debug_pubnames_section);
16683       output_pubnames (pubname_table);
16684     }
16685
16686 #ifdef DEBUG_PUBTYPES_SECTION
16687   /* Output public types table if necessary.  */
16688   if (!VEC_empty (pubname_entry, pubtype_table))
16689     {
16690       switch_to_section (debug_pubtypes_section);
16691       output_pubnames (pubtype_table);
16692     }
16693 #endif
16694
16695   /* Output the address range information.  We only put functions in the arange
16696      table, so don't write it out if we don't have any.  */
16697   if (fde_table_in_use)
16698     {
16699       switch_to_section (debug_aranges_section);
16700       output_aranges ();
16701     }
16702
16703   /* Output ranges section if necessary.  */
16704   if (ranges_table_in_use)
16705     {
16706       switch_to_section (debug_ranges_section);
16707       ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
16708       output_ranges ();
16709     }
16710
16711   /* Output the source line correspondence table.  We must do this
16712      even if there is no line information.  Otherwise, on an empty
16713      translation unit, we will generate a present, but empty,
16714      .debug_info section.  IRIX 6.5 `nm' will then complain when
16715      examining the file.  This is done late so that any filenames
16716      used by the debug_info section are marked as 'used'.  */
16717   if (! DWARF2_ASM_LINE_DEBUG_INFO)
16718     {
16719       switch_to_section (debug_line_section);
16720       output_line_info ();
16721     }
16722
16723   /* Have to end the macro section.  */
16724   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
16725     {
16726       switch_to_section (debug_macinfo_section);
16727       dw2_asm_output_data (1, 0, "End compilation unit");
16728     }
16729
16730   /* If we emitted any DW_FORM_strp form attribute, output the string
16731      table too.  */
16732   if (debug_str_hash)
16733     htab_traverse (debug_str_hash, output_indirect_string, NULL);
16734 }
16735 #else
16736
16737 /* This should never be used, but its address is needed for comparisons.  */
16738 const struct gcc_debug_hooks dwarf2_debug_hooks;
16739
16740 #endif /* DWARF2_DEBUGGING_INFO */
16741
16742 #include "gt-dwarf2out.h"