OSDN Git Service

614871ed917eec92888673ea3aa417532e8c5e42
[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 /* Decide whether we want to emit frame unwind information for the current
114    translation unit.  */
115
116 int
117 dwarf2out_do_frame (void)
118 {
119   /* We want to emit correct CFA location expressions or lists, so we
120      have to return true if we're going to output debug info, even if
121      we're not going to output frame or unwind info.  */
122   return (write_symbols == DWARF2_DEBUG
123           || write_symbols == VMS_AND_DWARF2_DEBUG
124           || DWARF2_FRAME_INFO
125 #ifdef DWARF2_UNWIND_INFO
126           || (DWARF2_UNWIND_INFO
127               && (flag_unwind_tables
128                   || (flag_exceptions && ! USING_SJLJ_EXCEPTIONS)))
129 #endif
130           );
131 }
132
133 /* Decide whether to emit frame unwind via assembler directives.  */
134
135 int
136 dwarf2out_do_cfi_asm (void)
137 {
138   int enc;
139
140 #ifdef MIPS_DEBUGGING_INFO
141   return false;
142 #endif
143   if (!flag_dwarf2_cfi_asm || !dwarf2out_do_frame ())
144     return false;
145   if (!eh_personality_libfunc)
146     return true;
147   if (!HAVE_GAS_CFI_PERSONALITY_DIRECTIVE)
148     return false;
149
150   /* Make sure the personality encoding is one the assembler can support.
151      In particular, aligned addresses can't be handled.  */
152   enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,/*global=*/1);
153   if ((enc & 0x70) != 0 && (enc & 0x70) != DW_EH_PE_pcrel)
154     return false;
155   enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,/*global=*/0);
156   if ((enc & 0x70) != 0 && (enc & 0x70) != DW_EH_PE_pcrel)
157     return false;
158
159   return true;
160 }
161
162 /* The size of the target's pointer type.  */
163 #ifndef PTR_SIZE
164 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
165 #endif
166
167 /* Array of RTXes referenced by the debugging information, which therefore
168    must be kept around forever.  */
169 static GTY(()) VEC(rtx,gc) *used_rtx_array;
170
171 /* A pointer to the base of a list of incomplete types which might be
172    completed at some later time.  incomplete_types_list needs to be a
173    VEC(tree,gc) because we want to tell the garbage collector about
174    it.  */
175 static GTY(()) VEC(tree,gc) *incomplete_types;
176
177 /* A pointer to the base of a table of references to declaration
178    scopes.  This table is a display which tracks the nesting
179    of declaration scopes at the current scope and containing
180    scopes.  This table is used to find the proper place to
181    define type declaration DIE's.  */
182 static GTY(()) VEC(tree,gc) *decl_scope_table;
183
184 /* Pointers to various DWARF2 sections.  */
185 static GTY(()) section *debug_info_section;
186 static GTY(()) section *debug_abbrev_section;
187 static GTY(()) section *debug_aranges_section;
188 static GTY(()) section *debug_macinfo_section;
189 static GTY(()) section *debug_line_section;
190 static GTY(()) section *debug_loc_section;
191 static GTY(()) section *debug_pubnames_section;
192 static GTY(()) section *debug_pubtypes_section;
193 static GTY(()) section *debug_str_section;
194 static GTY(()) section *debug_ranges_section;
195 static GTY(()) section *debug_frame_section;
196
197 /* How to start an assembler comment.  */
198 #ifndef ASM_COMMENT_START
199 #define ASM_COMMENT_START ";#"
200 #endif
201
202 typedef struct dw_cfi_struct *dw_cfi_ref;
203 typedef struct dw_fde_struct *dw_fde_ref;
204 typedef union  dw_cfi_oprnd_struct *dw_cfi_oprnd_ref;
205
206 /* Call frames are described using a sequence of Call Frame
207    Information instructions.  The register number, offset
208    and address fields are provided as possible operands;
209    their use is selected by the opcode field.  */
210
211 enum dw_cfi_oprnd_type {
212   dw_cfi_oprnd_unused,
213   dw_cfi_oprnd_reg_num,
214   dw_cfi_oprnd_offset,
215   dw_cfi_oprnd_addr,
216   dw_cfi_oprnd_loc
217 };
218
219 typedef union dw_cfi_oprnd_struct GTY(())
220 {
221   unsigned int GTY ((tag ("dw_cfi_oprnd_reg_num"))) dw_cfi_reg_num;
222   HOST_WIDE_INT GTY ((tag ("dw_cfi_oprnd_offset"))) dw_cfi_offset;
223   const char * GTY ((tag ("dw_cfi_oprnd_addr"))) dw_cfi_addr;
224   struct dw_loc_descr_struct * GTY ((tag ("dw_cfi_oprnd_loc"))) dw_cfi_loc;
225 }
226 dw_cfi_oprnd;
227
228 typedef struct dw_cfi_struct GTY(())
229 {
230   dw_cfi_ref dw_cfi_next;
231   enum dwarf_call_frame_info dw_cfi_opc;
232   dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd1_desc (%1.dw_cfi_opc)")))
233     dw_cfi_oprnd1;
234   dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd2_desc (%1.dw_cfi_opc)")))
235     dw_cfi_oprnd2;
236 }
237 dw_cfi_node;
238
239 /* This is how we define the location of the CFA. We use to handle it
240    as REG + OFFSET all the time,  but now it can be more complex.
241    It can now be either REG + CFA_OFFSET or *(REG + BASE_OFFSET) + CFA_OFFSET.
242    Instead of passing around REG and OFFSET, we pass a copy
243    of this structure.  */
244 typedef struct cfa_loc GTY(())
245 {
246   HOST_WIDE_INT offset;
247   HOST_WIDE_INT base_offset;
248   unsigned int reg;
249   int indirect;            /* 1 if CFA is accessed via a dereference.  */
250 } dw_cfa_location;
251
252 /* All call frame descriptions (FDE's) in the GCC generated DWARF
253    refer to a single Common Information Entry (CIE), defined at
254    the beginning of the .debug_frame section.  This use of a single
255    CIE obviates the need to keep track of multiple CIE's
256    in the DWARF generation routines below.  */
257
258 typedef struct dw_fde_struct GTY(())
259 {
260   tree decl;
261   const char *dw_fde_begin;
262   const char *dw_fde_current_label;
263   const char *dw_fde_end;
264   const char *dw_fde_hot_section_label;
265   const char *dw_fde_hot_section_end_label;
266   const char *dw_fde_unlikely_section_label;
267   const char *dw_fde_unlikely_section_end_label;
268   bool dw_fde_switched_sections;
269   dw_cfi_ref dw_fde_cfi;
270   unsigned funcdef_number;
271   HOST_WIDE_INT stack_realignment;
272   /* Dynamic realign argument pointer register.  */
273   unsigned int drap_reg;
274   /* Virtual dynamic realign argument pointer register.  */
275   unsigned int vdrap_reg;
276   unsigned all_throwers_are_sibcalls : 1;
277   unsigned nothrow : 1;
278   unsigned uses_eh_lsda : 1;
279   /* Whether we did stack realign in this call frame.  */
280   unsigned stack_realign : 1;
281   /* Whether dynamic realign argument pointer register has been saved.  */
282   unsigned drap_reg_saved: 1;
283 }
284 dw_fde_node;
285
286 /* Maximum size (in bytes) of an artificially generated label.  */
287 #define MAX_ARTIFICIAL_LABEL_BYTES      30
288
289 /* The size of addresses as they appear in the Dwarf 2 data.
290    Some architectures use word addresses to refer to code locations,
291    but Dwarf 2 info always uses byte addresses.  On such machines,
292    Dwarf 2 addresses need to be larger than the architecture's
293    pointers.  */
294 #ifndef DWARF2_ADDR_SIZE
295 #define DWARF2_ADDR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
296 #endif
297
298 /* The size in bytes of a DWARF field indicating an offset or length
299    relative to a debug info section, specified to be 4 bytes in the
300    DWARF-2 specification.  The SGI/MIPS ABI defines it to be the same
301    as PTR_SIZE.  */
302
303 #ifndef DWARF_OFFSET_SIZE
304 #define DWARF_OFFSET_SIZE 4
305 #endif
306
307 /* According to the (draft) DWARF 3 specification, the initial length
308    should either be 4 or 12 bytes.  When it's 12 bytes, the first 4
309    bytes are 0xffffffff, followed by the length stored in the next 8
310    bytes.
311
312    However, the SGI/MIPS ABI uses an initial length which is equal to
313    DWARF_OFFSET_SIZE.  It is defined (elsewhere) accordingly.  */
314
315 #ifndef DWARF_INITIAL_LENGTH_SIZE
316 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
317 #endif
318
319 #define DWARF_VERSION 2
320
321 /* Round SIZE up to the nearest BOUNDARY.  */
322 #define DWARF_ROUND(SIZE,BOUNDARY) \
323   ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
324
325 /* Offsets recorded in opcodes are a multiple of this alignment factor.  */
326 #ifndef DWARF_CIE_DATA_ALIGNMENT
327 #ifdef STACK_GROWS_DOWNWARD
328 #define DWARF_CIE_DATA_ALIGNMENT (-((int) UNITS_PER_WORD))
329 #else
330 #define DWARF_CIE_DATA_ALIGNMENT ((int) UNITS_PER_WORD)
331 #endif
332 #endif
333
334 /* CIE identifier.  */
335 #if HOST_BITS_PER_WIDE_INT >= 64
336 #define DWARF_CIE_ID \
337   (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
338 #else
339 #define DWARF_CIE_ID DW_CIE_ID
340 #endif
341
342 /* A pointer to the base of a table that contains frame description
343    information for each routine.  */
344 static GTY((length ("fde_table_allocated"))) dw_fde_ref fde_table;
345
346 /* Number of elements currently allocated for fde_table.  */
347 static GTY(()) unsigned fde_table_allocated;
348
349 /* Number of elements in fde_table currently in use.  */
350 static GTY(()) unsigned fde_table_in_use;
351
352 /* Size (in elements) of increments by which we may expand the
353    fde_table.  */
354 #define FDE_TABLE_INCREMENT 256
355
356 /* Get the current fde_table entry we should use.  */
357
358 static inline dw_fde_ref
359 current_fde (void)
360 {
361   return fde_table_in_use ? &fde_table[fde_table_in_use - 1] : NULL;
362 }
363
364 /* A list of call frame insns for the CIE.  */
365 static GTY(()) dw_cfi_ref cie_cfi_head;
366
367 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
368 /* Some DWARF extensions (e.g., MIPS/SGI) implement a subprogram
369    attribute that accelerates the lookup of the FDE associated
370    with the subprogram.  This variable holds the table index of the FDE
371    associated with the current function (body) definition.  */
372 static unsigned current_funcdef_fde;
373 #endif
374
375 struct indirect_string_node GTY(())
376 {
377   const char *str;
378   unsigned int refcount;
379   unsigned int form;
380   char *label;
381 };
382
383 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
384
385 static GTY(()) int dw2_string_counter;
386 static GTY(()) unsigned long dwarf2out_cfi_label_num;
387
388 /* True if the compilation unit places functions in more than one section.  */
389 static GTY(()) bool have_multiple_function_sections = false;
390
391 /* Whether the default text and cold text sections have been used at all.  */
392
393 static GTY(()) bool text_section_used = false;
394 static GTY(()) bool cold_text_section_used = false;
395
396 /* The default cold text section.  */
397 static GTY(()) section *cold_text_section;
398
399 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
400
401 /* Forward declarations for functions defined in this file.  */
402
403 static char *stripattributes (const char *);
404 static const char *dwarf_cfi_name (unsigned);
405 static dw_cfi_ref new_cfi (void);
406 static void add_cfi (dw_cfi_ref *, dw_cfi_ref);
407 static void add_fde_cfi (const char *, dw_cfi_ref);
408 static void lookup_cfa_1 (dw_cfi_ref, dw_cfa_location *);
409 static void lookup_cfa (dw_cfa_location *);
410 static void reg_save (const char *, unsigned, unsigned, HOST_WIDE_INT);
411 #ifdef DWARF2_UNWIND_INFO
412 static void initial_return_save (rtx);
413 #endif
414 static HOST_WIDE_INT stack_adjust_offset (const_rtx);
415 static void output_cfi (dw_cfi_ref, dw_fde_ref, int);
416 static void output_cfi_directive (dw_cfi_ref);
417 static void output_call_frame_info (int);
418 static void dwarf2out_note_section_used (void);
419 static void dwarf2out_stack_adjust (rtx, bool);
420 static void dwarf2out_args_size_adjust (HOST_WIDE_INT, const char *);
421 static void flush_queued_reg_saves (void);
422 static bool clobbers_queued_reg_save (const_rtx);
423 static void dwarf2out_frame_debug_expr (rtx, const char *);
424
425 /* Support for complex CFA locations.  */
426 static void output_cfa_loc (dw_cfi_ref);
427 static void output_cfa_loc_raw (dw_cfi_ref);
428 static void get_cfa_from_loc_descr (dw_cfa_location *,
429                                     struct dw_loc_descr_struct *);
430 static struct dw_loc_descr_struct *build_cfa_loc
431   (dw_cfa_location *, HOST_WIDE_INT);
432 static struct dw_loc_descr_struct *build_cfa_aligned_loc
433   (HOST_WIDE_INT, HOST_WIDE_INT);
434 static void def_cfa_1 (const char *, dw_cfa_location *);
435
436 /* How to start an assembler comment.  */
437 #ifndef ASM_COMMENT_START
438 #define ASM_COMMENT_START ";#"
439 #endif
440
441 /* Data and reference forms for relocatable data.  */
442 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
443 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
444
445 #ifndef DEBUG_FRAME_SECTION
446 #define DEBUG_FRAME_SECTION     ".debug_frame"
447 #endif
448
449 #ifndef FUNC_BEGIN_LABEL
450 #define FUNC_BEGIN_LABEL        "LFB"
451 #endif
452
453 #ifndef FUNC_END_LABEL
454 #define FUNC_END_LABEL          "LFE"
455 #endif
456
457 #ifndef FRAME_BEGIN_LABEL
458 #define FRAME_BEGIN_LABEL       "Lframe"
459 #endif
460 #define CIE_AFTER_SIZE_LABEL    "LSCIE"
461 #define CIE_END_LABEL           "LECIE"
462 #define FDE_LABEL               "LSFDE"
463 #define FDE_AFTER_SIZE_LABEL    "LASFDE"
464 #define FDE_END_LABEL           "LEFDE"
465 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
466 #define LINE_NUMBER_END_LABEL   "LELT"
467 #define LN_PROLOG_AS_LABEL      "LASLTP"
468 #define LN_PROLOG_END_LABEL     "LELTP"
469 #define DIE_LABEL_PREFIX        "DW"
470
471 /* The DWARF 2 CFA column which tracks the return address.  Normally this
472    is the column for PC, or the first column after all of the hard
473    registers.  */
474 #ifndef DWARF_FRAME_RETURN_COLUMN
475 #ifdef PC_REGNUM
476 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGNUM (PC_REGNUM)
477 #else
478 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGISTERS
479 #endif
480 #endif
481
482 /* The mapping from gcc register number to DWARF 2 CFA column number.  By
483    default, we just provide columns for all registers.  */
484 #ifndef DWARF_FRAME_REGNUM
485 #define DWARF_FRAME_REGNUM(REG) DBX_REGISTER_NUMBER (REG)
486 #endif
487 \f
488 /* Hook used by __throw.  */
489
490 rtx
491 expand_builtin_dwarf_sp_column (void)
492 {
493   unsigned int dwarf_regnum = DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM);
494   return GEN_INT (DWARF2_FRAME_REG_OUT (dwarf_regnum, 1));
495 }
496
497 /* Return a pointer to a copy of the section string name S with all
498    attributes stripped off, and an asterisk prepended (for assemble_name).  */
499
500 static inline char *
501 stripattributes (const char *s)
502 {
503   char *stripped = XNEWVEC (char, strlen (s) + 2);
504   char *p = stripped;
505
506   *p++ = '*';
507
508   while (*s && *s != ',')
509     *p++ = *s++;
510
511   *p = '\0';
512   return stripped;
513 }
514
515 /* MEM is a memory reference for the register size table, each element of
516    which has mode MODE.  Initialize column C as a return address column.  */
517
518 static void
519 init_return_column_size (enum machine_mode mode, rtx mem, unsigned int c)
520 {
521   HOST_WIDE_INT offset = c * GET_MODE_SIZE (mode);
522   HOST_WIDE_INT size = GET_MODE_SIZE (Pmode);
523   emit_move_insn (adjust_address (mem, mode, offset), GEN_INT (size));
524 }
525
526 /* Generate code to initialize the register size table.  */
527
528 void
529 expand_builtin_init_dwarf_reg_sizes (tree address)
530 {
531   unsigned int i;
532   enum machine_mode mode = TYPE_MODE (char_type_node);
533   rtx addr = expand_normal (address);
534   rtx mem = gen_rtx_MEM (BLKmode, addr);
535   bool wrote_return_column = false;
536
537   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
538     {
539       int rnum = DWARF2_FRAME_REG_OUT (DWARF_FRAME_REGNUM (i), 1);
540
541       if (rnum < DWARF_FRAME_REGISTERS)
542         {
543           HOST_WIDE_INT offset = rnum * GET_MODE_SIZE (mode);
544           enum machine_mode save_mode = reg_raw_mode[i];
545           HOST_WIDE_INT size;
546
547           if (HARD_REGNO_CALL_PART_CLOBBERED (i, save_mode))
548             save_mode = choose_hard_reg_mode (i, 1, true);
549           if (DWARF_FRAME_REGNUM (i) == DWARF_FRAME_RETURN_COLUMN)
550             {
551               if (save_mode == VOIDmode)
552                 continue;
553               wrote_return_column = true;
554             }
555           size = GET_MODE_SIZE (save_mode);
556           if (offset < 0)
557             continue;
558
559           emit_move_insn (adjust_address (mem, mode, offset),
560                           gen_int_mode (size, mode));
561         }
562     }
563
564   if (!wrote_return_column)
565     init_return_column_size (mode, mem, DWARF_FRAME_RETURN_COLUMN);
566
567 #ifdef DWARF_ALT_FRAME_RETURN_COLUMN
568   init_return_column_size (mode, mem, DWARF_ALT_FRAME_RETURN_COLUMN);
569 #endif
570
571   targetm.init_dwarf_reg_sizes_extra (address);
572 }
573
574 /* Convert a DWARF call frame info. operation to its string name */
575
576 static const char *
577 dwarf_cfi_name (unsigned int cfi_opc)
578 {
579   switch (cfi_opc)
580     {
581     case DW_CFA_advance_loc:
582       return "DW_CFA_advance_loc";
583     case DW_CFA_offset:
584       return "DW_CFA_offset";
585     case DW_CFA_restore:
586       return "DW_CFA_restore";
587     case DW_CFA_nop:
588       return "DW_CFA_nop";
589     case DW_CFA_set_loc:
590       return "DW_CFA_set_loc";
591     case DW_CFA_advance_loc1:
592       return "DW_CFA_advance_loc1";
593     case DW_CFA_advance_loc2:
594       return "DW_CFA_advance_loc2";
595     case DW_CFA_advance_loc4:
596       return "DW_CFA_advance_loc4";
597     case DW_CFA_offset_extended:
598       return "DW_CFA_offset_extended";
599     case DW_CFA_restore_extended:
600       return "DW_CFA_restore_extended";
601     case DW_CFA_undefined:
602       return "DW_CFA_undefined";
603     case DW_CFA_same_value:
604       return "DW_CFA_same_value";
605     case DW_CFA_register:
606       return "DW_CFA_register";
607     case DW_CFA_remember_state:
608       return "DW_CFA_remember_state";
609     case DW_CFA_restore_state:
610       return "DW_CFA_restore_state";
611     case DW_CFA_def_cfa:
612       return "DW_CFA_def_cfa";
613     case DW_CFA_def_cfa_register:
614       return "DW_CFA_def_cfa_register";
615     case DW_CFA_def_cfa_offset:
616       return "DW_CFA_def_cfa_offset";
617
618     /* DWARF 3 */
619     case DW_CFA_def_cfa_expression:
620       return "DW_CFA_def_cfa_expression";
621     case DW_CFA_expression:
622       return "DW_CFA_expression";
623     case DW_CFA_offset_extended_sf:
624       return "DW_CFA_offset_extended_sf";
625     case DW_CFA_def_cfa_sf:
626       return "DW_CFA_def_cfa_sf";
627     case DW_CFA_def_cfa_offset_sf:
628       return "DW_CFA_def_cfa_offset_sf";
629
630     /* SGI/MIPS specific */
631     case DW_CFA_MIPS_advance_loc8:
632       return "DW_CFA_MIPS_advance_loc8";
633
634     /* GNU extensions */
635     case DW_CFA_GNU_window_save:
636       return "DW_CFA_GNU_window_save";
637     case DW_CFA_GNU_args_size:
638       return "DW_CFA_GNU_args_size";
639     case DW_CFA_GNU_negative_offset_extended:
640       return "DW_CFA_GNU_negative_offset_extended";
641
642     default:
643       return "DW_CFA_<unknown>";
644     }
645 }
646
647 /* Return a pointer to a newly allocated Call Frame Instruction.  */
648
649 static inline dw_cfi_ref
650 new_cfi (void)
651 {
652   dw_cfi_ref cfi = GGC_NEW (dw_cfi_node);
653
654   cfi->dw_cfi_next = NULL;
655   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = 0;
656   cfi->dw_cfi_oprnd2.dw_cfi_reg_num = 0;
657
658   return cfi;
659 }
660
661 /* Add a Call Frame Instruction to list of instructions.  */
662
663 static inline void
664 add_cfi (dw_cfi_ref *list_head, dw_cfi_ref cfi)
665 {
666   dw_cfi_ref *p;
667   dw_fde_ref fde = current_fde ();
668
669   /* When DRAP is used, CFA is defined with an expression.  Redefine
670      CFA may lead to a different CFA value.   */
671   if (fde && fde->drap_reg != INVALID_REGNUM)
672     switch (cfi->dw_cfi_opc)
673       {
674         case DW_CFA_def_cfa_register:
675         case DW_CFA_def_cfa_offset:
676         case DW_CFA_def_cfa_offset_sf:
677         case DW_CFA_def_cfa:
678         case DW_CFA_def_cfa_sf:
679           gcc_unreachable ();
680
681         default:
682           break;
683       }
684
685   /* Find the end of the chain.  */
686   for (p = list_head; (*p) != NULL; p = &(*p)->dw_cfi_next)
687     ;
688
689   *p = cfi;
690 }
691
692 /* Generate a new label for the CFI info to refer to.  */
693
694 char *
695 dwarf2out_cfi_label (void)
696 {
697   static char label[20];
698
699   if (dwarf2out_do_cfi_asm ())
700     {
701       /* In this case, we will be emitting the asm directive instead of
702          the label, so just return a placeholder to keep the rest of the
703          interfaces happy.  */
704       strcpy (label, "<do not output>");
705     }
706   else
707     {
708       ASM_GENERATE_INTERNAL_LABEL (label, "LCFI", dwarf2out_cfi_label_num++);
709       ASM_OUTPUT_LABEL (asm_out_file, label);
710     }
711
712   return label;
713 }
714
715 /* Add CFI to the current fde at the PC value indicated by LABEL if specified,
716    or to the CIE if LABEL is NULL.  */
717
718 static void
719 add_fde_cfi (const char *label, dw_cfi_ref cfi)
720 {
721   dw_cfi_ref *list_head = &cie_cfi_head;
722
723   if (dwarf2out_do_cfi_asm ())
724     {
725       if (label)
726         {
727           output_cfi_directive (cfi);
728
729           /* We still have to add the cfi to the list so that
730              lookup_cfa works later on.  */
731           list_head = &current_fde ()->dw_fde_cfi;
732         }
733       /* ??? If this is a CFI for the CIE, we don't emit.  This
734          assumes that the standard CIE contents that the assembler
735          uses matches the standard CIE contents that the compiler
736          uses.  This is probably a bad assumption.  I'm not quite
737          sure how to address this for now.  */
738     }
739   else if (label)
740     {
741       dw_fde_ref fde = current_fde ();
742
743       gcc_assert (fde != NULL);
744
745       if (*label == 0)
746         label = dwarf2out_cfi_label ();
747
748       if (fde->dw_fde_current_label == NULL
749           || strcmp (label, fde->dw_fde_current_label) != 0)
750         {
751           dw_cfi_ref xcfi;
752
753           label = xstrdup (label);
754
755           /* Set the location counter to the new label.  */
756           xcfi = new_cfi ();
757           /* If we have a current label, advance from there, otherwise
758              set the location directly using set_loc.  */
759           xcfi->dw_cfi_opc = fde->dw_fde_current_label
760                              ? DW_CFA_advance_loc4
761                              : DW_CFA_set_loc;
762           xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
763           add_cfi (&fde->dw_fde_cfi, xcfi);
764
765           fde->dw_fde_current_label = label;
766         }
767
768       list_head = &fde->dw_fde_cfi;
769     }
770
771   add_cfi (list_head, cfi);
772 }
773
774 /* Subroutine of lookup_cfa.  */
775
776 static void
777 lookup_cfa_1 (dw_cfi_ref cfi, dw_cfa_location *loc)
778 {
779   switch (cfi->dw_cfi_opc)
780     {
781     case DW_CFA_def_cfa_offset:
782     case DW_CFA_def_cfa_offset_sf:
783       loc->offset = cfi->dw_cfi_oprnd1.dw_cfi_offset;
784       break;
785     case DW_CFA_def_cfa_register:
786       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
787       break;
788     case DW_CFA_def_cfa:
789     case DW_CFA_def_cfa_sf:
790       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
791       loc->offset = cfi->dw_cfi_oprnd2.dw_cfi_offset;
792       break;
793     case DW_CFA_def_cfa_expression:
794       get_cfa_from_loc_descr (loc, cfi->dw_cfi_oprnd1.dw_cfi_loc);
795       break;
796     default:
797       break;
798     }
799 }
800
801 /* Find the previous value for the CFA.  */
802
803 static void
804 lookup_cfa (dw_cfa_location *loc)
805 {
806   dw_cfi_ref cfi;
807   dw_fde_ref fde;
808
809   loc->reg = INVALID_REGNUM;
810   loc->offset = 0;
811   loc->indirect = 0;
812   loc->base_offset = 0;
813
814   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
815     lookup_cfa_1 (cfi, loc);
816
817   fde = current_fde ();
818   if (fde)
819     for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
820       lookup_cfa_1 (cfi, loc);
821 }
822
823 /* The current rule for calculating the DWARF2 canonical frame address.  */
824 static dw_cfa_location cfa;
825
826 /* The register used for saving registers to the stack, and its offset
827    from the CFA.  */
828 static dw_cfa_location cfa_store;
829
830 /* The running total of the size of arguments pushed onto the stack.  */
831 static HOST_WIDE_INT args_size;
832
833 /* The last args_size we actually output.  */
834 static HOST_WIDE_INT old_args_size;
835
836 /* Entry point to update the canonical frame address (CFA).
837    LABEL is passed to add_fde_cfi.  The value of CFA is now to be
838    calculated from REG+OFFSET.  */
839
840 void
841 dwarf2out_def_cfa (const char *label, unsigned int reg, HOST_WIDE_INT offset)
842 {
843   dw_cfa_location loc;
844   loc.indirect = 0;
845   loc.base_offset = 0;
846   loc.reg = reg;
847   loc.offset = offset;
848   def_cfa_1 (label, &loc);
849 }
850
851 /* Determine if two dw_cfa_location structures define the same data.  */
852
853 static bool
854 cfa_equal_p (const dw_cfa_location *loc1, const dw_cfa_location *loc2)
855 {
856   return (loc1->reg == loc2->reg
857           && loc1->offset == loc2->offset
858           && loc1->indirect == loc2->indirect
859           && (loc1->indirect == 0
860               || loc1->base_offset == loc2->base_offset));
861 }
862
863 /* This routine does the actual work.  The CFA is now calculated from
864    the dw_cfa_location structure.  */
865
866 static void
867 def_cfa_1 (const char *label, dw_cfa_location *loc_p)
868 {
869   dw_cfi_ref cfi;
870   dw_cfa_location old_cfa, loc;
871
872   cfa = *loc_p;
873   loc = *loc_p;
874
875   if (cfa_store.reg == loc.reg && loc.indirect == 0)
876     cfa_store.offset = loc.offset;
877
878   loc.reg = DWARF_FRAME_REGNUM (loc.reg);
879   lookup_cfa (&old_cfa);
880
881   /* If nothing changed, no need to issue any call frame instructions.  */
882   if (cfa_equal_p (&loc, &old_cfa))
883     return;
884
885   cfi = new_cfi ();
886
887   if (loc.reg == old_cfa.reg && !loc.indirect)
888     {
889       /* Construct a "DW_CFA_def_cfa_offset <offset>" instruction, indicating
890          the CFA register did not change but the offset did.  The data 
891          factoring for DW_CFA_def_cfa_offset_sf happens in output_cfi, or
892          in the assembler via the .cfi_def_cfa_offset directive.  */
893       if (loc.offset < 0)
894         cfi->dw_cfi_opc = DW_CFA_def_cfa_offset_sf;
895       else
896         cfi->dw_cfi_opc = DW_CFA_def_cfa_offset;
897       cfi->dw_cfi_oprnd1.dw_cfi_offset = loc.offset;
898     }
899
900 #ifndef MIPS_DEBUGGING_INFO  /* SGI dbx thinks this means no offset.  */
901   else if (loc.offset == old_cfa.offset
902            && old_cfa.reg != INVALID_REGNUM
903            && !loc.indirect)
904     {
905       /* Construct a "DW_CFA_def_cfa_register <register>" instruction,
906          indicating the CFA register has changed to <register> but the
907          offset has not changed.  */
908       cfi->dw_cfi_opc = DW_CFA_def_cfa_register;
909       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
910     }
911 #endif
912
913   else if (loc.indirect == 0)
914     {
915       /* Construct a "DW_CFA_def_cfa <register> <offset>" instruction,
916          indicating the CFA register has changed to <register> with
917          the specified offset.  The data factoring for DW_CFA_def_cfa_sf
918          happens in output_cfi, or in the assembler via the .cfi_def_cfa
919          directive.  */
920       if (loc.offset < 0)
921         cfi->dw_cfi_opc = DW_CFA_def_cfa_sf;
922       else
923         cfi->dw_cfi_opc = DW_CFA_def_cfa;
924       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
925       cfi->dw_cfi_oprnd2.dw_cfi_offset = loc.offset;
926     }
927   else
928     {
929       /* Construct a DW_CFA_def_cfa_expression instruction to
930          calculate the CFA using a full location expression since no
931          register-offset pair is available.  */
932       struct dw_loc_descr_struct *loc_list;
933
934       cfi->dw_cfi_opc = DW_CFA_def_cfa_expression;
935       loc_list = build_cfa_loc (&loc, 0);
936       cfi->dw_cfi_oprnd1.dw_cfi_loc = loc_list;
937     }
938
939   add_fde_cfi (label, cfi);
940 }
941
942 /* Add the CFI for saving a register.  REG is the CFA column number.
943    LABEL is passed to add_fde_cfi.
944    If SREG is -1, the register is saved at OFFSET from the CFA;
945    otherwise it is saved in SREG.  */
946
947 static void
948 reg_save (const char *label, unsigned int reg, unsigned int sreg, HOST_WIDE_INT offset)
949 {
950   dw_cfi_ref cfi = new_cfi ();
951   dw_fde_ref fde = current_fde ();
952
953   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
954
955   /* When stack is aligned, store REG using DW_CFA_expression with
956      FP.  */
957   if (fde
958       && fde->stack_realign
959       && sreg == INVALID_REGNUM)
960     {
961       cfi->dw_cfi_opc = DW_CFA_expression;
962       cfi->dw_cfi_oprnd2.dw_cfi_reg_num = reg;
963       cfi->dw_cfi_oprnd1.dw_cfi_loc
964         = build_cfa_aligned_loc (offset, fde->stack_realignment);
965     }
966   else if (sreg == INVALID_REGNUM)
967     {
968       if (offset < 0)
969         cfi->dw_cfi_opc = DW_CFA_offset_extended_sf;
970       else if (reg & ~0x3f)
971         cfi->dw_cfi_opc = DW_CFA_offset_extended;
972       else
973         cfi->dw_cfi_opc = DW_CFA_offset;
974       cfi->dw_cfi_oprnd2.dw_cfi_offset = offset;
975     }
976   else if (sreg == reg)
977     cfi->dw_cfi_opc = DW_CFA_same_value;
978   else
979     {
980       cfi->dw_cfi_opc = DW_CFA_register;
981       cfi->dw_cfi_oprnd2.dw_cfi_reg_num = sreg;
982     }
983
984   add_fde_cfi (label, cfi);
985 }
986
987 /* Add the CFI for saving a register window.  LABEL is passed to reg_save.
988    This CFI tells the unwinder that it needs to restore the window registers
989    from the previous frame's window save area.
990
991    ??? Perhaps we should note in the CIE where windows are saved (instead of
992    assuming 0(cfa)) and what registers are in the window.  */
993
994 void
995 dwarf2out_window_save (const char *label)
996 {
997   dw_cfi_ref cfi = new_cfi ();
998
999   cfi->dw_cfi_opc = DW_CFA_GNU_window_save;
1000   add_fde_cfi (label, cfi);
1001 }
1002
1003 /* Add a CFI to update the running total of the size of arguments
1004    pushed onto the stack.  */
1005
1006 void
1007 dwarf2out_args_size (const char *label, HOST_WIDE_INT size)
1008 {
1009   dw_cfi_ref cfi;
1010
1011   if (size == old_args_size)
1012     return;
1013
1014   old_args_size = size;
1015
1016   cfi = new_cfi ();
1017   cfi->dw_cfi_opc = DW_CFA_GNU_args_size;
1018   cfi->dw_cfi_oprnd1.dw_cfi_offset = size;
1019   add_fde_cfi (label, cfi);
1020 }
1021
1022 /* Entry point for saving a register to the stack.  REG is the GCC register
1023    number.  LABEL and OFFSET are passed to reg_save.  */
1024
1025 void
1026 dwarf2out_reg_save (const char *label, unsigned int reg, HOST_WIDE_INT offset)
1027 {
1028   reg_save (label, DWARF_FRAME_REGNUM (reg), INVALID_REGNUM, offset);
1029 }
1030
1031 /* Entry point for saving the return address in the stack.
1032    LABEL and OFFSET are passed to reg_save.  */
1033
1034 void
1035 dwarf2out_return_save (const char *label, HOST_WIDE_INT offset)
1036 {
1037   reg_save (label, DWARF_FRAME_RETURN_COLUMN, INVALID_REGNUM, offset);
1038 }
1039
1040 /* Entry point for saving the return address in a register.
1041    LABEL and SREG are passed to reg_save.  */
1042
1043 void
1044 dwarf2out_return_reg (const char *label, unsigned int sreg)
1045 {
1046   reg_save (label, DWARF_FRAME_RETURN_COLUMN, DWARF_FRAME_REGNUM (sreg), 0);
1047 }
1048
1049 #ifdef DWARF2_UNWIND_INFO
1050 /* Record the initial position of the return address.  RTL is
1051    INCOMING_RETURN_ADDR_RTX.  */
1052
1053 static void
1054 initial_return_save (rtx rtl)
1055 {
1056   unsigned int reg = INVALID_REGNUM;
1057   HOST_WIDE_INT offset = 0;
1058
1059   switch (GET_CODE (rtl))
1060     {
1061     case REG:
1062       /* RA is in a register.  */
1063       reg = DWARF_FRAME_REGNUM (REGNO (rtl));
1064       break;
1065
1066     case MEM:
1067       /* RA is on the stack.  */
1068       rtl = XEXP (rtl, 0);
1069       switch (GET_CODE (rtl))
1070         {
1071         case REG:
1072           gcc_assert (REGNO (rtl) == STACK_POINTER_REGNUM);
1073           offset = 0;
1074           break;
1075
1076         case PLUS:
1077           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1078           offset = INTVAL (XEXP (rtl, 1));
1079           break;
1080
1081         case MINUS:
1082           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1083           offset = -INTVAL (XEXP (rtl, 1));
1084           break;
1085
1086         default:
1087           gcc_unreachable ();
1088         }
1089
1090       break;
1091
1092     case PLUS:
1093       /* The return address is at some offset from any value we can
1094          actually load.  For instance, on the SPARC it is in %i7+8. Just
1095          ignore the offset for now; it doesn't matter for unwinding frames.  */
1096       gcc_assert (GET_CODE (XEXP (rtl, 1)) == CONST_INT);
1097       initial_return_save (XEXP (rtl, 0));
1098       return;
1099
1100     default:
1101       gcc_unreachable ();
1102     }
1103
1104   if (reg != DWARF_FRAME_RETURN_COLUMN)
1105     reg_save (NULL, DWARF_FRAME_RETURN_COLUMN, reg, offset - cfa.offset);
1106 }
1107 #endif
1108
1109 /* Given a SET, calculate the amount of stack adjustment it
1110    contains.  */
1111
1112 static HOST_WIDE_INT
1113 stack_adjust_offset (const_rtx pattern)
1114 {
1115   const_rtx src = SET_SRC (pattern);
1116   const_rtx dest = SET_DEST (pattern);
1117   HOST_WIDE_INT offset = 0;
1118   enum rtx_code code;
1119
1120   if (dest == stack_pointer_rtx)
1121     {
1122       /* (set (reg sp) (plus (reg sp) (const_int))) */
1123       code = GET_CODE (src);
1124       if (! (code == PLUS || code == MINUS)
1125           || XEXP (src, 0) != stack_pointer_rtx
1126           || GET_CODE (XEXP (src, 1)) != CONST_INT)
1127         return 0;
1128
1129       offset = INTVAL (XEXP (src, 1));
1130       if (code == PLUS)
1131         offset = -offset;
1132     }
1133   else if (MEM_P (dest))
1134     {
1135       /* (set (mem (pre_dec (reg sp))) (foo)) */
1136       src = XEXP (dest, 0);
1137       code = GET_CODE (src);
1138
1139       switch (code)
1140         {
1141         case PRE_MODIFY:
1142         case POST_MODIFY:
1143           if (XEXP (src, 0) == stack_pointer_rtx)
1144             {
1145               rtx val = XEXP (XEXP (src, 1), 1);
1146               /* We handle only adjustments by constant amount.  */
1147               gcc_assert (GET_CODE (XEXP (src, 1)) == PLUS
1148                           && GET_CODE (val) == CONST_INT);
1149               offset = -INTVAL (val);
1150               break;
1151             }
1152           return 0;
1153
1154         case PRE_DEC:
1155         case POST_DEC:
1156           if (XEXP (src, 0) == stack_pointer_rtx)
1157             {
1158               offset = GET_MODE_SIZE (GET_MODE (dest));
1159               break;
1160             }
1161           return 0;
1162
1163         case PRE_INC:
1164         case POST_INC:
1165           if (XEXP (src, 0) == stack_pointer_rtx)
1166             {
1167               offset = -GET_MODE_SIZE (GET_MODE (dest));
1168               break;
1169             }
1170           return 0;
1171
1172         default:
1173           return 0;
1174         }
1175     }
1176   else
1177     return 0;
1178
1179   return offset;
1180 }
1181
1182 /* Precomputed args_size for CODE_LABELs and BARRIERs preceeding them,
1183    indexed by INSN_UID.  */
1184
1185 static HOST_WIDE_INT *barrier_args_size;
1186
1187 /* Helper function for compute_barrier_args_size.  Handle one insn.  */
1188
1189 static HOST_WIDE_INT
1190 compute_barrier_args_size_1 (rtx insn, HOST_WIDE_INT cur_args_size,
1191                              VEC (rtx, heap) **next)
1192 {
1193   HOST_WIDE_INT offset = 0;
1194   int i;
1195
1196   if (! RTX_FRAME_RELATED_P (insn))
1197     {
1198       if (prologue_epilogue_contains (insn)
1199           || sibcall_epilogue_contains (insn))
1200         /* Nothing */;
1201       else if (GET_CODE (PATTERN (insn)) == SET)
1202         offset = stack_adjust_offset (PATTERN (insn));
1203       else if (GET_CODE (PATTERN (insn)) == PARALLEL
1204                || GET_CODE (PATTERN (insn)) == SEQUENCE)
1205         {
1206           /* There may be stack adjustments inside compound insns.  Search
1207              for them.  */
1208           for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1209             if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1210               offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i));
1211         }
1212     }
1213   else
1214     {
1215       rtx expr = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
1216
1217       if (expr)
1218         {
1219           expr = XEXP (expr, 0);
1220           if (GET_CODE (expr) == PARALLEL
1221               || GET_CODE (expr) == SEQUENCE)
1222             for (i = 1; i < XVECLEN (expr, 0); i++)
1223               {
1224                 rtx elem = XVECEXP (expr, 0, i);
1225
1226                 if (GET_CODE (elem) == SET && !RTX_FRAME_RELATED_P (elem))
1227                   offset += stack_adjust_offset (elem);
1228               }
1229         }
1230     }
1231
1232 #ifndef STACK_GROWS_DOWNWARD
1233   offset = -offset;
1234 #endif
1235
1236   cur_args_size += offset;
1237   if (cur_args_size < 0)
1238     cur_args_size = 0;
1239
1240   if (JUMP_P (insn))
1241     {
1242       rtx dest = JUMP_LABEL (insn);
1243
1244       if (dest)
1245         {
1246           if (barrier_args_size [INSN_UID (dest)] < 0)
1247             {
1248               barrier_args_size [INSN_UID (dest)] = cur_args_size;
1249               VEC_safe_push (rtx, heap, *next, dest);
1250             }
1251         }
1252     }
1253
1254   return cur_args_size;
1255 }
1256
1257 /* Walk the whole function and compute args_size on BARRIERs.  */
1258
1259 static void
1260 compute_barrier_args_size (void)
1261 {
1262   int max_uid = get_max_uid (), i;
1263   rtx insn;
1264   VEC (rtx, heap) *worklist, *next, *tmp;
1265
1266   barrier_args_size = XNEWVEC (HOST_WIDE_INT, max_uid);
1267   for (i = 0; i < max_uid; i++)
1268     barrier_args_size[i] = -1;
1269
1270   worklist = VEC_alloc (rtx, heap, 20);
1271   next = VEC_alloc (rtx, heap, 20);
1272   insn = get_insns ();
1273   barrier_args_size[INSN_UID (insn)] = 0;
1274   VEC_quick_push (rtx, worklist, insn);
1275   for (;;)
1276     {
1277       while (!VEC_empty (rtx, worklist))
1278         {
1279           rtx prev, body, first_insn;
1280           HOST_WIDE_INT cur_args_size;
1281
1282           first_insn = insn = VEC_pop (rtx, worklist);
1283           cur_args_size = barrier_args_size[INSN_UID (insn)];
1284           prev = prev_nonnote_insn (insn);
1285           if (prev && BARRIER_P (prev))
1286             barrier_args_size[INSN_UID (prev)] = cur_args_size;
1287
1288           for (; insn; insn = NEXT_INSN (insn))
1289             {
1290               if (INSN_DELETED_P (insn) || NOTE_P (insn))
1291                 continue;
1292               if (BARRIER_P (insn))
1293                 break;
1294
1295               if (LABEL_P (insn))
1296                 {
1297                   if (insn == first_insn)
1298                     continue;
1299                   else if (barrier_args_size[INSN_UID (insn)] < 0)
1300                     {
1301                       barrier_args_size[INSN_UID (insn)] = cur_args_size;
1302                       continue;
1303                     }
1304                   else
1305                     {
1306                       /* The insns starting with this label have been
1307                          already scanned or are in the worklist.  */
1308                       break;
1309                     }
1310                 }
1311
1312               body = PATTERN (insn);
1313               if (GET_CODE (body) == SEQUENCE)
1314                 {
1315                   for (i = 1; i < XVECLEN (body, 0); i++)
1316                     cur_args_size
1317                       = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1318                                                      cur_args_size, &next);
1319                   cur_args_size
1320                     = compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1321                                                    cur_args_size, &next);
1322                 }
1323               else
1324                 cur_args_size
1325                   = compute_barrier_args_size_1 (insn, cur_args_size, &next);
1326             }
1327         }
1328
1329       if (VEC_empty (rtx, next))
1330         break;
1331
1332       /* Swap WORKLIST with NEXT and truncate NEXT for next iteration.  */
1333       tmp = next;
1334       next = worklist;
1335       worklist = tmp;
1336       VEC_truncate (rtx, next, 0);
1337     }
1338
1339   VEC_free (rtx, heap, worklist);
1340   VEC_free (rtx, heap, next);
1341 }
1342
1343
1344 /* Check INSN to see if it looks like a push or a stack adjustment, and
1345    make a note of it if it does.  EH uses this information to find out how
1346    much extra space it needs to pop off the stack.  */
1347
1348 static void
1349 dwarf2out_stack_adjust (rtx insn, bool after_p)
1350 {
1351   HOST_WIDE_INT offset;
1352   const char *label;
1353   int i;
1354
1355   /* Don't handle epilogues at all.  Certainly it would be wrong to do so
1356      with this function.  Proper support would require all frame-related
1357      insns to be marked, and to be able to handle saving state around
1358      epilogues textually in the middle of the function.  */
1359   if (prologue_epilogue_contains (insn) || sibcall_epilogue_contains (insn))
1360     return;
1361
1362   /* If only calls can throw, and we have a frame pointer,
1363      save up adjustments until we see the CALL_INSN.  */
1364   if (!flag_asynchronous_unwind_tables && cfa.reg != STACK_POINTER_REGNUM)
1365     {
1366       if (CALL_P (insn) && !after_p)
1367         {
1368           /* Extract the size of the args from the CALL rtx itself.  */
1369           insn = PATTERN (insn);
1370           if (GET_CODE (insn) == PARALLEL)
1371             insn = XVECEXP (insn, 0, 0);
1372           if (GET_CODE (insn) == SET)
1373             insn = SET_SRC (insn);
1374           gcc_assert (GET_CODE (insn) == CALL);
1375           dwarf2out_args_size ("", INTVAL (XEXP (insn, 1)));
1376         }
1377       return;
1378     }
1379
1380   if (CALL_P (insn) && !after_p)
1381     {
1382       if (!flag_asynchronous_unwind_tables)
1383         dwarf2out_args_size ("", args_size);
1384       return;
1385     }
1386   else if (BARRIER_P (insn))
1387     {
1388       /* Don't call compute_barrier_args_size () if the only
1389          BARRIER is at the end of function.  */
1390       if (barrier_args_size == NULL && next_nonnote_insn (insn))
1391         compute_barrier_args_size ();
1392       if (barrier_args_size == NULL)
1393         offset = 0;
1394       else
1395         {
1396           offset = barrier_args_size[INSN_UID (insn)];
1397           if (offset < 0)
1398             offset = 0;
1399         }
1400
1401       offset -= args_size;
1402 #ifndef STACK_GROWS_DOWNWARD
1403       offset = -offset;
1404 #endif
1405     }
1406   else if (GET_CODE (PATTERN (insn)) == SET)
1407     offset = stack_adjust_offset (PATTERN (insn));
1408   else if (GET_CODE (PATTERN (insn)) == PARALLEL
1409            || GET_CODE (PATTERN (insn)) == SEQUENCE)
1410     {
1411       /* There may be stack adjustments inside compound insns.  Search
1412          for them.  */
1413       for (offset = 0, i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1414         if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1415           offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i));
1416     }
1417   else
1418     return;
1419
1420   if (offset == 0)
1421     return;
1422
1423   label = dwarf2out_cfi_label ();
1424   dwarf2out_args_size_adjust (offset, label);
1425 }
1426
1427 /* Adjust args_size based on stack adjustment OFFSET.  */
1428
1429 static void
1430 dwarf2out_args_size_adjust (HOST_WIDE_INT offset, const char *label)
1431 {
1432   if (cfa.reg == STACK_POINTER_REGNUM)
1433     cfa.offset += offset;
1434
1435   if (cfa_store.reg == STACK_POINTER_REGNUM)
1436     cfa_store.offset += offset;
1437
1438 #ifndef STACK_GROWS_DOWNWARD
1439   offset = -offset;
1440 #endif
1441
1442   args_size += offset;
1443   if (args_size < 0)
1444     args_size = 0;
1445
1446   def_cfa_1 (label, &cfa);
1447   if (flag_asynchronous_unwind_tables)
1448     dwarf2out_args_size (label, args_size);
1449 }
1450
1451 #endif
1452
1453 /* We delay emitting a register save until either (a) we reach the end
1454    of the prologue or (b) the register is clobbered.  This clusters
1455    register saves so that there are fewer pc advances.  */
1456
1457 struct queued_reg_save GTY(())
1458 {
1459   struct queued_reg_save *next;
1460   rtx reg;
1461   HOST_WIDE_INT cfa_offset;
1462   rtx saved_reg;
1463 };
1464
1465 static GTY(()) struct queued_reg_save *queued_reg_saves;
1466
1467 /* The caller's ORIG_REG is saved in SAVED_IN_REG.  */
1468 struct reg_saved_in_data GTY(()) {
1469   rtx orig_reg;
1470   rtx saved_in_reg;
1471 };
1472
1473 /* A list of registers saved in other registers.
1474    The list intentionally has a small maximum capacity of 4; if your
1475    port needs more than that, you might consider implementing a
1476    more efficient data structure.  */
1477 static GTY(()) struct reg_saved_in_data regs_saved_in_regs[4];
1478 static GTY(()) size_t num_regs_saved_in_regs;
1479
1480 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
1481 static const char *last_reg_save_label;
1482
1483 /* Add an entry to QUEUED_REG_SAVES saying that REG is now saved at
1484    SREG, or if SREG is NULL then it is saved at OFFSET to the CFA.  */
1485
1486 static void
1487 queue_reg_save (const char *label, rtx reg, rtx sreg, HOST_WIDE_INT offset)
1488 {
1489   struct queued_reg_save *q;
1490
1491   /* Duplicates waste space, but it's also necessary to remove them
1492      for correctness, since the queue gets output in reverse
1493      order.  */
1494   for (q = queued_reg_saves; q != NULL; q = q->next)
1495     if (REGNO (q->reg) == REGNO (reg))
1496       break;
1497
1498   if (q == NULL)
1499     {
1500       q = GGC_NEW (struct queued_reg_save);
1501       q->next = queued_reg_saves;
1502       queued_reg_saves = q;
1503     }
1504
1505   q->reg = reg;
1506   q->cfa_offset = offset;
1507   q->saved_reg = sreg;
1508
1509   last_reg_save_label = label;
1510 }
1511
1512 /* Output all the entries in QUEUED_REG_SAVES.  */
1513
1514 static void
1515 flush_queued_reg_saves (void)
1516 {
1517   struct queued_reg_save *q;
1518
1519   for (q = queued_reg_saves; q; q = q->next)
1520     {
1521       size_t i;
1522       unsigned int reg, sreg;
1523
1524       for (i = 0; i < num_regs_saved_in_regs; i++)
1525         if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (q->reg))
1526           break;
1527       if (q->saved_reg && i == num_regs_saved_in_regs)
1528         {
1529           gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1530           num_regs_saved_in_regs++;
1531         }
1532       if (i != num_regs_saved_in_regs)
1533         {
1534           regs_saved_in_regs[i].orig_reg = q->reg;
1535           regs_saved_in_regs[i].saved_in_reg = q->saved_reg;
1536         }
1537
1538       reg = DWARF_FRAME_REGNUM (REGNO (q->reg));
1539       if (q->saved_reg)
1540         sreg = DWARF_FRAME_REGNUM (REGNO (q->saved_reg));
1541       else
1542         sreg = INVALID_REGNUM;
1543       reg_save (last_reg_save_label, reg, sreg, q->cfa_offset);
1544     }
1545
1546   queued_reg_saves = NULL;
1547   last_reg_save_label = NULL;
1548 }
1549
1550 /* Does INSN clobber any register which QUEUED_REG_SAVES lists a saved
1551    location for?  Or, does it clobber a register which we've previously
1552    said that some other register is saved in, and for which we now
1553    have a new location for?  */
1554
1555 static bool
1556 clobbers_queued_reg_save (const_rtx insn)
1557 {
1558   struct queued_reg_save *q;
1559
1560   for (q = queued_reg_saves; q; q = q->next)
1561     {
1562       size_t i;
1563       if (modified_in_p (q->reg, insn))
1564         return true;
1565       for (i = 0; i < num_regs_saved_in_regs; i++)
1566         if (REGNO (q->reg) == REGNO (regs_saved_in_regs[i].orig_reg)
1567             && modified_in_p (regs_saved_in_regs[i].saved_in_reg, insn))
1568           return true;
1569     }
1570
1571   return false;
1572 }
1573
1574 /* Entry point for saving the first register into the second.  */
1575
1576 void
1577 dwarf2out_reg_save_reg (const char *label, rtx reg, rtx sreg)
1578 {
1579   size_t i;
1580   unsigned int regno, sregno;
1581
1582   for (i = 0; i < num_regs_saved_in_regs; i++)
1583     if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (reg))
1584       break;
1585   if (i == num_regs_saved_in_regs)
1586     {
1587       gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1588       num_regs_saved_in_regs++;
1589     }
1590   regs_saved_in_regs[i].orig_reg = reg;
1591   regs_saved_in_regs[i].saved_in_reg = sreg;
1592
1593   regno = DWARF_FRAME_REGNUM (REGNO (reg));
1594   sregno = DWARF_FRAME_REGNUM (REGNO (sreg));
1595   reg_save (label, regno, sregno, 0);
1596 }
1597
1598 /* What register, if any, is currently saved in REG?  */
1599
1600 static rtx
1601 reg_saved_in (rtx reg)
1602 {
1603   unsigned int regn = REGNO (reg);
1604   size_t i;
1605   struct queued_reg_save *q;
1606
1607   for (q = queued_reg_saves; q; q = q->next)
1608     if (q->saved_reg && regn == REGNO (q->saved_reg))
1609       return q->reg;
1610
1611   for (i = 0; i < num_regs_saved_in_regs; i++)
1612     if (regs_saved_in_regs[i].saved_in_reg
1613         && regn == REGNO (regs_saved_in_regs[i].saved_in_reg))
1614       return regs_saved_in_regs[i].orig_reg;
1615
1616   return NULL_RTX;
1617 }
1618
1619
1620 /* A temporary register holding an integral value used in adjusting SP
1621    or setting up the store_reg.  The "offset" field holds the integer
1622    value, not an offset.  */
1623 static dw_cfa_location cfa_temp;
1624
1625 /* Record call frame debugging information for an expression EXPR,
1626    which either sets SP or FP (adjusting how we calculate the frame
1627    address) or saves a register to the stack or another register.
1628    LABEL indicates the address of EXPR.
1629
1630    This function encodes a state machine mapping rtxes to actions on
1631    cfa, cfa_store, and cfa_temp.reg.  We describe these rules so
1632    users need not read the source code.
1633
1634   The High-Level Picture
1635
1636   Changes in the register we use to calculate the CFA: Currently we
1637   assume that if you copy the CFA register into another register, we
1638   should take the other one as the new CFA register; this seems to
1639   work pretty well.  If it's wrong for some target, it's simple
1640   enough not to set RTX_FRAME_RELATED_P on the insn in question.
1641
1642   Changes in the register we use for saving registers to the stack:
1643   This is usually SP, but not always.  Again, we deduce that if you
1644   copy SP into another register (and SP is not the CFA register),
1645   then the new register is the one we will be using for register
1646   saves.  This also seems to work.
1647
1648   Register saves: There's not much guesswork about this one; if
1649   RTX_FRAME_RELATED_P is set on an insn which modifies memory, it's a
1650   register save, and the register used to calculate the destination
1651   had better be the one we think we're using for this purpose.
1652   It's also assumed that a copy from a call-saved register to another
1653   register is saving that register if RTX_FRAME_RELATED_P is set on
1654   that instruction.  If the copy is from a call-saved register to
1655   the *same* register, that means that the register is now the same
1656   value as in the caller.
1657
1658   Except: If the register being saved is the CFA register, and the
1659   offset is nonzero, we are saving the CFA, so we assume we have to
1660   use DW_CFA_def_cfa_expression.  If the offset is 0, we assume that
1661   the intent is to save the value of SP from the previous frame.
1662
1663   In addition, if a register has previously been saved to a different
1664   register,
1665
1666   Invariants / Summaries of Rules
1667
1668   cfa          current rule for calculating the CFA.  It usually
1669                consists of a register and an offset.
1670   cfa_store    register used by prologue code to save things to the stack
1671                cfa_store.offset is the offset from the value of
1672                cfa_store.reg to the actual CFA
1673   cfa_temp     register holding an integral value.  cfa_temp.offset
1674                stores the value, which will be used to adjust the
1675                stack pointer.  cfa_temp is also used like cfa_store,
1676                to track stores to the stack via fp or a temp reg.
1677
1678   Rules  1- 4: Setting a register's value to cfa.reg or an expression
1679                with cfa.reg as the first operand changes the cfa.reg and its
1680                cfa.offset.  Rule 1 and 4 also set cfa_temp.reg and
1681                cfa_temp.offset.
1682
1683   Rules  6- 9: Set a non-cfa.reg register value to a constant or an
1684                expression yielding a constant.  This sets cfa_temp.reg
1685                and cfa_temp.offset.
1686
1687   Rule 5:      Create a new register cfa_store used to save items to the
1688                stack.
1689
1690   Rules 10-14: Save a register to the stack.  Define offset as the
1691                difference of the original location and cfa_store's
1692                location (or cfa_temp's location if cfa_temp is used).
1693
1694   Rules 16-20: If AND operation happens on sp in prologue, we assume
1695                stack is realigned.  We will use a group of DW_OP_XXX
1696                expressions to represent the location of the stored
1697                register instead of CFA+offset.
1698
1699   The Rules
1700
1701   "{a,b}" indicates a choice of a xor b.
1702   "<reg>:cfa.reg" indicates that <reg> must equal cfa.reg.
1703
1704   Rule 1:
1705   (set <reg1> <reg2>:cfa.reg)
1706   effects: cfa.reg = <reg1>
1707            cfa.offset unchanged
1708            cfa_temp.reg = <reg1>
1709            cfa_temp.offset = cfa.offset
1710
1711   Rule 2:
1712   (set sp ({minus,plus,losum} {sp,fp}:cfa.reg
1713                               {<const_int>,<reg>:cfa_temp.reg}))
1714   effects: cfa.reg = sp if fp used
1715            cfa.offset += {+/- <const_int>, cfa_temp.offset} if cfa.reg==sp
1716            cfa_store.offset += {+/- <const_int>, cfa_temp.offset}
1717              if cfa_store.reg==sp
1718
1719   Rule 3:
1720   (set fp ({minus,plus,losum} <reg>:cfa.reg <const_int>))
1721   effects: cfa.reg = fp
1722            cfa_offset += +/- <const_int>
1723
1724   Rule 4:
1725   (set <reg1> ({plus,losum} <reg2>:cfa.reg <const_int>))
1726   constraints: <reg1> != fp
1727                <reg1> != sp
1728   effects: cfa.reg = <reg1>
1729            cfa_temp.reg = <reg1>
1730            cfa_temp.offset = cfa.offset
1731
1732   Rule 5:
1733   (set <reg1> (plus <reg2>:cfa_temp.reg sp:cfa.reg))
1734   constraints: <reg1> != fp
1735                <reg1> != sp
1736   effects: cfa_store.reg = <reg1>
1737            cfa_store.offset = cfa.offset - cfa_temp.offset
1738
1739   Rule 6:
1740   (set <reg> <const_int>)
1741   effects: cfa_temp.reg = <reg>
1742            cfa_temp.offset = <const_int>
1743
1744   Rule 7:
1745   (set <reg1>:cfa_temp.reg (ior <reg2>:cfa_temp.reg <const_int>))
1746   effects: cfa_temp.reg = <reg1>
1747            cfa_temp.offset |= <const_int>
1748
1749   Rule 8:
1750   (set <reg> (high <exp>))
1751   effects: none
1752
1753   Rule 9:
1754   (set <reg> (lo_sum <exp> <const_int>))
1755   effects: cfa_temp.reg = <reg>
1756            cfa_temp.offset = <const_int>
1757
1758   Rule 10:
1759   (set (mem (pre_modify sp:cfa_store (???? <reg1> <const_int>))) <reg2>)
1760   effects: cfa_store.offset -= <const_int>
1761            cfa.offset = cfa_store.offset if cfa.reg == sp
1762            cfa.reg = sp
1763            cfa.base_offset = -cfa_store.offset
1764
1765   Rule 11:
1766   (set (mem ({pre_inc,pre_dec} sp:cfa_store.reg)) <reg>)
1767   effects: cfa_store.offset += -/+ mode_size(mem)
1768            cfa.offset = cfa_store.offset if cfa.reg == sp
1769            cfa.reg = sp
1770            cfa.base_offset = -cfa_store.offset
1771
1772   Rule 12:
1773   (set (mem ({minus,plus,losum} <reg1>:{cfa_store,cfa_temp} <const_int>))
1774
1775        <reg2>)
1776   effects: cfa.reg = <reg1>
1777            cfa.base_offset = -/+ <const_int> - {cfa_store,cfa_temp}.offset
1778
1779   Rule 13:
1780   (set (mem <reg1>:{cfa_store,cfa_temp}) <reg2>)
1781   effects: cfa.reg = <reg1>
1782            cfa.base_offset = -{cfa_store,cfa_temp}.offset
1783
1784   Rule 14:
1785   (set (mem (postinc <reg1>:cfa_temp <const_int>)) <reg2>)
1786   effects: cfa.reg = <reg1>
1787            cfa.base_offset = -cfa_temp.offset
1788            cfa_temp.offset -= mode_size(mem)
1789
1790   Rule 15:
1791   (set <reg> {unspec, unspec_volatile})
1792   effects: target-dependent
1793
1794   Rule 16:
1795   (set sp (and: sp <const_int>))
1796   constraints: cfa_store.reg == sp
1797   effects: current_fde.stack_realign = 1
1798            cfa_store.offset = 0
1799            fde->drap_reg = cfa.reg if cfa.reg != sp and cfa.reg != fp
1800
1801   Rule 17:
1802   (set (mem ({pre_inc, pre_dec} sp)) (mem (plus (cfa.reg) (const_int))))
1803   effects: cfa_store.offset += -/+ mode_size(mem)
1804
1805   Rule 18:
1806   (set (mem ({pre_inc, pre_dec} sp)) fp)
1807   constraints: fde->stack_realign == 1
1808   effects: cfa_store.offset = 0
1809            cfa.reg != HARD_FRAME_POINTER_REGNUM
1810
1811   Rule 19:
1812   (set (mem ({pre_inc, pre_dec} sp)) cfa.reg)
1813   constraints: fde->stack_realign == 1
1814                && cfa.offset == 0
1815                && cfa.indirect == 0
1816                && cfa.reg != HARD_FRAME_POINTER_REGNUM
1817   effects: Use DW_CFA_def_cfa_expression to define cfa
1818            cfa.reg == fde->drap_reg
1819
1820   Rule 20:
1821   (set reg fde->drap_reg)
1822   constraints: fde->vdrap_reg == INVALID_REGNUM
1823   effects: fde->vdrap_reg = reg.
1824   (set mem fde->drap_reg)
1825   constraints: fde->drap_reg_saved == 1
1826   effects: none.  */
1827
1828 static void
1829 dwarf2out_frame_debug_expr (rtx expr, const char *label)
1830 {
1831   rtx src, dest, span;
1832   HOST_WIDE_INT offset;
1833   dw_fde_ref fde;
1834
1835   /* If RTX_FRAME_RELATED_P is set on a PARALLEL, process each member of
1836      the PARALLEL independently. The first element is always processed if
1837      it is a SET. This is for backward compatibility.   Other elements
1838      are processed only if they are SETs and the RTX_FRAME_RELATED_P
1839      flag is set in them.  */
1840   if (GET_CODE (expr) == PARALLEL || GET_CODE (expr) == SEQUENCE)
1841     {
1842       int par_index;
1843       int limit = XVECLEN (expr, 0);
1844       rtx elem;
1845
1846       /* PARALLELs have strict read-modify-write semantics, so we
1847          ought to evaluate every rvalue before changing any lvalue.
1848          It's cumbersome to do that in general, but there's an
1849          easy approximation that is enough for all current users:
1850          handle register saves before register assignments.  */
1851       if (GET_CODE (expr) == PARALLEL)
1852         for (par_index = 0; par_index < limit; par_index++)
1853           {
1854             elem = XVECEXP (expr, 0, par_index);
1855             if (GET_CODE (elem) == SET
1856                 && MEM_P (SET_DEST (elem))
1857                 && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
1858               dwarf2out_frame_debug_expr (elem, label);
1859           }
1860
1861       for (par_index = 0; par_index < limit; par_index++)
1862         {
1863           elem = XVECEXP (expr, 0, par_index);
1864           if (GET_CODE (elem) == SET
1865               && (!MEM_P (SET_DEST (elem)) || GET_CODE (expr) == SEQUENCE)
1866               && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
1867             dwarf2out_frame_debug_expr (elem, label);
1868           else if (GET_CODE (elem) == SET
1869                    && par_index != 0
1870                    && !RTX_FRAME_RELATED_P (elem))
1871             {
1872               /* Stack adjustment combining might combine some post-prologue
1873                  stack adjustment into a prologue stack adjustment.  */
1874               HOST_WIDE_INT offset = stack_adjust_offset (elem);
1875
1876               if (offset != 0)
1877                 dwarf2out_args_size_adjust (offset, label);
1878             }
1879         }
1880       return;
1881     }
1882
1883   gcc_assert (GET_CODE (expr) == SET);
1884
1885   src = SET_SRC (expr);
1886   dest = SET_DEST (expr);
1887
1888   if (REG_P (src))
1889     {
1890       rtx rsi = reg_saved_in (src);
1891       if (rsi)
1892         src = rsi;
1893     }
1894
1895   fde = current_fde ();
1896
1897   if (GET_CODE (src) == REG
1898       && fde
1899       && fde->drap_reg == REGNO (src)
1900       && (fde->drap_reg_saved
1901           || GET_CODE (dest) == REG))
1902     {
1903       /* Rule 20 */
1904       /* If we are saving dynamic realign argument pointer to a
1905          register, the destination is virtual dynamic realign
1906          argument pointer.  It may be used to access argument.  */
1907       if (GET_CODE (dest) == REG)
1908         {
1909           gcc_assert (fde->vdrap_reg == INVALID_REGNUM);
1910           fde->vdrap_reg = REGNO (dest);
1911         }
1912       return;
1913     }
1914
1915   switch (GET_CODE (dest))
1916     {
1917     case REG:
1918       switch (GET_CODE (src))
1919         {
1920           /* Setting FP from SP.  */
1921         case REG:
1922           if (cfa.reg == (unsigned) REGNO (src))
1923             {
1924               /* Rule 1 */
1925               /* Update the CFA rule wrt SP or FP.  Make sure src is
1926                  relative to the current CFA register.
1927
1928                  We used to require that dest be either SP or FP, but the
1929                  ARM copies SP to a temporary register, and from there to
1930                  FP.  So we just rely on the backends to only set
1931                  RTX_FRAME_RELATED_P on appropriate insns.  */
1932               cfa.reg = REGNO (dest);
1933               cfa_temp.reg = cfa.reg;
1934               cfa_temp.offset = cfa.offset;
1935             }
1936           else
1937             {
1938               /* Saving a register in a register.  */
1939               gcc_assert (!fixed_regs [REGNO (dest)]
1940                           /* For the SPARC and its register window.  */
1941                           || (DWARF_FRAME_REGNUM (REGNO (src))
1942                               == DWARF_FRAME_RETURN_COLUMN));
1943
1944               /* After stack is aligned, we can only save SP in FP
1945                  if drap register is used.  In this case, we have
1946                  to restore stack pointer with the CFA value and we
1947                  don't generate this DWARF information.  */
1948               if (fde
1949                   && fde->stack_realign
1950                   && REGNO (src) == STACK_POINTER_REGNUM)
1951                 gcc_assert (REGNO (dest) == HARD_FRAME_POINTER_REGNUM
1952                             && fde->drap_reg != INVALID_REGNUM
1953                             && cfa.reg != REGNO (src));
1954               else
1955                 queue_reg_save (label, src, dest, 0);
1956             }
1957           break;
1958
1959         case PLUS:
1960         case MINUS:
1961         case LO_SUM:
1962           if (dest == stack_pointer_rtx)
1963             {
1964               /* Rule 2 */
1965               /* Adjusting SP.  */
1966               switch (GET_CODE (XEXP (src, 1)))
1967                 {
1968                 case CONST_INT:
1969                   offset = INTVAL (XEXP (src, 1));
1970                   break;
1971                 case REG:
1972                   gcc_assert ((unsigned) REGNO (XEXP (src, 1))
1973                               == cfa_temp.reg);
1974                   offset = cfa_temp.offset;
1975                   break;
1976                 default:
1977                   gcc_unreachable ();
1978                 }
1979
1980               if (XEXP (src, 0) == hard_frame_pointer_rtx)
1981                 {
1982                   /* Restoring SP from FP in the epilogue.  */
1983                   gcc_assert (cfa.reg == (unsigned) HARD_FRAME_POINTER_REGNUM);
1984                   cfa.reg = STACK_POINTER_REGNUM;
1985                 }
1986               else if (GET_CODE (src) == LO_SUM)
1987                 /* Assume we've set the source reg of the LO_SUM from sp.  */
1988                 ;
1989               else
1990                 gcc_assert (XEXP (src, 0) == stack_pointer_rtx);
1991
1992               if (GET_CODE (src) != MINUS)
1993                 offset = -offset;
1994               if (cfa.reg == STACK_POINTER_REGNUM)
1995                 cfa.offset += offset;
1996               if (cfa_store.reg == STACK_POINTER_REGNUM)
1997                 cfa_store.offset += offset;
1998             }
1999           else if (dest == hard_frame_pointer_rtx)
2000             {
2001               /* Rule 3 */
2002               /* Either setting the FP from an offset of the SP,
2003                  or adjusting the FP */
2004               gcc_assert (frame_pointer_needed);
2005
2006               gcc_assert (REG_P (XEXP (src, 0))
2007                           && (unsigned) REGNO (XEXP (src, 0)) == cfa.reg
2008                           && GET_CODE (XEXP (src, 1)) == CONST_INT);
2009               offset = INTVAL (XEXP (src, 1));
2010               if (GET_CODE (src) != MINUS)
2011                 offset = -offset;
2012               cfa.offset += offset;
2013               cfa.reg = HARD_FRAME_POINTER_REGNUM;
2014             }
2015           else
2016             {
2017               gcc_assert (GET_CODE (src) != MINUS);
2018
2019               /* Rule 4 */
2020               if (REG_P (XEXP (src, 0))
2021                   && REGNO (XEXP (src, 0)) == cfa.reg
2022                   && GET_CODE (XEXP (src, 1)) == CONST_INT)
2023                 {
2024                   /* Setting a temporary CFA register that will be copied
2025                      into the FP later on.  */
2026                   offset = - INTVAL (XEXP (src, 1));
2027                   cfa.offset += offset;
2028                   cfa.reg = REGNO (dest);
2029                   /* Or used to save regs to the stack.  */
2030                   cfa_temp.reg = cfa.reg;
2031                   cfa_temp.offset = cfa.offset;
2032                 }
2033
2034               /* Rule 5 */
2035               else if (REG_P (XEXP (src, 0))
2036                        && REGNO (XEXP (src, 0)) == cfa_temp.reg
2037                        && XEXP (src, 1) == stack_pointer_rtx)
2038                 {
2039                   /* Setting a scratch register that we will use instead
2040                      of SP for saving registers to the stack.  */
2041                   gcc_assert (cfa.reg == STACK_POINTER_REGNUM);
2042                   cfa_store.reg = REGNO (dest);
2043                   cfa_store.offset = cfa.offset - cfa_temp.offset;
2044                 }
2045
2046               /* Rule 9 */
2047               else if (GET_CODE (src) == LO_SUM
2048                        && GET_CODE (XEXP (src, 1)) == CONST_INT)
2049                 {
2050                   cfa_temp.reg = REGNO (dest);
2051                   cfa_temp.offset = INTVAL (XEXP (src, 1));
2052                 }
2053               else
2054                 gcc_unreachable ();
2055             }
2056           break;
2057
2058           /* Rule 6 */
2059         case CONST_INT:
2060           cfa_temp.reg = REGNO (dest);
2061           cfa_temp.offset = INTVAL (src);
2062           break;
2063
2064           /* Rule 7 */
2065         case IOR:
2066           gcc_assert (REG_P (XEXP (src, 0))
2067                       && (unsigned) REGNO (XEXP (src, 0)) == cfa_temp.reg
2068                       && GET_CODE (XEXP (src, 1)) == CONST_INT);
2069
2070           if ((unsigned) REGNO (dest) != cfa_temp.reg)
2071             cfa_temp.reg = REGNO (dest);
2072           cfa_temp.offset |= INTVAL (XEXP (src, 1));
2073           break;
2074
2075           /* Skip over HIGH, assuming it will be followed by a LO_SUM,
2076              which will fill in all of the bits.  */
2077           /* Rule 8 */
2078         case HIGH:
2079           break;
2080
2081           /* Rule 15 */
2082         case UNSPEC:
2083         case UNSPEC_VOLATILE:
2084           gcc_assert (targetm.dwarf_handle_frame_unspec);
2085           targetm.dwarf_handle_frame_unspec (label, expr, XINT (src, 1));
2086           return;
2087
2088           /* Rule 16 */
2089         case AND:
2090           /* If this AND operation happens on stack pointer in prologue,
2091              we assume the stack is realigned and we extract the
2092              alignment.  */
2093           if (fde && XEXP (src, 0) == stack_pointer_rtx)
2094             {
2095               gcc_assert (cfa_store.reg == REGNO (XEXP (src, 0)));
2096               fde->stack_realign = 1;
2097               fde->stack_realignment = INTVAL (XEXP (src, 1));
2098               cfa_store.offset = 0;
2099
2100               if (cfa.reg != STACK_POINTER_REGNUM
2101                   && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2102                 fde->drap_reg = cfa.reg;
2103             }
2104           return;
2105
2106         default:
2107           gcc_unreachable ();
2108         }
2109
2110       def_cfa_1 (label, &cfa);
2111       break;
2112
2113     case MEM:
2114
2115       /* Saving a register to the stack.  Make sure dest is relative to the
2116          CFA register.  */
2117       switch (GET_CODE (XEXP (dest, 0)))
2118         {
2119           /* Rule 10 */
2120           /* With a push.  */
2121         case PRE_MODIFY:
2122           /* We can't handle variable size modifications.  */
2123           gcc_assert (GET_CODE (XEXP (XEXP (XEXP (dest, 0), 1), 1))
2124                       == CONST_INT);
2125           offset = -INTVAL (XEXP (XEXP (XEXP (dest, 0), 1), 1));
2126
2127           gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
2128                       && cfa_store.reg == STACK_POINTER_REGNUM);
2129
2130           cfa_store.offset += offset;
2131           if (cfa.reg == STACK_POINTER_REGNUM)
2132             cfa.offset = cfa_store.offset;
2133
2134           offset = -cfa_store.offset;
2135           break;
2136
2137           /* Rule 11 */
2138         case PRE_INC:
2139         case PRE_DEC:
2140           offset = GET_MODE_SIZE (GET_MODE (dest));
2141           if (GET_CODE (XEXP (dest, 0)) == PRE_INC)
2142             offset = -offset;
2143
2144           gcc_assert ((REGNO (XEXP (XEXP (dest, 0), 0))
2145                        == STACK_POINTER_REGNUM)
2146                       && cfa_store.reg == STACK_POINTER_REGNUM);
2147
2148           cfa_store.offset += offset;
2149
2150           /* Rule 18: If stack is aligned, we will use FP as a
2151              reference to represent the address of the stored
2152              regiser.  */
2153           if (fde
2154               && fde->stack_realign
2155               && src == hard_frame_pointer_rtx)
2156             {
2157               gcc_assert (cfa.reg != HARD_FRAME_POINTER_REGNUM);
2158               cfa_store.offset = 0;
2159             }
2160
2161           if (cfa.reg == STACK_POINTER_REGNUM)
2162             cfa.offset = cfa_store.offset;
2163
2164           offset = -cfa_store.offset;
2165           break;
2166
2167           /* Rule 12 */
2168           /* With an offset.  */
2169         case PLUS:
2170         case MINUS:
2171         case LO_SUM:
2172           {
2173             int regno;
2174
2175             gcc_assert (GET_CODE (XEXP (XEXP (dest, 0), 1)) == CONST_INT
2176                         && REG_P (XEXP (XEXP (dest, 0), 0)));
2177             offset = INTVAL (XEXP (XEXP (dest, 0), 1));
2178             if (GET_CODE (XEXP (dest, 0)) == MINUS)
2179               offset = -offset;
2180
2181             regno = REGNO (XEXP (XEXP (dest, 0), 0));
2182
2183             if (cfa_store.reg == (unsigned) regno)
2184               offset -= cfa_store.offset;
2185             else
2186               {
2187                 gcc_assert (cfa_temp.reg == (unsigned) regno);
2188                 offset -= cfa_temp.offset;
2189               }
2190           }
2191           break;
2192
2193           /* Rule 13 */
2194           /* Without an offset.  */
2195         case REG:
2196           {
2197             int regno = REGNO (XEXP (dest, 0));
2198
2199             if (cfa_store.reg == (unsigned) regno)
2200               offset = -cfa_store.offset;
2201             else
2202               {
2203                 gcc_assert (cfa_temp.reg == (unsigned) regno);
2204                 offset = -cfa_temp.offset;
2205               }
2206           }
2207           break;
2208
2209           /* Rule 14 */
2210         case POST_INC:
2211           gcc_assert (cfa_temp.reg
2212                       == (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)));
2213           offset = -cfa_temp.offset;
2214           cfa_temp.offset -= GET_MODE_SIZE (GET_MODE (dest));
2215           break;
2216
2217         default:
2218           gcc_unreachable ();
2219         }
2220
2221         /* Rule 17 */
2222         /* If the source operand of this MEM operation is not a
2223            register, basically the source is return address.  Here
2224            we only care how much stack grew and we don't save it.  */
2225       if (!REG_P (src))
2226         break;
2227
2228       if (REGNO (src) != STACK_POINTER_REGNUM
2229           && REGNO (src) != HARD_FRAME_POINTER_REGNUM
2230           && (unsigned) REGNO (src) == cfa.reg)
2231         {
2232           /* We're storing the current CFA reg into the stack.  */
2233
2234           if (cfa.offset == 0)
2235             {
2236               /* Rule 19 */
2237               /* If stack is aligned, putting CFA reg into stack means
2238                  we can no longer use reg + offset to represent CFA.
2239                  Here we use DW_CFA_def_cfa_expression instead.  The
2240                  result of this expression equals to the original CFA
2241                  value.  */
2242               if (fde
2243                   && fde->stack_realign
2244                   && cfa.indirect == 0
2245                   && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2246                 {
2247                   dw_cfa_location cfa_exp;
2248
2249                   gcc_assert (fde->drap_reg == cfa.reg);
2250
2251                   cfa_exp.indirect = 1;
2252                   cfa_exp.reg = HARD_FRAME_POINTER_REGNUM;
2253                   cfa_exp.base_offset = offset;
2254                   cfa_exp.offset = 0;
2255
2256                   fde->drap_reg_saved = 1;
2257
2258                   def_cfa_1 (label, &cfa_exp);
2259                   break;
2260                 }
2261
2262               /* If the source register is exactly the CFA, assume
2263                  we're saving SP like any other register; this happens
2264                  on the ARM.  */
2265               def_cfa_1 (label, &cfa);
2266               queue_reg_save (label, stack_pointer_rtx, NULL_RTX, offset);
2267               break;
2268             }
2269           else
2270             {
2271               /* Otherwise, we'll need to look in the stack to
2272                  calculate the CFA.  */
2273               rtx x = XEXP (dest, 0);
2274
2275               if (!REG_P (x))
2276                 x = XEXP (x, 0);
2277               gcc_assert (REG_P (x));
2278
2279               cfa.reg = REGNO (x);
2280               cfa.base_offset = offset;
2281               cfa.indirect = 1;
2282               def_cfa_1 (label, &cfa);
2283               break;
2284             }
2285         }
2286
2287       def_cfa_1 (label, &cfa);
2288       {
2289         span = targetm.dwarf_register_span (src);
2290
2291         if (!span)
2292           queue_reg_save (label, src, NULL_RTX, offset);
2293         else
2294           {
2295             /* We have a PARALLEL describing where the contents of SRC
2296                live.  Queue register saves for each piece of the
2297                PARALLEL.  */
2298             int par_index;
2299             int limit;
2300             HOST_WIDE_INT span_offset = offset;
2301
2302             gcc_assert (GET_CODE (span) == PARALLEL);
2303
2304             limit = XVECLEN (span, 0);
2305             for (par_index = 0; par_index < limit; par_index++)
2306               {
2307                 rtx elem = XVECEXP (span, 0, par_index);
2308
2309                 queue_reg_save (label, elem, NULL_RTX, span_offset);
2310                 span_offset += GET_MODE_SIZE (GET_MODE (elem));
2311               }
2312           }
2313       }
2314       break;
2315
2316     default:
2317       gcc_unreachable ();
2318     }
2319 }
2320
2321 /* Record call frame debugging information for INSN, which either
2322    sets SP or FP (adjusting how we calculate the frame address) or saves a
2323    register to the stack.  If INSN is NULL_RTX, initialize our state.
2324
2325    If AFTER_P is false, we're being called before the insn is emitted,
2326    otherwise after.  Call instructions get invoked twice.  */
2327
2328 void
2329 dwarf2out_frame_debug (rtx insn, bool after_p)
2330 {
2331   const char *label;
2332   rtx src;
2333
2334   if (insn == NULL_RTX)
2335     {
2336       size_t i;
2337
2338       /* Flush any queued register saves.  */
2339       flush_queued_reg_saves ();
2340
2341       /* Set up state for generating call frame debug info.  */
2342       lookup_cfa (&cfa);
2343       gcc_assert (cfa.reg
2344                   == (unsigned long)DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM));
2345
2346       cfa.reg = STACK_POINTER_REGNUM;
2347       cfa_store = cfa;
2348       cfa_temp.reg = -1;
2349       cfa_temp.offset = 0;
2350
2351       for (i = 0; i < num_regs_saved_in_regs; i++)
2352         {
2353           regs_saved_in_regs[i].orig_reg = NULL_RTX;
2354           regs_saved_in_regs[i].saved_in_reg = NULL_RTX;
2355         }
2356       num_regs_saved_in_regs = 0;
2357
2358       if (barrier_args_size)
2359         {
2360           XDELETEVEC (barrier_args_size);
2361           barrier_args_size = NULL;
2362         }
2363       return;
2364     }
2365
2366   if (!NONJUMP_INSN_P (insn) || clobbers_queued_reg_save (insn))
2367     flush_queued_reg_saves ();
2368
2369   if (! RTX_FRAME_RELATED_P (insn))
2370     {
2371       if (!ACCUMULATE_OUTGOING_ARGS)
2372         dwarf2out_stack_adjust (insn, after_p);
2373       return;
2374     }
2375
2376   label = dwarf2out_cfi_label ();
2377   src = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
2378   if (src)
2379     insn = XEXP (src, 0);
2380   else
2381     insn = PATTERN (insn);
2382
2383   dwarf2out_frame_debug_expr (insn, label);
2384 }
2385
2386 #endif
2387
2388 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used.  */
2389 static enum dw_cfi_oprnd_type dw_cfi_oprnd1_desc
2390  (enum dwarf_call_frame_info cfi);
2391
2392 static enum dw_cfi_oprnd_type
2393 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
2394 {
2395   switch (cfi)
2396     {
2397     case DW_CFA_nop:
2398     case DW_CFA_GNU_window_save:
2399       return dw_cfi_oprnd_unused;
2400
2401     case DW_CFA_set_loc:
2402     case DW_CFA_advance_loc1:
2403     case DW_CFA_advance_loc2:
2404     case DW_CFA_advance_loc4:
2405     case DW_CFA_MIPS_advance_loc8:
2406       return dw_cfi_oprnd_addr;
2407
2408     case DW_CFA_offset:
2409     case DW_CFA_offset_extended:
2410     case DW_CFA_def_cfa:
2411     case DW_CFA_offset_extended_sf:
2412     case DW_CFA_def_cfa_sf:
2413     case DW_CFA_restore_extended:
2414     case DW_CFA_undefined:
2415     case DW_CFA_same_value:
2416     case DW_CFA_def_cfa_register:
2417     case DW_CFA_register:
2418       return dw_cfi_oprnd_reg_num;
2419
2420     case DW_CFA_def_cfa_offset:
2421     case DW_CFA_GNU_args_size:
2422     case DW_CFA_def_cfa_offset_sf:
2423       return dw_cfi_oprnd_offset;
2424
2425     case DW_CFA_def_cfa_expression:
2426     case DW_CFA_expression:
2427       return dw_cfi_oprnd_loc;
2428
2429     default:
2430       gcc_unreachable ();
2431     }
2432 }
2433
2434 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used.  */
2435 static enum dw_cfi_oprnd_type dw_cfi_oprnd2_desc
2436  (enum dwarf_call_frame_info cfi);
2437
2438 static enum dw_cfi_oprnd_type
2439 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
2440 {
2441   switch (cfi)
2442     {
2443     case DW_CFA_def_cfa:
2444     case DW_CFA_def_cfa_sf:
2445     case DW_CFA_offset:
2446     case DW_CFA_offset_extended_sf:
2447     case DW_CFA_offset_extended:
2448       return dw_cfi_oprnd_offset;
2449
2450     case DW_CFA_register:
2451       return dw_cfi_oprnd_reg_num;
2452
2453     default:
2454       return dw_cfi_oprnd_unused;
2455     }
2456 }
2457
2458 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
2459
2460 /* Switch to eh_frame_section.  If we don't have an eh_frame_section,
2461    switch to the data section instead, and write out a synthetic label
2462    for collect2.  */
2463
2464 static void
2465 switch_to_eh_frame_section (void)
2466 {
2467   tree label;
2468
2469 #ifdef EH_FRAME_SECTION_NAME
2470   if (eh_frame_section == 0)
2471     {
2472       int flags;
2473
2474       if (EH_TABLES_CAN_BE_READ_ONLY)
2475         {
2476           int fde_encoding;
2477           int per_encoding;
2478           int lsda_encoding;
2479
2480           fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
2481                                                        /*global=*/0);
2482           per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
2483                                                        /*global=*/1);
2484           lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
2485                                                         /*global=*/0);
2486           flags = ((! flag_pic
2487                     || ((fde_encoding & 0x70) != DW_EH_PE_absptr
2488                         && (fde_encoding & 0x70) != DW_EH_PE_aligned
2489                         && (per_encoding & 0x70) != DW_EH_PE_absptr
2490                         && (per_encoding & 0x70) != DW_EH_PE_aligned
2491                         && (lsda_encoding & 0x70) != DW_EH_PE_absptr
2492                         && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
2493                    ? 0 : SECTION_WRITE);
2494         }
2495       else
2496         flags = SECTION_WRITE;
2497       eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
2498     }
2499 #endif
2500
2501   if (eh_frame_section)
2502     switch_to_section (eh_frame_section);
2503   else
2504     {
2505       /* We have no special eh_frame section.  Put the information in
2506          the data section and emit special labels to guide collect2.  */
2507       switch_to_section (data_section);
2508       label = get_file_function_name ("F");
2509       ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
2510       targetm.asm_out.globalize_label (asm_out_file,
2511                                        IDENTIFIER_POINTER (label));
2512       ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
2513     }
2514 }
2515
2516 /* Divide OFF by DWARF_CIE_DATA_ALIGNMENT, asserting no remainder.  */
2517
2518 static HOST_WIDE_INT
2519 div_data_align (HOST_WIDE_INT off)
2520 {
2521   HOST_WIDE_INT r = off / DWARF_CIE_DATA_ALIGNMENT;
2522   gcc_assert (r * DWARF_CIE_DATA_ALIGNMENT == off);
2523   return r;
2524 }
2525
2526 /* Output a Call Frame Information opcode and its operand(s).  */
2527
2528 static void
2529 output_cfi (dw_cfi_ref cfi, dw_fde_ref fde, int for_eh)
2530 {
2531   unsigned long r;
2532   HOST_WIDE_INT off;
2533
2534   if (cfi->dw_cfi_opc == DW_CFA_advance_loc)
2535     dw2_asm_output_data (1, (cfi->dw_cfi_opc
2536                              | (cfi->dw_cfi_oprnd1.dw_cfi_offset & 0x3f)),
2537                          "DW_CFA_advance_loc " HOST_WIDE_INT_PRINT_HEX,
2538                          ((unsigned HOST_WIDE_INT)
2539                           cfi->dw_cfi_oprnd1.dw_cfi_offset));
2540   else if (cfi->dw_cfi_opc == DW_CFA_offset)
2541     {
2542       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2543       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
2544                            "DW_CFA_offset, column 0x%lx", r);
2545       off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
2546       dw2_asm_output_data_uleb128 (off, NULL);
2547     }
2548   else if (cfi->dw_cfi_opc == DW_CFA_restore)
2549     {
2550       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2551       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
2552                            "DW_CFA_restore, column 0x%lx", r);
2553     }
2554   else
2555     {
2556       dw2_asm_output_data (1, cfi->dw_cfi_opc,
2557                            "%s", dwarf_cfi_name (cfi->dw_cfi_opc));
2558
2559       switch (cfi->dw_cfi_opc)
2560         {
2561         case DW_CFA_set_loc:
2562           if (for_eh)
2563             dw2_asm_output_encoded_addr_rtx (
2564                 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0),
2565                 gen_rtx_SYMBOL_REF (Pmode, cfi->dw_cfi_oprnd1.dw_cfi_addr),
2566                 false, NULL);
2567           else
2568             dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2569                                  cfi->dw_cfi_oprnd1.dw_cfi_addr, NULL);
2570           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2571           break;
2572
2573         case DW_CFA_advance_loc1:
2574           dw2_asm_output_delta (1, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2575                                 fde->dw_fde_current_label, NULL);
2576           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2577           break;
2578
2579         case DW_CFA_advance_loc2:
2580           dw2_asm_output_delta (2, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2581                                 fde->dw_fde_current_label, NULL);
2582           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2583           break;
2584
2585         case DW_CFA_advance_loc4:
2586           dw2_asm_output_delta (4, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2587                                 fde->dw_fde_current_label, NULL);
2588           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2589           break;
2590
2591         case DW_CFA_MIPS_advance_loc8:
2592           dw2_asm_output_delta (8, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2593                                 fde->dw_fde_current_label, NULL);
2594           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2595           break;
2596
2597         case DW_CFA_offset_extended:
2598           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2599           dw2_asm_output_data_uleb128 (r, NULL);
2600           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
2601           dw2_asm_output_data_uleb128 (off, NULL);
2602           break;
2603
2604         case DW_CFA_def_cfa:
2605           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2606           dw2_asm_output_data_uleb128 (r, NULL);
2607           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
2608           break;
2609
2610         case DW_CFA_offset_extended_sf:
2611           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2612           dw2_asm_output_data_uleb128 (r, NULL);
2613           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
2614           dw2_asm_output_data_sleb128 (off, NULL);
2615           break;
2616
2617         case DW_CFA_def_cfa_sf:
2618           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2619           dw2_asm_output_data_uleb128 (r, NULL);
2620           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
2621           dw2_asm_output_data_sleb128 (off, NULL);
2622           break;
2623
2624         case DW_CFA_restore_extended:
2625         case DW_CFA_undefined:
2626         case DW_CFA_same_value:
2627         case DW_CFA_def_cfa_register:
2628           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2629           dw2_asm_output_data_uleb128 (r, NULL);
2630           break;
2631
2632         case DW_CFA_register:
2633           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2634           dw2_asm_output_data_uleb128 (r, NULL);
2635           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, for_eh);
2636           dw2_asm_output_data_uleb128 (r, NULL);
2637           break;
2638
2639         case DW_CFA_def_cfa_offset:
2640         case DW_CFA_GNU_args_size:
2641           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
2642           break;
2643
2644         case DW_CFA_def_cfa_offset_sf:
2645           off = div_data_align (cfi->dw_cfi_oprnd1.dw_cfi_offset);
2646           dw2_asm_output_data_sleb128 (off, NULL);
2647           break;
2648
2649         case DW_CFA_GNU_window_save:
2650           break;
2651
2652         case DW_CFA_def_cfa_expression:
2653         case DW_CFA_expression:
2654           output_cfa_loc (cfi);
2655           break;
2656
2657         case DW_CFA_GNU_negative_offset_extended:
2658           /* Obsoleted by DW_CFA_offset_extended_sf.  */
2659           gcc_unreachable ();
2660
2661         default:
2662           break;
2663         }
2664     }
2665 }
2666
2667 /* Similar, but do it via assembler directives instead.  */
2668
2669 static void
2670 output_cfi_directive (dw_cfi_ref cfi)
2671 {
2672   unsigned long r, r2;
2673
2674   switch (cfi->dw_cfi_opc)
2675     {
2676     case DW_CFA_advance_loc:
2677     case DW_CFA_advance_loc1:
2678     case DW_CFA_advance_loc2:
2679     case DW_CFA_advance_loc4:
2680     case DW_CFA_MIPS_advance_loc8:
2681     case DW_CFA_set_loc:
2682       /* Should only be created by add_fde_cfi in a code path not
2683          followed when emitting via directives.  The assembler is
2684          going to take care of this for us.  */
2685       gcc_unreachable ();
2686
2687     case DW_CFA_offset:
2688     case DW_CFA_offset_extended:
2689     case DW_CFA_offset_extended_sf:
2690       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 0);
2691       fprintf (asm_out_file, "\t.cfi_offset %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
2692                r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
2693       break;
2694
2695     case DW_CFA_restore:
2696     case DW_CFA_restore_extended:
2697       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 0);
2698       fprintf (asm_out_file, "\t.cfi_restore %lu\n", r);
2699       break;
2700
2701     case DW_CFA_undefined:
2702       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 0);
2703       fprintf (asm_out_file, "\t.cfi_undefined %lu\n", r);
2704       break;
2705
2706     case DW_CFA_same_value:
2707       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 0);
2708       fprintf (asm_out_file, "\t.cfi_same_value %lu\n", r);
2709       break;
2710
2711     case DW_CFA_def_cfa:
2712     case DW_CFA_def_cfa_sf:
2713       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 0);
2714       fprintf (asm_out_file, "\t.cfi_def_cfa %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
2715                r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
2716       break;
2717
2718     case DW_CFA_def_cfa_register:
2719       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 0);
2720       fprintf (asm_out_file, "\t.cfi_def_cfa_register %lu\n", r);
2721       break;
2722
2723     case DW_CFA_register:
2724       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 0);
2725       r2 = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, 0);
2726       fprintf (asm_out_file, "\t.cfi_register %lu, %lu\n", r, r2);
2727       break;
2728
2729     case DW_CFA_def_cfa_offset:
2730     case DW_CFA_def_cfa_offset_sf:
2731       fprintf (asm_out_file, "\t.cfi_def_cfa_offset "
2732                HOST_WIDE_INT_PRINT_DEC"\n",
2733                cfi->dw_cfi_oprnd1.dw_cfi_offset);
2734       break;
2735
2736     case DW_CFA_GNU_args_size:
2737       fprintf (asm_out_file, "\t.cfi_escape 0x%x,", DW_CFA_GNU_args_size);
2738       dw2_asm_output_data_uleb128_raw (cfi->dw_cfi_oprnd1.dw_cfi_offset);
2739       if (flag_debug_asm)
2740         fprintf (asm_out_file, "\t%s args_size "HOST_WIDE_INT_PRINT_DEC,
2741                  ASM_COMMENT_START, cfi->dw_cfi_oprnd1.dw_cfi_offset);
2742       fputc ('\n', asm_out_file);
2743       break;
2744
2745     case DW_CFA_GNU_window_save:
2746       fprintf (asm_out_file, "\t.cfi_window_save\n");
2747       break;
2748
2749     case DW_CFA_def_cfa_expression:
2750     case DW_CFA_expression:
2751       fprintf (asm_out_file, "\t.cfi_escape 0x%x,", cfi->dw_cfi_opc);
2752       output_cfa_loc_raw (cfi);
2753       fputc ('\n', asm_out_file);
2754       break;
2755
2756     default:
2757       gcc_unreachable ();
2758     }
2759 }
2760
2761 /* Output the call frame information used to record information
2762    that relates to calculating the frame pointer, and records the
2763    location of saved registers.  */
2764
2765 static void
2766 output_call_frame_info (int for_eh)
2767 {
2768   unsigned int i;
2769   dw_fde_ref fde;
2770   dw_cfi_ref cfi;
2771   char l1[20], l2[20], section_start_label[20];
2772   bool any_lsda_needed = false;
2773   char augmentation[6];
2774   int augmentation_size;
2775   int fde_encoding = DW_EH_PE_absptr;
2776   int per_encoding = DW_EH_PE_absptr;
2777   int lsda_encoding = DW_EH_PE_absptr;
2778   int return_reg;
2779
2780   /* Don't emit a CIE if there won't be any FDEs.  */
2781   if (fde_table_in_use == 0)
2782     return;
2783
2784   /* Nothing to do if the assembler's doing it all.  */
2785   if (dwarf2out_do_cfi_asm ())
2786     return;
2787
2788   /* If we make FDEs linkonce, we may have to emit an empty label for
2789      an FDE that wouldn't otherwise be emitted.  We want to avoid
2790      having an FDE kept around when the function it refers to is
2791      discarded.  Example where this matters: a primary function
2792      template in C++ requires EH information, but an explicit
2793      specialization doesn't.  */
2794   if (TARGET_USES_WEAK_UNWIND_INFO
2795       && ! flag_asynchronous_unwind_tables
2796       && flag_exceptions
2797       && for_eh)
2798     for (i = 0; i < fde_table_in_use; i++)
2799       if ((fde_table[i].nothrow || fde_table[i].all_throwers_are_sibcalls)
2800           && !fde_table[i].uses_eh_lsda
2801           && ! DECL_WEAK (fde_table[i].decl))
2802         targetm.asm_out.unwind_label (asm_out_file, fde_table[i].decl,
2803                                       for_eh, /* empty */ 1);
2804
2805   /* If we don't have any functions we'll want to unwind out of, don't
2806      emit any EH unwind information.  Note that if exceptions aren't
2807      enabled, we won't have collected nothrow information, and if we
2808      asked for asynchronous tables, we always want this info.  */
2809   if (for_eh)
2810     {
2811       bool any_eh_needed = !flag_exceptions || flag_asynchronous_unwind_tables;
2812
2813       for (i = 0; i < fde_table_in_use; i++)
2814         if (fde_table[i].uses_eh_lsda)
2815           any_eh_needed = any_lsda_needed = true;
2816         else if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
2817           any_eh_needed = true;
2818         else if (! fde_table[i].nothrow
2819                  && ! fde_table[i].all_throwers_are_sibcalls)
2820           any_eh_needed = true;
2821
2822       if (! any_eh_needed)
2823         return;
2824     }
2825
2826   /* We're going to be generating comments, so turn on app.  */
2827   if (flag_debug_asm)
2828     app_enable ();
2829
2830   if (for_eh)
2831     switch_to_eh_frame_section ();
2832   else
2833     {
2834       if (!debug_frame_section)
2835         debug_frame_section = get_section (DEBUG_FRAME_SECTION,
2836                                            SECTION_DEBUG, NULL);
2837       switch_to_section (debug_frame_section);
2838     }
2839
2840   ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
2841   ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
2842
2843   /* Output the CIE.  */
2844   ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
2845   ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
2846   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
2847     dw2_asm_output_data (4, 0xffffffff,
2848       "Initial length escape value indicating 64-bit DWARF extension");
2849   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
2850                         "Length of Common Information Entry");
2851   ASM_OUTPUT_LABEL (asm_out_file, l1);
2852
2853   /* Now that the CIE pointer is PC-relative for EH,
2854      use 0 to identify the CIE.  */
2855   dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
2856                        (for_eh ? 0 : DWARF_CIE_ID),
2857                        "CIE Identifier Tag");
2858
2859   dw2_asm_output_data (1, DW_CIE_VERSION, "CIE Version");
2860
2861   augmentation[0] = 0;
2862   augmentation_size = 0;
2863   if (for_eh)
2864     {
2865       char *p;
2866
2867       /* Augmentation:
2868          z      Indicates that a uleb128 is present to size the
2869                 augmentation section.
2870          L      Indicates the encoding (and thus presence) of
2871                 an LSDA pointer in the FDE augmentation.
2872          R      Indicates a non-default pointer encoding for
2873                 FDE code pointers.
2874          P      Indicates the presence of an encoding + language
2875                 personality routine in the CIE augmentation.  */
2876
2877       fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
2878       per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
2879       lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
2880
2881       p = augmentation + 1;
2882       if (eh_personality_libfunc)
2883         {
2884           *p++ = 'P';
2885           augmentation_size += 1 + size_of_encoded_value (per_encoding);
2886           assemble_external_libcall (eh_personality_libfunc);
2887         }
2888       if (any_lsda_needed)
2889         {
2890           *p++ = 'L';
2891           augmentation_size += 1;
2892         }
2893       if (fde_encoding != DW_EH_PE_absptr)
2894         {
2895           *p++ = 'R';
2896           augmentation_size += 1;
2897         }
2898       if (p > augmentation + 1)
2899         {
2900           augmentation[0] = 'z';
2901           *p = '\0';
2902         }
2903
2904       /* Ug.  Some platforms can't do unaligned dynamic relocations at all.  */
2905       if (eh_personality_libfunc && per_encoding == DW_EH_PE_aligned)
2906         {
2907           int offset = (  4             /* Length */
2908                         + 4             /* CIE Id */
2909                         + 1             /* CIE version */
2910                         + strlen (augmentation) + 1     /* Augmentation */
2911                         + size_of_uleb128 (1)           /* Code alignment */
2912                         + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
2913                         + 1             /* RA column */
2914                         + 1             /* Augmentation size */
2915                         + 1             /* Personality encoding */ );
2916           int pad = -offset & (PTR_SIZE - 1);
2917
2918           augmentation_size += pad;
2919
2920           /* Augmentations should be small, so there's scarce need to
2921              iterate for a solution.  Die if we exceed one uleb128 byte.  */
2922           gcc_assert (size_of_uleb128 (augmentation_size) == 1);
2923         }
2924     }
2925
2926   dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
2927   dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
2928   dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
2929                                "CIE Data Alignment Factor");
2930
2931   return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
2932   if (DW_CIE_VERSION == 1)
2933     dw2_asm_output_data (1, return_reg, "CIE RA Column");
2934   else
2935     dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
2936
2937   if (augmentation[0])
2938     {
2939       dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
2940       if (eh_personality_libfunc)
2941         {
2942           dw2_asm_output_data (1, per_encoding, "Personality (%s)",
2943                                eh_data_format_name (per_encoding));
2944           dw2_asm_output_encoded_addr_rtx (per_encoding,
2945                                            eh_personality_libfunc,
2946                                            true, NULL);
2947         }
2948
2949       if (any_lsda_needed)
2950         dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
2951                              eh_data_format_name (lsda_encoding));
2952
2953       if (fde_encoding != DW_EH_PE_absptr)
2954         dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
2955                              eh_data_format_name (fde_encoding));
2956     }
2957
2958   for (cfi = cie_cfi_head; cfi != NULL; cfi = cfi->dw_cfi_next)
2959     output_cfi (cfi, NULL, for_eh);
2960
2961   /* Pad the CIE out to an address sized boundary.  */
2962   ASM_OUTPUT_ALIGN (asm_out_file,
2963                     floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
2964   ASM_OUTPUT_LABEL (asm_out_file, l2);
2965
2966   /* Loop through all of the FDE's.  */
2967   for (i = 0; i < fde_table_in_use; i++)
2968     {
2969       fde = &fde_table[i];
2970
2971       /* Don't emit EH unwind info for leaf functions that don't need it.  */
2972       if (for_eh && !flag_asynchronous_unwind_tables && flag_exceptions
2973           && (fde->nothrow || fde->all_throwers_are_sibcalls)
2974           && ! (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
2975           && !fde->uses_eh_lsda)
2976         continue;
2977
2978       targetm.asm_out.unwind_label (asm_out_file, fde->decl, for_eh, /* empty */ 0);
2979       targetm.asm_out.internal_label (asm_out_file, FDE_LABEL, for_eh + i * 2);
2980       ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + i * 2);
2981       ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + i * 2);
2982       if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
2983         dw2_asm_output_data (4, 0xffffffff,
2984                              "Initial length escape value indicating 64-bit DWARF extension");
2985       dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
2986                             "FDE Length");
2987       ASM_OUTPUT_LABEL (asm_out_file, l1);
2988
2989       if (for_eh)
2990         dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
2991       else
2992         dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
2993                                debug_frame_section, "FDE CIE offset");
2994
2995       if (for_eh)
2996         {
2997           if (fde->dw_fde_switched_sections)
2998             {
2999               rtx sym_ref2 = gen_rtx_SYMBOL_REF (Pmode,
3000                                       fde->dw_fde_unlikely_section_label);
3001               rtx sym_ref3= gen_rtx_SYMBOL_REF (Pmode,
3002                                       fde->dw_fde_hot_section_label);
3003               SYMBOL_REF_FLAGS (sym_ref2) |= SYMBOL_FLAG_LOCAL;
3004               SYMBOL_REF_FLAGS (sym_ref3) |= SYMBOL_FLAG_LOCAL;
3005               dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref3, false,
3006                                                "FDE initial location");
3007               dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
3008                                     fde->dw_fde_hot_section_end_label,
3009                                     fde->dw_fde_hot_section_label,
3010                                     "FDE address range");
3011               dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref2, false,
3012                                                "FDE initial location");
3013               dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
3014                                     fde->dw_fde_unlikely_section_end_label,
3015                                     fde->dw_fde_unlikely_section_label,
3016                                     "FDE address range");
3017             }
3018           else
3019             {
3020               rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, fde->dw_fde_begin);
3021               SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
3022               dw2_asm_output_encoded_addr_rtx (fde_encoding,
3023                                                sym_ref,
3024                                                false,
3025                                                "FDE initial location");
3026               dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
3027                                     fde->dw_fde_end, fde->dw_fde_begin,
3028                                     "FDE address range");
3029             }
3030         }
3031       else
3032         {
3033           if (fde->dw_fde_switched_sections)
3034             {
3035               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
3036                                    fde->dw_fde_hot_section_label,
3037                                    "FDE initial location");
3038               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
3039                                     fde->dw_fde_hot_section_end_label,
3040                                     fde->dw_fde_hot_section_label,
3041                                     "FDE address range");
3042               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
3043                                    fde->dw_fde_unlikely_section_label,
3044                                    "FDE initial location");
3045               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
3046                                     fde->dw_fde_unlikely_section_end_label,
3047                                     fde->dw_fde_unlikely_section_label,
3048                                     "FDE address range");
3049             }
3050           else
3051             {
3052               dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
3053                                    "FDE initial location");
3054               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
3055                                     fde->dw_fde_end, fde->dw_fde_begin,
3056                                     "FDE address range");
3057             }
3058         }
3059
3060       if (augmentation[0])
3061         {
3062           if (any_lsda_needed)
3063             {
3064               int size = size_of_encoded_value (lsda_encoding);
3065
3066               if (lsda_encoding == DW_EH_PE_aligned)
3067                 {
3068                   int offset = (  4             /* Length */
3069                                 + 4             /* CIE offset */
3070                                 + 2 * size_of_encoded_value (fde_encoding)
3071                                 + 1             /* Augmentation size */ );
3072                   int pad = -offset & (PTR_SIZE - 1);
3073
3074                   size += pad;
3075                   gcc_assert (size_of_uleb128 (size) == 1);
3076                 }
3077
3078               dw2_asm_output_data_uleb128 (size, "Augmentation size");
3079
3080               if (fde->uses_eh_lsda)
3081                 {
3082                   ASM_GENERATE_INTERNAL_LABEL (l1, "LLSDA",
3083                                                fde->funcdef_number);
3084                   dw2_asm_output_encoded_addr_rtx (
3085                         lsda_encoding, gen_rtx_SYMBOL_REF (Pmode, l1),
3086                         false, "Language Specific Data Area");
3087                 }
3088               else
3089                 {
3090                   if (lsda_encoding == DW_EH_PE_aligned)
3091                     ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
3092                   dw2_asm_output_data
3093                     (size_of_encoded_value (lsda_encoding), 0,
3094                      "Language Specific Data Area (none)");
3095                 }
3096             }
3097           else
3098             dw2_asm_output_data_uleb128 (0, "Augmentation size");
3099         }
3100
3101       /* Loop through the Call Frame Instructions associated with
3102          this FDE.  */
3103       fde->dw_fde_current_label = fde->dw_fde_begin;
3104       for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
3105         output_cfi (cfi, fde, for_eh);
3106
3107       /* Pad the FDE out to an address sized boundary.  */
3108       ASM_OUTPUT_ALIGN (asm_out_file,
3109                         floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
3110       ASM_OUTPUT_LABEL (asm_out_file, l2);
3111     }
3112
3113   if (for_eh && targetm.terminate_dw2_eh_frame_info)
3114     dw2_asm_output_data (4, 0, "End of Table");
3115 #ifdef MIPS_DEBUGGING_INFO
3116   /* Work around Irix 6 assembler bug whereby labels at the end of a section
3117      get a value of 0.  Putting .align 0 after the label fixes it.  */
3118   ASM_OUTPUT_ALIGN (asm_out_file, 0);
3119 #endif
3120
3121   /* Turn off app to make assembly quicker.  */
3122   if (flag_debug_asm)
3123     app_disable ();
3124 }
3125
3126 /* Output a marker (i.e. a label) for the beginning of a function, before
3127    the prologue.  */
3128
3129 void
3130 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
3131                           const char *file ATTRIBUTE_UNUSED)
3132 {
3133   char label[MAX_ARTIFICIAL_LABEL_BYTES];
3134   char * dup_label;
3135   dw_fde_ref fde;
3136
3137   current_function_func_begin_label = NULL;
3138
3139 #ifdef TARGET_UNWIND_INFO
3140   /* ??? current_function_func_begin_label is also used by except.c
3141      for call-site information.  We must emit this label if it might
3142      be used.  */
3143   if ((! flag_exceptions || USING_SJLJ_EXCEPTIONS)
3144       && ! dwarf2out_do_frame ())
3145     return;
3146 #else
3147   if (! dwarf2out_do_frame ())
3148     return;
3149 #endif
3150
3151   switch_to_section (function_section (current_function_decl));
3152   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
3153                                current_function_funcdef_no);
3154   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
3155                           current_function_funcdef_no);
3156   dup_label = xstrdup (label);
3157   current_function_func_begin_label = dup_label;
3158
3159 #ifdef TARGET_UNWIND_INFO
3160   /* We can elide the fde allocation if we're not emitting debug info.  */
3161   if (! dwarf2out_do_frame ())
3162     return;
3163 #endif
3164
3165   /* Expand the fde table if necessary.  */
3166   if (fde_table_in_use == fde_table_allocated)
3167     {
3168       fde_table_allocated += FDE_TABLE_INCREMENT;
3169       fde_table = GGC_RESIZEVEC (dw_fde_node, fde_table, fde_table_allocated);
3170       memset (fde_table + fde_table_in_use, 0,
3171               FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
3172     }
3173
3174   /* Record the FDE associated with this function.  */
3175   current_funcdef_fde = fde_table_in_use;
3176
3177   /* Add the new FDE at the end of the fde_table.  */
3178   fde = &fde_table[fde_table_in_use++];
3179   fde->decl = current_function_decl;
3180   fde->dw_fde_begin = dup_label;
3181   fde->dw_fde_current_label = dup_label;
3182   fde->dw_fde_hot_section_label = NULL;
3183   fde->dw_fde_hot_section_end_label = NULL;
3184   fde->dw_fde_unlikely_section_label = NULL;
3185   fde->dw_fde_unlikely_section_end_label = NULL;
3186   fde->dw_fde_switched_sections = false;
3187   fde->dw_fde_end = NULL;
3188   fde->dw_fde_cfi = NULL;
3189   fde->funcdef_number = current_function_funcdef_no;
3190   fde->nothrow = TREE_NOTHROW (current_function_decl);
3191   fde->uses_eh_lsda = crtl->uses_eh_lsda;
3192   fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
3193   fde->drap_reg = INVALID_REGNUM;
3194   fde->vdrap_reg = INVALID_REGNUM;
3195
3196   args_size = old_args_size = 0;
3197
3198   /* We only want to output line number information for the genuine dwarf2
3199      prologue case, not the eh frame case.  */
3200 #ifdef DWARF2_DEBUGGING_INFO
3201   if (file)
3202     dwarf2out_source_line (line, file);
3203 #endif
3204
3205   if (dwarf2out_do_cfi_asm ())
3206     {
3207       int enc;
3208       rtx ref;
3209
3210       fprintf (asm_out_file, "\t.cfi_startproc\n");
3211
3212       if (eh_personality_libfunc)
3213         {
3214           enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1); 
3215           ref = eh_personality_libfunc;
3216
3217           /* ??? The GAS support isn't entirely consistent.  We have to
3218              handle indirect support ourselves, but PC-relative is done
3219              in the assembler.  Further, the assembler can't handle any
3220              of the weirder relocation types.  */
3221           if (enc & DW_EH_PE_indirect)
3222             ref = dw2_force_const_mem (ref, true);
3223
3224           fprintf (asm_out_file, "\t.cfi_personality 0x%x,", enc);
3225           output_addr_const (asm_out_file, ref);
3226           fputc ('\n', asm_out_file);
3227         }
3228
3229       if (crtl->uses_eh_lsda)
3230         {
3231           char lab[20];
3232
3233           enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
3234           ASM_GENERATE_INTERNAL_LABEL (lab, "LLSDA",
3235                                        current_function_funcdef_no);
3236           ref = gen_rtx_SYMBOL_REF (Pmode, lab);
3237           SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
3238
3239           if (enc & DW_EH_PE_indirect)
3240             ref = dw2_force_const_mem (ref, true);
3241
3242           fprintf (asm_out_file, "\t.cfi_lsda 0x%x,", enc);
3243           output_addr_const (asm_out_file, ref);
3244           fputc ('\n', asm_out_file);
3245         }
3246     }
3247 }
3248
3249 /* Output a marker (i.e. a label) for the absolute end of the generated code
3250    for a function definition.  This gets called *after* the epilogue code has
3251    been generated.  */
3252
3253 void
3254 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
3255                         const char *file ATTRIBUTE_UNUSED)
3256 {
3257   dw_fde_ref fde;
3258   char label[MAX_ARTIFICIAL_LABEL_BYTES];
3259
3260   if (dwarf2out_do_cfi_asm ())
3261     fprintf (asm_out_file, "\t.cfi_endproc\n");
3262
3263   /* Output a label to mark the endpoint of the code generated for this
3264      function.  */
3265   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
3266                                current_function_funcdef_no);
3267   ASM_OUTPUT_LABEL (asm_out_file, label);
3268   fde = current_fde ();
3269   gcc_assert (fde != NULL);
3270   fde->dw_fde_end = xstrdup (label);
3271 }
3272
3273 void
3274 dwarf2out_frame_init (void)
3275 {
3276   /* Allocate the initial hunk of the fde_table.  */
3277   fde_table = GGC_CNEWVEC (dw_fde_node, FDE_TABLE_INCREMENT);
3278   fde_table_allocated = FDE_TABLE_INCREMENT;
3279   fde_table_in_use = 0;
3280
3281   /* Generate the CFA instructions common to all FDE's.  Do it now for the
3282      sake of lookup_cfa.  */
3283
3284   /* On entry, the Canonical Frame Address is at SP.  */
3285   dwarf2out_def_cfa (NULL, STACK_POINTER_REGNUM, INCOMING_FRAME_SP_OFFSET);
3286
3287 #ifdef DWARF2_UNWIND_INFO
3288   if (DWARF2_UNWIND_INFO || DWARF2_FRAME_INFO)
3289     initial_return_save (INCOMING_RETURN_ADDR_RTX);
3290 #endif
3291 }
3292
3293 void
3294 dwarf2out_frame_finish (void)
3295 {
3296   /* Output call frame information.  */
3297   if (DWARF2_FRAME_INFO)
3298     output_call_frame_info (0);
3299
3300 #ifndef TARGET_UNWIND_INFO
3301   /* Output another copy for the unwinder.  */
3302   if (! USING_SJLJ_EXCEPTIONS && (flag_unwind_tables || flag_exceptions))
3303     output_call_frame_info (1);
3304 #endif
3305 }
3306
3307 /* Note that the current function section is being used for code.  */
3308
3309 static void
3310 dwarf2out_note_section_used (void)
3311 {
3312   section *sec = current_function_section ();
3313   if (sec == text_section)
3314     text_section_used = true;
3315   else if (sec == cold_text_section)
3316     cold_text_section_used = true;
3317 }
3318
3319 void
3320 dwarf2out_switch_text_section (void)
3321 {
3322   dw_fde_ref fde = current_fde ();
3323
3324   gcc_assert (cfun && fde);
3325
3326   fde->dw_fde_switched_sections = true;
3327   fde->dw_fde_hot_section_label = crtl->subsections.hot_section_label;
3328   fde->dw_fde_hot_section_end_label = crtl->subsections.hot_section_end_label;
3329   fde->dw_fde_unlikely_section_label = crtl->subsections.cold_section_label;
3330   fde->dw_fde_unlikely_section_end_label = crtl->subsections.cold_section_end_label;
3331   have_multiple_function_sections = true;
3332
3333   /* Reset the current label on switching text sections, so that we
3334      don't attempt to advance_loc4 between labels in different sections.  */
3335   fde->dw_fde_current_label = NULL;
3336
3337   /* There is no need to mark used sections when not debugging.  */
3338   if (cold_text_section != NULL)
3339     dwarf2out_note_section_used ();
3340 }
3341 #endif
3342 \f
3343 /* And now, the subset of the debugging information support code necessary
3344    for emitting location expressions.  */
3345
3346 /* Data about a single source file.  */
3347 struct dwarf_file_data GTY(())
3348 {
3349   const char * filename;
3350   int emitted_number;
3351 };
3352
3353 /* We need some way to distinguish DW_OP_addr with a direct symbol
3354    relocation from DW_OP_addr with a dtp-relative symbol relocation.  */
3355 #define INTERNAL_DW_OP_tls_addr         (0x100 + DW_OP_addr)
3356
3357
3358 typedef struct dw_val_struct *dw_val_ref;
3359 typedef struct die_struct *dw_die_ref;
3360 typedef const struct die_struct *const_dw_die_ref;
3361 typedef struct dw_loc_descr_struct *dw_loc_descr_ref;
3362 typedef struct dw_loc_list_struct *dw_loc_list_ref;
3363
3364 /* Each DIE may have a series of attribute/value pairs.  Values
3365    can take on several forms.  The forms that are used in this
3366    implementation are listed below.  */
3367
3368 enum dw_val_class
3369 {
3370   dw_val_class_addr,
3371   dw_val_class_offset,
3372   dw_val_class_loc,
3373   dw_val_class_loc_list,
3374   dw_val_class_range_list,
3375   dw_val_class_const,
3376   dw_val_class_unsigned_const,
3377   dw_val_class_long_long,
3378   dw_val_class_vec,
3379   dw_val_class_flag,
3380   dw_val_class_die_ref,
3381   dw_val_class_fde_ref,
3382   dw_val_class_lbl_id,
3383   dw_val_class_lineptr,
3384   dw_val_class_str,
3385   dw_val_class_macptr,
3386   dw_val_class_file
3387 };
3388
3389 /* Describe a double word constant value.  */
3390 /* ??? Every instance of long_long in the code really means CONST_DOUBLE.  */
3391
3392 typedef struct dw_long_long_struct GTY(())
3393 {
3394   unsigned long hi;
3395   unsigned long low;
3396 }
3397 dw_long_long_const;
3398
3399 /* Describe a floating point constant value, or a vector constant value.  */
3400
3401 typedef struct dw_vec_struct GTY(())
3402 {
3403   unsigned char * GTY((length ("%h.length"))) array;
3404   unsigned length;
3405   unsigned elt_size;
3406 }
3407 dw_vec_const;
3408
3409 /* The dw_val_node describes an attribute's value, as it is
3410    represented internally.  */
3411
3412 typedef struct dw_val_struct GTY(())
3413 {
3414   enum dw_val_class val_class;
3415   union dw_val_struct_union
3416     {
3417       rtx GTY ((tag ("dw_val_class_addr"))) val_addr;
3418       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_offset"))) val_offset;
3419       dw_loc_list_ref GTY ((tag ("dw_val_class_loc_list"))) val_loc_list;
3420       dw_loc_descr_ref GTY ((tag ("dw_val_class_loc"))) val_loc;
3421       HOST_WIDE_INT GTY ((default)) val_int;
3422       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_unsigned_const"))) val_unsigned;
3423       dw_long_long_const GTY ((tag ("dw_val_class_long_long"))) val_long_long;
3424       dw_vec_const GTY ((tag ("dw_val_class_vec"))) val_vec;
3425       struct dw_val_die_union
3426         {
3427           dw_die_ref die;
3428           int external;
3429         } GTY ((tag ("dw_val_class_die_ref"))) val_die_ref;
3430       unsigned GTY ((tag ("dw_val_class_fde_ref"))) val_fde_index;
3431       struct indirect_string_node * GTY ((tag ("dw_val_class_str"))) val_str;
3432       char * GTY ((tag ("dw_val_class_lbl_id"))) val_lbl_id;
3433       unsigned char GTY ((tag ("dw_val_class_flag"))) val_flag;
3434       struct dwarf_file_data * GTY ((tag ("dw_val_class_file"))) val_file;
3435     }
3436   GTY ((desc ("%1.val_class"))) v;
3437 }
3438 dw_val_node;
3439
3440 /* Locations in memory are described using a sequence of stack machine
3441    operations.  */
3442
3443 typedef struct dw_loc_descr_struct GTY(())
3444 {
3445   dw_loc_descr_ref dw_loc_next;
3446   enum dwarf_location_atom dw_loc_opc;
3447   dw_val_node dw_loc_oprnd1;
3448   dw_val_node dw_loc_oprnd2;
3449   int dw_loc_addr;
3450 }
3451 dw_loc_descr_node;
3452
3453 /* Location lists are ranges + location descriptions for that range,
3454    so you can track variables that are in different places over
3455    their entire life.  */
3456 typedef struct dw_loc_list_struct GTY(())
3457 {
3458   dw_loc_list_ref dw_loc_next;
3459   const char *begin; /* Label for begin address of range */
3460   const char *end;  /* Label for end address of range */
3461   char *ll_symbol; /* Label for beginning of location list.
3462                       Only on head of list */
3463   const char *section; /* Section this loclist is relative to */
3464   dw_loc_descr_ref expr;
3465 } dw_loc_list_node;
3466
3467 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
3468
3469 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
3470
3471 /* Convert a DWARF stack opcode into its string name.  */
3472
3473 static const char *
3474 dwarf_stack_op_name (unsigned int op)
3475 {
3476   switch (op)
3477     {
3478     case DW_OP_addr:
3479     case INTERNAL_DW_OP_tls_addr:
3480       return "DW_OP_addr";
3481     case DW_OP_deref:
3482       return "DW_OP_deref";
3483     case DW_OP_const1u:
3484       return "DW_OP_const1u";
3485     case DW_OP_const1s:
3486       return "DW_OP_const1s";
3487     case DW_OP_const2u:
3488       return "DW_OP_const2u";
3489     case DW_OP_const2s:
3490       return "DW_OP_const2s";
3491     case DW_OP_const4u:
3492       return "DW_OP_const4u";
3493     case DW_OP_const4s:
3494       return "DW_OP_const4s";
3495     case DW_OP_const8u:
3496       return "DW_OP_const8u";
3497     case DW_OP_const8s:
3498       return "DW_OP_const8s";
3499     case DW_OP_constu:
3500       return "DW_OP_constu";
3501     case DW_OP_consts:
3502       return "DW_OP_consts";
3503     case DW_OP_dup:
3504       return "DW_OP_dup";
3505     case DW_OP_drop:
3506       return "DW_OP_drop";
3507     case DW_OP_over:
3508       return "DW_OP_over";
3509     case DW_OP_pick:
3510       return "DW_OP_pick";
3511     case DW_OP_swap:
3512       return "DW_OP_swap";
3513     case DW_OP_rot:
3514       return "DW_OP_rot";
3515     case DW_OP_xderef:
3516       return "DW_OP_xderef";
3517     case DW_OP_abs:
3518       return "DW_OP_abs";
3519     case DW_OP_and:
3520       return "DW_OP_and";
3521     case DW_OP_div:
3522       return "DW_OP_div";
3523     case DW_OP_minus:
3524       return "DW_OP_minus";
3525     case DW_OP_mod:
3526       return "DW_OP_mod";
3527     case DW_OP_mul:
3528       return "DW_OP_mul";
3529     case DW_OP_neg:
3530       return "DW_OP_neg";
3531     case DW_OP_not:
3532       return "DW_OP_not";
3533     case DW_OP_or:
3534       return "DW_OP_or";
3535     case DW_OP_plus:
3536       return "DW_OP_plus";
3537     case DW_OP_plus_uconst:
3538       return "DW_OP_plus_uconst";
3539     case DW_OP_shl:
3540       return "DW_OP_shl";
3541     case DW_OP_shr:
3542       return "DW_OP_shr";
3543     case DW_OP_shra:
3544       return "DW_OP_shra";
3545     case DW_OP_xor:
3546       return "DW_OP_xor";
3547     case DW_OP_bra:
3548       return "DW_OP_bra";
3549     case DW_OP_eq:
3550       return "DW_OP_eq";
3551     case DW_OP_ge:
3552       return "DW_OP_ge";
3553     case DW_OP_gt:
3554       return "DW_OP_gt";
3555     case DW_OP_le:
3556       return "DW_OP_le";
3557     case DW_OP_lt:
3558       return "DW_OP_lt";
3559     case DW_OP_ne:
3560       return "DW_OP_ne";
3561     case DW_OP_skip:
3562       return "DW_OP_skip";
3563     case DW_OP_lit0:
3564       return "DW_OP_lit0";
3565     case DW_OP_lit1:
3566       return "DW_OP_lit1";
3567     case DW_OP_lit2:
3568       return "DW_OP_lit2";
3569     case DW_OP_lit3:
3570       return "DW_OP_lit3";
3571     case DW_OP_lit4:
3572       return "DW_OP_lit4";
3573     case DW_OP_lit5:
3574       return "DW_OP_lit5";
3575     case DW_OP_lit6:
3576       return "DW_OP_lit6";
3577     case DW_OP_lit7:
3578       return "DW_OP_lit7";
3579     case DW_OP_lit8:
3580       return "DW_OP_lit8";
3581     case DW_OP_lit9:
3582       return "DW_OP_lit9";
3583     case DW_OP_lit10:
3584       return "DW_OP_lit10";
3585     case DW_OP_lit11:
3586       return "DW_OP_lit11";
3587     case DW_OP_lit12:
3588       return "DW_OP_lit12";
3589     case DW_OP_lit13:
3590       return "DW_OP_lit13";
3591     case DW_OP_lit14:
3592       return "DW_OP_lit14";
3593     case DW_OP_lit15:
3594       return "DW_OP_lit15";
3595     case DW_OP_lit16:
3596       return "DW_OP_lit16";
3597     case DW_OP_lit17:
3598       return "DW_OP_lit17";
3599     case DW_OP_lit18:
3600       return "DW_OP_lit18";
3601     case DW_OP_lit19:
3602       return "DW_OP_lit19";
3603     case DW_OP_lit20:
3604       return "DW_OP_lit20";
3605     case DW_OP_lit21:
3606       return "DW_OP_lit21";
3607     case DW_OP_lit22:
3608       return "DW_OP_lit22";
3609     case DW_OP_lit23:
3610       return "DW_OP_lit23";
3611     case DW_OP_lit24:
3612       return "DW_OP_lit24";
3613     case DW_OP_lit25:
3614       return "DW_OP_lit25";
3615     case DW_OP_lit26:
3616       return "DW_OP_lit26";
3617     case DW_OP_lit27:
3618       return "DW_OP_lit27";
3619     case DW_OP_lit28:
3620       return "DW_OP_lit28";
3621     case DW_OP_lit29:
3622       return "DW_OP_lit29";
3623     case DW_OP_lit30:
3624       return "DW_OP_lit30";
3625     case DW_OP_lit31:
3626       return "DW_OP_lit31";
3627     case DW_OP_reg0:
3628       return "DW_OP_reg0";
3629     case DW_OP_reg1:
3630       return "DW_OP_reg1";
3631     case DW_OP_reg2:
3632       return "DW_OP_reg2";
3633     case DW_OP_reg3:
3634       return "DW_OP_reg3";
3635     case DW_OP_reg4:
3636       return "DW_OP_reg4";
3637     case DW_OP_reg5:
3638       return "DW_OP_reg5";
3639     case DW_OP_reg6:
3640       return "DW_OP_reg6";
3641     case DW_OP_reg7:
3642       return "DW_OP_reg7";
3643     case DW_OP_reg8:
3644       return "DW_OP_reg8";
3645     case DW_OP_reg9:
3646       return "DW_OP_reg9";
3647     case DW_OP_reg10:
3648       return "DW_OP_reg10";
3649     case DW_OP_reg11:
3650       return "DW_OP_reg11";
3651     case DW_OP_reg12:
3652       return "DW_OP_reg12";
3653     case DW_OP_reg13:
3654       return "DW_OP_reg13";
3655     case DW_OP_reg14:
3656       return "DW_OP_reg14";
3657     case DW_OP_reg15:
3658       return "DW_OP_reg15";
3659     case DW_OP_reg16:
3660       return "DW_OP_reg16";
3661     case DW_OP_reg17:
3662       return "DW_OP_reg17";
3663     case DW_OP_reg18:
3664       return "DW_OP_reg18";
3665     case DW_OP_reg19:
3666       return "DW_OP_reg19";
3667     case DW_OP_reg20:
3668       return "DW_OP_reg20";
3669     case DW_OP_reg21:
3670       return "DW_OP_reg21";
3671     case DW_OP_reg22:
3672       return "DW_OP_reg22";
3673     case DW_OP_reg23:
3674       return "DW_OP_reg23";
3675     case DW_OP_reg24:
3676       return "DW_OP_reg24";
3677     case DW_OP_reg25:
3678       return "DW_OP_reg25";
3679     case DW_OP_reg26:
3680       return "DW_OP_reg26";
3681     case DW_OP_reg27:
3682       return "DW_OP_reg27";
3683     case DW_OP_reg28:
3684       return "DW_OP_reg28";
3685     case DW_OP_reg29:
3686       return "DW_OP_reg29";
3687     case DW_OP_reg30:
3688       return "DW_OP_reg30";
3689     case DW_OP_reg31:
3690       return "DW_OP_reg31";
3691     case DW_OP_breg0:
3692       return "DW_OP_breg0";
3693     case DW_OP_breg1:
3694       return "DW_OP_breg1";
3695     case DW_OP_breg2:
3696       return "DW_OP_breg2";
3697     case DW_OP_breg3:
3698       return "DW_OP_breg3";
3699     case DW_OP_breg4:
3700       return "DW_OP_breg4";
3701     case DW_OP_breg5:
3702       return "DW_OP_breg5";
3703     case DW_OP_breg6:
3704       return "DW_OP_breg6";
3705     case DW_OP_breg7:
3706       return "DW_OP_breg7";
3707     case DW_OP_breg8:
3708       return "DW_OP_breg8";
3709     case DW_OP_breg9:
3710       return "DW_OP_breg9";
3711     case DW_OP_breg10:
3712       return "DW_OP_breg10";
3713     case DW_OP_breg11:
3714       return "DW_OP_breg11";
3715     case DW_OP_breg12:
3716       return "DW_OP_breg12";
3717     case DW_OP_breg13:
3718       return "DW_OP_breg13";
3719     case DW_OP_breg14:
3720       return "DW_OP_breg14";
3721     case DW_OP_breg15:
3722       return "DW_OP_breg15";
3723     case DW_OP_breg16:
3724       return "DW_OP_breg16";
3725     case DW_OP_breg17:
3726       return "DW_OP_breg17";
3727     case DW_OP_breg18:
3728       return "DW_OP_breg18";
3729     case DW_OP_breg19:
3730       return "DW_OP_breg19";
3731     case DW_OP_breg20:
3732       return "DW_OP_breg20";
3733     case DW_OP_breg21:
3734       return "DW_OP_breg21";
3735     case DW_OP_breg22:
3736       return "DW_OP_breg22";
3737     case DW_OP_breg23:
3738       return "DW_OP_breg23";
3739     case DW_OP_breg24:
3740       return "DW_OP_breg24";
3741     case DW_OP_breg25:
3742       return "DW_OP_breg25";
3743     case DW_OP_breg26:
3744       return "DW_OP_breg26";
3745     case DW_OP_breg27:
3746       return "DW_OP_breg27";
3747     case DW_OP_breg28:
3748       return "DW_OP_breg28";
3749     case DW_OP_breg29:
3750       return "DW_OP_breg29";
3751     case DW_OP_breg30:
3752       return "DW_OP_breg30";
3753     case DW_OP_breg31:
3754       return "DW_OP_breg31";
3755     case DW_OP_regx:
3756       return "DW_OP_regx";
3757     case DW_OP_fbreg:
3758       return "DW_OP_fbreg";
3759     case DW_OP_bregx:
3760       return "DW_OP_bregx";
3761     case DW_OP_piece:
3762       return "DW_OP_piece";
3763     case DW_OP_deref_size:
3764       return "DW_OP_deref_size";
3765     case DW_OP_xderef_size:
3766       return "DW_OP_xderef_size";
3767     case DW_OP_nop:
3768       return "DW_OP_nop";
3769     case DW_OP_push_object_address:
3770       return "DW_OP_push_object_address";
3771     case DW_OP_call2:
3772       return "DW_OP_call2";
3773     case DW_OP_call4:
3774       return "DW_OP_call4";
3775     case DW_OP_call_ref:
3776       return "DW_OP_call_ref";
3777     case DW_OP_GNU_push_tls_address:
3778       return "DW_OP_GNU_push_tls_address";
3779     case DW_OP_GNU_uninit:
3780       return "DW_OP_GNU_uninit";
3781     default:
3782       return "OP_<unknown>";
3783     }
3784 }
3785
3786 /* Return a pointer to a newly allocated location description.  Location
3787    descriptions are simple expression terms that can be strung
3788    together to form more complicated location (address) descriptions.  */
3789
3790 static inline dw_loc_descr_ref
3791 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
3792                unsigned HOST_WIDE_INT oprnd2)
3793 {
3794   dw_loc_descr_ref descr = GGC_CNEW (dw_loc_descr_node);
3795
3796   descr->dw_loc_opc = op;
3797   descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
3798   descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
3799   descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
3800   descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
3801
3802   return descr;
3803 }
3804
3805 /* Return a pointer to a newly allocated location description for
3806    REG and OFFSET.  */
3807
3808 static inline dw_loc_descr_ref
3809 new_reg_loc_descr (unsigned int reg,  unsigned HOST_WIDE_INT offset)
3810 {
3811   if (offset)
3812     {
3813       if (reg <= 31)
3814         return new_loc_descr (DW_OP_breg0 + reg, offset, 0);
3815       else
3816         return new_loc_descr (DW_OP_bregx, reg, offset);
3817     }
3818   else if (reg <= 31)
3819     return new_loc_descr (DW_OP_reg0 + reg, 0, 0);
3820   else
3821    return new_loc_descr (DW_OP_regx, reg, 0);
3822 }
3823
3824 /* Add a location description term to a location description expression.  */
3825
3826 static inline void
3827 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
3828 {
3829   dw_loc_descr_ref *d;
3830
3831   /* Find the end of the chain.  */
3832   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
3833     ;
3834
3835   *d = descr;
3836 }
3837
3838 /* Return the size of a location descriptor.  */
3839
3840 static unsigned long
3841 size_of_loc_descr (dw_loc_descr_ref loc)
3842 {
3843   unsigned long size = 1;
3844
3845   switch (loc->dw_loc_opc)
3846     {
3847     case DW_OP_addr:
3848     case INTERNAL_DW_OP_tls_addr:
3849       size += DWARF2_ADDR_SIZE;
3850       break;
3851     case DW_OP_const1u:
3852     case DW_OP_const1s:
3853       size += 1;
3854       break;
3855     case DW_OP_const2u:
3856     case DW_OP_const2s:
3857       size += 2;
3858       break;
3859     case DW_OP_const4u:
3860     case DW_OP_const4s:
3861       size += 4;
3862       break;
3863     case DW_OP_const8u:
3864     case DW_OP_const8s:
3865       size += 8;
3866       break;
3867     case DW_OP_constu:
3868       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3869       break;
3870     case DW_OP_consts:
3871       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3872       break;
3873     case DW_OP_pick:
3874       size += 1;
3875       break;
3876     case DW_OP_plus_uconst:
3877       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3878       break;
3879     case DW_OP_skip:
3880     case DW_OP_bra:
3881       size += 2;
3882       break;
3883     case DW_OP_breg0:
3884     case DW_OP_breg1:
3885     case DW_OP_breg2:
3886     case DW_OP_breg3:
3887     case DW_OP_breg4:
3888     case DW_OP_breg5:
3889     case DW_OP_breg6:
3890     case DW_OP_breg7:
3891     case DW_OP_breg8:
3892     case DW_OP_breg9:
3893     case DW_OP_breg10:
3894     case DW_OP_breg11:
3895     case DW_OP_breg12:
3896     case DW_OP_breg13:
3897     case DW_OP_breg14:
3898     case DW_OP_breg15:
3899     case DW_OP_breg16:
3900     case DW_OP_breg17:
3901     case DW_OP_breg18:
3902     case DW_OP_breg19:
3903     case DW_OP_breg20:
3904     case DW_OP_breg21:
3905     case DW_OP_breg22:
3906     case DW_OP_breg23:
3907     case DW_OP_breg24:
3908     case DW_OP_breg25:
3909     case DW_OP_breg26:
3910     case DW_OP_breg27:
3911     case DW_OP_breg28:
3912     case DW_OP_breg29:
3913     case DW_OP_breg30:
3914     case DW_OP_breg31:
3915       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3916       break;
3917     case DW_OP_regx:
3918       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3919       break;
3920     case DW_OP_fbreg:
3921       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3922       break;
3923     case DW_OP_bregx:
3924       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3925       size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
3926       break;
3927     case DW_OP_piece:
3928       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3929       break;
3930     case DW_OP_deref_size:
3931     case DW_OP_xderef_size:
3932       size += 1;
3933       break;
3934     case DW_OP_call2:
3935       size += 2;
3936       break;
3937     case DW_OP_call4:
3938       size += 4;
3939       break;
3940     case DW_OP_call_ref:
3941       size += DWARF2_ADDR_SIZE;
3942       break;
3943     default:
3944       break;
3945     }
3946
3947   return size;
3948 }
3949
3950 /* Return the size of a series of location descriptors.  */
3951
3952 static unsigned long
3953 size_of_locs (dw_loc_descr_ref loc)
3954 {
3955   dw_loc_descr_ref l;
3956   unsigned long size;
3957
3958   /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
3959      field, to avoid writing to a PCH file.  */
3960   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
3961     {
3962       if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
3963         break;
3964       size += size_of_loc_descr (l);
3965     }
3966   if (! l)
3967     return size;
3968
3969   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
3970     {
3971       l->dw_loc_addr = size;
3972       size += size_of_loc_descr (l);
3973     }
3974
3975   return size;
3976 }
3977
3978 /* Output location description stack opcode's operands (if any).  */
3979
3980 static void
3981 output_loc_operands (dw_loc_descr_ref loc)
3982 {
3983   dw_val_ref val1 = &loc->dw_loc_oprnd1;
3984   dw_val_ref val2 = &loc->dw_loc_oprnd2;
3985
3986   switch (loc->dw_loc_opc)
3987     {
3988 #ifdef DWARF2_DEBUGGING_INFO
3989     case DW_OP_addr:
3990       dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
3991       break;
3992     case DW_OP_const2u:
3993     case DW_OP_const2s:
3994       dw2_asm_output_data (2, val1->v.val_int, NULL);
3995       break;
3996     case DW_OP_const4u:
3997     case DW_OP_const4s:
3998       dw2_asm_output_data (4, val1->v.val_int, NULL);
3999       break;
4000     case DW_OP_const8u:
4001     case DW_OP_const8s:
4002       gcc_assert (HOST_BITS_PER_LONG >= 64);
4003       dw2_asm_output_data (8, val1->v.val_int, NULL);
4004       break;
4005     case DW_OP_skip:
4006     case DW_OP_bra:
4007       {
4008         int offset;
4009
4010         gcc_assert (val1->val_class == dw_val_class_loc);
4011         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
4012
4013         dw2_asm_output_data (2, offset, NULL);
4014       }
4015       break;
4016 #else
4017     case DW_OP_addr:
4018     case DW_OP_const2u:
4019     case DW_OP_const2s:
4020     case DW_OP_const4u:
4021     case DW_OP_const4s:
4022     case DW_OP_const8u:
4023     case DW_OP_const8s:
4024     case DW_OP_skip:
4025     case DW_OP_bra:
4026       /* We currently don't make any attempt to make sure these are
4027          aligned properly like we do for the main unwind info, so
4028          don't support emitting things larger than a byte if we're
4029          only doing unwinding.  */
4030       gcc_unreachable ();
4031 #endif
4032     case DW_OP_const1u:
4033     case DW_OP_const1s:
4034       dw2_asm_output_data (1, val1->v.val_int, NULL);
4035       break;
4036     case DW_OP_constu:
4037       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4038       break;
4039     case DW_OP_consts:
4040       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
4041       break;
4042     case DW_OP_pick:
4043       dw2_asm_output_data (1, val1->v.val_int, NULL);
4044       break;
4045     case DW_OP_plus_uconst:
4046       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4047       break;
4048     case DW_OP_breg0:
4049     case DW_OP_breg1:
4050     case DW_OP_breg2:
4051     case DW_OP_breg3:
4052     case DW_OP_breg4:
4053     case DW_OP_breg5:
4054     case DW_OP_breg6:
4055     case DW_OP_breg7:
4056     case DW_OP_breg8:
4057     case DW_OP_breg9:
4058     case DW_OP_breg10:
4059     case DW_OP_breg11:
4060     case DW_OP_breg12:
4061     case DW_OP_breg13:
4062     case DW_OP_breg14:
4063     case DW_OP_breg15:
4064     case DW_OP_breg16:
4065     case DW_OP_breg17:
4066     case DW_OP_breg18:
4067     case DW_OP_breg19:
4068     case DW_OP_breg20:
4069     case DW_OP_breg21:
4070     case DW_OP_breg22:
4071     case DW_OP_breg23:
4072     case DW_OP_breg24:
4073     case DW_OP_breg25:
4074     case DW_OP_breg26:
4075     case DW_OP_breg27:
4076     case DW_OP_breg28:
4077     case DW_OP_breg29:
4078     case DW_OP_breg30:
4079     case DW_OP_breg31:
4080       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
4081       break;
4082     case DW_OP_regx:
4083       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4084       break;
4085     case DW_OP_fbreg:
4086       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
4087       break;
4088     case DW_OP_bregx:
4089       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4090       dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
4091       break;
4092     case DW_OP_piece:
4093       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4094       break;
4095     case DW_OP_deref_size:
4096     case DW_OP_xderef_size:
4097       dw2_asm_output_data (1, val1->v.val_int, NULL);
4098       break;
4099
4100     case INTERNAL_DW_OP_tls_addr:
4101       if (targetm.asm_out.output_dwarf_dtprel)
4102         {
4103           targetm.asm_out.output_dwarf_dtprel (asm_out_file,
4104                                                DWARF2_ADDR_SIZE,
4105                                                val1->v.val_addr);
4106           fputc ('\n', asm_out_file);
4107         }
4108       else
4109         gcc_unreachable ();
4110       break;
4111
4112     default:
4113       /* Other codes have no operands.  */
4114       break;
4115     }
4116 }
4117
4118 /* Output a sequence of location operations.  */
4119
4120 static void
4121 output_loc_sequence (dw_loc_descr_ref loc)
4122 {
4123   for (; loc != NULL; loc = loc->dw_loc_next)
4124     {
4125       /* Output the opcode.  */
4126       dw2_asm_output_data (1, loc->dw_loc_opc,
4127                            "%s", dwarf_stack_op_name (loc->dw_loc_opc));
4128
4129       /* Output the operand(s) (if any).  */
4130       output_loc_operands (loc);
4131     }
4132 }
4133
4134 /* Output location description stack opcode's operands (if any).
4135    The output is single bytes on a line, suitable for .cfi_escape.  */
4136
4137 static void
4138 output_loc_operands_raw (dw_loc_descr_ref loc)
4139 {
4140   dw_val_ref val1 = &loc->dw_loc_oprnd1;
4141   dw_val_ref val2 = &loc->dw_loc_oprnd2;
4142
4143   switch (loc->dw_loc_opc)
4144     {
4145     case DW_OP_addr:
4146       /* We cannot output addresses in .cfi_escape, only bytes.  */
4147       gcc_unreachable ();
4148
4149     case DW_OP_const1u:
4150     case DW_OP_const1s:
4151     case DW_OP_pick:
4152     case DW_OP_deref_size:
4153     case DW_OP_xderef_size:
4154       fputc (',', asm_out_file);
4155       dw2_asm_output_data_raw (1, val1->v.val_int);
4156       break;
4157
4158     case DW_OP_const2u:
4159     case DW_OP_const2s:
4160       fputc (',', asm_out_file);
4161       dw2_asm_output_data_raw (2, val1->v.val_int);
4162       break;
4163
4164     case DW_OP_const4u:
4165     case DW_OP_const4s:
4166       fputc (',', asm_out_file);
4167       dw2_asm_output_data_raw (4, val1->v.val_int);
4168       break;
4169
4170     case DW_OP_const8u:
4171     case DW_OP_const8s:
4172       gcc_assert (HOST_BITS_PER_LONG >= 64);
4173       fputc (',', asm_out_file);
4174       dw2_asm_output_data_raw (8, val1->v.val_int);
4175       break;
4176
4177     case DW_OP_skip:
4178     case DW_OP_bra:
4179       {
4180         int offset;
4181
4182         gcc_assert (val1->val_class == dw_val_class_loc);
4183         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
4184
4185         fputc (',', asm_out_file);
4186         dw2_asm_output_data_raw (2, offset);
4187       }
4188       break;
4189
4190     case DW_OP_constu:
4191     case DW_OP_plus_uconst:
4192     case DW_OP_regx:
4193     case DW_OP_piece:
4194       fputc (',', asm_out_file);
4195       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
4196       break;
4197
4198     case DW_OP_consts:
4199     case DW_OP_breg0:
4200     case DW_OP_breg1:
4201     case DW_OP_breg2:
4202     case DW_OP_breg3:
4203     case DW_OP_breg4:
4204     case DW_OP_breg5:
4205     case DW_OP_breg6:
4206     case DW_OP_breg7:
4207     case DW_OP_breg8:
4208     case DW_OP_breg9:
4209     case DW_OP_breg10:
4210     case DW_OP_breg11:
4211     case DW_OP_breg12:
4212     case DW_OP_breg13:
4213     case DW_OP_breg14:
4214     case DW_OP_breg15:
4215     case DW_OP_breg16:
4216     case DW_OP_breg17:
4217     case DW_OP_breg18:
4218     case DW_OP_breg19:
4219     case DW_OP_breg20:
4220     case DW_OP_breg21:
4221     case DW_OP_breg22:
4222     case DW_OP_breg23:
4223     case DW_OP_breg24:
4224     case DW_OP_breg25:
4225     case DW_OP_breg26:
4226     case DW_OP_breg27:
4227     case DW_OP_breg28:
4228     case DW_OP_breg29:
4229     case DW_OP_breg30:
4230     case DW_OP_breg31:
4231     case DW_OP_fbreg:
4232       fputc (',', asm_out_file);
4233       dw2_asm_output_data_sleb128_raw (val1->v.val_int);
4234       break;
4235
4236     case DW_OP_bregx:
4237       fputc (',', asm_out_file);
4238       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
4239       fputc (',', asm_out_file);
4240       dw2_asm_output_data_sleb128_raw (val2->v.val_int);
4241       break;
4242
4243     case INTERNAL_DW_OP_tls_addr:
4244       gcc_unreachable ();
4245
4246     default:
4247       /* Other codes have no operands.  */
4248       break;
4249     }
4250 }
4251
4252 static void
4253 output_loc_sequence_raw (dw_loc_descr_ref loc)
4254 {
4255   while (1)
4256     {
4257       /* Output the opcode.  */
4258       fprintf (asm_out_file, "0x%x", loc->dw_loc_opc);
4259       output_loc_operands_raw (loc);
4260
4261       if (!loc->dw_loc_next)
4262         break;
4263       loc = loc->dw_loc_next;
4264
4265       fputc (',', asm_out_file);
4266     }
4267 }
4268
4269 /* This routine will generate the correct assembly data for a location
4270    description based on a cfi entry with a complex address.  */
4271
4272 static void
4273 output_cfa_loc (dw_cfi_ref cfi)
4274 {
4275   dw_loc_descr_ref loc;
4276   unsigned long size;
4277
4278   if (cfi->dw_cfi_opc == DW_CFA_expression)
4279     dw2_asm_output_data (1, cfi->dw_cfi_oprnd2.dw_cfi_reg_num, NULL);
4280
4281   /* Output the size of the block.  */
4282   loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
4283   size = size_of_locs (loc);
4284   dw2_asm_output_data_uleb128 (size, NULL);
4285
4286   /* Now output the operations themselves.  */
4287   output_loc_sequence (loc);
4288 }
4289
4290 /* Similar, but used for .cfi_escape.  */
4291
4292 static void
4293 output_cfa_loc_raw (dw_cfi_ref cfi)
4294 {
4295   dw_loc_descr_ref loc;
4296   unsigned long size;
4297
4298   if (cfi->dw_cfi_opc == DW_CFA_expression)
4299     fprintf (asm_out_file, "0x%x,", cfi->dw_cfi_oprnd2.dw_cfi_reg_num);
4300
4301   /* Output the size of the block.  */
4302   loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
4303   size = size_of_locs (loc);
4304   dw2_asm_output_data_uleb128_raw (size);
4305   fputc (',', asm_out_file);
4306
4307   /* Now output the operations themselves.  */
4308   output_loc_sequence_raw (loc);
4309 }
4310
4311 /* This function builds a dwarf location descriptor sequence from a
4312    dw_cfa_location, adding the given OFFSET to the result of the
4313    expression.  */
4314
4315 static struct dw_loc_descr_struct *
4316 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
4317 {
4318   struct dw_loc_descr_struct *head, *tmp;
4319
4320   offset += cfa->offset;
4321
4322   if (cfa->indirect)
4323     {
4324       head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
4325       head->dw_loc_oprnd1.val_class = dw_val_class_const;
4326       tmp = new_loc_descr (DW_OP_deref, 0, 0);
4327       add_loc_descr (&head, tmp);
4328       if (offset != 0)
4329         {
4330           tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
4331           add_loc_descr (&head, tmp);
4332         }
4333     }
4334   else
4335     head = new_reg_loc_descr (cfa->reg, offset);
4336
4337   return head;
4338 }
4339
4340 /* This function builds a dwarf location descriptor sequence for
4341    the address at OFFSET from the CFA when stack is aligned to
4342    ALIGNMENT byte.  */
4343
4344 static struct dw_loc_descr_struct *
4345 build_cfa_aligned_loc (HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
4346 {
4347   struct dw_loc_descr_struct *head;
4348   unsigned int dwarf_fp
4349     = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
4350
4351  /* When CFA is defined as FP+OFFSET, emulate stack alignment.  */
4352   if (cfa.reg == HARD_FRAME_POINTER_REGNUM && cfa.indirect == 0)
4353     {
4354       head = new_reg_loc_descr (dwarf_fp, 0);
4355       add_loc_descr (&head, int_loc_descriptor (alignment));
4356       add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
4357
4358       add_loc_descr (&head, int_loc_descriptor (offset));
4359       add_loc_descr (&head, new_loc_descr (DW_OP_plus, 0, 0));
4360     }
4361   else
4362     head = new_reg_loc_descr (dwarf_fp, offset);
4363   return head;
4364 }
4365
4366 /* This function fills in aa dw_cfa_location structure from a dwarf location
4367    descriptor sequence.  */
4368
4369 static void
4370 get_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_struct *loc)
4371 {
4372   struct dw_loc_descr_struct *ptr;
4373   cfa->offset = 0;
4374   cfa->base_offset = 0;
4375   cfa->indirect = 0;
4376   cfa->reg = -1;
4377
4378   for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
4379     {
4380       enum dwarf_location_atom op = ptr->dw_loc_opc;
4381
4382       switch (op)
4383         {
4384         case DW_OP_reg0:
4385         case DW_OP_reg1:
4386         case DW_OP_reg2:
4387         case DW_OP_reg3:
4388         case DW_OP_reg4:
4389         case DW_OP_reg5:
4390         case DW_OP_reg6:
4391         case DW_OP_reg7:
4392         case DW_OP_reg8:
4393         case DW_OP_reg9:
4394         case DW_OP_reg10:
4395         case DW_OP_reg11:
4396         case DW_OP_reg12:
4397         case DW_OP_reg13:
4398         case DW_OP_reg14:
4399         case DW_OP_reg15:
4400         case DW_OP_reg16:
4401         case DW_OP_reg17:
4402         case DW_OP_reg18:
4403         case DW_OP_reg19:
4404         case DW_OP_reg20:
4405         case DW_OP_reg21:
4406         case DW_OP_reg22:
4407         case DW_OP_reg23:
4408         case DW_OP_reg24:
4409         case DW_OP_reg25:
4410         case DW_OP_reg26:
4411         case DW_OP_reg27:
4412         case DW_OP_reg28:
4413         case DW_OP_reg29:
4414         case DW_OP_reg30:
4415         case DW_OP_reg31:
4416           cfa->reg = op - DW_OP_reg0;
4417           break;
4418         case DW_OP_regx:
4419           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
4420           break;
4421         case DW_OP_breg0:
4422         case DW_OP_breg1:
4423         case DW_OP_breg2:
4424         case DW_OP_breg3:
4425         case DW_OP_breg4:
4426         case DW_OP_breg5:
4427         case DW_OP_breg6:
4428         case DW_OP_breg7:
4429         case DW_OP_breg8:
4430         case DW_OP_breg9:
4431         case DW_OP_breg10:
4432         case DW_OP_breg11:
4433         case DW_OP_breg12:
4434         case DW_OP_breg13:
4435         case DW_OP_breg14:
4436         case DW_OP_breg15:
4437         case DW_OP_breg16:
4438         case DW_OP_breg17:
4439         case DW_OP_breg18:
4440         case DW_OP_breg19:
4441         case DW_OP_breg20:
4442         case DW_OP_breg21:
4443         case DW_OP_breg22:
4444         case DW_OP_breg23:
4445         case DW_OP_breg24:
4446         case DW_OP_breg25:
4447         case DW_OP_breg26:
4448         case DW_OP_breg27:
4449         case DW_OP_breg28:
4450         case DW_OP_breg29:
4451         case DW_OP_breg30:
4452         case DW_OP_breg31:
4453           cfa->reg = op - DW_OP_breg0;
4454           cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
4455           break;
4456         case DW_OP_bregx:
4457           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
4458           cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
4459           break;
4460         case DW_OP_deref:
4461           cfa->indirect = 1;
4462           break;
4463         case DW_OP_plus_uconst:
4464           cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
4465           break;
4466         default:
4467           internal_error ("DW_LOC_OP %s not implemented",
4468                           dwarf_stack_op_name (ptr->dw_loc_opc));
4469         }
4470     }
4471 }
4472 #endif /* .debug_frame support */
4473 \f
4474 /* And now, the support for symbolic debugging information.  */
4475 #ifdef DWARF2_DEBUGGING_INFO
4476
4477 /* .debug_str support.  */
4478 static int output_indirect_string (void **, void *);
4479
4480 static void dwarf2out_init (const char *);
4481 static void dwarf2out_finish (const char *);
4482 static void dwarf2out_define (unsigned int, const char *);
4483 static void dwarf2out_undef (unsigned int, const char *);
4484 static void dwarf2out_start_source_file (unsigned, const char *);
4485 static void dwarf2out_end_source_file (unsigned);
4486 static void dwarf2out_begin_block (unsigned, unsigned);
4487 static void dwarf2out_end_block (unsigned, unsigned);
4488 static bool dwarf2out_ignore_block (const_tree);
4489 static void dwarf2out_global_decl (tree);
4490 static void dwarf2out_type_decl (tree, int);
4491 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
4492 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
4493                                                  dw_die_ref);
4494 static void dwarf2out_abstract_function (tree);
4495 static void dwarf2out_var_location (rtx);
4496 static void dwarf2out_begin_function (tree);
4497
4498 /* The debug hooks structure.  */
4499
4500 const struct gcc_debug_hooks dwarf2_debug_hooks =
4501 {
4502   dwarf2out_init,
4503   dwarf2out_finish,
4504   dwarf2out_define,
4505   dwarf2out_undef,
4506   dwarf2out_start_source_file,
4507   dwarf2out_end_source_file,
4508   dwarf2out_begin_block,
4509   dwarf2out_end_block,
4510   dwarf2out_ignore_block,
4511   dwarf2out_source_line,
4512   dwarf2out_begin_prologue,
4513   debug_nothing_int_charstar,   /* end_prologue */
4514   dwarf2out_end_epilogue,
4515   dwarf2out_begin_function,
4516   debug_nothing_int,            /* end_function */
4517   dwarf2out_decl,               /* function_decl */
4518   dwarf2out_global_decl,
4519   dwarf2out_type_decl,          /* type_decl */
4520   dwarf2out_imported_module_or_decl,
4521   debug_nothing_tree,           /* deferred_inline_function */
4522   /* The DWARF 2 backend tries to reduce debugging bloat by not
4523      emitting the abstract description of inline functions until
4524      something tries to reference them.  */
4525   dwarf2out_abstract_function,  /* outlining_inline_function */
4526   debug_nothing_rtx,            /* label */
4527   debug_nothing_int,            /* handle_pch */
4528   dwarf2out_var_location,
4529   dwarf2out_switch_text_section,
4530   1                             /* start_end_main_source_file */
4531 };
4532 #endif
4533 \f
4534 /* NOTE: In the comments in this file, many references are made to
4535    "Debugging Information Entries".  This term is abbreviated as `DIE'
4536    throughout the remainder of this file.  */
4537
4538 /* An internal representation of the DWARF output is built, and then
4539    walked to generate the DWARF debugging info.  The walk of the internal
4540    representation is done after the entire program has been compiled.
4541    The types below are used to describe the internal representation.  */
4542
4543 /* Various DIE's use offsets relative to the beginning of the
4544    .debug_info section to refer to each other.  */
4545
4546 typedef long int dw_offset;
4547
4548 /* Define typedefs here to avoid circular dependencies.  */
4549
4550 typedef struct dw_attr_struct *dw_attr_ref;
4551 typedef struct dw_line_info_struct *dw_line_info_ref;
4552 typedef struct dw_separate_line_info_struct *dw_separate_line_info_ref;
4553 typedef struct pubname_struct *pubname_ref;
4554 typedef struct dw_ranges_struct *dw_ranges_ref;
4555 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
4556
4557 /* Each entry in the line_info_table maintains the file and
4558    line number associated with the label generated for that
4559    entry.  The label gives the PC value associated with
4560    the line number entry.  */
4561
4562 typedef struct dw_line_info_struct GTY(())
4563 {
4564   unsigned long dw_file_num;
4565   unsigned long dw_line_num;
4566 }
4567 dw_line_info_entry;
4568
4569 /* Line information for functions in separate sections; each one gets its
4570    own sequence.  */
4571 typedef struct dw_separate_line_info_struct GTY(())
4572 {
4573   unsigned long dw_file_num;
4574   unsigned long dw_line_num;
4575   unsigned long function;
4576 }
4577 dw_separate_line_info_entry;
4578
4579 /* Each DIE attribute has a field specifying the attribute kind,
4580    a link to the next attribute in the chain, and an attribute value.
4581    Attributes are typically linked below the DIE they modify.  */
4582
4583 typedef struct dw_attr_struct GTY(())
4584 {
4585   enum dwarf_attribute dw_attr;
4586   dw_val_node dw_attr_val;
4587 }
4588 dw_attr_node;
4589
4590 DEF_VEC_O(dw_attr_node);
4591 DEF_VEC_ALLOC_O(dw_attr_node,gc);
4592
4593 /* The Debugging Information Entry (DIE) structure.  DIEs form a tree.
4594    The children of each node form a circular list linked by
4595    die_sib.  die_child points to the node *before* the "first" child node.  */
4596
4597 typedef struct die_struct GTY((chain_circular ("%h.die_sib")))
4598 {
4599   enum dwarf_tag die_tag;
4600   char *die_symbol;
4601   VEC(dw_attr_node,gc) * die_attr;
4602   dw_die_ref die_parent;
4603   dw_die_ref die_child;
4604   dw_die_ref die_sib;
4605   dw_die_ref die_definition; /* ref from a specification to its definition */
4606   dw_offset die_offset;
4607   unsigned long die_abbrev;
4608   int die_mark;
4609   /* Die is used and must not be pruned as unused.  */
4610   int die_perennial_p;
4611   unsigned int decl_id;
4612 }
4613 die_node;
4614
4615 /* Evaluate 'expr' while 'c' is set to each child of DIE in order.  */
4616 #define FOR_EACH_CHILD(die, c, expr) do {       \
4617   c = die->die_child;                           \
4618   if (c) do {                                   \
4619     c = c->die_sib;                             \
4620     expr;                                       \
4621   } while (c != die->die_child);                \
4622 } while (0)
4623
4624 /* The pubname structure */
4625
4626 typedef struct pubname_struct GTY(())
4627 {
4628   dw_die_ref die;
4629   const char *name;
4630 }
4631 pubname_entry;
4632
4633 DEF_VEC_O(pubname_entry);
4634 DEF_VEC_ALLOC_O(pubname_entry, gc);
4635
4636 struct dw_ranges_struct GTY(())
4637 {
4638   /* If this is positive, it's a block number, otherwise it's a
4639      bitwise-negated index into dw_ranges_by_label.  */
4640   int num;
4641 };
4642
4643 struct dw_ranges_by_label_struct GTY(())
4644 {
4645   const char *begin;
4646   const char *end;
4647 };
4648
4649 /* The limbo die list structure.  */
4650 typedef struct limbo_die_struct GTY(())
4651 {
4652   dw_die_ref die;
4653   tree created_for;
4654   struct limbo_die_struct *next;
4655 }
4656 limbo_die_node;
4657
4658 /* How to start an assembler comment.  */
4659 #ifndef ASM_COMMENT_START
4660 #define ASM_COMMENT_START ";#"
4661 #endif
4662
4663 /* Define a macro which returns nonzero for a TYPE_DECL which was
4664    implicitly generated for a tagged type.
4665
4666    Note that unlike the gcc front end (which generates a NULL named
4667    TYPE_DECL node for each complete tagged type, each array type, and
4668    each function type node created) the g++ front end generates a
4669    _named_ TYPE_DECL node for each tagged type node created.
4670    These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
4671    generate a DW_TAG_typedef DIE for them.  */
4672
4673 #define TYPE_DECL_IS_STUB(decl)                         \
4674   (DECL_NAME (decl) == NULL_TREE                        \
4675    || (DECL_ARTIFICIAL (decl)                           \
4676        && is_tagged_type (TREE_TYPE (decl))             \
4677        && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl)))  \
4678            /* This is necessary for stub decls that     \
4679               appear in nested inline functions.  */    \
4680            || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
4681                && (decl_ultimate_origin (decl)          \
4682                    == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
4683
4684 /* Information concerning the compilation unit's programming
4685    language, and compiler version.  */
4686
4687 /* Fixed size portion of the DWARF compilation unit header.  */
4688 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
4689   (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
4690
4691 /* Fixed size portion of public names info.  */
4692 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
4693
4694 /* Fixed size portion of the address range info.  */
4695 #define DWARF_ARANGES_HEADER_SIZE                                       \
4696   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4,      \
4697                 DWARF2_ADDR_SIZE * 2)                                   \
4698    - DWARF_INITIAL_LENGTH_SIZE)
4699
4700 /* Size of padding portion in the address range info.  It must be
4701    aligned to twice the pointer size.  */
4702 #define DWARF_ARANGES_PAD_SIZE \
4703   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
4704                 DWARF2_ADDR_SIZE * 2)                              \
4705    - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
4706
4707 /* Use assembler line directives if available.  */
4708 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
4709 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
4710 #define DWARF2_ASM_LINE_DEBUG_INFO 1
4711 #else
4712 #define DWARF2_ASM_LINE_DEBUG_INFO 0
4713 #endif
4714 #endif
4715
4716 /* Minimum line offset in a special line info. opcode.
4717    This value was chosen to give a reasonable range of values.  */
4718 #define DWARF_LINE_BASE  -10
4719
4720 /* First special line opcode - leave room for the standard opcodes.  */
4721 #define DWARF_LINE_OPCODE_BASE  10
4722
4723 /* Range of line offsets in a special line info. opcode.  */
4724 #define DWARF_LINE_RANGE  (254-DWARF_LINE_OPCODE_BASE+1)
4725
4726 /* Flag that indicates the initial value of the is_stmt_start flag.
4727    In the present implementation, we do not mark any lines as
4728    the beginning of a source statement, because that information
4729    is not made available by the GCC front-end.  */
4730 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
4731
4732 #ifdef DWARF2_DEBUGGING_INFO
4733 /* This location is used by calc_die_sizes() to keep track
4734    the offset of each DIE within the .debug_info section.  */
4735 static unsigned long next_die_offset;
4736 #endif
4737
4738 /* Record the root of the DIE's built for the current compilation unit.  */
4739 static GTY(()) dw_die_ref comp_unit_die;
4740
4741 /* A list of DIEs with a NULL parent waiting to be relocated.  */
4742 static GTY(()) limbo_die_node *limbo_die_list;
4743
4744 /* Filenames referenced by this compilation unit.  */
4745 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
4746
4747 /* A hash table of references to DIE's that describe declarations.
4748    The key is a DECL_UID() which is a unique number identifying each decl.  */
4749 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
4750
4751 /* A hash table of references to DIE's that describe COMMON blocks.
4752    The key is DECL_UID() ^ die_parent.  */
4753 static GTY ((param_is (struct die_struct))) htab_t common_block_die_table;
4754
4755 /* Node of the variable location list.  */
4756 struct var_loc_node GTY ((chain_next ("%h.next")))
4757 {
4758   rtx GTY (()) var_loc_note;
4759   const char * GTY (()) label;
4760   const char * GTY (()) section_label;
4761   struct var_loc_node * GTY (()) next;
4762 };
4763
4764 /* Variable location list.  */
4765 struct var_loc_list_def GTY (())
4766 {
4767   struct var_loc_node * GTY (()) first;
4768
4769   /* Do not mark the last element of the chained list because
4770      it is marked through the chain.  */
4771   struct var_loc_node * GTY ((skip ("%h"))) last;
4772
4773   /* DECL_UID of the variable decl.  */
4774   unsigned int decl_id;
4775 };
4776 typedef struct var_loc_list_def var_loc_list;
4777
4778
4779 /* Table of decl location linked lists.  */
4780 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
4781
4782 /* A pointer to the base of a list of references to DIE's that
4783    are uniquely identified by their tag, presence/absence of
4784    children DIE's, and list of attribute/value pairs.  */
4785 static GTY((length ("abbrev_die_table_allocated")))
4786   dw_die_ref *abbrev_die_table;
4787
4788 /* Number of elements currently allocated for abbrev_die_table.  */
4789 static GTY(()) unsigned abbrev_die_table_allocated;
4790
4791 /* Number of elements in type_die_table currently in use.  */
4792 static GTY(()) unsigned abbrev_die_table_in_use;
4793
4794 /* Size (in elements) of increments by which we may expand the
4795    abbrev_die_table.  */
4796 #define ABBREV_DIE_TABLE_INCREMENT 256
4797
4798 /* A pointer to the base of a table that contains line information
4799    for each source code line in .text in the compilation unit.  */
4800 static GTY((length ("line_info_table_allocated")))
4801      dw_line_info_ref line_info_table;
4802
4803 /* Number of elements currently allocated for line_info_table.  */
4804 static GTY(()) unsigned line_info_table_allocated;
4805
4806 /* Number of elements in line_info_table currently in use.  */
4807 static GTY(()) unsigned line_info_table_in_use;
4808
4809 /* A pointer to the base of a table that contains line information
4810    for each source code line outside of .text in the compilation unit.  */
4811 static GTY ((length ("separate_line_info_table_allocated")))
4812      dw_separate_line_info_ref separate_line_info_table;
4813
4814 /* Number of elements currently allocated for separate_line_info_table.  */
4815 static GTY(()) unsigned separate_line_info_table_allocated;
4816
4817 /* Number of elements in separate_line_info_table currently in use.  */
4818 static GTY(()) unsigned separate_line_info_table_in_use;
4819
4820 /* Size (in elements) of increments by which we may expand the
4821    line_info_table.  */
4822 #define LINE_INFO_TABLE_INCREMENT 1024
4823
4824 /* A pointer to the base of a table that contains a list of publicly
4825    accessible names.  */
4826 static GTY (()) VEC (pubname_entry, gc) *  pubname_table;
4827
4828 /* A pointer to the base of a table that contains a list of publicly
4829    accessible types.  */
4830 static GTY (()) VEC (pubname_entry, gc) * pubtype_table;
4831
4832 /* Array of dies for which we should generate .debug_arange info.  */
4833 static GTY((length ("arange_table_allocated"))) dw_die_ref *arange_table;
4834
4835 /* Number of elements currently allocated for arange_table.  */
4836 static GTY(()) unsigned arange_table_allocated;
4837
4838 /* Number of elements in arange_table currently in use.  */
4839 static GTY(()) unsigned arange_table_in_use;
4840
4841 /* Size (in elements) of increments by which we may expand the
4842    arange_table.  */
4843 #define ARANGE_TABLE_INCREMENT 64
4844
4845 /* Array of dies for which we should generate .debug_ranges info.  */
4846 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
4847
4848 /* Number of elements currently allocated for ranges_table.  */
4849 static GTY(()) unsigned ranges_table_allocated;
4850
4851 /* Number of elements in ranges_table currently in use.  */
4852 static GTY(()) unsigned ranges_table_in_use;
4853
4854 /* Array of pairs of labels referenced in ranges_table.  */
4855 static GTY ((length ("ranges_by_label_allocated")))
4856      dw_ranges_by_label_ref ranges_by_label;
4857
4858 /* Number of elements currently allocated for ranges_by_label.  */
4859 static GTY(()) unsigned ranges_by_label_allocated;
4860
4861 /* Number of elements in ranges_by_label currently in use.  */
4862 static GTY(()) unsigned ranges_by_label_in_use;
4863
4864 /* Size (in elements) of increments by which we may expand the
4865    ranges_table.  */
4866 #define RANGES_TABLE_INCREMENT 64
4867
4868 /* Whether we have location lists that need outputting */
4869 static GTY(()) bool have_location_lists;
4870
4871 /* Unique label counter.  */
4872 static GTY(()) unsigned int loclabel_num;
4873
4874 #ifdef DWARF2_DEBUGGING_INFO
4875 /* Record whether the function being analyzed contains inlined functions.  */
4876 static int current_function_has_inlines;
4877 #endif
4878 #if 0 && defined (MIPS_DEBUGGING_INFO)
4879 static int comp_unit_has_inlines;
4880 #endif
4881
4882 /* The last file entry emitted by maybe_emit_file().  */
4883 static GTY(()) struct dwarf_file_data * last_emitted_file;
4884
4885 /* Number of internal labels generated by gen_internal_sym().  */
4886 static GTY(()) int label_num;
4887
4888 /* Cached result of previous call to lookup_filename.  */
4889 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
4890
4891 #ifdef DWARF2_DEBUGGING_INFO
4892
4893 /* Offset from the "steady-state frame pointer" to the frame base,
4894    within the current function.  */
4895 static HOST_WIDE_INT frame_pointer_fb_offset;
4896
4897 /* Forward declarations for functions defined in this file.  */
4898
4899 static int is_pseudo_reg (const_rtx);
4900 static tree type_main_variant (tree);
4901 static int is_tagged_type (const_tree);
4902 static const char *dwarf_tag_name (unsigned);
4903 static const char *dwarf_attr_name (unsigned);
4904 static const char *dwarf_form_name (unsigned);
4905 static tree decl_ultimate_origin (const_tree);
4906 static tree block_ultimate_origin (const_tree);
4907 static tree decl_class_context (tree);
4908 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
4909 static inline enum dw_val_class AT_class (dw_attr_ref);
4910 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
4911 static inline unsigned AT_flag (dw_attr_ref);
4912 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
4913 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
4914 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
4915 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
4916 static void add_AT_long_long (dw_die_ref, enum dwarf_attribute, unsigned long,
4917                               unsigned long);
4918 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
4919                                unsigned int, unsigned char *);
4920 static hashval_t debug_str_do_hash (const void *);
4921 static int debug_str_eq (const void *, const void *);
4922 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
4923 static inline const char *AT_string (dw_attr_ref);
4924 static int AT_string_form (dw_attr_ref);
4925 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
4926 static void add_AT_specification (dw_die_ref, dw_die_ref);
4927 static inline dw_die_ref AT_ref (dw_attr_ref);
4928 static inline int AT_ref_external (dw_attr_ref);
4929 static inline void set_AT_ref_external (dw_attr_ref, int);
4930 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
4931 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
4932 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
4933 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
4934                              dw_loc_list_ref);
4935 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
4936 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
4937 static inline rtx AT_addr (dw_attr_ref);
4938 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
4939 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
4940 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
4941 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
4942                            unsigned HOST_WIDE_INT);
4943 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
4944                                unsigned long);
4945 static inline const char *AT_lbl (dw_attr_ref);
4946 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
4947 static const char *get_AT_low_pc (dw_die_ref);
4948 static const char *get_AT_hi_pc (dw_die_ref);
4949 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
4950 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
4951 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
4952 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
4953 static bool is_c_family (void);
4954 static bool is_cxx (void);
4955 static bool is_java (void);
4956 static bool is_fortran (void);
4957 static bool is_ada (void);
4958 static void remove_AT (dw_die_ref, enum dwarf_attribute);
4959 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
4960 static void add_child_die (dw_die_ref, dw_die_ref);
4961 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
4962 static dw_die_ref lookup_type_die (tree);
4963 static void equate_type_number_to_die (tree, dw_die_ref);
4964 static hashval_t decl_die_table_hash (const void *);
4965 static int decl_die_table_eq (const void *, const void *);
4966 static dw_die_ref lookup_decl_die (tree);
4967 static hashval_t common_block_die_table_hash (const void *);
4968 static int common_block_die_table_eq (const void *, const void *);
4969 static hashval_t decl_loc_table_hash (const void *);
4970 static int decl_loc_table_eq (const void *, const void *);
4971 static var_loc_list *lookup_decl_loc (const_tree);
4972 static void equate_decl_number_to_die (tree, dw_die_ref);
4973 static void add_var_loc_to_decl (tree, struct var_loc_node *);
4974 static void print_spaces (FILE *);
4975 static void print_die (dw_die_ref, FILE *);
4976 static void print_dwarf_line_table (FILE *);
4977 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
4978 static dw_die_ref pop_compile_unit (dw_die_ref);
4979 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
4980 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
4981 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
4982 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
4983 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
4984 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
4985 static int same_die_p (dw_die_ref, dw_die_ref, int *);
4986 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
4987 static void compute_section_prefix (dw_die_ref);
4988 static int is_type_die (dw_die_ref);
4989 static int is_comdat_die (dw_die_ref);
4990 static int is_symbol_die (dw_die_ref);
4991 static void assign_symbol_names (dw_die_ref);
4992 static void break_out_includes (dw_die_ref);
4993 static hashval_t htab_cu_hash (const void *);
4994 static int htab_cu_eq (const void *, const void *);
4995 static void htab_cu_del (void *);
4996 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
4997 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
4998 static void add_sibling_attributes (dw_die_ref);
4999 static void build_abbrev_table (dw_die_ref);
5000 static void output_location_lists (dw_die_ref);
5001 static int constant_size (unsigned HOST_WIDE_INT);
5002 static unsigned long size_of_die (dw_die_ref);
5003 static void calc_die_sizes (dw_die_ref);
5004 static void mark_dies (dw_die_ref);
5005 static void unmark_dies (dw_die_ref);
5006 static void unmark_all_dies (dw_die_ref);
5007 static unsigned long size_of_pubnames (VEC (pubname_entry,gc) *);
5008 static unsigned long size_of_aranges (void);
5009 static enum dwarf_form value_format (dw_attr_ref);
5010 static void output_value_format (dw_attr_ref);
5011 static void output_abbrev_section (void);
5012 static void output_die_symbol (dw_die_ref);
5013 static void output_die (dw_die_ref);
5014 static void output_compilation_unit_header (void);
5015 static void output_comp_unit (dw_die_ref, int);
5016 static const char *dwarf2_name (tree, int);
5017 static void add_pubname (tree, dw_die_ref);
5018 static void add_pubname_string (const char *, dw_die_ref);
5019 static void add_pubtype (tree, dw_die_ref);
5020 static void output_pubnames (VEC (pubname_entry,gc) *);
5021 static void add_arange (tree, dw_die_ref);
5022 static void output_aranges (void);
5023 static unsigned int add_ranges_num (int);
5024 static unsigned int add_ranges (const_tree);
5025 static unsigned int add_ranges_by_labels (const char *, const char *);
5026 static void output_ranges (void);
5027 static void output_line_info (void);
5028 static void output_file_names (void);
5029 static dw_die_ref base_type_die (tree);
5030 static int is_base_type (tree);
5031 static bool is_subrange_type (const_tree);
5032 static dw_die_ref subrange_type_die (tree, dw_die_ref);
5033 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
5034 static int type_is_enum (const_tree);
5035 static unsigned int dbx_reg_number (const_rtx);
5036 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
5037 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
5038 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
5039                                                 enum var_init_status);
5040 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
5041                                                      enum var_init_status);
5042 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
5043                                          enum var_init_status);
5044 static int is_based_loc (const_rtx);
5045 static dw_loc_descr_ref mem_loc_descriptor (rtx, enum machine_mode mode,
5046                                             enum var_init_status);
5047 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
5048                                                enum var_init_status);
5049 static dw_loc_descr_ref loc_descriptor (rtx, enum var_init_status);
5050 static dw_loc_descr_ref loc_descriptor_from_tree_1 (tree, int);
5051 static dw_loc_descr_ref loc_descriptor_from_tree (tree);
5052 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
5053 static tree field_type (const_tree);
5054 static unsigned int simple_type_align_in_bits (const_tree);
5055 static unsigned int simple_decl_align_in_bits (const_tree);
5056 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
5057 static HOST_WIDE_INT field_byte_offset (const_tree);
5058 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
5059                                          dw_loc_descr_ref);
5060 static void add_data_member_location_attribute (dw_die_ref, tree);
5061 static void add_const_value_attribute (dw_die_ref, rtx);
5062 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
5063 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
5064 static void insert_float (const_rtx, unsigned char *);
5065 static rtx rtl_for_decl_location (tree);
5066 static void add_location_or_const_value_attribute (dw_die_ref, tree,
5067                                                    enum dwarf_attribute);
5068 static void tree_add_const_value_attribute (dw_die_ref, tree);
5069 static void add_name_attribute (dw_die_ref, const char *);
5070 static void add_comp_dir_attribute (dw_die_ref);
5071 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
5072 static void add_subscript_info (dw_die_ref, tree, bool);
5073 static void add_byte_size_attribute (dw_die_ref, tree);
5074 static void add_bit_offset_attribute (dw_die_ref, tree);
5075 static void add_bit_size_attribute (dw_die_ref, tree);
5076 static void add_prototyped_attribute (dw_die_ref, tree);
5077 static void add_abstract_origin_attribute (dw_die_ref, tree);
5078 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
5079 static void add_src_coords_attributes (dw_die_ref, tree);
5080 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
5081 static void push_decl_scope (tree);
5082 static void pop_decl_scope (void);
5083 static dw_die_ref scope_die_for (tree, dw_die_ref);
5084 static inline int local_scope_p (dw_die_ref);
5085 static inline int class_or_namespace_scope_p (dw_die_ref);
5086 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
5087 static void add_calling_convention_attribute (dw_die_ref, tree);
5088 static const char *type_tag (const_tree);
5089 static tree member_declared_type (const_tree);
5090 #if 0
5091 static const char *decl_start_label (tree);
5092 #endif
5093 static void gen_array_type_die (tree, dw_die_ref);
5094 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
5095 #if 0
5096 static void gen_entry_point_die (tree, dw_die_ref);
5097 #endif
5098 static void gen_inlined_enumeration_type_die (tree, dw_die_ref);
5099 static void gen_inlined_structure_type_die (tree, dw_die_ref);
5100 static void gen_inlined_union_type_die (tree, dw_die_ref);
5101 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
5102 static dw_die_ref gen_formal_parameter_die (tree, dw_die_ref);
5103 static void gen_unspecified_parameters_die (tree, dw_die_ref);
5104 static void gen_formal_types_die (tree, dw_die_ref);
5105 static void gen_subprogram_die (tree, dw_die_ref);
5106 static void gen_variable_die (tree, dw_die_ref);
5107 static void gen_const_die (tree, dw_die_ref);
5108 static void gen_label_die (tree, dw_die_ref);
5109 static void gen_lexical_block_die (tree, dw_die_ref, int);
5110 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
5111 static void gen_field_die (tree, dw_die_ref);
5112 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
5113 static dw_die_ref gen_compile_unit_die (const char *);
5114 static void gen_inheritance_die (tree, tree, dw_die_ref);
5115 static void gen_member_die (tree, dw_die_ref);
5116 static void gen_struct_or_union_type_die (tree, dw_die_ref,
5117                                                 enum debug_info_usage);
5118 static void gen_subroutine_type_die (tree, dw_die_ref);
5119 static void gen_typedef_die (tree, dw_die_ref);
5120 static void gen_type_die (tree, dw_die_ref);
5121 static void gen_tagged_type_instantiation_die (tree, dw_die_ref);
5122 static void gen_block_die (tree, dw_die_ref, int);
5123 static void decls_for_scope (tree, dw_die_ref, int);
5124 static int is_redundant_typedef (const_tree);
5125 static void gen_namespace_die (tree);
5126 static void gen_decl_die (tree, dw_die_ref);
5127 static dw_die_ref force_decl_die (tree);
5128 static dw_die_ref force_type_die (tree);
5129 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
5130 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
5131 static struct dwarf_file_data * lookup_filename (const char *);
5132 static void retry_incomplete_types (void);
5133 static void gen_type_die_for_member (tree, tree, dw_die_ref);
5134 static void splice_child_die (dw_die_ref, dw_die_ref);
5135 static int file_info_cmp (const void *, const void *);
5136 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
5137                                      const char *, const char *, unsigned);
5138 static void add_loc_descr_to_loc_list (dw_loc_list_ref *, dw_loc_descr_ref,
5139                                        const char *, const char *,
5140                                        const char *);
5141 static void output_loc_list (dw_loc_list_ref);
5142 static char *gen_internal_sym (const char *);
5143
5144 static void prune_unmark_dies (dw_die_ref);
5145 static void prune_unused_types_mark (dw_die_ref, int);
5146 static void prune_unused_types_walk (dw_die_ref);
5147 static void prune_unused_types_walk_attribs (dw_die_ref);
5148 static void prune_unused_types_prune (dw_die_ref);
5149 static void prune_unused_types (void);
5150 static int maybe_emit_file (struct dwarf_file_data *fd);
5151
5152 /* Section names used to hold DWARF debugging information.  */
5153 #ifndef DEBUG_INFO_SECTION
5154 #define DEBUG_INFO_SECTION      ".debug_info"
5155 #endif
5156 #ifndef DEBUG_ABBREV_SECTION
5157 #define DEBUG_ABBREV_SECTION    ".debug_abbrev"
5158 #endif
5159 #ifndef DEBUG_ARANGES_SECTION
5160 #define DEBUG_ARANGES_SECTION   ".debug_aranges"
5161 #endif
5162 #ifndef DEBUG_MACINFO_SECTION
5163 #define DEBUG_MACINFO_SECTION   ".debug_macinfo"
5164 #endif
5165 #ifndef DEBUG_LINE_SECTION
5166 #define DEBUG_LINE_SECTION      ".debug_line"
5167 #endif
5168 #ifndef DEBUG_LOC_SECTION
5169 #define DEBUG_LOC_SECTION       ".debug_loc"
5170 #endif
5171 #ifndef DEBUG_PUBNAMES_SECTION
5172 #define DEBUG_PUBNAMES_SECTION  ".debug_pubnames"
5173 #endif
5174 #ifndef DEBUG_STR_SECTION
5175 #define DEBUG_STR_SECTION       ".debug_str"
5176 #endif
5177 #ifndef DEBUG_RANGES_SECTION
5178 #define DEBUG_RANGES_SECTION    ".debug_ranges"
5179 #endif
5180
5181 /* Standard ELF section names for compiled code and data.  */
5182 #ifndef TEXT_SECTION_NAME
5183 #define TEXT_SECTION_NAME       ".text"
5184 #endif
5185
5186 /* Section flags for .debug_str section.  */
5187 #define DEBUG_STR_SECTION_FLAGS \
5188   (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings               \
5189    ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1        \
5190    : SECTION_DEBUG)
5191
5192 /* Labels we insert at beginning sections we can reference instead of
5193    the section names themselves.  */
5194
5195 #ifndef TEXT_SECTION_LABEL
5196 #define TEXT_SECTION_LABEL              "Ltext"
5197 #endif
5198 #ifndef COLD_TEXT_SECTION_LABEL
5199 #define COLD_TEXT_SECTION_LABEL         "Ltext_cold"
5200 #endif
5201 #ifndef DEBUG_LINE_SECTION_LABEL
5202 #define DEBUG_LINE_SECTION_LABEL        "Ldebug_line"
5203 #endif
5204 #ifndef DEBUG_INFO_SECTION_LABEL
5205 #define DEBUG_INFO_SECTION_LABEL        "Ldebug_info"
5206 #endif
5207 #ifndef DEBUG_ABBREV_SECTION_LABEL
5208 #define DEBUG_ABBREV_SECTION_LABEL      "Ldebug_abbrev"
5209 #endif
5210 #ifndef DEBUG_LOC_SECTION_LABEL
5211 #define DEBUG_LOC_SECTION_LABEL         "Ldebug_loc"
5212 #endif
5213 #ifndef DEBUG_RANGES_SECTION_LABEL
5214 #define DEBUG_RANGES_SECTION_LABEL      "Ldebug_ranges"
5215 #endif
5216 #ifndef DEBUG_MACINFO_SECTION_LABEL
5217 #define DEBUG_MACINFO_SECTION_LABEL     "Ldebug_macinfo"
5218 #endif
5219
5220 /* Definitions of defaults for formats and names of various special
5221    (artificial) labels which may be generated within this file (when the -g
5222    options is used and DWARF2_DEBUGGING_INFO is in effect.
5223    If necessary, these may be overridden from within the tm.h file, but
5224    typically, overriding these defaults is unnecessary.  */
5225
5226 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
5227 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5228 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5229 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
5230 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5231 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5232 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5233 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5234 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5235 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
5236
5237 #ifndef TEXT_END_LABEL
5238 #define TEXT_END_LABEL          "Letext"
5239 #endif
5240 #ifndef COLD_END_LABEL
5241 #define COLD_END_LABEL          "Letext_cold"
5242 #endif
5243 #ifndef BLOCK_BEGIN_LABEL
5244 #define BLOCK_BEGIN_LABEL       "LBB"
5245 #endif
5246 #ifndef BLOCK_END_LABEL
5247 #define BLOCK_END_LABEL         "LBE"
5248 #endif
5249 #ifndef LINE_CODE_LABEL
5250 #define LINE_CODE_LABEL         "LM"
5251 #endif
5252 #ifndef SEPARATE_LINE_CODE_LABEL
5253 #define SEPARATE_LINE_CODE_LABEL        "LSM"
5254 #endif
5255
5256 \f
5257 /* We allow a language front-end to designate a function that is to be
5258    called to "demangle" any name before it is put into a DIE.  */
5259
5260 static const char *(*demangle_name_func) (const char *);
5261
5262 void
5263 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
5264 {
5265   demangle_name_func = func;
5266 }
5267
5268 /* Test if rtl node points to a pseudo register.  */
5269
5270 static inline int
5271 is_pseudo_reg (const_rtx rtl)
5272 {
5273   return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
5274           || (GET_CODE (rtl) == SUBREG
5275               && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
5276 }
5277
5278 /* Return a reference to a type, with its const and volatile qualifiers
5279    removed.  */
5280
5281 static inline tree
5282 type_main_variant (tree type)
5283 {
5284   type = TYPE_MAIN_VARIANT (type);
5285
5286   /* ??? There really should be only one main variant among any group of
5287      variants of a given type (and all of the MAIN_VARIANT values for all
5288      members of the group should point to that one type) but sometimes the C
5289      front-end messes this up for array types, so we work around that bug
5290      here.  */
5291   if (TREE_CODE (type) == ARRAY_TYPE)
5292     while (type != TYPE_MAIN_VARIANT (type))
5293       type = TYPE_MAIN_VARIANT (type);
5294
5295   return type;
5296 }
5297
5298 /* Return nonzero if the given type node represents a tagged type.  */
5299
5300 static inline int
5301 is_tagged_type (const_tree type)
5302 {
5303   enum tree_code code = TREE_CODE (type);
5304
5305   return (code == RECORD_TYPE || code == UNION_TYPE
5306           || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
5307 }
5308
5309 /* Convert a DIE tag into its string name.  */
5310
5311 static const char *
5312 dwarf_tag_name (unsigned int tag)
5313 {
5314   switch (tag)
5315     {
5316     case DW_TAG_padding:
5317       return "DW_TAG_padding";
5318     case DW_TAG_array_type:
5319       return "DW_TAG_array_type";
5320     case DW_TAG_class_type:
5321       return "DW_TAG_class_type";
5322     case DW_TAG_entry_point:
5323       return "DW_TAG_entry_point";
5324     case DW_TAG_enumeration_type:
5325       return "DW_TAG_enumeration_type";
5326     case DW_TAG_formal_parameter:
5327       return "DW_TAG_formal_parameter";
5328     case DW_TAG_imported_declaration:
5329       return "DW_TAG_imported_declaration";
5330     case DW_TAG_label:
5331       return "DW_TAG_label";
5332     case DW_TAG_lexical_block:
5333       return "DW_TAG_lexical_block";
5334     case DW_TAG_member:
5335       return "DW_TAG_member";
5336     case DW_TAG_pointer_type:
5337       return "DW_TAG_pointer_type";
5338     case DW_TAG_reference_type:
5339       return "DW_TAG_reference_type";
5340     case DW_TAG_compile_unit:
5341       return "DW_TAG_compile_unit";
5342     case DW_TAG_string_type:
5343       return "DW_TAG_string_type";
5344     case DW_TAG_structure_type:
5345       return "DW_TAG_structure_type";
5346     case DW_TAG_subroutine_type:
5347       return "DW_TAG_subroutine_type";
5348     case DW_TAG_typedef:
5349       return "DW_TAG_typedef";
5350     case DW_TAG_union_type:
5351       return "DW_TAG_union_type";
5352     case DW_TAG_unspecified_parameters:
5353       return "DW_TAG_unspecified_parameters";
5354     case DW_TAG_variant:
5355       return "DW_TAG_variant";
5356     case DW_TAG_common_block:
5357       return "DW_TAG_common_block";
5358     case DW_TAG_common_inclusion:
5359       return "DW_TAG_common_inclusion";
5360     case DW_TAG_inheritance:
5361       return "DW_TAG_inheritance";
5362     case DW_TAG_inlined_subroutine:
5363       return "DW_TAG_inlined_subroutine";
5364     case DW_TAG_module:
5365       return "DW_TAG_module";
5366     case DW_TAG_ptr_to_member_type:
5367       return "DW_TAG_ptr_to_member_type";
5368     case DW_TAG_set_type:
5369       return "DW_TAG_set_type";
5370     case DW_TAG_subrange_type:
5371       return "DW_TAG_subrange_type";
5372     case DW_TAG_with_stmt:
5373       return "DW_TAG_with_stmt";
5374     case DW_TAG_access_declaration:
5375       return "DW_TAG_access_declaration";
5376     case DW_TAG_base_type:
5377       return "DW_TAG_base_type";
5378     case DW_TAG_catch_block:
5379       return "DW_TAG_catch_block";
5380     case DW_TAG_const_type:
5381       return "DW_TAG_const_type";
5382     case DW_TAG_constant:
5383       return "DW_TAG_constant";
5384     case DW_TAG_enumerator:
5385       return "DW_TAG_enumerator";
5386     case DW_TAG_file_type:
5387       return "DW_TAG_file_type";
5388     case DW_TAG_friend:
5389       return "DW_TAG_friend";
5390     case DW_TAG_namelist:
5391       return "DW_TAG_namelist";
5392     case DW_TAG_namelist_item:
5393       return "DW_TAG_namelist_item";
5394     case DW_TAG_packed_type:
5395       return "DW_TAG_packed_type";
5396     case DW_TAG_subprogram:
5397       return "DW_TAG_subprogram";
5398     case DW_TAG_template_type_param:
5399       return "DW_TAG_template_type_param";
5400     case DW_TAG_template_value_param:
5401       return "DW_TAG_template_value_param";
5402     case DW_TAG_thrown_type:
5403       return "DW_TAG_thrown_type";
5404     case DW_TAG_try_block:
5405       return "DW_TAG_try_block";
5406     case DW_TAG_variant_part:
5407       return "DW_TAG_variant_part";
5408     case DW_TAG_variable:
5409       return "DW_TAG_variable";
5410     case DW_TAG_volatile_type:
5411       return "DW_TAG_volatile_type";
5412     case DW_TAG_dwarf_procedure:
5413       return "DW_TAG_dwarf_procedure";
5414     case DW_TAG_restrict_type:
5415       return "DW_TAG_restrict_type";
5416     case DW_TAG_interface_type:
5417       return "DW_TAG_interface_type";
5418     case DW_TAG_namespace:
5419       return "DW_TAG_namespace";
5420     case DW_TAG_imported_module:
5421       return "DW_TAG_imported_module";
5422     case DW_TAG_unspecified_type:
5423       return "DW_TAG_unspecified_type";
5424     case DW_TAG_partial_unit:
5425       return "DW_TAG_partial_unit";
5426     case DW_TAG_imported_unit:
5427       return "DW_TAG_imported_unit";
5428     case DW_TAG_condition:
5429       return "DW_TAG_condition";
5430     case DW_TAG_shared_type:
5431       return "DW_TAG_shared_type";
5432     case DW_TAG_MIPS_loop:
5433       return "DW_TAG_MIPS_loop";
5434     case DW_TAG_format_label:
5435       return "DW_TAG_format_label";
5436     case DW_TAG_function_template:
5437       return "DW_TAG_function_template";
5438     case DW_TAG_class_template:
5439       return "DW_TAG_class_template";
5440     case DW_TAG_GNU_BINCL:
5441       return "DW_TAG_GNU_BINCL";
5442     case DW_TAG_GNU_EINCL:
5443       return "DW_TAG_GNU_EINCL";
5444     default:
5445       return "DW_TAG_<unknown>";
5446     }
5447 }
5448
5449 /* Convert a DWARF attribute code into its string name.  */
5450
5451 static const char *
5452 dwarf_attr_name (unsigned int attr)
5453 {
5454   switch (attr)
5455     {
5456     case DW_AT_sibling:
5457       return "DW_AT_sibling";
5458     case DW_AT_location:
5459       return "DW_AT_location";
5460     case DW_AT_name:
5461       return "DW_AT_name";
5462     case DW_AT_ordering:
5463       return "DW_AT_ordering";
5464     case DW_AT_subscr_data:
5465       return "DW_AT_subscr_data";
5466     case DW_AT_byte_size:
5467       return "DW_AT_byte_size";
5468     case DW_AT_bit_offset:
5469       return "DW_AT_bit_offset";
5470     case DW_AT_bit_size:
5471       return "DW_AT_bit_size";
5472     case DW_AT_element_list:
5473       return "DW_AT_element_list";
5474     case DW_AT_stmt_list:
5475       return "DW_AT_stmt_list";
5476     case DW_AT_low_pc:
5477       return "DW_AT_low_pc";
5478     case DW_AT_high_pc:
5479       return "DW_AT_high_pc";
5480     case DW_AT_language:
5481       return "DW_AT_language";
5482     case DW_AT_member:
5483       return "DW_AT_member";
5484     case DW_AT_discr:
5485       return "DW_AT_discr";
5486     case DW_AT_discr_value:
5487       return "DW_AT_discr_value";
5488     case DW_AT_visibility:
5489       return "DW_AT_visibility";
5490     case DW_AT_import:
5491       return "DW_AT_import";
5492     case DW_AT_string_length:
5493       return "DW_AT_string_length";
5494     case DW_AT_common_reference:
5495       return "DW_AT_common_reference";
5496     case DW_AT_comp_dir:
5497       return "DW_AT_comp_dir";
5498     case DW_AT_const_value:
5499       return "DW_AT_const_value";
5500     case DW_AT_containing_type:
5501       return "DW_AT_containing_type";
5502     case DW_AT_default_value:
5503       return "DW_AT_default_value";
5504     case DW_AT_inline:
5505       return "DW_AT_inline";
5506     case DW_AT_is_optional:
5507       return "DW_AT_is_optional";
5508     case DW_AT_lower_bound:
5509       return "DW_AT_lower_bound";
5510     case DW_AT_producer:
5511       return "DW_AT_producer";
5512     case DW_AT_prototyped:
5513       return "DW_AT_prototyped";
5514     case DW_AT_return_addr:
5515       return "DW_AT_return_addr";
5516     case DW_AT_start_scope:
5517       return "DW_AT_start_scope";
5518     case DW_AT_bit_stride:
5519       return "DW_AT_bit_stride";
5520     case DW_AT_upper_bound:
5521       return "DW_AT_upper_bound";
5522     case DW_AT_abstract_origin:
5523       return "DW_AT_abstract_origin";
5524     case DW_AT_accessibility:
5525       return "DW_AT_accessibility";
5526     case DW_AT_address_class:
5527       return "DW_AT_address_class";
5528     case DW_AT_artificial:
5529       return "DW_AT_artificial";
5530     case DW_AT_base_types:
5531       return "DW_AT_base_types";
5532     case DW_AT_calling_convention:
5533       return "DW_AT_calling_convention";
5534     case DW_AT_count:
5535       return "DW_AT_count";
5536     case DW_AT_data_member_location:
5537       return "DW_AT_data_member_location";
5538     case DW_AT_decl_column:
5539       return "DW_AT_decl_column";
5540     case DW_AT_decl_file:
5541       return "DW_AT_decl_file";
5542     case DW_AT_decl_line:
5543       return "DW_AT_decl_line";
5544     case DW_AT_declaration:
5545       return "DW_AT_declaration";
5546     case DW_AT_discr_list:
5547       return "DW_AT_discr_list";
5548     case DW_AT_encoding:
5549       return "DW_AT_encoding";
5550     case DW_AT_external:
5551       return "DW_AT_external";
5552     case DW_AT_frame_base:
5553       return "DW_AT_frame_base";
5554     case DW_AT_friend:
5555       return "DW_AT_friend";
5556     case DW_AT_identifier_case:
5557       return "DW_AT_identifier_case";
5558     case DW_AT_macro_info:
5559       return "DW_AT_macro_info";
5560     case DW_AT_namelist_items:
5561       return "DW_AT_namelist_items";
5562     case DW_AT_priority:
5563       return "DW_AT_priority";
5564     case DW_AT_segment:
5565       return "DW_AT_segment";
5566     case DW_AT_specification:
5567       return "DW_AT_specification";
5568     case DW_AT_static_link:
5569       return "DW_AT_static_link";
5570     case DW_AT_type:
5571       return "DW_AT_type";
5572     case DW_AT_use_location:
5573       return "DW_AT_use_location";
5574     case DW_AT_variable_parameter:
5575       return "DW_AT_variable_parameter";
5576     case DW_AT_virtuality:
5577       return "DW_AT_virtuality";
5578     case DW_AT_vtable_elem_location:
5579       return "DW_AT_vtable_elem_location";
5580
5581     case DW_AT_allocated:
5582       return "DW_AT_allocated";
5583     case DW_AT_associated:
5584       return "DW_AT_associated";
5585     case DW_AT_data_location:
5586       return "DW_AT_data_location";
5587     case DW_AT_byte_stride:
5588       return "DW_AT_byte_stride";
5589     case DW_AT_entry_pc:
5590       return "DW_AT_entry_pc";
5591     case DW_AT_use_UTF8:
5592       return "DW_AT_use_UTF8";
5593     case DW_AT_extension:
5594       return "DW_AT_extension";
5595     case DW_AT_ranges:
5596       return "DW_AT_ranges";
5597     case DW_AT_trampoline:
5598       return "DW_AT_trampoline";
5599     case DW_AT_call_column:
5600       return "DW_AT_call_column";
5601     case DW_AT_call_file:
5602       return "DW_AT_call_file";
5603     case DW_AT_call_line:
5604       return "DW_AT_call_line";
5605
5606     case DW_AT_MIPS_fde:
5607       return "DW_AT_MIPS_fde";
5608     case DW_AT_MIPS_loop_begin:
5609       return "DW_AT_MIPS_loop_begin";
5610     case DW_AT_MIPS_tail_loop_begin:
5611       return "DW_AT_MIPS_tail_loop_begin";
5612     case DW_AT_MIPS_epilog_begin:
5613       return "DW_AT_MIPS_epilog_begin";
5614     case DW_AT_MIPS_loop_unroll_factor:
5615       return "DW_AT_MIPS_loop_unroll_factor";
5616     case DW_AT_MIPS_software_pipeline_depth:
5617       return "DW_AT_MIPS_software_pipeline_depth";
5618     case DW_AT_MIPS_linkage_name:
5619       return "DW_AT_MIPS_linkage_name";
5620     case DW_AT_MIPS_stride:
5621       return "DW_AT_MIPS_stride";
5622     case DW_AT_MIPS_abstract_name:
5623       return "DW_AT_MIPS_abstract_name";
5624     case DW_AT_MIPS_clone_origin:
5625       return "DW_AT_MIPS_clone_origin";
5626     case DW_AT_MIPS_has_inlines:
5627       return "DW_AT_MIPS_has_inlines";
5628
5629     case DW_AT_sf_names:
5630       return "DW_AT_sf_names";
5631     case DW_AT_src_info:
5632       return "DW_AT_src_info";
5633     case DW_AT_mac_info:
5634       return "DW_AT_mac_info";
5635     case DW_AT_src_coords:
5636       return "DW_AT_src_coords";
5637     case DW_AT_body_begin:
5638       return "DW_AT_body_begin";
5639     case DW_AT_body_end:
5640       return "DW_AT_body_end";
5641     case DW_AT_GNU_vector:
5642       return "DW_AT_GNU_vector";
5643
5644     case DW_AT_VMS_rtnbeg_pd_address:
5645       return "DW_AT_VMS_rtnbeg_pd_address";
5646
5647     default:
5648       return "DW_AT_<unknown>";
5649     }
5650 }
5651
5652 /* Convert a DWARF value form code into its string name.  */
5653
5654 static const char *
5655 dwarf_form_name (unsigned int form)
5656 {
5657   switch (form)
5658     {
5659     case DW_FORM_addr:
5660       return "DW_FORM_addr";
5661     case DW_FORM_block2:
5662       return "DW_FORM_block2";
5663     case DW_FORM_block4:
5664       return "DW_FORM_block4";
5665     case DW_FORM_data2:
5666       return "DW_FORM_data2";
5667     case DW_FORM_data4:
5668       return "DW_FORM_data4";
5669     case DW_FORM_data8:
5670       return "DW_FORM_data8";
5671     case DW_FORM_string:
5672       return "DW_FORM_string";
5673     case DW_FORM_block:
5674       return "DW_FORM_block";
5675     case DW_FORM_block1:
5676       return "DW_FORM_block1";
5677     case DW_FORM_data1:
5678       return "DW_FORM_data1";
5679     case DW_FORM_flag:
5680       return "DW_FORM_flag";
5681     case DW_FORM_sdata:
5682       return "DW_FORM_sdata";
5683     case DW_FORM_strp:
5684       return "DW_FORM_strp";
5685     case DW_FORM_udata:
5686       return "DW_FORM_udata";
5687     case DW_FORM_ref_addr:
5688       return "DW_FORM_ref_addr";
5689     case DW_FORM_ref1:
5690       return "DW_FORM_ref1";
5691     case DW_FORM_ref2:
5692       return "DW_FORM_ref2";
5693     case DW_FORM_ref4:
5694       return "DW_FORM_ref4";
5695     case DW_FORM_ref8:
5696       return "DW_FORM_ref8";
5697     case DW_FORM_ref_udata:
5698       return "DW_FORM_ref_udata";
5699     case DW_FORM_indirect:
5700       return "DW_FORM_indirect";
5701     default:
5702       return "DW_FORM_<unknown>";
5703     }
5704 }
5705 \f
5706 /* Determine the "ultimate origin" of a decl.  The decl may be an inlined
5707    instance of an inlined instance of a decl which is local to an inline
5708    function, so we have to trace all of the way back through the origin chain
5709    to find out what sort of node actually served as the original seed for the
5710    given block.  */
5711
5712 static tree
5713 decl_ultimate_origin (const_tree decl)
5714 {
5715   if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
5716     return NULL_TREE;
5717
5718   /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
5719      nodes in the function to point to themselves; ignore that if
5720      we're trying to output the abstract instance of this function.  */
5721   if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
5722     return NULL_TREE;
5723
5724   /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
5725      most distant ancestor, this should never happen.  */
5726   gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
5727
5728   return DECL_ABSTRACT_ORIGIN (decl);
5729 }
5730
5731 /* Determine the "ultimate origin" of a block.  The block may be an inlined
5732    instance of an inlined instance of a block which is local to an inline
5733    function, so we have to trace all of the way back through the origin chain
5734    to find out what sort of node actually served as the original seed for the
5735    given block.  */
5736
5737 static tree
5738 block_ultimate_origin (const_tree block)
5739 {
5740   tree immediate_origin = BLOCK_ABSTRACT_ORIGIN (block);
5741
5742   /* output_inline_function sets BLOCK_ABSTRACT_ORIGIN for all the
5743      nodes in the function to point to themselves; ignore that if
5744      we're trying to output the abstract instance of this function.  */
5745   if (BLOCK_ABSTRACT (block) && immediate_origin == block)
5746     return NULL_TREE;
5747
5748   if (immediate_origin == NULL_TREE)
5749     return NULL_TREE;
5750   else
5751     {
5752       tree ret_val;
5753       tree lookahead = immediate_origin;
5754
5755       do
5756         {
5757           ret_val = lookahead;
5758           lookahead = (TREE_CODE (ret_val) == BLOCK
5759                        ? BLOCK_ABSTRACT_ORIGIN (ret_val) : NULL);
5760         }
5761       while (lookahead != NULL && lookahead != ret_val);
5762
5763       /* The block's abstract origin chain may not be the *ultimate* origin of
5764          the block. It could lead to a DECL that has an abstract origin set.
5765          If so, we want that DECL's abstract origin (which is what DECL_ORIGIN
5766          will give us if it has one).  Note that DECL's abstract origins are
5767          supposed to be the most distant ancestor (or so decl_ultimate_origin
5768          claims), so we don't need to loop following the DECL origins.  */
5769       if (DECL_P (ret_val))
5770         return DECL_ORIGIN (ret_val);
5771
5772       return ret_val;
5773     }
5774 }
5775
5776 /* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
5777    of a virtual function may refer to a base class, so we check the 'this'
5778    parameter.  */
5779
5780 static tree
5781 decl_class_context (tree decl)
5782 {
5783   tree context = NULL_TREE;
5784
5785   if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
5786     context = DECL_CONTEXT (decl);
5787   else
5788     context = TYPE_MAIN_VARIANT
5789       (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
5790
5791   if (context && !TYPE_P (context))
5792     context = NULL_TREE;
5793
5794   return context;
5795 }
5796 \f
5797 /* Add an attribute/value pair to a DIE.  */
5798
5799 static inline void
5800 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
5801 {
5802   /* Maybe this should be an assert?  */
5803   if (die == NULL)
5804     return;
5805
5806   if (die->die_attr == NULL)
5807     die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
5808   VEC_safe_push (dw_attr_node, gc, die->die_attr, attr);
5809 }
5810
5811 static inline enum dw_val_class
5812 AT_class (dw_attr_ref a)
5813 {
5814   return a->dw_attr_val.val_class;
5815 }
5816
5817 /* Add a flag value attribute to a DIE.  */
5818
5819 static inline void
5820 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
5821 {
5822   dw_attr_node attr;
5823
5824   attr.dw_attr = attr_kind;
5825   attr.dw_attr_val.val_class = dw_val_class_flag;
5826   attr.dw_attr_val.v.val_flag = flag;
5827   add_dwarf_attr (die, &attr);
5828 }
5829
5830 static inline unsigned
5831 AT_flag (dw_attr_ref a)
5832 {
5833   gcc_assert (a && AT_class (a) == dw_val_class_flag);
5834   return a->dw_attr_val.v.val_flag;
5835 }
5836
5837 /* Add a signed integer attribute value to a DIE.  */
5838
5839 static inline void
5840 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
5841 {
5842   dw_attr_node attr;
5843
5844   attr.dw_attr = attr_kind;
5845   attr.dw_attr_val.val_class = dw_val_class_const;
5846   attr.dw_attr_val.v.val_int = int_val;
5847   add_dwarf_attr (die, &attr);
5848 }
5849
5850 static inline HOST_WIDE_INT
5851 AT_int (dw_attr_ref a)
5852 {
5853   gcc_assert (a && AT_class (a) == dw_val_class_const);
5854   return a->dw_attr_val.v.val_int;
5855 }
5856
5857 /* Add an unsigned integer attribute value to a DIE.  */
5858
5859 static inline void
5860 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
5861                  unsigned HOST_WIDE_INT unsigned_val)
5862 {
5863   dw_attr_node attr;
5864
5865   attr.dw_attr = attr_kind;
5866   attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
5867   attr.dw_attr_val.v.val_unsigned = unsigned_val;
5868   add_dwarf_attr (die, &attr);
5869 }
5870
5871 static inline unsigned HOST_WIDE_INT
5872 AT_unsigned (dw_attr_ref a)
5873 {
5874   gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
5875   return a->dw_attr_val.v.val_unsigned;
5876 }
5877
5878 /* Add an unsigned double integer attribute value to a DIE.  */
5879
5880 static inline void
5881 add_AT_long_long (dw_die_ref die, enum dwarf_attribute attr_kind,
5882                   long unsigned int val_hi, long unsigned int val_low)
5883 {
5884   dw_attr_node attr;
5885
5886   attr.dw_attr = attr_kind;
5887   attr.dw_attr_val.val_class = dw_val_class_long_long;
5888   attr.dw_attr_val.v.val_long_long.hi = val_hi;
5889   attr.dw_attr_val.v.val_long_long.low = val_low;
5890   add_dwarf_attr (die, &attr);
5891 }
5892
5893 /* Add a floating point attribute value to a DIE and return it.  */
5894
5895 static inline void
5896 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
5897             unsigned int length, unsigned int elt_size, unsigned char *array)
5898 {
5899   dw_attr_node attr;
5900
5901   attr.dw_attr = attr_kind;
5902   attr.dw_attr_val.val_class = dw_val_class_vec;
5903   attr.dw_attr_val.v.val_vec.length = length;
5904   attr.dw_attr_val.v.val_vec.elt_size = elt_size;
5905   attr.dw_attr_val.v.val_vec.array = array;
5906   add_dwarf_attr (die, &attr);
5907 }
5908
5909 /* Hash and equality functions for debug_str_hash.  */
5910
5911 static hashval_t
5912 debug_str_do_hash (const void *x)
5913 {
5914   return htab_hash_string (((const struct indirect_string_node *)x)->str);
5915 }
5916
5917 static int
5918 debug_str_eq (const void *x1, const void *x2)
5919 {
5920   return strcmp ((((const struct indirect_string_node *)x1)->str),
5921                  (const char *)x2) == 0;
5922 }
5923
5924 /* Add a string attribute value to a DIE.  */
5925
5926 static inline void
5927 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
5928 {
5929   dw_attr_node attr;
5930   struct indirect_string_node *node;
5931   void **slot;
5932
5933   if (! debug_str_hash)
5934     debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
5935                                       debug_str_eq, NULL);
5936
5937   slot = htab_find_slot_with_hash (debug_str_hash, str,
5938                                    htab_hash_string (str), INSERT);
5939   if (*slot == NULL)
5940     {
5941       node = (struct indirect_string_node *)
5942                ggc_alloc_cleared (sizeof (struct indirect_string_node));
5943       node->str = ggc_strdup (str);
5944       *slot = node;
5945     }
5946   else
5947     node = (struct indirect_string_node *) *slot;
5948
5949   node->refcount++;
5950
5951   attr.dw_attr = attr_kind;
5952   attr.dw_attr_val.val_class = dw_val_class_str;
5953   attr.dw_attr_val.v.val_str = node;
5954   add_dwarf_attr (die, &attr);
5955 }
5956
5957 static inline const char *
5958 AT_string (dw_attr_ref a)
5959 {
5960   gcc_assert (a && AT_class (a) == dw_val_class_str);
5961   return a->dw_attr_val.v.val_str->str;
5962 }
5963
5964 /* Find out whether a string should be output inline in DIE
5965    or out-of-line in .debug_str section.  */
5966
5967 static int
5968 AT_string_form (dw_attr_ref a)
5969 {
5970   struct indirect_string_node *node;
5971   unsigned int len;
5972   char label[32];
5973
5974   gcc_assert (a && AT_class (a) == dw_val_class_str);
5975
5976   node = a->dw_attr_val.v.val_str;
5977   if (node->form)
5978     return node->form;
5979
5980   len = strlen (node->str) + 1;
5981
5982   /* If the string is shorter or equal to the size of the reference, it is
5983      always better to put it inline.  */
5984   if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
5985     return node->form = DW_FORM_string;
5986
5987   /* If we cannot expect the linker to merge strings in .debug_str
5988      section, only put it into .debug_str if it is worth even in this
5989      single module.  */
5990   if ((debug_str_section->common.flags & SECTION_MERGE) == 0
5991       && (len - DWARF_OFFSET_SIZE) * node->refcount <= len)
5992     return node->form = DW_FORM_string;
5993
5994   ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
5995   ++dw2_string_counter;
5996   node->label = xstrdup (label);
5997
5998   return node->form = DW_FORM_strp;
5999 }
6000
6001 /* Add a DIE reference attribute value to a DIE.  */
6002
6003 static inline void
6004 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
6005 {
6006   dw_attr_node attr;
6007
6008   attr.dw_attr = attr_kind;
6009   attr.dw_attr_val.val_class = dw_val_class_die_ref;
6010   attr.dw_attr_val.v.val_die_ref.die = targ_die;
6011   attr.dw_attr_val.v.val_die_ref.external = 0;
6012   add_dwarf_attr (die, &attr);
6013 }
6014
6015 /* Add an AT_specification attribute to a DIE, and also make the back
6016    pointer from the specification to the definition.  */
6017
6018 static inline void
6019 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
6020 {
6021   add_AT_die_ref (die, DW_AT_specification, targ_die);
6022   gcc_assert (!targ_die->die_definition);
6023   targ_die->die_definition = die;
6024 }
6025
6026 static inline dw_die_ref
6027 AT_ref (dw_attr_ref a)
6028 {
6029   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
6030   return a->dw_attr_val.v.val_die_ref.die;
6031 }
6032
6033 static inline int
6034 AT_ref_external (dw_attr_ref a)
6035 {
6036   if (a && AT_class (a) == dw_val_class_die_ref)
6037     return a->dw_attr_val.v.val_die_ref.external;
6038
6039   return 0;
6040 }
6041
6042 static inline void
6043 set_AT_ref_external (dw_attr_ref a, int i)
6044 {
6045   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
6046   a->dw_attr_val.v.val_die_ref.external = i;
6047 }
6048
6049 /* Add an FDE reference attribute value to a DIE.  */
6050
6051 static inline void
6052 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
6053 {
6054   dw_attr_node attr;
6055
6056   attr.dw_attr = attr_kind;
6057   attr.dw_attr_val.val_class = dw_val_class_fde_ref;
6058   attr.dw_attr_val.v.val_fde_index = targ_fde;
6059   add_dwarf_attr (die, &attr);
6060 }
6061
6062 /* Add a location description attribute value to a DIE.  */
6063
6064 static inline void
6065 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
6066 {
6067   dw_attr_node attr;
6068
6069   attr.dw_attr = attr_kind;
6070   attr.dw_attr_val.val_class = dw_val_class_loc;
6071   attr.dw_attr_val.v.val_loc = loc;
6072   add_dwarf_attr (die, &attr);
6073 }
6074
6075 static inline dw_loc_descr_ref
6076 AT_loc (dw_attr_ref a)
6077 {
6078   gcc_assert (a && AT_class (a) == dw_val_class_loc);
6079   return a->dw_attr_val.v.val_loc;
6080 }
6081
6082 static inline void
6083 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
6084 {
6085   dw_attr_node attr;
6086
6087   attr.dw_attr = attr_kind;
6088   attr.dw_attr_val.val_class = dw_val_class_loc_list;
6089   attr.dw_attr_val.v.val_loc_list = loc_list;
6090   add_dwarf_attr (die, &attr);
6091   have_location_lists = true;
6092 }
6093
6094 static inline dw_loc_list_ref
6095 AT_loc_list (dw_attr_ref a)
6096 {
6097   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
6098   return a->dw_attr_val.v.val_loc_list;
6099 }
6100
6101 /* Add an address constant attribute value to a DIE.  */
6102
6103 static inline void
6104 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
6105 {
6106   dw_attr_node attr;
6107
6108   attr.dw_attr = attr_kind;
6109   attr.dw_attr_val.val_class = dw_val_class_addr;
6110   attr.dw_attr_val.v.val_addr = addr;
6111   add_dwarf_attr (die, &attr);
6112 }
6113
6114 /* Get the RTX from to an address DIE attribute.  */
6115
6116 static inline rtx
6117 AT_addr (dw_attr_ref a)
6118 {
6119   gcc_assert (a && AT_class (a) == dw_val_class_addr);
6120   return a->dw_attr_val.v.val_addr;
6121 }
6122
6123 /* Add a file attribute value to a DIE.  */
6124
6125 static inline void
6126 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
6127              struct dwarf_file_data *fd)
6128 {
6129   dw_attr_node attr;
6130
6131   attr.dw_attr = attr_kind;
6132   attr.dw_attr_val.val_class = dw_val_class_file;
6133   attr.dw_attr_val.v.val_file = fd;
6134   add_dwarf_attr (die, &attr);
6135 }
6136
6137 /* Get the dwarf_file_data from a file DIE attribute.  */
6138
6139 static inline struct dwarf_file_data *
6140 AT_file (dw_attr_ref a)
6141 {
6142   gcc_assert (a && AT_class (a) == dw_val_class_file);
6143   return a->dw_attr_val.v.val_file;
6144 }
6145
6146 /* Add a label identifier attribute value to a DIE.  */
6147
6148 static inline void
6149 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
6150 {
6151   dw_attr_node attr;
6152
6153   attr.dw_attr = attr_kind;
6154   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
6155   attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
6156   add_dwarf_attr (die, &attr);
6157 }
6158
6159 /* Add a section offset attribute value to a DIE, an offset into the
6160    debug_line section.  */
6161
6162 static inline void
6163 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
6164                 const char *label)
6165 {
6166   dw_attr_node attr;
6167
6168   attr.dw_attr = attr_kind;
6169   attr.dw_attr_val.val_class = dw_val_class_lineptr;
6170   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
6171   add_dwarf_attr (die, &attr);
6172 }
6173
6174 /* Add a section offset attribute value to a DIE, an offset into the
6175    debug_macinfo section.  */
6176
6177 static inline void
6178 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
6179                const char *label)
6180 {
6181   dw_attr_node attr;
6182
6183   attr.dw_attr = attr_kind;
6184   attr.dw_attr_val.val_class = dw_val_class_macptr;
6185   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
6186   add_dwarf_attr (die, &attr);
6187 }
6188
6189 /* Add an offset attribute value to a DIE.  */
6190
6191 static inline void
6192 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
6193                unsigned HOST_WIDE_INT offset)
6194 {
6195   dw_attr_node attr;
6196
6197   attr.dw_attr = attr_kind;
6198   attr.dw_attr_val.val_class = dw_val_class_offset;
6199   attr.dw_attr_val.v.val_offset = offset;
6200   add_dwarf_attr (die, &attr);
6201 }
6202
6203 /* Add an range_list attribute value to a DIE.  */
6204
6205 static void
6206 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
6207                    long unsigned int offset)
6208 {
6209   dw_attr_node attr;
6210
6211   attr.dw_attr = attr_kind;
6212   attr.dw_attr_val.val_class = dw_val_class_range_list;
6213   attr.dw_attr_val.v.val_offset = offset;
6214   add_dwarf_attr (die, &attr);
6215 }
6216
6217 static inline const char *
6218 AT_lbl (dw_attr_ref a)
6219 {
6220   gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
6221                     || AT_class (a) == dw_val_class_lineptr
6222                     || AT_class (a) == dw_val_class_macptr));
6223   return a->dw_attr_val.v.val_lbl_id;
6224 }
6225
6226 /* Get the attribute of type attr_kind.  */
6227
6228 static dw_attr_ref
6229 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
6230 {
6231   dw_attr_ref a;
6232   unsigned ix;
6233   dw_die_ref spec = NULL;
6234
6235   if (! die)
6236     return NULL;
6237
6238   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6239     if (a->dw_attr == attr_kind)
6240       return a;
6241     else if (a->dw_attr == DW_AT_specification
6242              || a->dw_attr == DW_AT_abstract_origin)
6243       spec = AT_ref (a);
6244
6245   if (spec)
6246     return get_AT (spec, attr_kind);
6247
6248   return NULL;
6249 }
6250
6251 /* Return the "low pc" attribute value, typically associated with a subprogram
6252    DIE.  Return null if the "low pc" attribute is either not present, or if it
6253    cannot be represented as an assembler label identifier.  */
6254
6255 static inline const char *
6256 get_AT_low_pc (dw_die_ref die)
6257 {
6258   dw_attr_ref a = get_AT (die, DW_AT_low_pc);
6259
6260   return a ? AT_lbl (a) : NULL;
6261 }
6262
6263 /* Return the "high pc" attribute value, typically associated with a subprogram
6264    DIE.  Return null if the "high pc" attribute is either not present, or if it
6265    cannot be represented as an assembler label identifier.  */
6266
6267 static inline const char *
6268 get_AT_hi_pc (dw_die_ref die)
6269 {
6270   dw_attr_ref a = get_AT (die, DW_AT_high_pc);
6271
6272   return a ? AT_lbl (a) : NULL;
6273 }
6274
6275 /* Return the value of the string attribute designated by ATTR_KIND, or
6276    NULL if it is not present.  */
6277
6278 static inline const char *
6279 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
6280 {
6281   dw_attr_ref a = get_AT (die, attr_kind);
6282
6283   return a ? AT_string (a) : NULL;
6284 }
6285
6286 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
6287    if it is not present.  */
6288
6289 static inline int
6290 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
6291 {
6292   dw_attr_ref a = get_AT (die, attr_kind);
6293
6294   return a ? AT_flag (a) : 0;
6295 }
6296
6297 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
6298    if it is not present.  */
6299
6300 static inline unsigned
6301 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
6302 {
6303   dw_attr_ref a = get_AT (die, attr_kind);
6304
6305   return a ? AT_unsigned (a) : 0;
6306 }
6307
6308 static inline dw_die_ref
6309 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
6310 {
6311   dw_attr_ref a = get_AT (die, attr_kind);
6312
6313   return a ? AT_ref (a) : NULL;
6314 }
6315
6316 static inline struct dwarf_file_data *
6317 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
6318 {
6319   dw_attr_ref a = get_AT (die, attr_kind);
6320
6321   return a ? AT_file (a) : NULL;
6322 }
6323
6324 /* Return TRUE if the language is C or C++.  */
6325
6326 static inline bool
6327 is_c_family (void)
6328 {
6329   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
6330
6331   return (lang == DW_LANG_C || lang == DW_LANG_C89 || lang == DW_LANG_ObjC
6332           || lang == DW_LANG_C99
6333           || lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus);
6334 }
6335
6336 /* Return TRUE if the language is C++.  */
6337
6338 static inline bool
6339 is_cxx (void)
6340 {
6341   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
6342
6343   return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
6344 }
6345
6346 /* Return TRUE if the language is Fortran.  */
6347
6348 static inline bool
6349 is_fortran (void)
6350 {
6351   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
6352
6353   return (lang == DW_LANG_Fortran77
6354           || lang == DW_LANG_Fortran90
6355           || lang == DW_LANG_Fortran95);
6356 }
6357
6358 /* Return TRUE if the language is Java.  */
6359
6360 static inline bool
6361 is_java (void)
6362 {
6363   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
6364
6365   return lang == DW_LANG_Java;
6366 }
6367
6368 /* Return TRUE if the language is Ada.  */
6369
6370 static inline bool
6371 is_ada (void)
6372 {
6373   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
6374
6375   return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
6376 }
6377
6378 /* Remove the specified attribute if present.  */
6379
6380 static void
6381 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
6382 {
6383   dw_attr_ref a;
6384   unsigned ix;
6385
6386   if (! die)
6387     return;
6388
6389   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6390     if (a->dw_attr == attr_kind)
6391       {
6392         if (AT_class (a) == dw_val_class_str)
6393           if (a->dw_attr_val.v.val_str->refcount)
6394             a->dw_attr_val.v.val_str->refcount--;
6395
6396         /* VEC_ordered_remove should help reduce the number of abbrevs
6397            that are needed.  */
6398         VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
6399         return;
6400       }
6401 }
6402
6403 /* Remove CHILD from its parent.  PREV must have the property that
6404    PREV->DIE_SIB == CHILD.  Does not alter CHILD.  */
6405
6406 static void
6407 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
6408 {
6409   gcc_assert (child->die_parent == prev->die_parent);
6410   gcc_assert (prev->die_sib == child);
6411   if (prev == child)
6412     {
6413       gcc_assert (child->die_parent->die_child == child);
6414       prev = NULL;
6415     }
6416   else
6417     prev->die_sib = child->die_sib;
6418   if (child->die_parent->die_child == child)
6419     child->die_parent->die_child = prev;
6420 }
6421
6422 /* Remove child DIE whose die_tag is TAG.  Do nothing if no child
6423    matches TAG.  */
6424
6425 static void
6426 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
6427 {
6428   dw_die_ref c;
6429
6430   c = die->die_child;
6431   if (c) do {
6432     dw_die_ref prev = c;
6433     c = c->die_sib;
6434     while (c->die_tag == tag)
6435       {
6436         remove_child_with_prev (c, prev);
6437         /* Might have removed every child.  */
6438         if (c == c->die_sib)
6439           return;
6440         c = c->die_sib;
6441       }
6442   } while (c != die->die_child);
6443 }
6444
6445 /* Add a CHILD_DIE as the last child of DIE.  */
6446
6447 static void
6448 add_child_die (dw_die_ref die, dw_die_ref child_die)
6449 {
6450   /* FIXME this should probably be an assert.  */
6451   if (! die || ! child_die)
6452     return;
6453   gcc_assert (die != child_die);
6454
6455   child_die->die_parent = die;
6456   if (die->die_child)
6457     {
6458       child_die->die_sib = die->die_child->die_sib;
6459       die->die_child->die_sib = child_die;
6460     }
6461   else
6462     child_die->die_sib = child_die;
6463   die->die_child = child_die;
6464 }
6465
6466 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
6467    is the specification, to the end of PARENT's list of children.
6468    This is done by removing and re-adding it.  */
6469
6470 static void
6471 splice_child_die (dw_die_ref parent, dw_die_ref child)
6472 {
6473   dw_die_ref p;
6474
6475   /* We want the declaration DIE from inside the class, not the
6476      specification DIE at toplevel.  */
6477   if (child->die_parent != parent)
6478     {
6479       dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
6480
6481       if (tmp)
6482         child = tmp;
6483     }
6484
6485   gcc_assert (child->die_parent == parent
6486               || (child->die_parent
6487                   == get_AT_ref (parent, DW_AT_specification)));
6488
6489   for (p = child->die_parent->die_child; ; p = p->die_sib)
6490     if (p->die_sib == child)
6491       {
6492         remove_child_with_prev (child, p);
6493         break;
6494       }
6495
6496   add_child_die (parent, child);
6497 }
6498
6499 /* Return a pointer to a newly created DIE node.  */
6500
6501 static inline dw_die_ref
6502 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
6503 {
6504   dw_die_ref die = GGC_CNEW (die_node);
6505
6506   die->die_tag = tag_value;
6507
6508   if (parent_die != NULL)
6509     add_child_die (parent_die, die);
6510   else
6511     {
6512       limbo_die_node *limbo_node;
6513
6514       limbo_node = GGC_CNEW (limbo_die_node);
6515       limbo_node->die = die;
6516       limbo_node->created_for = t;
6517       limbo_node->next = limbo_die_list;
6518       limbo_die_list = limbo_node;
6519     }
6520
6521   return die;
6522 }
6523
6524 /* Return the DIE associated with the given type specifier.  */
6525
6526 static inline dw_die_ref
6527 lookup_type_die (tree type)
6528 {
6529   return TYPE_SYMTAB_DIE (type);
6530 }
6531
6532 /* Equate a DIE to a given type specifier.  */
6533
6534 static inline void
6535 equate_type_number_to_die (tree type, dw_die_ref type_die)
6536 {
6537   TYPE_SYMTAB_DIE (type) = type_die;
6538 }
6539
6540 /* Returns a hash value for X (which really is a die_struct).  */
6541
6542 static hashval_t
6543 decl_die_table_hash (const void *x)
6544 {
6545   return (hashval_t) ((const_dw_die_ref) x)->decl_id;
6546 }
6547
6548 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
6549
6550 static int
6551 decl_die_table_eq (const void *x, const void *y)
6552 {
6553   return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
6554 }
6555
6556 /* Return the DIE associated with a given declaration.  */
6557
6558 static inline dw_die_ref
6559 lookup_decl_die (tree decl)
6560 {
6561   return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
6562 }
6563
6564 /* Returns a hash value for X (which really is a var_loc_list).  */
6565
6566 static hashval_t
6567 decl_loc_table_hash (const void *x)
6568 {
6569   return (hashval_t) ((const var_loc_list *) x)->decl_id;
6570 }
6571
6572 /* Return nonzero if decl_id of var_loc_list X is the same as
6573    UID of decl *Y.  */
6574
6575 static int
6576 decl_loc_table_eq (const void *x, const void *y)
6577 {
6578   return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
6579 }
6580
6581 /* Return the var_loc list associated with a given declaration.  */
6582
6583 static inline var_loc_list *
6584 lookup_decl_loc (const_tree decl)
6585 {
6586   return (var_loc_list *)
6587     htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
6588 }
6589
6590 /* Equate a DIE to a particular declaration.  */
6591
6592 static void
6593 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
6594 {
6595   unsigned int decl_id = DECL_UID (decl);
6596   void **slot;
6597
6598   slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
6599   *slot = decl_die;
6600   decl_die->decl_id = decl_id;
6601 }
6602
6603 /* Add a variable location node to the linked list for DECL.  */
6604
6605 static void
6606 add_var_loc_to_decl (tree decl, struct var_loc_node *loc)
6607 {
6608   unsigned int decl_id = DECL_UID (decl);
6609   var_loc_list *temp;
6610   void **slot;
6611
6612   slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
6613   if (*slot == NULL)
6614     {
6615       temp = GGC_CNEW (var_loc_list);
6616       temp->decl_id = decl_id;
6617       *slot = temp;
6618     }
6619   else
6620     temp = (var_loc_list *) *slot;
6621
6622   if (temp->last)
6623     {
6624       /* If the current location is the same as the end of the list,
6625          and either both or neither of the locations is uninitialized,
6626          we have nothing to do.  */
6627       if ((!rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->last->var_loc_note),
6628                          NOTE_VAR_LOCATION_LOC (loc->var_loc_note)))
6629           || ((NOTE_VAR_LOCATION_STATUS (temp->last->var_loc_note)
6630                != NOTE_VAR_LOCATION_STATUS (loc->var_loc_note))
6631               && ((NOTE_VAR_LOCATION_STATUS (temp->last->var_loc_note)
6632                    == VAR_INIT_STATUS_UNINITIALIZED)
6633                   || (NOTE_VAR_LOCATION_STATUS (loc->var_loc_note)
6634                       == VAR_INIT_STATUS_UNINITIALIZED))))
6635         {
6636           /* Add LOC to the end of list and update LAST.  */
6637           temp->last->next = loc;
6638           temp->last = loc;
6639         }
6640     }
6641   /* Do not add empty location to the beginning of the list.  */
6642   else if (NOTE_VAR_LOCATION_LOC (loc->var_loc_note) != NULL_RTX)
6643     {
6644       temp->first = loc;
6645       temp->last = loc;
6646     }
6647 }
6648 \f
6649 /* Keep track of the number of spaces used to indent the
6650    output of the debugging routines that print the structure of
6651    the DIE internal representation.  */
6652 static int print_indent;
6653
6654 /* Indent the line the number of spaces given by print_indent.  */
6655
6656 static inline void
6657 print_spaces (FILE *outfile)
6658 {
6659   fprintf (outfile, "%*s", print_indent, "");
6660 }
6661
6662 /* Print the information associated with a given DIE, and its children.
6663    This routine is a debugging aid only.  */
6664
6665 static void
6666 print_die (dw_die_ref die, FILE *outfile)
6667 {
6668   dw_attr_ref a;
6669   dw_die_ref c;
6670   unsigned ix;
6671
6672   print_spaces (outfile);
6673   fprintf (outfile, "DIE %4ld: %s\n",
6674            die->die_offset, dwarf_tag_name (die->die_tag));
6675   print_spaces (outfile);
6676   fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
6677   fprintf (outfile, " offset: %ld\n", die->die_offset);
6678
6679   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6680     {
6681       print_spaces (outfile);
6682       fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
6683
6684       switch (AT_class (a))
6685         {
6686         case dw_val_class_addr:
6687           fprintf (outfile, "address");
6688           break;
6689         case dw_val_class_offset:
6690           fprintf (outfile, "offset");
6691           break;
6692         case dw_val_class_loc:
6693           fprintf (outfile, "location descriptor");
6694           break;
6695         case dw_val_class_loc_list:
6696           fprintf (outfile, "location list -> label:%s",
6697                    AT_loc_list (a)->ll_symbol);
6698           break;
6699         case dw_val_class_range_list:
6700           fprintf (outfile, "range list");
6701           break;
6702         case dw_val_class_const:
6703           fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
6704           break;
6705         case dw_val_class_unsigned_const:
6706           fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
6707           break;
6708         case dw_val_class_long_long:
6709           fprintf (outfile, "constant (%lu,%lu)",
6710                    a->dw_attr_val.v.val_long_long.hi,
6711                    a->dw_attr_val.v.val_long_long.low);
6712           break;
6713         case dw_val_class_vec:
6714           fprintf (outfile, "floating-point or vector constant");
6715           break;
6716         case dw_val_class_flag:
6717           fprintf (outfile, "%u", AT_flag (a));
6718           break;
6719         case dw_val_class_die_ref:
6720           if (AT_ref (a) != NULL)
6721             {
6722               if (AT_ref (a)->die_symbol)
6723                 fprintf (outfile, "die -> label: %s", AT_ref (a)->die_symbol);
6724               else
6725                 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
6726             }
6727           else
6728             fprintf (outfile, "die -> <null>");
6729           break;
6730         case dw_val_class_lbl_id:
6731         case dw_val_class_lineptr:
6732         case dw_val_class_macptr:
6733           fprintf (outfile, "label: %s", AT_lbl (a));
6734           break;
6735         case dw_val_class_str:
6736           if (AT_string (a) != NULL)
6737             fprintf (outfile, "\"%s\"", AT_string (a));
6738           else
6739             fprintf (outfile, "<null>");
6740           break;
6741         case dw_val_class_file:
6742           fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
6743                    AT_file (a)->emitted_number);
6744           break;
6745         default:
6746           break;
6747         }
6748
6749       fprintf (outfile, "\n");
6750     }
6751
6752   if (die->die_child != NULL)
6753     {
6754       print_indent += 4;
6755       FOR_EACH_CHILD (die, c, print_die (c, outfile));
6756       print_indent -= 4;
6757     }
6758   if (print_indent == 0)
6759     fprintf (outfile, "\n");
6760 }
6761
6762 /* Print the contents of the source code line number correspondence table.
6763    This routine is a debugging aid only.  */
6764
6765 static void
6766 print_dwarf_line_table (FILE *outfile)
6767 {
6768   unsigned i;
6769   dw_line_info_ref line_info;
6770
6771   fprintf (outfile, "\n\nDWARF source line information\n");
6772   for (i = 1; i < line_info_table_in_use; i++)
6773     {
6774       line_info = &line_info_table[i];
6775       fprintf (outfile, "%5d: %4ld %6ld\n", i,
6776                line_info->dw_file_num,
6777                line_info->dw_line_num);
6778     }
6779
6780   fprintf (outfile, "\n\n");
6781 }
6782
6783 /* Print the information collected for a given DIE.  */
6784
6785 void
6786 debug_dwarf_die (dw_die_ref die)
6787 {
6788   print_die (die, stderr);
6789 }
6790
6791 /* Print all DWARF information collected for the compilation unit.
6792    This routine is a debugging aid only.  */
6793
6794 void
6795 debug_dwarf (void)
6796 {
6797   print_indent = 0;
6798   print_die (comp_unit_die, stderr);
6799   if (! DWARF2_ASM_LINE_DEBUG_INFO)
6800     print_dwarf_line_table (stderr);
6801 }
6802 \f
6803 /* Start a new compilation unit DIE for an include file.  OLD_UNIT is the CU
6804    for the enclosing include file, if any.  BINCL_DIE is the DW_TAG_GNU_BINCL
6805    DIE that marks the start of the DIEs for this include file.  */
6806
6807 static dw_die_ref
6808 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
6809 {
6810   const char *filename = get_AT_string (bincl_die, DW_AT_name);
6811   dw_die_ref new_unit = gen_compile_unit_die (filename);
6812
6813   new_unit->die_sib = old_unit;
6814   return new_unit;
6815 }
6816
6817 /* Close an include-file CU and reopen the enclosing one.  */
6818
6819 static dw_die_ref
6820 pop_compile_unit (dw_die_ref old_unit)
6821 {
6822   dw_die_ref new_unit = old_unit->die_sib;
6823
6824   old_unit->die_sib = NULL;
6825   return new_unit;
6826 }
6827
6828 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6829 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
6830
6831 /* Calculate the checksum of a location expression.  */
6832
6833 static inline void
6834 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6835 {
6836   CHECKSUM (loc->dw_loc_opc);
6837   CHECKSUM (loc->dw_loc_oprnd1);
6838   CHECKSUM (loc->dw_loc_oprnd2);
6839 }
6840
6841 /* Calculate the checksum of an attribute.  */
6842
6843 static void
6844 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
6845 {
6846   dw_loc_descr_ref loc;
6847   rtx r;
6848
6849   CHECKSUM (at->dw_attr);
6850
6851   /* We don't care that this was compiled with a different compiler
6852      snapshot; if the output is the same, that's what matters.  */
6853   if (at->dw_attr == DW_AT_producer)
6854     return;
6855
6856   switch (AT_class (at))
6857     {
6858     case dw_val_class_const:
6859       CHECKSUM (at->dw_attr_val.v.val_int);
6860       break;
6861     case dw_val_class_unsigned_const:
6862       CHECKSUM (at->dw_attr_val.v.val_unsigned);
6863       break;
6864     case dw_val_class_long_long:
6865       CHECKSUM (at->dw_attr_val.v.val_long_long);
6866       break;
6867     case dw_val_class_vec:
6868       CHECKSUM (at->dw_attr_val.v.val_vec);
6869       break;
6870     case dw_val_class_flag:
6871       CHECKSUM (at->dw_attr_val.v.val_flag);
6872       break;
6873     case dw_val_class_str:
6874       CHECKSUM_STRING (AT_string (at));
6875       break;
6876
6877     case dw_val_class_addr:
6878       r = AT_addr (at);
6879       gcc_assert (GET_CODE (r) == SYMBOL_REF);
6880       CHECKSUM_STRING (XSTR (r, 0));
6881       break;
6882
6883     case dw_val_class_offset:
6884       CHECKSUM (at->dw_attr_val.v.val_offset);
6885       break;
6886
6887     case dw_val_class_loc:
6888       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6889         loc_checksum (loc, ctx);
6890       break;
6891
6892     case dw_val_class_die_ref:
6893       die_checksum (AT_ref (at), ctx, mark);
6894       break;
6895
6896     case dw_val_class_fde_ref:
6897     case dw_val_class_lbl_id:
6898     case dw_val_class_lineptr:
6899     case dw_val_class_macptr:
6900       break;
6901
6902     case dw_val_class_file:
6903       CHECKSUM_STRING (AT_file (at)->filename);
6904       break;
6905
6906     default:
6907       break;
6908     }
6909 }
6910
6911 /* Calculate the checksum of a DIE.  */
6912
6913 static void
6914 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6915 {
6916   dw_die_ref c;
6917   dw_attr_ref a;
6918   unsigned ix;
6919
6920   /* To avoid infinite recursion.  */
6921   if (die->die_mark)
6922     {
6923       CHECKSUM (die->die_mark);
6924       return;
6925     }
6926   die->die_mark = ++(*mark);
6927
6928   CHECKSUM (die->die_tag);
6929
6930   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6931     attr_checksum (a, ctx, mark);
6932
6933   FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
6934 }
6935
6936 #undef CHECKSUM
6937 #undef CHECKSUM_STRING
6938
6939 /* Do the location expressions look same?  */
6940 static inline int
6941 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6942 {
6943   return loc1->dw_loc_opc == loc2->dw_loc_opc
6944          && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6945          && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6946 }
6947
6948 /* Do the values look the same?  */
6949 static int
6950 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6951 {
6952   dw_loc_descr_ref loc1, loc2;
6953   rtx r1, r2;
6954
6955   if (v1->val_class != v2->val_class)
6956     return 0;
6957
6958   switch (v1->val_class)
6959     {
6960     case dw_val_class_const:
6961       return v1->v.val_int == v2->v.val_int;
6962     case dw_val_class_unsigned_const:
6963       return v1->v.val_unsigned == v2->v.val_unsigned;
6964     case dw_val_class_long_long:
6965       return v1->v.val_long_long.hi == v2->v.val_long_long.hi
6966              && v1->v.val_long_long.low == v2->v.val_long_long.low;
6967     case dw_val_class_vec:
6968       if (v1->v.val_vec.length != v2->v.val_vec.length
6969           || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6970         return 0;
6971       if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6972                   v1->v.val_vec.length * v1->v.val_vec.elt_size))
6973         return 0;
6974       return 1;
6975     case dw_val_class_flag:
6976       return v1->v.val_flag == v2->v.val_flag;
6977     case dw_val_class_str:
6978       return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
6979
6980     case dw_val_class_addr:
6981       r1 = v1->v.val_addr;
6982       r2 = v2->v.val_addr;
6983       if (GET_CODE (r1) != GET_CODE (r2))
6984         return 0;
6985       gcc_assert (GET_CODE (r1) == SYMBOL_REF);
6986       return !strcmp (XSTR (r1, 0), XSTR (r2, 0));
6987
6988     case dw_val_class_offset:
6989       return v1->v.val_offset == v2->v.val_offset;
6990
6991     case dw_val_class_loc:
6992       for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6993            loc1 && loc2;
6994            loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6995         if (!same_loc_p (loc1, loc2, mark))
6996           return 0;
6997       return !loc1 && !loc2;
6998
6999     case dw_val_class_die_ref:
7000       return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
7001
7002     case dw_val_class_fde_ref:
7003     case dw_val_class_lbl_id:
7004     case dw_val_class_lineptr:
7005     case dw_val_class_macptr:
7006       return 1;
7007
7008     case dw_val_class_file:
7009       return v1->v.val_file == v2->v.val_file;
7010
7011     default:
7012       return 1;
7013     }
7014 }
7015
7016 /* Do the attributes look the same?  */
7017
7018 static int
7019 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
7020 {
7021   if (at1->dw_attr != at2->dw_attr)
7022     return 0;
7023
7024   /* We don't care that this was compiled with a different compiler
7025      snapshot; if the output is the same, that's what matters. */
7026   if (at1->dw_attr == DW_AT_producer)
7027     return 1;
7028
7029   return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
7030 }
7031
7032 /* Do the dies look the same?  */
7033
7034 static int
7035 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
7036 {
7037   dw_die_ref c1, c2;
7038   dw_attr_ref a1;
7039   unsigned ix;
7040
7041   /* To avoid infinite recursion.  */
7042   if (die1->die_mark)
7043     return die1->die_mark == die2->die_mark;
7044   die1->die_mark = die2->die_mark = ++(*mark);
7045
7046   if (die1->die_tag != die2->die_tag)
7047     return 0;
7048
7049   if (VEC_length (dw_attr_node, die1->die_attr)
7050       != VEC_length (dw_attr_node, die2->die_attr))
7051     return 0;
7052
7053   for (ix = 0; VEC_iterate (dw_attr_node, die1->die_attr, ix, a1); ix++)
7054     if (!same_attr_p (a1, VEC_index (dw_attr_node, die2->die_attr, ix), mark))
7055       return 0;
7056
7057   c1 = die1->die_child;
7058   c2 = die2->die_child;
7059   if (! c1)
7060     {
7061       if (c2)
7062         return 0;
7063     }
7064   else
7065     for (;;)
7066       {
7067         if (!same_die_p (c1, c2, mark))
7068           return 0;
7069         c1 = c1->die_sib;
7070         c2 = c2->die_sib;
7071         if (c1 == die1->die_child)
7072           {
7073             if (c2 == die2->die_child)
7074               break;
7075             else
7076               return 0;
7077           }
7078     }
7079
7080   return 1;
7081 }
7082
7083 /* Do the dies look the same?  Wrapper around same_die_p.  */
7084
7085 static int
7086 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
7087 {
7088   int mark = 0;
7089   int ret = same_die_p (die1, die2, &mark);
7090
7091   unmark_all_dies (die1);
7092   unmark_all_dies (die2);
7093
7094   return ret;
7095 }
7096
7097 /* The prefix to attach to symbols on DIEs in the current comdat debug
7098    info section.  */
7099 static char *comdat_symbol_id;
7100
7101 /* The index of the current symbol within the current comdat CU.  */
7102 static unsigned int comdat_symbol_number;
7103
7104 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
7105    children, and set comdat_symbol_id accordingly.  */
7106
7107 static void
7108 compute_section_prefix (dw_die_ref unit_die)
7109 {
7110   const char *die_name = get_AT_string (unit_die, DW_AT_name);
7111   const char *base = die_name ? lbasename (die_name) : "anonymous";
7112   char *name = XALLOCAVEC (char, strlen (base) + 64);
7113   char *p;
7114   int i, mark;
7115   unsigned char checksum[16];
7116   struct md5_ctx ctx;
7117
7118   /* Compute the checksum of the DIE, then append part of it as hex digits to
7119      the name filename of the unit.  */
7120
7121   md5_init_ctx (&ctx);
7122   mark = 0;
7123   die_checksum (unit_die, &ctx, &mark);
7124   unmark_all_dies (unit_die);
7125   md5_finish_ctx (&ctx, checksum);
7126
7127   sprintf (name, "%s.", base);
7128   clean_symbol_name (name);
7129
7130   p = name + strlen (name);
7131   for (i = 0; i < 4; i++)
7132     {
7133       sprintf (p, "%.2x", checksum[i]);
7134       p += 2;
7135     }
7136
7137   comdat_symbol_id = unit_die->die_symbol = xstrdup (name);
7138   comdat_symbol_number = 0;
7139 }
7140
7141 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P.  */
7142
7143 static int
7144 is_type_die (dw_die_ref die)
7145 {
7146   switch (die->die_tag)
7147     {
7148     case DW_TAG_array_type:
7149     case DW_TAG_class_type:
7150     case DW_TAG_interface_type:
7151     case DW_TAG_enumeration_type:
7152     case DW_TAG_pointer_type:
7153     case DW_TAG_reference_type:
7154     case DW_TAG_string_type:
7155     case DW_TAG_structure_type:
7156     case DW_TAG_subroutine_type:
7157     case DW_TAG_union_type:
7158     case DW_TAG_ptr_to_member_type:
7159     case DW_TAG_set_type:
7160     case DW_TAG_subrange_type:
7161     case DW_TAG_base_type:
7162     case DW_TAG_const_type:
7163     case DW_TAG_file_type:
7164     case DW_TAG_packed_type:
7165     case DW_TAG_volatile_type:
7166     case DW_TAG_typedef:
7167       return 1;
7168     default:
7169       return 0;
7170     }
7171 }
7172
7173 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
7174    Basically, we want to choose the bits that are likely to be shared between
7175    compilations (types) and leave out the bits that are specific to individual
7176    compilations (functions).  */
7177
7178 static int
7179 is_comdat_die (dw_die_ref c)
7180 {
7181   /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
7182      we do for stabs.  The advantage is a greater likelihood of sharing between
7183      objects that don't include headers in the same order (and therefore would
7184      put the base types in a different comdat).  jason 8/28/00 */
7185
7186   if (c->die_tag == DW_TAG_base_type)
7187     return 0;
7188
7189   if (c->die_tag == DW_TAG_pointer_type
7190       || c->die_tag == DW_TAG_reference_type
7191       || c->die_tag == DW_TAG_const_type
7192       || c->die_tag == DW_TAG_volatile_type)
7193     {
7194       dw_die_ref t = get_AT_ref (c, DW_AT_type);
7195
7196       return t ? is_comdat_die (t) : 0;
7197     }
7198
7199   return is_type_die (c);
7200 }
7201
7202 /* Returns 1 iff C is the sort of DIE that might be referred to from another
7203    compilation unit.  */
7204
7205 static int
7206 is_symbol_die (dw_die_ref c)
7207 {
7208   return (is_type_die (c)
7209           || (get_AT (c, DW_AT_declaration)
7210               && !get_AT (c, DW_AT_specification))
7211           || c->die_tag == DW_TAG_namespace
7212           || c->die_tag == DW_TAG_module);
7213 }
7214
7215 static char *
7216 gen_internal_sym (const char *prefix)
7217 {
7218   char buf[256];
7219
7220   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
7221   return xstrdup (buf);
7222 }
7223
7224 /* Assign symbols to all worthy DIEs under DIE.  */
7225
7226 static void
7227 assign_symbol_names (dw_die_ref die)
7228 {
7229   dw_die_ref c;
7230
7231   if (is_symbol_die (die))
7232     {
7233       if (comdat_symbol_id)
7234         {
7235           char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
7236
7237           sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
7238                    comdat_symbol_id, comdat_symbol_number++);
7239           die->die_symbol = xstrdup (p);
7240         }
7241       else
7242         die->die_symbol = gen_internal_sym ("LDIE");
7243     }
7244
7245   FOR_EACH_CHILD (die, c, assign_symbol_names (c));
7246 }
7247
7248 struct cu_hash_table_entry
7249 {
7250   dw_die_ref cu;
7251   unsigned min_comdat_num, max_comdat_num;
7252   struct cu_hash_table_entry *next;
7253 };
7254
7255 /* Routines to manipulate hash table of CUs.  */
7256 static hashval_t
7257 htab_cu_hash (const void *of)
7258 {
7259   const struct cu_hash_table_entry *const entry =
7260     (const struct cu_hash_table_entry *) of;
7261
7262   return htab_hash_string (entry->cu->die_symbol);
7263 }
7264
7265 static int
7266 htab_cu_eq (const void *of1, const void *of2)
7267 {
7268   const struct cu_hash_table_entry *const entry1 =
7269     (const struct cu_hash_table_entry *) of1;
7270   const struct die_struct *const entry2 = (const struct die_struct *) of2;
7271
7272   return !strcmp (entry1->cu->die_symbol, entry2->die_symbol);
7273 }
7274
7275 static void
7276 htab_cu_del (void *what)
7277 {
7278   struct cu_hash_table_entry *next,
7279     *entry = (struct cu_hash_table_entry *) what;
7280
7281   while (entry)
7282     {
7283       next = entry->next;
7284       free (entry);
7285       entry = next;
7286     }
7287 }
7288
7289 /* Check whether we have already seen this CU and set up SYM_NUM
7290    accordingly.  */
7291 static int
7292 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
7293 {
7294   struct cu_hash_table_entry dummy;
7295   struct cu_hash_table_entry **slot, *entry, *last = &dummy;
7296
7297   dummy.max_comdat_num = 0;
7298
7299   slot = (struct cu_hash_table_entry **)
7300     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
7301         INSERT);
7302   entry = *slot;
7303
7304   for (; entry; last = entry, entry = entry->next)
7305     {
7306       if (same_die_p_wrap (cu, entry->cu))
7307         break;
7308     }
7309
7310   if (entry)
7311     {
7312       *sym_num = entry->min_comdat_num;
7313       return 1;
7314     }
7315
7316   entry = XCNEW (struct cu_hash_table_entry);
7317   entry->cu = cu;
7318   entry->min_comdat_num = *sym_num = last->max_comdat_num;
7319   entry->next = *slot;
7320   *slot = entry;
7321
7322   return 0;
7323 }
7324
7325 /* Record SYM_NUM to record of CU in HTABLE.  */
7326 static void
7327 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
7328 {
7329   struct cu_hash_table_entry **slot, *entry;
7330
7331   slot = (struct cu_hash_table_entry **)
7332     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
7333         NO_INSERT);
7334   entry = *slot;
7335
7336   entry->max_comdat_num = sym_num;
7337 }
7338
7339 /* Traverse the DIE (which is always comp_unit_die), and set up
7340    additional compilation units for each of the include files we see
7341    bracketed by BINCL/EINCL.  */
7342
7343 static void
7344 break_out_includes (dw_die_ref die)
7345 {
7346   dw_die_ref c;
7347   dw_die_ref unit = NULL;
7348   limbo_die_node *node, **pnode;
7349   htab_t cu_hash_table;
7350
7351   c = die->die_child;
7352   if (c) do {
7353     dw_die_ref prev = c;
7354     c = c->die_sib;
7355     while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
7356            || (unit && is_comdat_die (c)))
7357       {
7358         dw_die_ref next = c->die_sib;
7359
7360         /* This DIE is for a secondary CU; remove it from the main one.  */
7361         remove_child_with_prev (c, prev);
7362
7363         if (c->die_tag == DW_TAG_GNU_BINCL)
7364           unit = push_new_compile_unit (unit, c);
7365         else if (c->die_tag == DW_TAG_GNU_EINCL)
7366           unit = pop_compile_unit (unit);
7367         else
7368           add_child_die (unit, c);
7369         c = next;
7370         if (c == die->die_child)
7371           break;
7372       }
7373   } while (c != die->die_child);
7374
7375 #if 0
7376   /* We can only use this in debugging, since the frontend doesn't check
7377      to make sure that we leave every include file we enter.  */
7378   gcc_assert (!unit);
7379 #endif
7380
7381   assign_symbol_names (die);
7382   cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
7383   for (node = limbo_die_list, pnode = &limbo_die_list;
7384        node;
7385        node = node->next)
7386     {
7387       int is_dupl;
7388
7389       compute_section_prefix (node->die);
7390       is_dupl = check_duplicate_cu (node->die, cu_hash_table,
7391                         &comdat_symbol_number);
7392       assign_symbol_names (node->die);
7393       if (is_dupl)
7394         *pnode = node->next;
7395       else
7396         {
7397           pnode = &node->next;
7398           record_comdat_symbol_number (node->die, cu_hash_table,
7399                 comdat_symbol_number);
7400         }
7401     }
7402   htab_delete (cu_hash_table);
7403 }
7404
7405 /* Traverse the DIE and add a sibling attribute if it may have the
7406    effect of speeding up access to siblings.  To save some space,
7407    avoid generating sibling attributes for DIE's without children.  */
7408
7409 static void
7410 add_sibling_attributes (dw_die_ref die)
7411 {
7412   dw_die_ref c;
7413
7414   if (! die->die_child)
7415     return;
7416
7417   if (die->die_parent && die != die->die_parent->die_child)
7418     add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
7419
7420   FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
7421 }
7422
7423 /* Output all location lists for the DIE and its children.  */
7424
7425 static void
7426 output_location_lists (dw_die_ref die)
7427 {
7428   dw_die_ref c;
7429   dw_attr_ref a;
7430   unsigned ix;
7431
7432   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7433     if (AT_class (a) == dw_val_class_loc_list)
7434       output_loc_list (AT_loc_list (a));
7435
7436   FOR_EACH_CHILD (die, c, output_location_lists (c));
7437 }
7438
7439 /* The format of each DIE (and its attribute value pairs) is encoded in an
7440    abbreviation table.  This routine builds the abbreviation table and assigns
7441    a unique abbreviation id for each abbreviation entry.  The children of each
7442    die are visited recursively.  */
7443
7444 static void
7445 build_abbrev_table (dw_die_ref die)
7446 {
7447   unsigned long abbrev_id;
7448   unsigned int n_alloc;
7449   dw_die_ref c;
7450   dw_attr_ref a;
7451   unsigned ix;
7452
7453   /* Scan the DIE references, and mark as external any that refer to
7454      DIEs from other CUs (i.e. those which are not marked).  */
7455   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7456     if (AT_class (a) == dw_val_class_die_ref
7457         && AT_ref (a)->die_mark == 0)
7458       {
7459         gcc_assert (AT_ref (a)->die_symbol);
7460
7461         set_AT_ref_external (a, 1);
7462       }
7463
7464   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7465     {
7466       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7467       dw_attr_ref die_a, abbrev_a;
7468       unsigned ix;
7469       bool ok = true;
7470
7471       if (abbrev->die_tag != die->die_tag)
7472         continue;
7473       if ((abbrev->die_child != NULL) != (die->die_child != NULL))
7474         continue;
7475
7476       if (VEC_length (dw_attr_node, abbrev->die_attr)
7477           != VEC_length (dw_attr_node, die->die_attr))
7478         continue;
7479
7480       for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, die_a); ix++)
7481         {
7482           abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
7483           if ((abbrev_a->dw_attr != die_a->dw_attr)
7484               || (value_format (abbrev_a) != value_format (die_a)))
7485             {
7486               ok = false;
7487               break;
7488             }
7489         }
7490       if (ok)
7491         break;
7492     }
7493
7494   if (abbrev_id >= abbrev_die_table_in_use)
7495     {
7496       if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
7497         {
7498           n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
7499           abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
7500                                             n_alloc);
7501
7502           memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
7503                  (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
7504           abbrev_die_table_allocated = n_alloc;
7505         }
7506
7507       ++abbrev_die_table_in_use;
7508       abbrev_die_table[abbrev_id] = die;
7509     }
7510
7511   die->die_abbrev = abbrev_id;
7512   FOR_EACH_CHILD (die, c, build_abbrev_table (c));
7513 }
7514 \f
7515 /* Return the power-of-two number of bytes necessary to represent VALUE.  */
7516
7517 static int
7518 constant_size (unsigned HOST_WIDE_INT value)
7519 {
7520   int log;
7521
7522   if (value == 0)
7523     log = 0;
7524   else
7525     log = floor_log2 (value);
7526
7527   log = log / 8;
7528   log = 1 << (floor_log2 (log) + 1);
7529
7530   return log;
7531 }
7532
7533 /* Return the size of a DIE as it is represented in the
7534    .debug_info section.  */
7535
7536 static unsigned long
7537 size_of_die (dw_die_ref die)
7538 {
7539   unsigned long size = 0;
7540   dw_attr_ref a;
7541   unsigned ix;
7542
7543   size += size_of_uleb128 (die->die_abbrev);
7544   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7545     {
7546       switch (AT_class (a))
7547         {
7548         case dw_val_class_addr:
7549           size += DWARF2_ADDR_SIZE;
7550           break;
7551         case dw_val_class_offset:
7552           size += DWARF_OFFSET_SIZE;
7553           break;
7554         case dw_val_class_loc:
7555           {
7556             unsigned long lsize = size_of_locs (AT_loc (a));
7557
7558             /* Block length.  */
7559             size += constant_size (lsize);
7560             size += lsize;
7561           }
7562           break;
7563         case dw_val_class_loc_list:
7564           size += DWARF_OFFSET_SIZE;
7565           break;
7566         case dw_val_class_range_list:
7567           size += DWARF_OFFSET_SIZE;
7568           break;
7569         case dw_val_class_const:
7570           size += size_of_sleb128 (AT_int (a));
7571           break;
7572         case dw_val_class_unsigned_const:
7573           size += constant_size (AT_unsigned (a));
7574           break;
7575         case dw_val_class_long_long:
7576           size += 1 + 2*HOST_BITS_PER_LONG/HOST_BITS_PER_CHAR; /* block */
7577           break;
7578         case dw_val_class_vec:
7579           size += constant_size (a->dw_attr_val.v.val_vec.length
7580                                  * a->dw_attr_val.v.val_vec.elt_size)
7581                   + a->dw_attr_val.v.val_vec.length
7582                     * a->dw_attr_val.v.val_vec.elt_size; /* block */
7583           break;
7584         case dw_val_class_flag:
7585           size += 1;
7586           break;
7587         case dw_val_class_die_ref:
7588           if (AT_ref_external (a))
7589             size += DWARF2_ADDR_SIZE;
7590           else
7591             size += DWARF_OFFSET_SIZE;
7592           break;
7593         case dw_val_class_fde_ref:
7594           size += DWARF_OFFSET_SIZE;
7595           break;
7596         case dw_val_class_lbl_id:
7597           size += DWARF2_ADDR_SIZE;
7598           break;
7599         case dw_val_class_lineptr:
7600         case dw_val_class_macptr:
7601           size += DWARF_OFFSET_SIZE;
7602           break;
7603         case dw_val_class_str:
7604           if (AT_string_form (a) == DW_FORM_strp)
7605             size += DWARF_OFFSET_SIZE;
7606           else
7607             size += strlen (a->dw_attr_val.v.val_str->str) + 1;
7608           break;
7609         case dw_val_class_file:
7610           size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
7611           break;
7612         default:
7613           gcc_unreachable ();
7614         }
7615     }
7616
7617   return size;
7618 }
7619
7620 /* Size the debugging information associated with a given DIE.  Visits the
7621    DIE's children recursively.  Updates the global variable next_die_offset, on
7622    each time through.  Uses the current value of next_die_offset to update the
7623    die_offset field in each DIE.  */
7624
7625 static void
7626 calc_die_sizes (dw_die_ref die)
7627 {
7628   dw_die_ref c;
7629
7630   die->die_offset = next_die_offset;
7631   next_die_offset += size_of_die (die);
7632
7633   FOR_EACH_CHILD (die, c, calc_die_sizes (c));
7634
7635   if (die->die_child != NULL)
7636     /* Count the null byte used to terminate sibling lists.  */
7637     next_die_offset += 1;
7638 }
7639
7640 /* Set the marks for a die and its children.  We do this so
7641    that we know whether or not a reference needs to use FORM_ref_addr; only
7642    DIEs in the same CU will be marked.  We used to clear out the offset
7643    and use that as the flag, but ran into ordering problems.  */
7644
7645 static void
7646 mark_dies (dw_die_ref die)
7647 {
7648   dw_die_ref c;
7649
7650   gcc_assert (!die->die_mark);
7651
7652   die->die_mark = 1;
7653   FOR_EACH_CHILD (die, c, mark_dies (c));
7654 }
7655
7656 /* Clear the marks for a die and its children.  */
7657
7658 static void
7659 unmark_dies (dw_die_ref die)
7660 {
7661   dw_die_ref c;
7662
7663   gcc_assert (die->die_mark);
7664
7665   die->die_mark = 0;
7666   FOR_EACH_CHILD (die, c, unmark_dies (c));
7667 }
7668
7669 /* Clear the marks for a die, its children and referred dies.  */
7670
7671 static void
7672 unmark_all_dies (dw_die_ref die)
7673 {
7674   dw_die_ref c;
7675   dw_attr_ref a;
7676   unsigned ix;
7677
7678   if (!die->die_mark)
7679     return;
7680   die->die_mark = 0;
7681
7682   FOR_EACH_CHILD (die, c, unmark_all_dies (c));
7683
7684   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7685     if (AT_class (a) == dw_val_class_die_ref)
7686       unmark_all_dies (AT_ref (a));
7687 }
7688
7689 /* Return the size of the .debug_pubnames or .debug_pubtypes table
7690    generated for the compilation unit.  */
7691
7692 static unsigned long
7693 size_of_pubnames (VEC (pubname_entry, gc) * names)
7694 {
7695   unsigned long size;
7696   unsigned i;
7697   pubname_ref p;
7698
7699   size = DWARF_PUBNAMES_HEADER_SIZE;
7700   for (i = 0; VEC_iterate (pubname_entry, names, i, p); i++)
7701     if (names != pubtype_table
7702         || p->die->die_offset != 0
7703         || !flag_eliminate_unused_debug_types)
7704       size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
7705
7706   size += DWARF_OFFSET_SIZE;
7707   return size;
7708 }
7709
7710 /* Return the size of the information in the .debug_aranges section.  */
7711
7712 static unsigned long
7713 size_of_aranges (void)
7714 {
7715   unsigned long size;
7716
7717   size = DWARF_ARANGES_HEADER_SIZE;
7718
7719   /* Count the address/length pair for this compilation unit.  */
7720   if (text_section_used)
7721     size += 2 * DWARF2_ADDR_SIZE;
7722   if (cold_text_section_used)
7723     size += 2 * DWARF2_ADDR_SIZE;
7724   size += 2 * DWARF2_ADDR_SIZE * arange_table_in_use;
7725
7726   /* Count the two zero words used to terminated the address range table.  */
7727   size += 2 * DWARF2_ADDR_SIZE;
7728   return size;
7729 }
7730 \f
7731 /* Select the encoding of an attribute value.  */
7732
7733 static enum dwarf_form
7734 value_format (dw_attr_ref a)
7735 {
7736   switch (a->dw_attr_val.val_class)
7737     {
7738     case dw_val_class_addr:
7739       return DW_FORM_addr;
7740     case dw_val_class_range_list:
7741     case dw_val_class_offset:
7742     case dw_val_class_loc_list:
7743       switch (DWARF_OFFSET_SIZE)
7744         {
7745         case 4:
7746           return DW_FORM_data4;
7747         case 8:
7748           return DW_FORM_data8;
7749         default:
7750           gcc_unreachable ();
7751         }
7752     case dw_val_class_loc:
7753       switch (constant_size (size_of_locs (AT_loc (a))))
7754         {
7755         case 1:
7756           return DW_FORM_block1;
7757         case 2:
7758           return DW_FORM_block2;
7759         default:
7760           gcc_unreachable ();
7761         }
7762     case dw_val_class_const:
7763       return DW_FORM_sdata;
7764     case dw_val_class_unsigned_const:
7765       switch (constant_size (AT_unsigned (a)))
7766         {
7767         case 1:
7768           return DW_FORM_data1;
7769         case 2:
7770           return DW_FORM_data2;
7771         case 4:
7772           return DW_FORM_data4;
7773         case 8:
7774           return DW_FORM_data8;
7775         default:
7776           gcc_unreachable ();
7777         }
7778     case dw_val_class_long_long:
7779       return DW_FORM_block1;
7780     case dw_val_class_vec:
7781       switch (constant_size (a->dw_attr_val.v.val_vec.length
7782                              * a->dw_attr_val.v.val_vec.elt_size))
7783         {
7784         case 1:
7785           return DW_FORM_block1;
7786         case 2:
7787           return DW_FORM_block2;
7788         case 4:
7789           return DW_FORM_block4;
7790         default:
7791           gcc_unreachable ();
7792         }
7793     case dw_val_class_flag:
7794       return DW_FORM_flag;
7795     case dw_val_class_die_ref:
7796       if (AT_ref_external (a))
7797         return DW_FORM_ref_addr;
7798       else
7799         return DW_FORM_ref;
7800     case dw_val_class_fde_ref:
7801       return DW_FORM_data;
7802     case dw_val_class_lbl_id:
7803       return DW_FORM_addr;
7804     case dw_val_class_lineptr:
7805     case dw_val_class_macptr:
7806       return DW_FORM_data;
7807     case dw_val_class_str:
7808       return AT_string_form (a);
7809     case dw_val_class_file:
7810       switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
7811         {
7812         case 1:
7813           return DW_FORM_data1;
7814         case 2:
7815           return DW_FORM_data2;
7816         case 4:
7817           return DW_FORM_data4;
7818         default:
7819           gcc_unreachable ();
7820         }
7821
7822     default:
7823       gcc_unreachable ();
7824     }
7825 }
7826
7827 /* Output the encoding of an attribute value.  */
7828
7829 static void
7830 output_value_format (dw_attr_ref a)
7831 {
7832   enum dwarf_form form = value_format (a);
7833
7834   dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
7835 }
7836
7837 /* Output the .debug_abbrev section which defines the DIE abbreviation
7838    table.  */
7839
7840 static void
7841 output_abbrev_section (void)
7842 {
7843   unsigned long abbrev_id;
7844
7845   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7846     {
7847       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7848       unsigned ix;
7849       dw_attr_ref a_attr;
7850
7851       dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
7852       dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
7853                                    dwarf_tag_name (abbrev->die_tag));
7854
7855       if (abbrev->die_child != NULL)
7856         dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
7857       else
7858         dw2_asm_output_data (1, DW_children_no, "DW_children_no");
7859
7860       for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
7861            ix++)
7862         {
7863           dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
7864                                        dwarf_attr_name (a_attr->dw_attr));
7865           output_value_format (a_attr);
7866         }
7867
7868       dw2_asm_output_data (1, 0, NULL);
7869       dw2_asm_output_data (1, 0, NULL);
7870     }
7871
7872   /* Terminate the table.  */
7873   dw2_asm_output_data (1, 0, NULL);
7874 }
7875
7876 /* Output a symbol we can use to refer to this DIE from another CU.  */
7877
7878 static inline void
7879 output_die_symbol (dw_die_ref die)
7880 {
7881   char *sym = die->die_symbol;
7882
7883   if (sym == 0)
7884     return;
7885
7886   if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
7887     /* We make these global, not weak; if the target doesn't support
7888        .linkonce, it doesn't support combining the sections, so debugging
7889        will break.  */
7890     targetm.asm_out.globalize_label (asm_out_file, sym);
7891
7892   ASM_OUTPUT_LABEL (asm_out_file, sym);
7893 }
7894
7895 /* Return a new location list, given the begin and end range, and the
7896    expression. gensym tells us whether to generate a new internal symbol for
7897    this location list node, which is done for the head of the list only.  */
7898
7899 static inline dw_loc_list_ref
7900 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
7901               const char *section, unsigned int gensym)
7902 {
7903   dw_loc_list_ref retlist = GGC_CNEW (dw_loc_list_node);
7904
7905   retlist->begin = begin;
7906   retlist->end = end;
7907   retlist->expr = expr;
7908   retlist->section = section;
7909   if (gensym)
7910     retlist->ll_symbol = gen_internal_sym ("LLST");
7911
7912   return retlist;
7913 }
7914
7915 /* Add a location description expression to a location list.  */
7916
7917 static inline void
7918 add_loc_descr_to_loc_list (dw_loc_list_ref *list_head, dw_loc_descr_ref descr,
7919                            const char *begin, const char *end,
7920                            const char *section)
7921 {
7922   dw_loc_list_ref *d;
7923
7924   /* Find the end of the chain.  */
7925   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
7926     ;
7927
7928   /* Add a new location list node to the list.  */
7929   *d = new_loc_list (descr, begin, end, section, 0);
7930 }
7931
7932 /* Output the location list given to us.  */
7933
7934 static void
7935 output_loc_list (dw_loc_list_ref list_head)
7936 {
7937   dw_loc_list_ref curr = list_head;
7938
7939   ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
7940
7941   /* Walk the location list, and output each range + expression.  */
7942   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
7943     {
7944       unsigned long size;
7945       /* Don't output an entry that starts and ends at the same address.  */
7946       if (strcmp (curr->begin, curr->end) == 0)
7947         continue;
7948       if (!have_multiple_function_sections)
7949         {
7950           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
7951                                 "Location list begin address (%s)",
7952                                 list_head->ll_symbol);
7953           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
7954                                 "Location list end address (%s)",
7955                                 list_head->ll_symbol);
7956         }
7957       else
7958         {
7959           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
7960                                "Location list begin address (%s)",
7961                                list_head->ll_symbol);
7962           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
7963                                "Location list end address (%s)",
7964                                list_head->ll_symbol);
7965         }
7966       size = size_of_locs (curr->expr);
7967
7968       /* Output the block length for this list of location operations.  */
7969       gcc_assert (size <= 0xffff);
7970       dw2_asm_output_data (2, size, "%s", "Location expression size");
7971
7972       output_loc_sequence (curr->expr);
7973     }
7974
7975   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
7976                        "Location list terminator begin (%s)",
7977                        list_head->ll_symbol);
7978   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
7979                        "Location list terminator end (%s)",
7980                        list_head->ll_symbol);
7981 }
7982
7983 /* Output the DIE and its attributes.  Called recursively to generate
7984    the definitions of each child DIE.  */
7985
7986 static void
7987 output_die (dw_die_ref die)
7988 {
7989   dw_attr_ref a;
7990   dw_die_ref c;
7991   unsigned long size;
7992   unsigned ix;
7993
7994   /* If someone in another CU might refer to us, set up a symbol for
7995      them to point to.  */
7996   if (die->die_symbol)
7997     output_die_symbol (die);
7998
7999   dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (0x%lx) %s)",
8000                                (unsigned long)die->die_offset,
8001                                dwarf_tag_name (die->die_tag));
8002
8003   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
8004     {
8005       const char *name = dwarf_attr_name (a->dw_attr);
8006
8007       switch (AT_class (a))
8008         {
8009         case dw_val_class_addr:
8010           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8011           break;
8012
8013         case dw_val_class_offset:
8014           dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8015                                "%s", name);
8016           break;
8017
8018         case dw_val_class_range_list:
8019           {
8020             char *p = strchr (ranges_section_label, '\0');
8021
8022             sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
8023                      a->dw_attr_val.v.val_offset);
8024             dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8025                                    debug_ranges_section, "%s", name);
8026             *p = '\0';
8027           }
8028           break;
8029
8030         case dw_val_class_loc:
8031           size = size_of_locs (AT_loc (a));
8032
8033           /* Output the block length for this list of location operations.  */
8034           dw2_asm_output_data (constant_size (size), size, "%s", name);
8035
8036           output_loc_sequence (AT_loc (a));
8037           break;
8038
8039         case dw_val_class_const:
8040           /* ??? It would be slightly more efficient to use a scheme like is
8041              used for unsigned constants below, but gdb 4.x does not sign
8042              extend.  Gdb 5.x does sign extend.  */
8043           dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8044           break;
8045
8046         case dw_val_class_unsigned_const:
8047           dw2_asm_output_data (constant_size (AT_unsigned (a)),
8048                                AT_unsigned (a), "%s", name);
8049           break;
8050
8051         case dw_val_class_long_long:
8052           {
8053             unsigned HOST_WIDE_INT first, second;
8054
8055             dw2_asm_output_data (1,
8056                                  2 * HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
8057                                  "%s", name);
8058
8059             if (WORDS_BIG_ENDIAN)
8060               {
8061                 first = a->dw_attr_val.v.val_long_long.hi;
8062                 second = a->dw_attr_val.v.val_long_long.low;
8063               }
8064             else
8065               {
8066                 first = a->dw_attr_val.v.val_long_long.low;
8067                 second = a->dw_attr_val.v.val_long_long.hi;
8068               }
8069
8070             dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
8071                                  first, "long long constant");
8072             dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
8073                                  second, NULL);
8074           }
8075           break;
8076
8077         case dw_val_class_vec:
8078           {
8079             unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
8080             unsigned int len = a->dw_attr_val.v.val_vec.length;
8081             unsigned int i;
8082             unsigned char *p;
8083
8084             dw2_asm_output_data (constant_size (len * elt_size),
8085                                  len * elt_size, "%s", name);
8086             if (elt_size > sizeof (HOST_WIDE_INT))
8087               {
8088                 elt_size /= 2;
8089                 len *= 2;
8090               }
8091             for (i = 0, p = a->dw_attr_val.v.val_vec.array;
8092                  i < len;
8093                  i++, p += elt_size)
8094               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
8095                                    "fp or vector constant word %u", i);
8096             break;
8097           }
8098
8099         case dw_val_class_flag:
8100           dw2_asm_output_data (1, AT_flag (a), "%s", name);
8101           break;
8102
8103         case dw_val_class_loc_list:
8104           {
8105             char *sym = AT_loc_list (a)->ll_symbol;
8106
8107             gcc_assert (sym);
8108             dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8109                                    "%s", name);
8110           }
8111           break;
8112
8113         case dw_val_class_die_ref:
8114           if (AT_ref_external (a))
8115             {
8116               char *sym = AT_ref (a)->die_symbol;
8117
8118               gcc_assert (sym);
8119               dw2_asm_output_offset (DWARF2_ADDR_SIZE, sym, debug_info_section,
8120                                      "%s", name);
8121             }
8122           else
8123             {
8124               gcc_assert (AT_ref (a)->die_offset);
8125               dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
8126                                    "%s", name);
8127             }
8128           break;
8129
8130         case dw_val_class_fde_ref:
8131           {
8132             char l1[20];
8133
8134             ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
8135                                          a->dw_attr_val.v.val_fde_index * 2);
8136             dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
8137                                    "%s", name);
8138           }
8139           break;
8140
8141         case dw_val_class_lbl_id:
8142           dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8143           break;
8144
8145         case dw_val_class_lineptr:
8146           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8147                                  debug_line_section, "%s", name);
8148           break;
8149
8150         case dw_val_class_macptr:
8151           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8152                                  debug_macinfo_section, "%s", name);
8153           break;
8154
8155         case dw_val_class_str:
8156           if (AT_string_form (a) == DW_FORM_strp)
8157             dw2_asm_output_offset (DWARF_OFFSET_SIZE,
8158                                    a->dw_attr_val.v.val_str->label,
8159                                    debug_str_section,
8160                                    "%s: \"%s\"", name, AT_string (a));
8161           else
8162             dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
8163           break;
8164
8165         case dw_val_class_file:
8166           {
8167             int f = maybe_emit_file (a->dw_attr_val.v.val_file);
8168
8169             dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
8170                                  a->dw_attr_val.v.val_file->filename);
8171             break;
8172           }
8173
8174         default:
8175           gcc_unreachable ();
8176         }
8177     }
8178
8179   FOR_EACH_CHILD (die, c, output_die (c));
8180
8181   /* Add null byte to terminate sibling list.  */
8182   if (die->die_child != NULL)
8183     dw2_asm_output_data (1, 0, "end of children of DIE 0x%lx",
8184                          (unsigned long) die->die_offset);
8185 }
8186
8187 /* Output the compilation unit that appears at the beginning of the
8188    .debug_info section, and precedes the DIE descriptions.  */
8189
8190 static void
8191 output_compilation_unit_header (void)
8192 {
8193   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8194     dw2_asm_output_data (4, 0xffffffff,
8195       "Initial length escape value indicating 64-bit DWARF extension");
8196   dw2_asm_output_data (DWARF_OFFSET_SIZE,
8197                        next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
8198                        "Length of Compilation Unit Info");
8199   dw2_asm_output_data (2, DWARF_VERSION, "DWARF version number");
8200   dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
8201                          debug_abbrev_section,
8202                          "Offset Into Abbrev. Section");
8203   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8204 }
8205
8206 /* Output the compilation unit DIE and its children.  */
8207
8208 static void
8209 output_comp_unit (dw_die_ref die, int output_if_empty)
8210 {
8211   const char *secname;
8212   char *oldsym, *tmp;
8213
8214   /* Unless we are outputting main CU, we may throw away empty ones.  */
8215   if (!output_if_empty && die->die_child == NULL)
8216     return;
8217
8218   /* Even if there are no children of this DIE, we must output the information
8219      about the compilation unit.  Otherwise, on an empty translation unit, we
8220      will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
8221      will then complain when examining the file.  First mark all the DIEs in
8222      this CU so we know which get local refs.  */
8223   mark_dies (die);
8224
8225   build_abbrev_table (die);
8226
8227   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
8228   next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8229   calc_die_sizes (die);
8230
8231   oldsym = die->die_symbol;
8232   if (oldsym)
8233     {
8234       tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
8235
8236       sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
8237       secname = tmp;
8238       die->die_symbol = NULL;
8239       switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
8240     }
8241   else
8242     switch_to_section (debug_info_section);
8243
8244   /* Output debugging information.  */
8245   output_compilation_unit_header ();
8246   output_die (die);
8247
8248   /* Leave the marks on the main CU, so we can check them in
8249      output_pubnames.  */
8250   if (oldsym)
8251     {
8252       unmark_dies (die);
8253       die->die_symbol = oldsym;
8254     }
8255 }
8256
8257 /* Return the DWARF2/3 pubname associated with a decl.  */
8258
8259 static const char *
8260 dwarf2_name (tree decl, int scope)
8261 {
8262   return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
8263 }
8264
8265 /* Add a new entry to .debug_pubnames if appropriate.  */
8266
8267 static void
8268 add_pubname_string (const char *str, dw_die_ref die)
8269 {
8270   pubname_entry e;
8271
8272   e.die = die;
8273   e.name = xstrdup (str);
8274   VEC_safe_push (pubname_entry, gc, pubname_table, &e);
8275 }
8276
8277 static void
8278 add_pubname (tree decl, dw_die_ref die)
8279 {
8280
8281   if (TREE_PUBLIC (decl))
8282     add_pubname_string (dwarf2_name (decl, 1), die);
8283 }
8284
8285 /* Add a new entry to .debug_pubtypes if appropriate.  */
8286
8287 static void
8288 add_pubtype (tree decl, dw_die_ref die)
8289 {
8290   pubname_entry e;
8291
8292   e.name = NULL;
8293   if ((TREE_PUBLIC (decl)
8294        || die->die_parent == comp_unit_die)
8295       && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
8296     {
8297       e.die = die;
8298       if (TYPE_P (decl))
8299         {
8300           if (TYPE_NAME (decl))
8301             {
8302               if (TREE_CODE (TYPE_NAME (decl)) == IDENTIFIER_NODE)
8303                 e.name = IDENTIFIER_POINTER (TYPE_NAME (decl));
8304               else if (TREE_CODE (TYPE_NAME (decl)) == TYPE_DECL
8305                        && DECL_NAME (TYPE_NAME (decl)))
8306                 e.name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (decl)));
8307               else
8308                e.name = xstrdup ((const char *) get_AT_string (die, DW_AT_name));
8309             }
8310         }
8311       else
8312         e.name = xstrdup (dwarf2_name (decl, 1));
8313
8314       /* If we don't have a name for the type, there's no point in adding
8315          it to the table.  */
8316       if (e.name && e.name[0] != '\0')
8317         VEC_safe_push (pubname_entry, gc, pubtype_table, &e);
8318     }
8319 }
8320
8321 /* Output the public names table used to speed up access to externally
8322    visible names; or the public types table used to find type definitions.  */
8323
8324 static void
8325 output_pubnames (VEC (pubname_entry, gc) * names)
8326 {
8327   unsigned i;
8328   unsigned long pubnames_length = size_of_pubnames (names);
8329   pubname_ref pub;
8330
8331   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8332     dw2_asm_output_data (4, 0xffffffff,
8333       "Initial length escape value indicating 64-bit DWARF extension");
8334   if (names == pubname_table)
8335     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
8336                          "Length of Public Names Info");
8337   else
8338     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
8339                          "Length of Public Type Names Info");
8340   dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
8341   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
8342                          debug_info_section,
8343                          "Offset of Compilation Unit Info");
8344   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
8345                        "Compilation Unit Length");
8346
8347   for (i = 0; VEC_iterate (pubname_entry, names, i, pub); i++)
8348     {
8349       /* We shouldn't see pubnames for DIEs outside of the main CU.  */
8350       if (names == pubname_table)
8351         gcc_assert (pub->die->die_mark);
8352
8353       if (names != pubtype_table
8354           || pub->die->die_offset != 0
8355           || !flag_eliminate_unused_debug_types)
8356         {
8357           dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
8358                                "DIE offset");
8359
8360           dw2_asm_output_nstring (pub->name, -1, "external name");
8361         }
8362     }
8363
8364   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
8365 }
8366
8367 /* Add a new entry to .debug_aranges if appropriate.  */
8368
8369 static void
8370 add_arange (tree decl, dw_die_ref die)
8371 {
8372   if (! DECL_SECTION_NAME (decl))
8373     return;
8374
8375   if (arange_table_in_use == arange_table_allocated)
8376     {
8377       arange_table_allocated += ARANGE_TABLE_INCREMENT;
8378       arange_table = GGC_RESIZEVEC (dw_die_ref, arange_table,
8379                                     arange_table_allocated);
8380       memset (arange_table + arange_table_in_use, 0,
8381               ARANGE_TABLE_INCREMENT * sizeof (dw_die_ref));
8382     }
8383
8384   arange_table[arange_table_in_use++] = die;
8385 }
8386
8387 /* Output the information that goes into the .debug_aranges table.
8388    Namely, define the beginning and ending address range of the
8389    text section generated for this compilation unit.  */
8390
8391 static void
8392 output_aranges (void)
8393 {
8394   unsigned i;
8395   unsigned long aranges_length = size_of_aranges ();
8396
8397   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8398     dw2_asm_output_data (4, 0xffffffff,
8399       "Initial length escape value indicating 64-bit DWARF extension");
8400   dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
8401                        "Length of Address Ranges Info");
8402   dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
8403   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
8404                          debug_info_section,
8405                          "Offset of Compilation Unit Info");
8406   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
8407   dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
8408
8409   /* We need to align to twice the pointer size here.  */
8410   if (DWARF_ARANGES_PAD_SIZE)
8411     {
8412       /* Pad using a 2 byte words so that padding is correct for any
8413          pointer size.  */
8414       dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
8415                            2 * DWARF2_ADDR_SIZE);
8416       for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
8417         dw2_asm_output_data (2, 0, NULL);
8418     }
8419
8420   /* It is necessary not to output these entries if the sections were
8421      not used; if the sections were not used, the length will be 0 and
8422      the address may end up as 0 if the section is discarded by ld
8423      --gc-sections, leaving an invalid (0, 0) entry that can be
8424      confused with the terminator.  */
8425   if (text_section_used)
8426     {
8427       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
8428       dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
8429                             text_section_label, "Length");
8430     }
8431   if (cold_text_section_used)
8432     {
8433       dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
8434                            "Address");
8435       dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
8436                             cold_text_section_label, "Length");
8437     }
8438
8439   for (i = 0; i < arange_table_in_use; i++)
8440     {
8441       dw_die_ref die = arange_table[i];
8442
8443       /* We shouldn't see aranges for DIEs outside of the main CU.  */
8444       gcc_assert (die->die_mark);
8445
8446       if (die->die_tag == DW_TAG_subprogram)
8447         {
8448           dw2_asm_output_addr (DWARF2_ADDR_SIZE, get_AT_low_pc (die),
8449                                "Address");
8450           dw2_asm_output_delta (DWARF2_ADDR_SIZE, get_AT_hi_pc (die),
8451                                 get_AT_low_pc (die), "Length");
8452         }
8453       else
8454         {
8455           /* A static variable; extract the symbol from DW_AT_location.
8456              Note that this code isn't currently hit, as we only emit
8457              aranges for functions (jason 9/23/99).  */
8458           dw_attr_ref a = get_AT (die, DW_AT_location);
8459           dw_loc_descr_ref loc;
8460
8461           gcc_assert (a && AT_class (a) == dw_val_class_loc);
8462
8463           loc = AT_loc (a);
8464           gcc_assert (loc->dw_loc_opc == DW_OP_addr);
8465
8466           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE,
8467                                    loc->dw_loc_oprnd1.v.val_addr, "Address");
8468           dw2_asm_output_data (DWARF2_ADDR_SIZE,
8469                                get_AT_unsigned (die, DW_AT_byte_size),
8470                                "Length");
8471         }
8472     }
8473
8474   /* Output the terminator words.  */
8475   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8476   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8477 }
8478
8479 /* Add a new entry to .debug_ranges.  Return the offset at which it
8480    was placed.  */
8481
8482 static unsigned int
8483 add_ranges_num (int num)
8484 {
8485   unsigned int in_use = ranges_table_in_use;
8486
8487   if (in_use == ranges_table_allocated)
8488     {
8489       ranges_table_allocated += RANGES_TABLE_INCREMENT;
8490       ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
8491                                     ranges_table_allocated);
8492       memset (ranges_table + ranges_table_in_use, 0,
8493               RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
8494     }
8495
8496   ranges_table[in_use].num = num;
8497   ranges_table_in_use = in_use + 1;
8498
8499   return in_use * 2 * DWARF2_ADDR_SIZE;
8500 }
8501
8502 /* Add a new entry to .debug_ranges corresponding to a block, or a
8503    range terminator if BLOCK is NULL.  */
8504
8505 static unsigned int
8506 add_ranges (const_tree block)
8507 {
8508   return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
8509 }
8510
8511 /* Add a new entry to .debug_ranges corresponding to a pair of
8512    labels.  */
8513
8514 static unsigned int
8515 add_ranges_by_labels (const char *begin, const char *end)
8516 {
8517   unsigned int in_use = ranges_by_label_in_use;
8518
8519   if (in_use == ranges_by_label_allocated)
8520     {
8521       ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
8522       ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
8523                                        ranges_by_label,
8524                                        ranges_by_label_allocated);
8525       memset (ranges_by_label + ranges_by_label_in_use, 0,
8526               RANGES_TABLE_INCREMENT
8527               * sizeof (struct dw_ranges_by_label_struct));
8528     }
8529
8530   ranges_by_label[in_use].begin = begin;
8531   ranges_by_label[in_use].end = end;
8532   ranges_by_label_in_use = in_use + 1;
8533
8534   return add_ranges_num (-(int)in_use - 1);
8535 }
8536
8537 static void
8538 output_ranges (void)
8539 {
8540   unsigned i;
8541   static const char *const start_fmt = "Offset 0x%x";
8542   const char *fmt = start_fmt;
8543
8544   for (i = 0; i < ranges_table_in_use; i++)
8545     {
8546       int block_num = ranges_table[i].num;
8547
8548       if (block_num > 0)
8549         {
8550           char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
8551           char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
8552
8553           ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
8554           ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
8555
8556           /* If all code is in the text section, then the compilation
8557              unit base address defaults to DW_AT_low_pc, which is the
8558              base of the text section.  */
8559           if (!have_multiple_function_sections)
8560             {
8561               dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
8562                                     text_section_label,
8563                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
8564               dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
8565                                     text_section_label, NULL);
8566             }
8567
8568           /* Otherwise, the compilation unit base address is zero,
8569              which allows us to use absolute addresses, and not worry
8570              about whether the target supports cross-section
8571              arithmetic.  */
8572           else
8573             {
8574               dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
8575                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
8576               dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
8577             }
8578
8579           fmt = NULL;
8580         }
8581
8582       /* Negative block_num stands for an index into ranges_by_label.  */
8583       else if (block_num < 0)
8584         {
8585           int lab_idx = - block_num - 1;
8586
8587           if (!have_multiple_function_sections)
8588             {
8589               gcc_unreachable ();
8590 #if 0
8591               /* If we ever use add_ranges_by_labels () for a single
8592                  function section, all we have to do is to take out
8593                  the #if 0 above.  */
8594               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
8595                                     ranges_by_label[lab_idx].begin,
8596                                     text_section_label,
8597                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
8598               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
8599                                     ranges_by_label[lab_idx].end,
8600                                     text_section_label, NULL);
8601 #endif
8602             }
8603           else
8604             {
8605               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
8606                                    ranges_by_label[lab_idx].begin,
8607                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
8608               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
8609                                    ranges_by_label[lab_idx].end,
8610                                    NULL);
8611             }
8612         }
8613       else
8614         {
8615           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8616           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8617           fmt = start_fmt;
8618         }
8619     }
8620 }
8621
8622 /* Data structure containing information about input files.  */
8623 struct file_info
8624 {
8625   const char *path;     /* Complete file name.  */
8626   const char *fname;    /* File name part.  */
8627   int length;           /* Length of entire string.  */
8628   struct dwarf_file_data * file_idx;    /* Index in input file table.  */
8629   int dir_idx;          /* Index in directory table.  */
8630 };
8631
8632 /* Data structure containing information about directories with source
8633    files.  */
8634 struct dir_info
8635 {
8636   const char *path;     /* Path including directory name.  */
8637   int length;           /* Path length.  */
8638   int prefix;           /* Index of directory entry which is a prefix.  */
8639   int count;            /* Number of files in this directory.  */
8640   int dir_idx;          /* Index of directory used as base.  */
8641 };
8642
8643 /* Callback function for file_info comparison.  We sort by looking at
8644    the directories in the path.  */
8645
8646 static int
8647 file_info_cmp (const void *p1, const void *p2)
8648 {
8649   const struct file_info *const s1 = (const struct file_info *) p1;
8650   const struct file_info *const s2 = (const struct file_info *) p2;
8651   const unsigned char *cp1;
8652   const unsigned char *cp2;
8653
8654   /* Take care of file names without directories.  We need to make sure that
8655      we return consistent values to qsort since some will get confused if
8656      we return the same value when identical operands are passed in opposite
8657      orders.  So if neither has a directory, return 0 and otherwise return
8658      1 or -1 depending on which one has the directory.  */
8659   if ((s1->path == s1->fname || s2->path == s2->fname))
8660     return (s2->path == s2->fname) - (s1->path == s1->fname);
8661
8662   cp1 = (const unsigned char *) s1->path;
8663   cp2 = (const unsigned char *) s2->path;
8664
8665   while (1)
8666     {
8667       ++cp1;
8668       ++cp2;
8669       /* Reached the end of the first path?  If so, handle like above.  */
8670       if ((cp1 == (const unsigned char *) s1->fname)
8671           || (cp2 == (const unsigned char *) s2->fname))
8672         return ((cp2 == (const unsigned char *) s2->fname)
8673                 - (cp1 == (const unsigned char *) s1->fname));
8674
8675       /* Character of current path component the same?  */
8676       else if (*cp1 != *cp2)
8677         return *cp1 - *cp2;
8678     }
8679 }
8680
8681 struct file_name_acquire_data
8682 {
8683   struct file_info *files;
8684   int used_files;
8685   int max_files;
8686 };
8687
8688 /* Traversal function for the hash table.  */
8689
8690 static int
8691 file_name_acquire (void ** slot, void *data)
8692 {
8693   struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
8694   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
8695   struct file_info *fi;
8696   const char *f;
8697
8698   gcc_assert (fnad->max_files >= d->emitted_number);
8699
8700   if (! d->emitted_number)
8701     return 1;
8702
8703   gcc_assert (fnad->max_files != fnad->used_files);
8704
8705   fi = fnad->files + fnad->used_files++;
8706
8707   /* Skip all leading "./".  */
8708   f = d->filename;
8709   while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
8710     f += 2;
8711
8712   /* Create a new array entry.  */
8713   fi->path = f;
8714   fi->length = strlen (f);
8715   fi->file_idx = d;
8716
8717   /* Search for the file name part.  */
8718   f = strrchr (f, DIR_SEPARATOR);
8719 #if defined (DIR_SEPARATOR_2)
8720   {
8721     char *g = strrchr (fi->path, DIR_SEPARATOR_2);
8722
8723     if (g != NULL)
8724       {
8725         if (f == NULL || f < g)
8726           f = g;
8727       }
8728   }
8729 #endif
8730
8731   fi->fname = f == NULL ? fi->path : f + 1;
8732   return 1;
8733 }
8734
8735 /* Output the directory table and the file name table.  We try to minimize
8736    the total amount of memory needed.  A heuristic is used to avoid large
8737    slowdowns with many input files.  */
8738
8739 static void
8740 output_file_names (void)
8741 {
8742   struct file_name_acquire_data fnad;
8743   int numfiles;
8744   struct file_info *files;
8745   struct dir_info *dirs;
8746   int *saved;
8747   int *savehere;
8748   int *backmap;
8749   int ndirs;
8750   int idx_offset;
8751   int i;
8752   int idx;
8753
8754   if (!last_emitted_file)
8755     {
8756       dw2_asm_output_data (1, 0, "End directory table");
8757       dw2_asm_output_data (1, 0, "End file name table");
8758       return;
8759     }
8760
8761   numfiles = last_emitted_file->emitted_number;
8762
8763   /* Allocate the various arrays we need.  */
8764   files = XALLOCAVEC (struct file_info, numfiles);
8765   dirs = XALLOCAVEC (struct dir_info, numfiles);
8766
8767   fnad.files = files;
8768   fnad.used_files = 0;
8769   fnad.max_files = numfiles;
8770   htab_traverse (file_table, file_name_acquire, &fnad);
8771   gcc_assert (fnad.used_files == fnad.max_files);
8772
8773   qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
8774
8775   /* Find all the different directories used.  */
8776   dirs[0].path = files[0].path;
8777   dirs[0].length = files[0].fname - files[0].path;
8778   dirs[0].prefix = -1;
8779   dirs[0].count = 1;
8780   dirs[0].dir_idx = 0;
8781   files[0].dir_idx = 0;
8782   ndirs = 1;
8783
8784   for (i = 1; i < numfiles; i++)
8785     if (files[i].fname - files[i].path == dirs[ndirs - 1].length
8786         && memcmp (dirs[ndirs - 1].path, files[i].path,
8787                    dirs[ndirs - 1].length) == 0)
8788       {
8789         /* Same directory as last entry.  */
8790         files[i].dir_idx = ndirs - 1;
8791         ++dirs[ndirs - 1].count;
8792       }
8793     else
8794       {
8795         int j;
8796
8797         /* This is a new directory.  */
8798         dirs[ndirs].path = files[i].path;
8799         dirs[ndirs].length = files[i].fname - files[i].path;
8800         dirs[ndirs].count = 1;
8801         dirs[ndirs].dir_idx = ndirs;
8802         files[i].dir_idx = ndirs;
8803
8804         /* Search for a prefix.  */
8805         dirs[ndirs].prefix = -1;
8806         for (j = 0; j < ndirs; j++)
8807           if (dirs[j].length < dirs[ndirs].length
8808               && dirs[j].length > 1
8809               && (dirs[ndirs].prefix == -1
8810                   || dirs[j].length > dirs[dirs[ndirs].prefix].length)
8811               && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
8812             dirs[ndirs].prefix = j;
8813
8814         ++ndirs;
8815       }
8816
8817   /* Now to the actual work.  We have to find a subset of the directories which
8818      allow expressing the file name using references to the directory table
8819      with the least amount of characters.  We do not do an exhaustive search
8820      where we would have to check out every combination of every single
8821      possible prefix.  Instead we use a heuristic which provides nearly optimal
8822      results in most cases and never is much off.  */
8823   saved = XALLOCAVEC (int, ndirs);
8824   savehere = XALLOCAVEC (int, ndirs);
8825
8826   memset (saved, '\0', ndirs * sizeof (saved[0]));
8827   for (i = 0; i < ndirs; i++)
8828     {
8829       int j;
8830       int total;
8831
8832       /* We can always save some space for the current directory.  But this
8833          does not mean it will be enough to justify adding the directory.  */
8834       savehere[i] = dirs[i].length;
8835       total = (savehere[i] - saved[i]) * dirs[i].count;
8836
8837       for (j = i + 1; j < ndirs; j++)
8838         {
8839           savehere[j] = 0;
8840           if (saved[j] < dirs[i].length)
8841             {
8842               /* Determine whether the dirs[i] path is a prefix of the
8843                  dirs[j] path.  */
8844               int k;
8845
8846               k = dirs[j].prefix;
8847               while (k != -1 && k != (int) i)
8848                 k = dirs[k].prefix;
8849
8850               if (k == (int) i)
8851                 {
8852                   /* Yes it is.  We can possibly save some memory by
8853                      writing the filenames in dirs[j] relative to
8854                      dirs[i].  */
8855                   savehere[j] = dirs[i].length;
8856                   total += (savehere[j] - saved[j]) * dirs[j].count;
8857                 }
8858             }
8859         }
8860
8861       /* Check whether we can save enough to justify adding the dirs[i]
8862          directory.  */
8863       if (total > dirs[i].length + 1)
8864         {
8865           /* It's worthwhile adding.  */
8866           for (j = i; j < ndirs; j++)
8867             if (savehere[j] > 0)
8868               {
8869                 /* Remember how much we saved for this directory so far.  */
8870                 saved[j] = savehere[j];
8871
8872                 /* Remember the prefix directory.  */
8873                 dirs[j].dir_idx = i;
8874               }
8875         }
8876     }
8877
8878   /* Emit the directory name table.  */
8879   idx = 1;
8880   idx_offset = dirs[0].length > 0 ? 1 : 0;
8881   for (i = 1 - idx_offset; i < ndirs; i++)
8882     dw2_asm_output_nstring (dirs[i].path, dirs[i].length - 1,
8883                             "Directory Entry: 0x%x", i + idx_offset);
8884
8885   dw2_asm_output_data (1, 0, "End directory table");
8886
8887   /* We have to emit them in the order of emitted_number since that's
8888      used in the debug info generation.  To do this efficiently we
8889      generate a back-mapping of the indices first.  */
8890   backmap = XALLOCAVEC (int, numfiles);
8891   for (i = 0; i < numfiles; i++)
8892     backmap[files[i].file_idx->emitted_number - 1] = i;
8893
8894   /* Now write all the file names.  */
8895   for (i = 0; i < numfiles; i++)
8896     {
8897       int file_idx = backmap[i];
8898       int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
8899
8900       dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
8901                               "File Entry: 0x%x", (unsigned) i + 1);
8902
8903       /* Include directory index.  */
8904       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
8905
8906       /* Modification time.  */
8907       dw2_asm_output_data_uleb128 (0, NULL);
8908
8909       /* File length in bytes.  */
8910       dw2_asm_output_data_uleb128 (0, NULL);
8911     }
8912
8913   dw2_asm_output_data (1, 0, "End file name table");
8914 }
8915
8916
8917 /* Output the source line number correspondence information.  This
8918    information goes into the .debug_line section.  */
8919
8920 static void
8921 output_line_info (void)
8922 {
8923   char l1[20], l2[20], p1[20], p2[20];
8924   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
8925   char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
8926   unsigned opc;
8927   unsigned n_op_args;
8928   unsigned long lt_index;
8929   unsigned long current_line;
8930   long line_offset;
8931   long line_delta;
8932   unsigned long current_file;
8933   unsigned long function;
8934
8935   ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
8936   ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
8937   ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
8938   ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
8939
8940   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8941     dw2_asm_output_data (4, 0xffffffff,
8942       "Initial length escape value indicating 64-bit DWARF extension");
8943   dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
8944                         "Length of Source Line Info");
8945   ASM_OUTPUT_LABEL (asm_out_file, l1);
8946
8947   dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
8948   dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
8949   ASM_OUTPUT_LABEL (asm_out_file, p1);
8950
8951   /* Define the architecture-dependent minimum instruction length (in
8952    bytes).  In this implementation of DWARF, this field is used for
8953    information purposes only.  Since GCC generates assembly language,
8954    we have no a priori knowledge of how many instruction bytes are
8955    generated for each source line, and therefore can use only the
8956    DW_LNE_set_address and DW_LNS_fixed_advance_pc line information
8957    commands.  Accordingly, we fix this as `1', which is "correct
8958    enough" for all architectures, and don't let the target override.  */
8959   dw2_asm_output_data (1, 1,
8960                        "Minimum Instruction Length");
8961
8962   dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
8963                        "Default is_stmt_start flag");
8964   dw2_asm_output_data (1, DWARF_LINE_BASE,
8965                        "Line Base Value (Special Opcodes)");
8966   dw2_asm_output_data (1, DWARF_LINE_RANGE,
8967                        "Line Range Value (Special Opcodes)");
8968   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
8969                        "Special Opcode Base");
8970
8971   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
8972     {
8973       switch (opc)
8974         {
8975         case DW_LNS_advance_pc:
8976         case DW_LNS_advance_line:
8977         case DW_LNS_set_file:
8978         case DW_LNS_set_column:
8979         case DW_LNS_fixed_advance_pc:
8980           n_op_args = 1;
8981           break;
8982         default:
8983           n_op_args = 0;
8984           break;
8985         }
8986
8987       dw2_asm_output_data (1, n_op_args, "opcode: 0x%x has %d args",
8988                            opc, n_op_args);
8989     }
8990
8991   /* Write out the information about the files we use.  */
8992   output_file_names ();
8993   ASM_OUTPUT_LABEL (asm_out_file, p2);
8994
8995   /* We used to set the address register to the first location in the text
8996      section here, but that didn't accomplish anything since we already
8997      have a line note for the opening brace of the first function.  */
8998
8999   /* Generate the line number to PC correspondence table, encoded as
9000      a series of state machine operations.  */
9001   current_file = 1;
9002   current_line = 1;
9003
9004   if (cfun && in_cold_section_p)
9005     strcpy (prev_line_label, crtl->subsections.cold_section_label);
9006   else
9007     strcpy (prev_line_label, text_section_label);
9008   for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
9009     {
9010       dw_line_info_ref line_info = &line_info_table[lt_index];
9011
9012 #if 0
9013       /* Disable this optimization for now; GDB wants to see two line notes
9014          at the beginning of a function so it can find the end of the
9015          prologue.  */
9016
9017       /* Don't emit anything for redundant notes.  Just updating the
9018          address doesn't accomplish anything, because we already assume
9019          that anything after the last address is this line.  */
9020       if (line_info->dw_line_num == current_line
9021           && line_info->dw_file_num == current_file)
9022         continue;
9023 #endif
9024
9025       /* Emit debug info for the address of the current line.
9026
9027          Unfortunately, we have little choice here currently, and must always
9028          use the most general form.  GCC does not know the address delta
9029          itself, so we can't use DW_LNS_advance_pc.  Many ports do have length
9030          attributes which will give an upper bound on the address range.  We
9031          could perhaps use length attributes to determine when it is safe to
9032          use DW_LNS_fixed_advance_pc.  */
9033
9034       ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
9035       if (0)
9036         {
9037           /* This can handle deltas up to 0xffff.  This takes 3 bytes.  */
9038           dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
9039                                "DW_LNS_fixed_advance_pc");
9040           dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
9041         }
9042       else
9043         {
9044           /* This can handle any delta.  This takes
9045              4+DWARF2_ADDR_SIZE bytes.  */
9046           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9047           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9048           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9049           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9050         }
9051
9052       strcpy (prev_line_label, line_label);
9053
9054       /* Emit debug info for the source file of the current line, if
9055          different from the previous line.  */
9056       if (line_info->dw_file_num != current_file)
9057         {
9058           current_file = line_info->dw_file_num;
9059           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
9060           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
9061         }
9062
9063       /* Emit debug info for the current line number, choosing the encoding
9064          that uses the least amount of space.  */
9065       if (line_info->dw_line_num != current_line)
9066         {
9067           line_offset = line_info->dw_line_num - current_line;
9068           line_delta = line_offset - DWARF_LINE_BASE;
9069           current_line = line_info->dw_line_num;
9070           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
9071             /* This can handle deltas from -10 to 234, using the current
9072                definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.  This
9073                takes 1 byte.  */
9074             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
9075                                  "line %lu", current_line);
9076           else
9077             {
9078               /* This can handle any delta.  This takes at least 4 bytes,
9079                  depending on the value being encoded.  */
9080               dw2_asm_output_data (1, DW_LNS_advance_line,
9081                                    "advance to line %lu", current_line);
9082               dw2_asm_output_data_sleb128 (line_offset, NULL);
9083               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
9084             }
9085         }
9086       else
9087         /* We still need to start a new row, so output a copy insn.  */
9088         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
9089     }
9090
9091   /* Emit debug info for the address of the end of the function.  */
9092   if (0)
9093     {
9094       dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
9095                            "DW_LNS_fixed_advance_pc");
9096       dw2_asm_output_delta (2, text_end_label, prev_line_label, NULL);
9097     }
9098   else
9099     {
9100       dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9101       dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9102       dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9103       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_end_label, NULL);
9104     }
9105
9106   dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
9107   dw2_asm_output_data_uleb128 (1, NULL);
9108   dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
9109
9110   function = 0;
9111   current_file = 1;
9112   current_line = 1;
9113   for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
9114     {
9115       dw_separate_line_info_ref line_info
9116         = &separate_line_info_table[lt_index];
9117
9118 #if 0
9119       /* Don't emit anything for redundant notes.  */
9120       if (line_info->dw_line_num == current_line
9121           && line_info->dw_file_num == current_file
9122           && line_info->function == function)
9123         goto cont;
9124 #endif
9125
9126       /* Emit debug info for the address of the current line.  If this is
9127          a new function, or the first line of a function, then we need
9128          to handle it differently.  */
9129       ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
9130                                    lt_index);
9131       if (function != line_info->function)
9132         {
9133           function = line_info->function;
9134
9135           /* Set the address register to the first line in the function.  */
9136           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9137           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9138           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9139           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9140         }
9141       else
9142         {
9143           /* ??? See the DW_LNS_advance_pc comment above.  */
9144           if (0)
9145             {
9146               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
9147                                    "DW_LNS_fixed_advance_pc");
9148               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
9149             }
9150           else
9151             {
9152               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9153               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9154               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9155               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9156             }
9157         }
9158
9159       strcpy (prev_line_label, line_label);
9160
9161       /* Emit debug info for the source file of the current line, if
9162          different from the previous line.  */
9163       if (line_info->dw_file_num != current_file)
9164         {
9165           current_file = line_info->dw_file_num;
9166           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
9167           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
9168         }
9169
9170       /* Emit debug info for the current line number, choosing the encoding
9171          that uses the least amount of space.  */
9172       if (line_info->dw_line_num != current_line)
9173         {
9174           line_offset = line_info->dw_line_num - current_line;
9175           line_delta = line_offset - DWARF_LINE_BASE;
9176           current_line = line_info->dw_line_num;
9177           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
9178             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
9179                                  "line %lu", current_line);
9180           else
9181             {
9182               dw2_asm_output_data (1, DW_LNS_advance_line,
9183                                    "advance to line %lu", current_line);
9184               dw2_asm_output_data_sleb128 (line_offset, NULL);
9185               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
9186             }
9187         }
9188       else
9189         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
9190
9191 #if 0
9192     cont:
9193 #endif
9194
9195       lt_index++;
9196
9197       /* If we're done with a function, end its sequence.  */
9198       if (lt_index == separate_line_info_table_in_use
9199           || separate_line_info_table[lt_index].function != function)
9200         {
9201           current_file = 1;
9202           current_line = 1;
9203
9204           /* Emit debug info for the address of the end of the function.  */
9205           ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
9206           if (0)
9207             {
9208               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
9209                                    "DW_LNS_fixed_advance_pc");
9210               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
9211             }
9212           else
9213             {
9214               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9215               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9216               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9217               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9218             }
9219
9220           /* Output the marker for the end of this sequence.  */
9221           dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
9222           dw2_asm_output_data_uleb128 (1, NULL);
9223           dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
9224         }
9225     }
9226
9227   /* Output the marker for the end of the line number info.  */
9228   ASM_OUTPUT_LABEL (asm_out_file, l2);
9229 }
9230 \f
9231 /* Given a pointer to a tree node for some base type, return a pointer to
9232    a DIE that describes the given type.
9233
9234    This routine must only be called for GCC type nodes that correspond to
9235    Dwarf base (fundamental) types.  */
9236
9237 static dw_die_ref
9238 base_type_die (tree type)
9239 {
9240   dw_die_ref base_type_result;
9241   enum dwarf_type encoding;
9242
9243   if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
9244     return 0;
9245
9246   switch (TREE_CODE (type))
9247     {
9248     case INTEGER_TYPE:
9249       if (TYPE_STRING_FLAG (type))
9250         {
9251           if (TYPE_UNSIGNED (type))
9252             encoding = DW_ATE_unsigned_char;
9253           else
9254             encoding = DW_ATE_signed_char;
9255         }
9256       else if (TYPE_UNSIGNED (type))
9257         encoding = DW_ATE_unsigned;
9258       else
9259         encoding = DW_ATE_signed;
9260       break;
9261
9262     case REAL_TYPE:
9263       if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
9264         encoding = DW_ATE_decimal_float;
9265       else
9266         encoding = DW_ATE_float;
9267       break;
9268
9269     case FIXED_POINT_TYPE:
9270       if (TYPE_UNSIGNED (type))
9271         encoding = DW_ATE_unsigned_fixed;
9272       else
9273         encoding = DW_ATE_signed_fixed;
9274       break;
9275
9276       /* Dwarf2 doesn't know anything about complex ints, so use
9277          a user defined type for it.  */
9278     case COMPLEX_TYPE:
9279       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
9280         encoding = DW_ATE_complex_float;
9281       else
9282         encoding = DW_ATE_lo_user;
9283       break;
9284
9285     case BOOLEAN_TYPE:
9286       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
9287       encoding = DW_ATE_boolean;
9288       break;
9289
9290     default:
9291       /* No other TREE_CODEs are Dwarf fundamental types.  */
9292       gcc_unreachable ();
9293     }
9294
9295   base_type_result = new_die (DW_TAG_base_type, comp_unit_die, type);
9296
9297   /* This probably indicates a bug.  */
9298   if (! TYPE_NAME (type))
9299     add_name_attribute (base_type_result, "__unknown__");
9300
9301   add_AT_unsigned (base_type_result, DW_AT_byte_size,
9302                    int_size_in_bytes (type));
9303   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
9304
9305   return base_type_result;
9306 }
9307
9308 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
9309    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
9310
9311 static inline int
9312 is_base_type (tree type)
9313 {
9314   switch (TREE_CODE (type))
9315     {
9316     case ERROR_MARK:
9317     case VOID_TYPE:
9318     case INTEGER_TYPE:
9319     case REAL_TYPE:
9320     case FIXED_POINT_TYPE:
9321     case COMPLEX_TYPE:
9322     case BOOLEAN_TYPE:
9323       return 1;
9324
9325     case ARRAY_TYPE:
9326     case RECORD_TYPE:
9327     case UNION_TYPE:
9328     case QUAL_UNION_TYPE:
9329     case ENUMERAL_TYPE:
9330     case FUNCTION_TYPE:
9331     case METHOD_TYPE:
9332     case POINTER_TYPE:
9333     case REFERENCE_TYPE:
9334     case OFFSET_TYPE:
9335     case LANG_TYPE:
9336     case VECTOR_TYPE:
9337       return 0;
9338
9339     default:
9340       gcc_unreachable ();
9341     }
9342
9343   return 0;
9344 }
9345
9346 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
9347    node, return the size in bits for the type if it is a constant, or else
9348    return the alignment for the type if the type's size is not constant, or
9349    else return BITS_PER_WORD if the type actually turns out to be an
9350    ERROR_MARK node.  */
9351
9352 static inline unsigned HOST_WIDE_INT
9353 simple_type_size_in_bits (const_tree type)
9354 {
9355   if (TREE_CODE (type) == ERROR_MARK)
9356     return BITS_PER_WORD;
9357   else if (TYPE_SIZE (type) == NULL_TREE)
9358     return 0;
9359   else if (host_integerp (TYPE_SIZE (type), 1))
9360     return tree_low_cst (TYPE_SIZE (type), 1);
9361   else
9362     return TYPE_ALIGN (type);
9363 }
9364
9365 /* Return true if the debug information for the given type should be
9366    emitted as a subrange type.  */
9367
9368 static inline bool
9369 is_subrange_type (const_tree type)
9370 {
9371   tree subtype = TREE_TYPE (type);
9372
9373   /* Subrange types are identified by the fact that they are integer
9374      types, and that they have a subtype which is either an integer type
9375      or an enumeral type.  */
9376
9377   if (TREE_CODE (type) != INTEGER_TYPE
9378       || subtype == NULL_TREE)
9379     return false;
9380
9381   if (TREE_CODE (subtype) != INTEGER_TYPE
9382       && TREE_CODE (subtype) != ENUMERAL_TYPE
9383       && TREE_CODE (subtype) != BOOLEAN_TYPE)
9384     return false;
9385
9386   if (TREE_CODE (type) == TREE_CODE (subtype)
9387       && int_size_in_bytes (type) == int_size_in_bytes (subtype)
9388       && TYPE_MIN_VALUE (type) != NULL
9389       && TYPE_MIN_VALUE (subtype) != NULL
9390       && tree_int_cst_equal (TYPE_MIN_VALUE (type), TYPE_MIN_VALUE (subtype))
9391       && TYPE_MAX_VALUE (type) != NULL
9392       && TYPE_MAX_VALUE (subtype) != NULL
9393       && tree_int_cst_equal (TYPE_MAX_VALUE (type), TYPE_MAX_VALUE (subtype)))
9394     {
9395       /* The type and its subtype have the same representation.  If in
9396          addition the two types also have the same name, then the given
9397          type is not a subrange type, but rather a plain base type.  */
9398       /* FIXME: brobecker/2004-03-22:
9399          Sizetype INTEGER_CSTs nodes are canonicalized.  It should
9400          therefore be sufficient to check the TYPE_SIZE node pointers
9401          rather than checking the actual size.  Unfortunately, we have
9402          found some cases, such as in the Ada "integer" type, where
9403          this is not the case.  Until this problem is solved, we need to
9404          keep checking the actual size.  */
9405       tree type_name = TYPE_NAME (type);
9406       tree subtype_name = TYPE_NAME (subtype);
9407
9408       if (type_name != NULL && TREE_CODE (type_name) == TYPE_DECL)
9409         type_name = DECL_NAME (type_name);
9410
9411       if (subtype_name != NULL && TREE_CODE (subtype_name) == TYPE_DECL)
9412         subtype_name = DECL_NAME (subtype_name);
9413
9414       if (type_name == subtype_name)
9415         return false;
9416     }
9417
9418   return true;
9419 }
9420
9421 /*  Given a pointer to a tree node for a subrange type, return a pointer
9422     to a DIE that describes the given type.  */
9423
9424 static dw_die_ref
9425 subrange_type_die (tree type, dw_die_ref context_die)
9426 {
9427   dw_die_ref subrange_die;
9428   const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
9429
9430   if (context_die == NULL)
9431     context_die = comp_unit_die;
9432
9433   subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
9434
9435   if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
9436     {
9437       /* The size of the subrange type and its base type do not match,
9438          so we need to generate a size attribute for the subrange type.  */
9439       add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
9440     }
9441
9442   if (TYPE_MIN_VALUE (type) != NULL)
9443     add_bound_info (subrange_die, DW_AT_lower_bound,
9444                     TYPE_MIN_VALUE (type));
9445   if (TYPE_MAX_VALUE (type) != NULL)
9446     add_bound_info (subrange_die, DW_AT_upper_bound,
9447                     TYPE_MAX_VALUE (type));
9448
9449   return subrange_die;
9450 }
9451
9452 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
9453    entry that chains various modifiers in front of the given type.  */
9454
9455 static dw_die_ref
9456 modified_type_die (tree type, int is_const_type, int is_volatile_type,
9457                    dw_die_ref context_die)
9458 {
9459   enum tree_code code = TREE_CODE (type);
9460   dw_die_ref mod_type_die;
9461   dw_die_ref sub_die = NULL;
9462   tree item_type = NULL;
9463   tree qualified_type;
9464   tree name;
9465
9466   if (code == ERROR_MARK)
9467     return NULL;
9468
9469   /* See if we already have the appropriately qualified variant of
9470      this type.  */
9471   qualified_type
9472     = get_qualified_type (type,
9473                           ((is_const_type ? TYPE_QUAL_CONST : 0)
9474                            | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
9475
9476   /* If we do, then we can just use its DIE, if it exists.  */
9477   if (qualified_type)
9478     {
9479       mod_type_die = lookup_type_die (qualified_type);
9480       if (mod_type_die)
9481         return mod_type_die;
9482     }
9483
9484   name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
9485
9486   /* Handle C typedef types.  */
9487   if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name))
9488     {
9489       tree dtype = TREE_TYPE (name);
9490
9491       if (qualified_type == dtype)
9492         {
9493           /* For a named type, use the typedef.  */
9494           gen_type_die (qualified_type, context_die);
9495           return lookup_type_die (qualified_type);
9496         }
9497       else if (is_const_type < TYPE_READONLY (dtype)
9498                || is_volatile_type < TYPE_VOLATILE (dtype)
9499                || (is_const_type <= TYPE_READONLY (dtype)
9500                    && is_volatile_type <= TYPE_VOLATILE (dtype)
9501                    && DECL_ORIGINAL_TYPE (name) != type))
9502         /* cv-unqualified version of named type.  Just use the unnamed
9503            type to which it refers.  */
9504         return modified_type_die (DECL_ORIGINAL_TYPE (name),
9505                                   is_const_type, is_volatile_type,
9506                                   context_die);
9507       /* Else cv-qualified version of named type; fall through.  */
9508     }
9509
9510   if (is_const_type)
9511     {
9512       mod_type_die = new_die (DW_TAG_const_type, comp_unit_die, type);
9513       sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
9514     }
9515   else if (is_volatile_type)
9516     {
9517       mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die, type);
9518       sub_die = modified_type_die (type, 0, 0, context_die);
9519     }
9520   else if (code == POINTER_TYPE)
9521     {
9522       mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die, type);
9523       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
9524                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
9525       item_type = TREE_TYPE (type);
9526     }
9527   else if (code == REFERENCE_TYPE)
9528     {
9529       mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die, type);
9530       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
9531                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
9532       item_type = TREE_TYPE (type);
9533     }
9534   else if (is_subrange_type (type))
9535     {
9536       mod_type_die = subrange_type_die (type, context_die);
9537       item_type = TREE_TYPE (type);
9538     }
9539   else if (is_base_type (type))
9540     mod_type_die = base_type_die (type);
9541   else
9542     {
9543       gen_type_die (type, context_die);
9544
9545       /* We have to get the type_main_variant here (and pass that to the
9546          `lookup_type_die' routine) because the ..._TYPE node we have
9547          might simply be a *copy* of some original type node (where the
9548          copy was created to help us keep track of typedef names) and
9549          that copy might have a different TYPE_UID from the original
9550          ..._TYPE node.  */
9551       if (TREE_CODE (type) != VECTOR_TYPE)
9552         return lookup_type_die (type_main_variant (type));
9553       else
9554         /* Vectors have the debugging information in the type,
9555            not the main variant.  */
9556         return lookup_type_die (type);
9557     }
9558
9559   /* Builtin types don't have a DECL_ORIGINAL_TYPE.  For those,
9560      don't output a DW_TAG_typedef, since there isn't one in the
9561      user's program; just attach a DW_AT_name to the type.  */
9562   if (name
9563       && (TREE_CODE (name) != TYPE_DECL
9564           || (TREE_TYPE (name) == qualified_type && DECL_NAME (name))))
9565     {
9566       if (TREE_CODE (name) == TYPE_DECL)
9567         /* Could just call add_name_and_src_coords_attributes here,
9568            but since this is a builtin type it doesn't have any
9569            useful source coordinates anyway.  */
9570         name = DECL_NAME (name);
9571       add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
9572     }
9573
9574   if (qualified_type)
9575     equate_type_number_to_die (qualified_type, mod_type_die);
9576
9577   if (item_type)
9578     /* We must do this after the equate_type_number_to_die call, in case
9579        this is a recursive type.  This ensures that the modified_type_die
9580        recursion will terminate even if the type is recursive.  Recursive
9581        types are possible in Ada.  */
9582     sub_die = modified_type_die (item_type,
9583                                  TYPE_READONLY (item_type),
9584                                  TYPE_VOLATILE (item_type),
9585                                  context_die);
9586
9587   if (sub_die != NULL)
9588     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
9589
9590   return mod_type_die;
9591 }
9592
9593 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
9594    an enumerated type.  */
9595
9596 static inline int
9597 type_is_enum (const_tree type)
9598 {
9599   return TREE_CODE (type) == ENUMERAL_TYPE;
9600 }
9601
9602 /* Return the DBX register number described by a given RTL node.  */
9603
9604 static unsigned int
9605 dbx_reg_number (const_rtx rtl)
9606 {
9607   unsigned regno = REGNO (rtl);
9608
9609   gcc_assert (regno < FIRST_PSEUDO_REGISTER);
9610
9611 #ifdef LEAF_REG_REMAP
9612   if (current_function_uses_only_leaf_regs)
9613     {
9614       int leaf_reg = LEAF_REG_REMAP (regno);
9615       if (leaf_reg != -1)
9616         regno = (unsigned) leaf_reg;
9617     }
9618 #endif
9619
9620   return DBX_REGISTER_NUMBER (regno);
9621 }
9622
9623 /* Optionally add a DW_OP_piece term to a location description expression.
9624    DW_OP_piece is only added if the location description expression already
9625    doesn't end with DW_OP_piece.  */
9626
9627 static void
9628 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
9629 {
9630   dw_loc_descr_ref loc;
9631
9632   if (*list_head != NULL)
9633     {
9634       /* Find the end of the chain.  */
9635       for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
9636         ;
9637
9638       if (loc->dw_loc_opc != DW_OP_piece)
9639         loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
9640     }
9641 }
9642
9643 /* Return a location descriptor that designates a machine register or
9644    zero if there is none.  */
9645
9646 static dw_loc_descr_ref
9647 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
9648 {
9649   rtx regs;
9650
9651   if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
9652     return 0;
9653
9654   regs = targetm.dwarf_register_span (rtl);
9655
9656   if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
9657     return multiple_reg_loc_descriptor (rtl, regs, initialized);
9658   else
9659     return one_reg_loc_descriptor (dbx_reg_number (rtl), initialized);
9660 }
9661
9662 /* Return a location descriptor that designates a machine register for
9663    a given hard register number.  */
9664
9665 static dw_loc_descr_ref
9666 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
9667 {
9668   dw_loc_descr_ref reg_loc_descr = new_reg_loc_descr (regno, 0);
9669
9670   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
9671     add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9672
9673   return reg_loc_descr;
9674 }
9675
9676 /* Given an RTL of a register, return a location descriptor that
9677    designates a value that spans more than one register.  */
9678
9679 static dw_loc_descr_ref
9680 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
9681                              enum var_init_status initialized)
9682 {
9683   int nregs, size, i;
9684   unsigned reg;
9685   dw_loc_descr_ref loc_result = NULL;
9686
9687   reg = REGNO (rtl);
9688 #ifdef LEAF_REG_REMAP
9689   if (current_function_uses_only_leaf_regs)
9690     {
9691       int leaf_reg = LEAF_REG_REMAP (reg);
9692       if (leaf_reg != -1)
9693         reg = (unsigned) leaf_reg;
9694     }
9695 #endif
9696   gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
9697   nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
9698
9699   /* Simple, contiguous registers.  */
9700   if (regs == NULL_RTX)
9701     {
9702       size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
9703
9704       loc_result = NULL;
9705       while (nregs--)
9706         {
9707           dw_loc_descr_ref t;
9708
9709           t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
9710                                       VAR_INIT_STATUS_INITIALIZED);
9711           add_loc_descr (&loc_result, t);
9712           add_loc_descr_op_piece (&loc_result, size);
9713           ++reg;
9714         }
9715       return loc_result;
9716     }
9717
9718   /* Now onto stupid register sets in non contiguous locations.  */
9719
9720   gcc_assert (GET_CODE (regs) == PARALLEL);
9721
9722   size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
9723   loc_result = NULL;
9724
9725   for (i = 0; i < XVECLEN (regs, 0); ++i)
9726     {
9727       dw_loc_descr_ref t;
9728
9729       t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
9730                                   VAR_INIT_STATUS_INITIALIZED);
9731       add_loc_descr (&loc_result, t);
9732       size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
9733       add_loc_descr_op_piece (&loc_result, size);
9734     }
9735
9736   if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
9737     add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9738   return loc_result;
9739 }
9740
9741 #endif /* DWARF2_DEBUGGING_INFO */
9742
9743 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
9744
9745 /* Return a location descriptor that designates a constant.  */
9746
9747 static dw_loc_descr_ref
9748 int_loc_descriptor (HOST_WIDE_INT i)
9749 {
9750   enum dwarf_location_atom op;
9751
9752   /* Pick the smallest representation of a constant, rather than just
9753      defaulting to the LEB encoding.  */
9754   if (i >= 0)
9755     {
9756       if (i <= 31)
9757         op = DW_OP_lit0 + i;
9758       else if (i <= 0xff)
9759         op = DW_OP_const1u;
9760       else if (i <= 0xffff)
9761         op = DW_OP_const2u;
9762       else if (HOST_BITS_PER_WIDE_INT == 32
9763                || i <= 0xffffffff)
9764         op = DW_OP_const4u;
9765       else
9766         op = DW_OP_constu;
9767     }
9768   else
9769     {
9770       if (i >= -0x80)
9771         op = DW_OP_const1s;
9772       else if (i >= -0x8000)
9773         op = DW_OP_const2s;
9774       else if (HOST_BITS_PER_WIDE_INT == 32
9775                || i >= -0x80000000)
9776         op = DW_OP_const4s;
9777       else
9778         op = DW_OP_consts;
9779     }
9780
9781   return new_loc_descr (op, i, 0);
9782 }
9783 #endif
9784
9785 #ifdef DWARF2_DEBUGGING_INFO
9786
9787 /* Return a location descriptor that designates a base+offset location.  */
9788
9789 static dw_loc_descr_ref
9790 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
9791                  enum var_init_status initialized)
9792 {
9793   unsigned int regno;
9794   dw_loc_descr_ref result;
9795   dw_fde_ref fde = current_fde ();
9796
9797   /* We only use "frame base" when we're sure we're talking about the
9798      post-prologue local stack frame.  We do this by *not* running
9799      register elimination until this point, and recognizing the special
9800      argument pointer and soft frame pointer rtx's.  */
9801   if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
9802     {
9803       rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
9804
9805       if (elim != reg)
9806         {
9807           if (GET_CODE (elim) == PLUS)
9808             {
9809               offset += INTVAL (XEXP (elim, 1));
9810               elim = XEXP (elim, 0);
9811             }
9812           gcc_assert ((SUPPORTS_STACK_ALIGNMENT
9813                        && (elim == hard_frame_pointer_rtx
9814                            || elim == stack_pointer_rtx))
9815                       || elim == (frame_pointer_needed
9816                                   ? hard_frame_pointer_rtx
9817                                   : stack_pointer_rtx));
9818
9819           /* If drap register is used to align stack, use frame
9820              pointer + offset to access stack variables.  If stack
9821              is aligned without drap, use stack pointer + offset to
9822              access stack variables.  */
9823           if (crtl->stack_realign_tried
9824               && cfa.reg == HARD_FRAME_POINTER_REGNUM
9825               && reg == frame_pointer_rtx)
9826             {
9827               int base_reg
9828                 = DWARF_FRAME_REGNUM (cfa.indirect
9829                                       ? HARD_FRAME_POINTER_REGNUM
9830                                       : STACK_POINTER_REGNUM);
9831               return new_reg_loc_descr (base_reg, offset);
9832             }
9833
9834           offset += frame_pointer_fb_offset;
9835           return new_loc_descr (DW_OP_fbreg, offset, 0);
9836         }
9837     }
9838   else if (fde
9839            && fde->drap_reg != INVALID_REGNUM
9840            && (fde->drap_reg == REGNO (reg)
9841                || fde->vdrap_reg == REGNO (reg)))
9842     {
9843       /* Use cfa+offset to represent the location of arguments passed
9844          on stack when drap is used to align stack.  */
9845       return new_loc_descr (DW_OP_fbreg, offset, 0);
9846     }
9847
9848   regno = dbx_reg_number (reg);
9849   if (regno <= 31)
9850     result = new_loc_descr (DW_OP_breg0 + regno, offset, 0);
9851   else
9852     result = new_loc_descr (DW_OP_bregx, regno, offset);
9853
9854   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
9855     add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9856
9857   return result;
9858 }
9859
9860 /* Return true if this RTL expression describes a base+offset calculation.  */
9861
9862 static inline int
9863 is_based_loc (const_rtx rtl)
9864 {
9865   return (GET_CODE (rtl) == PLUS
9866           && ((REG_P (XEXP (rtl, 0))
9867                && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
9868                && GET_CODE (XEXP (rtl, 1)) == CONST_INT)));
9869 }
9870
9871 /* Return a descriptor that describes the concatenation of N locations
9872    used to form the address of a memory location.  */
9873
9874 static dw_loc_descr_ref
9875 concatn_mem_loc_descriptor (rtx concatn, enum machine_mode mode,
9876                             enum var_init_status initialized)
9877 {
9878   unsigned int i;
9879   dw_loc_descr_ref cc_loc_result = NULL;
9880   unsigned int n = XVECLEN (concatn, 0);
9881
9882   for (i = 0; i < n; ++i)
9883     {
9884       dw_loc_descr_ref ref;
9885       rtx x = XVECEXP (concatn, 0, i);
9886
9887       ref = mem_loc_descriptor (x, mode, VAR_INIT_STATUS_INITIALIZED);
9888       if (ref == NULL)
9889         return NULL;
9890
9891       add_loc_descr (&cc_loc_result, ref);
9892       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
9893     }
9894
9895   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
9896     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9897
9898   return cc_loc_result;
9899 }
9900
9901 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
9902    failed.  */
9903
9904 static dw_loc_descr_ref
9905 tls_mem_loc_descriptor (rtx mem)
9906 {
9907   tree base;
9908   dw_loc_descr_ref loc_result, loc_result2;
9909
9910   if (MEM_EXPR (mem) == NULL_TREE || MEM_OFFSET (mem) == NULL_RTX)
9911     return NULL;
9912
9913   base = get_base_address (MEM_EXPR (mem));
9914   if (base == NULL
9915       || TREE_CODE (base) != VAR_DECL
9916       || !DECL_THREAD_LOCAL_P (base))
9917     return NULL;
9918
9919   loc_result = loc_descriptor_from_tree_1 (MEM_EXPR (mem), 2);
9920   if (loc_result == NULL)
9921     return NULL;
9922
9923   if (INTVAL (MEM_OFFSET (mem)))
9924     {
9925       if (INTVAL (MEM_OFFSET (mem)) >= 0)
9926         add_loc_descr (&loc_result,
9927                        new_loc_descr (DW_OP_plus_uconst,
9928                                       INTVAL (MEM_OFFSET (mem)), 0));
9929       else
9930         {
9931           loc_result2 = mem_loc_descriptor (MEM_OFFSET (mem), GET_MODE (mem),
9932                                             VAR_INIT_STATUS_INITIALIZED);
9933           if (loc_result2 == 0)
9934             return NULL;
9935           add_loc_descr (&loc_result, loc_result2);
9936           add_loc_descr (&loc_result, new_loc_descr (DW_OP_plus, 0, 0));
9937         }
9938     }
9939
9940   return loc_result;
9941 }
9942
9943 /* The following routine converts the RTL for a variable or parameter
9944    (resident in memory) into an equivalent Dwarf representation of a
9945    mechanism for getting the address of that same variable onto the top of a
9946    hypothetical "address evaluation" stack.
9947
9948    When creating memory location descriptors, we are effectively transforming
9949    the RTL for a memory-resident object into its Dwarf postfix expression
9950    equivalent.  This routine recursively descends an RTL tree, turning
9951    it into Dwarf postfix code as it goes.
9952
9953    MODE is the mode of the memory reference, needed to handle some
9954    autoincrement addressing modes.
9955
9956    CAN_USE_FBREG is a flag whether we can use DW_AT_frame_base in the
9957    location list for RTL.
9958
9959    Return 0 if we can't represent the location.  */
9960
9961 static dw_loc_descr_ref
9962 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
9963                     enum var_init_status initialized)
9964 {
9965   dw_loc_descr_ref mem_loc_result = NULL;
9966   enum dwarf_location_atom op;
9967
9968   /* Note that for a dynamically sized array, the location we will generate a
9969      description of here will be the lowest numbered location which is
9970      actually within the array.  That's *not* necessarily the same as the
9971      zeroth element of the array.  */
9972
9973   rtl = targetm.delegitimize_address (rtl);
9974
9975   switch (GET_CODE (rtl))
9976     {
9977     case POST_INC:
9978     case POST_DEC:
9979     case POST_MODIFY:
9980       /* POST_INC and POST_DEC can be handled just like a SUBREG.  So we
9981          just fall into the SUBREG code.  */
9982
9983       /* ... fall through ...  */
9984
9985     case SUBREG:
9986       /* The case of a subreg may arise when we have a local (register)
9987          variable or a formal (register) parameter which doesn't quite fill
9988          up an entire register.  For now, just assume that it is
9989          legitimate to make the Dwarf info refer to the whole register which
9990          contains the given subreg.  */
9991       rtl = XEXP (rtl, 0);
9992
9993       /* ... fall through ...  */
9994
9995     case REG:
9996       /* Whenever a register number forms a part of the description of the
9997          method for calculating the (dynamic) address of a memory resident
9998          object, DWARF rules require the register number be referred to as
9999          a "base register".  This distinction is not based in any way upon
10000          what category of register the hardware believes the given register
10001          belongs to.  This is strictly DWARF terminology we're dealing with
10002          here. Note that in cases where the location of a memory-resident
10003          data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
10004          OP_CONST (0)) the actual DWARF location descriptor that we generate
10005          may just be OP_BASEREG (basereg).  This may look deceptively like
10006          the object in question was allocated to a register (rather than in
10007          memory) so DWARF consumers need to be aware of the subtle
10008          distinction between OP_REG and OP_BASEREG.  */
10009       if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
10010         mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
10011       else if (stack_realign_drap
10012                && crtl->drap_reg
10013                && crtl->args.internal_arg_pointer == rtl
10014                && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
10015         {
10016           /* If RTL is internal_arg_pointer, which has been optimized
10017              out, use DRAP instead.  */
10018           mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
10019                                             VAR_INIT_STATUS_INITIALIZED);
10020         }
10021       break;
10022
10023     case MEM:
10024       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
10025                                            VAR_INIT_STATUS_INITIALIZED);
10026       if (mem_loc_result == NULL)
10027         mem_loc_result = tls_mem_loc_descriptor (rtl);
10028       if (mem_loc_result != 0)
10029         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
10030       break;
10031
10032     case LO_SUM:
10033          rtl = XEXP (rtl, 1);
10034
10035       /* ... fall through ...  */
10036
10037     case LABEL_REF:
10038       /* Some ports can transform a symbol ref into a label ref, because
10039          the symbol ref is too far away and has to be dumped into a constant
10040          pool.  */
10041     case CONST:
10042     case SYMBOL_REF:
10043       /* Alternatively, the symbol in the constant pool might be referenced
10044          by a different symbol.  */
10045       if (GET_CODE (rtl) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (rtl))
10046         {
10047           bool marked;
10048           rtx tmp = get_pool_constant_mark (rtl, &marked);
10049
10050           if (GET_CODE (tmp) == SYMBOL_REF)
10051             {
10052               rtl = tmp;
10053               if (CONSTANT_POOL_ADDRESS_P (tmp))
10054                 get_pool_constant_mark (tmp, &marked);
10055               else
10056                 marked = true;
10057             }
10058
10059           /* If all references to this pool constant were optimized away,
10060              it was not output and thus we can't represent it.
10061              FIXME: might try to use DW_OP_const_value here, though
10062              DW_OP_piece complicates it.  */
10063           if (!marked)
10064             return 0;
10065         }
10066
10067       mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
10068       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
10069       mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
10070       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
10071       break;
10072
10073     case PRE_MODIFY:
10074       /* Extract the PLUS expression nested inside and fall into
10075          PLUS code below.  */
10076       rtl = XEXP (rtl, 1);
10077       goto plus;
10078
10079     case PRE_INC:
10080     case PRE_DEC:
10081       /* Turn these into a PLUS expression and fall into the PLUS code
10082          below.  */
10083       rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
10084                           GEN_INT (GET_CODE (rtl) == PRE_INC
10085                                    ? GET_MODE_UNIT_SIZE (mode)
10086                                    : -GET_MODE_UNIT_SIZE (mode)));
10087
10088       /* ... fall through ...  */
10089
10090     case PLUS:
10091     plus:
10092       if (is_based_loc (rtl))
10093         mem_loc_result = based_loc_descr (XEXP (rtl, 0),
10094                                           INTVAL (XEXP (rtl, 1)),
10095                                           VAR_INIT_STATUS_INITIALIZED);
10096       else
10097         {
10098           mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode,
10099                                                VAR_INIT_STATUS_INITIALIZED);
10100           if (mem_loc_result == 0)
10101             break;
10102
10103           if (GET_CODE (XEXP (rtl, 1)) == CONST_INT
10104               && INTVAL (XEXP (rtl, 1)) >= 0)
10105             add_loc_descr (&mem_loc_result,
10106                            new_loc_descr (DW_OP_plus_uconst,
10107                                           INTVAL (XEXP (rtl, 1)), 0));
10108           else
10109             {
10110               dw_loc_descr_ref mem_loc_result2
10111                 = mem_loc_descriptor (XEXP (rtl, 1), mode,
10112                                       VAR_INIT_STATUS_INITIALIZED);
10113               if (mem_loc_result2 == 0)
10114                 break;
10115               add_loc_descr (&mem_loc_result, mem_loc_result2);
10116               add_loc_descr (&mem_loc_result,
10117                              new_loc_descr (DW_OP_plus, 0, 0));
10118             }
10119         }
10120       break;
10121
10122     /* If a pseudo-reg is optimized away, it is possible for it to
10123        be replaced with a MEM containing a multiply or shift.  */
10124     case MULT:
10125       op = DW_OP_mul;
10126       goto do_binop;
10127
10128     case ASHIFT:
10129       op = DW_OP_shl;
10130       goto do_binop;
10131
10132     case ASHIFTRT:
10133       op = DW_OP_shra;
10134       goto do_binop;
10135
10136     case LSHIFTRT:
10137       op = DW_OP_shr;
10138       goto do_binop;
10139
10140     do_binop:
10141       {
10142         dw_loc_descr_ref op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
10143                                                    VAR_INIT_STATUS_INITIALIZED);
10144         dw_loc_descr_ref op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
10145                                                    VAR_INIT_STATUS_INITIALIZED);
10146
10147         if (op0 == 0 || op1 == 0)
10148           break;
10149
10150         mem_loc_result = op0;
10151         add_loc_descr (&mem_loc_result, op1);
10152         add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
10153         break;
10154       }
10155
10156     case CONST_INT:
10157       mem_loc_result = int_loc_descriptor (INTVAL (rtl));
10158       break;
10159
10160     case CONCATN:
10161       mem_loc_result = concatn_mem_loc_descriptor (rtl, mode,
10162                                                    VAR_INIT_STATUS_INITIALIZED);
10163       break;
10164
10165     case UNSPEC:
10166       /* If delegitimize_address couldn't do anything with the UNSPEC, we
10167          can't express it in the debug info.  This can happen e.g. with some
10168          TLS UNSPECs.  */
10169       break;
10170
10171     default:
10172       gcc_unreachable ();
10173     }
10174
10175   if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10176     add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10177
10178   return mem_loc_result;
10179 }
10180
10181 /* Return a descriptor that describes the concatenation of two locations.
10182    This is typically a complex variable.  */
10183
10184 static dw_loc_descr_ref
10185 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
10186 {
10187   dw_loc_descr_ref cc_loc_result = NULL;
10188   dw_loc_descr_ref x0_ref = loc_descriptor (x0, VAR_INIT_STATUS_INITIALIZED);
10189   dw_loc_descr_ref x1_ref = loc_descriptor (x1, VAR_INIT_STATUS_INITIALIZED);
10190
10191   if (x0_ref == 0 || x1_ref == 0)
10192     return 0;
10193
10194   cc_loc_result = x0_ref;
10195   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
10196
10197   add_loc_descr (&cc_loc_result, x1_ref);
10198   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
10199
10200   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10201     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10202
10203   return cc_loc_result;
10204 }
10205
10206 /* Return a descriptor that describes the concatenation of N
10207    locations.  */
10208
10209 static dw_loc_descr_ref
10210 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
10211 {
10212   unsigned int i;
10213   dw_loc_descr_ref cc_loc_result = NULL;
10214   unsigned int n = XVECLEN (concatn, 0);
10215
10216   for (i = 0; i < n; ++i)
10217     {
10218       dw_loc_descr_ref ref;
10219       rtx x = XVECEXP (concatn, 0, i);
10220
10221       ref = loc_descriptor (x, VAR_INIT_STATUS_INITIALIZED);
10222       if (ref == NULL)
10223         return NULL;
10224
10225       add_loc_descr (&cc_loc_result, ref);
10226       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
10227     }
10228
10229   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10230     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10231
10232   return cc_loc_result;
10233 }
10234
10235 /* Output a proper Dwarf location descriptor for a variable or parameter
10236    which is either allocated in a register or in a memory location.  For a
10237    register, we just generate an OP_REG and the register number.  For a
10238    memory location we provide a Dwarf postfix expression describing how to
10239    generate the (dynamic) address of the object onto the address stack.
10240
10241    If we don't know how to describe it, return 0.  */
10242
10243 static dw_loc_descr_ref
10244 loc_descriptor (rtx rtl, enum var_init_status initialized)
10245 {
10246   dw_loc_descr_ref loc_result = NULL;
10247
10248   switch (GET_CODE (rtl))
10249     {
10250     case SUBREG:
10251       /* The case of a subreg may arise when we have a local (register)
10252          variable or a formal (register) parameter which doesn't quite fill
10253          up an entire register.  For now, just assume that it is
10254          legitimate to make the Dwarf info refer to the whole register which
10255          contains the given subreg.  */
10256       rtl = SUBREG_REG (rtl);
10257
10258       /* ... fall through ...  */
10259
10260     case REG:
10261       loc_result = reg_loc_descriptor (rtl, initialized);
10262       break;
10263
10264     case MEM:
10265       loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
10266                                        initialized);
10267       if (loc_result == NULL)
10268         loc_result = tls_mem_loc_descriptor (rtl);
10269       break;
10270
10271     case CONCAT:
10272       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
10273                                           initialized);
10274       break;
10275
10276     case CONCATN:
10277       loc_result = concatn_loc_descriptor (rtl, initialized);
10278       break;
10279
10280     case VAR_LOCATION:
10281       /* Single part.  */
10282       if (GET_CODE (XEXP (rtl, 1)) != PARALLEL)
10283         {
10284           loc_result = loc_descriptor (XEXP (XEXP (rtl, 1), 0), initialized);
10285           break;
10286         }
10287
10288       rtl = XEXP (rtl, 1);
10289       /* FALLTHRU */
10290
10291     case PARALLEL:
10292       {
10293         rtvec par_elems = XVEC (rtl, 0);
10294         int num_elem = GET_NUM_ELEM (par_elems);
10295         enum machine_mode mode;
10296         int i;
10297
10298         /* Create the first one, so we have something to add to.  */
10299         loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
10300                                      initialized);
10301         if (loc_result == NULL)
10302           return NULL;
10303         mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
10304         add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
10305         for (i = 1; i < num_elem; i++)
10306           {
10307             dw_loc_descr_ref temp;
10308
10309             temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
10310                                    initialized);
10311             if (temp == NULL)
10312               return NULL;
10313             add_loc_descr (&loc_result, temp);
10314             mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
10315             add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
10316           }
10317       }
10318       break;
10319
10320     default:
10321       gcc_unreachable ();
10322     }
10323
10324   return loc_result;
10325 }
10326
10327 /* Similar, but generate the descriptor from trees instead of rtl.  This comes
10328    up particularly with variable length arrays.  WANT_ADDRESS is 2 if this is
10329    a top-level invocation of loc_descriptor_from_tree; is 1 if this is not a
10330    top-level invocation, and we require the address of LOC; is 0 if we require
10331    the value of LOC.  */
10332
10333 static dw_loc_descr_ref
10334 loc_descriptor_from_tree_1 (tree loc, int want_address)
10335 {
10336   dw_loc_descr_ref ret, ret1;
10337   int have_address = 0;
10338   enum dwarf_location_atom op;
10339
10340   /* ??? Most of the time we do not take proper care for sign/zero
10341      extending the values properly.  Hopefully this won't be a real
10342      problem...  */
10343
10344   switch (TREE_CODE (loc))
10345     {
10346     case ERROR_MARK:
10347       return 0;
10348
10349     case PLACEHOLDER_EXPR:
10350       /* This case involves extracting fields from an object to determine the
10351          position of other fields.  We don't try to encode this here.  The
10352          only user of this is Ada, which encodes the needed information using
10353          the names of types.  */
10354       return 0;
10355
10356     case CALL_EXPR:
10357       return 0;
10358
10359     case PREINCREMENT_EXPR:
10360     case PREDECREMENT_EXPR:
10361     case POSTINCREMENT_EXPR:
10362     case POSTDECREMENT_EXPR:
10363       /* There are no opcodes for these operations.  */
10364       return 0;
10365
10366     case ADDR_EXPR:
10367       /* If we already want an address, there's nothing we can do.  */
10368       if (want_address)
10369         return 0;
10370
10371       /* Otherwise, process the argument and look for the address.  */
10372       return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 1);
10373
10374     case VAR_DECL:
10375       if (DECL_THREAD_LOCAL_P (loc))
10376         {
10377           rtx rtl;
10378           unsigned first_op;
10379           unsigned second_op;
10380
10381           if (targetm.have_tls)
10382             {
10383               /* If this is not defined, we have no way to emit the
10384                  data.  */
10385               if (!targetm.asm_out.output_dwarf_dtprel)
10386                 return 0;
10387
10388                /* The way DW_OP_GNU_push_tls_address is specified, we
10389                   can only look up addresses of objects in the current
10390                   module.  */
10391               if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
10392                 return 0;
10393               first_op = INTERNAL_DW_OP_tls_addr;
10394               second_op = DW_OP_GNU_push_tls_address;
10395             }
10396           else
10397             {
10398               if (!targetm.emutls.debug_form_tls_address)
10399                 return 0;
10400               loc = emutls_decl (loc);
10401               first_op = DW_OP_addr;
10402               second_op = DW_OP_form_tls_address;
10403             }
10404
10405           rtl = rtl_for_decl_location (loc);
10406           if (rtl == NULL_RTX)
10407             return 0;
10408
10409           if (!MEM_P (rtl))
10410             return 0;
10411           rtl = XEXP (rtl, 0);
10412           if (! CONSTANT_P (rtl))
10413             return 0;
10414
10415           ret = new_loc_descr (first_op, 0, 0);
10416           ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
10417           ret->dw_loc_oprnd1.v.val_addr = rtl;
10418
10419           ret1 = new_loc_descr (second_op, 0, 0);
10420           add_loc_descr (&ret, ret1);
10421
10422           have_address = 1;
10423           break;
10424         }
10425       /* FALLTHRU */
10426
10427     case PARM_DECL:
10428       if (DECL_HAS_VALUE_EXPR_P (loc))
10429         return loc_descriptor_from_tree_1 (DECL_VALUE_EXPR (loc),
10430                                            want_address);
10431       /* FALLTHRU */
10432
10433     case RESULT_DECL:
10434     case FUNCTION_DECL:
10435       {
10436         rtx rtl = rtl_for_decl_location (loc);
10437
10438         if (rtl == NULL_RTX)
10439           return 0;
10440         else if (GET_CODE (rtl) == CONST_INT)
10441           {
10442             HOST_WIDE_INT val = INTVAL (rtl);
10443             if (TYPE_UNSIGNED (TREE_TYPE (loc)))
10444               val &= GET_MODE_MASK (DECL_MODE (loc));
10445             ret = int_loc_descriptor (val);
10446           }
10447         else if (GET_CODE (rtl) == CONST_STRING)
10448           return 0;
10449         else if (CONSTANT_P (rtl))
10450           {
10451             ret = new_loc_descr (DW_OP_addr, 0, 0);
10452             ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
10453             ret->dw_loc_oprnd1.v.val_addr = rtl;
10454           }
10455         else
10456           {
10457             enum machine_mode mode;
10458
10459             /* Certain constructs can only be represented at top-level.  */
10460             if (want_address == 2)
10461               return loc_descriptor (rtl, VAR_INIT_STATUS_INITIALIZED);
10462
10463             mode = GET_MODE (rtl);
10464             if (MEM_P (rtl))
10465               {
10466                 rtl = XEXP (rtl, 0);
10467                 have_address = 1;
10468               }
10469             ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
10470           }
10471       }
10472       break;
10473
10474     case INDIRECT_REF:
10475       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
10476       have_address = 1;
10477       break;
10478
10479     case COMPOUND_EXPR:
10480       return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), want_address);
10481
10482     CASE_CONVERT:
10483     case VIEW_CONVERT_EXPR:
10484     case SAVE_EXPR:
10485     case MODIFY_EXPR:
10486       return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), want_address);
10487
10488     case COMPONENT_REF:
10489     case BIT_FIELD_REF:
10490     case ARRAY_REF:
10491     case ARRAY_RANGE_REF:
10492       {
10493         tree obj, offset;
10494         HOST_WIDE_INT bitsize, bitpos, bytepos;
10495         enum machine_mode mode;
10496         int volatilep;
10497         int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
10498
10499         obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
10500                                    &unsignedp, &volatilep, false);
10501
10502         if (obj == loc)
10503           return 0;
10504
10505         ret = loc_descriptor_from_tree_1 (obj, 1);
10506         if (ret == 0
10507             || bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
10508           return 0;
10509
10510         if (offset != NULL_TREE)
10511           {
10512             /* Variable offset.  */
10513             ret1 = loc_descriptor_from_tree_1 (offset, 0);
10514             if (ret1 == 0)
10515               return 0;
10516             add_loc_descr (&ret, ret1);
10517             add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
10518           }
10519
10520         bytepos = bitpos / BITS_PER_UNIT;
10521         if (bytepos > 0)
10522           add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
10523         else if (bytepos < 0)
10524           {
10525             add_loc_descr (&ret, int_loc_descriptor (bytepos));
10526             add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
10527           }
10528
10529         have_address = 1;
10530         break;
10531       }
10532
10533     case INTEGER_CST:
10534       if (host_integerp (loc, 0))
10535         ret = int_loc_descriptor (tree_low_cst (loc, 0));
10536       else
10537         return 0;
10538       break;
10539
10540     case CONSTRUCTOR:
10541       {
10542         /* Get an RTL for this, if something has been emitted.  */
10543         rtx rtl = lookup_constant_def (loc);
10544         enum machine_mode mode;
10545
10546         if (!rtl || !MEM_P (rtl))
10547           return 0;
10548         mode = GET_MODE (rtl);
10549         rtl = XEXP (rtl, 0);
10550         ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
10551         have_address = 1;
10552         break;
10553       }
10554
10555     case TRUTH_AND_EXPR:
10556     case TRUTH_ANDIF_EXPR:
10557     case BIT_AND_EXPR:
10558       op = DW_OP_and;
10559       goto do_binop;
10560
10561     case TRUTH_XOR_EXPR:
10562     case BIT_XOR_EXPR:
10563       op = DW_OP_xor;
10564       goto do_binop;
10565
10566     case TRUTH_OR_EXPR:
10567     case TRUTH_ORIF_EXPR:
10568     case BIT_IOR_EXPR:
10569       op = DW_OP_or;
10570       goto do_binop;
10571
10572     case FLOOR_DIV_EXPR:
10573     case CEIL_DIV_EXPR:
10574     case ROUND_DIV_EXPR:
10575     case TRUNC_DIV_EXPR:
10576       op = DW_OP_div;
10577       goto do_binop;
10578
10579     case MINUS_EXPR:
10580       op = DW_OP_minus;
10581       goto do_binop;
10582
10583     case FLOOR_MOD_EXPR:
10584     case CEIL_MOD_EXPR:
10585     case ROUND_MOD_EXPR:
10586     case TRUNC_MOD_EXPR:
10587       op = DW_OP_mod;
10588       goto do_binop;
10589
10590     case MULT_EXPR:
10591       op = DW_OP_mul;
10592       goto do_binop;
10593
10594     case LSHIFT_EXPR:
10595       op = DW_OP_shl;
10596       goto do_binop;
10597
10598     case RSHIFT_EXPR:
10599       op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
10600       goto do_binop;
10601
10602     case POINTER_PLUS_EXPR:
10603     case PLUS_EXPR:
10604       if (TREE_CODE (TREE_OPERAND (loc, 1)) == INTEGER_CST
10605           && host_integerp (TREE_OPERAND (loc, 1), 0))
10606         {
10607           ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
10608           if (ret == 0)
10609             return 0;
10610
10611           add_loc_descr (&ret,
10612                          new_loc_descr (DW_OP_plus_uconst,
10613                                         tree_low_cst (TREE_OPERAND (loc, 1),
10614                                                       0),
10615                                         0));
10616           break;
10617         }
10618
10619       op = DW_OP_plus;
10620       goto do_binop;
10621
10622     case LE_EXPR:
10623       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
10624         return 0;
10625
10626       op = DW_OP_le;
10627       goto do_binop;
10628
10629     case GE_EXPR:
10630       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
10631         return 0;
10632
10633       op = DW_OP_ge;
10634       goto do_binop;
10635
10636     case LT_EXPR:
10637       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
10638         return 0;
10639
10640       op = DW_OP_lt;
10641       goto do_binop;
10642
10643     case GT_EXPR:
10644       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
10645         return 0;
10646
10647       op = DW_OP_gt;
10648       goto do_binop;
10649
10650     case EQ_EXPR:
10651       op = DW_OP_eq;
10652       goto do_binop;
10653
10654     case NE_EXPR:
10655       op = DW_OP_ne;
10656       goto do_binop;
10657
10658     do_binop:
10659       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
10660       ret1 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
10661       if (ret == 0 || ret1 == 0)
10662         return 0;
10663
10664       add_loc_descr (&ret, ret1);
10665       add_loc_descr (&ret, new_loc_descr (op, 0, 0));
10666       break;
10667
10668     case TRUTH_NOT_EXPR:
10669     case BIT_NOT_EXPR:
10670       op = DW_OP_not;
10671       goto do_unop;
10672
10673     case ABS_EXPR:
10674       op = DW_OP_abs;
10675       goto do_unop;
10676
10677     case NEGATE_EXPR:
10678       op = DW_OP_neg;
10679       goto do_unop;
10680
10681     do_unop:
10682       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
10683       if (ret == 0)
10684         return 0;
10685
10686       add_loc_descr (&ret, new_loc_descr (op, 0, 0));
10687       break;
10688
10689     case MIN_EXPR:
10690     case MAX_EXPR:
10691       {
10692         const enum tree_code code =
10693           TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
10694
10695         loc = build3 (COND_EXPR, TREE_TYPE (loc),
10696                       build2 (code, integer_type_node,
10697                               TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
10698                       TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
10699       }
10700
10701       /* ... fall through ...  */
10702
10703     case COND_EXPR:
10704       {
10705         dw_loc_descr_ref lhs
10706           = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
10707         dw_loc_descr_ref rhs
10708           = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 2), 0);
10709         dw_loc_descr_ref bra_node, jump_node, tmp;
10710
10711         ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
10712         if (ret == 0 || lhs == 0 || rhs == 0)
10713           return 0;
10714
10715         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
10716         add_loc_descr (&ret, bra_node);
10717
10718         add_loc_descr (&ret, rhs);
10719         jump_node = new_loc_descr (DW_OP_skip, 0, 0);
10720         add_loc_descr (&ret, jump_node);
10721
10722         add_loc_descr (&ret, lhs);
10723         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
10724         bra_node->dw_loc_oprnd1.v.val_loc = lhs;
10725
10726         /* ??? Need a node to point the skip at.  Use a nop.  */
10727         tmp = new_loc_descr (DW_OP_nop, 0, 0);
10728         add_loc_descr (&ret, tmp);
10729         jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
10730         jump_node->dw_loc_oprnd1.v.val_loc = tmp;
10731       }
10732       break;
10733
10734     case FIX_TRUNC_EXPR:
10735       return 0;
10736
10737     default:
10738       /* Leave front-end specific codes as simply unknown.  This comes
10739          up, for instance, with the C STMT_EXPR.  */
10740       if ((unsigned int) TREE_CODE (loc)
10741           >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
10742         return 0;
10743
10744 #ifdef ENABLE_CHECKING
10745       /* Otherwise this is a generic code; we should just lists all of
10746          these explicitly.  We forgot one.  */
10747       gcc_unreachable ();
10748 #else
10749       /* In a release build, we want to degrade gracefully: better to
10750          generate incomplete debugging information than to crash.  */
10751       return NULL;
10752 #endif
10753     }
10754
10755   /* Show if we can't fill the request for an address.  */
10756   if (want_address && !have_address)
10757     return 0;
10758
10759   /* If we've got an address and don't want one, dereference.  */
10760   if (!want_address && have_address && ret)
10761     {
10762       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
10763
10764       if (size > DWARF2_ADDR_SIZE || size == -1)
10765         return 0;
10766       else if (size == DWARF2_ADDR_SIZE)
10767         op = DW_OP_deref;
10768       else
10769         op = DW_OP_deref_size;
10770
10771       add_loc_descr (&ret, new_loc_descr (op, size, 0));
10772     }
10773
10774   return ret;
10775 }
10776
10777 static inline dw_loc_descr_ref
10778 loc_descriptor_from_tree (tree loc)
10779 {
10780   return loc_descriptor_from_tree_1 (loc, 2);
10781 }
10782
10783 /* Given a value, round it up to the lowest multiple of `boundary'
10784    which is not less than the value itself.  */
10785
10786 static inline HOST_WIDE_INT
10787 ceiling (HOST_WIDE_INT value, unsigned int boundary)
10788 {
10789   return (((value + boundary - 1) / boundary) * boundary);
10790 }
10791
10792 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
10793    pointer to the declared type for the relevant field variable, or return
10794    `integer_type_node' if the given node turns out to be an
10795    ERROR_MARK node.  */
10796
10797 static inline tree
10798 field_type (const_tree decl)
10799 {
10800   tree type;
10801
10802   if (TREE_CODE (decl) == ERROR_MARK)
10803     return integer_type_node;
10804
10805   type = DECL_BIT_FIELD_TYPE (decl);
10806   if (type == NULL_TREE)
10807     type = TREE_TYPE (decl);
10808
10809   return type;
10810 }
10811
10812 /* Given a pointer to a tree node, return the alignment in bits for
10813    it, or else return BITS_PER_WORD if the node actually turns out to
10814    be an ERROR_MARK node.  */
10815
10816 static inline unsigned
10817 simple_type_align_in_bits (const_tree type)
10818 {
10819   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
10820 }
10821
10822 static inline unsigned
10823 simple_decl_align_in_bits (const_tree decl)
10824 {
10825   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
10826 }
10827
10828 /* Return the result of rounding T up to ALIGN.  */
10829
10830 static inline HOST_WIDE_INT
10831 round_up_to_align (HOST_WIDE_INT t, unsigned int align)
10832 {
10833   /* We must be careful if T is negative because HOST_WIDE_INT can be
10834      either "above" or "below" unsigned int as per the C promotion
10835      rules, depending on the host, thus making the signedness of the
10836      direct multiplication and division unpredictable.  */
10837   unsigned HOST_WIDE_INT u = (unsigned HOST_WIDE_INT) t;
10838
10839   u += align - 1;
10840   u /= align;
10841   u *= align;
10842
10843   return (HOST_WIDE_INT) u;
10844 }
10845
10846 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
10847    lowest addressed byte of the "containing object" for the given FIELD_DECL,
10848    or return 0 if we are unable to determine what that offset is, either
10849    because the argument turns out to be a pointer to an ERROR_MARK node, or
10850    because the offset is actually variable.  (We can't handle the latter case
10851    just yet).  */
10852
10853 static HOST_WIDE_INT
10854 field_byte_offset (const_tree decl)
10855 {
10856   HOST_WIDE_INT object_offset_in_bits;
10857   HOST_WIDE_INT bitpos_int;
10858
10859   if (TREE_CODE (decl) == ERROR_MARK)
10860     return 0;
10861
10862   gcc_assert (TREE_CODE (decl) == FIELD_DECL);
10863
10864   /* We cannot yet cope with fields whose positions are variable, so
10865      for now, when we see such things, we simply return 0.  Someday, we may
10866      be able to handle such cases, but it will be damn difficult.  */
10867   if (! host_integerp (bit_position (decl), 0))
10868     return 0;
10869
10870   bitpos_int = int_bit_position (decl);
10871
10872 #ifdef PCC_BITFIELD_TYPE_MATTERS
10873   if (PCC_BITFIELD_TYPE_MATTERS)
10874     {
10875       tree type;
10876       tree field_size_tree;
10877       HOST_WIDE_INT deepest_bitpos;
10878       unsigned HOST_WIDE_INT field_size_in_bits;
10879       unsigned int type_align_in_bits;
10880       unsigned int decl_align_in_bits;
10881       unsigned HOST_WIDE_INT type_size_in_bits;
10882
10883       type = field_type (decl);
10884       field_size_tree = DECL_SIZE (decl);
10885
10886       /* The size could be unspecified if there was an error, or for
10887          a flexible array member.  */
10888       if (! field_size_tree)
10889         field_size_tree = bitsize_zero_node;
10890
10891       /* If we don't know the size of the field, pretend it's a full word.  */
10892       if (host_integerp (field_size_tree, 1))
10893         field_size_in_bits = tree_low_cst (field_size_tree, 1);
10894       else
10895         field_size_in_bits = BITS_PER_WORD;
10896
10897       type_size_in_bits = simple_type_size_in_bits (type);
10898       type_align_in_bits = simple_type_align_in_bits (type);
10899       decl_align_in_bits = simple_decl_align_in_bits (decl);
10900
10901       /* The GCC front-end doesn't make any attempt to keep track of the
10902          starting bit offset (relative to the start of the containing
10903          structure type) of the hypothetical "containing object" for a
10904          bit-field.  Thus, when computing the byte offset value for the
10905          start of the "containing object" of a bit-field, we must deduce
10906          this information on our own. This can be rather tricky to do in
10907          some cases.  For example, handling the following structure type
10908          definition when compiling for an i386/i486 target (which only
10909          aligns long long's to 32-bit boundaries) can be very tricky:
10910
10911          struct S { int field1; long long field2:31; };
10912
10913          Fortunately, there is a simple rule-of-thumb which can be used
10914          in such cases.  When compiling for an i386/i486, GCC will
10915          allocate 8 bytes for the structure shown above.  It decides to
10916          do this based upon one simple rule for bit-field allocation.
10917          GCC allocates each "containing object" for each bit-field at
10918          the first (i.e. lowest addressed) legitimate alignment boundary
10919          (based upon the required minimum alignment for the declared
10920          type of the field) which it can possibly use, subject to the
10921          condition that there is still enough available space remaining
10922          in the containing object (when allocated at the selected point)
10923          to fully accommodate all of the bits of the bit-field itself.
10924
10925          This simple rule makes it obvious why GCC allocates 8 bytes for
10926          each object of the structure type shown above.  When looking
10927          for a place to allocate the "containing object" for `field2',
10928          the compiler simply tries to allocate a 64-bit "containing
10929          object" at each successive 32-bit boundary (starting at zero)
10930          until it finds a place to allocate that 64- bit field such that
10931          at least 31 contiguous (and previously unallocated) bits remain
10932          within that selected 64 bit field.  (As it turns out, for the
10933          example above, the compiler finds it is OK to allocate the
10934          "containing object" 64-bit field at bit-offset zero within the
10935          structure type.)
10936
10937          Here we attempt to work backwards from the limited set of facts
10938          we're given, and we try to deduce from those facts, where GCC
10939          must have believed that the containing object started (within
10940          the structure type). The value we deduce is then used (by the
10941          callers of this routine) to generate DW_AT_location and
10942          DW_AT_bit_offset attributes for fields (both bit-fields and, in
10943          the case of DW_AT_location, regular fields as well).  */
10944
10945       /* Figure out the bit-distance from the start of the structure to
10946          the "deepest" bit of the bit-field.  */
10947       deepest_bitpos = bitpos_int + field_size_in_bits;
10948
10949       /* This is the tricky part.  Use some fancy footwork to deduce
10950          where the lowest addressed bit of the containing object must
10951          be.  */
10952       object_offset_in_bits = deepest_bitpos - type_size_in_bits;
10953
10954       /* Round up to type_align by default.  This works best for
10955          bitfields.  */
10956       object_offset_in_bits
10957         = round_up_to_align (object_offset_in_bits, type_align_in_bits);
10958
10959       if (object_offset_in_bits > bitpos_int)
10960         {
10961           object_offset_in_bits = deepest_bitpos - type_size_in_bits;
10962
10963           /* Round up to decl_align instead.  */
10964           object_offset_in_bits
10965             = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
10966         }
10967     }
10968   else
10969 #endif
10970     object_offset_in_bits = bitpos_int;
10971
10972   return object_offset_in_bits / BITS_PER_UNIT;
10973 }
10974 \f
10975 /* The following routines define various Dwarf attributes and any data
10976    associated with them.  */
10977
10978 /* Add a location description attribute value to a DIE.
10979
10980    This emits location attributes suitable for whole variables and
10981    whole parameters.  Note that the location attributes for struct fields are
10982    generated by the routine `data_member_location_attribute' below.  */
10983
10984 static inline void
10985 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
10986                              dw_loc_descr_ref descr)
10987 {
10988   if (descr != 0)
10989     add_AT_loc (die, attr_kind, descr);
10990 }
10991
10992 /* Attach the specialized form of location attribute used for data members of
10993    struct and union types.  In the special case of a FIELD_DECL node which
10994    represents a bit-field, the "offset" part of this special location
10995    descriptor must indicate the distance in bytes from the lowest-addressed
10996    byte of the containing struct or union type to the lowest-addressed byte of
10997    the "containing object" for the bit-field.  (See the `field_byte_offset'
10998    function above).
10999
11000    For any given bit-field, the "containing object" is a hypothetical object
11001    (of some integral or enum type) within which the given bit-field lives.  The
11002    type of this hypothetical "containing object" is always the same as the
11003    declared type of the individual bit-field itself (for GCC anyway... the
11004    DWARF spec doesn't actually mandate this).  Note that it is the size (in
11005    bytes) of the hypothetical "containing object" which will be given in the
11006    DW_AT_byte_size attribute for this bit-field.  (See the
11007    `byte_size_attribute' function below.)  It is also used when calculating the
11008    value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
11009    function below.)  */
11010
11011 static void
11012 add_data_member_location_attribute (dw_die_ref die, tree decl)
11013 {
11014   HOST_WIDE_INT offset;
11015   dw_loc_descr_ref loc_descr = 0;
11016
11017   if (TREE_CODE (decl) == TREE_BINFO)
11018     {
11019       /* We're working on the TAG_inheritance for a base class.  */
11020       if (BINFO_VIRTUAL_P (decl) && is_cxx ())
11021         {
11022           /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
11023              aren't at a fixed offset from all (sub)objects of the same
11024              type.  We need to extract the appropriate offset from our
11025              vtable.  The following dwarf expression means
11026
11027                BaseAddr = ObAddr + *((*ObAddr) - Offset)
11028
11029              This is specific to the V3 ABI, of course.  */
11030
11031           dw_loc_descr_ref tmp;
11032
11033           /* Make a copy of the object address.  */
11034           tmp = new_loc_descr (DW_OP_dup, 0, 0);
11035           add_loc_descr (&loc_descr, tmp);
11036
11037           /* Extract the vtable address.  */
11038           tmp = new_loc_descr (DW_OP_deref, 0, 0);
11039           add_loc_descr (&loc_descr, tmp);
11040
11041           /* Calculate the address of the offset.  */
11042           offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
11043           gcc_assert (offset < 0);
11044
11045           tmp = int_loc_descriptor (-offset);
11046           add_loc_descr (&loc_descr, tmp);
11047           tmp = new_loc_descr (DW_OP_minus, 0, 0);
11048           add_loc_descr (&loc_descr, tmp);
11049
11050           /* Extract the offset.  */
11051           tmp = new_loc_descr (DW_OP_deref, 0, 0);
11052           add_loc_descr (&loc_descr, tmp);
11053
11054           /* Add it to the object address.  */
11055           tmp = new_loc_descr (DW_OP_plus, 0, 0);
11056           add_loc_descr (&loc_descr, tmp);
11057         }
11058       else
11059         offset = tree_low_cst (BINFO_OFFSET (decl), 0);
11060     }
11061   else
11062     offset = field_byte_offset (decl);
11063
11064   if (! loc_descr)
11065     {
11066       enum dwarf_location_atom op;
11067
11068       /* The DWARF2 standard says that we should assume that the structure
11069          address is already on the stack, so we can specify a structure field
11070          address by using DW_OP_plus_uconst.  */
11071
11072 #ifdef MIPS_DEBUGGING_INFO
11073       /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
11074          operator correctly.  It works only if we leave the offset on the
11075          stack.  */
11076       op = DW_OP_constu;
11077 #else
11078       op = DW_OP_plus_uconst;
11079 #endif
11080
11081       loc_descr = new_loc_descr (op, offset, 0);
11082     }
11083
11084   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
11085 }
11086
11087 /* Writes integer values to dw_vec_const array.  */
11088
11089 static void
11090 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
11091 {
11092   while (size != 0)
11093     {
11094       *dest++ = val & 0xff;
11095       val >>= 8;
11096       --size;
11097     }
11098 }
11099
11100 /* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
11101
11102 static HOST_WIDE_INT
11103 extract_int (const unsigned char *src, unsigned int size)
11104 {
11105   HOST_WIDE_INT val = 0;
11106
11107   src += size;
11108   while (size != 0)
11109     {
11110       val <<= 8;
11111       val |= *--src & 0xff;
11112       --size;
11113     }
11114   return val;
11115 }
11116
11117 /* Writes floating point values to dw_vec_const array.  */
11118
11119 static void
11120 insert_float (const_rtx rtl, unsigned char *array)
11121 {
11122   REAL_VALUE_TYPE rv;
11123   long val[4];
11124   int i;
11125
11126   REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
11127   real_to_target (val, &rv, GET_MODE (rtl));
11128
11129   /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
11130   for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
11131     {
11132       insert_int (val[i], 4, array);
11133       array += 4;
11134     }
11135 }
11136
11137 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
11138    does not have a "location" either in memory or in a register.  These
11139    things can arise in GNU C when a constant is passed as an actual parameter
11140    to an inlined function.  They can also arise in C++ where declared
11141    constants do not necessarily get memory "homes".  */
11142
11143 static void
11144 add_const_value_attribute (dw_die_ref die, rtx rtl)
11145 {
11146   switch (GET_CODE (rtl))
11147     {
11148     case CONST_INT:
11149       {
11150         HOST_WIDE_INT val = INTVAL (rtl);
11151
11152         if (val < 0)
11153           add_AT_int (die, DW_AT_const_value, val);
11154         else
11155           add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
11156       }
11157       break;
11158
11159     case CONST_DOUBLE:
11160       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
11161          floating-point constant.  A CONST_DOUBLE is used whenever the
11162          constant requires more than one word in order to be adequately
11163          represented.  We output CONST_DOUBLEs as blocks.  */
11164       {
11165         enum machine_mode mode = GET_MODE (rtl);
11166
11167         if (SCALAR_FLOAT_MODE_P (mode))
11168           {
11169             unsigned int length = GET_MODE_SIZE (mode);
11170             unsigned char *array = GGC_NEWVEC (unsigned char, length);
11171
11172             insert_float (rtl, array);
11173             add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
11174           }
11175         else
11176           {
11177             /* ??? We really should be using HOST_WIDE_INT throughout.  */
11178             gcc_assert (HOST_BITS_PER_LONG == HOST_BITS_PER_WIDE_INT);
11179
11180             add_AT_long_long (die, DW_AT_const_value,
11181                               CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
11182           }
11183       }
11184       break;
11185
11186     case CONST_VECTOR:
11187       {
11188         enum machine_mode mode = GET_MODE (rtl);
11189         unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
11190         unsigned int length = CONST_VECTOR_NUNITS (rtl);
11191         unsigned char *array = GGC_NEWVEC (unsigned char, length * elt_size);
11192         unsigned int i;
11193         unsigned char *p;
11194
11195         switch (GET_MODE_CLASS (mode))
11196           {
11197           case MODE_VECTOR_INT:
11198             for (i = 0, p = array; i < length; i++, p += elt_size)
11199               {
11200                 rtx elt = CONST_VECTOR_ELT (rtl, i);
11201                 HOST_WIDE_INT lo, hi;
11202
11203                 switch (GET_CODE (elt))
11204                   {
11205                   case CONST_INT:
11206                     lo = INTVAL (elt);
11207                     hi = -(lo < 0);
11208                     break;
11209
11210                   case CONST_DOUBLE:
11211                     lo = CONST_DOUBLE_LOW (elt);
11212                     hi = CONST_DOUBLE_HIGH (elt);
11213                     break;
11214
11215                   default:
11216                     gcc_unreachable ();
11217                   }
11218
11219                 if (elt_size <= sizeof (HOST_WIDE_INT))
11220                   insert_int (lo, elt_size, p);
11221                 else
11222                   {
11223                     unsigned char *p0 = p;
11224                     unsigned char *p1 = p + sizeof (HOST_WIDE_INT);
11225
11226                     gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
11227                     if (WORDS_BIG_ENDIAN)
11228                       {
11229                         p0 = p1;
11230                         p1 = p;
11231                       }
11232                     insert_int (lo, sizeof (HOST_WIDE_INT), p0);
11233                     insert_int (hi, sizeof (HOST_WIDE_INT), p1);
11234                   }
11235               }
11236             break;
11237
11238           case MODE_VECTOR_FLOAT:
11239             for (i = 0, p = array; i < length; i++, p += elt_size)
11240               {
11241                 rtx elt = CONST_VECTOR_ELT (rtl, i);
11242                 insert_float (elt, p);
11243               }
11244             break;
11245
11246           default:
11247             gcc_unreachable ();
11248           }
11249
11250         add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
11251       }
11252       break;
11253
11254     case CONST_STRING:
11255       add_AT_string (die, DW_AT_const_value, XSTR (rtl, 0));
11256       break;
11257
11258     case SYMBOL_REF:
11259     case LABEL_REF:
11260     case CONST:
11261       add_AT_addr (die, DW_AT_const_value, rtl);
11262       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
11263       break;
11264
11265     case PLUS:
11266       /* In cases where an inlined instance of an inline function is passed
11267          the address of an `auto' variable (which is local to the caller) we
11268          can get a situation where the DECL_RTL of the artificial local
11269          variable (for the inlining) which acts as a stand-in for the
11270          corresponding formal parameter (of the inline function) will look
11271          like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
11272          exactly a compile-time constant expression, but it isn't the address
11273          of the (artificial) local variable either.  Rather, it represents the
11274          *value* which the artificial local variable always has during its
11275          lifetime.  We currently have no way to represent such quasi-constant
11276          values in Dwarf, so for now we just punt and generate nothing.  */
11277       break;
11278
11279     default:
11280       /* No other kinds of rtx should be possible here.  */
11281       gcc_unreachable ();
11282     }
11283
11284 }
11285
11286 /* Determine whether the evaluation of EXPR references any variables
11287    or functions which aren't otherwise used (and therefore may not be
11288    output).  */
11289 static tree
11290 reference_to_unused (tree * tp, int * walk_subtrees,
11291                      void * data ATTRIBUTE_UNUSED)
11292 {
11293   if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
11294     *walk_subtrees = 0;
11295
11296   if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
11297       && ! TREE_ASM_WRITTEN (*tp))
11298     return *tp;
11299   /* ???  The C++ FE emits debug information for using decls, so
11300      putting gcc_unreachable here falls over.  See PR31899.  For now
11301      be conservative.  */
11302   else if (!cgraph_global_info_ready
11303            && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
11304     return *tp;
11305   else if (DECL_P (*tp) && TREE_CODE (*tp) == VAR_DECL)
11306     {
11307       struct varpool_node *node = varpool_node (*tp);
11308       if (!node->needed)
11309         return *tp;
11310     }
11311   else if (DECL_P (*tp) && TREE_CODE (*tp) == FUNCTION_DECL
11312            && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
11313     {
11314       struct cgraph_node *node = cgraph_node (*tp);
11315       if (!node->output)
11316         return *tp;
11317     }
11318   else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
11319     return *tp;
11320
11321   return NULL_TREE;
11322 }
11323
11324 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
11325    for use in a later add_const_value_attribute call.  */
11326
11327 static rtx
11328 rtl_for_decl_init (tree init, tree type)
11329 {
11330   rtx rtl = NULL_RTX;
11331
11332   /* If a variable is initialized with a string constant without embedded
11333      zeros, build CONST_STRING.  */
11334   if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
11335     {
11336       tree enttype = TREE_TYPE (type);
11337       tree domain = TYPE_DOMAIN (type);
11338       enum machine_mode mode = TYPE_MODE (enttype);
11339
11340       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
11341           && domain
11342           && integer_zerop (TYPE_MIN_VALUE (domain))
11343           && compare_tree_int (TYPE_MAX_VALUE (domain),
11344                                TREE_STRING_LENGTH (init) - 1) == 0
11345           && ((size_t) TREE_STRING_LENGTH (init)
11346               == strlen (TREE_STRING_POINTER (init)) + 1))
11347         rtl = gen_rtx_CONST_STRING (VOIDmode,
11348                                     ggc_strdup (TREE_STRING_POINTER (init)));
11349     }
11350   /* Other aggregates, and complex values, could be represented using
11351      CONCAT: FIXME!  */
11352   else if (AGGREGATE_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
11353     ;
11354   /* Vectors only work if their mode is supported by the target.
11355      FIXME: generic vectors ought to work too.  */
11356   else if (TREE_CODE (type) == VECTOR_TYPE && TYPE_MODE (type) == BLKmode)
11357     ;
11358   /* If the initializer is something that we know will expand into an
11359      immediate RTL constant, expand it now.  We must be careful not to
11360      reference variables which won't be output.  */
11361   else if (initializer_constant_valid_p (init, type)
11362            && ! walk_tree (&init, reference_to_unused, NULL, NULL))
11363     {
11364       /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
11365          possible.  */
11366       if (TREE_CODE (type) == VECTOR_TYPE)
11367         switch (TREE_CODE (init))
11368           {
11369           case VECTOR_CST:
11370             break;
11371           case CONSTRUCTOR:
11372             if (TREE_CONSTANT (init))
11373               {
11374                 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
11375                 bool constant_p = true;
11376                 tree value;
11377                 unsigned HOST_WIDE_INT ix;
11378
11379                 /* Even when ctor is constant, it might contain non-*_CST
11380                    elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
11381                    belong into VECTOR_CST nodes.  */
11382                 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
11383                   if (!CONSTANT_CLASS_P (value))
11384                     {
11385                       constant_p = false;
11386                       break;
11387                     }
11388
11389                 if (constant_p)
11390                   {
11391                     init = build_vector_from_ctor (type, elts);
11392                     break;
11393                   }
11394               }
11395             /* FALLTHRU */
11396
11397           default:
11398             return NULL;
11399           }
11400
11401       rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
11402
11403       /* If expand_expr returns a MEM, it wasn't immediate.  */
11404       gcc_assert (!rtl || !MEM_P (rtl));
11405     }
11406
11407   return rtl;
11408 }
11409
11410 /* Generate RTL for the variable DECL to represent its location.  */
11411
11412 static rtx
11413 rtl_for_decl_location (tree decl)
11414 {
11415   rtx rtl;
11416
11417   /* Here we have to decide where we are going to say the parameter "lives"
11418      (as far as the debugger is concerned).  We only have a couple of
11419      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
11420
11421      DECL_RTL normally indicates where the parameter lives during most of the
11422      activation of the function.  If optimization is enabled however, this
11423      could be either NULL or else a pseudo-reg.  Both of those cases indicate
11424      that the parameter doesn't really live anywhere (as far as the code
11425      generation parts of GCC are concerned) during most of the function's
11426      activation.  That will happen (for example) if the parameter is never
11427      referenced within the function.
11428
11429      We could just generate a location descriptor here for all non-NULL
11430      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
11431      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
11432      where DECL_RTL is NULL or is a pseudo-reg.
11433
11434      Note however that we can only get away with using DECL_INCOMING_RTL as
11435      a backup substitute for DECL_RTL in certain limited cases.  In cases
11436      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
11437      we can be sure that the parameter was passed using the same type as it is
11438      declared to have within the function, and that its DECL_INCOMING_RTL
11439      points us to a place where a value of that type is passed.
11440
11441      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
11442      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
11443      because in these cases DECL_INCOMING_RTL points us to a value of some
11444      type which is *different* from the type of the parameter itself.  Thus,
11445      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
11446      such cases, the debugger would end up (for example) trying to fetch a
11447      `float' from a place which actually contains the first part of a
11448      `double'.  That would lead to really incorrect and confusing
11449      output at debug-time.
11450
11451      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
11452      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
11453      are a couple of exceptions however.  On little-endian machines we can
11454      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
11455      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
11456      an integral type that is smaller than TREE_TYPE (decl). These cases arise
11457      when (on a little-endian machine) a non-prototyped function has a
11458      parameter declared to be of type `short' or `char'.  In such cases,
11459      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
11460      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
11461      passed `int' value.  If the debugger then uses that address to fetch
11462      a `short' or a `char' (on a little-endian machine) the result will be
11463      the correct data, so we allow for such exceptional cases below.
11464
11465      Note that our goal here is to describe the place where the given formal
11466      parameter lives during most of the function's activation (i.e. between the
11467      end of the prologue and the start of the epilogue).  We'll do that as best
11468      as we can. Note however that if the given formal parameter is modified
11469      sometime during the execution of the function, then a stack backtrace (at
11470      debug-time) will show the function as having been called with the *new*
11471      value rather than the value which was originally passed in.  This happens
11472      rarely enough that it is not a major problem, but it *is* a problem, and
11473      I'd like to fix it.
11474
11475      A future version of dwarf2out.c may generate two additional attributes for
11476      any given DW_TAG_formal_parameter DIE which will describe the "passed
11477      type" and the "passed location" for the given formal parameter in addition
11478      to the attributes we now generate to indicate the "declared type" and the
11479      "active location" for each parameter.  This additional set of attributes
11480      could be used by debuggers for stack backtraces. Separately, note that
11481      sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
11482      This happens (for example) for inlined-instances of inline function formal
11483      parameters which are never referenced.  This really shouldn't be
11484      happening.  All PARM_DECL nodes should get valid non-NULL
11485      DECL_INCOMING_RTL values.  FIXME.  */
11486
11487   /* Use DECL_RTL as the "location" unless we find something better.  */
11488   rtl = DECL_RTL_IF_SET (decl);
11489
11490   /* When generating abstract instances, ignore everything except
11491      constants, symbols living in memory, and symbols living in
11492      fixed registers.  */
11493   if (! reload_completed)
11494     {
11495       if (rtl
11496           && (CONSTANT_P (rtl)
11497               || (MEM_P (rtl)
11498                   && CONSTANT_P (XEXP (rtl, 0)))
11499               || (REG_P (rtl)
11500                   && TREE_CODE (decl) == VAR_DECL
11501                   && TREE_STATIC (decl))))
11502         {
11503           rtl = targetm.delegitimize_address (rtl);
11504           return rtl;
11505         }
11506       rtl = NULL_RTX;
11507     }
11508   else if (TREE_CODE (decl) == PARM_DECL)
11509     {
11510       if (rtl == NULL_RTX || is_pseudo_reg (rtl))
11511         {
11512           tree declared_type = TREE_TYPE (decl);
11513           tree passed_type = DECL_ARG_TYPE (decl);
11514           enum machine_mode dmode = TYPE_MODE (declared_type);
11515           enum machine_mode pmode = TYPE_MODE (passed_type);
11516
11517           /* This decl represents a formal parameter which was optimized out.
11518              Note that DECL_INCOMING_RTL may be NULL in here, but we handle
11519              all cases where (rtl == NULL_RTX) just below.  */
11520           if (dmode == pmode)
11521             rtl = DECL_INCOMING_RTL (decl);
11522           else if (SCALAR_INT_MODE_P (dmode)
11523                    && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
11524                    && DECL_INCOMING_RTL (decl))
11525             {
11526               rtx inc = DECL_INCOMING_RTL (decl);
11527               if (REG_P (inc))
11528                 rtl = inc;
11529               else if (MEM_P (inc))
11530                 {
11531                   if (BYTES_BIG_ENDIAN)
11532                     rtl = adjust_address_nv (inc, dmode,
11533                                              GET_MODE_SIZE (pmode)
11534                                              - GET_MODE_SIZE (dmode));
11535                   else
11536                     rtl = inc;
11537                 }
11538             }
11539         }
11540
11541       /* If the parm was passed in registers, but lives on the stack, then
11542          make a big endian correction if the mode of the type of the
11543          parameter is not the same as the mode of the rtl.  */
11544       /* ??? This is the same series of checks that are made in dbxout.c before
11545          we reach the big endian correction code there.  It isn't clear if all
11546          of these checks are necessary here, but keeping them all is the safe
11547          thing to do.  */
11548       else if (MEM_P (rtl)
11549                && XEXP (rtl, 0) != const0_rtx
11550                && ! CONSTANT_P (XEXP (rtl, 0))
11551                /* Not passed in memory.  */
11552                && !MEM_P (DECL_INCOMING_RTL (decl))
11553                /* Not passed by invisible reference.  */
11554                && (!REG_P (XEXP (rtl, 0))
11555                    || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
11556                    || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
11557 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
11558                    || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
11559 #endif
11560                      )
11561                /* Big endian correction check.  */
11562                && BYTES_BIG_ENDIAN
11563                && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
11564                && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
11565                    < UNITS_PER_WORD))
11566         {
11567           int offset = (UNITS_PER_WORD
11568                         - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
11569
11570           rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
11571                              plus_constant (XEXP (rtl, 0), offset));
11572         }
11573     }
11574   else if (TREE_CODE (decl) == VAR_DECL
11575            && rtl
11576            && MEM_P (rtl)
11577            && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
11578            && BYTES_BIG_ENDIAN)
11579     {
11580       int rsize = GET_MODE_SIZE (GET_MODE (rtl));
11581       int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
11582
11583       /* If a variable is declared "register" yet is smaller than
11584          a register, then if we store the variable to memory, it
11585          looks like we're storing a register-sized value, when in
11586          fact we are not.  We need to adjust the offset of the
11587          storage location to reflect the actual value's bytes,
11588          else gdb will not be able to display it.  */
11589       if (rsize > dsize)
11590         rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
11591                            plus_constant (XEXP (rtl, 0), rsize-dsize));
11592     }
11593
11594   /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
11595      and will have been substituted directly into all expressions that use it.
11596      C does not have such a concept, but C++ and other languages do.  */
11597   if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
11598     rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
11599
11600   if (rtl)
11601     rtl = targetm.delegitimize_address (rtl);
11602
11603   /* If we don't look past the constant pool, we risk emitting a
11604      reference to a constant pool entry that isn't referenced from
11605      code, and thus is not emitted.  */
11606   if (rtl)
11607     rtl = avoid_constant_pool_reference (rtl);
11608
11609   return rtl;
11610 }
11611
11612 /* We need to figure out what section we should use as the base for the
11613    address ranges where a given location is valid.
11614    1. If this particular DECL has a section associated with it, use that.
11615    2. If this function has a section associated with it, use that.
11616    3. Otherwise, use the text section.
11617    XXX: If you split a variable across multiple sections, we won't notice.  */
11618
11619 static const char *
11620 secname_for_decl (const_tree decl)
11621 {
11622   const char *secname;
11623
11624   if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
11625     {
11626       tree sectree = DECL_SECTION_NAME (decl);
11627       secname = TREE_STRING_POINTER (sectree);
11628     }
11629   else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
11630     {
11631       tree sectree = DECL_SECTION_NAME (current_function_decl);
11632       secname = TREE_STRING_POINTER (sectree);
11633     }
11634   else if (cfun && in_cold_section_p)
11635     secname = crtl->subsections.cold_section_label;
11636   else
11637     secname = text_section_label;
11638
11639   return secname;
11640 }
11641
11642 /* Check whether decl is a Fortran COMMON symbol.  If not, NULL_TREE is
11643    returned.  If so, the decl for the COMMON block is returned, and the
11644    value is the offset into the common block for the symbol.  */
11645
11646 static tree
11647 fortran_common (tree decl, HOST_WIDE_INT *value)
11648 {
11649   tree val_expr, cvar;
11650   enum machine_mode mode;
11651   HOST_WIDE_INT bitsize, bitpos;
11652   tree offset;
11653   int volatilep = 0, unsignedp = 0;
11654
11655   /* If the decl isn't a VAR_DECL, or if it isn't public or static, or if
11656      it does not have a value (the offset into the common area), or if it
11657      is thread local (as opposed to global) then it isn't common, and shouldn't
11658      be handled as such.  */
11659   if (TREE_CODE (decl) != VAR_DECL
11660       || !TREE_PUBLIC (decl)
11661       || !TREE_STATIC (decl)
11662       || !DECL_HAS_VALUE_EXPR_P (decl)
11663       || !is_fortran ())
11664     return NULL_TREE;
11665
11666   val_expr = DECL_VALUE_EXPR (decl);
11667   if (TREE_CODE (val_expr) != COMPONENT_REF)
11668     return NULL_TREE;
11669
11670   cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
11671                               &mode, &unsignedp, &volatilep, true);
11672
11673   if (cvar == NULL_TREE
11674       || TREE_CODE (cvar) != VAR_DECL
11675       || DECL_ARTIFICIAL (cvar)
11676       || !TREE_PUBLIC (cvar))
11677     return NULL_TREE;
11678
11679   *value = 0;
11680   if (offset != NULL)
11681     {
11682       if (!host_integerp (offset, 0))
11683         return NULL_TREE;
11684       *value = tree_low_cst (offset, 0);
11685     }
11686   if (bitpos != 0)
11687     *value += bitpos / BITS_PER_UNIT;
11688
11689   return cvar;
11690 }
11691
11692 /* Dereference a location expression LOC if DECL is passed by invisible
11693    reference.  */
11694
11695 static dw_loc_descr_ref
11696 loc_by_reference (dw_loc_descr_ref loc, tree decl)
11697 {
11698   HOST_WIDE_INT size;
11699   enum dwarf_location_atom op;
11700
11701   if (loc == NULL)
11702     return NULL;
11703
11704   if ((TREE_CODE (decl) != PARM_DECL && TREE_CODE (decl) != RESULT_DECL)
11705       || !DECL_BY_REFERENCE (decl))
11706     return loc;
11707
11708   size = int_size_in_bytes (TREE_TYPE (decl));
11709   if (size > DWARF2_ADDR_SIZE || size == -1)
11710     return 0;
11711   else if (size == DWARF2_ADDR_SIZE)
11712     op = DW_OP_deref;
11713   else
11714     op = DW_OP_deref_size;
11715   add_loc_descr (&loc, new_loc_descr (op, size, 0));
11716   return loc;
11717 }
11718
11719 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
11720    data attribute for a variable or a parameter.  We generate the
11721    DW_AT_const_value attribute only in those cases where the given variable
11722    or parameter does not have a true "location" either in memory or in a
11723    register.  This can happen (for example) when a constant is passed as an
11724    actual argument in a call to an inline function.  (It's possible that
11725    these things can crop up in other ways also.)  Note that one type of
11726    constant value which can be passed into an inlined function is a constant
11727    pointer.  This can happen for example if an actual argument in an inlined
11728    function call evaluates to a compile-time constant address.  */
11729
11730 static void
11731 add_location_or_const_value_attribute (dw_die_ref die, tree decl,
11732                                        enum dwarf_attribute attr)
11733 {
11734   rtx rtl;
11735   dw_loc_descr_ref descr;
11736   var_loc_list *loc_list;
11737   struct var_loc_node *node;
11738   if (TREE_CODE (decl) == ERROR_MARK)
11739     return;
11740
11741   gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
11742               || TREE_CODE (decl) == RESULT_DECL);
11743
11744   /* See if we possibly have multiple locations for this variable.  */
11745   loc_list = lookup_decl_loc (decl);
11746
11747   /* If it truly has multiple locations, the first and last node will
11748      differ.  */
11749   if (loc_list && loc_list->first != loc_list->last)
11750     {
11751       const char *endname, *secname;
11752       dw_loc_list_ref list;
11753       rtx varloc;
11754       enum var_init_status initialized;
11755
11756       /* Now that we know what section we are using for a base,
11757          actually construct the list of locations.
11758          The first location information is what is passed to the
11759          function that creates the location list, and the remaining
11760          locations just get added on to that list.
11761          Note that we only know the start address for a location
11762          (IE location changes), so to build the range, we use
11763          the range [current location start, next location start].
11764          This means we have to special case the last node, and generate
11765          a range of [last location start, end of function label].  */
11766
11767       node = loc_list->first;
11768       varloc = NOTE_VAR_LOCATION (node->var_loc_note);
11769       secname = secname_for_decl (decl);
11770
11771       if (NOTE_VAR_LOCATION_LOC (node->var_loc_note))
11772         initialized = NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
11773       else
11774         initialized = VAR_INIT_STATUS_INITIALIZED;
11775
11776       descr = loc_by_reference (loc_descriptor (varloc, initialized), decl);
11777       list = new_loc_list (descr, node->label, node->next->label, secname, 1);
11778       node = node->next;
11779
11780       for (; node->next; node = node->next)
11781         if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
11782           {
11783             /* The variable has a location between NODE->LABEL and
11784                NODE->NEXT->LABEL.  */
11785             enum var_init_status initialized =
11786               NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
11787             varloc = NOTE_VAR_LOCATION (node->var_loc_note);
11788             descr = loc_by_reference (loc_descriptor (varloc, initialized),
11789                                       decl);
11790             add_loc_descr_to_loc_list (&list, descr,
11791                                        node->label, node->next->label, secname);
11792           }
11793
11794       /* If the variable has a location at the last label
11795          it keeps its location until the end of function.  */
11796       if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
11797         {
11798           char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
11799           enum var_init_status initialized =
11800             NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
11801
11802           varloc = NOTE_VAR_LOCATION (node->var_loc_note);
11803           if (!current_function_decl)
11804             endname = text_end_label;
11805           else
11806             {
11807               ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
11808                                            current_function_funcdef_no);
11809               endname = ggc_strdup (label_id);
11810             }
11811           descr = loc_by_reference (loc_descriptor (varloc, initialized),
11812                                     decl);
11813           add_loc_descr_to_loc_list (&list, descr,
11814                                      node->label, endname, secname);
11815         }
11816
11817       /* Finally, add the location list to the DIE, and we are done.  */
11818       add_AT_loc_list (die, attr, list);
11819       return;
11820     }
11821
11822   /* Try to get some constant RTL for this decl, and use that as the value of
11823      the location.  */
11824
11825   rtl = rtl_for_decl_location (decl);
11826   if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING))
11827     {
11828       add_const_value_attribute (die, rtl);
11829       return;
11830     }
11831
11832   /* If we have tried to generate the location otherwise, and it
11833      didn't work out (we wouldn't be here if we did), and we have a one entry
11834      location list, try generating a location from that.  */
11835   if (loc_list && loc_list->first)
11836     {
11837       enum var_init_status status;
11838       node = loc_list->first;
11839       status = NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
11840       descr = loc_descriptor (NOTE_VAR_LOCATION (node->var_loc_note), status);
11841       if (descr)
11842         {
11843           descr = loc_by_reference (descr, decl);
11844           add_AT_location_description (die, attr, descr);
11845           return;
11846         }
11847     }
11848
11849   /* We couldn't get any rtl, so try directly generating the location
11850      description from the tree.  */
11851   descr = loc_descriptor_from_tree (decl);
11852   if (descr)
11853     {
11854       descr = loc_by_reference (descr, decl);
11855       add_AT_location_description (die, attr, descr);
11856       return;
11857     }
11858   /* None of that worked, so it must not really have a location;
11859      try adding a constant value attribute from the DECL_INITIAL.  */
11860   tree_add_const_value_attribute (die, decl);
11861 }
11862
11863 /* Helper function for tree_add_const_value_attribute.  Natively encode
11864    initializer INIT into an array.  Return true if successful.  */
11865
11866 static bool
11867 native_encode_initializer (tree init, unsigned char *array, int size)
11868 {
11869   tree type;
11870
11871   if (init == NULL_TREE)
11872     return false;
11873
11874   STRIP_NOPS (init);
11875   switch (TREE_CODE (init))
11876     {
11877     case STRING_CST:
11878       type = TREE_TYPE (init);
11879       if (TREE_CODE (type) == ARRAY_TYPE)
11880         {
11881           tree enttype = TREE_TYPE (type);
11882           enum machine_mode mode = TYPE_MODE (enttype);
11883
11884           if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
11885             return false;
11886           if (int_size_in_bytes (type) != size)
11887             return false;
11888           if (size > TREE_STRING_LENGTH (init))
11889             {
11890               memcpy (array, TREE_STRING_POINTER (init),
11891                       TREE_STRING_LENGTH (init));
11892               memset (array + TREE_STRING_LENGTH (init),
11893                       '\0', size - TREE_STRING_LENGTH (init));
11894             }
11895           else
11896             memcpy (array, TREE_STRING_POINTER (init), size);
11897           return true;
11898         }
11899       return false;
11900     case CONSTRUCTOR:
11901       type = TREE_TYPE (init);
11902       if (int_size_in_bytes (type) != size)
11903         return false;
11904       if (TREE_CODE (type) == ARRAY_TYPE)
11905         {
11906           HOST_WIDE_INT min_index;
11907           unsigned HOST_WIDE_INT cnt;
11908           int curpos = 0, fieldsize;
11909           constructor_elt *ce;
11910
11911           if (TYPE_DOMAIN (type) == NULL_TREE
11912               || !host_integerp (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0))
11913             return false;
11914
11915           fieldsize = int_size_in_bytes (TREE_TYPE (type));
11916           if (fieldsize <= 0)
11917             return false;
11918
11919           min_index = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0);
11920           memset (array, '\0', size);
11921           for (cnt = 0;
11922                VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce);
11923                cnt++)
11924             {
11925               tree val = ce->value;
11926               tree index = ce->index;
11927               int pos = curpos;
11928               if (index && TREE_CODE (index) == RANGE_EXPR)
11929                 pos = (tree_low_cst (TREE_OPERAND (index, 0), 0) - min_index)
11930                       * fieldsize;
11931               else if (index)
11932                 pos = (tree_low_cst (index, 0) - min_index) * fieldsize;
11933
11934               if (val)
11935                 {
11936                   STRIP_NOPS (val);
11937                   if (!native_encode_initializer (val, array + pos, fieldsize))
11938                     return false;
11939                 }
11940               curpos = pos + fieldsize;
11941               if (index && TREE_CODE (index) == RANGE_EXPR)
11942                 {
11943                   int count = tree_low_cst (TREE_OPERAND (index, 1), 0)
11944                               - tree_low_cst (TREE_OPERAND (index, 0), 0);
11945                   while (count > 0)
11946                     {
11947                       if (val)
11948                         memcpy (array + curpos, array + pos, fieldsize);
11949                       curpos += fieldsize;
11950                     }
11951                 }
11952               gcc_assert (curpos <= size);
11953             }
11954           return true;
11955         }
11956       else if (TREE_CODE (type) == RECORD_TYPE
11957                || TREE_CODE (type) == UNION_TYPE)
11958         {
11959           tree field = NULL_TREE;
11960           unsigned HOST_WIDE_INT cnt;
11961           constructor_elt *ce;
11962
11963           if (int_size_in_bytes (type) != size)
11964             return false;
11965
11966           if (TREE_CODE (type) == RECORD_TYPE)
11967             field = TYPE_FIELDS (type);
11968
11969           for (cnt = 0;
11970                VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce);
11971                cnt++, field = field ? TREE_CHAIN (field) : 0)
11972             {
11973               tree val = ce->value;
11974               int pos, fieldsize;
11975
11976               if (ce->index != 0)
11977                 field = ce->index;
11978
11979               if (val)
11980                 STRIP_NOPS (val);
11981
11982               if (field == NULL_TREE || DECL_BIT_FIELD (field))
11983                 return false;
11984
11985               if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
11986                   && TYPE_DOMAIN (TREE_TYPE (field))
11987                   && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
11988                 return false;
11989               else if (DECL_SIZE_UNIT (field) == NULL_TREE
11990                        || !host_integerp (DECL_SIZE_UNIT (field), 0))
11991                 return false;
11992               fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 0);
11993               pos = int_byte_position (field);
11994               gcc_assert (pos + fieldsize <= size);
11995               if (val
11996                   && !native_encode_initializer (val, array + pos, fieldsize))
11997                 return false;
11998             }
11999           return true;
12000         }
12001       return false;
12002     case VIEW_CONVERT_EXPR:
12003     case NON_LVALUE_EXPR:
12004       return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
12005     default:
12006       return native_encode_expr (init, array, size) == size;
12007     }
12008 }
12009
12010 /* If we don't have a copy of this variable in memory for some reason (such
12011    as a C++ member constant that doesn't have an out-of-line definition),
12012    we should tell the debugger about the constant value.  */
12013
12014 static void
12015 tree_add_const_value_attribute (dw_die_ref var_die, tree decl)
12016 {
12017   tree init;
12018   tree type = TREE_TYPE (decl);
12019   rtx rtl;
12020
12021   if (TREE_CODE (decl) != VAR_DECL && TREE_CODE (decl) != CONST_DECL)
12022     return;
12023
12024   init = DECL_INITIAL (decl);
12025   if (TREE_READONLY (decl) && ! TREE_THIS_VOLATILE (decl) && init)
12026     /* OK */;
12027   else
12028     return;
12029
12030   rtl = rtl_for_decl_init (init, type);
12031   if (rtl)
12032     add_const_value_attribute (var_die, rtl);
12033   /* If the host and target are sane, try harder.  */
12034   else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
12035            && initializer_constant_valid_p (init, type))
12036     {
12037       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
12038       if (size > 0 && (int) size == size)
12039         {
12040           unsigned char *array = GGC_CNEWVEC (unsigned char, size);
12041
12042           if (native_encode_initializer (init, array, size))
12043             add_AT_vec (var_die, DW_AT_const_value, size, 1, array);
12044         }
12045     }
12046 }
12047
12048 /* Convert the CFI instructions for the current function into a
12049    location list.  This is used for DW_AT_frame_base when we targeting
12050    a dwarf2 consumer that does not support the dwarf3
12051    DW_OP_call_frame_cfa.  OFFSET is a constant to be added to all CFA
12052    expressions.  */
12053
12054 static dw_loc_list_ref
12055 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
12056 {
12057   dw_fde_ref fde;
12058   dw_loc_list_ref list, *list_tail;
12059   dw_cfi_ref cfi;
12060   dw_cfa_location last_cfa, next_cfa;
12061   const char *start_label, *last_label, *section;
12062
12063   fde = current_fde ();
12064   gcc_assert (fde != NULL);
12065
12066   section = secname_for_decl (current_function_decl);
12067   list_tail = &list;
12068   list = NULL;
12069
12070   next_cfa.reg = INVALID_REGNUM;
12071   next_cfa.offset = 0;
12072   next_cfa.indirect = 0;
12073   next_cfa.base_offset = 0;
12074
12075   start_label = fde->dw_fde_begin;
12076
12077   /* ??? Bald assumption that the CIE opcode list does not contain
12078      advance opcodes.  */
12079   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
12080     lookup_cfa_1 (cfi, &next_cfa);
12081
12082   last_cfa = next_cfa;
12083   last_label = start_label;
12084
12085   for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
12086     switch (cfi->dw_cfi_opc)
12087       {
12088       case DW_CFA_set_loc:
12089       case DW_CFA_advance_loc1:
12090       case DW_CFA_advance_loc2:
12091       case DW_CFA_advance_loc4:
12092         if (!cfa_equal_p (&last_cfa, &next_cfa))
12093           {
12094             *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
12095                                        start_label, last_label, section,
12096                                        list == NULL);
12097
12098             list_tail = &(*list_tail)->dw_loc_next;
12099             last_cfa = next_cfa;
12100             start_label = last_label;
12101           }
12102         last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
12103         break;
12104
12105       case DW_CFA_advance_loc:
12106         /* The encoding is complex enough that we should never emit this.  */
12107       case DW_CFA_remember_state:
12108       case DW_CFA_restore_state:
12109         /* We don't handle these two in this function.  It would be possible
12110            if it were to be required.  */
12111         gcc_unreachable ();
12112
12113       default:
12114         lookup_cfa_1 (cfi, &next_cfa);
12115         break;
12116       }
12117
12118   if (!cfa_equal_p (&last_cfa, &next_cfa))
12119     {
12120       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
12121                                  start_label, last_label, section,
12122                                  list == NULL);
12123       list_tail = &(*list_tail)->dw_loc_next;
12124       start_label = last_label;
12125     }
12126   *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
12127                              start_label, fde->dw_fde_end, section,
12128                              list == NULL);
12129
12130   return list;
12131 }
12132
12133 /* Compute a displacement from the "steady-state frame pointer" to the
12134    frame base (often the same as the CFA), and store it in
12135    frame_pointer_fb_offset.  OFFSET is added to the displacement
12136    before the latter is negated.  */
12137
12138 static void
12139 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
12140 {
12141   rtx reg, elim;
12142
12143 #ifdef FRAME_POINTER_CFA_OFFSET
12144   reg = frame_pointer_rtx;
12145   offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
12146 #else
12147   reg = arg_pointer_rtx;
12148   offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
12149 #endif
12150
12151   elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
12152   if (GET_CODE (elim) == PLUS)
12153     {
12154       offset += INTVAL (XEXP (elim, 1));
12155       elim = XEXP (elim, 0);
12156     }
12157
12158   gcc_assert ((SUPPORTS_STACK_ALIGNMENT
12159                && (elim == hard_frame_pointer_rtx
12160                    || elim == stack_pointer_rtx))
12161               || elim == (frame_pointer_needed
12162                           ? hard_frame_pointer_rtx
12163                           : stack_pointer_rtx));
12164
12165   frame_pointer_fb_offset = -offset;
12166 }
12167
12168 /* Generate a DW_AT_name attribute given some string value to be included as
12169    the value of the attribute.  */
12170
12171 static void
12172 add_name_attribute (dw_die_ref die, const char *name_string)
12173 {
12174   if (name_string != NULL && *name_string != 0)
12175     {
12176       if (demangle_name_func)
12177         name_string = (*demangle_name_func) (name_string);
12178
12179       add_AT_string (die, DW_AT_name, name_string);
12180     }
12181 }
12182
12183 /* Generate a DW_AT_comp_dir attribute for DIE.  */
12184
12185 static void
12186 add_comp_dir_attribute (dw_die_ref die)
12187 {
12188   const char *wd = get_src_pwd ();
12189   if (wd != NULL)
12190     add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
12191 }
12192
12193 /* Given a tree node describing an array bound (either lower or upper) output
12194    a representation for that bound.  */
12195
12196 static void
12197 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
12198 {
12199   switch (TREE_CODE (bound))
12200     {
12201     case ERROR_MARK:
12202       return;
12203
12204     /* All fixed-bounds are represented by INTEGER_CST nodes.  */
12205     case INTEGER_CST:
12206       if (! host_integerp (bound, 0)
12207           || (bound_attr == DW_AT_lower_bound
12208               && (((is_c_family () || is_java ()) &&  integer_zerop (bound))
12209                   || (is_fortran () && integer_onep (bound)))))
12210         /* Use the default.  */
12211         ;
12212       else
12213         add_AT_unsigned (subrange_die, bound_attr, tree_low_cst (bound, 0));
12214       break;
12215
12216     CASE_CONVERT:
12217     case VIEW_CONVERT_EXPR:
12218       add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
12219       break;
12220
12221     case SAVE_EXPR:
12222       break;
12223
12224     case VAR_DECL:
12225     case PARM_DECL:
12226     case RESULT_DECL:
12227       {
12228         dw_die_ref decl_die = lookup_decl_die (bound);
12229         dw_loc_descr_ref loc;
12230
12231         /* ??? Can this happen, or should the variable have been bound
12232            first?  Probably it can, since I imagine that we try to create
12233            the types of parameters in the order in which they exist in
12234            the list, and won't have created a forward reference to a
12235            later parameter.  */
12236         if (decl_die != NULL)
12237           add_AT_die_ref (subrange_die, bound_attr, decl_die);
12238         else
12239           {
12240             loc = loc_descriptor_from_tree_1 (bound, 0);
12241             add_AT_location_description (subrange_die, bound_attr, loc);
12242           }
12243         break;
12244       }
12245
12246     default:
12247       {
12248         /* Otherwise try to create a stack operation procedure to
12249            evaluate the value of the array bound.  */
12250
12251         dw_die_ref ctx, decl_die;
12252         dw_loc_descr_ref loc;
12253
12254         loc = loc_descriptor_from_tree (bound);
12255         if (loc == NULL)
12256           break;
12257
12258         if (current_function_decl == 0)
12259           ctx = comp_unit_die;
12260         else
12261           ctx = lookup_decl_die (current_function_decl);
12262
12263         decl_die = new_die (DW_TAG_variable, ctx, bound);
12264         add_AT_flag (decl_die, DW_AT_artificial, 1);
12265         add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
12266         add_AT_loc (decl_die, DW_AT_location, loc);
12267
12268         add_AT_die_ref (subrange_die, bound_attr, decl_die);
12269         break;
12270       }
12271     }
12272 }
12273
12274 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
12275    possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
12276    Note that the block of subscript information for an array type also
12277    includes information about the element type of the given array type.  */
12278
12279 static void
12280 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
12281 {
12282   unsigned dimension_number;
12283   tree lower, upper;
12284   dw_die_ref subrange_die;
12285
12286   for (dimension_number = 0;
12287        TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
12288        type = TREE_TYPE (type), dimension_number++)
12289     {
12290       tree domain = TYPE_DOMAIN (type);
12291
12292       if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
12293         break;
12294
12295       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
12296          and (in GNU C only) variable bounds.  Handle all three forms
12297          here.  */
12298       subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
12299       if (domain)
12300         {
12301           /* We have an array type with specified bounds.  */
12302           lower = TYPE_MIN_VALUE (domain);
12303           upper = TYPE_MAX_VALUE (domain);
12304
12305           /* Define the index type.  */
12306           if (TREE_TYPE (domain))
12307             {
12308               /* ??? This is probably an Ada unnamed subrange type.  Ignore the
12309                  TREE_TYPE field.  We can't emit debug info for this
12310                  because it is an unnamed integral type.  */
12311               if (TREE_CODE (domain) == INTEGER_TYPE
12312                   && TYPE_NAME (domain) == NULL_TREE
12313                   && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
12314                   && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
12315                 ;
12316               else
12317                 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
12318                                     type_die);
12319             }
12320
12321           /* ??? If upper is NULL, the array has unspecified length,
12322              but it does have a lower bound.  This happens with Fortran
12323                dimension arr(N:*)
12324              Since the debugger is definitely going to need to know N
12325              to produce useful results, go ahead and output the lower
12326              bound solo, and hope the debugger can cope.  */
12327
12328           add_bound_info (subrange_die, DW_AT_lower_bound, lower);
12329           if (upper)
12330             add_bound_info (subrange_die, DW_AT_upper_bound, upper);
12331         }
12332
12333       /* Otherwise we have an array type with an unspecified length.  The
12334          DWARF-2 spec does not say how to handle this; let's just leave out the
12335          bounds.  */
12336     }
12337 }
12338
12339 static void
12340 add_byte_size_attribute (dw_die_ref die, tree tree_node)
12341 {
12342   unsigned size;
12343
12344   switch (TREE_CODE (tree_node))
12345     {
12346     case ERROR_MARK:
12347       size = 0;
12348       break;
12349     case ENUMERAL_TYPE:
12350     case RECORD_TYPE:
12351     case UNION_TYPE:
12352     case QUAL_UNION_TYPE:
12353       size = int_size_in_bytes (tree_node);
12354       break;
12355     case FIELD_DECL:
12356       /* For a data member of a struct or union, the DW_AT_byte_size is
12357          generally given as the number of bytes normally allocated for an
12358          object of the *declared* type of the member itself.  This is true
12359          even for bit-fields.  */
12360       size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
12361       break;
12362     default:
12363       gcc_unreachable ();
12364     }
12365
12366   /* Note that `size' might be -1 when we get to this point.  If it is, that
12367      indicates that the byte size of the entity in question is variable.  We
12368      have no good way of expressing this fact in Dwarf at the present time,
12369      so just let the -1 pass on through.  */
12370   add_AT_unsigned (die, DW_AT_byte_size, size);
12371 }
12372
12373 /* For a FIELD_DECL node which represents a bit-field, output an attribute
12374    which specifies the distance in bits from the highest order bit of the
12375    "containing object" for the bit-field to the highest order bit of the
12376    bit-field itself.
12377
12378    For any given bit-field, the "containing object" is a hypothetical object
12379    (of some integral or enum type) within which the given bit-field lives.  The
12380    type of this hypothetical "containing object" is always the same as the
12381    declared type of the individual bit-field itself.  The determination of the
12382    exact location of the "containing object" for a bit-field is rather
12383    complicated.  It's handled by the `field_byte_offset' function (above).
12384
12385    Note that it is the size (in bytes) of the hypothetical "containing object"
12386    which will be given in the DW_AT_byte_size attribute for this bit-field.
12387    (See `byte_size_attribute' above).  */
12388
12389 static inline void
12390 add_bit_offset_attribute (dw_die_ref die, tree decl)
12391 {
12392   HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
12393   tree type = DECL_BIT_FIELD_TYPE (decl);
12394   HOST_WIDE_INT bitpos_int;
12395   HOST_WIDE_INT highest_order_object_bit_offset;
12396   HOST_WIDE_INT highest_order_field_bit_offset;
12397   HOST_WIDE_INT unsigned bit_offset;
12398
12399   /* Must be a field and a bit field.  */
12400   gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
12401
12402   /* We can't yet handle bit-fields whose offsets are variable, so if we
12403      encounter such things, just return without generating any attribute
12404      whatsoever.  Likewise for variable or too large size.  */
12405   if (! host_integerp (bit_position (decl), 0)
12406       || ! host_integerp (DECL_SIZE (decl), 1))
12407     return;
12408
12409   bitpos_int = int_bit_position (decl);
12410
12411   /* Note that the bit offset is always the distance (in bits) from the
12412      highest-order bit of the "containing object" to the highest-order bit of
12413      the bit-field itself.  Since the "high-order end" of any object or field
12414      is different on big-endian and little-endian machines, the computation
12415      below must take account of these differences.  */
12416   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
12417   highest_order_field_bit_offset = bitpos_int;
12418
12419   if (! BYTES_BIG_ENDIAN)
12420     {
12421       highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
12422       highest_order_object_bit_offset += simple_type_size_in_bits (type);
12423     }
12424
12425   bit_offset
12426     = (! BYTES_BIG_ENDIAN
12427        ? highest_order_object_bit_offset - highest_order_field_bit_offset
12428        : highest_order_field_bit_offset - highest_order_object_bit_offset);
12429
12430   add_AT_unsigned (die, DW_AT_bit_offset, bit_offset);
12431 }
12432
12433 /* For a FIELD_DECL node which represents a bit field, output an attribute
12434    which specifies the length in bits of the given field.  */
12435
12436 static inline void
12437 add_bit_size_attribute (dw_die_ref die, tree decl)
12438 {
12439   /* Must be a field and a bit field.  */
12440   gcc_assert (TREE_CODE (decl) == FIELD_DECL
12441               && DECL_BIT_FIELD_TYPE (decl));
12442
12443   if (host_integerp (DECL_SIZE (decl), 1))
12444     add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
12445 }
12446
12447 /* If the compiled language is ANSI C, then add a 'prototyped'
12448    attribute, if arg types are given for the parameters of a function.  */
12449
12450 static inline void
12451 add_prototyped_attribute (dw_die_ref die, tree func_type)
12452 {
12453   if (get_AT_unsigned (comp_unit_die, DW_AT_language) == DW_LANG_C89
12454       && TYPE_ARG_TYPES (func_type) != NULL)
12455     add_AT_flag (die, DW_AT_prototyped, 1);
12456 }
12457
12458 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
12459    by looking in either the type declaration or object declaration
12460    equate table.  */
12461
12462 static inline void
12463 add_abstract_origin_attribute (dw_die_ref die, tree origin)
12464 {
12465   dw_die_ref origin_die = NULL;
12466
12467   if (TREE_CODE (origin) != FUNCTION_DECL)
12468     {
12469       /* We may have gotten separated from the block for the inlined
12470          function, if we're in an exception handler or some such; make
12471          sure that the abstract function has been written out.
12472
12473          Doing this for nested functions is wrong, however; functions are
12474          distinct units, and our context might not even be inline.  */
12475       tree fn = origin;
12476
12477       if (TYPE_P (fn))
12478         fn = TYPE_STUB_DECL (fn);
12479
12480       fn = decl_function_context (fn);
12481       if (fn)
12482         dwarf2out_abstract_function (fn);
12483     }
12484
12485   if (DECL_P (origin))
12486     origin_die = lookup_decl_die (origin);
12487   else if (TYPE_P (origin))
12488     origin_die = lookup_type_die (origin);
12489
12490   /* XXX: Functions that are never lowered don't always have correct block
12491      trees (in the case of java, they simply have no block tree, in some other
12492      languages).  For these functions, there is nothing we can really do to
12493      output correct debug info for inlined functions in all cases.  Rather
12494      than die, we'll just produce deficient debug info now, in that we will
12495      have variables without a proper abstract origin.  In the future, when all
12496      functions are lowered, we should re-add a gcc_assert (origin_die)
12497      here.  */
12498
12499   if (origin_die)
12500       add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
12501 }
12502
12503 /* We do not currently support the pure_virtual attribute.  */
12504
12505 static inline void
12506 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
12507 {
12508   if (DECL_VINDEX (func_decl))
12509     {
12510       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
12511
12512       if (host_integerp (DECL_VINDEX (func_decl), 0))
12513         add_AT_loc (die, DW_AT_vtable_elem_location,
12514                     new_loc_descr (DW_OP_constu,
12515                                    tree_low_cst (DECL_VINDEX (func_decl), 0),
12516                                    0));
12517
12518       /* GNU extension: Record what type this method came from originally.  */
12519       if (debug_info_level > DINFO_LEVEL_TERSE)
12520         add_AT_die_ref (die, DW_AT_containing_type,
12521                         lookup_type_die (DECL_CONTEXT (func_decl)));
12522     }
12523 }
12524 \f
12525 /* Add source coordinate attributes for the given decl.  */
12526
12527 static void
12528 add_src_coords_attributes (dw_die_ref die, tree decl)
12529 {
12530   expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
12531
12532   add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
12533   add_AT_unsigned (die, DW_AT_decl_line, s.line);
12534 }
12535
12536 /* Add a DW_AT_name attribute and source coordinate attribute for the
12537    given decl, but only if it actually has a name.  */
12538
12539 static void
12540 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
12541 {
12542   tree decl_name;
12543
12544   decl_name = DECL_NAME (decl);
12545   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
12546     {
12547       add_name_attribute (die, dwarf2_name (decl, 0));
12548       if (! DECL_ARTIFICIAL (decl))
12549         add_src_coords_attributes (die, decl);
12550
12551       if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
12552           && TREE_PUBLIC (decl)
12553           && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
12554           && !DECL_ABSTRACT (decl)
12555           && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
12556           && !is_fortran ())
12557         add_AT_string (die, DW_AT_MIPS_linkage_name,
12558                        IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
12559     }
12560
12561 #ifdef VMS_DEBUGGING_INFO
12562   /* Get the function's name, as described by its RTL.  This may be different
12563      from the DECL_NAME name used in the source file.  */
12564   if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
12565     {
12566       add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
12567                    XEXP (DECL_RTL (decl), 0));
12568       VEC_safe_push (tree, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
12569     }
12570 #endif
12571 }
12572
12573 /* Push a new declaration scope.  */
12574
12575 static void
12576 push_decl_scope (tree scope)
12577 {
12578   VEC_safe_push (tree, gc, decl_scope_table, scope);
12579 }
12580
12581 /* Pop a declaration scope.  */
12582
12583 static inline void
12584 pop_decl_scope (void)
12585 {
12586   VEC_pop (tree, decl_scope_table);
12587 }
12588
12589 /* Return the DIE for the scope that immediately contains this type.
12590    Non-named types get global scope.  Named types nested in other
12591    types get their containing scope if it's open, or global scope
12592    otherwise.  All other types (i.e. function-local named types) get
12593    the current active scope.  */
12594
12595 static dw_die_ref
12596 scope_die_for (tree t, dw_die_ref context_die)
12597 {
12598   dw_die_ref scope_die = NULL;
12599   tree containing_scope;
12600   int i;
12601
12602   /* Non-types always go in the current scope.  */
12603   gcc_assert (TYPE_P (t));
12604
12605   containing_scope = TYPE_CONTEXT (t);
12606
12607   /* Use the containing namespace if it was passed in (for a declaration).  */
12608   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
12609     {
12610       if (context_die == lookup_decl_die (containing_scope))
12611         /* OK */;
12612       else
12613         containing_scope = NULL_TREE;
12614     }
12615
12616   /* Ignore function type "scopes" from the C frontend.  They mean that
12617      a tagged type is local to a parmlist of a function declarator, but
12618      that isn't useful to DWARF.  */
12619   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
12620     containing_scope = NULL_TREE;
12621
12622   if (containing_scope == NULL_TREE)
12623     scope_die = comp_unit_die;
12624   else if (TYPE_P (containing_scope))
12625     {
12626       /* For types, we can just look up the appropriate DIE.  But
12627          first we check to see if we're in the middle of emitting it
12628          so we know where the new DIE should go.  */
12629       for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
12630         if (VEC_index (tree, decl_scope_table, i) == containing_scope)
12631           break;
12632
12633       if (i < 0)
12634         {
12635           gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
12636                       || TREE_ASM_WRITTEN (containing_scope));
12637
12638           /* If none of the current dies are suitable, we get file scope.  */
12639           scope_die = comp_unit_die;
12640         }
12641       else
12642         scope_die = lookup_type_die (containing_scope);
12643     }
12644   else
12645     scope_die = context_die;
12646
12647   return scope_die;
12648 }
12649
12650 /* Returns nonzero if CONTEXT_DIE is internal to a function.  */
12651
12652 static inline int
12653 local_scope_p (dw_die_ref context_die)
12654 {
12655   for (; context_die; context_die = context_die->die_parent)
12656     if (context_die->die_tag == DW_TAG_inlined_subroutine
12657         || context_die->die_tag == DW_TAG_subprogram)
12658       return 1;
12659
12660   return 0;
12661 }
12662
12663 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
12664    whether or not to treat a DIE in this context as a declaration.  */
12665
12666 static inline int
12667 class_or_namespace_scope_p (dw_die_ref context_die)
12668 {
12669   return (context_die
12670           && (context_die->die_tag == DW_TAG_structure_type
12671               || context_die->die_tag == DW_TAG_class_type
12672               || context_die->die_tag == DW_TAG_interface_type
12673               || context_die->die_tag == DW_TAG_union_type
12674               || context_die->die_tag == DW_TAG_namespace));
12675 }
12676
12677 /* Many forms of DIEs require a "type description" attribute.  This
12678    routine locates the proper "type descriptor" die for the type given
12679    by 'type', and adds a DW_AT_type attribute below the given die.  */
12680
12681 static void
12682 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
12683                     int decl_volatile, dw_die_ref context_die)
12684 {
12685   enum tree_code code  = TREE_CODE (type);
12686   dw_die_ref type_die  = NULL;
12687
12688   /* ??? If this type is an unnamed subrange type of an integral, floating-point
12689      or fixed-point type, use the inner type.  This is because we have no
12690      support for unnamed types in base_type_die.  This can happen if this is
12691      an Ada subrange type.  Correct solution is emit a subrange type die.  */
12692   if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
12693       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
12694     type = TREE_TYPE (type), code = TREE_CODE (type);
12695
12696   if (code == ERROR_MARK
12697       /* Handle a special case.  For functions whose return type is void, we
12698          generate *no* type attribute.  (Note that no object may have type
12699          `void', so this only applies to function return types).  */
12700       || code == VOID_TYPE)
12701     return;
12702
12703   type_die = modified_type_die (type,
12704                                 decl_const || TYPE_READONLY (type),
12705                                 decl_volatile || TYPE_VOLATILE (type),
12706                                 context_die);
12707
12708   if (type_die != NULL)
12709     add_AT_die_ref (object_die, DW_AT_type, type_die);
12710 }
12711
12712 /* Given an object die, add the calling convention attribute for the
12713    function call type.  */
12714 static void
12715 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
12716 {
12717   enum dwarf_calling_convention value = DW_CC_normal;
12718
12719   value = targetm.dwarf_calling_convention (TREE_TYPE (decl));
12720
12721   /* DWARF doesn't provide a way to identify a program's source-level
12722      entry point.  DW_AT_calling_convention attributes are only meant
12723      to describe functions' calling conventions.  However, lacking a
12724      better way to signal the Fortran main program, we use this for the
12725      time being, following existing custom.  */
12726   if (is_fortran ()
12727       && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
12728     value = DW_CC_program;
12729
12730   /* Only add the attribute if the backend requests it, and
12731      is not DW_CC_normal.  */
12732   if (value && (value != DW_CC_normal))
12733     add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
12734 }
12735
12736 /* Given a tree pointer to a struct, class, union, or enum type node, return
12737    a pointer to the (string) tag name for the given type, or zero if the type
12738    was declared without a tag.  */
12739
12740 static const char *
12741 type_tag (const_tree type)
12742 {
12743   const char *name = 0;
12744
12745   if (TYPE_NAME (type) != 0)
12746     {
12747       tree t = 0;
12748
12749       /* Find the IDENTIFIER_NODE for the type name.  */
12750       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
12751         t = TYPE_NAME (type);
12752
12753       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
12754          a TYPE_DECL node, regardless of whether or not a `typedef' was
12755          involved.  */
12756       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12757                && ! DECL_IGNORED_P (TYPE_NAME (type)))
12758         {
12759           /* We want to be extra verbose.  Don't call dwarf_name if
12760              DECL_NAME isn't set.  The default hook for decl_printable_name
12761              doesn't like that, and in this context it's correct to return
12762              0, instead of "<anonymous>" or the like.  */
12763           if (DECL_NAME (TYPE_NAME (type)))
12764             name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
12765         }
12766
12767       /* Now get the name as a string, or invent one.  */
12768       if (!name && t != 0)
12769         name = IDENTIFIER_POINTER (t);
12770     }
12771
12772   return (name == 0 || *name == '\0') ? 0 : name;
12773 }
12774
12775 /* Return the type associated with a data member, make a special check
12776    for bit field types.  */
12777
12778 static inline tree
12779 member_declared_type (const_tree member)
12780 {
12781   return (DECL_BIT_FIELD_TYPE (member)
12782           ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
12783 }
12784
12785 /* Get the decl's label, as described by its RTL. This may be different
12786    from the DECL_NAME name used in the source file.  */
12787
12788 #if 0
12789 static const char *
12790 decl_start_label (tree decl)
12791 {
12792   rtx x;
12793   const char *fnname;
12794
12795   x = DECL_RTL (decl);
12796   gcc_assert (MEM_P (x));
12797
12798   x = XEXP (x, 0);
12799   gcc_assert (GET_CODE (x) == SYMBOL_REF);
12800
12801   fnname = XSTR (x, 0);
12802   return fnname;
12803 }
12804 #endif
12805 \f
12806 /* These routines generate the internal representation of the DIE's for
12807    the compilation unit.  Debugging information is collected by walking
12808    the declaration trees passed in from dwarf2out_decl().  */
12809
12810 static void
12811 gen_array_type_die (tree type, dw_die_ref context_die)
12812 {
12813   dw_die_ref scope_die = scope_die_for (type, context_die);
12814   dw_die_ref array_die;
12815
12816   /* GNU compilers represent multidimensional array types as sequences of one
12817      dimensional array types whose element types are themselves array types.
12818      We sometimes squish that down to a single array_type DIE with multiple
12819      subscripts in the Dwarf debugging info.  The draft Dwarf specification
12820      say that we are allowed to do this kind of compression in C, because
12821      there is no difference between an array of arrays and a multidimensional
12822      array.  We don't do this for Ada to remain as close as possible to the
12823      actual representation, which is especially important against the language
12824      flexibilty wrt arrays of variable size.  */
12825
12826   bool collapse_nested_arrays = !is_ada ();
12827   tree element_type;
12828
12829   /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
12830      DW_TAG_string_type doesn't have DW_AT_type attribute).  */
12831   if (TYPE_STRING_FLAG (type)
12832       && TREE_CODE (type) == ARRAY_TYPE
12833       && is_fortran ()
12834       && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
12835     {
12836       HOST_WIDE_INT size;
12837
12838       array_die = new_die (DW_TAG_string_type, scope_die, type);
12839       add_name_attribute (array_die, type_tag (type));
12840       equate_type_number_to_die (type, array_die);
12841       size = int_size_in_bytes (type);
12842       if (size >= 0)
12843         add_AT_unsigned (array_die, DW_AT_byte_size, size);
12844       else if (TYPE_DOMAIN (type) != NULL_TREE
12845                && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
12846                && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
12847         {
12848           tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
12849           dw_loc_descr_ref loc = loc_descriptor_from_tree (szdecl);
12850
12851           size = int_size_in_bytes (TREE_TYPE (szdecl));
12852           if (loc && size > 0)
12853             {
12854               add_AT_loc (array_die, DW_AT_string_length, loc);
12855               if (size != DWARF2_ADDR_SIZE)
12856                 add_AT_unsigned (array_die, DW_AT_byte_size, size);
12857             }
12858         }
12859       return;
12860     }
12861
12862   /* ??? The SGI dwarf reader fails for array of array of enum types
12863      (e.g. const enum machine_mode insn_operand_mode[2][10]) unless the inner
12864      array type comes before the outer array type.  We thus call gen_type_die
12865      before we new_die and must prevent nested array types collapsing for this
12866      target.  */
12867
12868 #ifdef MIPS_DEBUGGING_INFO
12869   gen_type_die (TREE_TYPE (type), context_die);
12870   collapse_nested_arrays = false;
12871 #endif
12872
12873   array_die = new_die (DW_TAG_array_type, scope_die, type);
12874   add_name_attribute (array_die, type_tag (type));
12875   equate_type_number_to_die (type, array_die);
12876
12877   if (TREE_CODE (type) == VECTOR_TYPE)
12878     {
12879       /* The frontend feeds us a representation for the vector as a struct
12880          containing an array.  Pull out the array type.  */
12881       type = TREE_TYPE (TYPE_FIELDS (TYPE_DEBUG_REPRESENTATION_TYPE (type)));
12882       add_AT_flag (array_die, DW_AT_GNU_vector, 1);
12883     }
12884
12885   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
12886   if (is_fortran ()
12887       && TREE_CODE (type) == ARRAY_TYPE
12888       && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
12889       && !TYPE_STRING_FLAG (TREE_TYPE (type)))
12890     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
12891
12892 #if 0
12893   /* We default the array ordering.  SDB will probably do
12894      the right things even if DW_AT_ordering is not present.  It's not even
12895      an issue until we start to get into multidimensional arrays anyway.  If
12896      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
12897      then we'll have to put the DW_AT_ordering attribute back in.  (But if
12898      and when we find out that we need to put these in, we will only do so
12899      for multidimensional arrays.  */
12900   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
12901 #endif
12902
12903 #ifdef MIPS_DEBUGGING_INFO
12904   /* The SGI compilers handle arrays of unknown bound by setting
12905      AT_declaration and not emitting any subrange DIEs.  */
12906   if (! TYPE_DOMAIN (type))
12907     add_AT_flag (array_die, DW_AT_declaration, 1);
12908   else
12909 #endif
12910     add_subscript_info (array_die, type, collapse_nested_arrays);
12911
12912   /* Add representation of the type of the elements of this array type and
12913      emit the corresponding DIE if we haven't done it already.  */  
12914   element_type = TREE_TYPE (type);
12915   if (collapse_nested_arrays)
12916     while (TREE_CODE (element_type) == ARRAY_TYPE)
12917       {
12918         if (TYPE_STRING_FLAG (element_type) && is_fortran ())
12919           break;
12920         element_type = TREE_TYPE (element_type);
12921       }
12922
12923 #ifndef MIPS_DEBUGGING_INFO
12924   gen_type_die (element_type, context_die);
12925 #endif
12926
12927   add_type_attribute (array_die, element_type, 0, 0, context_die);
12928
12929   if (get_AT (array_die, DW_AT_name))
12930     add_pubtype (type, array_die);
12931 }
12932
12933 static dw_loc_descr_ref
12934 descr_info_loc (tree val, tree base_decl)
12935 {
12936   HOST_WIDE_INT size;
12937   dw_loc_descr_ref loc, loc2;
12938   enum dwarf_location_atom op;
12939
12940   if (val == base_decl)
12941     return new_loc_descr (DW_OP_push_object_address, 0, 0);
12942
12943   switch (TREE_CODE (val))
12944     {
12945     CASE_CONVERT:
12946       return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
12947     case VAR_DECL:
12948       return loc_descriptor_from_tree_1 (val, 0);
12949     case INTEGER_CST:
12950       if (host_integerp (val, 0))
12951         return int_loc_descriptor (tree_low_cst (val, 0));
12952       break;
12953     case INDIRECT_REF:
12954       size = int_size_in_bytes (TREE_TYPE (val));
12955       if (size < 0)
12956         break;
12957       loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
12958       if (!loc)
12959         break;
12960       if (size == DWARF2_ADDR_SIZE)
12961         add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
12962       else
12963         add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
12964       return loc;
12965     case POINTER_PLUS_EXPR:
12966     case PLUS_EXPR:
12967       if (host_integerp (TREE_OPERAND (val, 1), 1)
12968           && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
12969              < 16384)
12970         {
12971           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
12972           if (!loc)
12973             break;
12974           add_loc_descr (&loc,
12975                          new_loc_descr (DW_OP_plus_uconst,
12976                                         tree_low_cst (TREE_OPERAND (val, 1),
12977                                                       1), 0));
12978         }
12979       else
12980         {
12981           op = DW_OP_plus;
12982         do_binop:
12983           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
12984           if (!loc)
12985             break;
12986           loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
12987           if (!loc2)
12988             break;
12989           add_loc_descr (&loc, loc2);
12990           add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
12991         }
12992       return loc;
12993     case MINUS_EXPR:
12994       op = DW_OP_minus;
12995       goto do_binop;
12996     case MULT_EXPR:
12997       op = DW_OP_mul;
12998       goto do_binop;
12999     case EQ_EXPR:
13000       op = DW_OP_eq;
13001       goto do_binop;
13002     case NE_EXPR:
13003       op = DW_OP_ne;
13004       goto do_binop;
13005     default:
13006       break;
13007     }
13008   return NULL;
13009 }
13010
13011 static void
13012 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
13013                       tree val, tree base_decl)
13014 {
13015   dw_loc_descr_ref loc;
13016
13017   if (host_integerp (val, 0))
13018     {
13019       add_AT_unsigned (die, attr, tree_low_cst (val, 0));
13020       return;
13021     }
13022
13023   loc = descr_info_loc (val, base_decl);
13024   if (!loc)
13025     return;
13026
13027   add_AT_loc (die, attr, loc);
13028 }
13029
13030 /* This routine generates DIE for array with hidden descriptor, details
13031    are filled into *info by a langhook.  */
13032
13033 static void
13034 gen_descr_array_type_die (tree type, struct array_descr_info *info,
13035                           dw_die_ref context_die)
13036 {
13037   dw_die_ref scope_die = scope_die_for (type, context_die);
13038   dw_die_ref array_die;
13039   int dim;
13040
13041   array_die = new_die (DW_TAG_array_type, scope_die, type);
13042   add_name_attribute (array_die, type_tag (type));
13043   equate_type_number_to_die (type, array_die);
13044
13045   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
13046   if (is_fortran ()
13047       && info->ndimensions >= 2)
13048     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
13049
13050   if (info->data_location)
13051     add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
13052                           info->base_decl);
13053   if (info->associated)
13054     add_descr_info_field (array_die, DW_AT_associated, info->associated,
13055                           info->base_decl);
13056   if (info->allocated)
13057     add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
13058                           info->base_decl);
13059
13060   for (dim = 0; dim < info->ndimensions; dim++)
13061     {
13062       dw_die_ref subrange_die
13063         = new_die (DW_TAG_subrange_type, array_die, NULL);
13064
13065       if (info->dimen[dim].lower_bound)
13066         {
13067           /* If it is the default value, omit it.  */
13068           if ((is_c_family () || is_java ())
13069               && integer_zerop (info->dimen[dim].lower_bound))
13070             ;
13071           else if (is_fortran ()
13072                    && integer_onep (info->dimen[dim].lower_bound))
13073             ;
13074           else
13075             add_descr_info_field (subrange_die, DW_AT_lower_bound,
13076                                   info->dimen[dim].lower_bound,
13077                                   info->base_decl);
13078         }
13079       if (info->dimen[dim].upper_bound)
13080         add_descr_info_field (subrange_die, DW_AT_upper_bound,
13081                               info->dimen[dim].upper_bound,
13082                               info->base_decl);
13083       if (info->dimen[dim].stride)
13084         add_descr_info_field (subrange_die, DW_AT_byte_stride,
13085                               info->dimen[dim].stride,
13086                               info->base_decl);
13087     }
13088
13089   gen_type_die (info->element_type, context_die);
13090   add_type_attribute (array_die, info->element_type, 0, 0, context_die);
13091
13092   if (get_AT (array_die, DW_AT_name))
13093     add_pubtype (type, array_die);
13094 }
13095
13096 #if 0
13097 static void
13098 gen_entry_point_die (tree decl, dw_die_ref context_die)
13099 {
13100   tree origin = decl_ultimate_origin (decl);
13101   dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
13102
13103   if (origin != NULL)
13104     add_abstract_origin_attribute (decl_die, origin);
13105   else
13106     {
13107       add_name_and_src_coords_attributes (decl_die, decl);
13108       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
13109                           0, 0, context_die);
13110     }
13111
13112   if (DECL_ABSTRACT (decl))
13113     equate_decl_number_to_die (decl, decl_die);
13114   else
13115     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
13116 }
13117 #endif
13118
13119 /* Walk through the list of incomplete types again, trying once more to
13120    emit full debugging info for them.  */
13121
13122 static void
13123 retry_incomplete_types (void)
13124 {
13125   int i;
13126
13127   for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
13128     gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die);
13129 }
13130
13131 /* Generate a DIE to represent an inlined instance of an enumeration type.  */
13132
13133 static void
13134 gen_inlined_enumeration_type_die (tree type, dw_die_ref context_die)
13135 {
13136   dw_die_ref type_die = new_die (DW_TAG_enumeration_type, context_die, type);
13137
13138   /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
13139      be incomplete and such types are not marked.  */
13140   add_abstract_origin_attribute (type_die, type);
13141 }
13142
13143 /* Determine what tag to use for a record type.  */
13144
13145 static enum dwarf_tag
13146 record_type_tag (tree type)
13147 {
13148   if (! lang_hooks.types.classify_record)
13149     return DW_TAG_structure_type;
13150
13151   switch (lang_hooks.types.classify_record (type))
13152     {
13153     case RECORD_IS_STRUCT:
13154       return DW_TAG_structure_type;
13155
13156     case RECORD_IS_CLASS:
13157       return DW_TAG_class_type;
13158
13159     case RECORD_IS_INTERFACE:
13160       return DW_TAG_interface_type;
13161
13162     default:
13163       gcc_unreachable ();
13164     }
13165 }
13166
13167 /* Generate a DIE to represent an inlined instance of a structure type.  */
13168
13169 static void
13170 gen_inlined_structure_type_die (tree type, dw_die_ref context_die)
13171 {
13172   dw_die_ref type_die = new_die (record_type_tag (type), context_die, type);
13173
13174   /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
13175      be incomplete and such types are not marked.  */
13176   add_abstract_origin_attribute (type_die, type);
13177 }
13178
13179 /* Generate a DIE to represent an inlined instance of a union type.  */
13180
13181 static void
13182 gen_inlined_union_type_die (tree type, dw_die_ref context_die)
13183 {
13184   dw_die_ref type_die = new_die (DW_TAG_union_type, context_die, type);
13185
13186   /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
13187      be incomplete and such types are not marked.  */
13188   add_abstract_origin_attribute (type_die, type);
13189 }
13190
13191 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
13192    include all of the information about the enumeration values also. Each
13193    enumerated type name/value is listed as a child of the enumerated type
13194    DIE.  */
13195
13196 static dw_die_ref
13197 gen_enumeration_type_die (tree type, dw_die_ref context_die)
13198 {
13199   dw_die_ref type_die = lookup_type_die (type);
13200
13201   if (type_die == NULL)
13202     {
13203       type_die = new_die (DW_TAG_enumeration_type,
13204                           scope_die_for (type, context_die), type);
13205       equate_type_number_to_die (type, type_die);
13206       add_name_attribute (type_die, type_tag (type));
13207     }
13208   else if (! TYPE_SIZE (type))
13209     return type_die;
13210   else
13211     remove_AT (type_die, DW_AT_declaration);
13212
13213   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
13214      given enum type is incomplete, do not generate the DW_AT_byte_size
13215      attribute or the DW_AT_element_list attribute.  */
13216   if (TYPE_SIZE (type))
13217     {
13218       tree link;
13219
13220       TREE_ASM_WRITTEN (type) = 1;
13221       add_byte_size_attribute (type_die, type);
13222       if (TYPE_STUB_DECL (type) != NULL_TREE)
13223         add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
13224
13225       /* If the first reference to this type was as the return type of an
13226          inline function, then it may not have a parent.  Fix this now.  */
13227       if (type_die->die_parent == NULL)
13228         add_child_die (scope_die_for (type, context_die), type_die);
13229
13230       for (link = TYPE_VALUES (type);
13231            link != NULL; link = TREE_CHAIN (link))
13232         {
13233           dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
13234           tree value = TREE_VALUE (link);
13235
13236           add_name_attribute (enum_die,
13237                               IDENTIFIER_POINTER (TREE_PURPOSE (link)));
13238
13239           if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
13240             /* DWARF2 does not provide a way of indicating whether or
13241                not enumeration constants are signed or unsigned.  GDB
13242                always assumes the values are signed, so we output all
13243                values as if they were signed.  That means that
13244                enumeration constants with very large unsigned values
13245                will appear to have negative values in the debugger.  */
13246             add_AT_int (enum_die, DW_AT_const_value,
13247                         tree_low_cst (value, tree_int_cst_sgn (value) > 0));
13248         }
13249     }
13250   else
13251     add_AT_flag (type_die, DW_AT_declaration, 1);
13252
13253   if (get_AT (type_die, DW_AT_name))
13254     add_pubtype (type, type_die);
13255
13256   return type_die;
13257 }
13258
13259 /* Generate a DIE to represent either a real live formal parameter decl or to
13260    represent just the type of some formal parameter position in some function
13261    type.
13262
13263    Note that this routine is a bit unusual because its argument may be a
13264    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
13265    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
13266    node.  If it's the former then this function is being called to output a
13267    DIE to represent a formal parameter object (or some inlining thereof).  If
13268    it's the latter, then this function is only being called to output a
13269    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
13270    argument type of some subprogram type.  */
13271
13272 static dw_die_ref
13273 gen_formal_parameter_die (tree node, dw_die_ref context_die)
13274 {
13275   dw_die_ref parm_die
13276     = new_die (DW_TAG_formal_parameter, context_die, node);
13277   tree origin;
13278
13279   switch (TREE_CODE_CLASS (TREE_CODE (node)))
13280     {
13281     case tcc_declaration:
13282       origin = decl_ultimate_origin (node);
13283       if (origin != NULL)
13284         add_abstract_origin_attribute (parm_die, origin);
13285       else
13286         {
13287           tree type = TREE_TYPE (node);
13288           add_name_and_src_coords_attributes (parm_die, node);
13289           if (DECL_BY_REFERENCE (node))
13290             add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
13291                                 context_die);
13292           else
13293             add_type_attribute (parm_die, type,
13294                                 TREE_READONLY (node),
13295                                 TREE_THIS_VOLATILE (node),
13296                                 context_die);
13297           if (DECL_ARTIFICIAL (node))
13298             add_AT_flag (parm_die, DW_AT_artificial, 1);
13299         }
13300
13301       equate_decl_number_to_die (node, parm_die);
13302       if (! DECL_ABSTRACT (node))
13303         add_location_or_const_value_attribute (parm_die, node, DW_AT_location);
13304
13305       break;
13306
13307     case tcc_type:
13308       /* We were called with some kind of a ..._TYPE node.  */
13309       add_type_attribute (parm_die, node, 0, 0, context_die);
13310       break;
13311
13312     default:
13313       gcc_unreachable ();
13314     }
13315
13316   return parm_die;
13317 }
13318
13319 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
13320    at the end of an (ANSI prototyped) formal parameters list.  */
13321
13322 static void
13323 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
13324 {
13325   new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
13326 }
13327
13328 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
13329    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
13330    parameters as specified in some function type specification (except for
13331    those which appear as part of a function *definition*).  */
13332
13333 static void
13334 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
13335 {
13336   tree link;
13337   tree formal_type = NULL;
13338   tree first_parm_type;
13339   tree arg;
13340
13341   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
13342     {
13343       arg = DECL_ARGUMENTS (function_or_method_type);
13344       function_or_method_type = TREE_TYPE (function_or_method_type);
13345     }
13346   else
13347     arg = NULL_TREE;
13348
13349   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
13350
13351   /* Make our first pass over the list of formal parameter types and output a
13352      DW_TAG_formal_parameter DIE for each one.  */
13353   for (link = first_parm_type; link; )
13354     {
13355       dw_die_ref parm_die;
13356
13357       formal_type = TREE_VALUE (link);
13358       if (formal_type == void_type_node)
13359         break;
13360
13361       /* Output a (nameless) DIE to represent the formal parameter itself.  */
13362       parm_die = gen_formal_parameter_die (formal_type, context_die);
13363       if ((TREE_CODE (function_or_method_type) == METHOD_TYPE
13364            && link == first_parm_type)
13365           || (arg && DECL_ARTIFICIAL (arg)))
13366         add_AT_flag (parm_die, DW_AT_artificial, 1);
13367
13368       link = TREE_CHAIN (link);
13369       if (arg)
13370         arg = TREE_CHAIN (arg);
13371     }
13372
13373   /* If this function type has an ellipsis, add a
13374      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
13375   if (formal_type != void_type_node)
13376     gen_unspecified_parameters_die (function_or_method_type, context_die);
13377
13378   /* Make our second (and final) pass over the list of formal parameter types
13379      and output DIEs to represent those types (as necessary).  */
13380   for (link = TYPE_ARG_TYPES (function_or_method_type);
13381        link && TREE_VALUE (link);
13382        link = TREE_CHAIN (link))
13383     gen_type_die (TREE_VALUE (link), context_die);
13384 }
13385
13386 /* We want to generate the DIE for TYPE so that we can generate the
13387    die for MEMBER, which has been defined; we will need to refer back
13388    to the member declaration nested within TYPE.  If we're trying to
13389    generate minimal debug info for TYPE, processing TYPE won't do the
13390    trick; we need to attach the member declaration by hand.  */
13391
13392 static void
13393 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
13394 {
13395   gen_type_die (type, context_die);
13396
13397   /* If we're trying to avoid duplicate debug info, we may not have
13398      emitted the member decl for this function.  Emit it now.  */
13399   if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
13400       && ! lookup_decl_die (member))
13401     {
13402       dw_die_ref type_die;
13403       gcc_assert (!decl_ultimate_origin (member));
13404
13405       push_decl_scope (type);
13406       type_die = lookup_type_die (type);
13407       if (TREE_CODE (member) == FUNCTION_DECL)
13408         gen_subprogram_die (member, type_die);
13409       else if (TREE_CODE (member) == FIELD_DECL)
13410         {
13411           /* Ignore the nameless fields that are used to skip bits but handle
13412              C++ anonymous unions and structs.  */
13413           if (DECL_NAME (member) != NULL_TREE
13414               || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
13415               || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
13416             {
13417               gen_type_die (member_declared_type (member), type_die);
13418               gen_field_die (member, type_die);
13419             }
13420         }
13421       else
13422         gen_variable_die (member, type_die);
13423
13424       pop_decl_scope ();
13425     }
13426 }
13427
13428 /* Generate the DWARF2 info for the "abstract" instance of a function which we
13429    may later generate inlined and/or out-of-line instances of.  */
13430
13431 static void
13432 dwarf2out_abstract_function (tree decl)
13433 {
13434   dw_die_ref old_die;
13435   tree save_fn;
13436   tree context;
13437   int was_abstract = DECL_ABSTRACT (decl);
13438
13439   /* Make sure we have the actual abstract inline, not a clone.  */
13440   decl = DECL_ORIGIN (decl);
13441
13442   old_die = lookup_decl_die (decl);
13443   if (old_die && get_AT (old_die, DW_AT_inline))
13444     /* We've already generated the abstract instance.  */
13445     return;
13446
13447   /* Be sure we've emitted the in-class declaration DIE (if any) first, so
13448      we don't get confused by DECL_ABSTRACT.  */
13449   if (debug_info_level > DINFO_LEVEL_TERSE)
13450     {
13451       context = decl_class_context (decl);
13452       if (context)
13453         gen_type_die_for_member
13454           (context, decl, decl_function_context (decl) ? NULL : comp_unit_die);
13455     }
13456
13457   /* Pretend we've just finished compiling this function.  */
13458   save_fn = current_function_decl;
13459   current_function_decl = decl;
13460   push_cfun (DECL_STRUCT_FUNCTION (decl));
13461
13462   set_decl_abstract_flags (decl, 1);
13463   dwarf2out_decl (decl);
13464   if (! was_abstract)
13465     set_decl_abstract_flags (decl, 0);
13466
13467   current_function_decl = save_fn;
13468   pop_cfun ();
13469 }
13470
13471 /* Helper function of premark_used_types() which gets called through
13472    htab_traverse_resize().
13473
13474    Marks the DIE of a given type in *SLOT as perennial, so it never gets
13475    marked as unused by prune_unused_types.  */
13476 static int
13477 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
13478 {
13479   tree type;
13480   dw_die_ref die;
13481
13482   type = (tree) *slot;
13483   die = lookup_type_die (type);
13484   if (die != NULL)
13485     die->die_perennial_p = 1;
13486   return 1;
13487 }
13488
13489 /* Mark all members of used_types_hash as perennial.  */
13490 static void
13491 premark_used_types (void)
13492 {
13493   if (cfun && cfun->used_types_hash)
13494     htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
13495 }
13496
13497 /* Generate a DIE to represent a declared function (either file-scope or
13498    block-local).  */
13499
13500 static void
13501 gen_subprogram_die (tree decl, dw_die_ref context_die)
13502 {
13503   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
13504   tree origin = decl_ultimate_origin (decl);
13505   dw_die_ref subr_die;
13506   tree fn_arg_types;
13507   tree outer_scope;
13508   dw_die_ref old_die = lookup_decl_die (decl);
13509   int declaration = (current_function_decl != decl
13510                      || class_or_namespace_scope_p (context_die));
13511
13512   premark_used_types ();
13513
13514   /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
13515      started to generate the abstract instance of an inline, decided to output
13516      its containing class, and proceeded to emit the declaration of the inline
13517      from the member list for the class.  If so, DECLARATION takes priority;
13518      we'll get back to the abstract instance when done with the class.  */
13519
13520   /* The class-scope declaration DIE must be the primary DIE.  */
13521   if (origin && declaration && class_or_namespace_scope_p (context_die))
13522     {
13523       origin = NULL;
13524       gcc_assert (!old_die);
13525     }
13526
13527   /* Now that the C++ front end lazily declares artificial member fns, we
13528      might need to retrofit the declaration into its class.  */
13529   if (!declaration && !origin && !old_die
13530       && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
13531       && !class_or_namespace_scope_p (context_die)
13532       && debug_info_level > DINFO_LEVEL_TERSE)
13533     old_die = force_decl_die (decl);
13534
13535   if (origin != NULL)
13536     {
13537       gcc_assert (!declaration || local_scope_p (context_die));
13538
13539       /* Fixup die_parent for the abstract instance of a nested
13540          inline function.  */
13541       if (old_die && old_die->die_parent == NULL)
13542         add_child_die (context_die, old_die);
13543
13544       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
13545       add_abstract_origin_attribute (subr_die, origin);
13546     }
13547   else if (old_die)
13548     {
13549       expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
13550       struct dwarf_file_data * file_index = lookup_filename (s.file);
13551
13552       if (!get_AT_flag (old_die, DW_AT_declaration)
13553           /* We can have a normal definition following an inline one in the
13554              case of redefinition of GNU C extern inlines.
13555              It seems reasonable to use AT_specification in this case.  */
13556           && !get_AT (old_die, DW_AT_inline))
13557         {
13558           /* Detect and ignore this case, where we are trying to output
13559              something we have already output.  */
13560           return;
13561         }
13562
13563       /* If the definition comes from the same place as the declaration,
13564          maybe use the old DIE.  We always want the DIE for this function
13565          that has the *_pc attributes to be under comp_unit_die so the
13566          debugger can find it.  We also need to do this for abstract
13567          instances of inlines, since the spec requires the out-of-line copy
13568          to have the same parent.  For local class methods, this doesn't
13569          apply; we just use the old DIE.  */
13570       if ((old_die->die_parent == comp_unit_die || context_die == NULL)
13571           && (DECL_ARTIFICIAL (decl)
13572               || (get_AT_file (old_die, DW_AT_decl_file) == file_index
13573                   && (get_AT_unsigned (old_die, DW_AT_decl_line)
13574                       == (unsigned) s.line))))
13575         {
13576           subr_die = old_die;
13577
13578           /* Clear out the declaration attribute and the formal parameters.
13579              Do not remove all children, because it is possible that this
13580              declaration die was forced using force_decl_die(). In such
13581              cases die that forced declaration die (e.g. TAG_imported_module)
13582              is one of the children that we do not want to remove.  */
13583           remove_AT (subr_die, DW_AT_declaration);
13584           remove_child_TAG (subr_die, DW_TAG_formal_parameter);
13585         }
13586       else
13587         {
13588           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
13589           add_AT_specification (subr_die, old_die);
13590           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
13591             add_AT_file (subr_die, DW_AT_decl_file, file_index);
13592           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
13593             add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
13594         }
13595     }
13596   else
13597     {
13598       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
13599
13600       if (TREE_PUBLIC (decl))
13601         add_AT_flag (subr_die, DW_AT_external, 1);
13602
13603       add_name_and_src_coords_attributes (subr_die, decl);
13604       if (debug_info_level > DINFO_LEVEL_TERSE)
13605         {
13606           add_prototyped_attribute (subr_die, TREE_TYPE (decl));
13607           add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
13608                               0, 0, context_die);
13609         }
13610
13611       add_pure_or_virtual_attribute (subr_die, decl);
13612       if (DECL_ARTIFICIAL (decl))
13613         add_AT_flag (subr_die, DW_AT_artificial, 1);
13614
13615       if (TREE_PROTECTED (decl))
13616         add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_protected);
13617       else if (TREE_PRIVATE (decl))
13618         add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_private);
13619     }
13620
13621   if (declaration)
13622     {
13623       if (!old_die || !get_AT (old_die, DW_AT_inline))
13624         {
13625           add_AT_flag (subr_die, DW_AT_declaration, 1);
13626
13627           /* The first time we see a member function, it is in the context of
13628              the class to which it belongs.  We make sure of this by emitting
13629              the class first.  The next time is the definition, which is
13630              handled above.  The two may come from the same source text.
13631
13632              Note that force_decl_die() forces function declaration die. It is
13633              later reused to represent definition.  */
13634           equate_decl_number_to_die (decl, subr_die);
13635         }
13636     }
13637   else if (DECL_ABSTRACT (decl))
13638     {
13639       if (DECL_DECLARED_INLINE_P (decl))
13640         {
13641           if (cgraph_function_possibly_inlined_p (decl))
13642             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
13643           else
13644             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
13645         }
13646       else
13647         {
13648           if (cgraph_function_possibly_inlined_p (decl))
13649             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
13650           else
13651             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
13652         }
13653
13654       if (DECL_DECLARED_INLINE_P (decl)
13655           && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
13656         add_AT_flag (subr_die, DW_AT_artificial, 1);
13657
13658       equate_decl_number_to_die (decl, subr_die);
13659     }
13660   else if (!DECL_EXTERNAL (decl))
13661     {
13662       HOST_WIDE_INT cfa_fb_offset;
13663
13664       if (!old_die || !get_AT (old_die, DW_AT_inline))
13665         equate_decl_number_to_die (decl, subr_die);
13666
13667       if (!flag_reorder_blocks_and_partition)
13668         {
13669           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
13670                                        current_function_funcdef_no);
13671           add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
13672           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
13673                                        current_function_funcdef_no);
13674           add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
13675
13676           add_pubname (decl, subr_die);
13677           add_arange (decl, subr_die);
13678         }
13679       else
13680         {  /* Do nothing for now; maybe need to duplicate die, one for
13681               hot section and one for cold section, then use the hot/cold
13682               section begin/end labels to generate the aranges...  */
13683           /*
13684             add_AT_lbl_id (subr_die, DW_AT_low_pc, hot_section_label);
13685             add_AT_lbl_id (subr_die, DW_AT_high_pc, hot_section_end_label);
13686             add_AT_lbl_id (subr_die, DW_AT_lo_user, unlikely_section_label);
13687             add_AT_lbl_id (subr_die, DW_AT_hi_user, cold_section_end_label);
13688
13689             add_pubname (decl, subr_die);
13690             add_arange (decl, subr_die);
13691             add_arange (decl, subr_die);
13692            */
13693         }
13694
13695 #ifdef MIPS_DEBUGGING_INFO
13696       /* Add a reference to the FDE for this routine.  */
13697       add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
13698 #endif
13699
13700       cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
13701
13702       /* We define the "frame base" as the function's CFA.  This is more
13703          convenient for several reasons: (1) It's stable across the prologue
13704          and epilogue, which makes it better than just a frame pointer,
13705          (2) With dwarf3, there exists a one-byte encoding that allows us
13706          to reference the .debug_frame data by proxy, but failing that,
13707          (3) We can at least reuse the code inspection and interpretation
13708          code that determines the CFA position at various points in the
13709          function.  */
13710       /* ??? Use some command-line or configury switch to enable the use
13711          of dwarf3 DW_OP_call_frame_cfa.  At present there are no dwarf
13712          consumers that understand it; fall back to "pure" dwarf2 and
13713          convert the CFA data into a location list.  */
13714       {
13715         dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
13716         if (list->dw_loc_next)
13717           add_AT_loc_list (subr_die, DW_AT_frame_base, list);
13718         else
13719           add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
13720       }
13721
13722       /* Compute a displacement from the "steady-state frame pointer" to
13723          the CFA.  The former is what all stack slots and argument slots
13724          will reference in the rtl; the later is what we've told the
13725          debugger about.  We'll need to adjust all frame_base references
13726          by this displacement.  */
13727       compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
13728
13729       if (cfun->static_chain_decl)
13730         add_AT_location_description (subr_die, DW_AT_static_link,
13731                  loc_descriptor_from_tree (cfun->static_chain_decl));
13732     }
13733
13734   /* Now output descriptions of the arguments for this function. This gets
13735      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
13736      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
13737      `...' at the end of the formal parameter list.  In order to find out if
13738      there was a trailing ellipsis or not, we must instead look at the type
13739      associated with the FUNCTION_DECL.  This will be a node of type
13740      FUNCTION_TYPE. If the chain of type nodes hanging off of this
13741      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
13742      an ellipsis at the end.  */
13743
13744   /* In the case where we are describing a mere function declaration, all we
13745      need to do here (and all we *can* do here) is to describe the *types* of
13746      its formal parameters.  */
13747   if (debug_info_level <= DINFO_LEVEL_TERSE)
13748     ;
13749   else if (declaration)
13750     gen_formal_types_die (decl, subr_die);
13751   else
13752     {
13753       /* Generate DIEs to represent all known formal parameters.  */
13754       tree arg_decls = DECL_ARGUMENTS (decl);
13755       tree parm;
13756
13757       /* When generating DIEs, generate the unspecified_parameters DIE
13758          instead if we come across the arg "__builtin_va_alist" */
13759       for (parm = arg_decls; parm; parm = TREE_CHAIN (parm))
13760         if (TREE_CODE (parm) == PARM_DECL)
13761           {
13762             if (DECL_NAME (parm)
13763                 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (parm)),
13764                             "__builtin_va_alist"))
13765               gen_unspecified_parameters_die (parm, subr_die);
13766             else
13767               gen_decl_die (parm, subr_die);
13768           }
13769
13770       /* Decide whether we need an unspecified_parameters DIE at the end.
13771          There are 2 more cases to do this for: 1) the ansi ... declaration -
13772          this is detectable when the end of the arg list is not a
13773          void_type_node 2) an unprototyped function declaration (not a
13774          definition).  This just means that we have no info about the
13775          parameters at all.  */
13776       fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
13777       if (fn_arg_types != NULL)
13778         {
13779           /* This is the prototyped case, check for....  */
13780           if (TREE_VALUE (tree_last (fn_arg_types)) != void_type_node)
13781             gen_unspecified_parameters_die (decl, subr_die);
13782         }
13783       else if (DECL_INITIAL (decl) == NULL_TREE)
13784         gen_unspecified_parameters_die (decl, subr_die);
13785     }
13786
13787   /* Output Dwarf info for all of the stuff within the body of the function
13788      (if it has one - it may be just a declaration).  */
13789   outer_scope = DECL_INITIAL (decl);
13790
13791   /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
13792      a function.  This BLOCK actually represents the outermost binding contour
13793      for the function, i.e. the contour in which the function's formal
13794      parameters and labels get declared. Curiously, it appears that the front
13795      end doesn't actually put the PARM_DECL nodes for the current function onto
13796      the BLOCK_VARS list for this outer scope, but are strung off of the
13797      DECL_ARGUMENTS list for the function instead.
13798
13799      The BLOCK_VARS list for the `outer_scope' does provide us with a list of
13800      the LABEL_DECL nodes for the function however, and we output DWARF info
13801      for those in decls_for_scope.  Just within the `outer_scope' there will be
13802      a BLOCK node representing the function's outermost pair of curly braces,
13803      and any blocks used for the base and member initializers of a C++
13804      constructor function.  */
13805   if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
13806     {
13807       /* Emit a DW_TAG_variable DIE for a named return value.  */
13808       if (DECL_NAME (DECL_RESULT (decl)))
13809         gen_decl_die (DECL_RESULT (decl), subr_die);
13810
13811       current_function_has_inlines = 0;
13812       decls_for_scope (outer_scope, subr_die, 0);
13813
13814 #if 0 && defined (MIPS_DEBUGGING_INFO)
13815       if (current_function_has_inlines)
13816         {
13817           add_AT_flag (subr_die, DW_AT_MIPS_has_inlines, 1);
13818           if (! comp_unit_has_inlines)
13819             {
13820               add_AT_flag (comp_unit_die, DW_AT_MIPS_has_inlines, 1);
13821               comp_unit_has_inlines = 1;
13822             }
13823         }
13824 #endif
13825     }
13826   /* Add the calling convention attribute if requested.  */
13827   add_calling_convention_attribute (subr_die, decl);
13828
13829 }
13830
13831 /* Returns a hash value for X (which really is a die_struct).  */
13832
13833 static hashval_t
13834 common_block_die_table_hash (const void *x)
13835 {
13836   const_dw_die_ref d = (const_dw_die_ref) x;
13837   return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
13838 }
13839
13840 /* Return nonzero if decl_id and die_parent of die_struct X is the same
13841    as decl_id and die_parent of die_struct Y.  */
13842
13843 static int
13844 common_block_die_table_eq (const void *x, const void *y)
13845 {
13846   const_dw_die_ref d = (const_dw_die_ref) x;
13847   const_dw_die_ref e = (const_dw_die_ref) y;
13848   return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
13849 }
13850
13851 /* Generate a DIE to represent a declared data object.  */
13852
13853 static void
13854 gen_variable_die (tree decl, dw_die_ref context_die)
13855 {
13856   HOST_WIDE_INT off;
13857   tree com_decl;
13858   dw_die_ref var_die;
13859   tree origin = decl_ultimate_origin (decl);
13860   dw_die_ref old_die = lookup_decl_die (decl);
13861   int declaration = (DECL_EXTERNAL (decl)
13862                      /* If DECL is COMDAT and has not actually been
13863                         emitted, we cannot take its address; there
13864                         might end up being no definition anywhere in
13865                         the program.  For example, consider the C++
13866                         test case:
13867
13868                           template <class T>
13869                           struct S { static const int i = 7; };
13870
13871                           template <class T>
13872                           const int S<T>::i;
13873
13874                           int f() { return S<int>::i; }
13875
13876                         Here, S<int>::i is not DECL_EXTERNAL, but no
13877                         definition is required, so the compiler will
13878                         not emit a definition.  */
13879                      || (TREE_CODE (decl) == VAR_DECL
13880                          && DECL_COMDAT (decl) && !TREE_ASM_WRITTEN (decl))
13881                      || class_or_namespace_scope_p (context_die));
13882
13883   com_decl = fortran_common (decl, &off);
13884
13885   /* Symbol in common gets emitted as a child of the common block, in the form
13886      of a data member.  */
13887   if (com_decl)
13888     {
13889       tree field;
13890       dw_die_ref com_die;
13891       dw_loc_descr_ref loc;
13892       die_node com_die_arg;
13893
13894       var_die = lookup_decl_die (decl);
13895       if (var_die)
13896         {
13897           if (get_AT (var_die, DW_AT_location) == NULL)
13898             {
13899               loc = loc_descriptor_from_tree (com_decl);
13900               if (loc)
13901                 {
13902                   if (off)
13903                     {
13904                       /* Optimize the common case.  */
13905                       if (loc->dw_loc_opc == DW_OP_addr
13906                           && loc->dw_loc_next == NULL
13907                           && GET_CODE (loc->dw_loc_oprnd1.v.val_addr)
13908                              == SYMBOL_REF)
13909                         loc->dw_loc_oprnd1.v.val_addr
13910                           = plus_constant (loc->dw_loc_oprnd1.v.val_addr, off);
13911                         else
13912                           add_loc_descr (&loc,
13913                                          new_loc_descr (DW_OP_plus_uconst,
13914                                                         off, 0));
13915                     }
13916                   add_AT_loc (var_die, DW_AT_location, loc);
13917                   remove_AT (var_die, DW_AT_declaration);
13918                 }
13919             }
13920           return;
13921         }
13922
13923       if (common_block_die_table == NULL)
13924         common_block_die_table
13925           = htab_create_ggc (10, common_block_die_table_hash,
13926                              common_block_die_table_eq, NULL);
13927
13928       field = TREE_OPERAND (DECL_VALUE_EXPR (decl), 0);
13929       com_die_arg.decl_id = DECL_UID (com_decl);
13930       com_die_arg.die_parent = context_die;
13931       com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
13932       loc = loc_descriptor_from_tree (com_decl);
13933       if (com_die == NULL)
13934         {
13935           const char *cnam
13936             = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
13937           void **slot;
13938
13939           com_die = new_die (DW_TAG_common_block, context_die, decl);
13940           add_name_and_src_coords_attributes (com_die, com_decl);
13941           if (loc)
13942             {
13943               add_AT_loc (com_die, DW_AT_location, loc);
13944               /* Avoid sharing the same loc descriptor between
13945                  DW_TAG_common_block and DW_TAG_variable.  */
13946               loc = loc_descriptor_from_tree (com_decl);
13947             }
13948           else if (DECL_EXTERNAL (decl))
13949             add_AT_flag (com_die, DW_AT_declaration, 1);
13950           add_pubname_string (cnam, com_die); /* ??? needed? */
13951           com_die->decl_id = DECL_UID (com_decl);
13952           slot = htab_find_slot (common_block_die_table, com_die, INSERT);
13953           *slot = (void *) com_die;
13954         }
13955       else if (get_AT (com_die, DW_AT_location) == NULL && loc)
13956         {
13957           add_AT_loc (com_die, DW_AT_location, loc);
13958           loc = loc_descriptor_from_tree (com_decl);
13959           remove_AT (com_die, DW_AT_declaration);
13960         }
13961       var_die = new_die (DW_TAG_variable, com_die, decl);
13962       add_name_and_src_coords_attributes (var_die, decl);
13963       add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
13964                           TREE_THIS_VOLATILE (decl), context_die);
13965       add_AT_flag (var_die, DW_AT_external, 1);
13966       if (loc)
13967         {
13968           if (off)
13969             {
13970               /* Optimize the common case.  */
13971               if (loc->dw_loc_opc == DW_OP_addr
13972                   && loc->dw_loc_next == NULL
13973                   && GET_CODE (loc->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
13974                 loc->dw_loc_oprnd1.v.val_addr
13975                   = plus_constant (loc->dw_loc_oprnd1.v.val_addr, off);
13976               else
13977                 add_loc_descr (&loc, new_loc_descr (DW_OP_plus_uconst,
13978                                                     off, 0));
13979             }
13980           add_AT_loc (var_die, DW_AT_location, loc);
13981         }
13982       else if (DECL_EXTERNAL (decl))
13983         add_AT_flag (var_die, DW_AT_declaration, 1);
13984       equate_decl_number_to_die (decl, var_die);
13985       return;
13986     }
13987
13988   var_die = new_die (DW_TAG_variable, context_die, decl);
13989
13990   if (origin != NULL)
13991     add_abstract_origin_attribute (var_die, origin);
13992
13993   /* Loop unrolling can create multiple blocks that refer to the same
13994      static variable, so we must test for the DW_AT_declaration flag.
13995
13996      ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
13997      copy decls and set the DECL_ABSTRACT flag on them instead of
13998      sharing them.
13999
14000      ??? Duplicated blocks have been rewritten to use .debug_ranges.
14001
14002      ??? The declare_in_namespace support causes us to get two DIEs for one
14003      variable, both of which are declarations.  We want to avoid considering
14004      one to be a specification, so we must test that this DIE is not a
14005      declaration.  */
14006   else if (old_die && TREE_STATIC (decl) && ! declaration
14007            && get_AT_flag (old_die, DW_AT_declaration) == 1)
14008     {
14009       /* This is a definition of a C++ class level static.  */
14010       add_AT_specification (var_die, old_die);
14011       if (DECL_NAME (decl))
14012         {
14013           expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
14014           struct dwarf_file_data * file_index = lookup_filename (s.file);
14015
14016           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
14017             add_AT_file (var_die, DW_AT_decl_file, file_index);
14018
14019           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
14020             add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
14021         }
14022     }
14023   else
14024     {
14025       tree type = TREE_TYPE (decl);
14026
14027       add_name_and_src_coords_attributes (var_die, decl);
14028       if ((TREE_CODE (decl) == PARM_DECL
14029            || TREE_CODE (decl) == RESULT_DECL)
14030           && DECL_BY_REFERENCE (decl))
14031         add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
14032       else
14033         add_type_attribute (var_die, type, TREE_READONLY (decl),
14034                             TREE_THIS_VOLATILE (decl), context_die);
14035
14036       if (TREE_PUBLIC (decl))
14037         add_AT_flag (var_die, DW_AT_external, 1);
14038
14039       if (DECL_ARTIFICIAL (decl))
14040         add_AT_flag (var_die, DW_AT_artificial, 1);
14041
14042       if (TREE_PROTECTED (decl))
14043         add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_protected);
14044       else if (TREE_PRIVATE (decl))
14045         add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_private);
14046     }
14047
14048   if (declaration)
14049     add_AT_flag (var_die, DW_AT_declaration, 1);
14050
14051   if (DECL_ABSTRACT (decl) || declaration)
14052     equate_decl_number_to_die (decl, var_die);
14053
14054   if (! declaration && ! DECL_ABSTRACT (decl))
14055     {
14056       add_location_or_const_value_attribute (var_die, decl, DW_AT_location);
14057       add_pubname (decl, var_die);
14058     }
14059   else
14060     tree_add_const_value_attribute (var_die, decl);
14061 }
14062
14063 /* Generate a DIE to represent a named constant.  */
14064
14065 static void
14066 gen_const_die (tree decl, dw_die_ref context_die)
14067 {
14068   dw_die_ref const_die;
14069   tree type = TREE_TYPE (decl);
14070
14071   const_die = new_die (DW_TAG_constant, context_die, decl);
14072   add_name_and_src_coords_attributes (const_die, decl);
14073   add_type_attribute (const_die, type, 1, 0, context_die);
14074   if (TREE_PUBLIC (decl))
14075     add_AT_flag (const_die, DW_AT_external, 1);
14076   if (DECL_ARTIFICIAL (decl))
14077     add_AT_flag (const_die, DW_AT_artificial, 1);
14078   tree_add_const_value_attribute (const_die, decl);
14079 }
14080
14081 /* Generate a DIE to represent a label identifier.  */
14082
14083 static void
14084 gen_label_die (tree decl, dw_die_ref context_die)
14085 {
14086   tree origin = decl_ultimate_origin (decl);
14087   dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
14088   rtx insn;
14089   char label[MAX_ARTIFICIAL_LABEL_BYTES];
14090
14091   if (origin != NULL)
14092     add_abstract_origin_attribute (lbl_die, origin);
14093   else
14094     add_name_and_src_coords_attributes (lbl_die, decl);
14095
14096   if (DECL_ABSTRACT (decl))
14097     equate_decl_number_to_die (decl, lbl_die);
14098   else
14099     {
14100       insn = DECL_RTL_IF_SET (decl);
14101
14102       /* Deleted labels are programmer specified labels which have been
14103          eliminated because of various optimizations.  We still emit them
14104          here so that it is possible to put breakpoints on them.  */
14105       if (insn
14106           && (LABEL_P (insn)
14107               || ((NOTE_P (insn)
14108                    && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
14109         {
14110           /* When optimization is enabled (via -O) some parts of the compiler
14111              (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
14112              represent source-level labels which were explicitly declared by
14113              the user.  This really shouldn't be happening though, so catch
14114              it if it ever does happen.  */
14115           gcc_assert (!INSN_DELETED_P (insn));
14116
14117           ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
14118           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
14119         }
14120     }
14121 }
14122
14123 /* A helper function for gen_inlined_subroutine_die.  Add source coordinate
14124    attributes to the DIE for a block STMT, to describe where the inlined
14125    function was called from.  This is similar to add_src_coords_attributes.  */
14126
14127 static inline void
14128 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
14129 {
14130   expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
14131
14132   add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
14133   add_AT_unsigned (die, DW_AT_call_line, s.line);
14134 }
14135
14136
14137 /* If STMT's abstract origin is a function declaration and STMT's
14138    first subblock's abstract origin is the function's outermost block,
14139    then we're looking at the main entry point.  */
14140 static bool
14141 is_inlined_entry_point (const_tree stmt)
14142 {
14143   tree decl, block;
14144
14145   if (!stmt || TREE_CODE (stmt) != BLOCK)
14146     return false;
14147
14148   decl = block_ultimate_origin (stmt);
14149
14150   if (!decl || TREE_CODE (decl) != FUNCTION_DECL)
14151     return false;
14152
14153   block = BLOCK_SUBBLOCKS (stmt);
14154
14155   if (block)
14156     {
14157       if (TREE_CODE (block) != BLOCK)
14158         return false;
14159
14160       block = block_ultimate_origin (block);
14161     }
14162
14163   return block == DECL_INITIAL (decl);
14164 }
14165
14166 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
14167    Add low_pc and high_pc attributes to the DIE for a block STMT.  */
14168
14169 static inline void
14170 add_high_low_attributes (tree stmt, dw_die_ref die)
14171 {
14172   char label[MAX_ARTIFICIAL_LABEL_BYTES];
14173
14174   if (BLOCK_FRAGMENT_CHAIN (stmt))
14175     {
14176       tree chain;
14177
14178       if (is_inlined_entry_point (stmt))
14179         {
14180           ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
14181                                        BLOCK_NUMBER (stmt));
14182           add_AT_lbl_id (die, DW_AT_entry_pc, label);
14183         }
14184
14185       add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
14186
14187       chain = BLOCK_FRAGMENT_CHAIN (stmt);
14188       do
14189         {
14190           add_ranges (chain);
14191           chain = BLOCK_FRAGMENT_CHAIN (chain);
14192         }
14193       while (chain);
14194       add_ranges (NULL);
14195     }
14196   else
14197     {
14198       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
14199                                    BLOCK_NUMBER (stmt));
14200       add_AT_lbl_id (die, DW_AT_low_pc, label);
14201       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
14202                                    BLOCK_NUMBER (stmt));
14203       add_AT_lbl_id (die, DW_AT_high_pc, label);
14204     }
14205 }
14206
14207 /* Generate a DIE for a lexical block.  */
14208
14209 static void
14210 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
14211 {
14212   dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
14213
14214   if (! BLOCK_ABSTRACT (stmt))
14215     add_high_low_attributes (stmt, stmt_die);
14216
14217   decls_for_scope (stmt, stmt_die, depth);
14218 }
14219
14220 /* Generate a DIE for an inlined subprogram.  */
14221
14222 static void
14223 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
14224 {
14225   tree decl = block_ultimate_origin (stmt);
14226
14227   /* Emit info for the abstract instance first, if we haven't yet.  We
14228      must emit this even if the block is abstract, otherwise when we
14229      emit the block below (or elsewhere), we may end up trying to emit
14230      a die whose origin die hasn't been emitted, and crashing.  */
14231   dwarf2out_abstract_function (decl);
14232
14233   if (! BLOCK_ABSTRACT (stmt))
14234     {
14235       dw_die_ref subr_die
14236         = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
14237
14238       add_abstract_origin_attribute (subr_die, decl);
14239       add_high_low_attributes (stmt, subr_die);
14240       add_call_src_coords_attributes (stmt, subr_die);
14241
14242       decls_for_scope (stmt, subr_die, depth);
14243       current_function_has_inlines = 1;
14244     }
14245   else
14246     /* We may get here if we're the outer block of function A that was
14247        inlined into function B that was inlined into function C.  When
14248        generating debugging info for C, dwarf2out_abstract_function(B)
14249        would mark all inlined blocks as abstract, including this one.
14250        So, we wouldn't (and shouldn't) expect labels to be generated
14251        for this one.  Instead, just emit debugging info for
14252        declarations within the block.  This is particularly important
14253        in the case of initializers of arguments passed from B to us:
14254        if they're statement expressions containing declarations, we
14255        wouldn't generate dies for their abstract variables, and then,
14256        when generating dies for the real variables, we'd die (pun
14257        intended :-)  */
14258     gen_lexical_block_die (stmt, context_die, depth);
14259 }
14260
14261 /* Generate a DIE for a field in a record, or structure.  */
14262
14263 static void
14264 gen_field_die (tree decl, dw_die_ref context_die)
14265 {
14266   dw_die_ref decl_die;
14267
14268   if (TREE_TYPE (decl) == error_mark_node)
14269     return;
14270
14271   decl_die = new_die (DW_TAG_member, context_die, decl);
14272   add_name_and_src_coords_attributes (decl_die, decl);
14273   add_type_attribute (decl_die, member_declared_type (decl),
14274                       TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
14275                       context_die);
14276
14277   if (DECL_BIT_FIELD_TYPE (decl))
14278     {
14279       add_byte_size_attribute (decl_die, decl);
14280       add_bit_size_attribute (decl_die, decl);
14281       add_bit_offset_attribute (decl_die, decl);
14282     }
14283
14284   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
14285     add_data_member_location_attribute (decl_die, decl);
14286
14287   if (DECL_ARTIFICIAL (decl))
14288     add_AT_flag (decl_die, DW_AT_artificial, 1);
14289
14290   if (TREE_PROTECTED (decl))
14291     add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_protected);
14292   else if (TREE_PRIVATE (decl))
14293     add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_private);
14294
14295   /* Equate decl number to die, so that we can look up this decl later on.  */
14296   equate_decl_number_to_die (decl, decl_die);
14297 }
14298
14299 #if 0
14300 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
14301    Use modified_type_die instead.
14302    We keep this code here just in case these types of DIEs may be needed to
14303    represent certain things in other languages (e.g. Pascal) someday.  */
14304
14305 static void
14306 gen_pointer_type_die (tree type, dw_die_ref context_die)
14307 {
14308   dw_die_ref ptr_die
14309     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
14310
14311   equate_type_number_to_die (type, ptr_die);
14312   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
14313   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
14314 }
14315
14316 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
14317    Use modified_type_die instead.
14318    We keep this code here just in case these types of DIEs may be needed to
14319    represent certain things in other languages (e.g. Pascal) someday.  */
14320
14321 static void
14322 gen_reference_type_die (tree type, dw_die_ref context_die)
14323 {
14324   dw_die_ref ref_die
14325     = new_die (DW_TAG_reference_type, scope_die_for (type, context_die), type);
14326
14327   equate_type_number_to_die (type, ref_die);
14328   add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
14329   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
14330 }
14331 #endif
14332
14333 /* Generate a DIE for a pointer to a member type.  */
14334
14335 static void
14336 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
14337 {
14338   dw_die_ref ptr_die
14339     = new_die (DW_TAG_ptr_to_member_type,
14340                scope_die_for (type, context_die), type);
14341
14342   equate_type_number_to_die (type, ptr_die);
14343   add_AT_die_ref (ptr_die, DW_AT_containing_type,
14344                   lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
14345   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
14346 }
14347
14348 /* Generate the DIE for the compilation unit.  */
14349
14350 static dw_die_ref
14351 gen_compile_unit_die (const char *filename)
14352 {
14353   dw_die_ref die;
14354   char producer[250];
14355   const char *language_string = lang_hooks.name;
14356   int language;
14357
14358   die = new_die (DW_TAG_compile_unit, NULL, NULL);
14359
14360   if (filename)
14361     {
14362       add_name_attribute (die, filename);
14363       /* Don't add cwd for <built-in>.  */
14364       if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
14365         add_comp_dir_attribute (die);
14366     }
14367
14368   sprintf (producer, "%s %s", language_string, version_string);
14369
14370 #ifdef MIPS_DEBUGGING_INFO
14371   /* The MIPS/SGI compilers place the 'cc' command line options in the producer
14372      string.  The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
14373      not appear in the producer string, the debugger reaches the conclusion
14374      that the object file is stripped and has no debugging information.
14375      To get the MIPS/SGI debugger to believe that there is debugging
14376      information in the object file, we add a -g to the producer string.  */
14377   if (debug_info_level > DINFO_LEVEL_TERSE)
14378     strcat (producer, " -g");
14379 #endif
14380
14381   add_AT_string (die, DW_AT_producer, producer);
14382
14383   if (strcmp (language_string, "GNU C++") == 0)
14384     language = DW_LANG_C_plus_plus;
14385   else if (strcmp (language_string, "GNU Ada") == 0)
14386     language = DW_LANG_Ada95;
14387   else if (strcmp (language_string, "GNU F77") == 0)
14388     language = DW_LANG_Fortran77;
14389   else if (strcmp (language_string, "GNU Fortran") == 0)
14390     language = DW_LANG_Fortran95;
14391   else if (strcmp (language_string, "GNU Pascal") == 0)
14392     language = DW_LANG_Pascal83;
14393   else if (strcmp (language_string, "GNU Java") == 0)
14394     language = DW_LANG_Java;
14395   else if (strcmp (language_string, "GNU Objective-C") == 0)
14396     language = DW_LANG_ObjC;
14397   else if (strcmp (language_string, "GNU Objective-C++") == 0)
14398     language = DW_LANG_ObjC_plus_plus;
14399   else
14400     language = DW_LANG_C89;
14401
14402   add_AT_unsigned (die, DW_AT_language, language);
14403   return die;
14404 }
14405
14406 /* Generate the DIE for a base class.  */
14407
14408 static void
14409 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
14410 {
14411   dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
14412
14413   add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
14414   add_data_member_location_attribute (die, binfo);
14415
14416   if (BINFO_VIRTUAL_P (binfo))
14417     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
14418
14419   if (access == access_public_node)
14420     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
14421   else if (access == access_protected_node)
14422     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
14423 }
14424
14425 /* Generate a DIE for a class member.  */
14426
14427 static void
14428 gen_member_die (tree type, dw_die_ref context_die)
14429 {
14430   tree member;
14431   tree binfo = TYPE_BINFO (type);
14432   dw_die_ref child;
14433
14434   /* If this is not an incomplete type, output descriptions of each of its
14435      members. Note that as we output the DIEs necessary to represent the
14436      members of this record or union type, we will also be trying to output
14437      DIEs to represent the *types* of those members. However the `type'
14438      function (above) will specifically avoid generating type DIEs for member
14439      types *within* the list of member DIEs for this (containing) type except
14440      for those types (of members) which are explicitly marked as also being
14441      members of this (containing) type themselves.  The g++ front- end can
14442      force any given type to be treated as a member of some other (containing)
14443      type by setting the TYPE_CONTEXT of the given (member) type to point to
14444      the TREE node representing the appropriate (containing) type.  */
14445
14446   /* First output info about the base classes.  */
14447   if (binfo)
14448     {
14449       VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
14450       int i;
14451       tree base;
14452
14453       for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
14454         gen_inheritance_die (base,
14455                              (accesses ? VEC_index (tree, accesses, i)
14456                               : access_public_node), context_die);
14457     }
14458
14459   /* Now output info about the data members and type members.  */
14460   for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member))
14461     {
14462       /* If we thought we were generating minimal debug info for TYPE
14463          and then changed our minds, some of the member declarations
14464          may have already been defined.  Don't define them again, but
14465          do put them in the right order.  */
14466
14467       child = lookup_decl_die (member);
14468       if (child)
14469         splice_child_die (context_die, child);
14470       else
14471         gen_decl_die (member, context_die);
14472     }
14473
14474   /* Now output info about the function members (if any).  */
14475   for (member = TYPE_METHODS (type); member; member = TREE_CHAIN (member))
14476     {
14477       /* Don't include clones in the member list.  */
14478       if (DECL_ABSTRACT_ORIGIN (member))
14479         continue;
14480
14481       child = lookup_decl_die (member);
14482       if (child)
14483         splice_child_die (context_die, child);
14484       else
14485         gen_decl_die (member, context_die);
14486     }
14487 }
14488
14489 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
14490    is set, we pretend that the type was never defined, so we only get the
14491    member DIEs needed by later specification DIEs.  */
14492
14493 static void
14494 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
14495                                 enum debug_info_usage usage)
14496 {
14497   dw_die_ref type_die = lookup_type_die (type);
14498   dw_die_ref scope_die = 0;
14499   int nested = 0;
14500   int complete = (TYPE_SIZE (type)
14501                   && (! TYPE_STUB_DECL (type)
14502                       || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
14503   int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
14504   complete = complete && should_emit_struct_debug (type, usage);
14505
14506   if (type_die && ! complete)
14507     return;
14508
14509   if (TYPE_CONTEXT (type) != NULL_TREE
14510       && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
14511           || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
14512     nested = 1;
14513
14514   scope_die = scope_die_for (type, context_die);
14515
14516   if (! type_die || (nested && scope_die == comp_unit_die))
14517     /* First occurrence of type or toplevel definition of nested class.  */
14518     {
14519       dw_die_ref old_die = type_die;
14520
14521       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
14522                           ? record_type_tag (type) : DW_TAG_union_type,
14523                           scope_die, type);
14524       equate_type_number_to_die (type, type_die);
14525       if (old_die)
14526         add_AT_specification (type_die, old_die);
14527       else
14528         add_name_attribute (type_die, type_tag (type));
14529     }
14530   else
14531     remove_AT (type_die, DW_AT_declaration);
14532
14533   /* If this type has been completed, then give it a byte_size attribute and
14534      then give a list of members.  */
14535   if (complete && !ns_decl)
14536     {
14537       /* Prevent infinite recursion in cases where the type of some member of
14538          this type is expressed in terms of this type itself.  */
14539       TREE_ASM_WRITTEN (type) = 1;
14540       add_byte_size_attribute (type_die, type);
14541       if (TYPE_STUB_DECL (type) != NULL_TREE)
14542         add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
14543
14544       /* If the first reference to this type was as the return type of an
14545          inline function, then it may not have a parent.  Fix this now.  */
14546       if (type_die->die_parent == NULL)
14547         add_child_die (scope_die, type_die);
14548
14549       push_decl_scope (type);
14550       gen_member_die (type, type_die);
14551       pop_decl_scope ();
14552
14553       /* GNU extension: Record what type our vtable lives in.  */
14554       if (TYPE_VFIELD (type))
14555         {
14556           tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
14557
14558           gen_type_die (vtype, context_die);
14559           add_AT_die_ref (type_die, DW_AT_containing_type,
14560                           lookup_type_die (vtype));
14561         }
14562     }
14563   else
14564     {
14565       add_AT_flag (type_die, DW_AT_declaration, 1);
14566
14567       /* We don't need to do this for function-local types.  */
14568       if (TYPE_STUB_DECL (type)
14569           && ! decl_function_context (TYPE_STUB_DECL (type)))
14570         VEC_safe_push (tree, gc, incomplete_types, type);
14571     }
14572
14573   if (get_AT (type_die, DW_AT_name))
14574     add_pubtype (type, type_die);
14575 }
14576
14577 /* Generate a DIE for a subroutine _type_.  */
14578
14579 static void
14580 gen_subroutine_type_die (tree type, dw_die_ref context_die)
14581 {
14582   tree return_type = TREE_TYPE (type);
14583   dw_die_ref subr_die
14584     = new_die (DW_TAG_subroutine_type,
14585                scope_die_for (type, context_die), type);
14586
14587   equate_type_number_to_die (type, subr_die);
14588   add_prototyped_attribute (subr_die, type);
14589   add_type_attribute (subr_die, return_type, 0, 0, context_die);
14590   gen_formal_types_die (type, subr_die);
14591
14592   if (get_AT (subr_die, DW_AT_name))
14593     add_pubtype (type, subr_die);
14594 }
14595
14596 /* Generate a DIE for a type definition.  */
14597
14598 static void
14599 gen_typedef_die (tree decl, dw_die_ref context_die)
14600 {
14601   dw_die_ref type_die;
14602   tree origin;
14603
14604   if (TREE_ASM_WRITTEN (decl))
14605     return;
14606
14607   TREE_ASM_WRITTEN (decl) = 1;
14608   type_die = new_die (DW_TAG_typedef, context_die, decl);
14609   origin = decl_ultimate_origin (decl);
14610   if (origin != NULL)
14611     add_abstract_origin_attribute (type_die, origin);
14612   else
14613     {
14614       tree type;
14615
14616       add_name_and_src_coords_attributes (type_die, decl);
14617       if (DECL_ORIGINAL_TYPE (decl))
14618         {
14619           type = DECL_ORIGINAL_TYPE (decl);
14620
14621           gcc_assert (type != TREE_TYPE (decl));
14622           equate_type_number_to_die (TREE_TYPE (decl), type_die);
14623         }
14624       else
14625         type = TREE_TYPE (decl);
14626
14627       add_type_attribute (type_die, type, TREE_READONLY (decl),
14628                           TREE_THIS_VOLATILE (decl), context_die);
14629     }
14630
14631   if (DECL_ABSTRACT (decl))
14632     equate_decl_number_to_die (decl, type_die);
14633
14634   if (get_AT (type_die, DW_AT_name))
14635     add_pubtype (decl, type_die);
14636 }
14637
14638 /* Generate a type description DIE.  */
14639
14640 static void
14641 gen_type_die_with_usage (tree type, dw_die_ref context_die,
14642                                 enum debug_info_usage usage)
14643 {
14644   int need_pop;
14645   struct array_descr_info info;
14646
14647   if (type == NULL_TREE || type == error_mark_node)
14648     return;
14649
14650   if (TYPE_NAME (type) && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
14651       && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
14652     {
14653       if (TREE_ASM_WRITTEN (type))
14654         return;
14655
14656       /* Prevent broken recursion; we can't hand off to the same type.  */
14657       gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
14658
14659       TREE_ASM_WRITTEN (type) = 1;
14660       gen_decl_die (TYPE_NAME (type), context_die);
14661       return;
14662     }
14663
14664   /* If this is an array type with hidden descriptor, handle it first.  */
14665   if (!TREE_ASM_WRITTEN (type)
14666       && lang_hooks.types.get_array_descr_info
14667       && lang_hooks.types.get_array_descr_info (type, &info))
14668     {
14669       gen_descr_array_type_die (type, &info, context_die);
14670       TREE_ASM_WRITTEN (type) = 1;
14671       return;
14672     }
14673
14674   /* We are going to output a DIE to represent the unqualified version
14675      of this type (i.e. without any const or volatile qualifiers) so
14676      get the main variant (i.e. the unqualified version) of this type
14677      now.  (Vectors are special because the debugging info is in the
14678      cloned type itself).  */
14679   if (TREE_CODE (type) != VECTOR_TYPE)
14680     type = type_main_variant (type);
14681
14682   if (TREE_ASM_WRITTEN (type))
14683     return;
14684
14685   switch (TREE_CODE (type))
14686     {
14687     case ERROR_MARK:
14688       break;
14689
14690     case POINTER_TYPE:
14691     case REFERENCE_TYPE:
14692       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
14693          ensures that the gen_type_die recursion will terminate even if the
14694          type is recursive.  Recursive types are possible in Ada.  */
14695       /* ??? We could perhaps do this for all types before the switch
14696          statement.  */
14697       TREE_ASM_WRITTEN (type) = 1;
14698
14699       /* For these types, all that is required is that we output a DIE (or a
14700          set of DIEs) to represent the "basis" type.  */
14701       gen_type_die_with_usage (TREE_TYPE (type), context_die,
14702                                 DINFO_USAGE_IND_USE);
14703       break;
14704
14705     case OFFSET_TYPE:
14706       /* This code is used for C++ pointer-to-data-member types.
14707          Output a description of the relevant class type.  */
14708       gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
14709                                         DINFO_USAGE_IND_USE);
14710
14711       /* Output a description of the type of the object pointed to.  */
14712       gen_type_die_with_usage (TREE_TYPE (type), context_die,
14713                                         DINFO_USAGE_IND_USE);
14714
14715       /* Now output a DIE to represent this pointer-to-data-member type
14716          itself.  */
14717       gen_ptr_to_mbr_type_die (type, context_die);
14718       break;
14719
14720     case FUNCTION_TYPE:
14721       /* Force out return type (in case it wasn't forced out already).  */
14722       gen_type_die_with_usage (TREE_TYPE (type), context_die,
14723                                         DINFO_USAGE_DIR_USE);
14724       gen_subroutine_type_die (type, context_die);
14725       break;
14726
14727     case METHOD_TYPE:
14728       /* Force out return type (in case it wasn't forced out already).  */
14729       gen_type_die_with_usage (TREE_TYPE (type), context_die,
14730                                         DINFO_USAGE_DIR_USE);
14731       gen_subroutine_type_die (type, context_die);
14732       break;
14733
14734     case ARRAY_TYPE:
14735       gen_array_type_die (type, context_die);
14736       break;
14737
14738     case VECTOR_TYPE:
14739       gen_array_type_die (type, context_die);
14740       break;
14741
14742     case ENUMERAL_TYPE:
14743     case RECORD_TYPE:
14744     case UNION_TYPE:
14745     case QUAL_UNION_TYPE:
14746       /* If this is a nested type whose containing class hasn't been written
14747          out yet, writing it out will cover this one, too.  This does not apply
14748          to instantiations of member class templates; they need to be added to
14749          the containing class as they are generated.  FIXME: This hurts the
14750          idea of combining type decls from multiple TUs, since we can't predict
14751          what set of template instantiations we'll get.  */
14752       if (TYPE_CONTEXT (type)
14753           && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
14754           && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
14755         {
14756           gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
14757
14758           if (TREE_ASM_WRITTEN (type))
14759             return;
14760
14761           /* If that failed, attach ourselves to the stub.  */
14762           push_decl_scope (TYPE_CONTEXT (type));
14763           context_die = lookup_type_die (TYPE_CONTEXT (type));
14764           need_pop = 1;
14765         }
14766       else
14767         {
14768           context_die = declare_in_namespace (type, context_die);
14769           need_pop = 0;
14770         }
14771
14772       if (TREE_CODE (type) == ENUMERAL_TYPE)
14773         {
14774           /* This might have been written out by the call to
14775              declare_in_namespace.  */
14776           if (!TREE_ASM_WRITTEN (type))
14777             gen_enumeration_type_die (type, context_die);
14778         }
14779       else
14780         gen_struct_or_union_type_die (type, context_die, usage);
14781
14782       if (need_pop)
14783         pop_decl_scope ();
14784
14785       /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
14786          it up if it is ever completed.  gen_*_type_die will set it for us
14787          when appropriate.  */
14788       return;
14789
14790     case VOID_TYPE:
14791     case INTEGER_TYPE:
14792     case REAL_TYPE:
14793     case FIXED_POINT_TYPE:
14794     case COMPLEX_TYPE:
14795     case BOOLEAN_TYPE:
14796       /* No DIEs needed for fundamental types.  */
14797       break;
14798
14799     case LANG_TYPE:
14800       /* No Dwarf representation currently defined.  */
14801       break;
14802
14803     default:
14804       gcc_unreachable ();
14805     }
14806
14807   TREE_ASM_WRITTEN (type) = 1;
14808 }
14809
14810 static void
14811 gen_type_die (tree type, dw_die_ref context_die)
14812 {
14813   gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
14814 }
14815
14816 /* Generate a DIE for a tagged type instantiation.  */
14817
14818 static void
14819 gen_tagged_type_instantiation_die (tree type, dw_die_ref context_die)
14820 {
14821   if (type == NULL_TREE || type == error_mark_node)
14822     return;
14823
14824   /* We are going to output a DIE to represent the unqualified version of
14825      this type (i.e. without any const or volatile qualifiers) so make sure
14826      that we have the main variant (i.e. the unqualified version) of this
14827      type now.  */
14828   gcc_assert (type == type_main_variant (type));
14829
14830   /* Do not check TREE_ASM_WRITTEN (type) as it may not be set if this is
14831      an instance of an unresolved type.  */
14832
14833   switch (TREE_CODE (type))
14834     {
14835     case ERROR_MARK:
14836       break;
14837
14838     case ENUMERAL_TYPE:
14839       gen_inlined_enumeration_type_die (type, context_die);
14840       break;
14841
14842     case RECORD_TYPE:
14843       gen_inlined_structure_type_die (type, context_die);
14844       break;
14845
14846     case UNION_TYPE:
14847     case QUAL_UNION_TYPE:
14848       gen_inlined_union_type_die (type, context_die);
14849       break;
14850
14851     default:
14852       gcc_unreachable ();
14853     }
14854 }
14855
14856 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
14857    things which are local to the given block.  */
14858
14859 static void
14860 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
14861 {
14862   int must_output_die = 0;
14863   tree origin;
14864   tree decl;
14865   enum tree_code origin_code;
14866
14867   /* Ignore blocks that are NULL.  */
14868   if (stmt == NULL_TREE)
14869     return;
14870
14871   /* If the block is one fragment of a non-contiguous block, do not
14872      process the variables, since they will have been done by the
14873      origin block.  Do process subblocks.  */
14874   if (BLOCK_FRAGMENT_ORIGIN (stmt))
14875     {
14876       tree sub;
14877
14878       for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
14879         gen_block_die (sub, context_die, depth + 1);
14880
14881       return;
14882     }
14883
14884   /* Determine the "ultimate origin" of this block.  This block may be an
14885      inlined instance of an inlined instance of inline function, so we have
14886      to trace all of the way back through the origin chain to find out what
14887      sort of node actually served as the original seed for the creation of
14888      the current block.  */
14889   origin = block_ultimate_origin (stmt);
14890   origin_code = (origin != NULL) ? TREE_CODE (origin) : ERROR_MARK;
14891
14892   /* Determine if we need to output any Dwarf DIEs at all to represent this
14893      block.  */
14894   if (origin_code == FUNCTION_DECL)
14895     /* The outer scopes for inlinings *must* always be represented.  We
14896        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
14897     must_output_die = 1;
14898   else
14899     {
14900       /* In the case where the current block represents an inlining of the
14901          "body block" of an inline function, we must *NOT* output any DIE for
14902          this block because we have already output a DIE to represent the whole
14903          inlined function scope and the "body block" of any function doesn't
14904          really represent a different scope according to ANSI C rules.  So we
14905          check here to make sure that this block does not represent a "body
14906          block inlining" before trying to set the MUST_OUTPUT_DIE flag.  */
14907       if (! is_body_block (origin ? origin : stmt))
14908         {
14909           /* Determine if this block directly contains any "significant"
14910              local declarations which we will need to output DIEs for.  */
14911           if (debug_info_level > DINFO_LEVEL_TERSE)
14912             /* We are not in terse mode so *any* local declaration counts
14913                as being a "significant" one.  */
14914             must_output_die = (BLOCK_VARS (stmt) != NULL
14915                                && (TREE_USED (stmt)
14916                                    || TREE_ASM_WRITTEN (stmt)
14917                                    || BLOCK_ABSTRACT (stmt)));
14918           else
14919             /* We are in terse mode, so only local (nested) function
14920                definitions count as "significant" local declarations.  */
14921             for (decl = BLOCK_VARS (stmt);
14922                  decl != NULL; decl = TREE_CHAIN (decl))
14923               if (TREE_CODE (decl) == FUNCTION_DECL
14924                   && DECL_INITIAL (decl))
14925                 {
14926                   must_output_die = 1;
14927                   break;
14928                 }
14929         }
14930     }
14931
14932   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
14933      DIE for any block which contains no significant local declarations at
14934      all.  Rather, in such cases we just call `decls_for_scope' so that any
14935      needed Dwarf info for any sub-blocks will get properly generated. Note
14936      that in terse mode, our definition of what constitutes a "significant"
14937      local declaration gets restricted to include only inlined function
14938      instances and local (nested) function definitions.  */
14939   if (must_output_die)
14940     {
14941       if (origin_code == FUNCTION_DECL)
14942         gen_inlined_subroutine_die (stmt, context_die, depth);
14943       else
14944         gen_lexical_block_die (stmt, context_die, depth);
14945     }
14946   else
14947     decls_for_scope (stmt, context_die, depth);
14948 }
14949
14950 /* Generate all of the decls declared within a given scope and (recursively)
14951    all of its sub-blocks.  */
14952
14953 static void
14954 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
14955 {
14956   tree decl;
14957   tree subblocks;
14958
14959   /* Ignore NULL blocks.  */
14960   if (stmt == NULL_TREE)
14961     return;
14962
14963   if (TREE_USED (stmt))
14964     {
14965       /* Output the DIEs to represent all of the data objects and typedefs
14966          declared directly within this block but not within any nested
14967          sub-blocks.  Also, nested function and tag DIEs have been
14968          generated with a parent of NULL; fix that up now.  */
14969       for (decl = BLOCK_VARS (stmt); decl != NULL; decl = TREE_CHAIN (decl))
14970         {
14971           dw_die_ref die;
14972
14973           if (TREE_CODE (decl) == FUNCTION_DECL)
14974             die = lookup_decl_die (decl);
14975           else if (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl))
14976             die = lookup_type_die (TREE_TYPE (decl));
14977           else
14978             die = NULL;
14979
14980           if (die != NULL && die->die_parent == NULL)
14981             add_child_die (context_die, die);
14982           /* Do not produce debug information for static variables since
14983              these might be optimized out.  We are called for these later
14984              in varpool_analyze_pending_decls.
14985
14986              But *do* produce it for Fortran COMMON variables because,
14987              even though they are static, their names can differ depending
14988              on the scope, which we need to preserve.  */
14989           if (TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl)
14990               && !(is_fortran () && TREE_PUBLIC (decl)))
14991             ;
14992           else if (TREE_CODE (decl) == IMPORTED_DECL)
14993             dwarf2out_imported_module_or_decl_1 (decl, DECL_NAME (decl),
14994                                                  stmt, context_die);
14995           else
14996             gen_decl_die (decl, context_die);
14997         }
14998     }
14999
15000   /* If we're at -g1, we're not interested in subblocks.  */
15001   if (debug_info_level <= DINFO_LEVEL_TERSE)
15002     return;
15003
15004   /* Output the DIEs to represent all sub-blocks (and the items declared
15005      therein) of this block.  */
15006   for (subblocks = BLOCK_SUBBLOCKS (stmt);
15007        subblocks != NULL;
15008        subblocks = BLOCK_CHAIN (subblocks))
15009     gen_block_die (subblocks, context_die, depth + 1);
15010 }
15011
15012 /* Is this a typedef we can avoid emitting?  */
15013
15014 static inline int
15015 is_redundant_typedef (const_tree decl)
15016 {
15017   if (TYPE_DECL_IS_STUB (decl))
15018     return 1;
15019
15020   if (DECL_ARTIFICIAL (decl)
15021       && DECL_CONTEXT (decl)
15022       && is_tagged_type (DECL_CONTEXT (decl))
15023       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
15024       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
15025     /* Also ignore the artificial member typedef for the class name.  */
15026     return 1;
15027
15028   return 0;
15029 }
15030
15031 /* Returns the DIE for a context.  */
15032
15033 static inline dw_die_ref
15034 get_context_die (tree context)
15035 {
15036   if (context)
15037     {
15038       /* Find die that represents this context.  */
15039       if (TYPE_P (context))
15040         return force_type_die (context);
15041       else
15042         return force_decl_die (context);
15043     }
15044   return comp_unit_die;
15045 }
15046
15047 /* Returns the DIE for decl.  A DIE will always be returned.  */
15048
15049 static dw_die_ref
15050 force_decl_die (tree decl)
15051 {
15052   dw_die_ref decl_die;
15053   unsigned saved_external_flag;
15054   tree save_fn = NULL_TREE;
15055   decl_die = lookup_decl_die (decl);
15056   if (!decl_die)
15057     {
15058       dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
15059
15060       decl_die = lookup_decl_die (decl);
15061       if (decl_die)
15062         return decl_die;
15063
15064       switch (TREE_CODE (decl))
15065         {
15066         case FUNCTION_DECL:
15067           /* Clear current_function_decl, so that gen_subprogram_die thinks
15068              that this is a declaration. At this point, we just want to force
15069              declaration die.  */
15070           save_fn = current_function_decl;
15071           current_function_decl = NULL_TREE;
15072           gen_subprogram_die (decl, context_die);
15073           current_function_decl = save_fn;
15074           break;
15075
15076         case VAR_DECL:
15077           /* Set external flag to force declaration die. Restore it after
15078            gen_decl_die() call.  */
15079           saved_external_flag = DECL_EXTERNAL (decl);
15080           DECL_EXTERNAL (decl) = 1;
15081           gen_decl_die (decl, context_die);
15082           DECL_EXTERNAL (decl) = saved_external_flag;
15083           break;
15084
15085         case NAMESPACE_DECL:
15086           dwarf2out_decl (decl);
15087           break;
15088
15089         default:
15090           gcc_unreachable ();
15091         }
15092
15093       /* We should be able to find the DIE now.  */
15094       if (!decl_die)
15095         decl_die = lookup_decl_die (decl);
15096       gcc_assert (decl_die);
15097     }
15098
15099   return decl_die;
15100 }
15101
15102 /* Returns the DIE for TYPE, that must not be a base type.  A DIE is
15103    always returned.  */
15104
15105 static dw_die_ref
15106 force_type_die (tree type)
15107 {
15108   dw_die_ref type_die;
15109
15110   type_die = lookup_type_die (type);
15111   if (!type_die)
15112     {
15113       dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
15114
15115       type_die = modified_type_die (type, TYPE_READONLY (type),
15116                                     TYPE_VOLATILE (type), context_die);
15117       gcc_assert (type_die);
15118     }
15119   return type_die;
15120 }
15121
15122 /* Force out any required namespaces to be able to output DECL,
15123    and return the new context_die for it, if it's changed.  */
15124
15125 static dw_die_ref
15126 setup_namespace_context (tree thing, dw_die_ref context_die)
15127 {
15128   tree context = (DECL_P (thing)
15129                   ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
15130   if (context && TREE_CODE (context) == NAMESPACE_DECL)
15131     /* Force out the namespace.  */
15132     context_die = force_decl_die (context);
15133
15134   return context_die;
15135 }
15136
15137 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
15138    type) within its namespace, if appropriate.
15139
15140    For compatibility with older debuggers, namespace DIEs only contain
15141    declarations; all definitions are emitted at CU scope.  */
15142
15143 static dw_die_ref
15144 declare_in_namespace (tree thing, dw_die_ref context_die)
15145 {
15146   dw_die_ref ns_context;
15147
15148   if (debug_info_level <= DINFO_LEVEL_TERSE)
15149     return context_die;
15150
15151   /* If this decl is from an inlined function, then don't try to emit it in its
15152      namespace, as we will get confused.  It would have already been emitted
15153      when the abstract instance of the inline function was emitted anyways.  */
15154   if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
15155     return context_die;
15156
15157   ns_context = setup_namespace_context (thing, context_die);
15158
15159   if (ns_context != context_die)
15160     {
15161       if (is_fortran ())
15162         return ns_context;
15163       if (DECL_P (thing))
15164         gen_decl_die (thing, ns_context);
15165       else
15166         gen_type_die (thing, ns_context);
15167     }
15168   return context_die;
15169 }
15170
15171 /* Generate a DIE for a namespace or namespace alias.  */
15172
15173 static void
15174 gen_namespace_die (tree decl)
15175 {
15176   dw_die_ref context_die = setup_namespace_context (decl, comp_unit_die);
15177
15178   /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
15179      they are an alias of.  */
15180   if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
15181     {
15182       /* Output a real namespace or module.  */
15183       dw_die_ref namespace_die
15184         = new_die (is_fortran () ? DW_TAG_module : DW_TAG_namespace,
15185                    context_die, decl);
15186       /* For Fortran modules defined in different CU don't add src coords.  */
15187       if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
15188         add_name_attribute (namespace_die, dwarf2_name (decl, 0));
15189       else
15190         add_name_and_src_coords_attributes (namespace_die, decl);
15191       if (DECL_EXTERNAL (decl))
15192         add_AT_flag (namespace_die, DW_AT_declaration, 1);
15193       equate_decl_number_to_die (decl, namespace_die);
15194     }
15195   else
15196     {
15197       /* Output a namespace alias.  */
15198
15199       /* Force out the namespace we are an alias of, if necessary.  */
15200       dw_die_ref origin_die
15201         = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
15202
15203       /* Now create the namespace alias DIE.  */
15204       dw_die_ref namespace_die
15205         = new_die (DW_TAG_imported_declaration, context_die, decl);
15206       add_name_and_src_coords_attributes (namespace_die, decl);
15207       add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
15208       equate_decl_number_to_die (decl, namespace_die);
15209     }
15210 }
15211
15212 /* Generate Dwarf debug information for a decl described by DECL.  */
15213
15214 static void
15215 gen_decl_die (tree decl, dw_die_ref context_die)
15216 {
15217   tree origin;
15218
15219   if (DECL_P (decl) && DECL_IGNORED_P (decl))
15220     return;
15221
15222   switch (TREE_CODE (decl))
15223     {
15224     case ERROR_MARK:
15225       break;
15226
15227     case CONST_DECL:
15228       if (!is_fortran ())
15229         {
15230           /* The individual enumerators of an enum type get output when we output
15231              the Dwarf representation of the relevant enum type itself.  */
15232           break;
15233         }
15234
15235       /* Emit its type.  */
15236       gen_type_die (TREE_TYPE (decl), context_die);
15237
15238       /* And its containing namespace.  */
15239       context_die = declare_in_namespace (decl, context_die);
15240
15241       gen_const_die (decl, context_die);
15242       break;
15243
15244     case FUNCTION_DECL:
15245       /* Don't output any DIEs to represent mere function declarations,
15246          unless they are class members or explicit block externs.  */
15247       if (DECL_INITIAL (decl) == NULL_TREE && DECL_CONTEXT (decl) == NULL_TREE
15248           && (current_function_decl == NULL_TREE || DECL_ARTIFICIAL (decl)))
15249         break;
15250
15251 #if 0
15252       /* FIXME */
15253       /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
15254          on local redeclarations of global functions.  That seems broken.  */
15255       if (current_function_decl != decl)
15256         /* This is only a declaration.  */;
15257 #endif
15258
15259       /* If we're emitting a clone, emit info for the abstract instance.  */
15260       if (DECL_ORIGIN (decl) != decl)
15261         dwarf2out_abstract_function (DECL_ABSTRACT_ORIGIN (decl));
15262
15263       /* If we're emitting an out-of-line copy of an inline function,
15264          emit info for the abstract instance and set up to refer to it.  */
15265       else if (cgraph_function_possibly_inlined_p (decl)
15266                && ! DECL_ABSTRACT (decl)
15267                && ! class_or_namespace_scope_p (context_die)
15268                /* dwarf2out_abstract_function won't emit a die if this is just
15269                   a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
15270                   that case, because that works only if we have a die.  */
15271                && DECL_INITIAL (decl) != NULL_TREE)
15272         {
15273           dwarf2out_abstract_function (decl);
15274           set_decl_origin_self (decl);
15275         }
15276
15277       /* Otherwise we're emitting the primary DIE for this decl.  */
15278       else if (debug_info_level > DINFO_LEVEL_TERSE)
15279         {
15280           /* Before we describe the FUNCTION_DECL itself, make sure that we
15281              have described its return type.  */
15282           gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
15283
15284           /* And its virtual context.  */
15285           if (DECL_VINDEX (decl) != NULL_TREE)
15286             gen_type_die (DECL_CONTEXT (decl), context_die);
15287
15288           /* And its containing type.  */
15289           origin = decl_class_context (decl);
15290           if (origin != NULL_TREE)
15291             gen_type_die_for_member (origin, decl, context_die);
15292
15293           /* And its containing namespace.  */
15294           context_die = declare_in_namespace (decl, context_die);
15295         }
15296
15297       /* Now output a DIE to represent the function itself.  */
15298       gen_subprogram_die (decl, context_die);
15299       break;
15300
15301     case TYPE_DECL:
15302       /* If we are in terse mode, don't generate any DIEs to represent any
15303          actual typedefs.  */
15304       if (debug_info_level <= DINFO_LEVEL_TERSE)
15305         break;
15306
15307       /* In the special case of a TYPE_DECL node representing the declaration
15308          of some type tag, if the given TYPE_DECL is marked as having been
15309          instantiated from some other (original) TYPE_DECL node (e.g. one which
15310          was generated within the original definition of an inline function) we
15311          have to generate a special (abbreviated) DW_TAG_structure_type,
15312          DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  */
15313       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE
15314           && is_tagged_type (TREE_TYPE (decl)))
15315         {
15316           gen_tagged_type_instantiation_die (TREE_TYPE (decl), context_die);
15317           break;
15318         }
15319
15320       if (is_redundant_typedef (decl))
15321         gen_type_die (TREE_TYPE (decl), context_die);
15322       else
15323         /* Output a DIE to represent the typedef itself.  */
15324         gen_typedef_die (decl, context_die);
15325       break;
15326
15327     case LABEL_DECL:
15328       if (debug_info_level >= DINFO_LEVEL_NORMAL)
15329         gen_label_die (decl, context_die);
15330       break;
15331
15332     case VAR_DECL:
15333     case RESULT_DECL:
15334       /* If we are in terse mode, don't generate any DIEs to represent any
15335          variable declarations or definitions.  */
15336       if (debug_info_level <= DINFO_LEVEL_TERSE)
15337         break;
15338
15339       /* Output any DIEs that are needed to specify the type of this data
15340          object.  */
15341       if (TREE_CODE (decl) == RESULT_DECL && DECL_BY_REFERENCE (decl))
15342         gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
15343       else
15344         gen_type_die (TREE_TYPE (decl), context_die);
15345
15346       /* And its containing type.  */
15347       origin = decl_class_context (decl);
15348       if (origin != NULL_TREE)
15349         gen_type_die_for_member (origin, decl, context_die);
15350
15351       /* And its containing namespace.  */
15352       context_die = declare_in_namespace (decl, context_die);
15353
15354       /* Now output the DIE to represent the data object itself.  This gets
15355          complicated because of the possibility that the VAR_DECL really
15356          represents an inlined instance of a formal parameter for an inline
15357          function.  */
15358       origin = decl_ultimate_origin (decl);
15359       if (origin != NULL_TREE && TREE_CODE (origin) == PARM_DECL)
15360         gen_formal_parameter_die (decl, context_die);
15361       else
15362         gen_variable_die (decl, context_die);
15363       break;
15364
15365     case FIELD_DECL:
15366       /* Ignore the nameless fields that are used to skip bits but handle C++
15367          anonymous unions and structs.  */
15368       if (DECL_NAME (decl) != NULL_TREE
15369           || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
15370           || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
15371         {
15372           gen_type_die (member_declared_type (decl), context_die);
15373           gen_field_die (decl, context_die);
15374         }
15375       break;
15376
15377     case PARM_DECL:
15378       if (DECL_BY_REFERENCE (decl))
15379         gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
15380       else
15381         gen_type_die (TREE_TYPE (decl), context_die);
15382       gen_formal_parameter_die (decl, context_die);
15383       break;
15384
15385     case NAMESPACE_DECL:
15386     case IMPORTED_DECL:
15387       gen_namespace_die (decl);
15388       break;
15389
15390     default:
15391       /* Probably some frontend-internal decl.  Assume we don't care.  */
15392       gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
15393       break;
15394     }
15395 }
15396 \f
15397 /* Output debug information for global decl DECL.  Called from toplev.c after
15398    compilation proper has finished.  */
15399
15400 static void
15401 dwarf2out_global_decl (tree decl)
15402 {
15403   /* Output DWARF2 information for file-scope tentative data object
15404      declarations, file-scope (extern) function declarations (which
15405      had no corresponding body) and file-scope tagged type declarations
15406      and definitions which have not yet been forced out.  */
15407   if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
15408     dwarf2out_decl (decl);
15409 }
15410
15411 /* Output debug information for type decl DECL.  Called from toplev.c
15412    and from language front ends (to record built-in types).  */
15413 static void
15414 dwarf2out_type_decl (tree decl, int local)
15415 {
15416   if (!local)
15417     dwarf2out_decl (decl);
15418 }
15419
15420 /* Output debug information for imported module or decl DECL.
15421    NAME is non-NULL name in the lexical block if the decl has been renamed.
15422    LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
15423    that DECL belongs to.
15424    LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK.  */
15425 static void
15426 dwarf2out_imported_module_or_decl_1 (tree decl,
15427                                      tree name,
15428                                      tree lexical_block,
15429                                      dw_die_ref lexical_block_die)
15430 {
15431   expanded_location xloc;
15432   dw_die_ref imported_die = NULL;
15433   dw_die_ref at_import_die;
15434
15435   if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
15436     {
15437       if (is_base_type (TREE_TYPE (decl)))
15438         at_import_die = base_type_die (TREE_TYPE (decl));
15439       else
15440         at_import_die = force_type_die (TREE_TYPE (decl));
15441       /* For namespace N { typedef void T; } using N::T; base_type_die
15442          returns NULL, but DW_TAG_imported_declaration requires
15443          the DW_AT_import tag.  Force creation of DW_TAG_typedef.  */
15444       if (!at_import_die)
15445         {
15446           gcc_assert (TREE_CODE (decl) == TYPE_DECL);
15447           gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
15448           at_import_die = lookup_type_die (TREE_TYPE (decl));
15449           gcc_assert (at_import_die);
15450         }
15451     }
15452   else if (TREE_CODE (decl) == IMPORTED_DECL)
15453     {
15454       tree imported_ns_decl;
15455       /* IMPORTED_DECL nodes that are not imported namespace are just not
15456          supported yet.  */
15457       gcc_assert (DECL_INITIAL (decl)
15458                   && TREE_CODE (DECL_INITIAL (decl)) == NAMESPACE_DECL);
15459       imported_ns_decl = DECL_INITIAL (decl);
15460       at_import_die = lookup_decl_die (imported_ns_decl);
15461       if (!at_import_die)
15462         at_import_die = force_decl_die (imported_ns_decl);
15463       gcc_assert (at_import_die);
15464     }
15465   else
15466     {
15467       at_import_die = lookup_decl_die (decl);
15468       if (!at_import_die)
15469         {
15470           /* If we're trying to avoid duplicate debug info, we may not have
15471              emitted the member decl for this field.  Emit it now.  */
15472           if (TREE_CODE (decl) == FIELD_DECL)
15473             {
15474               tree type = DECL_CONTEXT (decl);
15475
15476               if (TYPE_CONTEXT (type)
15477                   && TYPE_P (TYPE_CONTEXT (type))
15478                   && !should_emit_struct_debug (TYPE_CONTEXT (type),
15479                                                 DINFO_USAGE_DIR_USE))
15480                 return;
15481               gen_type_die_for_member (type, decl,
15482                                        get_context_die (TYPE_CONTEXT (type)));
15483             }
15484           at_import_die = force_decl_die (decl);
15485         }
15486     }
15487
15488   if (TREE_CODE (decl) == NAMESPACE_DECL)
15489     imported_die = new_die (DW_TAG_imported_module,
15490                             lexical_block_die,
15491                             lexical_block);
15492   else
15493     imported_die = new_die (DW_TAG_imported_declaration,
15494                             lexical_block_die,
15495                             lexical_block);
15496
15497   xloc = expand_location (input_location);
15498   add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
15499   add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
15500   if (name)
15501     add_AT_string (imported_die, DW_AT_name,
15502                    IDENTIFIER_POINTER (name));
15503   add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
15504 }
15505
15506 /* Output debug information for imported module or decl DECL.
15507    NAME is non-NULL name in context if the decl has been renamed.
15508    CHILD is true if decl is one of the renamed decls as part of
15509    importing whole module.  */
15510
15511 static void
15512 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
15513                                    bool child)
15514 {
15515   /* dw_die_ref at_import_die;  */
15516   dw_die_ref scope_die;
15517
15518   if (debug_info_level <= DINFO_LEVEL_TERSE)
15519     return;
15520
15521   gcc_assert (decl);
15522
15523   /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
15524      We need decl DIE for reference and scope die. First, get DIE for the decl
15525      itself.  */
15526
15527   /* Get the scope die for decl context. Use comp_unit_die for global module
15528      or decl. If die is not found for non globals, force new die.  */
15529   if (context
15530       && TYPE_P (context)
15531       && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
15532     return;
15533   scope_die = get_context_die (context);
15534
15535   if (child)
15536     {
15537       gcc_assert (scope_die->die_child);
15538       gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
15539       gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
15540       scope_die = scope_die->die_child;
15541     }
15542
15543   /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
15544   dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
15545
15546 }
15547
15548 /* Write the debugging output for DECL.  */
15549
15550 void
15551 dwarf2out_decl (tree decl)
15552 {
15553   dw_die_ref context_die = comp_unit_die;
15554
15555   switch (TREE_CODE (decl))
15556     {
15557     case ERROR_MARK:
15558       return;
15559
15560     case FUNCTION_DECL:
15561       /* What we would really like to do here is to filter out all mere
15562          file-scope declarations of file-scope functions which are never
15563          referenced later within this translation unit (and keep all of ones
15564          that *are* referenced later on) but we aren't clairvoyant, so we have
15565          no idea which functions will be referenced in the future (i.e. later
15566          on within the current translation unit). So here we just ignore all
15567          file-scope function declarations which are not also definitions.  If
15568          and when the debugger needs to know something about these functions,
15569          it will have to hunt around and find the DWARF information associated
15570          with the definition of the function.
15571
15572          We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
15573          nodes represent definitions and which ones represent mere
15574          declarations.  We have to check DECL_INITIAL instead. That's because
15575          the C front-end supports some weird semantics for "extern inline"
15576          function definitions.  These can get inlined within the current
15577          translation unit (and thus, we need to generate Dwarf info for their
15578          abstract instances so that the Dwarf info for the concrete inlined
15579          instances can have something to refer to) but the compiler never
15580          generates any out-of-lines instances of such things (despite the fact
15581          that they *are* definitions).
15582
15583          The important point is that the C front-end marks these "extern
15584          inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
15585          them anyway. Note that the C++ front-end also plays some similar games
15586          for inline function definitions appearing within include files which
15587          also contain `#pragma interface' pragmas.  */
15588       if (DECL_INITIAL (decl) == NULL_TREE)
15589         return;
15590
15591       /* If we're a nested function, initially use a parent of NULL; if we're
15592          a plain function, this will be fixed up in decls_for_scope.  If
15593          we're a method, it will be ignored, since we already have a DIE.  */
15594       if (decl_function_context (decl)
15595           /* But if we're in terse mode, we don't care about scope.  */
15596           && debug_info_level > DINFO_LEVEL_TERSE)
15597         context_die = NULL;
15598       break;
15599
15600     case VAR_DECL:
15601       /* Ignore this VAR_DECL if it refers to a file-scope extern data object
15602          declaration and if the declaration was never even referenced from
15603          within this entire compilation unit.  We suppress these DIEs in
15604          order to save space in the .debug section (by eliminating entries
15605          which are probably useless).  Note that we must not suppress
15606          block-local extern declarations (whether used or not) because that
15607          would screw-up the debugger's name lookup mechanism and cause it to
15608          miss things which really ought to be in scope at a given point.  */
15609       if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
15610         return;
15611
15612       /* For local statics lookup proper context die.  */
15613       if (TREE_STATIC (decl) && decl_function_context (decl))
15614         context_die = lookup_decl_die (DECL_CONTEXT (decl));
15615
15616       /* If we are in terse mode, don't generate any DIEs to represent any
15617          variable declarations or definitions.  */
15618       if (debug_info_level <= DINFO_LEVEL_TERSE)
15619         return;
15620       break;
15621
15622     case CONST_DECL:
15623       if (debug_info_level <= DINFO_LEVEL_TERSE)
15624         return;
15625       if (!is_fortran ())
15626         return;
15627       if (TREE_STATIC (decl) && decl_function_context (decl))
15628         context_die = lookup_decl_die (DECL_CONTEXT (decl));
15629       break;
15630
15631     case NAMESPACE_DECL:
15632     case IMPORTED_DECL:
15633       if (debug_info_level <= DINFO_LEVEL_TERSE)
15634         return;
15635       if (lookup_decl_die (decl) != NULL)
15636         return;
15637       break;
15638
15639     case TYPE_DECL:
15640       /* Don't emit stubs for types unless they are needed by other DIEs.  */
15641       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
15642         return;
15643
15644       /* Don't bother trying to generate any DIEs to represent any of the
15645          normal built-in types for the language we are compiling.  */
15646       if (DECL_IS_BUILTIN (decl))
15647         {
15648           /* OK, we need to generate one for `bool' so GDB knows what type
15649              comparisons have.  */
15650           if (is_cxx ()
15651               && TREE_CODE (TREE_TYPE (decl)) == BOOLEAN_TYPE
15652               && ! DECL_IGNORED_P (decl))
15653             modified_type_die (TREE_TYPE (decl), 0, 0, NULL);
15654
15655           return;
15656         }
15657
15658       /* If we are in terse mode, don't generate any DIEs for types.  */
15659       if (debug_info_level <= DINFO_LEVEL_TERSE)
15660         return;
15661
15662       /* If we're a function-scope tag, initially use a parent of NULL;
15663          this will be fixed up in decls_for_scope.  */
15664       if (decl_function_context (decl))
15665         context_die = NULL;
15666
15667       break;
15668
15669     default:
15670       return;
15671     }
15672
15673   gen_decl_die (decl, context_die);
15674 }
15675
15676 /* Output a marker (i.e. a label) for the beginning of the generated code for
15677    a lexical block.  */
15678
15679 static void
15680 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
15681                        unsigned int blocknum)
15682 {
15683   switch_to_section (current_function_section ());
15684   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
15685 }
15686
15687 /* Output a marker (i.e. a label) for the end of the generated code for a
15688    lexical block.  */
15689
15690 static void
15691 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
15692 {
15693   switch_to_section (current_function_section ());
15694   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
15695 }
15696
15697 /* Returns nonzero if it is appropriate not to emit any debugging
15698    information for BLOCK, because it doesn't contain any instructions.
15699
15700    Don't allow this for blocks with nested functions or local classes
15701    as we would end up with orphans, and in the presence of scheduling
15702    we may end up calling them anyway.  */
15703
15704 static bool
15705 dwarf2out_ignore_block (const_tree block)
15706 {
15707   tree decl;
15708
15709   for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
15710     if (TREE_CODE (decl) == FUNCTION_DECL
15711         || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
15712       return 0;
15713
15714   return 1;
15715 }
15716
15717 /* Hash table routines for file_hash.  */
15718
15719 static int
15720 file_table_eq (const void *p1_p, const void *p2_p)
15721 {
15722   const struct dwarf_file_data *const p1 =
15723     (const struct dwarf_file_data *) p1_p;
15724   const char *const p2 = (const char *) p2_p;
15725   return strcmp (p1->filename, p2) == 0;
15726 }
15727
15728 static hashval_t
15729 file_table_hash (const void *p_p)
15730 {
15731   const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
15732   return htab_hash_string (p->filename);
15733 }
15734
15735 /* Lookup FILE_NAME (in the list of filenames that we know about here in
15736    dwarf2out.c) and return its "index".  The index of each (known) filename is
15737    just a unique number which is associated with only that one filename.  We
15738    need such numbers for the sake of generating labels (in the .debug_sfnames
15739    section) and references to those files numbers (in the .debug_srcinfo
15740    and.debug_macinfo sections).  If the filename given as an argument is not
15741    found in our current list, add it to the list and assign it the next
15742    available unique index number.  In order to speed up searches, we remember
15743    the index of the filename was looked up last.  This handles the majority of
15744    all searches.  */
15745
15746 static struct dwarf_file_data *
15747 lookup_filename (const char *file_name)
15748 {
15749   void ** slot;
15750   struct dwarf_file_data * created;
15751
15752   /* Check to see if the file name that was searched on the previous
15753      call matches this file name.  If so, return the index.  */
15754   if (file_table_last_lookup
15755       && (file_name == file_table_last_lookup->filename
15756           || strcmp (file_table_last_lookup->filename, file_name) == 0))
15757     return file_table_last_lookup;
15758
15759   /* Didn't match the previous lookup, search the table.  */
15760   slot = htab_find_slot_with_hash (file_table, file_name,
15761                                    htab_hash_string (file_name), INSERT);
15762   if (*slot)
15763     return (struct dwarf_file_data *) *slot;
15764
15765   created = GGC_NEW (struct dwarf_file_data);
15766   created->filename = file_name;
15767   created->emitted_number = 0;
15768   *slot = created;
15769   return created;
15770 }
15771
15772 /* If the assembler will construct the file table, then translate the compiler
15773    internal file table number into the assembler file table number, and emit
15774    a .file directive if we haven't already emitted one yet.  The file table
15775    numbers are different because we prune debug info for unused variables and
15776    types, which may include filenames.  */
15777
15778 static int
15779 maybe_emit_file (struct dwarf_file_data * fd)
15780 {
15781   if (! fd->emitted_number)
15782     {
15783       if (last_emitted_file)
15784         fd->emitted_number = last_emitted_file->emitted_number + 1;
15785       else
15786         fd->emitted_number = 1;
15787       last_emitted_file = fd;
15788
15789       if (DWARF2_ASM_LINE_DEBUG_INFO)
15790         {
15791           fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
15792           output_quoted_string (asm_out_file,
15793                                 remap_debug_filename (fd->filename));
15794           fputc ('\n', asm_out_file);
15795         }
15796     }
15797
15798   return fd->emitted_number;
15799 }
15800
15801 /* Called by the final INSN scan whenever we see a var location.  We
15802    use it to drop labels in the right places, and throw the location in
15803    our lookup table.  */
15804
15805 static void
15806 dwarf2out_var_location (rtx loc_note)
15807 {
15808   char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
15809   struct var_loc_node *newloc;
15810   rtx prev_insn;
15811   static rtx last_insn;
15812   static const char *last_label;
15813   tree decl;
15814
15815   if (!DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
15816     return;
15817   prev_insn = PREV_INSN (loc_note);
15818
15819   newloc = GGC_CNEW (struct var_loc_node);
15820   /* If the insn we processed last time is the previous insn
15821      and it is also a var location note, use the label we emitted
15822      last time.  */
15823   if (last_insn != NULL_RTX
15824       && last_insn == prev_insn
15825       && NOTE_P (prev_insn)
15826       && NOTE_KIND (prev_insn) == NOTE_INSN_VAR_LOCATION)
15827     {
15828       newloc->label = last_label;
15829     }
15830   else
15831     {
15832       ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
15833       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
15834       loclabel_num++;
15835       newloc->label = ggc_strdup (loclabel);
15836     }
15837   newloc->var_loc_note = loc_note;
15838   newloc->next = NULL;
15839
15840   if (cfun && in_cold_section_p)
15841     newloc->section_label = crtl->subsections.cold_section_label;
15842   else
15843     newloc->section_label = text_section_label;
15844
15845   last_insn = loc_note;
15846   last_label = newloc->label;
15847   decl = NOTE_VAR_LOCATION_DECL (loc_note);
15848   add_var_loc_to_decl (decl, newloc);
15849 }
15850
15851 /* We need to reset the locations at the beginning of each
15852    function. We can't do this in the end_function hook, because the
15853    declarations that use the locations won't have been output when
15854    that hook is called.  Also compute have_multiple_function_sections here.  */
15855
15856 static void
15857 dwarf2out_begin_function (tree fun)
15858 {
15859   htab_empty (decl_loc_table);
15860
15861   if (function_section (fun) != text_section)
15862     have_multiple_function_sections = true;
15863
15864   dwarf2out_note_section_used ();
15865 }
15866
15867 /* Output a label to mark the beginning of a source code line entry
15868    and record information relating to this source line, in
15869    'line_info_table' for later output of the .debug_line section.  */
15870
15871 static void
15872 dwarf2out_source_line (unsigned int line, const char *filename)
15873 {
15874   if (debug_info_level >= DINFO_LEVEL_NORMAL
15875       && line != 0)
15876     {
15877       int file_num = maybe_emit_file (lookup_filename (filename));
15878
15879       switch_to_section (current_function_section ());
15880
15881       /* If requested, emit something human-readable.  */
15882       if (flag_debug_asm)
15883         fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
15884                  filename, line);
15885
15886       if (DWARF2_ASM_LINE_DEBUG_INFO)
15887         {
15888           /* Emit the .loc directive understood by GNU as.  */
15889           fprintf (asm_out_file, "\t.loc %d %d 0\n", file_num, line);
15890
15891           /* Indicate that line number info exists.  */
15892           line_info_table_in_use++;
15893         }
15894       else if (function_section (current_function_decl) != text_section)
15895         {
15896           dw_separate_line_info_ref line_info;
15897           targetm.asm_out.internal_label (asm_out_file,
15898                                           SEPARATE_LINE_CODE_LABEL,
15899                                           separate_line_info_table_in_use);
15900
15901           /* Expand the line info table if necessary.  */
15902           if (separate_line_info_table_in_use
15903               == separate_line_info_table_allocated)
15904             {
15905               separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
15906               separate_line_info_table
15907                 = GGC_RESIZEVEC (dw_separate_line_info_entry,
15908                                  separate_line_info_table,
15909                                  separate_line_info_table_allocated);
15910               memset (separate_line_info_table
15911                        + separate_line_info_table_in_use,
15912                       0,
15913                       (LINE_INFO_TABLE_INCREMENT
15914                        * sizeof (dw_separate_line_info_entry)));
15915             }
15916
15917           /* Add the new entry at the end of the line_info_table.  */
15918           line_info
15919             = &separate_line_info_table[separate_line_info_table_in_use++];
15920           line_info->dw_file_num = file_num;
15921           line_info->dw_line_num = line;
15922           line_info->function = current_function_funcdef_no;
15923         }
15924       else
15925         {
15926           dw_line_info_ref line_info;
15927
15928           targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL,
15929                                      line_info_table_in_use);
15930
15931           /* Expand the line info table if necessary.  */
15932           if (line_info_table_in_use == line_info_table_allocated)
15933             {
15934               line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
15935               line_info_table
15936                 = GGC_RESIZEVEC (dw_line_info_entry, line_info_table,
15937                                  line_info_table_allocated);
15938               memset (line_info_table + line_info_table_in_use, 0,
15939                       LINE_INFO_TABLE_INCREMENT * sizeof (dw_line_info_entry));
15940             }
15941
15942           /* Add the new entry at the end of the line_info_table.  */
15943           line_info = &line_info_table[line_info_table_in_use++];
15944           line_info->dw_file_num = file_num;
15945           line_info->dw_line_num = line;
15946         }
15947     }
15948 }
15949
15950 /* Record the beginning of a new source file.  */
15951
15952 static void
15953 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
15954 {
15955   if (flag_eliminate_dwarf2_dups)
15956     {
15957       /* Record the beginning of the file for break_out_includes.  */
15958       dw_die_ref bincl_die;
15959
15960       bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die, NULL);
15961       add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
15962     }
15963
15964   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
15965     {
15966       int file_num = maybe_emit_file (lookup_filename (filename));
15967
15968       switch_to_section (debug_macinfo_section);
15969       dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
15970       dw2_asm_output_data_uleb128 (lineno, "Included from line number %d",
15971                                    lineno);
15972
15973       dw2_asm_output_data_uleb128 (file_num, "file %s", filename);
15974     }
15975 }
15976
15977 /* Record the end of a source file.  */
15978
15979 static void
15980 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
15981 {
15982   if (flag_eliminate_dwarf2_dups)
15983     /* Record the end of the file for break_out_includes.  */
15984     new_die (DW_TAG_GNU_EINCL, comp_unit_die, NULL);
15985
15986   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
15987     {
15988       switch_to_section (debug_macinfo_section);
15989       dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
15990     }
15991 }
15992
15993 /* Called from debug_define in toplev.c.  The `buffer' parameter contains
15994    the tail part of the directive line, i.e. the part which is past the
15995    initial whitespace, #, whitespace, directive-name, whitespace part.  */
15996
15997 static void
15998 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
15999                   const char *buffer ATTRIBUTE_UNUSED)
16000 {
16001   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
16002     {
16003       switch_to_section (debug_macinfo_section);
16004       dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
16005       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
16006       dw2_asm_output_nstring (buffer, -1, "The macro");
16007     }
16008 }
16009
16010 /* Called from debug_undef in toplev.c.  The `buffer' parameter contains
16011    the tail part of the directive line, i.e. the part which is past the
16012    initial whitespace, #, whitespace, directive-name, whitespace part.  */
16013
16014 static void
16015 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
16016                  const char *buffer ATTRIBUTE_UNUSED)
16017 {
16018   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
16019     {
16020       switch_to_section (debug_macinfo_section);
16021       dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
16022       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
16023       dw2_asm_output_nstring (buffer, -1, "The macro");
16024     }
16025 }
16026
16027 /* Set up for Dwarf output at the start of compilation.  */
16028
16029 static void
16030 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
16031 {
16032   /* Allocate the file_table.  */
16033   file_table = htab_create_ggc (50, file_table_hash,
16034                                 file_table_eq, NULL);
16035
16036   /* Allocate the decl_die_table.  */
16037   decl_die_table = htab_create_ggc (10, decl_die_table_hash,
16038                                     decl_die_table_eq, NULL);
16039
16040   /* Allocate the decl_loc_table.  */
16041   decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
16042                                     decl_loc_table_eq, NULL);
16043
16044   /* Allocate the initial hunk of the decl_scope_table.  */
16045   decl_scope_table = VEC_alloc (tree, gc, 256);
16046
16047   /* Allocate the initial hunk of the abbrev_die_table.  */
16048   abbrev_die_table = GGC_CNEWVEC (dw_die_ref, ABBREV_DIE_TABLE_INCREMENT);
16049   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
16050   /* Zero-th entry is allocated, but unused.  */
16051   abbrev_die_table_in_use = 1;
16052
16053   /* Allocate the initial hunk of the line_info_table.  */
16054   line_info_table = GGC_CNEWVEC (dw_line_info_entry, LINE_INFO_TABLE_INCREMENT);
16055   line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
16056
16057   /* Zero-th entry is allocated, but unused.  */
16058   line_info_table_in_use = 1;
16059
16060   /* Allocate the pubtypes and pubnames vectors.  */
16061   pubname_table = VEC_alloc (pubname_entry, gc, 32);
16062   pubtype_table = VEC_alloc (pubname_entry, gc, 32);
16063
16064   /* Generate the initial DIE for the .debug section.  Note that the (string)
16065      value given in the DW_AT_name attribute of the DW_TAG_compile_unit DIE
16066      will (typically) be a relative pathname and that this pathname should be
16067      taken as being relative to the directory from which the compiler was
16068      invoked when the given (base) source file was compiled.  We will fill
16069      in this value in dwarf2out_finish.  */
16070   comp_unit_die = gen_compile_unit_die (NULL);
16071
16072   incomplete_types = VEC_alloc (tree, gc, 64);
16073
16074   used_rtx_array = VEC_alloc (rtx, gc, 32);
16075
16076   debug_info_section = get_section (DEBUG_INFO_SECTION,
16077                                     SECTION_DEBUG, NULL);
16078   debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
16079                                       SECTION_DEBUG, NULL);
16080   debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
16081                                        SECTION_DEBUG, NULL);
16082   debug_macinfo_section = get_section (DEBUG_MACINFO_SECTION,
16083                                        SECTION_DEBUG, NULL);
16084   debug_line_section = get_section (DEBUG_LINE_SECTION,
16085                                     SECTION_DEBUG, NULL);
16086   debug_loc_section = get_section (DEBUG_LOC_SECTION,
16087                                    SECTION_DEBUG, NULL);
16088   debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
16089                                         SECTION_DEBUG, NULL);
16090 #ifdef DEBUG_PUBTYPES_SECTION
16091   debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
16092                                         SECTION_DEBUG, NULL);
16093 #endif
16094   debug_str_section = get_section (DEBUG_STR_SECTION,
16095                                    DEBUG_STR_SECTION_FLAGS, NULL);
16096   debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
16097                                       SECTION_DEBUG, NULL);
16098   debug_frame_section = get_section (DEBUG_FRAME_SECTION,
16099                                      SECTION_DEBUG, NULL);
16100
16101   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
16102   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
16103                                DEBUG_ABBREV_SECTION_LABEL, 0);
16104   ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
16105   ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
16106                                COLD_TEXT_SECTION_LABEL, 0);
16107   ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
16108
16109   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
16110                                DEBUG_INFO_SECTION_LABEL, 0);
16111   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
16112                                DEBUG_LINE_SECTION_LABEL, 0);
16113   ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
16114                                DEBUG_RANGES_SECTION_LABEL, 0);
16115   switch_to_section (debug_abbrev_section);
16116   ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
16117   switch_to_section (debug_info_section);
16118   ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
16119   switch_to_section (debug_line_section);
16120   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
16121
16122   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
16123     {
16124       switch_to_section (debug_macinfo_section);
16125       ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
16126                                    DEBUG_MACINFO_SECTION_LABEL, 0);
16127       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
16128     }
16129
16130   switch_to_section (text_section);
16131   ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
16132   if (flag_reorder_blocks_and_partition)
16133     {
16134       cold_text_section = unlikely_text_section ();
16135       switch_to_section (cold_text_section);
16136       ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
16137     }
16138 }
16139
16140 /* A helper function for dwarf2out_finish called through
16141    ht_forall.  Emit one queued .debug_str string.  */
16142
16143 static int
16144 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
16145 {
16146   struct indirect_string_node *node = (struct indirect_string_node *) *h;
16147
16148   if (node->form == DW_FORM_strp)
16149     {
16150       switch_to_section (debug_str_section);
16151       ASM_OUTPUT_LABEL (asm_out_file, node->label);
16152       assemble_string (node->str, strlen (node->str) + 1);
16153     }
16154
16155   return 1;
16156 }
16157
16158 #if ENABLE_ASSERT_CHECKING
16159 /* Verify that all marks are clear.  */
16160
16161 static void
16162 verify_marks_clear (dw_die_ref die)
16163 {
16164   dw_die_ref c;
16165
16166   gcc_assert (! die->die_mark);
16167   FOR_EACH_CHILD (die, c, verify_marks_clear (c));
16168 }
16169 #endif /* ENABLE_ASSERT_CHECKING */
16170
16171 /* Clear the marks for a die and its children.
16172    Be cool if the mark isn't set.  */
16173
16174 static void
16175 prune_unmark_dies (dw_die_ref die)
16176 {
16177   dw_die_ref c;
16178
16179   if (die->die_mark)
16180     die->die_mark = 0;
16181   FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
16182 }
16183
16184 /* Given DIE that we're marking as used, find any other dies
16185    it references as attributes and mark them as used.  */
16186
16187 static void
16188 prune_unused_types_walk_attribs (dw_die_ref die)
16189 {
16190   dw_attr_ref a;
16191   unsigned ix;
16192
16193   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
16194     {
16195       if (a->dw_attr_val.val_class == dw_val_class_die_ref)
16196         {
16197           /* A reference to another DIE.
16198              Make sure that it will get emitted.  */
16199           prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
16200         }
16201       /* Set the string's refcount to 0 so that prune_unused_types_mark
16202          accounts properly for it.  */
16203       if (AT_class (a) == dw_val_class_str)
16204         a->dw_attr_val.v.val_str->refcount = 0;
16205     }
16206 }
16207
16208
16209 /* Mark DIE as being used.  If DOKIDS is true, then walk down
16210    to DIE's children.  */
16211
16212 static void
16213 prune_unused_types_mark (dw_die_ref die, int dokids)
16214 {
16215   dw_die_ref c;
16216
16217   if (die->die_mark == 0)
16218     {
16219       /* We haven't done this node yet.  Mark it as used.  */
16220       die->die_mark = 1;
16221
16222       /* We also have to mark its parents as used.
16223          (But we don't want to mark our parents' kids due to this.)  */
16224       if (die->die_parent)
16225         prune_unused_types_mark (die->die_parent, 0);
16226
16227       /* Mark any referenced nodes.  */
16228       prune_unused_types_walk_attribs (die);
16229
16230       /* If this node is a specification,
16231          also mark the definition, if it exists.  */
16232       if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
16233         prune_unused_types_mark (die->die_definition, 1);
16234     }
16235
16236   if (dokids && die->die_mark != 2)
16237     {
16238       /* We need to walk the children, but haven't done so yet.
16239          Remember that we've walked the kids.  */
16240       die->die_mark = 2;
16241
16242       /* If this is an array type, we need to make sure our
16243          kids get marked, even if they're types.  */
16244       if (die->die_tag == DW_TAG_array_type)
16245         FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
16246       else
16247         FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
16248     }
16249 }
16250
16251
16252 /* Walk the tree DIE and mark types that we actually use.  */
16253
16254 static void
16255 prune_unused_types_walk (dw_die_ref die)
16256 {
16257   dw_die_ref c;
16258
16259   /* Don't do anything if this node is already marked.  */
16260   if (die->die_mark)
16261     return;
16262
16263   switch (die->die_tag)
16264     {
16265     case DW_TAG_const_type:
16266     case DW_TAG_packed_type:
16267     case DW_TAG_pointer_type:
16268     case DW_TAG_reference_type:
16269     case DW_TAG_volatile_type:
16270     case DW_TAG_typedef:
16271     case DW_TAG_array_type:
16272     case DW_TAG_structure_type:
16273     case DW_TAG_union_type:
16274     case DW_TAG_class_type:
16275     case DW_TAG_interface_type:
16276     case DW_TAG_friend:
16277     case DW_TAG_variant_part:
16278     case DW_TAG_enumeration_type:
16279     case DW_TAG_subroutine_type:
16280     case DW_TAG_string_type:
16281     case DW_TAG_set_type:
16282     case DW_TAG_subrange_type:
16283     case DW_TAG_ptr_to_member_type:
16284     case DW_TAG_file_type:
16285       if (die->die_perennial_p)
16286         break;
16287
16288       /* It's a type node --- don't mark it.  */
16289       return;
16290
16291     default:
16292       /* Mark everything else.  */
16293       break;
16294   }
16295
16296   die->die_mark = 1;
16297
16298   /* Now, mark any dies referenced from here.  */
16299   prune_unused_types_walk_attribs (die);
16300
16301   /* Mark children.  */
16302   FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
16303 }
16304
16305 /* Increment the string counts on strings referred to from DIE's
16306    attributes.  */
16307
16308 static void
16309 prune_unused_types_update_strings (dw_die_ref die)
16310 {
16311   dw_attr_ref a;
16312   unsigned ix;
16313
16314   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
16315     if (AT_class (a) == dw_val_class_str)
16316       {
16317         struct indirect_string_node *s = a->dw_attr_val.v.val_str;
16318         s->refcount++;
16319         /* Avoid unnecessarily putting strings that are used less than
16320            twice in the hash table.  */
16321         if (s->refcount
16322             == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
16323           {
16324             void ** slot;
16325             slot = htab_find_slot_with_hash (debug_str_hash, s->str,
16326                                              htab_hash_string (s->str),
16327                                              INSERT);
16328             gcc_assert (*slot == NULL);
16329             *slot = s;
16330           }
16331       }
16332 }
16333
16334 /* Remove from the tree DIE any dies that aren't marked.  */
16335
16336 static void
16337 prune_unused_types_prune (dw_die_ref die)
16338 {
16339   dw_die_ref c;
16340
16341   gcc_assert (die->die_mark);
16342   prune_unused_types_update_strings (die);
16343
16344   if (! die->die_child)
16345     return;
16346
16347   c = die->die_child;
16348   do {
16349     dw_die_ref prev = c;
16350     for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
16351       if (c == die->die_child)
16352         {
16353           /* No marked children between 'prev' and the end of the list.  */
16354           if (prev == c)
16355             /* No marked children at all.  */
16356             die->die_child = NULL;
16357           else
16358             {
16359               prev->die_sib = c->die_sib;
16360               die->die_child = prev;
16361             }
16362           return;
16363         }
16364
16365     if (c != prev->die_sib)
16366       prev->die_sib = c;
16367     prune_unused_types_prune (c);
16368   } while (c != die->die_child);
16369 }
16370
16371
16372 /* Remove dies representing declarations that we never use.  */
16373
16374 static void
16375 prune_unused_types (void)
16376 {
16377   unsigned int i;
16378   limbo_die_node *node;
16379   pubname_ref pub;
16380
16381 #if ENABLE_ASSERT_CHECKING
16382   /* All the marks should already be clear.  */
16383   verify_marks_clear (comp_unit_die);
16384   for (node = limbo_die_list; node; node = node->next)
16385     verify_marks_clear (node->die);
16386 #endif /* ENABLE_ASSERT_CHECKING */
16387
16388   /* Set the mark on nodes that are actually used.  */
16389   prune_unused_types_walk (comp_unit_die);
16390   for (node = limbo_die_list; node; node = node->next)
16391     prune_unused_types_walk (node->die);
16392
16393   /* Also set the mark on nodes referenced from the
16394      pubname_table or arange_table.  */
16395   for (i = 0; VEC_iterate (pubname_entry, pubname_table, i, pub); i++)
16396     prune_unused_types_mark (pub->die, 1);
16397   for (i = 0; i < arange_table_in_use; i++)
16398     prune_unused_types_mark (arange_table[i], 1);
16399
16400   /* Get rid of nodes that aren't marked; and update the string counts.  */
16401   if (debug_str_hash)
16402     htab_empty (debug_str_hash);
16403   prune_unused_types_prune (comp_unit_die);
16404   for (node = limbo_die_list; node; node = node->next)
16405     prune_unused_types_prune (node->die);
16406
16407   /* Leave the marks clear.  */
16408   prune_unmark_dies (comp_unit_die);
16409   for (node = limbo_die_list; node; node = node->next)
16410     prune_unmark_dies (node->die);
16411 }
16412
16413 /* Set the parameter to true if there are any relative pathnames in
16414    the file table.  */
16415 static int
16416 file_table_relative_p (void ** slot, void *param)
16417 {
16418   bool *p = (bool *) param;
16419   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
16420   if (!IS_ABSOLUTE_PATH (d->filename))
16421     {
16422       *p = true;
16423       return 0;
16424     }
16425   return 1;
16426 }
16427
16428 /* Output stuff that dwarf requires at the end of every file,
16429    and generate the DWARF-2 debugging info.  */
16430
16431 static void
16432 dwarf2out_finish (const char *filename)
16433 {
16434   limbo_die_node *node, *next_node;
16435   dw_die_ref die = 0;
16436
16437   /* Add the name for the main input file now.  We delayed this from
16438      dwarf2out_init to avoid complications with PCH.  */
16439   add_name_attribute (comp_unit_die, remap_debug_filename (filename));
16440   if (!IS_ABSOLUTE_PATH (filename))
16441     add_comp_dir_attribute (comp_unit_die);
16442   else if (get_AT (comp_unit_die, DW_AT_comp_dir) == NULL)
16443     {
16444       bool p = false;
16445       htab_traverse (file_table, file_table_relative_p, &p);
16446       if (p)
16447         add_comp_dir_attribute (comp_unit_die);
16448     }
16449
16450   /* Traverse the limbo die list, and add parent/child links.  The only
16451      dies without parents that should be here are concrete instances of
16452      inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
16453      For concrete instances, we can get the parent die from the abstract
16454      instance.  */
16455   for (node = limbo_die_list; node; node = next_node)
16456     {
16457       next_node = node->next;
16458       die = node->die;
16459
16460       if (die->die_parent == NULL)
16461         {
16462           dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
16463
16464           if (origin)
16465             add_child_die (origin->die_parent, die);
16466           else if (die == comp_unit_die)
16467             ;
16468           else if (errorcount > 0 || sorrycount > 0)
16469             /* It's OK to be confused by errors in the input.  */
16470             add_child_die (comp_unit_die, die);
16471           else
16472             {
16473               /* In certain situations, the lexical block containing a
16474                  nested function can be optimized away, which results
16475                  in the nested function die being orphaned.  Likewise
16476                  with the return type of that nested function.  Force
16477                  this to be a child of the containing function.
16478
16479                  It may happen that even the containing function got fully
16480                  inlined and optimized out.  In that case we are lost and
16481                  assign the empty child.  This should not be big issue as
16482                  the function is likely unreachable too.  */
16483               tree context = NULL_TREE;
16484
16485               gcc_assert (node->created_for);
16486
16487               if (DECL_P (node->created_for))
16488                 context = DECL_CONTEXT (node->created_for);
16489               else if (TYPE_P (node->created_for))
16490                 context = TYPE_CONTEXT (node->created_for);
16491
16492               gcc_assert (context
16493                           && (TREE_CODE (context) == FUNCTION_DECL
16494                               || TREE_CODE (context) == NAMESPACE_DECL));
16495
16496               origin = lookup_decl_die (context);
16497               if (origin)
16498                 add_child_die (origin, die);
16499               else
16500                 add_child_die (comp_unit_die, die);
16501             }
16502         }
16503     }
16504
16505   limbo_die_list = NULL;
16506
16507   /* Walk through the list of incomplete types again, trying once more to
16508      emit full debugging info for them.  */
16509   retry_incomplete_types ();
16510
16511   if (flag_eliminate_unused_debug_types)
16512     prune_unused_types ();
16513
16514   /* Generate separate CUs for each of the include files we've seen.
16515      They will go into limbo_die_list.  */
16516   if (flag_eliminate_dwarf2_dups)
16517     break_out_includes (comp_unit_die);
16518
16519   /* Traverse the DIE's and add add sibling attributes to those DIE's
16520      that have children.  */
16521   add_sibling_attributes (comp_unit_die);
16522   for (node = limbo_die_list; node; node = node->next)
16523     add_sibling_attributes (node->die);
16524
16525   /* Output a terminator label for the .text section.  */
16526   switch_to_section (text_section);
16527   targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
16528   if (flag_reorder_blocks_and_partition)
16529     {
16530       switch_to_section (unlikely_text_section ());
16531       targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
16532     }
16533
16534   /* We can only use the low/high_pc attributes if all of the code was
16535      in .text.  */
16536   if (!have_multiple_function_sections)
16537     {
16538       add_AT_lbl_id (comp_unit_die, DW_AT_low_pc, text_section_label);
16539       add_AT_lbl_id (comp_unit_die, DW_AT_high_pc, text_end_label);
16540     }
16541
16542   else
16543     {
16544       unsigned fde_idx = 0;
16545
16546       /* We need to give .debug_loc and .debug_ranges an appropriate
16547          "base address".  Use zero so that these addresses become
16548          absolute.  Historically, we've emitted the unexpected
16549          DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
16550          Emit both to give time for other tools to adapt.  */
16551       add_AT_addr (comp_unit_die, DW_AT_low_pc, const0_rtx);
16552       add_AT_addr (comp_unit_die, DW_AT_entry_pc, const0_rtx);
16553
16554       add_AT_range_list (comp_unit_die, DW_AT_ranges,
16555                          add_ranges_by_labels (text_section_label,
16556                                                text_end_label));
16557       if (flag_reorder_blocks_and_partition)
16558         add_ranges_by_labels (cold_text_section_label,
16559                               cold_end_label);
16560
16561       for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
16562         {
16563           dw_fde_ref fde = &fde_table[fde_idx];
16564
16565           if (fde->dw_fde_switched_sections)
16566             {
16567               add_ranges_by_labels (fde->dw_fde_hot_section_label,
16568                                     fde->dw_fde_hot_section_end_label);
16569               add_ranges_by_labels (fde->dw_fde_unlikely_section_label,
16570                                     fde->dw_fde_unlikely_section_end_label);
16571             }
16572           else
16573             add_ranges_by_labels (fde->dw_fde_begin,
16574                                   fde->dw_fde_end);
16575         }
16576
16577       add_ranges (NULL);
16578     }
16579
16580   /* Output location list section if necessary.  */
16581   if (have_location_lists)
16582     {
16583       /* Output the location lists info.  */
16584       switch_to_section (debug_loc_section);
16585       ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
16586                                    DEBUG_LOC_SECTION_LABEL, 0);
16587       ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
16588       output_location_lists (die);
16589     }
16590
16591   if (debug_info_level >= DINFO_LEVEL_NORMAL)
16592     add_AT_lineptr (comp_unit_die, DW_AT_stmt_list,
16593                     debug_line_section_label);
16594
16595   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
16596     add_AT_macptr (comp_unit_die, DW_AT_macro_info, macinfo_section_label);
16597
16598   /* Output all of the compilation units.  We put the main one last so that
16599      the offsets are available to output_pubnames.  */
16600   for (node = limbo_die_list; node; node = node->next)
16601     output_comp_unit (node->die, 0);
16602
16603   output_comp_unit (comp_unit_die, 0);
16604
16605   /* Output the abbreviation table.  */
16606   switch_to_section (debug_abbrev_section);
16607   output_abbrev_section ();
16608
16609   /* Output public names table if necessary.  */
16610   if (!VEC_empty (pubname_entry, pubname_table))
16611     {
16612       switch_to_section (debug_pubnames_section);
16613       output_pubnames (pubname_table);
16614     }
16615
16616 #ifdef DEBUG_PUBTYPES_SECTION
16617   /* Output public types table if necessary.  */
16618   if (!VEC_empty (pubname_entry, pubtype_table))
16619     {
16620       switch_to_section (debug_pubtypes_section);
16621       output_pubnames (pubtype_table);
16622     }
16623 #endif
16624
16625   /* Output the address range information.  We only put functions in the arange
16626      table, so don't write it out if we don't have any.  */
16627   if (fde_table_in_use)
16628     {
16629       switch_to_section (debug_aranges_section);
16630       output_aranges ();
16631     }
16632
16633   /* Output ranges section if necessary.  */
16634   if (ranges_table_in_use)
16635     {
16636       switch_to_section (debug_ranges_section);
16637       ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
16638       output_ranges ();
16639     }
16640
16641   /* Output the source line correspondence table.  We must do this
16642      even if there is no line information.  Otherwise, on an empty
16643      translation unit, we will generate a present, but empty,
16644      .debug_info section.  IRIX 6.5 `nm' will then complain when
16645      examining the file.  This is done late so that any filenames
16646      used by the debug_info section are marked as 'used'.  */
16647   if (! DWARF2_ASM_LINE_DEBUG_INFO)
16648     {
16649       switch_to_section (debug_line_section);
16650       output_line_info ();
16651     }
16652
16653   /* Have to end the macro section.  */
16654   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
16655     {
16656       switch_to_section (debug_macinfo_section);
16657       dw2_asm_output_data (1, 0, "End compilation unit");
16658     }
16659
16660   /* If we emitted any DW_FORM_strp form attribute, output the string
16661      table too.  */
16662   if (debug_str_hash)
16663     htab_traverse (debug_str_hash, output_indirect_string, NULL);
16664 }
16665 #else
16666
16667 /* This should never be used, but its address is needed for comparisons.  */
16668 const struct gcc_debug_hooks dwarf2_debug_hooks;
16669
16670 #endif /* DWARF2_DEBUGGING_INFO */
16671
16672 #include "gt-dwarf2out.h"