OSDN Git Service

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