OSDN Git Service

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