dwarf2out.c (compute_barrier_args_size): Set barrier_args_size for labels for which...
[platform/upstream/gcc.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 /* The size of the target's pointer type.  */
134 #ifndef PTR_SIZE
135 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
136 #endif
137
138 /* Array of RTXes referenced by the debugging information, which therefore
139    must be kept around forever.  */
140 static GTY(()) VEC(rtx,gc) *used_rtx_array;
141
142 /* A pointer to the base of a list of incomplete types which might be
143    completed at some later time.  incomplete_types_list needs to be a
144    VEC(tree,gc) because we want to tell the garbage collector about
145    it.  */
146 static GTY(()) VEC(tree,gc) *incomplete_types;
147
148 /* A pointer to the base of a table of references to declaration
149    scopes.  This table is a display which tracks the nesting
150    of declaration scopes at the current scope and containing
151    scopes.  This table is used to find the proper place to
152    define type declaration DIE's.  */
153 static GTY(()) VEC(tree,gc) *decl_scope_table;
154
155 /* Pointers to various DWARF2 sections.  */
156 static GTY(()) section *debug_info_section;
157 static GTY(()) section *debug_abbrev_section;
158 static GTY(()) section *debug_aranges_section;
159 static GTY(()) section *debug_macinfo_section;
160 static GTY(()) section *debug_line_section;
161 static GTY(()) section *debug_loc_section;
162 static GTY(()) section *debug_pubnames_section;
163 static GTY(()) section *debug_pubtypes_section;
164 static GTY(()) section *debug_str_section;
165 static GTY(()) section *debug_ranges_section;
166 static GTY(()) section *debug_frame_section;
167
168 /* How to start an assembler comment.  */
169 #ifndef ASM_COMMENT_START
170 #define ASM_COMMENT_START ";#"
171 #endif
172
173 typedef struct dw_cfi_struct *dw_cfi_ref;
174 typedef struct dw_fde_struct *dw_fde_ref;
175 typedef union  dw_cfi_oprnd_struct *dw_cfi_oprnd_ref;
176
177 /* Call frames are described using a sequence of Call Frame
178    Information instructions.  The register number, offset
179    and address fields are provided as possible operands;
180    their use is selected by the opcode field.  */
181
182 enum dw_cfi_oprnd_type {
183   dw_cfi_oprnd_unused,
184   dw_cfi_oprnd_reg_num,
185   dw_cfi_oprnd_offset,
186   dw_cfi_oprnd_addr,
187   dw_cfi_oprnd_loc
188 };
189
190 typedef union dw_cfi_oprnd_struct GTY(())
191 {
192   unsigned int GTY ((tag ("dw_cfi_oprnd_reg_num"))) dw_cfi_reg_num;
193   HOST_WIDE_INT GTY ((tag ("dw_cfi_oprnd_offset"))) dw_cfi_offset;
194   const char * GTY ((tag ("dw_cfi_oprnd_addr"))) dw_cfi_addr;
195   struct dw_loc_descr_struct * GTY ((tag ("dw_cfi_oprnd_loc"))) dw_cfi_loc;
196 }
197 dw_cfi_oprnd;
198
199 typedef struct dw_cfi_struct GTY(())
200 {
201   dw_cfi_ref dw_cfi_next;
202   enum dwarf_call_frame_info dw_cfi_opc;
203   dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd1_desc (%1.dw_cfi_opc)")))
204     dw_cfi_oprnd1;
205   dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd2_desc (%1.dw_cfi_opc)")))
206     dw_cfi_oprnd2;
207 }
208 dw_cfi_node;
209
210 /* This is how we define the location of the CFA. We use to handle it
211    as REG + OFFSET all the time,  but now it can be more complex.
212    It can now be either REG + CFA_OFFSET or *(REG + BASE_OFFSET) + CFA_OFFSET.
213    Instead of passing around REG and OFFSET, we pass a copy
214    of this structure.  */
215 typedef struct cfa_loc GTY(())
216 {
217   HOST_WIDE_INT offset;
218   HOST_WIDE_INT base_offset;
219   unsigned int reg;
220   int indirect;            /* 1 if CFA is accessed via a dereference.  */
221 } dw_cfa_location;
222
223 /* All call frame descriptions (FDE's) in the GCC generated DWARF
224    refer to a single Common Information Entry (CIE), defined at
225    the beginning of the .debug_frame section.  This use of a single
226    CIE obviates the need to keep track of multiple CIE's
227    in the DWARF generation routines below.  */
228
229 typedef struct dw_fde_struct GTY(())
230 {
231   tree decl;
232   const char *dw_fde_begin;
233   const char *dw_fde_current_label;
234   const char *dw_fde_end;
235   const char *dw_fde_hot_section_label;
236   const char *dw_fde_hot_section_end_label;
237   const char *dw_fde_unlikely_section_label;
238   const char *dw_fde_unlikely_section_end_label;
239   bool dw_fde_switched_sections;
240   dw_cfi_ref dw_fde_cfi;
241   unsigned funcdef_number;
242   HOST_WIDE_INT stack_realignment;
243   /* Dynamic realign argument pointer register.  */
244   unsigned int drap_reg;
245   /* Virtual dynamic realign argument pointer register.  */
246   unsigned int vdrap_reg;
247   unsigned all_throwers_are_sibcalls : 1;
248   unsigned nothrow : 1;
249   unsigned uses_eh_lsda : 1;
250   /* Whether we did stack realign in this call frame.  */
251   unsigned stack_realign : 1;
252   /* Whether dynamic realign argument pointer register has been saved.  */
253   unsigned drap_reg_saved: 1;
254 }
255 dw_fde_node;
256
257 /* Maximum size (in bytes) of an artificially generated label.  */
258 #define MAX_ARTIFICIAL_LABEL_BYTES      30
259
260 /* The size of addresses as they appear in the Dwarf 2 data.
261    Some architectures use word addresses to refer to code locations,
262    but Dwarf 2 info always uses byte addresses.  On such machines,
263    Dwarf 2 addresses need to be larger than the architecture's
264    pointers.  */
265 #ifndef DWARF2_ADDR_SIZE
266 #define DWARF2_ADDR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
267 #endif
268
269 /* The size in bytes of a DWARF field indicating an offset or length
270    relative to a debug info section, specified to be 4 bytes in the
271    DWARF-2 specification.  The SGI/MIPS ABI defines it to be the same
272    as PTR_SIZE.  */
273
274 #ifndef DWARF_OFFSET_SIZE
275 #define DWARF_OFFSET_SIZE 4
276 #endif
277
278 /* According to the (draft) DWARF 3 specification, the initial length
279    should either be 4 or 12 bytes.  When it's 12 bytes, the first 4
280    bytes are 0xffffffff, followed by the length stored in the next 8
281    bytes.
282
283    However, the SGI/MIPS ABI uses an initial length which is equal to
284    DWARF_OFFSET_SIZE.  It is defined (elsewhere) accordingly.  */
285
286 #ifndef DWARF_INITIAL_LENGTH_SIZE
287 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
288 #endif
289
290 #define DWARF_VERSION 2
291
292 /* Round SIZE up to the nearest BOUNDARY.  */
293 #define DWARF_ROUND(SIZE,BOUNDARY) \
294   ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
295
296 /* Offsets recorded in opcodes are a multiple of this alignment factor.  */
297 #ifndef DWARF_CIE_DATA_ALIGNMENT
298 #ifdef STACK_GROWS_DOWNWARD
299 #define DWARF_CIE_DATA_ALIGNMENT (-((int) UNITS_PER_WORD))
300 #else
301 #define DWARF_CIE_DATA_ALIGNMENT ((int) UNITS_PER_WORD)
302 #endif
303 #endif
304
305 /* CIE identifier.  */
306 #if HOST_BITS_PER_WIDE_INT >= 64
307 #define DWARF_CIE_ID \
308   (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
309 #else
310 #define DWARF_CIE_ID DW_CIE_ID
311 #endif
312
313 /* A pointer to the base of a table that contains frame description
314    information for each routine.  */
315 static GTY((length ("fde_table_allocated"))) dw_fde_ref fde_table;
316
317 /* Number of elements currently allocated for fde_table.  */
318 static GTY(()) unsigned fde_table_allocated;
319
320 /* Number of elements in fde_table currently in use.  */
321 static GTY(()) unsigned fde_table_in_use;
322
323 /* Size (in elements) of increments by which we may expand the
324    fde_table.  */
325 #define FDE_TABLE_INCREMENT 256
326
327 /* Get the current fde_table entry we should use.  */
328
329 static inline dw_fde_ref
330 current_fde (void)
331 {
332   return fde_table_in_use ? &fde_table[fde_table_in_use - 1] : NULL;
333 }
334
335 /* A list of call frame insns for the CIE.  */
336 static GTY(()) dw_cfi_ref cie_cfi_head;
337
338 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
339 /* Some DWARF extensions (e.g., MIPS/SGI) implement a subprogram
340    attribute that accelerates the lookup of the FDE associated
341    with the subprogram.  This variable holds the table index of the FDE
342    associated with the current function (body) definition.  */
343 static unsigned current_funcdef_fde;
344 #endif
345
346 struct indirect_string_node GTY(())
347 {
348   const char *str;
349   unsigned int refcount;
350   unsigned int form;
351   char *label;
352 };
353
354 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
355
356 static GTY(()) int dw2_string_counter;
357 static GTY(()) unsigned long dwarf2out_cfi_label_num;
358
359 /* True if the compilation unit places functions in more than one section.  */
360 static GTY(()) bool have_multiple_function_sections = false;
361
362 /* Whether the default text and cold text sections have been used at all.  */
363
364 static GTY(()) bool text_section_used = false;
365 static GTY(()) bool cold_text_section_used = false;
366
367 /* The default cold text section.  */
368 static GTY(()) section *cold_text_section;
369
370 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
371
372 /* Forward declarations for functions defined in this file.  */
373
374 static char *stripattributes (const char *);
375 static const char *dwarf_cfi_name (unsigned);
376 static dw_cfi_ref new_cfi (void);
377 static void add_cfi (dw_cfi_ref *, dw_cfi_ref);
378 static void add_fde_cfi (const char *, dw_cfi_ref);
379 static void lookup_cfa_1 (dw_cfi_ref, dw_cfa_location *);
380 static void lookup_cfa (dw_cfa_location *);
381 static void reg_save (const char *, unsigned, unsigned, HOST_WIDE_INT);
382 #ifdef DWARF2_UNWIND_INFO
383 static void initial_return_save (rtx);
384 #endif
385 static HOST_WIDE_INT stack_adjust_offset (const_rtx);
386 static void output_cfi (dw_cfi_ref, dw_fde_ref, int);
387 static void output_call_frame_info (int);
388 static void dwarf2out_note_section_used (void);
389 static void dwarf2out_stack_adjust (rtx, bool);
390 static void dwarf2out_args_size_adjust (HOST_WIDE_INT, const char *);
391 static void flush_queued_reg_saves (void);
392 static bool clobbers_queued_reg_save (const_rtx);
393 static void dwarf2out_frame_debug_expr (rtx, const char *);
394
395 /* Support for complex CFA locations.  */
396 static void output_cfa_loc (dw_cfi_ref);
397 static void get_cfa_from_loc_descr (dw_cfa_location *,
398                                     struct dw_loc_descr_struct *);
399 static struct dw_loc_descr_struct *build_cfa_loc
400   (dw_cfa_location *, HOST_WIDE_INT);
401 static struct dw_loc_descr_struct *build_cfa_aligned_loc
402   (HOST_WIDE_INT, HOST_WIDE_INT);
403 static void def_cfa_1 (const char *, dw_cfa_location *);
404
405 /* How to start an assembler comment.  */
406 #ifndef ASM_COMMENT_START
407 #define ASM_COMMENT_START ";#"
408 #endif
409
410 /* Data and reference forms for relocatable data.  */
411 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
412 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
413
414 #ifndef DEBUG_FRAME_SECTION
415 #define DEBUG_FRAME_SECTION     ".debug_frame"
416 #endif
417
418 #ifndef FUNC_BEGIN_LABEL
419 #define FUNC_BEGIN_LABEL        "LFB"
420 #endif
421
422 #ifndef FUNC_END_LABEL
423 #define FUNC_END_LABEL          "LFE"
424 #endif
425
426 #ifndef FRAME_BEGIN_LABEL
427 #define FRAME_BEGIN_LABEL       "Lframe"
428 #endif
429 #define CIE_AFTER_SIZE_LABEL    "LSCIE"
430 #define CIE_END_LABEL           "LECIE"
431 #define FDE_LABEL               "LSFDE"
432 #define FDE_AFTER_SIZE_LABEL    "LASFDE"
433 #define FDE_END_LABEL           "LEFDE"
434 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
435 #define LINE_NUMBER_END_LABEL   "LELT"
436 #define LN_PROLOG_AS_LABEL      "LASLTP"
437 #define LN_PROLOG_END_LABEL     "LELTP"
438 #define DIE_LABEL_PREFIX        "DW"
439
440 /* The DWARF 2 CFA column which tracks the return address.  Normally this
441    is the column for PC, or the first column after all of the hard
442    registers.  */
443 #ifndef DWARF_FRAME_RETURN_COLUMN
444 #ifdef PC_REGNUM
445 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGNUM (PC_REGNUM)
446 #else
447 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGISTERS
448 #endif
449 #endif
450
451 /* The mapping from gcc register number to DWARF 2 CFA column number.  By
452    default, we just provide columns for all registers.  */
453 #ifndef DWARF_FRAME_REGNUM
454 #define DWARF_FRAME_REGNUM(REG) DBX_REGISTER_NUMBER (REG)
455 #endif
456 \f
457 /* Hook used by __throw.  */
458
459 rtx
460 expand_builtin_dwarf_sp_column (void)
461 {
462   unsigned int dwarf_regnum = DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM);
463   return GEN_INT (DWARF2_FRAME_REG_OUT (dwarf_regnum, 1));
464 }
465
466 /* Return a pointer to a copy of the section string name S with all
467    attributes stripped off, and an asterisk prepended (for assemble_name).  */
468
469 static inline char *
470 stripattributes (const char *s)
471 {
472   char *stripped = XNEWVEC (char, strlen (s) + 2);
473   char *p = stripped;
474
475   *p++ = '*';
476
477   while (*s && *s != ',')
478     *p++ = *s++;
479
480   *p = '\0';
481   return stripped;
482 }
483
484 /* MEM is a memory reference for the register size table, each element of
485    which has mode MODE.  Initialize column C as a return address column.  */
486
487 static void
488 init_return_column_size (enum machine_mode mode, rtx mem, unsigned int c)
489 {
490   HOST_WIDE_INT offset = c * GET_MODE_SIZE (mode);
491   HOST_WIDE_INT size = GET_MODE_SIZE (Pmode);
492   emit_move_insn (adjust_address (mem, mode, offset), GEN_INT (size));
493 }
494
495 /* Generate code to initialize the register size table.  */
496
497 void
498 expand_builtin_init_dwarf_reg_sizes (tree address)
499 {
500   unsigned int i;
501   enum machine_mode mode = TYPE_MODE (char_type_node);
502   rtx addr = expand_normal (address);
503   rtx mem = gen_rtx_MEM (BLKmode, addr);
504   bool wrote_return_column = false;
505
506   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
507     {
508       int rnum = DWARF2_FRAME_REG_OUT (DWARF_FRAME_REGNUM (i), 1);
509
510       if (rnum < DWARF_FRAME_REGISTERS)
511         {
512           HOST_WIDE_INT offset = rnum * GET_MODE_SIZE (mode);
513           enum machine_mode save_mode = reg_raw_mode[i];
514           HOST_WIDE_INT size;
515
516           if (HARD_REGNO_CALL_PART_CLOBBERED (i, save_mode))
517             save_mode = choose_hard_reg_mode (i, 1, true);
518           if (DWARF_FRAME_REGNUM (i) == DWARF_FRAME_RETURN_COLUMN)
519             {
520               if (save_mode == VOIDmode)
521                 continue;
522               wrote_return_column = true;
523             }
524           size = GET_MODE_SIZE (save_mode);
525           if (offset < 0)
526             continue;
527
528           emit_move_insn (adjust_address (mem, mode, offset),
529                           gen_int_mode (size, mode));
530         }
531     }
532
533   if (!wrote_return_column)
534     init_return_column_size (mode, mem, DWARF_FRAME_RETURN_COLUMN);
535
536 #ifdef DWARF_ALT_FRAME_RETURN_COLUMN
537   init_return_column_size (mode, mem, DWARF_ALT_FRAME_RETURN_COLUMN);
538 #endif
539
540   targetm.init_dwarf_reg_sizes_extra (address);
541 }
542
543 /* Convert a DWARF call frame info. operation to its string name */
544
545 static const char *
546 dwarf_cfi_name (unsigned int cfi_opc)
547 {
548   switch (cfi_opc)
549     {
550     case DW_CFA_advance_loc:
551       return "DW_CFA_advance_loc";
552     case DW_CFA_offset:
553       return "DW_CFA_offset";
554     case DW_CFA_restore:
555       return "DW_CFA_restore";
556     case DW_CFA_nop:
557       return "DW_CFA_nop";
558     case DW_CFA_set_loc:
559       return "DW_CFA_set_loc";
560     case DW_CFA_advance_loc1:
561       return "DW_CFA_advance_loc1";
562     case DW_CFA_advance_loc2:
563       return "DW_CFA_advance_loc2";
564     case DW_CFA_advance_loc4:
565       return "DW_CFA_advance_loc4";
566     case DW_CFA_offset_extended:
567       return "DW_CFA_offset_extended";
568     case DW_CFA_restore_extended:
569       return "DW_CFA_restore_extended";
570     case DW_CFA_undefined:
571       return "DW_CFA_undefined";
572     case DW_CFA_same_value:
573       return "DW_CFA_same_value";
574     case DW_CFA_register:
575       return "DW_CFA_register";
576     case DW_CFA_remember_state:
577       return "DW_CFA_remember_state";
578     case DW_CFA_restore_state:
579       return "DW_CFA_restore_state";
580     case DW_CFA_def_cfa:
581       return "DW_CFA_def_cfa";
582     case DW_CFA_def_cfa_register:
583       return "DW_CFA_def_cfa_register";
584     case DW_CFA_def_cfa_offset:
585       return "DW_CFA_def_cfa_offset";
586
587     /* DWARF 3 */
588     case DW_CFA_def_cfa_expression:
589       return "DW_CFA_def_cfa_expression";
590     case DW_CFA_expression:
591       return "DW_CFA_expression";
592     case DW_CFA_offset_extended_sf:
593       return "DW_CFA_offset_extended_sf";
594     case DW_CFA_def_cfa_sf:
595       return "DW_CFA_def_cfa_sf";
596     case DW_CFA_def_cfa_offset_sf:
597       return "DW_CFA_def_cfa_offset_sf";
598
599     /* SGI/MIPS specific */
600     case DW_CFA_MIPS_advance_loc8:
601       return "DW_CFA_MIPS_advance_loc8";
602
603     /* GNU extensions */
604     case DW_CFA_GNU_window_save:
605       return "DW_CFA_GNU_window_save";
606     case DW_CFA_GNU_args_size:
607       return "DW_CFA_GNU_args_size";
608     case DW_CFA_GNU_negative_offset_extended:
609       return "DW_CFA_GNU_negative_offset_extended";
610
611     default:
612       return "DW_CFA_<unknown>";
613     }
614 }
615
616 /* Return a pointer to a newly allocated Call Frame Instruction.  */
617
618 static inline dw_cfi_ref
619 new_cfi (void)
620 {
621   dw_cfi_ref cfi = GGC_NEW (dw_cfi_node);
622
623   cfi->dw_cfi_next = NULL;
624   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = 0;
625   cfi->dw_cfi_oprnd2.dw_cfi_reg_num = 0;
626
627   return cfi;
628 }
629
630 /* Add a Call Frame Instruction to list of instructions.  */
631
632 static inline void
633 add_cfi (dw_cfi_ref *list_head, dw_cfi_ref cfi)
634 {
635   dw_cfi_ref *p;
636   dw_fde_ref fde = current_fde ();
637
638   /* When DRAP is used, CFA is defined with an expression.  Redefine
639      CFA may lead to a different CFA value.   */
640   if (fde && fde->drap_reg != INVALID_REGNUM)
641     switch (cfi->dw_cfi_opc)
642       {
643         case DW_CFA_def_cfa_register:
644         case DW_CFA_def_cfa_offset:
645         case DW_CFA_def_cfa_offset_sf:
646         case DW_CFA_def_cfa:
647         case DW_CFA_def_cfa_sf:
648           gcc_unreachable ();
649
650         default:
651           break;
652       }
653
654   /* Find the end of the chain.  */
655   for (p = list_head; (*p) != NULL; p = &(*p)->dw_cfi_next)
656     ;
657
658   *p = cfi;
659 }
660
661 /* Generate a new label for the CFI info to refer to.  */
662
663 char *
664 dwarf2out_cfi_label (void)
665 {
666   static char label[20];
667
668   ASM_GENERATE_INTERNAL_LABEL (label, "LCFI", dwarf2out_cfi_label_num++);
669   ASM_OUTPUT_LABEL (asm_out_file, label);
670   return label;
671 }
672
673 /* Add CFI to the current fde at the PC value indicated by LABEL if specified,
674    or to the CIE if LABEL is NULL.  */
675
676 static void
677 add_fde_cfi (const char *label, dw_cfi_ref cfi)
678 {
679   if (label)
680     {
681       dw_fde_ref fde = current_fde ();
682
683       gcc_assert (fde != NULL);
684
685       if (*label == 0)
686         label = dwarf2out_cfi_label ();
687
688       if (fde->dw_fde_current_label == NULL
689           || strcmp (label, fde->dw_fde_current_label) != 0)
690         {
691           dw_cfi_ref xcfi;
692
693           label = xstrdup (label);
694
695           /* Set the location counter to the new label.  */
696           xcfi = new_cfi ();
697           /* If we have a current label, advance from there, otherwise
698              set the location directly using set_loc.  */
699           xcfi->dw_cfi_opc = fde->dw_fde_current_label
700                              ? DW_CFA_advance_loc4
701                              : DW_CFA_set_loc;
702           xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
703           add_cfi (&fde->dw_fde_cfi, xcfi);
704
705           fde->dw_fde_current_label = label;
706         }
707
708       add_cfi (&fde->dw_fde_cfi, cfi);
709     }
710
711   else
712     add_cfi (&cie_cfi_head, cfi);
713 }
714
715 /* Subroutine of lookup_cfa.  */
716
717 static void
718 lookup_cfa_1 (dw_cfi_ref cfi, dw_cfa_location *loc)
719 {
720   switch (cfi->dw_cfi_opc)
721     {
722     case DW_CFA_def_cfa_offset:
723       loc->offset = cfi->dw_cfi_oprnd1.dw_cfi_offset;
724       break;
725     case DW_CFA_def_cfa_offset_sf:
726       loc->offset
727         = cfi->dw_cfi_oprnd1.dw_cfi_offset * DWARF_CIE_DATA_ALIGNMENT;
728       break;
729     case DW_CFA_def_cfa_register:
730       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
731       break;
732     case DW_CFA_def_cfa:
733       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
734       loc->offset = cfi->dw_cfi_oprnd2.dw_cfi_offset;
735       break;
736     case DW_CFA_def_cfa_sf:
737       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
738       loc->offset
739         = cfi->dw_cfi_oprnd2.dw_cfi_offset * DWARF_CIE_DATA_ALIGNMENT;
740       break;
741     case DW_CFA_def_cfa_expression:
742       get_cfa_from_loc_descr (loc, cfi->dw_cfi_oprnd1.dw_cfi_loc);
743       break;
744     default:
745       break;
746     }
747 }
748
749 /* Find the previous value for the CFA.  */
750
751 static void
752 lookup_cfa (dw_cfa_location *loc)
753 {
754   dw_cfi_ref cfi;
755   dw_fde_ref fde;
756
757   loc->reg = INVALID_REGNUM;
758   loc->offset = 0;
759   loc->indirect = 0;
760   loc->base_offset = 0;
761
762   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
763     lookup_cfa_1 (cfi, loc);
764
765   fde = current_fde ();
766   if (fde)
767     for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
768       lookup_cfa_1 (cfi, loc);
769 }
770
771 /* The current rule for calculating the DWARF2 canonical frame address.  */
772 static dw_cfa_location cfa;
773
774 /* The register used for saving registers to the stack, and its offset
775    from the CFA.  */
776 static dw_cfa_location cfa_store;
777
778 /* The running total of the size of arguments pushed onto the stack.  */
779 static HOST_WIDE_INT args_size;
780
781 /* The last args_size we actually output.  */
782 static HOST_WIDE_INT old_args_size;
783
784 /* Entry point to update the canonical frame address (CFA).
785    LABEL is passed to add_fde_cfi.  The value of CFA is now to be
786    calculated from REG+OFFSET.  */
787
788 void
789 dwarf2out_def_cfa (const char *label, unsigned int reg, HOST_WIDE_INT offset)
790 {
791   dw_cfa_location loc;
792   loc.indirect = 0;
793   loc.base_offset = 0;
794   loc.reg = reg;
795   loc.offset = offset;
796   def_cfa_1 (label, &loc);
797 }
798
799 /* Determine if two dw_cfa_location structures define the same data.  */
800
801 static bool
802 cfa_equal_p (const dw_cfa_location *loc1, const dw_cfa_location *loc2)
803 {
804   return (loc1->reg == loc2->reg
805           && loc1->offset == loc2->offset
806           && loc1->indirect == loc2->indirect
807           && (loc1->indirect == 0
808               || loc1->base_offset == loc2->base_offset));
809 }
810
811 /* This routine does the actual work.  The CFA is now calculated from
812    the dw_cfa_location structure.  */
813
814 static void
815 def_cfa_1 (const char *label, dw_cfa_location *loc_p)
816 {
817   dw_cfi_ref cfi;
818   dw_cfa_location old_cfa, loc;
819
820   cfa = *loc_p;
821   loc = *loc_p;
822
823   if (cfa_store.reg == loc.reg && loc.indirect == 0)
824     cfa_store.offset = loc.offset;
825
826   loc.reg = DWARF_FRAME_REGNUM (loc.reg);
827   lookup_cfa (&old_cfa);
828
829   /* If nothing changed, no need to issue any call frame instructions.  */
830   if (cfa_equal_p (&loc, &old_cfa))
831     return;
832
833   cfi = new_cfi ();
834
835   if (loc.reg == old_cfa.reg && !loc.indirect)
836     {
837       /* Construct a "DW_CFA_def_cfa_offset <offset>" instruction, indicating
838          the CFA register did not change but the offset did.  */
839       if (loc.offset < 0)
840         {
841           HOST_WIDE_INT f_offset = loc.offset / DWARF_CIE_DATA_ALIGNMENT;
842           gcc_assert (f_offset * DWARF_CIE_DATA_ALIGNMENT == loc.offset);
843
844           cfi->dw_cfi_opc = DW_CFA_def_cfa_offset_sf;
845           cfi->dw_cfi_oprnd1.dw_cfi_offset = f_offset;
846         }
847       else
848         {
849           cfi->dw_cfi_opc = DW_CFA_def_cfa_offset;
850           cfi->dw_cfi_oprnd1.dw_cfi_offset = loc.offset;
851         }
852     }
853
854 #ifndef MIPS_DEBUGGING_INFO  /* SGI dbx thinks this means no offset.  */
855   else if (loc.offset == old_cfa.offset
856            && old_cfa.reg != INVALID_REGNUM
857            && !loc.indirect)
858     {
859       /* Construct a "DW_CFA_def_cfa_register <register>" instruction,
860          indicating the CFA register has changed to <register> but the
861          offset has not changed.  */
862       cfi->dw_cfi_opc = DW_CFA_def_cfa_register;
863       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
864     }
865 #endif
866
867   else if (loc.indirect == 0)
868     {
869       /* Construct a "DW_CFA_def_cfa <register> <offset>" instruction,
870          indicating the CFA register has changed to <register> with
871          the specified offset.  */
872       if (loc.offset < 0)
873         {
874           HOST_WIDE_INT f_offset = loc.offset / DWARF_CIE_DATA_ALIGNMENT;
875           gcc_assert (f_offset * DWARF_CIE_DATA_ALIGNMENT == loc.offset);
876
877           cfi->dw_cfi_opc = DW_CFA_def_cfa_sf;
878           cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
879           cfi->dw_cfi_oprnd2.dw_cfi_offset = f_offset;
880         }
881       else
882         {
883           cfi->dw_cfi_opc = DW_CFA_def_cfa;
884           cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
885           cfi->dw_cfi_oprnd2.dw_cfi_offset = loc.offset;
886         }
887     }
888   else
889     {
890       /* Construct a DW_CFA_def_cfa_expression instruction to
891          calculate the CFA using a full location expression since no
892          register-offset pair is available.  */
893       struct dw_loc_descr_struct *loc_list;
894
895       cfi->dw_cfi_opc = DW_CFA_def_cfa_expression;
896       loc_list = build_cfa_loc (&loc, 0);
897       cfi->dw_cfi_oprnd1.dw_cfi_loc = loc_list;
898     }
899
900   add_fde_cfi (label, cfi);
901 }
902
903 /* Add the CFI for saving a register.  REG is the CFA column number.
904    LABEL is passed to add_fde_cfi.
905    If SREG is -1, the register is saved at OFFSET from the CFA;
906    otherwise it is saved in SREG.  */
907
908 static void
909 reg_save (const char *label, unsigned int reg, unsigned int sreg, HOST_WIDE_INT offset)
910 {
911   dw_cfi_ref cfi = new_cfi ();
912   dw_fde_ref fde = current_fde ();
913
914   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
915
916   /* When stack is aligned, store REG using DW_CFA_expression with
917      FP.  */
918   if (fde
919       && fde->stack_realign
920       && sreg == INVALID_REGNUM)
921     {
922       cfi->dw_cfi_opc = DW_CFA_expression;
923       cfi->dw_cfi_oprnd2.dw_cfi_reg_num = reg;
924       cfi->dw_cfi_oprnd1.dw_cfi_loc
925         = build_cfa_aligned_loc (offset, fde->stack_realignment);
926     }
927   else if (sreg == INVALID_REGNUM)
928     {
929       if (reg & ~0x3f)
930         /* The register number won't fit in 6 bits, so we have to use
931            the long form.  */
932         cfi->dw_cfi_opc = DW_CFA_offset_extended;
933       else
934         cfi->dw_cfi_opc = DW_CFA_offset;
935
936 #ifdef ENABLE_CHECKING
937       {
938         /* If we get an offset that is not a multiple of
939            DWARF_CIE_DATA_ALIGNMENT, there is either a bug in the
940            definition of DWARF_CIE_DATA_ALIGNMENT, or a bug in the machine
941            description.  */
942         HOST_WIDE_INT check_offset = offset / DWARF_CIE_DATA_ALIGNMENT;
943
944         gcc_assert (check_offset * DWARF_CIE_DATA_ALIGNMENT == offset);
945       }
946 #endif
947       offset /= DWARF_CIE_DATA_ALIGNMENT;
948       if (offset < 0)
949         cfi->dw_cfi_opc = DW_CFA_offset_extended_sf;
950
951       cfi->dw_cfi_oprnd2.dw_cfi_offset = offset;
952     }
953   else if (sreg == reg)
954     cfi->dw_cfi_opc = DW_CFA_same_value;
955   else
956     {
957       cfi->dw_cfi_opc = DW_CFA_register;
958       cfi->dw_cfi_oprnd2.dw_cfi_reg_num = sreg;
959     }
960
961   add_fde_cfi (label, cfi);
962 }
963
964 /* Add the CFI for saving a register window.  LABEL is passed to reg_save.
965    This CFI tells the unwinder that it needs to restore the window registers
966    from the previous frame's window save area.
967
968    ??? Perhaps we should note in the CIE where windows are saved (instead of
969    assuming 0(cfa)) and what registers are in the window.  */
970
971 void
972 dwarf2out_window_save (const char *label)
973 {
974   dw_cfi_ref cfi = new_cfi ();
975
976   cfi->dw_cfi_opc = DW_CFA_GNU_window_save;
977   add_fde_cfi (label, cfi);
978 }
979
980 /* Add a CFI to update the running total of the size of arguments
981    pushed onto the stack.  */
982
983 void
984 dwarf2out_args_size (const char *label, HOST_WIDE_INT size)
985 {
986   dw_cfi_ref cfi;
987
988   if (size == old_args_size)
989     return;
990
991   old_args_size = size;
992
993   cfi = new_cfi ();
994   cfi->dw_cfi_opc = DW_CFA_GNU_args_size;
995   cfi->dw_cfi_oprnd1.dw_cfi_offset = size;
996   add_fde_cfi (label, cfi);
997 }
998
999 /* Entry point for saving a register to the stack.  REG is the GCC register
1000    number.  LABEL and OFFSET are passed to reg_save.  */
1001
1002 void
1003 dwarf2out_reg_save (const char *label, unsigned int reg, HOST_WIDE_INT offset)
1004 {
1005   reg_save (label, DWARF_FRAME_REGNUM (reg), INVALID_REGNUM, offset);
1006 }
1007
1008 /* Entry point for saving the return address in the stack.
1009    LABEL and OFFSET are passed to reg_save.  */
1010
1011 void
1012 dwarf2out_return_save (const char *label, HOST_WIDE_INT offset)
1013 {
1014   reg_save (label, DWARF_FRAME_RETURN_COLUMN, INVALID_REGNUM, offset);
1015 }
1016
1017 /* Entry point for saving the return address in a register.
1018    LABEL and SREG are passed to reg_save.  */
1019
1020 void
1021 dwarf2out_return_reg (const char *label, unsigned int sreg)
1022 {
1023   reg_save (label, DWARF_FRAME_RETURN_COLUMN, DWARF_FRAME_REGNUM (sreg), 0);
1024 }
1025
1026 #ifdef DWARF2_UNWIND_INFO
1027 /* Record the initial position of the return address.  RTL is
1028    INCOMING_RETURN_ADDR_RTX.  */
1029
1030 static void
1031 initial_return_save (rtx rtl)
1032 {
1033   unsigned int reg = INVALID_REGNUM;
1034   HOST_WIDE_INT offset = 0;
1035
1036   switch (GET_CODE (rtl))
1037     {
1038     case REG:
1039       /* RA is in a register.  */
1040       reg = DWARF_FRAME_REGNUM (REGNO (rtl));
1041       break;
1042
1043     case MEM:
1044       /* RA is on the stack.  */
1045       rtl = XEXP (rtl, 0);
1046       switch (GET_CODE (rtl))
1047         {
1048         case REG:
1049           gcc_assert (REGNO (rtl) == STACK_POINTER_REGNUM);
1050           offset = 0;
1051           break;
1052
1053         case PLUS:
1054           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1055           offset = INTVAL (XEXP (rtl, 1));
1056           break;
1057
1058         case MINUS:
1059           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1060           offset = -INTVAL (XEXP (rtl, 1));
1061           break;
1062
1063         default:
1064           gcc_unreachable ();
1065         }
1066
1067       break;
1068
1069     case PLUS:
1070       /* The return address is at some offset from any value we can
1071          actually load.  For instance, on the SPARC it is in %i7+8. Just
1072          ignore the offset for now; it doesn't matter for unwinding frames.  */
1073       gcc_assert (GET_CODE (XEXP (rtl, 1)) == CONST_INT);
1074       initial_return_save (XEXP (rtl, 0));
1075       return;
1076
1077     default:
1078       gcc_unreachable ();
1079     }
1080
1081   if (reg != DWARF_FRAME_RETURN_COLUMN)
1082     reg_save (NULL, DWARF_FRAME_RETURN_COLUMN, reg, offset - cfa.offset);
1083 }
1084 #endif
1085
1086 /* Given a SET, calculate the amount of stack adjustment it
1087    contains.  */
1088
1089 static HOST_WIDE_INT
1090 stack_adjust_offset (const_rtx pattern)
1091 {
1092   const_rtx src = SET_SRC (pattern);
1093   const_rtx dest = SET_DEST (pattern);
1094   HOST_WIDE_INT offset = 0;
1095   enum rtx_code code;
1096
1097   if (dest == stack_pointer_rtx)
1098     {
1099       /* (set (reg sp) (plus (reg sp) (const_int))) */
1100       code = GET_CODE (src);
1101       if (! (code == PLUS || code == MINUS)
1102           || XEXP (src, 0) != stack_pointer_rtx
1103           || GET_CODE (XEXP (src, 1)) != CONST_INT)
1104         return 0;
1105
1106       offset = INTVAL (XEXP (src, 1));
1107       if (code == PLUS)
1108         offset = -offset;
1109     }
1110   else if (MEM_P (dest))
1111     {
1112       /* (set (mem (pre_dec (reg sp))) (foo)) */
1113       src = XEXP (dest, 0);
1114       code = GET_CODE (src);
1115
1116       switch (code)
1117         {
1118         case PRE_MODIFY:
1119         case POST_MODIFY:
1120           if (XEXP (src, 0) == stack_pointer_rtx)
1121             {
1122               rtx val = XEXP (XEXP (src, 1), 1);
1123               /* We handle only adjustments by constant amount.  */
1124               gcc_assert (GET_CODE (XEXP (src, 1)) == PLUS
1125                           && GET_CODE (val) == CONST_INT);
1126               offset = -INTVAL (val);
1127               break;
1128             }
1129           return 0;
1130
1131         case PRE_DEC:
1132         case POST_DEC:
1133           if (XEXP (src, 0) == stack_pointer_rtx)
1134             {
1135               offset = GET_MODE_SIZE (GET_MODE (dest));
1136               break;
1137             }
1138           return 0;
1139
1140         case PRE_INC:
1141         case POST_INC:
1142           if (XEXP (src, 0) == stack_pointer_rtx)
1143             {
1144               offset = -GET_MODE_SIZE (GET_MODE (dest));
1145               break;
1146             }
1147           return 0;
1148
1149         default:
1150           return 0;
1151         }
1152     }
1153   else
1154     return 0;
1155
1156   return offset;
1157 }
1158
1159 /* Precomputed args_size for CODE_LABELs and BARRIERs preceeding them,
1160    indexed by INSN_UID.  */
1161
1162 static HOST_WIDE_INT *barrier_args_size;
1163
1164 /* Helper function for compute_barrier_args_size.  Handle one insn.  */
1165
1166 static HOST_WIDE_INT
1167 compute_barrier_args_size_1 (rtx insn, HOST_WIDE_INT cur_args_size,
1168                              VEC (rtx, heap) **next)
1169 {
1170   HOST_WIDE_INT offset = 0;
1171   int i;
1172
1173   if (! RTX_FRAME_RELATED_P (insn))
1174     {
1175       if (prologue_epilogue_contains (insn)
1176           || sibcall_epilogue_contains (insn))
1177         /* Nothing */;
1178       else if (GET_CODE (PATTERN (insn)) == SET)
1179         offset = stack_adjust_offset (PATTERN (insn));
1180       else if (GET_CODE (PATTERN (insn)) == PARALLEL
1181                || GET_CODE (PATTERN (insn)) == SEQUENCE)
1182         {
1183           /* There may be stack adjustments inside compound insns.  Search
1184              for them.  */
1185           for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1186             if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1187               offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i));
1188         }
1189     }
1190   else
1191     {
1192       rtx expr = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
1193
1194       if (expr)
1195         {
1196           expr = XEXP (expr, 0);
1197           if (GET_CODE (expr) == PARALLEL
1198               || GET_CODE (expr) == SEQUENCE)
1199             for (i = 1; i < XVECLEN (expr, 0); i++)
1200               {
1201                 rtx elem = XVECEXP (expr, 0, i);
1202
1203                 if (GET_CODE (elem) == SET && !RTX_FRAME_RELATED_P (elem))
1204                   offset += stack_adjust_offset (elem);
1205               }
1206         }
1207     }
1208
1209 #ifndef STACK_GROWS_DOWNWARD
1210   offset = -offset;
1211 #endif
1212
1213   cur_args_size += offset;
1214   if (cur_args_size < 0)
1215     cur_args_size = 0;
1216
1217   if (JUMP_P (insn))
1218     {
1219       rtx dest = JUMP_LABEL (insn);
1220
1221       if (dest)
1222         {
1223           if (barrier_args_size [INSN_UID (dest)] < 0)
1224             {
1225               barrier_args_size [INSN_UID (dest)] = cur_args_size;
1226               VEC_safe_push (rtx, heap, *next, dest);
1227             }
1228           else
1229             gcc_assert (barrier_args_size[INSN_UID (dest)]
1230                         == cur_args_size);
1231         }
1232     }
1233
1234   return cur_args_size;
1235 }
1236
1237 /* Walk the whole function and compute args_size on BARRIERs.  */
1238
1239 static void
1240 compute_barrier_args_size (void)
1241 {
1242   int max_uid = get_max_uid (), i;
1243   rtx insn;
1244   VEC (rtx, heap) *worklist, *next, *tmp;
1245
1246   barrier_args_size = XNEWVEC (HOST_WIDE_INT, max_uid);
1247   for (i = 0; i < max_uid; i++)
1248     barrier_args_size[i] = -1;
1249
1250   worklist = VEC_alloc (rtx, heap, 20);
1251   next = VEC_alloc (rtx, heap, 20);
1252   insn = get_insns ();
1253   barrier_args_size[INSN_UID (insn)] = 0;
1254   VEC_quick_push (rtx, worklist, insn);
1255   for (;;)
1256     {
1257       while (!VEC_empty (rtx, worklist))
1258         {
1259           rtx prev, body, first_insn;
1260           HOST_WIDE_INT cur_args_size;
1261
1262           first_insn = insn = VEC_pop (rtx, worklist);
1263           cur_args_size = barrier_args_size[INSN_UID (insn)];
1264           prev = prev_nonnote_insn (insn);
1265           if (prev && BARRIER_P (prev))
1266             barrier_args_size[INSN_UID (prev)] = cur_args_size;
1267
1268           for (; insn; insn = NEXT_INSN (insn))
1269             {
1270               if (INSN_DELETED_P (insn) || NOTE_P (insn))
1271                 continue;
1272               if (BARRIER_P (insn))
1273                 break;
1274
1275               if (LABEL_P (insn))
1276                 {
1277                   if (insn == first_insn)
1278                     continue;
1279                   else if (barrier_args_size[INSN_UID (insn)] < 0)
1280                     {
1281                       barrier_args_size[INSN_UID (insn)] = cur_args_size;
1282                       continue;
1283                     }
1284                   else
1285                     {
1286                       /* The insns starting with this label have been
1287                          already scanned or are in the worklist.  */
1288                       gcc_assert (barrier_args_size[INSN_UID (insn)]
1289                                   == cur_args_size);
1290                       break;
1291                     }
1292                 }
1293
1294               body = PATTERN (insn);
1295               if (GET_CODE (body) == SEQUENCE)
1296                 {
1297                   for (i = 1; i < XVECLEN (body, 0); i++)
1298                     cur_args_size
1299                       = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1300                                                      cur_args_size, &next);
1301                   cur_args_size
1302                     = compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1303                                                    cur_args_size, &next);
1304                 }
1305               else
1306                 cur_args_size
1307                   = compute_barrier_args_size_1 (insn, cur_args_size, &next);
1308             }
1309         }
1310
1311       if (VEC_empty (rtx, next))
1312         break;
1313
1314       /* Swap WORKLIST with NEXT and truncate NEXT for next iteration.  */
1315       tmp = next;
1316       next = worklist;
1317       worklist = tmp;
1318       VEC_truncate (rtx, next, 0);
1319     }
1320
1321   VEC_free (rtx, heap, worklist);
1322   VEC_free (rtx, heap, next);
1323 }
1324
1325
1326 /* Check INSN to see if it looks like a push or a stack adjustment, and
1327    make a note of it if it does.  EH uses this information to find out how
1328    much extra space it needs to pop off the stack.  */
1329
1330 static void
1331 dwarf2out_stack_adjust (rtx insn, bool after_p)
1332 {
1333   HOST_WIDE_INT offset;
1334   const char *label;
1335   int i;
1336
1337   /* Don't handle epilogues at all.  Certainly it would be wrong to do so
1338      with this function.  Proper support would require all frame-related
1339      insns to be marked, and to be able to handle saving state around
1340      epilogues textually in the middle of the function.  */
1341   if (prologue_epilogue_contains (insn) || sibcall_epilogue_contains (insn))
1342     return;
1343
1344   /* If only calls can throw, and we have a frame pointer,
1345      save up adjustments until we see the CALL_INSN.  */
1346   if (!flag_asynchronous_unwind_tables && cfa.reg != STACK_POINTER_REGNUM)
1347     {
1348       if (CALL_P (insn) && !after_p)
1349         {
1350           /* Extract the size of the args from the CALL rtx itself.  */
1351           insn = PATTERN (insn);
1352           if (GET_CODE (insn) == PARALLEL)
1353             insn = XVECEXP (insn, 0, 0);
1354           if (GET_CODE (insn) == SET)
1355             insn = SET_SRC (insn);
1356           gcc_assert (GET_CODE (insn) == CALL);
1357           dwarf2out_args_size ("", INTVAL (XEXP (insn, 1)));
1358         }
1359       return;
1360     }
1361
1362   if (CALL_P (insn) && !after_p)
1363     {
1364       if (!flag_asynchronous_unwind_tables)
1365         dwarf2out_args_size ("", args_size);
1366       return;
1367     }
1368   else if (BARRIER_P (insn))
1369     {
1370       /* Don't call compute_barrier_args_size () if the only
1371          BARRIER is at the end of function.  */
1372       if (barrier_args_size == NULL && next_nonnote_insn (insn))
1373         compute_barrier_args_size ();
1374       if (barrier_args_size == NULL)
1375         offset = 0;
1376       else
1377         {
1378           offset = barrier_args_size[INSN_UID (insn)];
1379           if (offset < 0)
1380             offset = 0;
1381         }
1382
1383       offset -= args_size;
1384 #ifndef STACK_GROWS_DOWNWARD
1385       offset = -offset;
1386 #endif
1387     }
1388   else if (GET_CODE (PATTERN (insn)) == SET)
1389     offset = stack_adjust_offset (PATTERN (insn));
1390   else if (GET_CODE (PATTERN (insn)) == PARALLEL
1391            || GET_CODE (PATTERN (insn)) == SEQUENCE)
1392     {
1393       /* There may be stack adjustments inside compound insns.  Search
1394          for them.  */
1395       for (offset = 0, i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1396         if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1397           offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i));
1398     }
1399   else
1400     return;
1401
1402   if (offset == 0)
1403     return;
1404
1405   label = dwarf2out_cfi_label ();
1406   dwarf2out_args_size_adjust (offset, label);
1407 }
1408
1409 /* Adjust args_size based on stack adjustment OFFSET.  */
1410
1411 static void
1412 dwarf2out_args_size_adjust (HOST_WIDE_INT offset, const char *label)
1413 {
1414   if (cfa.reg == STACK_POINTER_REGNUM)
1415     cfa.offset += offset;
1416
1417   if (cfa_store.reg == STACK_POINTER_REGNUM)
1418     cfa_store.offset += offset;
1419
1420 #ifndef STACK_GROWS_DOWNWARD
1421   offset = -offset;
1422 #endif
1423
1424   args_size += offset;
1425   if (args_size < 0)
1426     args_size = 0;
1427
1428   def_cfa_1 (label, &cfa);
1429   if (flag_asynchronous_unwind_tables)
1430     dwarf2out_args_size (label, args_size);
1431 }
1432
1433 #endif
1434
1435 /* We delay emitting a register save until either (a) we reach the end
1436    of the prologue or (b) the register is clobbered.  This clusters
1437    register saves so that there are fewer pc advances.  */
1438
1439 struct queued_reg_save GTY(())
1440 {
1441   struct queued_reg_save *next;
1442   rtx reg;
1443   HOST_WIDE_INT cfa_offset;
1444   rtx saved_reg;
1445 };
1446
1447 static GTY(()) struct queued_reg_save *queued_reg_saves;
1448
1449 /* The caller's ORIG_REG is saved in SAVED_IN_REG.  */
1450 struct reg_saved_in_data GTY(()) {
1451   rtx orig_reg;
1452   rtx saved_in_reg;
1453 };
1454
1455 /* A list of registers saved in other registers.
1456    The list intentionally has a small maximum capacity of 4; if your
1457    port needs more than that, you might consider implementing a
1458    more efficient data structure.  */
1459 static GTY(()) struct reg_saved_in_data regs_saved_in_regs[4];
1460 static GTY(()) size_t num_regs_saved_in_regs;
1461
1462 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
1463 static const char *last_reg_save_label;
1464
1465 /* Add an entry to QUEUED_REG_SAVES saying that REG is now saved at
1466    SREG, or if SREG is NULL then it is saved at OFFSET to the CFA.  */
1467
1468 static void
1469 queue_reg_save (const char *label, rtx reg, rtx sreg, HOST_WIDE_INT offset)
1470 {
1471   struct queued_reg_save *q;
1472
1473   /* Duplicates waste space, but it's also necessary to remove them
1474      for correctness, since the queue gets output in reverse
1475      order.  */
1476   for (q = queued_reg_saves; q != NULL; q = q->next)
1477     if (REGNO (q->reg) == REGNO (reg))
1478       break;
1479
1480   if (q == NULL)
1481     {
1482       q = GGC_NEW (struct queued_reg_save);
1483       q->next = queued_reg_saves;
1484       queued_reg_saves = q;
1485     }
1486
1487   q->reg = reg;
1488   q->cfa_offset = offset;
1489   q->saved_reg = sreg;
1490
1491   last_reg_save_label = label;
1492 }
1493
1494 /* Output all the entries in QUEUED_REG_SAVES.  */
1495
1496 static void
1497 flush_queued_reg_saves (void)
1498 {
1499   struct queued_reg_save *q;
1500
1501   for (q = queued_reg_saves; q; q = q->next)
1502     {
1503       size_t i;
1504       unsigned int reg, sreg;
1505
1506       for (i = 0; i < num_regs_saved_in_regs; i++)
1507         if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (q->reg))
1508           break;
1509       if (q->saved_reg && i == num_regs_saved_in_regs)
1510         {
1511           gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1512           num_regs_saved_in_regs++;
1513         }
1514       if (i != num_regs_saved_in_regs)
1515         {
1516           regs_saved_in_regs[i].orig_reg = q->reg;
1517           regs_saved_in_regs[i].saved_in_reg = q->saved_reg;
1518         }
1519
1520       reg = DWARF_FRAME_REGNUM (REGNO (q->reg));
1521       if (q->saved_reg)
1522         sreg = DWARF_FRAME_REGNUM (REGNO (q->saved_reg));
1523       else
1524         sreg = INVALID_REGNUM;
1525       reg_save (last_reg_save_label, reg, sreg, q->cfa_offset);
1526     }
1527
1528   queued_reg_saves = NULL;
1529   last_reg_save_label = NULL;
1530 }
1531
1532 /* Does INSN clobber any register which QUEUED_REG_SAVES lists a saved
1533    location for?  Or, does it clobber a register which we've previously
1534    said that some other register is saved in, and for which we now
1535    have a new location for?  */
1536
1537 static bool
1538 clobbers_queued_reg_save (const_rtx insn)
1539 {
1540   struct queued_reg_save *q;
1541
1542   for (q = queued_reg_saves; q; q = q->next)
1543     {
1544       size_t i;
1545       if (modified_in_p (q->reg, insn))
1546         return true;
1547       for (i = 0; i < num_regs_saved_in_regs; i++)
1548         if (REGNO (q->reg) == REGNO (regs_saved_in_regs[i].orig_reg)
1549             && modified_in_p (regs_saved_in_regs[i].saved_in_reg, insn))
1550           return true;
1551     }
1552
1553   return false;
1554 }
1555
1556 /* Entry point for saving the first register into the second.  */
1557
1558 void
1559 dwarf2out_reg_save_reg (const char *label, rtx reg, rtx sreg)
1560 {
1561   size_t i;
1562   unsigned int regno, sregno;
1563
1564   for (i = 0; i < num_regs_saved_in_regs; i++)
1565     if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (reg))
1566       break;
1567   if (i == num_regs_saved_in_regs)
1568     {
1569       gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1570       num_regs_saved_in_regs++;
1571     }
1572   regs_saved_in_regs[i].orig_reg = reg;
1573   regs_saved_in_regs[i].saved_in_reg = sreg;
1574
1575   regno = DWARF_FRAME_REGNUM (REGNO (reg));
1576   sregno = DWARF_FRAME_REGNUM (REGNO (sreg));
1577   reg_save (label, regno, sregno, 0);
1578 }
1579
1580 /* What register, if any, is currently saved in REG?  */
1581
1582 static rtx
1583 reg_saved_in (rtx reg)
1584 {
1585   unsigned int regn = REGNO (reg);
1586   size_t i;
1587   struct queued_reg_save *q;
1588
1589   for (q = queued_reg_saves; q; q = q->next)
1590     if (q->saved_reg && regn == REGNO (q->saved_reg))
1591       return q->reg;
1592
1593   for (i = 0; i < num_regs_saved_in_regs; i++)
1594     if (regs_saved_in_regs[i].saved_in_reg
1595         && regn == REGNO (regs_saved_in_regs[i].saved_in_reg))
1596       return regs_saved_in_regs[i].orig_reg;
1597
1598   return NULL_RTX;
1599 }
1600
1601
1602 /* A temporary register holding an integral value used in adjusting SP
1603    or setting up the store_reg.  The "offset" field holds the integer
1604    value, not an offset.  */
1605 static dw_cfa_location cfa_temp;
1606
1607 /* Record call frame debugging information for an expression EXPR,
1608    which either sets SP or FP (adjusting how we calculate the frame
1609    address) or saves a register to the stack or another register.
1610    LABEL indicates the address of EXPR.
1611
1612    This function encodes a state machine mapping rtxes to actions on
1613    cfa, cfa_store, and cfa_temp.reg.  We describe these rules so
1614    users need not read the source code.
1615
1616   The High-Level Picture
1617
1618   Changes in the register we use to calculate the CFA: Currently we
1619   assume that if you copy the CFA register into another register, we
1620   should take the other one as the new CFA register; this seems to
1621   work pretty well.  If it's wrong for some target, it's simple
1622   enough not to set RTX_FRAME_RELATED_P on the insn in question.
1623
1624   Changes in the register we use for saving registers to the stack:
1625   This is usually SP, but not always.  Again, we deduce that if you
1626   copy SP into another register (and SP is not the CFA register),
1627   then the new register is the one we will be using for register
1628   saves.  This also seems to work.
1629
1630   Register saves: There's not much guesswork about this one; if
1631   RTX_FRAME_RELATED_P is set on an insn which modifies memory, it's a
1632   register save, and the register used to calculate the destination
1633   had better be the one we think we're using for this purpose.
1634   It's also assumed that a copy from a call-saved register to another
1635   register is saving that register if RTX_FRAME_RELATED_P is set on
1636   that instruction.  If the copy is from a call-saved register to
1637   the *same* register, that means that the register is now the same
1638   value as in the caller.
1639
1640   Except: If the register being saved is the CFA register, and the
1641   offset is nonzero, we are saving the CFA, so we assume we have to
1642   use DW_CFA_def_cfa_expression.  If the offset is 0, we assume that
1643   the intent is to save the value of SP from the previous frame.
1644
1645   In addition, if a register has previously been saved to a different
1646   register,
1647
1648   Invariants / Summaries of Rules
1649
1650   cfa          current rule for calculating the CFA.  It usually
1651                consists of a register and an offset.
1652   cfa_store    register used by prologue code to save things to the stack
1653                cfa_store.offset is the offset from the value of
1654                cfa_store.reg to the actual CFA
1655   cfa_temp     register holding an integral value.  cfa_temp.offset
1656                stores the value, which will be used to adjust the
1657                stack pointer.  cfa_temp is also used like cfa_store,
1658                to track stores to the stack via fp or a temp reg.
1659
1660   Rules  1- 4: Setting a register's value to cfa.reg or an expression
1661                with cfa.reg as the first operand changes the cfa.reg and its
1662                cfa.offset.  Rule 1 and 4 also set cfa_temp.reg and
1663                cfa_temp.offset.
1664
1665   Rules  6- 9: Set a non-cfa.reg register value to a constant or an
1666                expression yielding a constant.  This sets cfa_temp.reg
1667                and cfa_temp.offset.
1668
1669   Rule 5:      Create a new register cfa_store used to save items to the
1670                stack.
1671
1672   Rules 10-14: Save a register to the stack.  Define offset as the
1673                difference of the original location and cfa_store's
1674                location (or cfa_temp's location if cfa_temp is used).
1675
1676   Rules 16-20: If AND operation happens on sp in prologue, we assume
1677                stack is realigned.  We will use a group of DW_OP_XXX
1678                expressions to represent the location of the stored
1679                register instead of CFA+offset.
1680
1681   The Rules
1682
1683   "{a,b}" indicates a choice of a xor b.
1684   "<reg>:cfa.reg" indicates that <reg> must equal cfa.reg.
1685
1686   Rule 1:
1687   (set <reg1> <reg2>:cfa.reg)
1688   effects: cfa.reg = <reg1>
1689            cfa.offset unchanged
1690            cfa_temp.reg = <reg1>
1691            cfa_temp.offset = cfa.offset
1692
1693   Rule 2:
1694   (set sp ({minus,plus,losum} {sp,fp}:cfa.reg
1695                               {<const_int>,<reg>:cfa_temp.reg}))
1696   effects: cfa.reg = sp if fp used
1697            cfa.offset += {+/- <const_int>, cfa_temp.offset} if cfa.reg==sp
1698            cfa_store.offset += {+/- <const_int>, cfa_temp.offset}
1699              if cfa_store.reg==sp
1700
1701   Rule 3:
1702   (set fp ({minus,plus,losum} <reg>:cfa.reg <const_int>))
1703   effects: cfa.reg = fp
1704            cfa_offset += +/- <const_int>
1705
1706   Rule 4:
1707   (set <reg1> ({plus,losum} <reg2>:cfa.reg <const_int>))
1708   constraints: <reg1> != fp
1709                <reg1> != sp
1710   effects: cfa.reg = <reg1>
1711            cfa_temp.reg = <reg1>
1712            cfa_temp.offset = cfa.offset
1713
1714   Rule 5:
1715   (set <reg1> (plus <reg2>:cfa_temp.reg sp:cfa.reg))
1716   constraints: <reg1> != fp
1717                <reg1> != sp
1718   effects: cfa_store.reg = <reg1>
1719            cfa_store.offset = cfa.offset - cfa_temp.offset
1720
1721   Rule 6:
1722   (set <reg> <const_int>)
1723   effects: cfa_temp.reg = <reg>
1724            cfa_temp.offset = <const_int>
1725
1726   Rule 7:
1727   (set <reg1>:cfa_temp.reg (ior <reg2>:cfa_temp.reg <const_int>))
1728   effects: cfa_temp.reg = <reg1>
1729            cfa_temp.offset |= <const_int>
1730
1731   Rule 8:
1732   (set <reg> (high <exp>))
1733   effects: none
1734
1735   Rule 9:
1736   (set <reg> (lo_sum <exp> <const_int>))
1737   effects: cfa_temp.reg = <reg>
1738            cfa_temp.offset = <const_int>
1739
1740   Rule 10:
1741   (set (mem (pre_modify sp:cfa_store (???? <reg1> <const_int>))) <reg2>)
1742   effects: cfa_store.offset -= <const_int>
1743            cfa.offset = cfa_store.offset if cfa.reg == sp
1744            cfa.reg = sp
1745            cfa.base_offset = -cfa_store.offset
1746
1747   Rule 11:
1748   (set (mem ({pre_inc,pre_dec} sp:cfa_store.reg)) <reg>)
1749   effects: cfa_store.offset += -/+ mode_size(mem)
1750            cfa.offset = cfa_store.offset if cfa.reg == sp
1751            cfa.reg = sp
1752            cfa.base_offset = -cfa_store.offset
1753
1754   Rule 12:
1755   (set (mem ({minus,plus,losum} <reg1>:{cfa_store,cfa_temp} <const_int>))
1756
1757        <reg2>)
1758   effects: cfa.reg = <reg1>
1759            cfa.base_offset = -/+ <const_int> - {cfa_store,cfa_temp}.offset
1760
1761   Rule 13:
1762   (set (mem <reg1>:{cfa_store,cfa_temp}) <reg2>)
1763   effects: cfa.reg = <reg1>
1764            cfa.base_offset = -{cfa_store,cfa_temp}.offset
1765
1766   Rule 14:
1767   (set (mem (postinc <reg1>:cfa_temp <const_int>)) <reg2>)
1768   effects: cfa.reg = <reg1>
1769            cfa.base_offset = -cfa_temp.offset
1770            cfa_temp.offset -= mode_size(mem)
1771
1772   Rule 15:
1773   (set <reg> {unspec, unspec_volatile})
1774   effects: target-dependent
1775
1776   Rule 16:
1777   (set sp (and: sp <const_int>))
1778   constraints: cfa_store.reg == sp
1779   effects: current_fde.stack_realign = 1
1780            cfa_store.offset = 0
1781            fde->drap_reg = cfa.reg if cfa.reg != sp and cfa.reg != fp
1782
1783   Rule 17:
1784   (set (mem ({pre_inc, pre_dec} sp)) (mem (plus (cfa.reg) (const_int))))
1785   effects: cfa_store.offset += -/+ mode_size(mem)
1786
1787   Rule 18:
1788   (set (mem ({pre_inc, pre_dec} sp)) fp)
1789   constraints: fde->stack_realign == 1
1790   effects: cfa_store.offset = 0
1791            cfa.reg != HARD_FRAME_POINTER_REGNUM
1792
1793   Rule 19:
1794   (set (mem ({pre_inc, pre_dec} sp)) cfa.reg)
1795   constraints: fde->stack_realign == 1
1796                && cfa.offset == 0
1797                && cfa.indirect == 0
1798                && cfa.reg != HARD_FRAME_POINTER_REGNUM
1799   effects: Use DW_CFA_def_cfa_expression to define cfa
1800            cfa.reg == fde->drap_reg
1801
1802   Rule 20:
1803   (set reg fde->drap_reg)
1804   constraints: fde->vdrap_reg == INVALID_REGNUM
1805   effects: fde->vdrap_reg = reg.
1806   (set mem fde->drap_reg)
1807   constraints: fde->drap_reg_saved == 1
1808   effects: none.  */
1809
1810 static void
1811 dwarf2out_frame_debug_expr (rtx expr, const char *label)
1812 {
1813   rtx src, dest, span;
1814   HOST_WIDE_INT offset;
1815   dw_fde_ref fde;
1816
1817   /* If RTX_FRAME_RELATED_P is set on a PARALLEL, process each member of
1818      the PARALLEL independently. The first element is always processed if
1819      it is a SET. This is for backward compatibility.   Other elements
1820      are processed only if they are SETs and the RTX_FRAME_RELATED_P
1821      flag is set in them.  */
1822   if (GET_CODE (expr) == PARALLEL || GET_CODE (expr) == SEQUENCE)
1823     {
1824       int par_index;
1825       int limit = XVECLEN (expr, 0);
1826       rtx elem;
1827
1828       /* PARALLELs have strict read-modify-write semantics, so we
1829          ought to evaluate every rvalue before changing any lvalue.
1830          It's cumbersome to do that in general, but there's an
1831          easy approximation that is enough for all current users:
1832          handle register saves before register assignments.  */
1833       if (GET_CODE (expr) == PARALLEL)
1834         for (par_index = 0; par_index < limit; par_index++)
1835           {
1836             elem = XVECEXP (expr, 0, par_index);
1837             if (GET_CODE (elem) == SET
1838                 && MEM_P (SET_DEST (elem))
1839                 && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
1840               dwarf2out_frame_debug_expr (elem, label);
1841           }
1842
1843       for (par_index = 0; par_index < limit; par_index++)
1844         {
1845           elem = XVECEXP (expr, 0, par_index);
1846           if (GET_CODE (elem) == SET
1847               && (!MEM_P (SET_DEST (elem)) || GET_CODE (expr) == SEQUENCE)
1848               && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
1849             dwarf2out_frame_debug_expr (elem, label);
1850           else if (GET_CODE (elem) == SET
1851                    && par_index != 0
1852                    && !RTX_FRAME_RELATED_P (elem))
1853             {
1854               /* Stack adjustment combining might combine some post-prologue
1855                  stack adjustment into a prologue stack adjustment.  */
1856               HOST_WIDE_INT offset = stack_adjust_offset (elem);
1857
1858               if (offset != 0)
1859                 dwarf2out_args_size_adjust (offset, label);
1860             }
1861         }
1862       return;
1863     }
1864
1865   gcc_assert (GET_CODE (expr) == SET);
1866
1867   src = SET_SRC (expr);
1868   dest = SET_DEST (expr);
1869
1870   if (REG_P (src))
1871     {
1872       rtx rsi = reg_saved_in (src);
1873       if (rsi)
1874         src = rsi;
1875     }
1876
1877   fde = current_fde ();
1878
1879   if (GET_CODE (src) == REG
1880       && fde
1881       && fde->drap_reg == REGNO (src)
1882       && (fde->drap_reg_saved
1883           || GET_CODE (dest) == REG))
1884     {
1885       /* Rule 20 */
1886       /* If we are saving dynamic realign argument pointer to a
1887          register, the destination is virtual dynamic realign
1888          argument pointer.  It may be used to access argument.  */
1889       if (GET_CODE (dest) == REG)
1890         {
1891           gcc_assert (fde->vdrap_reg == INVALID_REGNUM);
1892           fde->vdrap_reg = REGNO (dest);
1893         }
1894       return;
1895     }
1896
1897   switch (GET_CODE (dest))
1898     {
1899     case REG:
1900       switch (GET_CODE (src))
1901         {
1902           /* Setting FP from SP.  */
1903         case REG:
1904           if (cfa.reg == (unsigned) REGNO (src))
1905             {
1906               /* Rule 1 */
1907               /* Update the CFA rule wrt SP or FP.  Make sure src is
1908                  relative to the current CFA register.
1909
1910                  We used to require that dest be either SP or FP, but the
1911                  ARM copies SP to a temporary register, and from there to
1912                  FP.  So we just rely on the backends to only set
1913                  RTX_FRAME_RELATED_P on appropriate insns.  */
1914               cfa.reg = REGNO (dest);
1915               cfa_temp.reg = cfa.reg;
1916               cfa_temp.offset = cfa.offset;
1917             }
1918           else
1919             {
1920               /* Saving a register in a register.  */
1921               gcc_assert (!fixed_regs [REGNO (dest)]
1922                           /* For the SPARC and its register window.  */
1923                           || (DWARF_FRAME_REGNUM (REGNO (src))
1924                               == DWARF_FRAME_RETURN_COLUMN));
1925
1926               /* After stack is aligned, we can only save SP in FP
1927                  if drap register is used.  In this case, we have
1928                  to restore stack pointer with the CFA value and we
1929                  don't generate this DWARF information.  */
1930               if (fde
1931                   && fde->stack_realign
1932                   && REGNO (src) == STACK_POINTER_REGNUM)
1933                 gcc_assert (REGNO (dest) == HARD_FRAME_POINTER_REGNUM
1934                             && fde->drap_reg != INVALID_REGNUM
1935                             && cfa.reg != REGNO (src));
1936               else
1937                 queue_reg_save (label, src, dest, 0);
1938             }
1939           break;
1940
1941         case PLUS:
1942         case MINUS:
1943         case LO_SUM:
1944           if (dest == stack_pointer_rtx)
1945             {
1946               /* Rule 2 */
1947               /* Adjusting SP.  */
1948               switch (GET_CODE (XEXP (src, 1)))
1949                 {
1950                 case CONST_INT:
1951                   offset = INTVAL (XEXP (src, 1));
1952                   break;
1953                 case REG:
1954                   gcc_assert ((unsigned) REGNO (XEXP (src, 1))
1955                               == cfa_temp.reg);
1956                   offset = cfa_temp.offset;
1957                   break;
1958                 default:
1959                   gcc_unreachable ();
1960                 }
1961
1962               if (XEXP (src, 0) == hard_frame_pointer_rtx)
1963                 {
1964                   /* Restoring SP from FP in the epilogue.  */
1965                   gcc_assert (cfa.reg == (unsigned) HARD_FRAME_POINTER_REGNUM);
1966                   cfa.reg = STACK_POINTER_REGNUM;
1967                 }
1968               else if (GET_CODE (src) == LO_SUM)
1969                 /* Assume we've set the source reg of the LO_SUM from sp.  */
1970                 ;
1971               else
1972                 gcc_assert (XEXP (src, 0) == stack_pointer_rtx);
1973
1974               if (GET_CODE (src) != MINUS)
1975                 offset = -offset;
1976               if (cfa.reg == STACK_POINTER_REGNUM)
1977                 cfa.offset += offset;
1978               if (cfa_store.reg == STACK_POINTER_REGNUM)
1979                 cfa_store.offset += offset;
1980             }
1981           else if (dest == hard_frame_pointer_rtx)
1982             {
1983               /* Rule 3 */
1984               /* Either setting the FP from an offset of the SP,
1985                  or adjusting the FP */
1986               gcc_assert (frame_pointer_needed);
1987
1988               gcc_assert (REG_P (XEXP (src, 0))
1989                           && (unsigned) REGNO (XEXP (src, 0)) == cfa.reg
1990                           && GET_CODE (XEXP (src, 1)) == CONST_INT);
1991               offset = INTVAL (XEXP (src, 1));
1992               if (GET_CODE (src) != MINUS)
1993                 offset = -offset;
1994               cfa.offset += offset;
1995               cfa.reg = HARD_FRAME_POINTER_REGNUM;
1996             }
1997           else
1998             {
1999               gcc_assert (GET_CODE (src) != MINUS);
2000
2001               /* Rule 4 */
2002               if (REG_P (XEXP (src, 0))
2003                   && REGNO (XEXP (src, 0)) == cfa.reg
2004                   && GET_CODE (XEXP (src, 1)) == CONST_INT)
2005                 {
2006                   /* Setting a temporary CFA register that will be copied
2007                      into the FP later on.  */
2008                   offset = - INTVAL (XEXP (src, 1));
2009                   cfa.offset += offset;
2010                   cfa.reg = REGNO (dest);
2011                   /* Or used to save regs to the stack.  */
2012                   cfa_temp.reg = cfa.reg;
2013                   cfa_temp.offset = cfa.offset;
2014                 }
2015
2016               /* Rule 5 */
2017               else if (REG_P (XEXP (src, 0))
2018                        && REGNO (XEXP (src, 0)) == cfa_temp.reg
2019                        && XEXP (src, 1) == stack_pointer_rtx)
2020                 {
2021                   /* Setting a scratch register that we will use instead
2022                      of SP for saving registers to the stack.  */
2023                   gcc_assert (cfa.reg == STACK_POINTER_REGNUM);
2024                   cfa_store.reg = REGNO (dest);
2025                   cfa_store.offset = cfa.offset - cfa_temp.offset;
2026                 }
2027
2028               /* Rule 9 */
2029               else if (GET_CODE (src) == LO_SUM
2030                        && GET_CODE (XEXP (src, 1)) == CONST_INT)
2031                 {
2032                   cfa_temp.reg = REGNO (dest);
2033                   cfa_temp.offset = INTVAL (XEXP (src, 1));
2034                 }
2035               else
2036                 gcc_unreachable ();
2037             }
2038           break;
2039
2040           /* Rule 6 */
2041         case CONST_INT:
2042           cfa_temp.reg = REGNO (dest);
2043           cfa_temp.offset = INTVAL (src);
2044           break;
2045
2046           /* Rule 7 */
2047         case IOR:
2048           gcc_assert (REG_P (XEXP (src, 0))
2049                       && (unsigned) REGNO (XEXP (src, 0)) == cfa_temp.reg
2050                       && GET_CODE (XEXP (src, 1)) == CONST_INT);
2051
2052           if ((unsigned) REGNO (dest) != cfa_temp.reg)
2053             cfa_temp.reg = REGNO (dest);
2054           cfa_temp.offset |= INTVAL (XEXP (src, 1));
2055           break;
2056
2057           /* Skip over HIGH, assuming it will be followed by a LO_SUM,
2058              which will fill in all of the bits.  */
2059           /* Rule 8 */
2060         case HIGH:
2061           break;
2062
2063           /* Rule 15 */
2064         case UNSPEC:
2065         case UNSPEC_VOLATILE:
2066           gcc_assert (targetm.dwarf_handle_frame_unspec);
2067           targetm.dwarf_handle_frame_unspec (label, expr, XINT (src, 1));
2068           return;
2069
2070           /* Rule 16 */
2071         case AND:
2072           /* If this AND operation happens on stack pointer in prologue,
2073              we assume the stack is realigned and we extract the
2074              alignment.  */
2075           if (fde && XEXP (src, 0) == stack_pointer_rtx)
2076             {
2077               gcc_assert (cfa_store.reg == REGNO (XEXP (src, 0)));
2078               fde->stack_realign = 1;
2079               fde->stack_realignment = INTVAL (XEXP (src, 1));
2080               cfa_store.offset = 0;
2081
2082               if (cfa.reg != STACK_POINTER_REGNUM
2083                   && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2084                 fde->drap_reg = cfa.reg;
2085             }
2086           return;
2087
2088         default:
2089           gcc_unreachable ();
2090         }
2091
2092       def_cfa_1 (label, &cfa);
2093       break;
2094
2095     case MEM:
2096
2097       /* Saving a register to the stack.  Make sure dest is relative to the
2098          CFA register.  */
2099       switch (GET_CODE (XEXP (dest, 0)))
2100         {
2101           /* Rule 10 */
2102           /* With a push.  */
2103         case PRE_MODIFY:
2104           /* We can't handle variable size modifications.  */
2105           gcc_assert (GET_CODE (XEXP (XEXP (XEXP (dest, 0), 1), 1))
2106                       == CONST_INT);
2107           offset = -INTVAL (XEXP (XEXP (XEXP (dest, 0), 1), 1));
2108
2109           gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
2110                       && cfa_store.reg == STACK_POINTER_REGNUM);
2111
2112           cfa_store.offset += offset;
2113           if (cfa.reg == STACK_POINTER_REGNUM)
2114             cfa.offset = cfa_store.offset;
2115
2116           offset = -cfa_store.offset;
2117           break;
2118
2119           /* Rule 11 */
2120         case PRE_INC:
2121         case PRE_DEC:
2122           offset = GET_MODE_SIZE (GET_MODE (dest));
2123           if (GET_CODE (XEXP (dest, 0)) == PRE_INC)
2124             offset = -offset;
2125
2126           gcc_assert ((REGNO (XEXP (XEXP (dest, 0), 0))
2127                        == STACK_POINTER_REGNUM)
2128                       && cfa_store.reg == STACK_POINTER_REGNUM);
2129
2130           cfa_store.offset += offset;
2131
2132           /* Rule 18: If stack is aligned, we will use FP as a
2133              reference to represent the address of the stored
2134              regiser.  */
2135           if (fde
2136               && fde->stack_realign
2137               && src == hard_frame_pointer_rtx)
2138             {
2139               gcc_assert (cfa.reg != HARD_FRAME_POINTER_REGNUM);
2140               cfa_store.offset = 0;
2141             }
2142
2143           if (cfa.reg == STACK_POINTER_REGNUM)
2144             cfa.offset = cfa_store.offset;
2145
2146           offset = -cfa_store.offset;
2147           break;
2148
2149           /* Rule 12 */
2150           /* With an offset.  */
2151         case PLUS:
2152         case MINUS:
2153         case LO_SUM:
2154           {
2155             int regno;
2156
2157             gcc_assert (GET_CODE (XEXP (XEXP (dest, 0), 1)) == CONST_INT
2158                         && REG_P (XEXP (XEXP (dest, 0), 0)));
2159             offset = INTVAL (XEXP (XEXP (dest, 0), 1));
2160             if (GET_CODE (XEXP (dest, 0)) == MINUS)
2161               offset = -offset;
2162
2163             regno = REGNO (XEXP (XEXP (dest, 0), 0));
2164
2165             if (cfa_store.reg == (unsigned) regno)
2166               offset -= cfa_store.offset;
2167             else
2168               {
2169                 gcc_assert (cfa_temp.reg == (unsigned) regno);
2170                 offset -= cfa_temp.offset;
2171               }
2172           }
2173           break;
2174
2175           /* Rule 13 */
2176           /* Without an offset.  */
2177         case REG:
2178           {
2179             int regno = REGNO (XEXP (dest, 0));
2180
2181             if (cfa_store.reg == (unsigned) regno)
2182               offset = -cfa_store.offset;
2183             else
2184               {
2185                 gcc_assert (cfa_temp.reg == (unsigned) regno);
2186                 offset = -cfa_temp.offset;
2187               }
2188           }
2189           break;
2190
2191           /* Rule 14 */
2192         case POST_INC:
2193           gcc_assert (cfa_temp.reg
2194                       == (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)));
2195           offset = -cfa_temp.offset;
2196           cfa_temp.offset -= GET_MODE_SIZE (GET_MODE (dest));
2197           break;
2198
2199         default:
2200           gcc_unreachable ();
2201         }
2202
2203         /* Rule 17 */
2204         /* If the source operand of this MEM operation is not a
2205            register, basically the source is return address.  Here
2206            we only care how much stack grew and we don't save it.  */
2207       if (!REG_P (src))
2208         break;
2209
2210       if (REGNO (src) != STACK_POINTER_REGNUM
2211           && REGNO (src) != HARD_FRAME_POINTER_REGNUM
2212           && (unsigned) REGNO (src) == cfa.reg)
2213         {
2214           /* We're storing the current CFA reg into the stack.  */
2215
2216           if (cfa.offset == 0)
2217             {
2218               /* Rule 19 */
2219               /* If stack is aligned, putting CFA reg into stack means
2220                  we can no longer use reg + offset to represent CFA.
2221                  Here we use DW_CFA_def_cfa_expression instead.  The
2222                  result of this expression equals to the original CFA
2223                  value.  */
2224               if (fde
2225                   && fde->stack_realign
2226                   && cfa.indirect == 0
2227                   && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2228                 {
2229                   dw_cfa_location cfa_exp;
2230
2231                   gcc_assert (fde->drap_reg == cfa.reg);
2232
2233                   cfa_exp.indirect = 1;
2234                   cfa_exp.reg = HARD_FRAME_POINTER_REGNUM;
2235                   cfa_exp.base_offset = offset;
2236                   cfa_exp.offset = 0;
2237
2238                   fde->drap_reg_saved = 1;
2239
2240                   def_cfa_1 (label, &cfa_exp);
2241                   break;
2242                 }
2243
2244               /* If the source register is exactly the CFA, assume
2245                  we're saving SP like any other register; this happens
2246                  on the ARM.  */
2247               def_cfa_1 (label, &cfa);
2248               queue_reg_save (label, stack_pointer_rtx, NULL_RTX, offset);
2249               break;
2250             }
2251           else
2252             {
2253               /* Otherwise, we'll need to look in the stack to
2254                  calculate the CFA.  */
2255               rtx x = XEXP (dest, 0);
2256
2257               if (!REG_P (x))
2258                 x = XEXP (x, 0);
2259               gcc_assert (REG_P (x));
2260
2261               cfa.reg = REGNO (x);
2262               cfa.base_offset = offset;
2263               cfa.indirect = 1;
2264               def_cfa_1 (label, &cfa);
2265               break;
2266             }
2267         }
2268
2269       def_cfa_1 (label, &cfa);
2270       {
2271         span = targetm.dwarf_register_span (src);
2272
2273         if (!span)
2274           queue_reg_save (label, src, NULL_RTX, offset);
2275         else
2276           {
2277             /* We have a PARALLEL describing where the contents of SRC
2278                live.  Queue register saves for each piece of the
2279                PARALLEL.  */
2280             int par_index;
2281             int limit;
2282             HOST_WIDE_INT span_offset = offset;
2283
2284             gcc_assert (GET_CODE (span) == PARALLEL);
2285
2286             limit = XVECLEN (span, 0);
2287             for (par_index = 0; par_index < limit; par_index++)
2288               {
2289                 rtx elem = XVECEXP (span, 0, par_index);
2290
2291                 queue_reg_save (label, elem, NULL_RTX, span_offset);
2292                 span_offset += GET_MODE_SIZE (GET_MODE (elem));
2293               }
2294           }
2295       }
2296       break;
2297
2298     default:
2299       gcc_unreachable ();
2300     }
2301 }
2302
2303 /* Record call frame debugging information for INSN, which either
2304    sets SP or FP (adjusting how we calculate the frame address) or saves a
2305    register to the stack.  If INSN is NULL_RTX, initialize our state.
2306
2307    If AFTER_P is false, we're being called before the insn is emitted,
2308    otherwise after.  Call instructions get invoked twice.  */
2309
2310 void
2311 dwarf2out_frame_debug (rtx insn, bool after_p)
2312 {
2313   const char *label;
2314   rtx src;
2315
2316   if (insn == NULL_RTX)
2317     {
2318       size_t i;
2319
2320       /* Flush any queued register saves.  */
2321       flush_queued_reg_saves ();
2322
2323       /* Set up state for generating call frame debug info.  */
2324       lookup_cfa (&cfa);
2325       gcc_assert (cfa.reg
2326                   == (unsigned long)DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM));
2327
2328       cfa.reg = STACK_POINTER_REGNUM;
2329       cfa_store = cfa;
2330       cfa_temp.reg = -1;
2331       cfa_temp.offset = 0;
2332
2333       for (i = 0; i < num_regs_saved_in_regs; i++)
2334         {
2335           regs_saved_in_regs[i].orig_reg = NULL_RTX;
2336           regs_saved_in_regs[i].saved_in_reg = NULL_RTX;
2337         }
2338       num_regs_saved_in_regs = 0;
2339
2340       if (barrier_args_size)
2341         {
2342           XDELETEVEC (barrier_args_size);
2343           barrier_args_size = NULL;
2344         }
2345       return;
2346     }
2347
2348   if (!NONJUMP_INSN_P (insn) || clobbers_queued_reg_save (insn))
2349     flush_queued_reg_saves ();
2350
2351   if (! RTX_FRAME_RELATED_P (insn))
2352     {
2353       if (!ACCUMULATE_OUTGOING_ARGS)
2354         dwarf2out_stack_adjust (insn, after_p);
2355       return;
2356     }
2357
2358   label = dwarf2out_cfi_label ();
2359   src = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
2360   if (src)
2361     insn = XEXP (src, 0);
2362   else
2363     insn = PATTERN (insn);
2364
2365   dwarf2out_frame_debug_expr (insn, label);
2366 }
2367
2368 #endif
2369
2370 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used.  */
2371 static enum dw_cfi_oprnd_type dw_cfi_oprnd1_desc
2372  (enum dwarf_call_frame_info cfi);
2373
2374 static enum dw_cfi_oprnd_type
2375 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
2376 {
2377   switch (cfi)
2378     {
2379     case DW_CFA_nop:
2380     case DW_CFA_GNU_window_save:
2381       return dw_cfi_oprnd_unused;
2382
2383     case DW_CFA_set_loc:
2384     case DW_CFA_advance_loc1:
2385     case DW_CFA_advance_loc2:
2386     case DW_CFA_advance_loc4:
2387     case DW_CFA_MIPS_advance_loc8:
2388       return dw_cfi_oprnd_addr;
2389
2390     case DW_CFA_offset:
2391     case DW_CFA_offset_extended:
2392     case DW_CFA_def_cfa:
2393     case DW_CFA_offset_extended_sf:
2394     case DW_CFA_def_cfa_sf:
2395     case DW_CFA_restore_extended:
2396     case DW_CFA_undefined:
2397     case DW_CFA_same_value:
2398     case DW_CFA_def_cfa_register:
2399     case DW_CFA_register:
2400       return dw_cfi_oprnd_reg_num;
2401
2402     case DW_CFA_def_cfa_offset:
2403     case DW_CFA_GNU_args_size:
2404     case DW_CFA_def_cfa_offset_sf:
2405       return dw_cfi_oprnd_offset;
2406
2407     case DW_CFA_def_cfa_expression:
2408     case DW_CFA_expression:
2409       return dw_cfi_oprnd_loc;
2410
2411     default:
2412       gcc_unreachable ();
2413     }
2414 }
2415
2416 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used.  */
2417 static enum dw_cfi_oprnd_type dw_cfi_oprnd2_desc
2418  (enum dwarf_call_frame_info cfi);
2419
2420 static enum dw_cfi_oprnd_type
2421 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
2422 {
2423   switch (cfi)
2424     {
2425     case DW_CFA_def_cfa:
2426     case DW_CFA_def_cfa_sf:
2427     case DW_CFA_offset:
2428     case DW_CFA_offset_extended_sf:
2429     case DW_CFA_offset_extended:
2430       return dw_cfi_oprnd_offset;
2431
2432     case DW_CFA_register:
2433       return dw_cfi_oprnd_reg_num;
2434
2435     default:
2436       return dw_cfi_oprnd_unused;
2437     }
2438 }
2439
2440 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
2441
2442 /* Switch to eh_frame_section.  If we don't have an eh_frame_section,
2443    switch to the data section instead, and write out a synthetic label
2444    for collect2.  */
2445
2446 static void
2447 switch_to_eh_frame_section (void)
2448 {
2449   tree label;
2450
2451 #ifdef EH_FRAME_SECTION_NAME
2452   if (eh_frame_section == 0)
2453     {
2454       int flags;
2455
2456       if (EH_TABLES_CAN_BE_READ_ONLY)
2457         {
2458           int fde_encoding;
2459           int per_encoding;
2460           int lsda_encoding;
2461
2462           fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
2463                                                        /*global=*/0);
2464           per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
2465                                                        /*global=*/1);
2466           lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
2467                                                         /*global=*/0);
2468           flags = ((! flag_pic
2469                     || ((fde_encoding & 0x70) != DW_EH_PE_absptr
2470                         && (fde_encoding & 0x70) != DW_EH_PE_aligned
2471                         && (per_encoding & 0x70) != DW_EH_PE_absptr
2472                         && (per_encoding & 0x70) != DW_EH_PE_aligned
2473                         && (lsda_encoding & 0x70) != DW_EH_PE_absptr
2474                         && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
2475                    ? 0 : SECTION_WRITE);
2476         }
2477       else
2478         flags = SECTION_WRITE;
2479       eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
2480     }
2481 #endif
2482
2483   if (eh_frame_section)
2484     switch_to_section (eh_frame_section);
2485   else
2486     {
2487       /* We have no special eh_frame section.  Put the information in
2488          the data section and emit special labels to guide collect2.  */
2489       switch_to_section (data_section);
2490       label = get_file_function_name ("F");
2491       ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
2492       targetm.asm_out.globalize_label (asm_out_file,
2493                                        IDENTIFIER_POINTER (label));
2494       ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
2495     }
2496 }
2497
2498 /* Output a Call Frame Information opcode and its operand(s).  */
2499
2500 static void
2501 output_cfi (dw_cfi_ref cfi, dw_fde_ref fde, int for_eh)
2502 {
2503   unsigned long r;
2504   if (cfi->dw_cfi_opc == DW_CFA_advance_loc)
2505     dw2_asm_output_data (1, (cfi->dw_cfi_opc
2506                              | (cfi->dw_cfi_oprnd1.dw_cfi_offset & 0x3f)),
2507                          "DW_CFA_advance_loc " HOST_WIDE_INT_PRINT_HEX,
2508                          ((unsigned HOST_WIDE_INT)
2509                           cfi->dw_cfi_oprnd1.dw_cfi_offset));
2510   else if (cfi->dw_cfi_opc == DW_CFA_offset)
2511     {
2512       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2513       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
2514                            "DW_CFA_offset, column 0x%lx", r);
2515       dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
2516     }
2517   else if (cfi->dw_cfi_opc == DW_CFA_restore)
2518     {
2519       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2520       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
2521                            "DW_CFA_restore, column 0x%lx", r);
2522     }
2523   else
2524     {
2525       dw2_asm_output_data (1, cfi->dw_cfi_opc,
2526                            "%s", dwarf_cfi_name (cfi->dw_cfi_opc));
2527
2528       switch (cfi->dw_cfi_opc)
2529         {
2530         case DW_CFA_set_loc:
2531           if (for_eh)
2532             dw2_asm_output_encoded_addr_rtx (
2533                 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0),
2534                 gen_rtx_SYMBOL_REF (Pmode, cfi->dw_cfi_oprnd1.dw_cfi_addr),
2535                 false, NULL);
2536           else
2537             dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2538                                  cfi->dw_cfi_oprnd1.dw_cfi_addr, NULL);
2539           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2540           break;
2541
2542         case DW_CFA_advance_loc1:
2543           dw2_asm_output_delta (1, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2544                                 fde->dw_fde_current_label, NULL);
2545           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2546           break;
2547
2548         case DW_CFA_advance_loc2:
2549           dw2_asm_output_delta (2, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2550                                 fde->dw_fde_current_label, NULL);
2551           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2552           break;
2553
2554         case DW_CFA_advance_loc4:
2555           dw2_asm_output_delta (4, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2556                                 fde->dw_fde_current_label, NULL);
2557           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2558           break;
2559
2560         case DW_CFA_MIPS_advance_loc8:
2561           dw2_asm_output_delta (8, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2562                                 fde->dw_fde_current_label, NULL);
2563           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2564           break;
2565
2566         case DW_CFA_offset_extended:
2567         case DW_CFA_def_cfa:
2568           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2569           dw2_asm_output_data_uleb128 (r, NULL);
2570           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
2571           break;
2572
2573         case DW_CFA_offset_extended_sf:
2574         case DW_CFA_def_cfa_sf:
2575           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2576           dw2_asm_output_data_uleb128 (r, NULL);
2577           dw2_asm_output_data_sleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
2578           break;
2579
2580         case DW_CFA_restore_extended:
2581         case DW_CFA_undefined:
2582         case DW_CFA_same_value:
2583         case DW_CFA_def_cfa_register:
2584           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2585           dw2_asm_output_data_uleb128 (r, NULL);
2586           break;
2587
2588         case DW_CFA_register:
2589           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2590           dw2_asm_output_data_uleb128 (r, NULL);
2591           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, for_eh);
2592           dw2_asm_output_data_uleb128 (r, NULL);
2593           break;
2594
2595         case DW_CFA_def_cfa_offset:
2596         case DW_CFA_GNU_args_size:
2597           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
2598           break;
2599
2600         case DW_CFA_def_cfa_offset_sf:
2601           dw2_asm_output_data_sleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
2602           break;
2603
2604         case DW_CFA_GNU_window_save:
2605           break;
2606
2607         case DW_CFA_def_cfa_expression:
2608         case DW_CFA_expression:
2609           output_cfa_loc (cfi);
2610           break;
2611
2612         case DW_CFA_GNU_negative_offset_extended:
2613           /* Obsoleted by DW_CFA_offset_extended_sf.  */
2614           gcc_unreachable ();
2615
2616         default:
2617           break;
2618         }
2619     }
2620 }
2621
2622 /* Output the call frame information used to record information
2623    that relates to calculating the frame pointer, and records the
2624    location of saved registers.  */
2625
2626 static void
2627 output_call_frame_info (int for_eh)
2628 {
2629   unsigned int i;
2630   dw_fde_ref fde;
2631   dw_cfi_ref cfi;
2632   char l1[20], l2[20], section_start_label[20];
2633   bool any_lsda_needed = false;
2634   char augmentation[6];
2635   int augmentation_size;
2636   int fde_encoding = DW_EH_PE_absptr;
2637   int per_encoding = DW_EH_PE_absptr;
2638   int lsda_encoding = DW_EH_PE_absptr;
2639   int return_reg;
2640
2641   /* Don't emit a CIE if there won't be any FDEs.  */
2642   if (fde_table_in_use == 0)
2643     return;
2644
2645   /* If we make FDEs linkonce, we may have to emit an empty label for
2646      an FDE that wouldn't otherwise be emitted.  We want to avoid
2647      having an FDE kept around when the function it refers to is
2648      discarded.  Example where this matters: a primary function
2649      template in C++ requires EH information, but an explicit
2650      specialization doesn't.  */
2651   if (TARGET_USES_WEAK_UNWIND_INFO
2652       && ! flag_asynchronous_unwind_tables
2653       && flag_exceptions
2654       && for_eh)
2655     for (i = 0; i < fde_table_in_use; i++)
2656       if ((fde_table[i].nothrow || fde_table[i].all_throwers_are_sibcalls)
2657           && !fde_table[i].uses_eh_lsda
2658           && ! DECL_WEAK (fde_table[i].decl))
2659         targetm.asm_out.unwind_label (asm_out_file, fde_table[i].decl,
2660                                       for_eh, /* empty */ 1);
2661
2662   /* If we don't have any functions we'll want to unwind out of, don't
2663      emit any EH unwind information.  Note that if exceptions aren't
2664      enabled, we won't have collected nothrow information, and if we
2665      asked for asynchronous tables, we always want this info.  */
2666   if (for_eh)
2667     {
2668       bool any_eh_needed = !flag_exceptions || flag_asynchronous_unwind_tables;
2669
2670       for (i = 0; i < fde_table_in_use; i++)
2671         if (fde_table[i].uses_eh_lsda)
2672           any_eh_needed = any_lsda_needed = true;
2673         else if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
2674           any_eh_needed = true;
2675         else if (! fde_table[i].nothrow
2676                  && ! fde_table[i].all_throwers_are_sibcalls)
2677           any_eh_needed = true;
2678
2679       if (! any_eh_needed)
2680         return;
2681     }
2682
2683   /* We're going to be generating comments, so turn on app.  */
2684   if (flag_debug_asm)
2685     app_enable ();
2686
2687   if (for_eh)
2688     switch_to_eh_frame_section ();
2689   else
2690     {
2691       if (!debug_frame_section)
2692         debug_frame_section = get_section (DEBUG_FRAME_SECTION,
2693                                            SECTION_DEBUG, NULL);
2694       switch_to_section (debug_frame_section);
2695     }
2696
2697   ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
2698   ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
2699
2700   /* Output the CIE.  */
2701   ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
2702   ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
2703   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
2704     dw2_asm_output_data (4, 0xffffffff,
2705       "Initial length escape value indicating 64-bit DWARF extension");
2706   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
2707                         "Length of Common Information Entry");
2708   ASM_OUTPUT_LABEL (asm_out_file, l1);
2709
2710   /* Now that the CIE pointer is PC-relative for EH,
2711      use 0 to identify the CIE.  */
2712   dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
2713                        (for_eh ? 0 : DWARF_CIE_ID),
2714                        "CIE Identifier Tag");
2715
2716   dw2_asm_output_data (1, DW_CIE_VERSION, "CIE Version");
2717
2718   augmentation[0] = 0;
2719   augmentation_size = 0;
2720   if (for_eh)
2721     {
2722       char *p;
2723
2724       /* Augmentation:
2725          z      Indicates that a uleb128 is present to size the
2726                 augmentation section.
2727          L      Indicates the encoding (and thus presence) of
2728                 an LSDA pointer in the FDE augmentation.
2729          R      Indicates a non-default pointer encoding for
2730                 FDE code pointers.
2731          P      Indicates the presence of an encoding + language
2732                 personality routine in the CIE augmentation.  */
2733
2734       fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
2735       per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
2736       lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
2737
2738       p = augmentation + 1;
2739       if (eh_personality_libfunc)
2740         {
2741           *p++ = 'P';
2742           augmentation_size += 1 + size_of_encoded_value (per_encoding);
2743           assemble_external_libcall (eh_personality_libfunc);
2744         }
2745       if (any_lsda_needed)
2746         {
2747           *p++ = 'L';
2748           augmentation_size += 1;
2749         }
2750       if (fde_encoding != DW_EH_PE_absptr)
2751         {
2752           *p++ = 'R';
2753           augmentation_size += 1;
2754         }
2755       if (p > augmentation + 1)
2756         {
2757           augmentation[0] = 'z';
2758           *p = '\0';
2759         }
2760
2761       /* Ug.  Some platforms can't do unaligned dynamic relocations at all.  */
2762       if (eh_personality_libfunc && per_encoding == DW_EH_PE_aligned)
2763         {
2764           int offset = (  4             /* Length */
2765                         + 4             /* CIE Id */
2766                         + 1             /* CIE version */
2767                         + strlen (augmentation) + 1     /* Augmentation */
2768                         + size_of_uleb128 (1)           /* Code alignment */
2769                         + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
2770                         + 1             /* RA column */
2771                         + 1             /* Augmentation size */
2772                         + 1             /* Personality encoding */ );
2773           int pad = -offset & (PTR_SIZE - 1);
2774
2775           augmentation_size += pad;
2776
2777           /* Augmentations should be small, so there's scarce need to
2778              iterate for a solution.  Die if we exceed one uleb128 byte.  */
2779           gcc_assert (size_of_uleb128 (augmentation_size) == 1);
2780         }
2781     }
2782
2783   dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
2784   dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
2785   dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
2786                                "CIE Data Alignment Factor");
2787
2788   return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
2789   if (DW_CIE_VERSION == 1)
2790     dw2_asm_output_data (1, return_reg, "CIE RA Column");
2791   else
2792     dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
2793
2794   if (augmentation[0])
2795     {
2796       dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
2797       if (eh_personality_libfunc)
2798         {
2799           dw2_asm_output_data (1, per_encoding, "Personality (%s)",
2800                                eh_data_format_name (per_encoding));
2801           dw2_asm_output_encoded_addr_rtx (per_encoding,
2802                                            eh_personality_libfunc,
2803                                            true, NULL);
2804         }
2805
2806       if (any_lsda_needed)
2807         dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
2808                              eh_data_format_name (lsda_encoding));
2809
2810       if (fde_encoding != DW_EH_PE_absptr)
2811         dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
2812                              eh_data_format_name (fde_encoding));
2813     }
2814
2815   for (cfi = cie_cfi_head; cfi != NULL; cfi = cfi->dw_cfi_next)
2816     output_cfi (cfi, NULL, for_eh);
2817
2818   /* Pad the CIE out to an address sized boundary.  */
2819   ASM_OUTPUT_ALIGN (asm_out_file,
2820                     floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
2821   ASM_OUTPUT_LABEL (asm_out_file, l2);
2822
2823   /* Loop through all of the FDE's.  */
2824   for (i = 0; i < fde_table_in_use; i++)
2825     {
2826       fde = &fde_table[i];
2827
2828       /* Don't emit EH unwind info for leaf functions that don't need it.  */
2829       if (for_eh && !flag_asynchronous_unwind_tables && flag_exceptions
2830           && (fde->nothrow || fde->all_throwers_are_sibcalls)
2831           && ! (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
2832           && !fde->uses_eh_lsda)
2833         continue;
2834
2835       targetm.asm_out.unwind_label (asm_out_file, fde->decl, for_eh, /* empty */ 0);
2836       targetm.asm_out.internal_label (asm_out_file, FDE_LABEL, for_eh + i * 2);
2837       ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + i * 2);
2838       ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + i * 2);
2839       if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
2840         dw2_asm_output_data (4, 0xffffffff,
2841                              "Initial length escape value indicating 64-bit DWARF extension");
2842       dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
2843                             "FDE Length");
2844       ASM_OUTPUT_LABEL (asm_out_file, l1);
2845
2846       if (for_eh)
2847         dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
2848       else
2849         dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
2850                                debug_frame_section, "FDE CIE offset");
2851
2852       if (for_eh)
2853         {
2854           if (fde->dw_fde_switched_sections)
2855             {
2856               rtx sym_ref2 = gen_rtx_SYMBOL_REF (Pmode,
2857                                       fde->dw_fde_unlikely_section_label);
2858               rtx sym_ref3= gen_rtx_SYMBOL_REF (Pmode,
2859                                       fde->dw_fde_hot_section_label);
2860               SYMBOL_REF_FLAGS (sym_ref2) |= SYMBOL_FLAG_LOCAL;
2861               SYMBOL_REF_FLAGS (sym_ref3) |= SYMBOL_FLAG_LOCAL;
2862               dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref3, false,
2863                                                "FDE initial location");
2864               dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
2865                                     fde->dw_fde_hot_section_end_label,
2866                                     fde->dw_fde_hot_section_label,
2867                                     "FDE address range");
2868               dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref2, false,
2869                                                "FDE initial location");
2870               dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
2871                                     fde->dw_fde_unlikely_section_end_label,
2872                                     fde->dw_fde_unlikely_section_label,
2873                                     "FDE address range");
2874             }
2875           else
2876             {
2877               rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, fde->dw_fde_begin);
2878               SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
2879               dw2_asm_output_encoded_addr_rtx (fde_encoding,
2880                                                sym_ref,
2881                                                false,
2882                                                "FDE initial location");
2883               dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
2884                                     fde->dw_fde_end, fde->dw_fde_begin,
2885                                     "FDE address range");
2886             }
2887         }
2888       else
2889         {
2890           if (fde->dw_fde_switched_sections)
2891             {
2892               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2893                                    fde->dw_fde_hot_section_label,
2894                                    "FDE initial location");
2895               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
2896                                     fde->dw_fde_hot_section_end_label,
2897                                     fde->dw_fde_hot_section_label,
2898                                     "FDE address range");
2899               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2900                                    fde->dw_fde_unlikely_section_label,
2901                                    "FDE initial location");
2902               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
2903                                     fde->dw_fde_unlikely_section_end_label,
2904                                     fde->dw_fde_unlikely_section_label,
2905                                     "FDE address range");
2906             }
2907           else
2908             {
2909               dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
2910                                    "FDE initial location");
2911               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
2912                                     fde->dw_fde_end, fde->dw_fde_begin,
2913                                     "FDE address range");
2914             }
2915         }
2916
2917       if (augmentation[0])
2918         {
2919           if (any_lsda_needed)
2920             {
2921               int size = size_of_encoded_value (lsda_encoding);
2922
2923               if (lsda_encoding == DW_EH_PE_aligned)
2924                 {
2925                   int offset = (  4             /* Length */
2926                                 + 4             /* CIE offset */
2927                                 + 2 * size_of_encoded_value (fde_encoding)
2928                                 + 1             /* Augmentation size */ );
2929                   int pad = -offset & (PTR_SIZE - 1);
2930
2931                   size += pad;
2932                   gcc_assert (size_of_uleb128 (size) == 1);
2933                 }
2934
2935               dw2_asm_output_data_uleb128 (size, "Augmentation size");
2936
2937               if (fde->uses_eh_lsda)
2938                 {
2939                   ASM_GENERATE_INTERNAL_LABEL (l1, "LLSDA",
2940                                                fde->funcdef_number);
2941                   dw2_asm_output_encoded_addr_rtx (
2942                         lsda_encoding, gen_rtx_SYMBOL_REF (Pmode, l1),
2943                         false, "Language Specific Data Area");
2944                 }
2945               else
2946                 {
2947                   if (lsda_encoding == DW_EH_PE_aligned)
2948                     ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
2949                   dw2_asm_output_data
2950                     (size_of_encoded_value (lsda_encoding), 0,
2951                      "Language Specific Data Area (none)");
2952                 }
2953             }
2954           else
2955             dw2_asm_output_data_uleb128 (0, "Augmentation size");
2956         }
2957
2958       /* Loop through the Call Frame Instructions associated with
2959          this FDE.  */
2960       fde->dw_fde_current_label = fde->dw_fde_begin;
2961       for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
2962         output_cfi (cfi, fde, for_eh);
2963
2964       /* Pad the FDE out to an address sized boundary.  */
2965       ASM_OUTPUT_ALIGN (asm_out_file,
2966                         floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
2967       ASM_OUTPUT_LABEL (asm_out_file, l2);
2968     }
2969
2970   if (for_eh && targetm.terminate_dw2_eh_frame_info)
2971     dw2_asm_output_data (4, 0, "End of Table");
2972 #ifdef MIPS_DEBUGGING_INFO
2973   /* Work around Irix 6 assembler bug whereby labels at the end of a section
2974      get a value of 0.  Putting .align 0 after the label fixes it.  */
2975   ASM_OUTPUT_ALIGN (asm_out_file, 0);
2976 #endif
2977
2978   /* Turn off app to make assembly quicker.  */
2979   if (flag_debug_asm)
2980     app_disable ();
2981 }
2982
2983 /* Output a marker (i.e. a label) for the beginning of a function, before
2984    the prologue.  */
2985
2986 void
2987 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
2988                           const char *file ATTRIBUTE_UNUSED)
2989 {
2990   char label[MAX_ARTIFICIAL_LABEL_BYTES];
2991   char * dup_label;
2992   dw_fde_ref fde;
2993
2994   current_function_func_begin_label = NULL;
2995
2996 #ifdef TARGET_UNWIND_INFO
2997   /* ??? current_function_func_begin_label is also used by except.c
2998      for call-site information.  We must emit this label if it might
2999      be used.  */
3000   if ((! flag_exceptions || USING_SJLJ_EXCEPTIONS)
3001       && ! dwarf2out_do_frame ())
3002     return;
3003 #else
3004   if (! dwarf2out_do_frame ())
3005     return;
3006 #endif
3007
3008   switch_to_section (function_section (current_function_decl));
3009   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
3010                                current_function_funcdef_no);
3011   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
3012                           current_function_funcdef_no);
3013   dup_label = xstrdup (label);
3014   current_function_func_begin_label = dup_label;
3015
3016 #ifdef TARGET_UNWIND_INFO
3017   /* We can elide the fde allocation if we're not emitting debug info.  */
3018   if (! dwarf2out_do_frame ())
3019     return;
3020 #endif
3021
3022   /* Expand the fde table if necessary.  */
3023   if (fde_table_in_use == fde_table_allocated)
3024     {
3025       fde_table_allocated += FDE_TABLE_INCREMENT;
3026       fde_table = GGC_RESIZEVEC (dw_fde_node, fde_table, fde_table_allocated);
3027       memset (fde_table + fde_table_in_use, 0,
3028               FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
3029     }
3030
3031   /* Record the FDE associated with this function.  */
3032   current_funcdef_fde = fde_table_in_use;
3033
3034   /* Add the new FDE at the end of the fde_table.  */
3035   fde = &fde_table[fde_table_in_use++];
3036   fde->decl = current_function_decl;
3037   fde->dw_fde_begin = dup_label;
3038   fde->dw_fde_current_label = dup_label;
3039   fde->dw_fde_hot_section_label = NULL;
3040   fde->dw_fde_hot_section_end_label = NULL;
3041   fde->dw_fde_unlikely_section_label = NULL;
3042   fde->dw_fde_unlikely_section_end_label = NULL;
3043   fde->dw_fde_switched_sections = false;
3044   fde->dw_fde_end = NULL;
3045   fde->dw_fde_cfi = NULL;
3046   fde->funcdef_number = current_function_funcdef_no;
3047   fde->nothrow = TREE_NOTHROW (current_function_decl);
3048   fde->uses_eh_lsda = crtl->uses_eh_lsda;
3049   fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
3050   fde->drap_reg = INVALID_REGNUM;
3051   fde->vdrap_reg = INVALID_REGNUM;
3052
3053   args_size = old_args_size = 0;
3054
3055   /* We only want to output line number information for the genuine dwarf2
3056      prologue case, not the eh frame case.  */
3057 #ifdef DWARF2_DEBUGGING_INFO
3058   if (file)
3059     dwarf2out_source_line (line, file);
3060 #endif
3061 }
3062
3063 /* Output a marker (i.e. a label) for the absolute end of the generated code
3064    for a function definition.  This gets called *after* the epilogue code has
3065    been generated.  */
3066
3067 void
3068 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
3069                         const char *file ATTRIBUTE_UNUSED)
3070 {
3071   dw_fde_ref fde;
3072   char label[MAX_ARTIFICIAL_LABEL_BYTES];
3073
3074   /* Output a label to mark the endpoint of the code generated for this
3075      function.  */
3076   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
3077                                current_function_funcdef_no);
3078   ASM_OUTPUT_LABEL (asm_out_file, label);
3079   fde = current_fde ();
3080   gcc_assert (fde != NULL);
3081   fde->dw_fde_end = xstrdup (label);
3082 }
3083
3084 void
3085 dwarf2out_frame_init (void)
3086 {
3087   /* Allocate the initial hunk of the fde_table.  */
3088   fde_table = GGC_CNEWVEC (dw_fde_node, FDE_TABLE_INCREMENT);
3089   fde_table_allocated = FDE_TABLE_INCREMENT;
3090   fde_table_in_use = 0;
3091
3092   /* Generate the CFA instructions common to all FDE's.  Do it now for the
3093      sake of lookup_cfa.  */
3094
3095   /* On entry, the Canonical Frame Address is at SP.  */
3096   dwarf2out_def_cfa (NULL, STACK_POINTER_REGNUM, INCOMING_FRAME_SP_OFFSET);
3097
3098 #ifdef DWARF2_UNWIND_INFO
3099   if (DWARF2_UNWIND_INFO || DWARF2_FRAME_INFO)
3100     initial_return_save (INCOMING_RETURN_ADDR_RTX);
3101 #endif
3102 }
3103
3104 void
3105 dwarf2out_frame_finish (void)
3106 {
3107   /* Output call frame information.  */
3108   if (DWARF2_FRAME_INFO)
3109     output_call_frame_info (0);
3110
3111 #ifndef TARGET_UNWIND_INFO
3112   /* Output another copy for the unwinder.  */
3113   if (! USING_SJLJ_EXCEPTIONS && (flag_unwind_tables || flag_exceptions))
3114     output_call_frame_info (1);
3115 #endif
3116 }
3117
3118 /* Note that the current function section is being used for code.  */
3119
3120 static void
3121 dwarf2out_note_section_used (void)
3122 {
3123   section *sec = current_function_section ();
3124   if (sec == text_section)
3125     text_section_used = true;
3126   else if (sec == cold_text_section)
3127     cold_text_section_used = true;
3128 }
3129
3130 void
3131 dwarf2out_switch_text_section (void)
3132 {
3133   dw_fde_ref fde = current_fde ();
3134
3135   gcc_assert (cfun && fde);
3136
3137   fde->dw_fde_switched_sections = true;
3138   fde->dw_fde_hot_section_label = crtl->subsections.hot_section_label;
3139   fde->dw_fde_hot_section_end_label = crtl->subsections.hot_section_end_label;
3140   fde->dw_fde_unlikely_section_label = crtl->subsections.cold_section_label;
3141   fde->dw_fde_unlikely_section_end_label = crtl->subsections.cold_section_end_label;
3142   have_multiple_function_sections = true;
3143
3144   /* Reset the current label on switching text sections, so that we
3145      don't attempt to advance_loc4 between labels in different sections.  */
3146   fde->dw_fde_current_label = NULL;
3147
3148   /* There is no need to mark used sections when not debugging.  */
3149   if (cold_text_section != NULL)
3150     dwarf2out_note_section_used ();
3151 }
3152 #endif
3153 \f
3154 /* And now, the subset of the debugging information support code necessary
3155    for emitting location expressions.  */
3156
3157 /* Data about a single source file.  */
3158 struct dwarf_file_data GTY(())
3159 {
3160   const char * filename;
3161   int emitted_number;
3162 };
3163
3164 /* We need some way to distinguish DW_OP_addr with a direct symbol
3165    relocation from DW_OP_addr with a dtp-relative symbol relocation.  */
3166 #define INTERNAL_DW_OP_tls_addr         (0x100 + DW_OP_addr)
3167
3168
3169 typedef struct dw_val_struct *dw_val_ref;
3170 typedef struct die_struct *dw_die_ref;
3171 typedef const struct die_struct *const_dw_die_ref;
3172 typedef struct dw_loc_descr_struct *dw_loc_descr_ref;
3173 typedef struct dw_loc_list_struct *dw_loc_list_ref;
3174
3175 /* Each DIE may have a series of attribute/value pairs.  Values
3176    can take on several forms.  The forms that are used in this
3177    implementation are listed below.  */
3178
3179 enum dw_val_class
3180 {
3181   dw_val_class_addr,
3182   dw_val_class_offset,
3183   dw_val_class_loc,
3184   dw_val_class_loc_list,
3185   dw_val_class_range_list,
3186   dw_val_class_const,
3187   dw_val_class_unsigned_const,
3188   dw_val_class_long_long,
3189   dw_val_class_vec,
3190   dw_val_class_flag,
3191   dw_val_class_die_ref,
3192   dw_val_class_fde_ref,
3193   dw_val_class_lbl_id,
3194   dw_val_class_lineptr,
3195   dw_val_class_str,
3196   dw_val_class_macptr,
3197   dw_val_class_file
3198 };
3199
3200 /* Describe a double word constant value.  */
3201 /* ??? Every instance of long_long in the code really means CONST_DOUBLE.  */
3202
3203 typedef struct dw_long_long_struct GTY(())
3204 {
3205   unsigned long hi;
3206   unsigned long low;
3207 }
3208 dw_long_long_const;
3209
3210 /* Describe a floating point constant value, or a vector constant value.  */
3211
3212 typedef struct dw_vec_struct GTY(())
3213 {
3214   unsigned char * GTY((length ("%h.length"))) array;
3215   unsigned length;
3216   unsigned elt_size;
3217 }
3218 dw_vec_const;
3219
3220 /* The dw_val_node describes an attribute's value, as it is
3221    represented internally.  */
3222
3223 typedef struct dw_val_struct GTY(())
3224 {
3225   enum dw_val_class val_class;
3226   union dw_val_struct_union
3227     {
3228       rtx GTY ((tag ("dw_val_class_addr"))) val_addr;
3229       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_offset"))) val_offset;
3230       dw_loc_list_ref GTY ((tag ("dw_val_class_loc_list"))) val_loc_list;
3231       dw_loc_descr_ref GTY ((tag ("dw_val_class_loc"))) val_loc;
3232       HOST_WIDE_INT GTY ((default)) val_int;
3233       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_unsigned_const"))) val_unsigned;
3234       dw_long_long_const GTY ((tag ("dw_val_class_long_long"))) val_long_long;
3235       dw_vec_const GTY ((tag ("dw_val_class_vec"))) val_vec;
3236       struct dw_val_die_union
3237         {
3238           dw_die_ref die;
3239           int external;
3240         } GTY ((tag ("dw_val_class_die_ref"))) val_die_ref;
3241       unsigned GTY ((tag ("dw_val_class_fde_ref"))) val_fde_index;
3242       struct indirect_string_node * GTY ((tag ("dw_val_class_str"))) val_str;
3243       char * GTY ((tag ("dw_val_class_lbl_id"))) val_lbl_id;
3244       unsigned char GTY ((tag ("dw_val_class_flag"))) val_flag;
3245       struct dwarf_file_data * GTY ((tag ("dw_val_class_file"))) val_file;
3246     }
3247   GTY ((desc ("%1.val_class"))) v;
3248 }
3249 dw_val_node;
3250
3251 /* Locations in memory are described using a sequence of stack machine
3252    operations.  */
3253
3254 typedef struct dw_loc_descr_struct GTY(())
3255 {
3256   dw_loc_descr_ref dw_loc_next;
3257   enum dwarf_location_atom dw_loc_opc;
3258   dw_val_node dw_loc_oprnd1;
3259   dw_val_node dw_loc_oprnd2;
3260   int dw_loc_addr;
3261 }
3262 dw_loc_descr_node;
3263
3264 /* Location lists are ranges + location descriptions for that range,
3265    so you can track variables that are in different places over
3266    their entire life.  */
3267 typedef struct dw_loc_list_struct GTY(())
3268 {
3269   dw_loc_list_ref dw_loc_next;
3270   const char *begin; /* Label for begin address of range */
3271   const char *end;  /* Label for end address of range */
3272   char *ll_symbol; /* Label for beginning of location list.
3273                       Only on head of list */
3274   const char *section; /* Section this loclist is relative to */
3275   dw_loc_descr_ref expr;
3276 } dw_loc_list_node;
3277
3278 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
3279
3280 static const char *dwarf_stack_op_name (unsigned);
3281 static dw_loc_descr_ref new_loc_descr (enum dwarf_location_atom,
3282                                        unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT);
3283 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
3284 static void add_loc_descr (dw_loc_descr_ref *, dw_loc_descr_ref);
3285 static unsigned long size_of_loc_descr (dw_loc_descr_ref);
3286 static unsigned long size_of_locs (dw_loc_descr_ref);
3287 static void output_loc_operands (dw_loc_descr_ref);
3288 static void output_loc_sequence (dw_loc_descr_ref);
3289
3290 /* Convert a DWARF stack opcode into its string name.  */
3291
3292 static const char *
3293 dwarf_stack_op_name (unsigned int op)
3294 {
3295   switch (op)
3296     {
3297     case DW_OP_addr:
3298     case INTERNAL_DW_OP_tls_addr:
3299       return "DW_OP_addr";
3300     case DW_OP_deref:
3301       return "DW_OP_deref";
3302     case DW_OP_const1u:
3303       return "DW_OP_const1u";
3304     case DW_OP_const1s:
3305       return "DW_OP_const1s";
3306     case DW_OP_const2u:
3307       return "DW_OP_const2u";
3308     case DW_OP_const2s:
3309       return "DW_OP_const2s";
3310     case DW_OP_const4u:
3311       return "DW_OP_const4u";
3312     case DW_OP_const4s:
3313       return "DW_OP_const4s";
3314     case DW_OP_const8u:
3315       return "DW_OP_const8u";
3316     case DW_OP_const8s:
3317       return "DW_OP_const8s";
3318     case DW_OP_constu:
3319       return "DW_OP_constu";
3320     case DW_OP_consts:
3321       return "DW_OP_consts";
3322     case DW_OP_dup:
3323       return "DW_OP_dup";
3324     case DW_OP_drop:
3325       return "DW_OP_drop";
3326     case DW_OP_over:
3327       return "DW_OP_over";
3328     case DW_OP_pick:
3329       return "DW_OP_pick";
3330     case DW_OP_swap:
3331       return "DW_OP_swap";
3332     case DW_OP_rot:
3333       return "DW_OP_rot";
3334     case DW_OP_xderef:
3335       return "DW_OP_xderef";
3336     case DW_OP_abs:
3337       return "DW_OP_abs";
3338     case DW_OP_and:
3339       return "DW_OP_and";
3340     case DW_OP_div:
3341       return "DW_OP_div";
3342     case DW_OP_minus:
3343       return "DW_OP_minus";
3344     case DW_OP_mod:
3345       return "DW_OP_mod";
3346     case DW_OP_mul:
3347       return "DW_OP_mul";
3348     case DW_OP_neg:
3349       return "DW_OP_neg";
3350     case DW_OP_not:
3351       return "DW_OP_not";
3352     case DW_OP_or:
3353       return "DW_OP_or";
3354     case DW_OP_plus:
3355       return "DW_OP_plus";
3356     case DW_OP_plus_uconst:
3357       return "DW_OP_plus_uconst";
3358     case DW_OP_shl:
3359       return "DW_OP_shl";
3360     case DW_OP_shr:
3361       return "DW_OP_shr";
3362     case DW_OP_shra:
3363       return "DW_OP_shra";
3364     case DW_OP_xor:
3365       return "DW_OP_xor";
3366     case DW_OP_bra:
3367       return "DW_OP_bra";
3368     case DW_OP_eq:
3369       return "DW_OP_eq";
3370     case DW_OP_ge:
3371       return "DW_OP_ge";
3372     case DW_OP_gt:
3373       return "DW_OP_gt";
3374     case DW_OP_le:
3375       return "DW_OP_le";
3376     case DW_OP_lt:
3377       return "DW_OP_lt";
3378     case DW_OP_ne:
3379       return "DW_OP_ne";
3380     case DW_OP_skip:
3381       return "DW_OP_skip";
3382     case DW_OP_lit0:
3383       return "DW_OP_lit0";
3384     case DW_OP_lit1:
3385       return "DW_OP_lit1";
3386     case DW_OP_lit2:
3387       return "DW_OP_lit2";
3388     case DW_OP_lit3:
3389       return "DW_OP_lit3";
3390     case DW_OP_lit4:
3391       return "DW_OP_lit4";
3392     case DW_OP_lit5:
3393       return "DW_OP_lit5";
3394     case DW_OP_lit6:
3395       return "DW_OP_lit6";
3396     case DW_OP_lit7:
3397       return "DW_OP_lit7";
3398     case DW_OP_lit8:
3399       return "DW_OP_lit8";
3400     case DW_OP_lit9:
3401       return "DW_OP_lit9";
3402     case DW_OP_lit10:
3403       return "DW_OP_lit10";
3404     case DW_OP_lit11:
3405       return "DW_OP_lit11";
3406     case DW_OP_lit12:
3407       return "DW_OP_lit12";
3408     case DW_OP_lit13:
3409       return "DW_OP_lit13";
3410     case DW_OP_lit14:
3411       return "DW_OP_lit14";
3412     case DW_OP_lit15:
3413       return "DW_OP_lit15";
3414     case DW_OP_lit16:
3415       return "DW_OP_lit16";
3416     case DW_OP_lit17:
3417       return "DW_OP_lit17";
3418     case DW_OP_lit18:
3419       return "DW_OP_lit18";
3420     case DW_OP_lit19:
3421       return "DW_OP_lit19";
3422     case DW_OP_lit20:
3423       return "DW_OP_lit20";
3424     case DW_OP_lit21:
3425       return "DW_OP_lit21";
3426     case DW_OP_lit22:
3427       return "DW_OP_lit22";
3428     case DW_OP_lit23:
3429       return "DW_OP_lit23";
3430     case DW_OP_lit24:
3431       return "DW_OP_lit24";
3432     case DW_OP_lit25:
3433       return "DW_OP_lit25";
3434     case DW_OP_lit26:
3435       return "DW_OP_lit26";
3436     case DW_OP_lit27:
3437       return "DW_OP_lit27";
3438     case DW_OP_lit28:
3439       return "DW_OP_lit28";
3440     case DW_OP_lit29:
3441       return "DW_OP_lit29";
3442     case DW_OP_lit30:
3443       return "DW_OP_lit30";
3444     case DW_OP_lit31:
3445       return "DW_OP_lit31";
3446     case DW_OP_reg0:
3447       return "DW_OP_reg0";
3448     case DW_OP_reg1:
3449       return "DW_OP_reg1";
3450     case DW_OP_reg2:
3451       return "DW_OP_reg2";
3452     case DW_OP_reg3:
3453       return "DW_OP_reg3";
3454     case DW_OP_reg4:
3455       return "DW_OP_reg4";
3456     case DW_OP_reg5:
3457       return "DW_OP_reg5";
3458     case DW_OP_reg6:
3459       return "DW_OP_reg6";
3460     case DW_OP_reg7:
3461       return "DW_OP_reg7";
3462     case DW_OP_reg8:
3463       return "DW_OP_reg8";
3464     case DW_OP_reg9:
3465       return "DW_OP_reg9";
3466     case DW_OP_reg10:
3467       return "DW_OP_reg10";
3468     case DW_OP_reg11:
3469       return "DW_OP_reg11";
3470     case DW_OP_reg12:
3471       return "DW_OP_reg12";
3472     case DW_OP_reg13:
3473       return "DW_OP_reg13";
3474     case DW_OP_reg14:
3475       return "DW_OP_reg14";
3476     case DW_OP_reg15:
3477       return "DW_OP_reg15";
3478     case DW_OP_reg16:
3479       return "DW_OP_reg16";
3480     case DW_OP_reg17:
3481       return "DW_OP_reg17";
3482     case DW_OP_reg18:
3483       return "DW_OP_reg18";
3484     case DW_OP_reg19:
3485       return "DW_OP_reg19";
3486     case DW_OP_reg20:
3487       return "DW_OP_reg20";
3488     case DW_OP_reg21:
3489       return "DW_OP_reg21";
3490     case DW_OP_reg22:
3491       return "DW_OP_reg22";
3492     case DW_OP_reg23:
3493       return "DW_OP_reg23";
3494     case DW_OP_reg24:
3495       return "DW_OP_reg24";
3496     case DW_OP_reg25:
3497       return "DW_OP_reg25";
3498     case DW_OP_reg26:
3499       return "DW_OP_reg26";
3500     case DW_OP_reg27:
3501       return "DW_OP_reg27";
3502     case DW_OP_reg28:
3503       return "DW_OP_reg28";
3504     case DW_OP_reg29:
3505       return "DW_OP_reg29";
3506     case DW_OP_reg30:
3507       return "DW_OP_reg30";
3508     case DW_OP_reg31:
3509       return "DW_OP_reg31";
3510     case DW_OP_breg0:
3511       return "DW_OP_breg0";
3512     case DW_OP_breg1:
3513       return "DW_OP_breg1";
3514     case DW_OP_breg2:
3515       return "DW_OP_breg2";
3516     case DW_OP_breg3:
3517       return "DW_OP_breg3";
3518     case DW_OP_breg4:
3519       return "DW_OP_breg4";
3520     case DW_OP_breg5:
3521       return "DW_OP_breg5";
3522     case DW_OP_breg6:
3523       return "DW_OP_breg6";
3524     case DW_OP_breg7:
3525       return "DW_OP_breg7";
3526     case DW_OP_breg8:
3527       return "DW_OP_breg8";
3528     case DW_OP_breg9:
3529       return "DW_OP_breg9";
3530     case DW_OP_breg10:
3531       return "DW_OP_breg10";
3532     case DW_OP_breg11:
3533       return "DW_OP_breg11";
3534     case DW_OP_breg12:
3535       return "DW_OP_breg12";
3536     case DW_OP_breg13:
3537       return "DW_OP_breg13";
3538     case DW_OP_breg14:
3539       return "DW_OP_breg14";
3540     case DW_OP_breg15:
3541       return "DW_OP_breg15";
3542     case DW_OP_breg16:
3543       return "DW_OP_breg16";
3544     case DW_OP_breg17:
3545       return "DW_OP_breg17";
3546     case DW_OP_breg18:
3547       return "DW_OP_breg18";
3548     case DW_OP_breg19:
3549       return "DW_OP_breg19";
3550     case DW_OP_breg20:
3551       return "DW_OP_breg20";
3552     case DW_OP_breg21:
3553       return "DW_OP_breg21";
3554     case DW_OP_breg22:
3555       return "DW_OP_breg22";
3556     case DW_OP_breg23:
3557       return "DW_OP_breg23";
3558     case DW_OP_breg24:
3559       return "DW_OP_breg24";
3560     case DW_OP_breg25:
3561       return "DW_OP_breg25";
3562     case DW_OP_breg26:
3563       return "DW_OP_breg26";
3564     case DW_OP_breg27:
3565       return "DW_OP_breg27";
3566     case DW_OP_breg28:
3567       return "DW_OP_breg28";
3568     case DW_OP_breg29:
3569       return "DW_OP_breg29";
3570     case DW_OP_breg30:
3571       return "DW_OP_breg30";
3572     case DW_OP_breg31:
3573       return "DW_OP_breg31";
3574     case DW_OP_regx:
3575       return "DW_OP_regx";
3576     case DW_OP_fbreg:
3577       return "DW_OP_fbreg";
3578     case DW_OP_bregx:
3579       return "DW_OP_bregx";
3580     case DW_OP_piece:
3581       return "DW_OP_piece";
3582     case DW_OP_deref_size:
3583       return "DW_OP_deref_size";
3584     case DW_OP_xderef_size:
3585       return "DW_OP_xderef_size";
3586     case DW_OP_nop:
3587       return "DW_OP_nop";
3588     case DW_OP_push_object_address:
3589       return "DW_OP_push_object_address";
3590     case DW_OP_call2:
3591       return "DW_OP_call2";
3592     case DW_OP_call4:
3593       return "DW_OP_call4";
3594     case DW_OP_call_ref:
3595       return "DW_OP_call_ref";
3596     case DW_OP_GNU_push_tls_address:
3597       return "DW_OP_GNU_push_tls_address";
3598     case DW_OP_GNU_uninit:
3599       return "DW_OP_GNU_uninit";
3600     default:
3601       return "OP_<unknown>";
3602     }
3603 }
3604
3605 /* Return a pointer to a newly allocated location description.  Location
3606    descriptions are simple expression terms that can be strung
3607    together to form more complicated location (address) descriptions.  */
3608
3609 static inline dw_loc_descr_ref
3610 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
3611                unsigned HOST_WIDE_INT oprnd2)
3612 {
3613   dw_loc_descr_ref descr = GGC_CNEW (dw_loc_descr_node);
3614
3615   descr->dw_loc_opc = op;
3616   descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
3617   descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
3618   descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
3619   descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
3620
3621   return descr;
3622 }
3623
3624 /* Add a location description term to a location description expression.  */
3625
3626 static inline void
3627 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
3628 {
3629   dw_loc_descr_ref *d;
3630
3631   /* Find the end of the chain.  */
3632   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
3633     ;
3634
3635   *d = descr;
3636 }
3637
3638 /* Return the size of a location descriptor.  */
3639
3640 static unsigned long
3641 size_of_loc_descr (dw_loc_descr_ref loc)
3642 {
3643   unsigned long size = 1;
3644
3645   switch (loc->dw_loc_opc)
3646     {
3647     case DW_OP_addr:
3648     case INTERNAL_DW_OP_tls_addr:
3649       size += DWARF2_ADDR_SIZE;
3650       break;
3651     case DW_OP_const1u:
3652     case DW_OP_const1s:
3653       size += 1;
3654       break;
3655     case DW_OP_const2u:
3656     case DW_OP_const2s:
3657       size += 2;
3658       break;
3659     case DW_OP_const4u:
3660     case DW_OP_const4s:
3661       size += 4;
3662       break;
3663     case DW_OP_const8u:
3664     case DW_OP_const8s:
3665       size += 8;
3666       break;
3667     case DW_OP_constu:
3668       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3669       break;
3670     case DW_OP_consts:
3671       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3672       break;
3673     case DW_OP_pick:
3674       size += 1;
3675       break;
3676     case DW_OP_plus_uconst:
3677       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3678       break;
3679     case DW_OP_skip:
3680     case DW_OP_bra:
3681       size += 2;
3682       break;
3683     case DW_OP_breg0:
3684     case DW_OP_breg1:
3685     case DW_OP_breg2:
3686     case DW_OP_breg3:
3687     case DW_OP_breg4:
3688     case DW_OP_breg5:
3689     case DW_OP_breg6:
3690     case DW_OP_breg7:
3691     case DW_OP_breg8:
3692     case DW_OP_breg9:
3693     case DW_OP_breg10:
3694     case DW_OP_breg11:
3695     case DW_OP_breg12:
3696     case DW_OP_breg13:
3697     case DW_OP_breg14:
3698     case DW_OP_breg15:
3699     case DW_OP_breg16:
3700     case DW_OP_breg17:
3701     case DW_OP_breg18:
3702     case DW_OP_breg19:
3703     case DW_OP_breg20:
3704     case DW_OP_breg21:
3705     case DW_OP_breg22:
3706     case DW_OP_breg23:
3707     case DW_OP_breg24:
3708     case DW_OP_breg25:
3709     case DW_OP_breg26:
3710     case DW_OP_breg27:
3711     case DW_OP_breg28:
3712     case DW_OP_breg29:
3713     case DW_OP_breg30:
3714     case DW_OP_breg31:
3715       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3716       break;
3717     case DW_OP_regx:
3718       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3719       break;
3720     case DW_OP_fbreg:
3721       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3722       break;
3723     case DW_OP_bregx:
3724       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3725       size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
3726       break;
3727     case DW_OP_piece:
3728       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3729       break;
3730     case DW_OP_deref_size:
3731     case DW_OP_xderef_size:
3732       size += 1;
3733       break;
3734     case DW_OP_call2:
3735       size += 2;
3736       break;
3737     case DW_OP_call4:
3738       size += 4;
3739       break;
3740     case DW_OP_call_ref:
3741       size += DWARF2_ADDR_SIZE;
3742       break;
3743     default:
3744       break;
3745     }
3746
3747   return size;
3748 }
3749
3750 /* Return the size of a series of location descriptors.  */
3751
3752 static unsigned long
3753 size_of_locs (dw_loc_descr_ref loc)
3754 {
3755   dw_loc_descr_ref l;
3756   unsigned long size;
3757
3758   /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
3759      field, to avoid writing to a PCH file.  */
3760   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
3761     {
3762       if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
3763         break;
3764       size += size_of_loc_descr (l);
3765     }
3766   if (! l)
3767     return size;
3768
3769   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
3770     {
3771       l->dw_loc_addr = size;
3772       size += size_of_loc_descr (l);
3773     }
3774
3775   return size;
3776 }
3777
3778 /* Output location description stack opcode's operands (if any).  */
3779
3780 static void
3781 output_loc_operands (dw_loc_descr_ref loc)
3782 {
3783   dw_val_ref val1 = &loc->dw_loc_oprnd1;
3784   dw_val_ref val2 = &loc->dw_loc_oprnd2;
3785
3786   switch (loc->dw_loc_opc)
3787     {
3788 #ifdef DWARF2_DEBUGGING_INFO
3789     case DW_OP_addr:
3790       dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
3791       break;
3792     case DW_OP_const2u:
3793     case DW_OP_const2s:
3794       dw2_asm_output_data (2, val1->v.val_int, NULL);
3795       break;
3796     case DW_OP_const4u:
3797     case DW_OP_const4s:
3798       dw2_asm_output_data (4, val1->v.val_int, NULL);
3799       break;
3800     case DW_OP_const8u:
3801     case DW_OP_const8s:
3802       gcc_assert (HOST_BITS_PER_LONG >= 64);
3803       dw2_asm_output_data (8, val1->v.val_int, NULL);
3804       break;
3805     case DW_OP_skip:
3806     case DW_OP_bra:
3807       {
3808         int offset;
3809
3810         gcc_assert (val1->val_class == dw_val_class_loc);
3811         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
3812
3813         dw2_asm_output_data (2, offset, NULL);
3814       }
3815       break;
3816 #else
3817     case DW_OP_addr:
3818     case DW_OP_const2u:
3819     case DW_OP_const2s:
3820     case DW_OP_const4u:
3821     case DW_OP_const4s:
3822     case DW_OP_const8u:
3823     case DW_OP_const8s:
3824     case DW_OP_skip:
3825     case DW_OP_bra:
3826       /* We currently don't make any attempt to make sure these are
3827          aligned properly like we do for the main unwind info, so
3828          don't support emitting things larger than a byte if we're
3829          only doing unwinding.  */
3830       gcc_unreachable ();
3831 #endif
3832     case DW_OP_const1u:
3833     case DW_OP_const1s:
3834       dw2_asm_output_data (1, val1->v.val_int, NULL);
3835       break;
3836     case DW_OP_constu:
3837       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3838       break;
3839     case DW_OP_consts:
3840       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
3841       break;
3842     case DW_OP_pick:
3843       dw2_asm_output_data (1, val1->v.val_int, NULL);
3844       break;
3845     case DW_OP_plus_uconst:
3846       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3847       break;
3848     case DW_OP_breg0:
3849     case DW_OP_breg1:
3850     case DW_OP_breg2:
3851     case DW_OP_breg3:
3852     case DW_OP_breg4:
3853     case DW_OP_breg5:
3854     case DW_OP_breg6:
3855     case DW_OP_breg7:
3856     case DW_OP_breg8:
3857     case DW_OP_breg9:
3858     case DW_OP_breg10:
3859     case DW_OP_breg11:
3860     case DW_OP_breg12:
3861     case DW_OP_breg13:
3862     case DW_OP_breg14:
3863     case DW_OP_breg15:
3864     case DW_OP_breg16:
3865     case DW_OP_breg17:
3866     case DW_OP_breg18:
3867     case DW_OP_breg19:
3868     case DW_OP_breg20:
3869     case DW_OP_breg21:
3870     case DW_OP_breg22:
3871     case DW_OP_breg23:
3872     case DW_OP_breg24:
3873     case DW_OP_breg25:
3874     case DW_OP_breg26:
3875     case DW_OP_breg27:
3876     case DW_OP_breg28:
3877     case DW_OP_breg29:
3878     case DW_OP_breg30:
3879     case DW_OP_breg31:
3880       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
3881       break;
3882     case DW_OP_regx:
3883       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3884       break;
3885     case DW_OP_fbreg:
3886       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
3887       break;
3888     case DW_OP_bregx:
3889       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3890       dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
3891       break;
3892     case DW_OP_piece:
3893       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3894       break;
3895     case DW_OP_deref_size:
3896     case DW_OP_xderef_size:
3897       dw2_asm_output_data (1, val1->v.val_int, NULL);
3898       break;
3899
3900     case INTERNAL_DW_OP_tls_addr:
3901       if (targetm.asm_out.output_dwarf_dtprel)
3902         {
3903           targetm.asm_out.output_dwarf_dtprel (asm_out_file,
3904                                                DWARF2_ADDR_SIZE,
3905                                                val1->v.val_addr);
3906           fputc ('\n', asm_out_file);
3907         }
3908       else
3909         gcc_unreachable ();
3910       break;
3911
3912     default:
3913       /* Other codes have no operands.  */
3914       break;
3915     }
3916 }
3917
3918 /* Output a sequence of location operations.  */
3919
3920 static void
3921 output_loc_sequence (dw_loc_descr_ref loc)
3922 {
3923   for (; loc != NULL; loc = loc->dw_loc_next)
3924     {
3925       /* Output the opcode.  */
3926       dw2_asm_output_data (1, loc->dw_loc_opc,
3927                            "%s", dwarf_stack_op_name (loc->dw_loc_opc));
3928
3929       /* Output the operand(s) (if any).  */
3930       output_loc_operands (loc);
3931     }
3932 }
3933
3934 /* This routine will generate the correct assembly data for a location
3935    description based on a cfi entry with a complex address.  */
3936
3937 static void
3938 output_cfa_loc (dw_cfi_ref cfi)
3939 {
3940   dw_loc_descr_ref loc;
3941   unsigned long size;
3942
3943   if (cfi->dw_cfi_opc == DW_CFA_expression)
3944     dw2_asm_output_data (1, cfi->dw_cfi_oprnd2.dw_cfi_reg_num, NULL);
3945
3946   /* Output the size of the block.  */
3947   loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
3948   size = size_of_locs (loc);
3949   dw2_asm_output_data_uleb128 (size, NULL);
3950
3951   /* Now output the operations themselves.  */
3952   output_loc_sequence (loc);
3953 }
3954
3955 /* This function builds a dwarf location descriptor sequence from a
3956    dw_cfa_location, adding the given OFFSET to the result of the
3957    expression.  */
3958
3959 static struct dw_loc_descr_struct *
3960 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
3961 {
3962   struct dw_loc_descr_struct *head, *tmp;
3963
3964   offset += cfa->offset;
3965
3966   if (cfa->indirect)
3967     {
3968       if (cfa->base_offset)
3969         {
3970           if (cfa->reg <= 31)
3971             head = new_loc_descr (DW_OP_breg0 + cfa->reg, cfa->base_offset, 0);
3972           else
3973             head = new_loc_descr (DW_OP_bregx, cfa->reg, cfa->base_offset);
3974         }
3975       else if (cfa->reg <= 31)
3976         head = new_loc_descr (DW_OP_reg0 + cfa->reg, 0, 0);
3977       else
3978         head = new_loc_descr (DW_OP_regx, cfa->reg, 0);
3979
3980       head->dw_loc_oprnd1.val_class = dw_val_class_const;
3981       tmp = new_loc_descr (DW_OP_deref, 0, 0);
3982       add_loc_descr (&head, tmp);
3983       if (offset != 0)
3984         {
3985           tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
3986           add_loc_descr (&head, tmp);
3987         }
3988     }
3989   else
3990     {
3991       if (offset == 0)
3992         if (cfa->reg <= 31)
3993           head = new_loc_descr (DW_OP_reg0 + cfa->reg, 0, 0);
3994         else
3995           head = new_loc_descr (DW_OP_regx, cfa->reg, 0);
3996       else if (cfa->reg <= 31)
3997         head = new_loc_descr (DW_OP_breg0 + cfa->reg, offset, 0);
3998       else
3999         head = new_loc_descr (DW_OP_bregx, cfa->reg, offset);
4000     }
4001
4002   return head;
4003 }
4004
4005 /* This function builds a dwarf location descriptor sequence for
4006    the address at OFFSET from the CFA when stack is aligned to
4007    ALIGNMENT byte.  */
4008
4009 static struct dw_loc_descr_struct *
4010 build_cfa_aligned_loc (HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
4011 {
4012   struct dw_loc_descr_struct *head;
4013   unsigned int dwarf_fp
4014     = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
4015
4016  /* When CFA is defined as FP+OFFSET, emulate stack alignment.  */
4017   if (cfa.reg == HARD_FRAME_POINTER_REGNUM && cfa.indirect == 0)
4018     {
4019       if (dwarf_fp <= 31)
4020         head = new_loc_descr (DW_OP_breg0 + dwarf_fp, 0, 0);
4021       else
4022         head = new_loc_descr (DW_OP_bregx, dwarf_fp, 0);
4023
4024       add_loc_descr (&head, int_loc_descriptor (alignment));
4025       add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
4026
4027       add_loc_descr (&head, int_loc_descriptor (offset));
4028       add_loc_descr (&head, new_loc_descr (DW_OP_plus, 0, 0));
4029     }
4030   else if (dwarf_fp <= 31)
4031     head = new_loc_descr (DW_OP_breg0 + dwarf_fp, offset, 0);
4032   else
4033     head = new_loc_descr (DW_OP_bregx, dwarf_fp, offset);
4034   return head;
4035 }
4036
4037 /* This function fills in aa dw_cfa_location structure from a dwarf location
4038    descriptor sequence.  */
4039
4040 static void
4041 get_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_struct *loc)
4042 {
4043   struct dw_loc_descr_struct *ptr;
4044   cfa->offset = 0;
4045   cfa->base_offset = 0;
4046   cfa->indirect = 0;
4047   cfa->reg = -1;
4048
4049   for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
4050     {
4051       enum dwarf_location_atom op = ptr->dw_loc_opc;
4052
4053       switch (op)
4054         {
4055         case DW_OP_reg0:
4056         case DW_OP_reg1:
4057         case DW_OP_reg2:
4058         case DW_OP_reg3:
4059         case DW_OP_reg4:
4060         case DW_OP_reg5:
4061         case DW_OP_reg6:
4062         case DW_OP_reg7:
4063         case DW_OP_reg8:
4064         case DW_OP_reg9:
4065         case DW_OP_reg10:
4066         case DW_OP_reg11:
4067         case DW_OP_reg12:
4068         case DW_OP_reg13:
4069         case DW_OP_reg14:
4070         case DW_OP_reg15:
4071         case DW_OP_reg16:
4072         case DW_OP_reg17:
4073         case DW_OP_reg18:
4074         case DW_OP_reg19:
4075         case DW_OP_reg20:
4076         case DW_OP_reg21:
4077         case DW_OP_reg22:
4078         case DW_OP_reg23:
4079         case DW_OP_reg24:
4080         case DW_OP_reg25:
4081         case DW_OP_reg26:
4082         case DW_OP_reg27:
4083         case DW_OP_reg28:
4084         case DW_OP_reg29:
4085         case DW_OP_reg30:
4086         case DW_OP_reg31:
4087           cfa->reg = op - DW_OP_reg0;
4088           break;
4089         case DW_OP_regx:
4090           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
4091           break;
4092         case DW_OP_breg0:
4093         case DW_OP_breg1:
4094         case DW_OP_breg2:
4095         case DW_OP_breg3:
4096         case DW_OP_breg4:
4097         case DW_OP_breg5:
4098         case DW_OP_breg6:
4099         case DW_OP_breg7:
4100         case DW_OP_breg8:
4101         case DW_OP_breg9:
4102         case DW_OP_breg10:
4103         case DW_OP_breg11:
4104         case DW_OP_breg12:
4105         case DW_OP_breg13:
4106         case DW_OP_breg14:
4107         case DW_OP_breg15:
4108         case DW_OP_breg16:
4109         case DW_OP_breg17:
4110         case DW_OP_breg18:
4111         case DW_OP_breg19:
4112         case DW_OP_breg20:
4113         case DW_OP_breg21:
4114         case DW_OP_breg22:
4115         case DW_OP_breg23:
4116         case DW_OP_breg24:
4117         case DW_OP_breg25:
4118         case DW_OP_breg26:
4119         case DW_OP_breg27:
4120         case DW_OP_breg28:
4121         case DW_OP_breg29:
4122         case DW_OP_breg30:
4123         case DW_OP_breg31:
4124           cfa->reg = op - DW_OP_breg0;
4125           cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
4126           break;
4127         case DW_OP_bregx:
4128           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
4129           cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
4130           break;
4131         case DW_OP_deref:
4132           cfa->indirect = 1;
4133           break;
4134         case DW_OP_plus_uconst:
4135           cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
4136           break;
4137         default:
4138           internal_error ("DW_LOC_OP %s not implemented",
4139                           dwarf_stack_op_name (ptr->dw_loc_opc));
4140         }
4141     }
4142 }
4143 #endif /* .debug_frame support */
4144 \f
4145 /* And now, the support for symbolic debugging information.  */
4146 #ifdef DWARF2_DEBUGGING_INFO
4147
4148 /* .debug_str support.  */
4149 static int output_indirect_string (void **, void *);
4150
4151 static void dwarf2out_init (const char *);
4152 static void dwarf2out_finish (const char *);
4153 static void dwarf2out_define (unsigned int, const char *);
4154 static void dwarf2out_undef (unsigned int, const char *);
4155 static void dwarf2out_start_source_file (unsigned, const char *);
4156 static void dwarf2out_end_source_file (unsigned);
4157 static void dwarf2out_begin_block (unsigned, unsigned);
4158 static void dwarf2out_end_block (unsigned, unsigned);
4159 static bool dwarf2out_ignore_block (const_tree);
4160 static void dwarf2out_global_decl (tree);
4161 static void dwarf2out_type_decl (tree, int);
4162 static void dwarf2out_imported_module_or_decl (tree, tree);
4163 static void dwarf2out_abstract_function (tree);
4164 static void dwarf2out_var_location (rtx);
4165 static void dwarf2out_begin_function (tree);
4166
4167 /* The debug hooks structure.  */
4168
4169 const struct gcc_debug_hooks dwarf2_debug_hooks =
4170 {
4171   dwarf2out_init,
4172   dwarf2out_finish,
4173   dwarf2out_define,
4174   dwarf2out_undef,
4175   dwarf2out_start_source_file,
4176   dwarf2out_end_source_file,
4177   dwarf2out_begin_block,
4178   dwarf2out_end_block,
4179   dwarf2out_ignore_block,
4180   dwarf2out_source_line,
4181   dwarf2out_begin_prologue,
4182   debug_nothing_int_charstar,   /* end_prologue */
4183   dwarf2out_end_epilogue,
4184   dwarf2out_begin_function,
4185   debug_nothing_int,            /* end_function */
4186   dwarf2out_decl,               /* function_decl */
4187   dwarf2out_global_decl,
4188   dwarf2out_type_decl,          /* type_decl */
4189   dwarf2out_imported_module_or_decl,
4190   debug_nothing_tree,           /* deferred_inline_function */
4191   /* The DWARF 2 backend tries to reduce debugging bloat by not
4192      emitting the abstract description of inline functions until
4193      something tries to reference them.  */
4194   dwarf2out_abstract_function,  /* outlining_inline_function */
4195   debug_nothing_rtx,            /* label */
4196   debug_nothing_int,            /* handle_pch */
4197   dwarf2out_var_location,
4198   dwarf2out_switch_text_section,
4199   1                             /* start_end_main_source_file */
4200 };
4201 #endif
4202 \f
4203 /* NOTE: In the comments in this file, many references are made to
4204    "Debugging Information Entries".  This term is abbreviated as `DIE'
4205    throughout the remainder of this file.  */
4206
4207 /* An internal representation of the DWARF output is built, and then
4208    walked to generate the DWARF debugging info.  The walk of the internal
4209    representation is done after the entire program has been compiled.
4210    The types below are used to describe the internal representation.  */
4211
4212 /* Various DIE's use offsets relative to the beginning of the
4213    .debug_info section to refer to each other.  */
4214
4215 typedef long int dw_offset;
4216
4217 /* Define typedefs here to avoid circular dependencies.  */
4218
4219 typedef struct dw_attr_struct *dw_attr_ref;
4220 typedef struct dw_line_info_struct *dw_line_info_ref;
4221 typedef struct dw_separate_line_info_struct *dw_separate_line_info_ref;
4222 typedef struct pubname_struct *pubname_ref;
4223 typedef struct dw_ranges_struct *dw_ranges_ref;
4224 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
4225
4226 /* Each entry in the line_info_table maintains the file and
4227    line number associated with the label generated for that
4228    entry.  The label gives the PC value associated with
4229    the line number entry.  */
4230
4231 typedef struct dw_line_info_struct GTY(())
4232 {
4233   unsigned long dw_file_num;
4234   unsigned long dw_line_num;
4235 }
4236 dw_line_info_entry;
4237
4238 /* Line information for functions in separate sections; each one gets its
4239    own sequence.  */
4240 typedef struct dw_separate_line_info_struct GTY(())
4241 {
4242   unsigned long dw_file_num;
4243   unsigned long dw_line_num;
4244   unsigned long function;
4245 }
4246 dw_separate_line_info_entry;
4247
4248 /* Each DIE attribute has a field specifying the attribute kind,
4249    a link to the next attribute in the chain, and an attribute value.
4250    Attributes are typically linked below the DIE they modify.  */
4251
4252 typedef struct dw_attr_struct GTY(())
4253 {
4254   enum dwarf_attribute dw_attr;
4255   dw_val_node dw_attr_val;
4256 }
4257 dw_attr_node;
4258
4259 DEF_VEC_O(dw_attr_node);
4260 DEF_VEC_ALLOC_O(dw_attr_node,gc);
4261
4262 /* The Debugging Information Entry (DIE) structure.  DIEs form a tree.
4263    The children of each node form a circular list linked by
4264    die_sib.  die_child points to the node *before* the "first" child node.  */
4265
4266 typedef struct die_struct GTY((chain_circular ("%h.die_sib")))
4267 {
4268   enum dwarf_tag die_tag;
4269   char *die_symbol;
4270   VEC(dw_attr_node,gc) * die_attr;
4271   dw_die_ref die_parent;
4272   dw_die_ref die_child;
4273   dw_die_ref die_sib;
4274   dw_die_ref die_definition; /* ref from a specification to its definition */
4275   dw_offset die_offset;
4276   unsigned long die_abbrev;
4277   int die_mark;
4278   /* Die is used and must not be pruned as unused.  */
4279   int die_perennial_p;
4280   unsigned int decl_id;
4281 }
4282 die_node;
4283
4284 /* Evaluate 'expr' while 'c' is set to each child of DIE in order.  */
4285 #define FOR_EACH_CHILD(die, c, expr) do {       \
4286   c = die->die_child;                           \
4287   if (c) do {                                   \
4288     c = c->die_sib;                             \
4289     expr;                                       \
4290   } while (c != die->die_child);                \
4291 } while (0)
4292
4293 /* The pubname structure */
4294
4295 typedef struct pubname_struct GTY(())
4296 {
4297   dw_die_ref die;
4298   const char *name;
4299 }
4300 pubname_entry;
4301
4302 DEF_VEC_O(pubname_entry);
4303 DEF_VEC_ALLOC_O(pubname_entry, gc);
4304
4305 struct dw_ranges_struct GTY(())
4306 {
4307   /* If this is positive, it's a block number, otherwise it's a
4308      bitwise-negated index into dw_ranges_by_label.  */
4309   int num;
4310 };
4311
4312 struct dw_ranges_by_label_struct GTY(())
4313 {
4314   const char *begin;
4315   const char *end;
4316 };
4317
4318 /* The limbo die list structure.  */
4319 typedef struct limbo_die_struct GTY(())
4320 {
4321   dw_die_ref die;
4322   tree created_for;
4323   struct limbo_die_struct *next;
4324 }
4325 limbo_die_node;
4326
4327 /* How to start an assembler comment.  */
4328 #ifndef ASM_COMMENT_START
4329 #define ASM_COMMENT_START ";#"
4330 #endif
4331
4332 /* Define a macro which returns nonzero for a TYPE_DECL which was
4333    implicitly generated for a tagged type.
4334
4335    Note that unlike the gcc front end (which generates a NULL named
4336    TYPE_DECL node for each complete tagged type, each array type, and
4337    each function type node created) the g++ front end generates a
4338    _named_ TYPE_DECL node for each tagged type node created.
4339    These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
4340    generate a DW_TAG_typedef DIE for them.  */
4341
4342 #define TYPE_DECL_IS_STUB(decl)                         \
4343   (DECL_NAME (decl) == NULL_TREE                        \
4344    || (DECL_ARTIFICIAL (decl)                           \
4345        && is_tagged_type (TREE_TYPE (decl))             \
4346        && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl)))  \
4347            /* This is necessary for stub decls that     \
4348               appear in nested inline functions.  */    \
4349            || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
4350                && (decl_ultimate_origin (decl)          \
4351                    == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
4352
4353 /* Information concerning the compilation unit's programming
4354    language, and compiler version.  */
4355
4356 /* Fixed size portion of the DWARF compilation unit header.  */
4357 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
4358   (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
4359
4360 /* Fixed size portion of public names info.  */
4361 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
4362
4363 /* Fixed size portion of the address range info.  */
4364 #define DWARF_ARANGES_HEADER_SIZE                                       \
4365   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4,      \
4366                 DWARF2_ADDR_SIZE * 2)                                   \
4367    - DWARF_INITIAL_LENGTH_SIZE)
4368
4369 /* Size of padding portion in the address range info.  It must be
4370    aligned to twice the pointer size.  */
4371 #define DWARF_ARANGES_PAD_SIZE \
4372   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
4373                 DWARF2_ADDR_SIZE * 2)                              \
4374    - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
4375
4376 /* Use assembler line directives if available.  */
4377 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
4378 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
4379 #define DWARF2_ASM_LINE_DEBUG_INFO 1
4380 #else
4381 #define DWARF2_ASM_LINE_DEBUG_INFO 0
4382 #endif
4383 #endif
4384
4385 /* Minimum line offset in a special line info. opcode.
4386    This value was chosen to give a reasonable range of values.  */
4387 #define DWARF_LINE_BASE  -10
4388
4389 /* First special line opcode - leave room for the standard opcodes.  */
4390 #define DWARF_LINE_OPCODE_BASE  10
4391
4392 /* Range of line offsets in a special line info. opcode.  */
4393 #define DWARF_LINE_RANGE  (254-DWARF_LINE_OPCODE_BASE+1)
4394
4395 /* Flag that indicates the initial value of the is_stmt_start flag.
4396    In the present implementation, we do not mark any lines as
4397    the beginning of a source statement, because that information
4398    is not made available by the GCC front-end.  */
4399 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
4400
4401 #ifdef DWARF2_DEBUGGING_INFO
4402 /* This location is used by calc_die_sizes() to keep track
4403    the offset of each DIE within the .debug_info section.  */
4404 static unsigned long next_die_offset;
4405 #endif
4406
4407 /* Record the root of the DIE's built for the current compilation unit.  */
4408 static GTY(()) dw_die_ref comp_unit_die;
4409
4410 /* A list of DIEs with a NULL parent waiting to be relocated.  */
4411 static GTY(()) limbo_die_node *limbo_die_list;
4412
4413 /* Filenames referenced by this compilation unit.  */
4414 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
4415
4416 /* A hash table of references to DIE's that describe declarations.
4417    The key is a DECL_UID() which is a unique number identifying each decl.  */
4418 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
4419
4420 /* Node of the variable location list.  */
4421 struct var_loc_node GTY ((chain_next ("%h.next")))
4422 {
4423   rtx GTY (()) var_loc_note;
4424   const char * GTY (()) label;
4425   const char * GTY (()) section_label;
4426   struct var_loc_node * GTY (()) next;
4427 };
4428
4429 /* Variable location list.  */
4430 struct var_loc_list_def GTY (())
4431 {
4432   struct var_loc_node * GTY (()) first;
4433
4434   /* Do not mark the last element of the chained list because
4435      it is marked through the chain.  */
4436   struct var_loc_node * GTY ((skip ("%h"))) last;
4437
4438   /* DECL_UID of the variable decl.  */
4439   unsigned int decl_id;
4440 };
4441 typedef struct var_loc_list_def var_loc_list;
4442
4443
4444 /* Table of decl location linked lists.  */
4445 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
4446
4447 /* A pointer to the base of a list of references to DIE's that
4448    are uniquely identified by their tag, presence/absence of
4449    children DIE's, and list of attribute/value pairs.  */
4450 static GTY((length ("abbrev_die_table_allocated")))
4451   dw_die_ref *abbrev_die_table;
4452
4453 /* Number of elements currently allocated for abbrev_die_table.  */
4454 static GTY(()) unsigned abbrev_die_table_allocated;
4455
4456 /* Number of elements in type_die_table currently in use.  */
4457 static GTY(()) unsigned abbrev_die_table_in_use;
4458
4459 /* Size (in elements) of increments by which we may expand the
4460    abbrev_die_table.  */
4461 #define ABBREV_DIE_TABLE_INCREMENT 256
4462
4463 /* A pointer to the base of a table that contains line information
4464    for each source code line in .text in the compilation unit.  */
4465 static GTY((length ("line_info_table_allocated")))
4466      dw_line_info_ref line_info_table;
4467
4468 /* Number of elements currently allocated for line_info_table.  */
4469 static GTY(()) unsigned line_info_table_allocated;
4470
4471 /* Number of elements in line_info_table currently in use.  */
4472 static GTY(()) unsigned line_info_table_in_use;
4473
4474 /* A pointer to the base of a table that contains line information
4475    for each source code line outside of .text in the compilation unit.  */
4476 static GTY ((length ("separate_line_info_table_allocated")))
4477      dw_separate_line_info_ref separate_line_info_table;
4478
4479 /* Number of elements currently allocated for separate_line_info_table.  */
4480 static GTY(()) unsigned separate_line_info_table_allocated;
4481
4482 /* Number of elements in separate_line_info_table currently in use.  */
4483 static GTY(()) unsigned separate_line_info_table_in_use;
4484
4485 /* Size (in elements) of increments by which we may expand the
4486    line_info_table.  */
4487 #define LINE_INFO_TABLE_INCREMENT 1024
4488
4489 /* A pointer to the base of a table that contains a list of publicly
4490    accessible names.  */
4491 static GTY (()) VEC (pubname_entry, gc) *  pubname_table;
4492
4493 /* A pointer to the base of a table that contains a list of publicly
4494    accessible types.  */
4495 static GTY (()) VEC (pubname_entry, gc) * pubtype_table;
4496
4497 /* Array of dies for which we should generate .debug_arange info.  */
4498 static GTY((length ("arange_table_allocated"))) dw_die_ref *arange_table;
4499
4500 /* Number of elements currently allocated for arange_table.  */
4501 static GTY(()) unsigned arange_table_allocated;
4502
4503 /* Number of elements in arange_table currently in use.  */
4504 static GTY(()) unsigned arange_table_in_use;
4505
4506 /* Size (in elements) of increments by which we may expand the
4507    arange_table.  */
4508 #define ARANGE_TABLE_INCREMENT 64
4509
4510 /* Array of dies for which we should generate .debug_ranges info.  */
4511 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
4512
4513 /* Number of elements currently allocated for ranges_table.  */
4514 static GTY(()) unsigned ranges_table_allocated;
4515
4516 /* Number of elements in ranges_table currently in use.  */
4517 static GTY(()) unsigned ranges_table_in_use;
4518
4519 /* Array of pairs of labels referenced in ranges_table.  */
4520 static GTY ((length ("ranges_by_label_allocated")))
4521      dw_ranges_by_label_ref ranges_by_label;
4522
4523 /* Number of elements currently allocated for ranges_by_label.  */
4524 static GTY(()) unsigned ranges_by_label_allocated;
4525
4526 /* Number of elements in ranges_by_label currently in use.  */
4527 static GTY(()) unsigned ranges_by_label_in_use;
4528
4529 /* Size (in elements) of increments by which we may expand the
4530    ranges_table.  */
4531 #define RANGES_TABLE_INCREMENT 64
4532
4533 /* Whether we have location lists that need outputting */
4534 static GTY(()) bool have_location_lists;
4535
4536 /* Unique label counter.  */
4537 static GTY(()) unsigned int loclabel_num;
4538
4539 #ifdef DWARF2_DEBUGGING_INFO
4540 /* Record whether the function being analyzed contains inlined functions.  */
4541 static int current_function_has_inlines;
4542 #endif
4543 #if 0 && defined (MIPS_DEBUGGING_INFO)
4544 static int comp_unit_has_inlines;
4545 #endif
4546
4547 /* The last file entry emitted by maybe_emit_file().  */
4548 static GTY(()) struct dwarf_file_data * last_emitted_file;
4549
4550 /* Number of internal labels generated by gen_internal_sym().  */
4551 static GTY(()) int label_num;
4552
4553 /* Cached result of previous call to lookup_filename.  */
4554 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
4555
4556 #ifdef DWARF2_DEBUGGING_INFO
4557
4558 /* Offset from the "steady-state frame pointer" to the frame base,
4559    within the current function.  */
4560 static HOST_WIDE_INT frame_pointer_fb_offset;
4561
4562 /* Forward declarations for functions defined in this file.  */
4563
4564 static int is_pseudo_reg (const_rtx);
4565 static tree type_main_variant (tree);
4566 static int is_tagged_type (const_tree);
4567 static const char *dwarf_tag_name (unsigned);
4568 static const char *dwarf_attr_name (unsigned);
4569 static const char *dwarf_form_name (unsigned);
4570 static tree decl_ultimate_origin (const_tree);
4571 static tree block_ultimate_origin (const_tree);
4572 static tree decl_class_context (tree);
4573 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
4574 static inline enum dw_val_class AT_class (dw_attr_ref);
4575 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
4576 static inline unsigned AT_flag (dw_attr_ref);
4577 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
4578 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
4579 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
4580 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
4581 static void add_AT_long_long (dw_die_ref, enum dwarf_attribute, unsigned long,
4582                               unsigned long);
4583 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
4584                                unsigned int, unsigned char *);
4585 static hashval_t debug_str_do_hash (const void *);
4586 static int debug_str_eq (const void *, const void *);
4587 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
4588 static inline const char *AT_string (dw_attr_ref);
4589 static int AT_string_form (dw_attr_ref);
4590 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
4591 static void add_AT_specification (dw_die_ref, dw_die_ref);
4592 static inline dw_die_ref AT_ref (dw_attr_ref);
4593 static inline int AT_ref_external (dw_attr_ref);
4594 static inline void set_AT_ref_external (dw_attr_ref, int);
4595 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
4596 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
4597 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
4598 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
4599                              dw_loc_list_ref);
4600 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
4601 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
4602 static inline rtx AT_addr (dw_attr_ref);
4603 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
4604 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
4605 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
4606 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
4607                            unsigned HOST_WIDE_INT);
4608 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
4609                                unsigned long);
4610 static inline const char *AT_lbl (dw_attr_ref);
4611 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
4612 static const char *get_AT_low_pc (dw_die_ref);
4613 static const char *get_AT_hi_pc (dw_die_ref);
4614 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
4615 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
4616 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
4617 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
4618 static bool is_c_family (void);
4619 static bool is_cxx (void);
4620 static bool is_java (void);
4621 static bool is_fortran (void);
4622 static bool is_ada (void);
4623 static void remove_AT (dw_die_ref, enum dwarf_attribute);
4624 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
4625 static void add_child_die (dw_die_ref, dw_die_ref);
4626 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
4627 static dw_die_ref lookup_type_die (tree);
4628 static void equate_type_number_to_die (tree, dw_die_ref);
4629 static hashval_t decl_die_table_hash (const void *);
4630 static int decl_die_table_eq (const void *, const void *);
4631 static dw_die_ref lookup_decl_die (tree);
4632 static hashval_t decl_loc_table_hash (const void *);
4633 static int decl_loc_table_eq (const void *, const void *);
4634 static var_loc_list *lookup_decl_loc (const_tree);
4635 static void equate_decl_number_to_die (tree, dw_die_ref);
4636 static void add_var_loc_to_decl (tree, struct var_loc_node *);
4637 static void print_spaces (FILE *);
4638 static void print_die (dw_die_ref, FILE *);
4639 static void print_dwarf_line_table (FILE *);
4640 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
4641 static dw_die_ref pop_compile_unit (dw_die_ref);
4642 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
4643 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
4644 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
4645 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
4646 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
4647 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
4648 static int same_die_p (dw_die_ref, dw_die_ref, int *);
4649 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
4650 static void compute_section_prefix (dw_die_ref);
4651 static int is_type_die (dw_die_ref);
4652 static int is_comdat_die (dw_die_ref);
4653 static int is_symbol_die (dw_die_ref);
4654 static void assign_symbol_names (dw_die_ref);
4655 static void break_out_includes (dw_die_ref);
4656 static hashval_t htab_cu_hash (const void *);
4657 static int htab_cu_eq (const void *, const void *);
4658 static void htab_cu_del (void *);
4659 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
4660 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
4661 static void add_sibling_attributes (dw_die_ref);
4662 static void build_abbrev_table (dw_die_ref);
4663 static void output_location_lists (dw_die_ref);
4664 static int constant_size (long unsigned);
4665 static unsigned long size_of_die (dw_die_ref);
4666 static void calc_die_sizes (dw_die_ref);
4667 static void mark_dies (dw_die_ref);
4668 static void unmark_dies (dw_die_ref);
4669 static void unmark_all_dies (dw_die_ref);
4670 static unsigned long size_of_pubnames (VEC (pubname_entry,gc) *);
4671 static unsigned long size_of_aranges (void);
4672 static enum dwarf_form value_format (dw_attr_ref);
4673 static void output_value_format (dw_attr_ref);
4674 static void output_abbrev_section (void);
4675 static void output_die_symbol (dw_die_ref);
4676 static void output_die (dw_die_ref);
4677 static void output_compilation_unit_header (void);
4678 static void output_comp_unit (dw_die_ref, int);
4679 static const char *dwarf2_name (tree, int);
4680 static void add_pubname (tree, dw_die_ref);
4681 static void add_pubname_string (const char *, dw_die_ref);
4682 static void add_pubtype (tree, dw_die_ref);
4683 static void output_pubnames (VEC (pubname_entry,gc) *);
4684 static void add_arange (tree, dw_die_ref);
4685 static void output_aranges (void);
4686 static unsigned int add_ranges_num (int);
4687 static unsigned int add_ranges (const_tree);
4688 static unsigned int add_ranges_by_labels (const char *, const char *);
4689 static void output_ranges (void);
4690 static void output_line_info (void);
4691 static void output_file_names (void);
4692 static dw_die_ref base_type_die (tree);
4693 static int is_base_type (tree);
4694 static bool is_subrange_type (const_tree);
4695 static dw_die_ref subrange_type_die (tree, dw_die_ref);
4696 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
4697 static int type_is_enum (const_tree);
4698 static unsigned int dbx_reg_number (const_rtx);
4699 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
4700 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
4701 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
4702                                                 enum var_init_status);
4703 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
4704                                                      enum var_init_status);
4705 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
4706                                          enum var_init_status);
4707 static int is_based_loc (const_rtx);
4708 static dw_loc_descr_ref mem_loc_descriptor (rtx, enum machine_mode mode,
4709                                             enum var_init_status);
4710 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
4711                                                enum var_init_status);
4712 static dw_loc_descr_ref loc_descriptor (rtx, enum var_init_status);
4713 static dw_loc_descr_ref loc_descriptor_from_tree_1 (tree, int);
4714 static dw_loc_descr_ref loc_descriptor_from_tree (tree);
4715 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
4716 static tree field_type (const_tree);
4717 static unsigned int simple_type_align_in_bits (const_tree);
4718 static unsigned int simple_decl_align_in_bits (const_tree);
4719 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
4720 static HOST_WIDE_INT field_byte_offset (const_tree);
4721 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
4722                                          dw_loc_descr_ref);
4723 static void add_data_member_location_attribute (dw_die_ref, tree);
4724 static void add_const_value_attribute (dw_die_ref, rtx);
4725 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
4726 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
4727 static void insert_float (const_rtx, unsigned char *);
4728 static rtx rtl_for_decl_location (tree);
4729 static void add_location_or_const_value_attribute (dw_die_ref, tree,
4730                                                    enum dwarf_attribute);
4731 static void tree_add_const_value_attribute (dw_die_ref, tree);
4732 static void add_name_attribute (dw_die_ref, const char *);
4733 static void add_comp_dir_attribute (dw_die_ref);
4734 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
4735 static void add_subscript_info (dw_die_ref, tree, bool);
4736 static void add_byte_size_attribute (dw_die_ref, tree);
4737 static void add_bit_offset_attribute (dw_die_ref, tree);
4738 static void add_bit_size_attribute (dw_die_ref, tree);
4739 static void add_prototyped_attribute (dw_die_ref, tree);
4740 static void add_abstract_origin_attribute (dw_die_ref, tree);
4741 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
4742 static void add_src_coords_attributes (dw_die_ref, tree);
4743 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
4744 static void push_decl_scope (tree);
4745 static void pop_decl_scope (void);
4746 static dw_die_ref scope_die_for (tree, dw_die_ref);
4747 static inline int local_scope_p (dw_die_ref);
4748 static inline int class_or_namespace_scope_p (dw_die_ref);
4749 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
4750 static void add_calling_convention_attribute (dw_die_ref, tree);
4751 static const char *type_tag (const_tree);
4752 static tree member_declared_type (const_tree);
4753 #if 0
4754 static const char *decl_start_label (tree);
4755 #endif
4756 static void gen_array_type_die (tree, dw_die_ref);
4757 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
4758 #if 0
4759 static void gen_entry_point_die (tree, dw_die_ref);
4760 #endif
4761 static void gen_inlined_enumeration_type_die (tree, dw_die_ref);
4762 static void gen_inlined_structure_type_die (tree, dw_die_ref);
4763 static void gen_inlined_union_type_die (tree, dw_die_ref);
4764 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
4765 static dw_die_ref gen_formal_parameter_die (tree, dw_die_ref);
4766 static void gen_unspecified_parameters_die (tree, dw_die_ref);
4767 static void gen_formal_types_die (tree, dw_die_ref);
4768 static void gen_subprogram_die (tree, dw_die_ref);
4769 static void gen_variable_die (tree, dw_die_ref);
4770 static void gen_label_die (tree, dw_die_ref);
4771 static void gen_lexical_block_die (tree, dw_die_ref, int);
4772 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
4773 static void gen_field_die (tree, dw_die_ref);
4774 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
4775 static dw_die_ref gen_compile_unit_die (const char *);
4776 static void gen_inheritance_die (tree, tree, dw_die_ref);
4777 static void gen_member_die (tree, dw_die_ref);
4778 static void gen_struct_or_union_type_die (tree, dw_die_ref,
4779                                                 enum debug_info_usage);
4780 static void gen_subroutine_type_die (tree, dw_die_ref);
4781 static void gen_typedef_die (tree, dw_die_ref);
4782 static void gen_type_die (tree, dw_die_ref);
4783 static void gen_tagged_type_instantiation_die (tree, dw_die_ref);
4784 static void gen_block_die (tree, dw_die_ref, int);
4785 static void decls_for_scope (tree, dw_die_ref, int);
4786 static int is_redundant_typedef (const_tree);
4787 static void gen_namespace_die (tree);
4788 static void gen_decl_die (tree, dw_die_ref);
4789 static dw_die_ref force_decl_die (tree);
4790 static dw_die_ref force_type_die (tree);
4791 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
4792 static void declare_in_namespace (tree, dw_die_ref);
4793 static struct dwarf_file_data * lookup_filename (const char *);
4794 static void retry_incomplete_types (void);
4795 static void gen_type_die_for_member (tree, tree, dw_die_ref);
4796 static void splice_child_die (dw_die_ref, dw_die_ref);
4797 static int file_info_cmp (const void *, const void *);
4798 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
4799                                      const char *, const char *, unsigned);
4800 static void add_loc_descr_to_loc_list (dw_loc_list_ref *, dw_loc_descr_ref,
4801                                        const char *, const char *,
4802                                        const char *);
4803 static void output_loc_list (dw_loc_list_ref);
4804 static char *gen_internal_sym (const char *);
4805
4806 static void prune_unmark_dies (dw_die_ref);
4807 static void prune_unused_types_mark (dw_die_ref, int);
4808 static void prune_unused_types_walk (dw_die_ref);
4809 static void prune_unused_types_walk_attribs (dw_die_ref);
4810 static void prune_unused_types_prune (dw_die_ref);
4811 static void prune_unused_types (void);
4812 static int maybe_emit_file (struct dwarf_file_data *fd);
4813
4814 /* Section names used to hold DWARF debugging information.  */
4815 #ifndef DEBUG_INFO_SECTION
4816 #define DEBUG_INFO_SECTION      ".debug_info"
4817 #endif
4818 #ifndef DEBUG_ABBREV_SECTION
4819 #define DEBUG_ABBREV_SECTION    ".debug_abbrev"
4820 #endif
4821 #ifndef DEBUG_ARANGES_SECTION
4822 #define DEBUG_ARANGES_SECTION   ".debug_aranges"
4823 #endif
4824 #ifndef DEBUG_MACINFO_SECTION
4825 #define DEBUG_MACINFO_SECTION   ".debug_macinfo"
4826 #endif
4827 #ifndef DEBUG_LINE_SECTION
4828 #define DEBUG_LINE_SECTION      ".debug_line"
4829 #endif
4830 #ifndef DEBUG_LOC_SECTION
4831 #define DEBUG_LOC_SECTION       ".debug_loc"
4832 #endif
4833 #ifndef DEBUG_PUBNAMES_SECTION
4834 #define DEBUG_PUBNAMES_SECTION  ".debug_pubnames"
4835 #endif
4836 #ifndef DEBUG_STR_SECTION
4837 #define DEBUG_STR_SECTION       ".debug_str"
4838 #endif
4839 #ifndef DEBUG_RANGES_SECTION
4840 #define DEBUG_RANGES_SECTION    ".debug_ranges"
4841 #endif
4842
4843 /* Standard ELF section names for compiled code and data.  */
4844 #ifndef TEXT_SECTION_NAME
4845 #define TEXT_SECTION_NAME       ".text"
4846 #endif
4847
4848 /* Section flags for .debug_str section.  */
4849 #define DEBUG_STR_SECTION_FLAGS \
4850   (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings               \
4851    ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1        \
4852    : SECTION_DEBUG)
4853
4854 /* Labels we insert at beginning sections we can reference instead of
4855    the section names themselves.  */
4856
4857 #ifndef TEXT_SECTION_LABEL
4858 #define TEXT_SECTION_LABEL              "Ltext"
4859 #endif
4860 #ifndef COLD_TEXT_SECTION_LABEL
4861 #define COLD_TEXT_SECTION_LABEL         "Ltext_cold"
4862 #endif
4863 #ifndef DEBUG_LINE_SECTION_LABEL
4864 #define DEBUG_LINE_SECTION_LABEL        "Ldebug_line"
4865 #endif
4866 #ifndef DEBUG_INFO_SECTION_LABEL
4867 #define DEBUG_INFO_SECTION_LABEL        "Ldebug_info"
4868 #endif
4869 #ifndef DEBUG_ABBREV_SECTION_LABEL
4870 #define DEBUG_ABBREV_SECTION_LABEL      "Ldebug_abbrev"
4871 #endif
4872 #ifndef DEBUG_LOC_SECTION_LABEL
4873 #define DEBUG_LOC_SECTION_LABEL         "Ldebug_loc"
4874 #endif
4875 #ifndef DEBUG_RANGES_SECTION_LABEL
4876 #define DEBUG_RANGES_SECTION_LABEL      "Ldebug_ranges"
4877 #endif
4878 #ifndef DEBUG_MACINFO_SECTION_LABEL
4879 #define DEBUG_MACINFO_SECTION_LABEL     "Ldebug_macinfo"
4880 #endif
4881
4882 /* Definitions of defaults for formats and names of various special
4883    (artificial) labels which may be generated within this file (when the -g
4884    options is used and DWARF2_DEBUGGING_INFO is in effect.
4885    If necessary, these may be overridden from within the tm.h file, but
4886    typically, overriding these defaults is unnecessary.  */
4887
4888 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4889 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4890 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4891 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4892 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4893 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4894 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4895 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4896 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4897 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
4898
4899 #ifndef TEXT_END_LABEL
4900 #define TEXT_END_LABEL          "Letext"
4901 #endif
4902 #ifndef COLD_END_LABEL
4903 #define COLD_END_LABEL          "Letext_cold"
4904 #endif
4905 #ifndef BLOCK_BEGIN_LABEL
4906 #define BLOCK_BEGIN_LABEL       "LBB"
4907 #endif
4908 #ifndef BLOCK_END_LABEL
4909 #define BLOCK_END_LABEL         "LBE"
4910 #endif
4911 #ifndef LINE_CODE_LABEL
4912 #define LINE_CODE_LABEL         "LM"
4913 #endif
4914 #ifndef SEPARATE_LINE_CODE_LABEL
4915 #define SEPARATE_LINE_CODE_LABEL        "LSM"
4916 #endif
4917
4918 \f
4919 /* We allow a language front-end to designate a function that is to be
4920    called to "demangle" any name before it is put into a DIE.  */
4921
4922 static const char *(*demangle_name_func) (const char *);
4923
4924 void
4925 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
4926 {
4927   demangle_name_func = func;
4928 }
4929
4930 /* Test if rtl node points to a pseudo register.  */
4931
4932 static inline int
4933 is_pseudo_reg (const_rtx rtl)
4934 {
4935   return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
4936           || (GET_CODE (rtl) == SUBREG
4937               && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
4938 }
4939
4940 /* Return a reference to a type, with its const and volatile qualifiers
4941    removed.  */
4942
4943 static inline tree
4944 type_main_variant (tree type)
4945 {
4946   type = TYPE_MAIN_VARIANT (type);
4947
4948   /* ??? There really should be only one main variant among any group of
4949      variants of a given type (and all of the MAIN_VARIANT values for all
4950      members of the group should point to that one type) but sometimes the C
4951      front-end messes this up for array types, so we work around that bug
4952      here.  */
4953   if (TREE_CODE (type) == ARRAY_TYPE)
4954     while (type != TYPE_MAIN_VARIANT (type))
4955       type = TYPE_MAIN_VARIANT (type);
4956
4957   return type;
4958 }
4959
4960 /* Return nonzero if the given type node represents a tagged type.  */
4961
4962 static inline int
4963 is_tagged_type (const_tree type)
4964 {
4965   enum tree_code code = TREE_CODE (type);
4966
4967   return (code == RECORD_TYPE || code == UNION_TYPE
4968           || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
4969 }
4970
4971 /* Convert a DIE tag into its string name.  */
4972
4973 static const char *
4974 dwarf_tag_name (unsigned int tag)
4975 {
4976   switch (tag)
4977     {
4978     case DW_TAG_padding:
4979       return "DW_TAG_padding";
4980     case DW_TAG_array_type:
4981       return "DW_TAG_array_type";
4982     case DW_TAG_class_type:
4983       return "DW_TAG_class_type";
4984     case DW_TAG_entry_point:
4985       return "DW_TAG_entry_point";
4986     case DW_TAG_enumeration_type:
4987       return "DW_TAG_enumeration_type";
4988     case DW_TAG_formal_parameter:
4989       return "DW_TAG_formal_parameter";
4990     case DW_TAG_imported_declaration:
4991       return "DW_TAG_imported_declaration";
4992     case DW_TAG_label:
4993       return "DW_TAG_label";
4994     case DW_TAG_lexical_block:
4995       return "DW_TAG_lexical_block";
4996     case DW_TAG_member:
4997       return "DW_TAG_member";
4998     case DW_TAG_pointer_type:
4999       return "DW_TAG_pointer_type";
5000     case DW_TAG_reference_type:
5001       return "DW_TAG_reference_type";
5002     case DW_TAG_compile_unit:
5003       return "DW_TAG_compile_unit";
5004     case DW_TAG_string_type:
5005       return "DW_TAG_string_type";
5006     case DW_TAG_structure_type:
5007       return "DW_TAG_structure_type";
5008     case DW_TAG_subroutine_type:
5009       return "DW_TAG_subroutine_type";
5010     case DW_TAG_typedef:
5011       return "DW_TAG_typedef";
5012     case DW_TAG_union_type:
5013       return "DW_TAG_union_type";
5014     case DW_TAG_unspecified_parameters:
5015       return "DW_TAG_unspecified_parameters";
5016     case DW_TAG_variant:
5017       return "DW_TAG_variant";
5018     case DW_TAG_common_block:
5019       return "DW_TAG_common_block";
5020     case DW_TAG_common_inclusion:
5021       return "DW_TAG_common_inclusion";
5022     case DW_TAG_inheritance:
5023       return "DW_TAG_inheritance";
5024     case DW_TAG_inlined_subroutine:
5025       return "DW_TAG_inlined_subroutine";
5026     case DW_TAG_module:
5027       return "DW_TAG_module";
5028     case DW_TAG_ptr_to_member_type:
5029       return "DW_TAG_ptr_to_member_type";
5030     case DW_TAG_set_type:
5031       return "DW_TAG_set_type";
5032     case DW_TAG_subrange_type:
5033       return "DW_TAG_subrange_type";
5034     case DW_TAG_with_stmt:
5035       return "DW_TAG_with_stmt";
5036     case DW_TAG_access_declaration:
5037       return "DW_TAG_access_declaration";
5038     case DW_TAG_base_type:
5039       return "DW_TAG_base_type";
5040     case DW_TAG_catch_block:
5041       return "DW_TAG_catch_block";
5042     case DW_TAG_const_type:
5043       return "DW_TAG_const_type";
5044     case DW_TAG_constant:
5045       return "DW_TAG_constant";
5046     case DW_TAG_enumerator:
5047       return "DW_TAG_enumerator";
5048     case DW_TAG_file_type:
5049       return "DW_TAG_file_type";
5050     case DW_TAG_friend:
5051       return "DW_TAG_friend";
5052     case DW_TAG_namelist:
5053       return "DW_TAG_namelist";
5054     case DW_TAG_namelist_item:
5055       return "DW_TAG_namelist_item";
5056     case DW_TAG_packed_type:
5057       return "DW_TAG_packed_type";
5058     case DW_TAG_subprogram:
5059       return "DW_TAG_subprogram";
5060     case DW_TAG_template_type_param:
5061       return "DW_TAG_template_type_param";
5062     case DW_TAG_template_value_param:
5063       return "DW_TAG_template_value_param";
5064     case DW_TAG_thrown_type:
5065       return "DW_TAG_thrown_type";
5066     case DW_TAG_try_block:
5067       return "DW_TAG_try_block";
5068     case DW_TAG_variant_part:
5069       return "DW_TAG_variant_part";
5070     case DW_TAG_variable:
5071       return "DW_TAG_variable";
5072     case DW_TAG_volatile_type:
5073       return "DW_TAG_volatile_type";
5074     case DW_TAG_dwarf_procedure:
5075       return "DW_TAG_dwarf_procedure";
5076     case DW_TAG_restrict_type:
5077       return "DW_TAG_restrict_type";
5078     case DW_TAG_interface_type:
5079       return "DW_TAG_interface_type";
5080     case DW_TAG_namespace:
5081       return "DW_TAG_namespace";
5082     case DW_TAG_imported_module:
5083       return "DW_TAG_imported_module";
5084     case DW_TAG_unspecified_type:
5085       return "DW_TAG_unspecified_type";
5086     case DW_TAG_partial_unit:
5087       return "DW_TAG_partial_unit";
5088     case DW_TAG_imported_unit:
5089       return "DW_TAG_imported_unit";
5090     case DW_TAG_condition:
5091       return "DW_TAG_condition";
5092     case DW_TAG_shared_type:
5093       return "DW_TAG_shared_type";
5094     case DW_TAG_MIPS_loop:
5095       return "DW_TAG_MIPS_loop";
5096     case DW_TAG_format_label:
5097       return "DW_TAG_format_label";
5098     case DW_TAG_function_template:
5099       return "DW_TAG_function_template";
5100     case DW_TAG_class_template:
5101       return "DW_TAG_class_template";
5102     case DW_TAG_GNU_BINCL:
5103       return "DW_TAG_GNU_BINCL";
5104     case DW_TAG_GNU_EINCL:
5105       return "DW_TAG_GNU_EINCL";
5106     default:
5107       return "DW_TAG_<unknown>";
5108     }
5109 }
5110
5111 /* Convert a DWARF attribute code into its string name.  */
5112
5113 static const char *
5114 dwarf_attr_name (unsigned int attr)
5115 {
5116   switch (attr)
5117     {
5118     case DW_AT_sibling:
5119       return "DW_AT_sibling";
5120     case DW_AT_location:
5121       return "DW_AT_location";
5122     case DW_AT_name:
5123       return "DW_AT_name";
5124     case DW_AT_ordering:
5125       return "DW_AT_ordering";
5126     case DW_AT_subscr_data:
5127       return "DW_AT_subscr_data";
5128     case DW_AT_byte_size:
5129       return "DW_AT_byte_size";
5130     case DW_AT_bit_offset:
5131       return "DW_AT_bit_offset";
5132     case DW_AT_bit_size:
5133       return "DW_AT_bit_size";
5134     case DW_AT_element_list:
5135       return "DW_AT_element_list";
5136     case DW_AT_stmt_list:
5137       return "DW_AT_stmt_list";
5138     case DW_AT_low_pc:
5139       return "DW_AT_low_pc";
5140     case DW_AT_high_pc:
5141       return "DW_AT_high_pc";
5142     case DW_AT_language:
5143       return "DW_AT_language";
5144     case DW_AT_member:
5145       return "DW_AT_member";
5146     case DW_AT_discr:
5147       return "DW_AT_discr";
5148     case DW_AT_discr_value:
5149       return "DW_AT_discr_value";
5150     case DW_AT_visibility:
5151       return "DW_AT_visibility";
5152     case DW_AT_import:
5153       return "DW_AT_import";
5154     case DW_AT_string_length:
5155       return "DW_AT_string_length";
5156     case DW_AT_common_reference:
5157       return "DW_AT_common_reference";
5158     case DW_AT_comp_dir:
5159       return "DW_AT_comp_dir";
5160     case DW_AT_const_value:
5161       return "DW_AT_const_value";
5162     case DW_AT_containing_type:
5163       return "DW_AT_containing_type";
5164     case DW_AT_default_value:
5165       return "DW_AT_default_value";
5166     case DW_AT_inline:
5167       return "DW_AT_inline";
5168     case DW_AT_is_optional:
5169       return "DW_AT_is_optional";
5170     case DW_AT_lower_bound:
5171       return "DW_AT_lower_bound";
5172     case DW_AT_producer:
5173       return "DW_AT_producer";
5174     case DW_AT_prototyped:
5175       return "DW_AT_prototyped";
5176     case DW_AT_return_addr:
5177       return "DW_AT_return_addr";
5178     case DW_AT_start_scope:
5179       return "DW_AT_start_scope";
5180     case DW_AT_bit_stride:
5181       return "DW_AT_bit_stride";
5182     case DW_AT_upper_bound:
5183       return "DW_AT_upper_bound";
5184     case DW_AT_abstract_origin:
5185       return "DW_AT_abstract_origin";
5186     case DW_AT_accessibility:
5187       return "DW_AT_accessibility";
5188     case DW_AT_address_class:
5189       return "DW_AT_address_class";
5190     case DW_AT_artificial:
5191       return "DW_AT_artificial";
5192     case DW_AT_base_types:
5193       return "DW_AT_base_types";
5194     case DW_AT_calling_convention:
5195       return "DW_AT_calling_convention";
5196     case DW_AT_count:
5197       return "DW_AT_count";
5198     case DW_AT_data_member_location:
5199       return "DW_AT_data_member_location";
5200     case DW_AT_decl_column:
5201       return "DW_AT_decl_column";
5202     case DW_AT_decl_file:
5203       return "DW_AT_decl_file";
5204     case DW_AT_decl_line:
5205       return "DW_AT_decl_line";
5206     case DW_AT_declaration:
5207       return "DW_AT_declaration";
5208     case DW_AT_discr_list:
5209       return "DW_AT_discr_list";
5210     case DW_AT_encoding:
5211       return "DW_AT_encoding";
5212     case DW_AT_external:
5213       return "DW_AT_external";
5214     case DW_AT_frame_base:
5215       return "DW_AT_frame_base";
5216     case DW_AT_friend:
5217       return "DW_AT_friend";
5218     case DW_AT_identifier_case:
5219       return "DW_AT_identifier_case";
5220     case DW_AT_macro_info:
5221       return "DW_AT_macro_info";
5222     case DW_AT_namelist_items:
5223       return "DW_AT_namelist_items";
5224     case DW_AT_priority:
5225       return "DW_AT_priority";
5226     case DW_AT_segment:
5227       return "DW_AT_segment";
5228     case DW_AT_specification:
5229       return "DW_AT_specification";
5230     case DW_AT_static_link:
5231       return "DW_AT_static_link";
5232     case DW_AT_type:
5233       return "DW_AT_type";
5234     case DW_AT_use_location:
5235       return "DW_AT_use_location";
5236     case DW_AT_variable_parameter:
5237       return "DW_AT_variable_parameter";
5238     case DW_AT_virtuality:
5239       return "DW_AT_virtuality";
5240     case DW_AT_vtable_elem_location:
5241       return "DW_AT_vtable_elem_location";
5242
5243     case DW_AT_allocated:
5244       return "DW_AT_allocated";
5245     case DW_AT_associated:
5246       return "DW_AT_associated";
5247     case DW_AT_data_location:
5248       return "DW_AT_data_location";
5249     case DW_AT_byte_stride:
5250       return "DW_AT_byte_stride";
5251     case DW_AT_entry_pc:
5252       return "DW_AT_entry_pc";
5253     case DW_AT_use_UTF8:
5254       return "DW_AT_use_UTF8";
5255     case DW_AT_extension:
5256       return "DW_AT_extension";
5257     case DW_AT_ranges:
5258       return "DW_AT_ranges";
5259     case DW_AT_trampoline:
5260       return "DW_AT_trampoline";
5261     case DW_AT_call_column:
5262       return "DW_AT_call_column";
5263     case DW_AT_call_file:
5264       return "DW_AT_call_file";
5265     case DW_AT_call_line:
5266       return "DW_AT_call_line";
5267
5268     case DW_AT_MIPS_fde:
5269       return "DW_AT_MIPS_fde";
5270     case DW_AT_MIPS_loop_begin:
5271       return "DW_AT_MIPS_loop_begin";
5272     case DW_AT_MIPS_tail_loop_begin:
5273       return "DW_AT_MIPS_tail_loop_begin";
5274     case DW_AT_MIPS_epilog_begin:
5275       return "DW_AT_MIPS_epilog_begin";
5276     case DW_AT_MIPS_loop_unroll_factor:
5277       return "DW_AT_MIPS_loop_unroll_factor";
5278     case DW_AT_MIPS_software_pipeline_depth:
5279       return "DW_AT_MIPS_software_pipeline_depth";
5280     case DW_AT_MIPS_linkage_name:
5281       return "DW_AT_MIPS_linkage_name";
5282     case DW_AT_MIPS_stride:
5283       return "DW_AT_MIPS_stride";
5284     case DW_AT_MIPS_abstract_name:
5285       return "DW_AT_MIPS_abstract_name";
5286     case DW_AT_MIPS_clone_origin:
5287       return "DW_AT_MIPS_clone_origin";
5288     case DW_AT_MIPS_has_inlines:
5289       return "DW_AT_MIPS_has_inlines";
5290
5291     case DW_AT_sf_names:
5292       return "DW_AT_sf_names";
5293     case DW_AT_src_info:
5294       return "DW_AT_src_info";
5295     case DW_AT_mac_info:
5296       return "DW_AT_mac_info";
5297     case DW_AT_src_coords:
5298       return "DW_AT_src_coords";
5299     case DW_AT_body_begin:
5300       return "DW_AT_body_begin";
5301     case DW_AT_body_end:
5302       return "DW_AT_body_end";
5303     case DW_AT_GNU_vector:
5304       return "DW_AT_GNU_vector";
5305
5306     case DW_AT_VMS_rtnbeg_pd_address:
5307       return "DW_AT_VMS_rtnbeg_pd_address";
5308
5309     default:
5310       return "DW_AT_<unknown>";
5311     }
5312 }
5313
5314 /* Convert a DWARF value form code into its string name.  */
5315
5316 static const char *
5317 dwarf_form_name (unsigned int form)
5318 {
5319   switch (form)
5320     {
5321     case DW_FORM_addr:
5322       return "DW_FORM_addr";
5323     case DW_FORM_block2:
5324       return "DW_FORM_block2";
5325     case DW_FORM_block4:
5326       return "DW_FORM_block4";
5327     case DW_FORM_data2:
5328       return "DW_FORM_data2";
5329     case DW_FORM_data4:
5330       return "DW_FORM_data4";
5331     case DW_FORM_data8:
5332       return "DW_FORM_data8";
5333     case DW_FORM_string:
5334       return "DW_FORM_string";
5335     case DW_FORM_block:
5336       return "DW_FORM_block";
5337     case DW_FORM_block1:
5338       return "DW_FORM_block1";
5339     case DW_FORM_data1:
5340       return "DW_FORM_data1";
5341     case DW_FORM_flag:
5342       return "DW_FORM_flag";
5343     case DW_FORM_sdata:
5344       return "DW_FORM_sdata";
5345     case DW_FORM_strp:
5346       return "DW_FORM_strp";
5347     case DW_FORM_udata:
5348       return "DW_FORM_udata";
5349     case DW_FORM_ref_addr:
5350       return "DW_FORM_ref_addr";
5351     case DW_FORM_ref1:
5352       return "DW_FORM_ref1";
5353     case DW_FORM_ref2:
5354       return "DW_FORM_ref2";
5355     case DW_FORM_ref4:
5356       return "DW_FORM_ref4";
5357     case DW_FORM_ref8:
5358       return "DW_FORM_ref8";
5359     case DW_FORM_ref_udata:
5360       return "DW_FORM_ref_udata";
5361     case DW_FORM_indirect:
5362       return "DW_FORM_indirect";
5363     default:
5364       return "DW_FORM_<unknown>";
5365     }
5366 }
5367 \f
5368 /* Determine the "ultimate origin" of a decl.  The decl may be an inlined
5369    instance of an inlined instance of a decl which is local to an inline
5370    function, so we have to trace all of the way back through the origin chain
5371    to find out what sort of node actually served as the original seed for the
5372    given block.  */
5373
5374 static tree
5375 decl_ultimate_origin (const_tree decl)
5376 {
5377   if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
5378     return NULL_TREE;
5379
5380   /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
5381      nodes in the function to point to themselves; ignore that if
5382      we're trying to output the abstract instance of this function.  */
5383   if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
5384     return NULL_TREE;
5385
5386   /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
5387      most distant ancestor, this should never happen.  */
5388   gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
5389
5390   return DECL_ABSTRACT_ORIGIN (decl);
5391 }
5392
5393 /* Determine the "ultimate origin" of a block.  The block may be an inlined
5394    instance of an inlined instance of a block which is local to an inline
5395    function, so we have to trace all of the way back through the origin chain
5396    to find out what sort of node actually served as the original seed for the
5397    given block.  */
5398
5399 static tree
5400 block_ultimate_origin (const_tree block)
5401 {
5402   tree immediate_origin = BLOCK_ABSTRACT_ORIGIN (block);
5403
5404   /* output_inline_function sets BLOCK_ABSTRACT_ORIGIN for all the
5405      nodes in the function to point to themselves; ignore that if
5406      we're trying to output the abstract instance of this function.  */
5407   if (BLOCK_ABSTRACT (block) && immediate_origin == block)
5408     return NULL_TREE;
5409
5410   if (immediate_origin == NULL_TREE)
5411     return NULL_TREE;
5412   else
5413     {
5414       tree ret_val;
5415       tree lookahead = immediate_origin;
5416
5417       do
5418         {
5419           ret_val = lookahead;
5420           lookahead = (TREE_CODE (ret_val) == BLOCK
5421                        ? BLOCK_ABSTRACT_ORIGIN (ret_val) : NULL);
5422         }
5423       while (lookahead != NULL && lookahead != ret_val);
5424
5425       /* The block's abstract origin chain may not be the *ultimate* origin of
5426          the block. It could lead to a DECL that has an abstract origin set.
5427          If so, we want that DECL's abstract origin (which is what DECL_ORIGIN
5428          will give us if it has one).  Note that DECL's abstract origins are
5429          supposed to be the most distant ancestor (or so decl_ultimate_origin
5430          claims), so we don't need to loop following the DECL origins.  */
5431       if (DECL_P (ret_val))
5432         return DECL_ORIGIN (ret_val);
5433
5434       return ret_val;
5435     }
5436 }
5437
5438 /* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
5439    of a virtual function may refer to a base class, so we check the 'this'
5440    parameter.  */
5441
5442 static tree
5443 decl_class_context (tree decl)
5444 {
5445   tree context = NULL_TREE;
5446
5447   if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
5448     context = DECL_CONTEXT (decl);
5449   else
5450     context = TYPE_MAIN_VARIANT
5451       (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
5452
5453   if (context && !TYPE_P (context))
5454     context = NULL_TREE;
5455
5456   return context;
5457 }
5458 \f
5459 /* Add an attribute/value pair to a DIE.  */
5460
5461 static inline void
5462 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
5463 {
5464   /* Maybe this should be an assert?  */
5465   if (die == NULL)
5466     return;
5467
5468   if (die->die_attr == NULL)
5469     die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
5470   VEC_safe_push (dw_attr_node, gc, die->die_attr, attr);
5471 }
5472
5473 static inline enum dw_val_class
5474 AT_class (dw_attr_ref a)
5475 {
5476   return a->dw_attr_val.val_class;
5477 }
5478
5479 /* Add a flag value attribute to a DIE.  */
5480
5481 static inline void
5482 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
5483 {
5484   dw_attr_node attr;
5485
5486   attr.dw_attr = attr_kind;
5487   attr.dw_attr_val.val_class = dw_val_class_flag;
5488   attr.dw_attr_val.v.val_flag = flag;
5489   add_dwarf_attr (die, &attr);
5490 }
5491
5492 static inline unsigned
5493 AT_flag (dw_attr_ref a)
5494 {
5495   gcc_assert (a && AT_class (a) == dw_val_class_flag);
5496   return a->dw_attr_val.v.val_flag;
5497 }
5498
5499 /* Add a signed integer attribute value to a DIE.  */
5500
5501 static inline void
5502 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
5503 {
5504   dw_attr_node attr;
5505
5506   attr.dw_attr = attr_kind;
5507   attr.dw_attr_val.val_class = dw_val_class_const;
5508   attr.dw_attr_val.v.val_int = int_val;
5509   add_dwarf_attr (die, &attr);
5510 }
5511
5512 static inline HOST_WIDE_INT
5513 AT_int (dw_attr_ref a)
5514 {
5515   gcc_assert (a && AT_class (a) == dw_val_class_const);
5516   return a->dw_attr_val.v.val_int;
5517 }
5518
5519 /* Add an unsigned integer attribute value to a DIE.  */
5520
5521 static inline void
5522 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
5523                  unsigned HOST_WIDE_INT unsigned_val)
5524 {
5525   dw_attr_node attr;
5526
5527   attr.dw_attr = attr_kind;
5528   attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
5529   attr.dw_attr_val.v.val_unsigned = unsigned_val;
5530   add_dwarf_attr (die, &attr);
5531 }
5532
5533 static inline unsigned HOST_WIDE_INT
5534 AT_unsigned (dw_attr_ref a)
5535 {
5536   gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
5537   return a->dw_attr_val.v.val_unsigned;
5538 }
5539
5540 /* Add an unsigned double integer attribute value to a DIE.  */
5541
5542 static inline void
5543 add_AT_long_long (dw_die_ref die, enum dwarf_attribute attr_kind,
5544                   long unsigned int val_hi, long unsigned int val_low)
5545 {
5546   dw_attr_node attr;
5547
5548   attr.dw_attr = attr_kind;
5549   attr.dw_attr_val.val_class = dw_val_class_long_long;
5550   attr.dw_attr_val.v.val_long_long.hi = val_hi;
5551   attr.dw_attr_val.v.val_long_long.low = val_low;
5552   add_dwarf_attr (die, &attr);
5553 }
5554
5555 /* Add a floating point attribute value to a DIE and return it.  */
5556
5557 static inline void
5558 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
5559             unsigned int length, unsigned int elt_size, unsigned char *array)
5560 {
5561   dw_attr_node attr;
5562
5563   attr.dw_attr = attr_kind;
5564   attr.dw_attr_val.val_class = dw_val_class_vec;
5565   attr.dw_attr_val.v.val_vec.length = length;
5566   attr.dw_attr_val.v.val_vec.elt_size = elt_size;
5567   attr.dw_attr_val.v.val_vec.array = array;
5568   add_dwarf_attr (die, &attr);
5569 }
5570
5571 /* Hash and equality functions for debug_str_hash.  */
5572
5573 static hashval_t
5574 debug_str_do_hash (const void *x)
5575 {
5576   return htab_hash_string (((const struct indirect_string_node *)x)->str);
5577 }
5578
5579 static int
5580 debug_str_eq (const void *x1, const void *x2)
5581 {
5582   return strcmp ((((const struct indirect_string_node *)x1)->str),
5583                  (const char *)x2) == 0;
5584 }
5585
5586 /* Add a string attribute value to a DIE.  */
5587
5588 static inline void
5589 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
5590 {
5591   dw_attr_node attr;
5592   struct indirect_string_node *node;
5593   void **slot;
5594
5595   if (! debug_str_hash)
5596     debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
5597                                       debug_str_eq, NULL);
5598
5599   slot = htab_find_slot_with_hash (debug_str_hash, str,
5600                                    htab_hash_string (str), INSERT);
5601   if (*slot == NULL)
5602     {
5603       node = (struct indirect_string_node *)
5604                ggc_alloc_cleared (sizeof (struct indirect_string_node));
5605       node->str = ggc_strdup (str);
5606       *slot = node;
5607     }
5608   else
5609     node = (struct indirect_string_node *) *slot;
5610
5611   node->refcount++;
5612
5613   attr.dw_attr = attr_kind;
5614   attr.dw_attr_val.val_class = dw_val_class_str;
5615   attr.dw_attr_val.v.val_str = node;
5616   add_dwarf_attr (die, &attr);
5617 }
5618
5619 static inline const char *
5620 AT_string (dw_attr_ref a)
5621 {
5622   gcc_assert (a && AT_class (a) == dw_val_class_str);
5623   return a->dw_attr_val.v.val_str->str;
5624 }
5625
5626 /* Find out whether a string should be output inline in DIE
5627    or out-of-line in .debug_str section.  */
5628
5629 static int
5630 AT_string_form (dw_attr_ref a)
5631 {
5632   struct indirect_string_node *node;
5633   unsigned int len;
5634   char label[32];
5635
5636   gcc_assert (a && AT_class (a) == dw_val_class_str);
5637
5638   node = a->dw_attr_val.v.val_str;
5639   if (node->form)
5640     return node->form;
5641
5642   len = strlen (node->str) + 1;
5643
5644   /* If the string is shorter or equal to the size of the reference, it is
5645      always better to put it inline.  */
5646   if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
5647     return node->form = DW_FORM_string;
5648
5649   /* If we cannot expect the linker to merge strings in .debug_str
5650      section, only put it into .debug_str if it is worth even in this
5651      single module.  */
5652   if ((debug_str_section->common.flags & SECTION_MERGE) == 0
5653       && (len - DWARF_OFFSET_SIZE) * node->refcount <= len)
5654     return node->form = DW_FORM_string;
5655
5656   ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
5657   ++dw2_string_counter;
5658   node->label = xstrdup (label);
5659
5660   return node->form = DW_FORM_strp;
5661 }
5662
5663 /* Add a DIE reference attribute value to a DIE.  */
5664
5665 static inline void
5666 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
5667 {
5668   dw_attr_node attr;
5669
5670   attr.dw_attr = attr_kind;
5671   attr.dw_attr_val.val_class = dw_val_class_die_ref;
5672   attr.dw_attr_val.v.val_die_ref.die = targ_die;
5673   attr.dw_attr_val.v.val_die_ref.external = 0;
5674   add_dwarf_attr (die, &attr);
5675 }
5676
5677 /* Add an AT_specification attribute to a DIE, and also make the back
5678    pointer from the specification to the definition.  */
5679
5680 static inline void
5681 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
5682 {
5683   add_AT_die_ref (die, DW_AT_specification, targ_die);
5684   gcc_assert (!targ_die->die_definition);
5685   targ_die->die_definition = die;
5686 }
5687
5688 static inline dw_die_ref
5689 AT_ref (dw_attr_ref a)
5690 {
5691   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
5692   return a->dw_attr_val.v.val_die_ref.die;
5693 }
5694
5695 static inline int
5696 AT_ref_external (dw_attr_ref a)
5697 {
5698   if (a && AT_class (a) == dw_val_class_die_ref)
5699     return a->dw_attr_val.v.val_die_ref.external;
5700
5701   return 0;
5702 }
5703
5704 static inline void
5705 set_AT_ref_external (dw_attr_ref a, int i)
5706 {
5707   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
5708   a->dw_attr_val.v.val_die_ref.external = i;
5709 }
5710
5711 /* Add an FDE reference attribute value to a DIE.  */
5712
5713 static inline void
5714 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
5715 {
5716   dw_attr_node attr;
5717
5718   attr.dw_attr = attr_kind;
5719   attr.dw_attr_val.val_class = dw_val_class_fde_ref;
5720   attr.dw_attr_val.v.val_fde_index = targ_fde;
5721   add_dwarf_attr (die, &attr);
5722 }
5723
5724 /* Add a location description attribute value to a DIE.  */
5725
5726 static inline void
5727 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
5728 {
5729   dw_attr_node attr;
5730
5731   attr.dw_attr = attr_kind;
5732   attr.dw_attr_val.val_class = dw_val_class_loc;
5733   attr.dw_attr_val.v.val_loc = loc;
5734   add_dwarf_attr (die, &attr);
5735 }
5736
5737 static inline dw_loc_descr_ref
5738 AT_loc (dw_attr_ref a)
5739 {
5740   gcc_assert (a && AT_class (a) == dw_val_class_loc);
5741   return a->dw_attr_val.v.val_loc;
5742 }
5743
5744 static inline void
5745 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
5746 {
5747   dw_attr_node attr;
5748
5749   attr.dw_attr = attr_kind;
5750   attr.dw_attr_val.val_class = dw_val_class_loc_list;
5751   attr.dw_attr_val.v.val_loc_list = loc_list;
5752   add_dwarf_attr (die, &attr);
5753   have_location_lists = true;
5754 }
5755
5756 static inline dw_loc_list_ref
5757 AT_loc_list (dw_attr_ref a)
5758 {
5759   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
5760   return a->dw_attr_val.v.val_loc_list;
5761 }
5762
5763 /* Add an address constant attribute value to a DIE.  */
5764
5765 static inline void
5766 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
5767 {
5768   dw_attr_node attr;
5769
5770   attr.dw_attr = attr_kind;
5771   attr.dw_attr_val.val_class = dw_val_class_addr;
5772   attr.dw_attr_val.v.val_addr = addr;
5773   add_dwarf_attr (die, &attr);
5774 }
5775
5776 /* Get the RTX from to an address DIE attribute.  */
5777
5778 static inline rtx
5779 AT_addr (dw_attr_ref a)
5780 {
5781   gcc_assert (a && AT_class (a) == dw_val_class_addr);
5782   return a->dw_attr_val.v.val_addr;
5783 }
5784
5785 /* Add a file attribute value to a DIE.  */
5786
5787 static inline void
5788 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
5789              struct dwarf_file_data *fd)
5790 {
5791   dw_attr_node attr;
5792
5793   attr.dw_attr = attr_kind;
5794   attr.dw_attr_val.val_class = dw_val_class_file;
5795   attr.dw_attr_val.v.val_file = fd;
5796   add_dwarf_attr (die, &attr);
5797 }
5798
5799 /* Get the dwarf_file_data from a file DIE attribute.  */
5800
5801 static inline struct dwarf_file_data *
5802 AT_file (dw_attr_ref a)
5803 {
5804   gcc_assert (a && AT_class (a) == dw_val_class_file);
5805   return a->dw_attr_val.v.val_file;
5806 }
5807
5808 /* Add a label identifier attribute value to a DIE.  */
5809
5810 static inline void
5811 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
5812 {
5813   dw_attr_node attr;
5814
5815   attr.dw_attr = attr_kind;
5816   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
5817   attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
5818   add_dwarf_attr (die, &attr);
5819 }
5820
5821 /* Add a section offset attribute value to a DIE, an offset into the
5822    debug_line section.  */
5823
5824 static inline void
5825 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5826                 const char *label)
5827 {
5828   dw_attr_node attr;
5829
5830   attr.dw_attr = attr_kind;
5831   attr.dw_attr_val.val_class = dw_val_class_lineptr;
5832   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
5833   add_dwarf_attr (die, &attr);
5834 }
5835
5836 /* Add a section offset attribute value to a DIE, an offset into the
5837    debug_macinfo section.  */
5838
5839 static inline void
5840 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5841                const char *label)
5842 {
5843   dw_attr_node attr;
5844
5845   attr.dw_attr = attr_kind;
5846   attr.dw_attr_val.val_class = dw_val_class_macptr;
5847   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
5848   add_dwarf_attr (die, &attr);
5849 }
5850
5851 /* Add an offset attribute value to a DIE.  */
5852
5853 static inline void
5854 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
5855                unsigned HOST_WIDE_INT offset)
5856 {
5857   dw_attr_node attr;
5858
5859   attr.dw_attr = attr_kind;
5860   attr.dw_attr_val.val_class = dw_val_class_offset;
5861   attr.dw_attr_val.v.val_offset = offset;
5862   add_dwarf_attr (die, &attr);
5863 }
5864
5865 /* Add an range_list attribute value to a DIE.  */
5866
5867 static void
5868 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
5869                    long unsigned int offset)
5870 {
5871   dw_attr_node attr;
5872
5873   attr.dw_attr = attr_kind;
5874   attr.dw_attr_val.val_class = dw_val_class_range_list;
5875   attr.dw_attr_val.v.val_offset = offset;
5876   add_dwarf_attr (die, &attr);
5877 }
5878
5879 static inline const char *
5880 AT_lbl (dw_attr_ref a)
5881 {
5882   gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
5883                     || AT_class (a) == dw_val_class_lineptr
5884                     || AT_class (a) == dw_val_class_macptr));
5885   return a->dw_attr_val.v.val_lbl_id;
5886 }
5887
5888 /* Get the attribute of type attr_kind.  */
5889
5890 static dw_attr_ref
5891 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5892 {
5893   dw_attr_ref a;
5894   unsigned ix;
5895   dw_die_ref spec = NULL;
5896
5897   if (! die)
5898     return NULL;
5899
5900   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
5901     if (a->dw_attr == attr_kind)
5902       return a;
5903     else if (a->dw_attr == DW_AT_specification
5904              || a->dw_attr == DW_AT_abstract_origin)
5905       spec = AT_ref (a);
5906
5907   if (spec)
5908     return get_AT (spec, attr_kind);
5909
5910   return NULL;
5911 }
5912
5913 /* Return the "low pc" attribute value, typically associated with a subprogram
5914    DIE.  Return null if the "low pc" attribute is either not present, or if it
5915    cannot be represented as an assembler label identifier.  */
5916
5917 static inline const char *
5918 get_AT_low_pc (dw_die_ref die)
5919 {
5920   dw_attr_ref a = get_AT (die, DW_AT_low_pc);
5921
5922   return a ? AT_lbl (a) : NULL;
5923 }
5924
5925 /* Return the "high pc" attribute value, typically associated with a subprogram
5926    DIE.  Return null if the "high pc" attribute is either not present, or if it
5927    cannot be represented as an assembler label identifier.  */
5928
5929 static inline const char *
5930 get_AT_hi_pc (dw_die_ref die)
5931 {
5932   dw_attr_ref a = get_AT (die, DW_AT_high_pc);
5933
5934   return a ? AT_lbl (a) : NULL;
5935 }
5936
5937 /* Return the value of the string attribute designated by ATTR_KIND, or
5938    NULL if it is not present.  */
5939
5940 static inline const char *
5941 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
5942 {
5943   dw_attr_ref a = get_AT (die, attr_kind);
5944
5945   return a ? AT_string (a) : NULL;
5946 }
5947
5948 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
5949    if it is not present.  */
5950
5951 static inline int
5952 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
5953 {
5954   dw_attr_ref a = get_AT (die, attr_kind);
5955
5956   return a ? AT_flag (a) : 0;
5957 }
5958
5959 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
5960    if it is not present.  */
5961
5962 static inline unsigned
5963 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
5964 {
5965   dw_attr_ref a = get_AT (die, attr_kind);
5966
5967   return a ? AT_unsigned (a) : 0;
5968 }
5969
5970 static inline dw_die_ref
5971 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
5972 {
5973   dw_attr_ref a = get_AT (die, attr_kind);
5974
5975   return a ? AT_ref (a) : NULL;
5976 }
5977
5978 static inline struct dwarf_file_data *
5979 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
5980 {
5981   dw_attr_ref a = get_AT (die, attr_kind);
5982
5983   return a ? AT_file (a) : NULL;
5984 }
5985
5986 /* Return TRUE if the language is C or C++.  */
5987
5988 static inline bool
5989 is_c_family (void)
5990 {
5991   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5992
5993   return (lang == DW_LANG_C || lang == DW_LANG_C89 || lang == DW_LANG_ObjC
5994           || lang == DW_LANG_C99
5995           || lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus);
5996 }
5997
5998 /* Return TRUE if the language is C++.  */
5999
6000 static inline bool
6001 is_cxx (void)
6002 {
6003   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
6004
6005   return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
6006 }
6007
6008 /* Return TRUE if the language is Fortran.  */
6009
6010 static inline bool
6011 is_fortran (void)
6012 {
6013   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
6014
6015   return (lang == DW_LANG_Fortran77
6016           || lang == DW_LANG_Fortran90
6017           || lang == DW_LANG_Fortran95);
6018 }
6019
6020 /* Return TRUE if the language is Java.  */
6021
6022 static inline bool
6023 is_java (void)
6024 {
6025   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
6026
6027   return lang == DW_LANG_Java;
6028 }
6029
6030 /* Return TRUE if the language is Ada.  */
6031
6032 static inline bool
6033 is_ada (void)
6034 {
6035   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
6036
6037   return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
6038 }
6039
6040 /* Remove the specified attribute if present.  */
6041
6042 static void
6043 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
6044 {
6045   dw_attr_ref a;
6046   unsigned ix;
6047
6048   if (! die)
6049     return;
6050
6051   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6052     if (a->dw_attr == attr_kind)
6053       {
6054         if (AT_class (a) == dw_val_class_str)
6055           if (a->dw_attr_val.v.val_str->refcount)
6056             a->dw_attr_val.v.val_str->refcount--;
6057
6058         /* VEC_ordered_remove should help reduce the number of abbrevs
6059            that are needed.  */
6060         VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
6061         return;
6062       }
6063 }
6064
6065 /* Remove CHILD from its parent.  PREV must have the property that
6066    PREV->DIE_SIB == CHILD.  Does not alter CHILD.  */
6067
6068 static void
6069 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
6070 {
6071   gcc_assert (child->die_parent == prev->die_parent);
6072   gcc_assert (prev->die_sib == child);
6073   if (prev == child)
6074     {
6075       gcc_assert (child->die_parent->die_child == child);
6076       prev = NULL;
6077     }
6078   else
6079     prev->die_sib = child->die_sib;
6080   if (child->die_parent->die_child == child)
6081     child->die_parent->die_child = prev;
6082 }
6083
6084 /* Remove child DIE whose die_tag is TAG.  Do nothing if no child
6085    matches TAG.  */
6086
6087 static void
6088 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
6089 {
6090   dw_die_ref c;
6091
6092   c = die->die_child;
6093   if (c) do {
6094     dw_die_ref prev = c;
6095     c = c->die_sib;
6096     while (c->die_tag == tag)
6097       {
6098         remove_child_with_prev (c, prev);
6099         /* Might have removed every child.  */
6100         if (c == c->die_sib)
6101           return;
6102         c = c->die_sib;
6103       }
6104   } while (c != die->die_child);
6105 }
6106
6107 /* Add a CHILD_DIE as the last child of DIE.  */
6108
6109 static void
6110 add_child_die (dw_die_ref die, dw_die_ref child_die)
6111 {
6112   /* FIXME this should probably be an assert.  */
6113   if (! die || ! child_die)
6114     return;
6115   gcc_assert (die != child_die);
6116
6117   child_die->die_parent = die;
6118   if (die->die_child)
6119     {
6120       child_die->die_sib = die->die_child->die_sib;
6121       die->die_child->die_sib = child_die;
6122     }
6123   else
6124     child_die->die_sib = child_die;
6125   die->die_child = child_die;
6126 }
6127
6128 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
6129    is the specification, to the end of PARENT's list of children.
6130    This is done by removing and re-adding it.  */
6131
6132 static void
6133 splice_child_die (dw_die_ref parent, dw_die_ref child)
6134 {
6135   dw_die_ref p;
6136
6137   /* We want the declaration DIE from inside the class, not the
6138      specification DIE at toplevel.  */
6139   if (child->die_parent != parent)
6140     {
6141       dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
6142
6143       if (tmp)
6144         child = tmp;
6145     }
6146
6147   gcc_assert (child->die_parent == parent
6148               || (child->die_parent
6149                   == get_AT_ref (parent, DW_AT_specification)));
6150
6151   for (p = child->die_parent->die_child; ; p = p->die_sib)
6152     if (p->die_sib == child)
6153       {
6154         remove_child_with_prev (child, p);
6155         break;
6156       }
6157
6158   add_child_die (parent, child);
6159 }
6160
6161 /* Return a pointer to a newly created DIE node.  */
6162
6163 static inline dw_die_ref
6164 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
6165 {
6166   dw_die_ref die = GGC_CNEW (die_node);
6167
6168   die->die_tag = tag_value;
6169
6170   if (parent_die != NULL)
6171     add_child_die (parent_die, die);
6172   else
6173     {
6174       limbo_die_node *limbo_node;
6175
6176       limbo_node = GGC_CNEW (limbo_die_node);
6177       limbo_node->die = die;
6178       limbo_node->created_for = t;
6179       limbo_node->next = limbo_die_list;
6180       limbo_die_list = limbo_node;
6181     }
6182
6183   return die;
6184 }
6185
6186 /* Return the DIE associated with the given type specifier.  */
6187
6188 static inline dw_die_ref
6189 lookup_type_die (tree type)
6190 {
6191   return TYPE_SYMTAB_DIE (type);
6192 }
6193
6194 /* Equate a DIE to a given type specifier.  */
6195
6196 static inline void
6197 equate_type_number_to_die (tree type, dw_die_ref type_die)
6198 {
6199   TYPE_SYMTAB_DIE (type) = type_die;
6200 }
6201
6202 /* Returns a hash value for X (which really is a die_struct).  */
6203
6204 static hashval_t
6205 decl_die_table_hash (const void *x)
6206 {
6207   return (hashval_t) ((const_dw_die_ref) x)->decl_id;
6208 }
6209
6210 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
6211
6212 static int
6213 decl_die_table_eq (const void *x, const void *y)
6214 {
6215   return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
6216 }
6217
6218 /* Return the DIE associated with a given declaration.  */
6219
6220 static inline dw_die_ref
6221 lookup_decl_die (tree decl)
6222 {
6223   return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
6224 }
6225
6226 /* Returns a hash value for X (which really is a var_loc_list).  */
6227
6228 static hashval_t
6229 decl_loc_table_hash (const void *x)
6230 {
6231   return (hashval_t) ((const var_loc_list *) x)->decl_id;
6232 }
6233
6234 /* Return nonzero if decl_id of var_loc_list X is the same as
6235    UID of decl *Y.  */
6236
6237 static int
6238 decl_loc_table_eq (const void *x, const void *y)
6239 {
6240   return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
6241 }
6242
6243 /* Return the var_loc list associated with a given declaration.  */
6244
6245 static inline var_loc_list *
6246 lookup_decl_loc (const_tree decl)
6247 {
6248   return (var_loc_list *)
6249     htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
6250 }
6251
6252 /* Equate a DIE to a particular declaration.  */
6253
6254 static void
6255 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
6256 {
6257   unsigned int decl_id = DECL_UID (decl);
6258   void **slot;
6259
6260   slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
6261   *slot = decl_die;
6262   decl_die->decl_id = decl_id;
6263 }
6264
6265 /* Add a variable location node to the linked list for DECL.  */
6266
6267 static void
6268 add_var_loc_to_decl (tree decl, struct var_loc_node *loc)
6269 {
6270   unsigned int decl_id = DECL_UID (decl);
6271   var_loc_list *temp;
6272   void **slot;
6273
6274   slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
6275   if (*slot == NULL)
6276     {
6277       temp = GGC_CNEW (var_loc_list);
6278       temp->decl_id = decl_id;
6279       *slot = temp;
6280     }
6281   else
6282     temp = (var_loc_list *) *slot;
6283
6284   if (temp->last)
6285     {
6286       /* If the current location is the same as the end of the list,
6287          and either both or neither of the locations is uninitialized,
6288          we have nothing to do.  */
6289       if ((!rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->last->var_loc_note),
6290                          NOTE_VAR_LOCATION_LOC (loc->var_loc_note)))
6291           || ((NOTE_VAR_LOCATION_STATUS (temp->last->var_loc_note)
6292                != NOTE_VAR_LOCATION_STATUS (loc->var_loc_note))
6293               && ((NOTE_VAR_LOCATION_STATUS (temp->last->var_loc_note)
6294                    == VAR_INIT_STATUS_UNINITIALIZED)
6295                   || (NOTE_VAR_LOCATION_STATUS (loc->var_loc_note)
6296                       == VAR_INIT_STATUS_UNINITIALIZED))))
6297         {
6298           /* Add LOC to the end of list and update LAST.  */
6299           temp->last->next = loc;
6300           temp->last = loc;
6301         }
6302     }
6303   /* Do not add empty location to the beginning of the list.  */
6304   else if (NOTE_VAR_LOCATION_LOC (loc->var_loc_note) != NULL_RTX)
6305     {
6306       temp->first = loc;
6307       temp->last = loc;
6308     }
6309 }
6310 \f
6311 /* Keep track of the number of spaces used to indent the
6312    output of the debugging routines that print the structure of
6313    the DIE internal representation.  */
6314 static int print_indent;
6315
6316 /* Indent the line the number of spaces given by print_indent.  */
6317
6318 static inline void
6319 print_spaces (FILE *outfile)
6320 {
6321   fprintf (outfile, "%*s", print_indent, "");
6322 }
6323
6324 /* Print the information associated with a given DIE, and its children.
6325    This routine is a debugging aid only.  */
6326
6327 static void
6328 print_die (dw_die_ref die, FILE *outfile)
6329 {
6330   dw_attr_ref a;
6331   dw_die_ref c;
6332   unsigned ix;
6333
6334   print_spaces (outfile);
6335   fprintf (outfile, "DIE %4ld: %s\n",
6336            die->die_offset, dwarf_tag_name (die->die_tag));
6337   print_spaces (outfile);
6338   fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
6339   fprintf (outfile, " offset: %ld\n", die->die_offset);
6340
6341   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6342     {
6343       print_spaces (outfile);
6344       fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
6345
6346       switch (AT_class (a))
6347         {
6348         case dw_val_class_addr:
6349           fprintf (outfile, "address");
6350           break;
6351         case dw_val_class_offset:
6352           fprintf (outfile, "offset");
6353           break;
6354         case dw_val_class_loc:
6355           fprintf (outfile, "location descriptor");
6356           break;
6357         case dw_val_class_loc_list:
6358           fprintf (outfile, "location list -> label:%s",
6359                    AT_loc_list (a)->ll_symbol);
6360           break;
6361         case dw_val_class_range_list:
6362           fprintf (outfile, "range list");
6363           break;
6364         case dw_val_class_const:
6365           fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
6366           break;
6367         case dw_val_class_unsigned_const:
6368           fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
6369           break;
6370         case dw_val_class_long_long:
6371           fprintf (outfile, "constant (%lu,%lu)",
6372                    a->dw_attr_val.v.val_long_long.hi,
6373                    a->dw_attr_val.v.val_long_long.low);
6374           break;
6375         case dw_val_class_vec:
6376           fprintf (outfile, "floating-point or vector constant");
6377           break;
6378         case dw_val_class_flag:
6379           fprintf (outfile, "%u", AT_flag (a));
6380           break;
6381         case dw_val_class_die_ref:
6382           if (AT_ref (a) != NULL)
6383             {
6384               if (AT_ref (a)->die_symbol)
6385                 fprintf (outfile, "die -> label: %s", AT_ref (a)->die_symbol);
6386               else
6387                 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
6388             }
6389           else
6390             fprintf (outfile, "die -> <null>");
6391           break;
6392         case dw_val_class_lbl_id:
6393         case dw_val_class_lineptr:
6394         case dw_val_class_macptr:
6395           fprintf (outfile, "label: %s", AT_lbl (a));
6396           break;
6397         case dw_val_class_str:
6398           if (AT_string (a) != NULL)
6399             fprintf (outfile, "\"%s\"", AT_string (a));
6400           else
6401             fprintf (outfile, "<null>");
6402           break;
6403         case dw_val_class_file:
6404           fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
6405                    AT_file (a)->emitted_number);
6406           break;
6407         default:
6408           break;
6409         }
6410
6411       fprintf (outfile, "\n");
6412     }
6413
6414   if (die->die_child != NULL)
6415     {
6416       print_indent += 4;
6417       FOR_EACH_CHILD (die, c, print_die (c, outfile));
6418       print_indent -= 4;
6419     }
6420   if (print_indent == 0)
6421     fprintf (outfile, "\n");
6422 }
6423
6424 /* Print the contents of the source code line number correspondence table.
6425    This routine is a debugging aid only.  */
6426
6427 static void
6428 print_dwarf_line_table (FILE *outfile)
6429 {
6430   unsigned i;
6431   dw_line_info_ref line_info;
6432
6433   fprintf (outfile, "\n\nDWARF source line information\n");
6434   for (i = 1; i < line_info_table_in_use; i++)
6435     {
6436       line_info = &line_info_table[i];
6437       fprintf (outfile, "%5d: %4ld %6ld\n", i,
6438                line_info->dw_file_num,
6439                line_info->dw_line_num);
6440     }
6441
6442   fprintf (outfile, "\n\n");
6443 }
6444
6445 /* Print the information collected for a given DIE.  */
6446
6447 void
6448 debug_dwarf_die (dw_die_ref die)
6449 {
6450   print_die (die, stderr);
6451 }
6452
6453 /* Print all DWARF information collected for the compilation unit.
6454    This routine is a debugging aid only.  */
6455
6456 void
6457 debug_dwarf (void)
6458 {
6459   print_indent = 0;
6460   print_die (comp_unit_die, stderr);
6461   if (! DWARF2_ASM_LINE_DEBUG_INFO)
6462     print_dwarf_line_table (stderr);
6463 }
6464 \f
6465 /* Start a new compilation unit DIE for an include file.  OLD_UNIT is the CU
6466    for the enclosing include file, if any.  BINCL_DIE is the DW_TAG_GNU_BINCL
6467    DIE that marks the start of the DIEs for this include file.  */
6468
6469 static dw_die_ref
6470 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
6471 {
6472   const char *filename = get_AT_string (bincl_die, DW_AT_name);
6473   dw_die_ref new_unit = gen_compile_unit_die (filename);
6474
6475   new_unit->die_sib = old_unit;
6476   return new_unit;
6477 }
6478
6479 /* Close an include-file CU and reopen the enclosing one.  */
6480
6481 static dw_die_ref
6482 pop_compile_unit (dw_die_ref old_unit)
6483 {
6484   dw_die_ref new_unit = old_unit->die_sib;
6485
6486   old_unit->die_sib = NULL;
6487   return new_unit;
6488 }
6489
6490 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6491 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
6492
6493 /* Calculate the checksum of a location expression.  */
6494
6495 static inline void
6496 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6497 {
6498   CHECKSUM (loc->dw_loc_opc);
6499   CHECKSUM (loc->dw_loc_oprnd1);
6500   CHECKSUM (loc->dw_loc_oprnd2);
6501 }
6502
6503 /* Calculate the checksum of an attribute.  */
6504
6505 static void
6506 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
6507 {
6508   dw_loc_descr_ref loc;
6509   rtx r;
6510
6511   CHECKSUM (at->dw_attr);
6512
6513   /* We don't care that this was compiled with a different compiler
6514      snapshot; if the output is the same, that's what matters.  */
6515   if (at->dw_attr == DW_AT_producer)
6516     return;
6517
6518   switch (AT_class (at))
6519     {
6520     case dw_val_class_const:
6521       CHECKSUM (at->dw_attr_val.v.val_int);
6522       break;
6523     case dw_val_class_unsigned_const:
6524       CHECKSUM (at->dw_attr_val.v.val_unsigned);
6525       break;
6526     case dw_val_class_long_long:
6527       CHECKSUM (at->dw_attr_val.v.val_long_long);
6528       break;
6529     case dw_val_class_vec:
6530       CHECKSUM (at->dw_attr_val.v.val_vec);
6531       break;
6532     case dw_val_class_flag:
6533       CHECKSUM (at->dw_attr_val.v.val_flag);
6534       break;
6535     case dw_val_class_str:
6536       CHECKSUM_STRING (AT_string (at));
6537       break;
6538
6539     case dw_val_class_addr:
6540       r = AT_addr (at);
6541       gcc_assert (GET_CODE (r) == SYMBOL_REF);
6542       CHECKSUM_STRING (XSTR (r, 0));
6543       break;
6544
6545     case dw_val_class_offset:
6546       CHECKSUM (at->dw_attr_val.v.val_offset);
6547       break;
6548
6549     case dw_val_class_loc:
6550       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6551         loc_checksum (loc, ctx);
6552       break;
6553
6554     case dw_val_class_die_ref:
6555       die_checksum (AT_ref (at), ctx, mark);
6556       break;
6557
6558     case dw_val_class_fde_ref:
6559     case dw_val_class_lbl_id:
6560     case dw_val_class_lineptr:
6561     case dw_val_class_macptr:
6562       break;
6563
6564     case dw_val_class_file:
6565       CHECKSUM_STRING (AT_file (at)->filename);
6566       break;
6567
6568     default:
6569       break;
6570     }
6571 }
6572
6573 /* Calculate the checksum of a DIE.  */
6574
6575 static void
6576 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6577 {
6578   dw_die_ref c;
6579   dw_attr_ref a;
6580   unsigned ix;
6581
6582   /* To avoid infinite recursion.  */
6583   if (die->die_mark)
6584     {
6585       CHECKSUM (die->die_mark);
6586       return;
6587     }
6588   die->die_mark = ++(*mark);
6589
6590   CHECKSUM (die->die_tag);
6591
6592   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6593     attr_checksum (a, ctx, mark);
6594
6595   FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
6596 }
6597
6598 #undef CHECKSUM
6599 #undef CHECKSUM_STRING
6600
6601 /* Do the location expressions look same?  */
6602 static inline int
6603 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6604 {
6605   return loc1->dw_loc_opc == loc2->dw_loc_opc
6606          && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6607          && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6608 }
6609
6610 /* Do the values look the same?  */
6611 static int
6612 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6613 {
6614   dw_loc_descr_ref loc1, loc2;
6615   rtx r1, r2;
6616
6617   if (v1->val_class != v2->val_class)
6618     return 0;
6619
6620   switch (v1->val_class)
6621     {
6622     case dw_val_class_const:
6623       return v1->v.val_int == v2->v.val_int;
6624     case dw_val_class_unsigned_const:
6625       return v1->v.val_unsigned == v2->v.val_unsigned;
6626     case dw_val_class_long_long:
6627       return v1->v.val_long_long.hi == v2->v.val_long_long.hi
6628              && v1->v.val_long_long.low == v2->v.val_long_long.low;
6629     case dw_val_class_vec:
6630       if (v1->v.val_vec.length != v2->v.val_vec.length
6631           || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6632         return 0;
6633       if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6634                   v1->v.val_vec.length * v1->v.val_vec.elt_size))
6635         return 0;
6636       return 1;
6637     case dw_val_class_flag:
6638       return v1->v.val_flag == v2->v.val_flag;
6639     case dw_val_class_str:
6640       return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
6641
6642     case dw_val_class_addr:
6643       r1 = v1->v.val_addr;
6644       r2 = v2->v.val_addr;
6645       if (GET_CODE (r1) != GET_CODE (r2))
6646         return 0;
6647       gcc_assert (GET_CODE (r1) == SYMBOL_REF);
6648       return !strcmp (XSTR (r1, 0), XSTR (r2, 0));
6649
6650     case dw_val_class_offset:
6651       return v1->v.val_offset == v2->v.val_offset;
6652
6653     case dw_val_class_loc:
6654       for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6655            loc1 && loc2;
6656            loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6657         if (!same_loc_p (loc1, loc2, mark))
6658           return 0;
6659       return !loc1 && !loc2;
6660
6661     case dw_val_class_die_ref:
6662       return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6663
6664     case dw_val_class_fde_ref:
6665     case dw_val_class_lbl_id:
6666     case dw_val_class_lineptr:
6667     case dw_val_class_macptr:
6668       return 1;
6669
6670     case dw_val_class_file:
6671       return v1->v.val_file == v2->v.val_file;
6672
6673     default:
6674       return 1;
6675     }
6676 }
6677
6678 /* Do the attributes look the same?  */
6679
6680 static int
6681 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
6682 {
6683   if (at1->dw_attr != at2->dw_attr)
6684     return 0;
6685
6686   /* We don't care that this was compiled with a different compiler
6687      snapshot; if the output is the same, that's what matters. */
6688   if (at1->dw_attr == DW_AT_producer)
6689     return 1;
6690
6691   return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6692 }
6693
6694 /* Do the dies look the same?  */
6695
6696 static int
6697 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6698 {
6699   dw_die_ref c1, c2;
6700   dw_attr_ref a1;
6701   unsigned ix;
6702
6703   /* To avoid infinite recursion.  */
6704   if (die1->die_mark)
6705     return die1->die_mark == die2->die_mark;
6706   die1->die_mark = die2->die_mark = ++(*mark);
6707
6708   if (die1->die_tag != die2->die_tag)
6709     return 0;
6710
6711   if (VEC_length (dw_attr_node, die1->die_attr)
6712       != VEC_length (dw_attr_node, die2->die_attr))
6713     return 0;
6714
6715   for (ix = 0; VEC_iterate (dw_attr_node, die1->die_attr, ix, a1); ix++)
6716     if (!same_attr_p (a1, VEC_index (dw_attr_node, die2->die_attr, ix), mark))
6717       return 0;
6718
6719   c1 = die1->die_child;
6720   c2 = die2->die_child;
6721   if (! c1)
6722     {
6723       if (c2)
6724         return 0;
6725     }
6726   else
6727     for (;;)
6728       {
6729         if (!same_die_p (c1, c2, mark))
6730           return 0;
6731         c1 = c1->die_sib;
6732         c2 = c2->die_sib;
6733         if (c1 == die1->die_child)
6734           {
6735             if (c2 == die2->die_child)
6736               break;
6737             else
6738               return 0;
6739           }
6740     }
6741
6742   return 1;
6743 }
6744
6745 /* Do the dies look the same?  Wrapper around same_die_p.  */
6746
6747 static int
6748 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6749 {
6750   int mark = 0;
6751   int ret = same_die_p (die1, die2, &mark);
6752
6753   unmark_all_dies (die1);
6754   unmark_all_dies (die2);
6755
6756   return ret;
6757 }
6758
6759 /* The prefix to attach to symbols on DIEs in the current comdat debug
6760    info section.  */
6761 static char *comdat_symbol_id;
6762
6763 /* The index of the current symbol within the current comdat CU.  */
6764 static unsigned int comdat_symbol_number;
6765
6766 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6767    children, and set comdat_symbol_id accordingly.  */
6768
6769 static void
6770 compute_section_prefix (dw_die_ref unit_die)
6771 {
6772   const char *die_name = get_AT_string (unit_die, DW_AT_name);
6773   const char *base = die_name ? lbasename (die_name) : "anonymous";
6774   char *name = XALLOCAVEC (char, strlen (base) + 64);
6775   char *p;
6776   int i, mark;
6777   unsigned char checksum[16];
6778   struct md5_ctx ctx;
6779
6780   /* Compute the checksum of the DIE, then append part of it as hex digits to
6781      the name filename of the unit.  */
6782
6783   md5_init_ctx (&ctx);
6784   mark = 0;
6785   die_checksum (unit_die, &ctx, &mark);
6786   unmark_all_dies (unit_die);
6787   md5_finish_ctx (&ctx, checksum);
6788
6789   sprintf (name, "%s.", base);
6790   clean_symbol_name (name);
6791
6792   p = name + strlen (name);
6793   for (i = 0; i < 4; i++)
6794     {
6795       sprintf (p, "%.2x", checksum[i]);
6796       p += 2;
6797     }
6798
6799   comdat_symbol_id = unit_die->die_symbol = xstrdup (name);
6800   comdat_symbol_number = 0;
6801 }
6802
6803 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P.  */
6804
6805 static int
6806 is_type_die (dw_die_ref die)
6807 {
6808   switch (die->die_tag)
6809     {
6810     case DW_TAG_array_type:
6811     case DW_TAG_class_type:
6812     case DW_TAG_interface_type:
6813     case DW_TAG_enumeration_type:
6814     case DW_TAG_pointer_type:
6815     case DW_TAG_reference_type:
6816     case DW_TAG_string_type:
6817     case DW_TAG_structure_type:
6818     case DW_TAG_subroutine_type:
6819     case DW_TAG_union_type:
6820     case DW_TAG_ptr_to_member_type:
6821     case DW_TAG_set_type:
6822     case DW_TAG_subrange_type:
6823     case DW_TAG_base_type:
6824     case DW_TAG_const_type:
6825     case DW_TAG_file_type:
6826     case DW_TAG_packed_type:
6827     case DW_TAG_volatile_type:
6828     case DW_TAG_typedef:
6829       return 1;
6830     default:
6831       return 0;
6832     }
6833 }
6834
6835 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6836    Basically, we want to choose the bits that are likely to be shared between
6837    compilations (types) and leave out the bits that are specific to individual
6838    compilations (functions).  */
6839
6840 static int
6841 is_comdat_die (dw_die_ref c)
6842 {
6843   /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6844      we do for stabs.  The advantage is a greater likelihood of sharing between
6845      objects that don't include headers in the same order (and therefore would
6846      put the base types in a different comdat).  jason 8/28/00 */
6847
6848   if (c->die_tag == DW_TAG_base_type)
6849     return 0;
6850
6851   if (c->die_tag == DW_TAG_pointer_type
6852       || c->die_tag == DW_TAG_reference_type
6853       || c->die_tag == DW_TAG_const_type
6854       || c->die_tag == DW_TAG_volatile_type)
6855     {
6856       dw_die_ref t = get_AT_ref (c, DW_AT_type);
6857
6858       return t ? is_comdat_die (t) : 0;
6859     }
6860
6861   return is_type_die (c);
6862 }
6863
6864 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6865    compilation unit.  */
6866
6867 static int
6868 is_symbol_die (dw_die_ref c)
6869 {
6870   return (is_type_die (c)
6871           || (get_AT (c, DW_AT_declaration)
6872               && !get_AT (c, DW_AT_specification))
6873           || c->die_tag == DW_TAG_namespace);
6874 }
6875
6876 static char *
6877 gen_internal_sym (const char *prefix)
6878 {
6879   char buf[256];
6880
6881   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6882   return xstrdup (buf);
6883 }
6884
6885 /* Assign symbols to all worthy DIEs under DIE.  */
6886
6887 static void
6888 assign_symbol_names (dw_die_ref die)
6889 {
6890   dw_die_ref c;
6891
6892   if (is_symbol_die (die))
6893     {
6894       if (comdat_symbol_id)
6895         {
6896           char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
6897
6898           sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6899                    comdat_symbol_id, comdat_symbol_number++);
6900           die->die_symbol = xstrdup (p);
6901         }
6902       else
6903         die->die_symbol = gen_internal_sym ("LDIE");
6904     }
6905
6906   FOR_EACH_CHILD (die, c, assign_symbol_names (c));
6907 }
6908
6909 struct cu_hash_table_entry
6910 {
6911   dw_die_ref cu;
6912   unsigned min_comdat_num, max_comdat_num;
6913   struct cu_hash_table_entry *next;
6914 };
6915
6916 /* Routines to manipulate hash table of CUs.  */
6917 static hashval_t
6918 htab_cu_hash (const void *of)
6919 {
6920   const struct cu_hash_table_entry *const entry =
6921     (const struct cu_hash_table_entry *) of;
6922
6923   return htab_hash_string (entry->cu->die_symbol);
6924 }
6925
6926 static int
6927 htab_cu_eq (const void *of1, const void *of2)
6928 {
6929   const struct cu_hash_table_entry *const entry1 =
6930     (const struct cu_hash_table_entry *) of1;
6931   const struct die_struct *const entry2 = (const struct die_struct *) of2;
6932
6933   return !strcmp (entry1->cu->die_symbol, entry2->die_symbol);
6934 }
6935
6936 static void
6937 htab_cu_del (void *what)
6938 {
6939   struct cu_hash_table_entry *next,
6940     *entry = (struct cu_hash_table_entry *) what;
6941
6942   while (entry)
6943     {
6944       next = entry->next;
6945       free (entry);
6946       entry = next;
6947     }
6948 }
6949
6950 /* Check whether we have already seen this CU and set up SYM_NUM
6951    accordingly.  */
6952 static int
6953 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
6954 {
6955   struct cu_hash_table_entry dummy;
6956   struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6957
6958   dummy.max_comdat_num = 0;
6959
6960   slot = (struct cu_hash_table_entry **)
6961     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
6962         INSERT);
6963   entry = *slot;
6964
6965   for (; entry; last = entry, entry = entry->next)
6966     {
6967       if (same_die_p_wrap (cu, entry->cu))
6968         break;
6969     }
6970
6971   if (entry)
6972     {
6973       *sym_num = entry->min_comdat_num;
6974       return 1;
6975     }
6976
6977   entry = XCNEW (struct cu_hash_table_entry);
6978   entry->cu = cu;
6979   entry->min_comdat_num = *sym_num = last->max_comdat_num;
6980   entry->next = *slot;
6981   *slot = entry;
6982
6983   return 0;
6984 }
6985
6986 /* Record SYM_NUM to record of CU in HTABLE.  */
6987 static void
6988 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
6989 {
6990   struct cu_hash_table_entry **slot, *entry;
6991
6992   slot = (struct cu_hash_table_entry **)
6993     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
6994         NO_INSERT);
6995   entry = *slot;
6996
6997   entry->max_comdat_num = sym_num;
6998 }
6999
7000 /* Traverse the DIE (which is always comp_unit_die), and set up
7001    additional compilation units for each of the include files we see
7002    bracketed by BINCL/EINCL.  */
7003
7004 static void
7005 break_out_includes (dw_die_ref die)
7006 {
7007   dw_die_ref c;
7008   dw_die_ref unit = NULL;
7009   limbo_die_node *node, **pnode;
7010   htab_t cu_hash_table;
7011
7012   c = die->die_child;
7013   if (c) do {
7014     dw_die_ref prev = c;
7015     c = c->die_sib;
7016     while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
7017            || (unit && is_comdat_die (c)))
7018       {
7019         dw_die_ref next = c->die_sib;
7020
7021         /* This DIE is for a secondary CU; remove it from the main one.  */
7022         remove_child_with_prev (c, prev);
7023
7024         if (c->die_tag == DW_TAG_GNU_BINCL)
7025           unit = push_new_compile_unit (unit, c);
7026         else if (c->die_tag == DW_TAG_GNU_EINCL)
7027           unit = pop_compile_unit (unit);
7028         else
7029           add_child_die (unit, c);
7030         c = next;
7031         if (c == die->die_child)
7032           break;
7033       }
7034   } while (c != die->die_child);
7035
7036 #if 0
7037   /* We can only use this in debugging, since the frontend doesn't check
7038      to make sure that we leave every include file we enter.  */
7039   gcc_assert (!unit);
7040 #endif
7041
7042   assign_symbol_names (die);
7043   cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
7044   for (node = limbo_die_list, pnode = &limbo_die_list;
7045        node;
7046        node = node->next)
7047     {
7048       int is_dupl;
7049
7050       compute_section_prefix (node->die);
7051       is_dupl = check_duplicate_cu (node->die, cu_hash_table,
7052                         &comdat_symbol_number);
7053       assign_symbol_names (node->die);
7054       if (is_dupl)
7055         *pnode = node->next;
7056       else
7057         {
7058           pnode = &node->next;
7059           record_comdat_symbol_number (node->die, cu_hash_table,
7060                 comdat_symbol_number);
7061         }
7062     }
7063   htab_delete (cu_hash_table);
7064 }
7065
7066 /* Traverse the DIE and add a sibling attribute if it may have the
7067    effect of speeding up access to siblings.  To save some space,
7068    avoid generating sibling attributes for DIE's without children.  */
7069
7070 static void
7071 add_sibling_attributes (dw_die_ref die)
7072 {
7073   dw_die_ref c;
7074
7075   if (! die->die_child)
7076     return;
7077
7078   if (die->die_parent && die != die->die_parent->die_child)
7079     add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
7080
7081   FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
7082 }
7083
7084 /* Output all location lists for the DIE and its children.  */
7085
7086 static void
7087 output_location_lists (dw_die_ref die)
7088 {
7089   dw_die_ref c;
7090   dw_attr_ref a;
7091   unsigned ix;
7092
7093   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7094     if (AT_class (a) == dw_val_class_loc_list)
7095       output_loc_list (AT_loc_list (a));
7096
7097   FOR_EACH_CHILD (die, c, output_location_lists (c));
7098 }
7099
7100 /* The format of each DIE (and its attribute value pairs) is encoded in an
7101    abbreviation table.  This routine builds the abbreviation table and assigns
7102    a unique abbreviation id for each abbreviation entry.  The children of each
7103    die are visited recursively.  */
7104
7105 static void
7106 build_abbrev_table (dw_die_ref die)
7107 {
7108   unsigned long abbrev_id;
7109   unsigned int n_alloc;
7110   dw_die_ref c;
7111   dw_attr_ref a;
7112   unsigned ix;
7113
7114   /* Scan the DIE references, and mark as external any that refer to
7115      DIEs from other CUs (i.e. those which are not marked).  */
7116   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7117     if (AT_class (a) == dw_val_class_die_ref
7118         && AT_ref (a)->die_mark == 0)
7119       {
7120         gcc_assert (AT_ref (a)->die_symbol);
7121
7122         set_AT_ref_external (a, 1);
7123       }
7124
7125   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7126     {
7127       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7128       dw_attr_ref die_a, abbrev_a;
7129       unsigned ix;
7130       bool ok = true;
7131
7132       if (abbrev->die_tag != die->die_tag)
7133         continue;
7134       if ((abbrev->die_child != NULL) != (die->die_child != NULL))
7135         continue;
7136
7137       if (VEC_length (dw_attr_node, abbrev->die_attr)
7138           != VEC_length (dw_attr_node, die->die_attr))
7139         continue;
7140
7141       for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, die_a); ix++)
7142         {
7143           abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
7144           if ((abbrev_a->dw_attr != die_a->dw_attr)
7145               || (value_format (abbrev_a) != value_format (die_a)))
7146             {
7147               ok = false;
7148               break;
7149             }
7150         }
7151       if (ok)
7152         break;
7153     }
7154
7155   if (abbrev_id >= abbrev_die_table_in_use)
7156     {
7157       if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
7158         {
7159           n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
7160           abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
7161                                             n_alloc);
7162
7163           memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
7164                  (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
7165           abbrev_die_table_allocated = n_alloc;
7166         }
7167
7168       ++abbrev_die_table_in_use;
7169       abbrev_die_table[abbrev_id] = die;
7170     }
7171
7172   die->die_abbrev = abbrev_id;
7173   FOR_EACH_CHILD (die, c, build_abbrev_table (c));
7174 }
7175 \f
7176 /* Return the power-of-two number of bytes necessary to represent VALUE.  */
7177
7178 static int
7179 constant_size (long unsigned int value)
7180 {
7181   int log;
7182
7183   if (value == 0)
7184     log = 0;
7185   else
7186     log = floor_log2 (value);
7187
7188   log = log / 8;
7189   log = 1 << (floor_log2 (log) + 1);
7190
7191   return log;
7192 }
7193
7194 /* Return the size of a DIE as it is represented in the
7195    .debug_info section.  */
7196
7197 static unsigned long
7198 size_of_die (dw_die_ref die)
7199 {
7200   unsigned long size = 0;
7201   dw_attr_ref a;
7202   unsigned ix;
7203
7204   size += size_of_uleb128 (die->die_abbrev);
7205   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7206     {
7207       switch (AT_class (a))
7208         {
7209         case dw_val_class_addr:
7210           size += DWARF2_ADDR_SIZE;
7211           break;
7212         case dw_val_class_offset:
7213           size += DWARF_OFFSET_SIZE;
7214           break;
7215         case dw_val_class_loc:
7216           {
7217             unsigned long lsize = size_of_locs (AT_loc (a));
7218
7219             /* Block length.  */
7220             size += constant_size (lsize);
7221             size += lsize;
7222           }
7223           break;
7224         case dw_val_class_loc_list:
7225           size += DWARF_OFFSET_SIZE;
7226           break;
7227         case dw_val_class_range_list:
7228           size += DWARF_OFFSET_SIZE;
7229           break;
7230         case dw_val_class_const:
7231           size += size_of_sleb128 (AT_int (a));
7232           break;
7233         case dw_val_class_unsigned_const:
7234           size += constant_size (AT_unsigned (a));
7235           break;
7236         case dw_val_class_long_long:
7237           size += 1 + 2*HOST_BITS_PER_LONG/HOST_BITS_PER_CHAR; /* block */
7238           break;
7239         case dw_val_class_vec:
7240           size += 1 + (a->dw_attr_val.v.val_vec.length
7241                        * a->dw_attr_val.v.val_vec.elt_size); /* block */
7242           break;
7243         case dw_val_class_flag:
7244           size += 1;
7245           break;
7246         case dw_val_class_die_ref:
7247           if (AT_ref_external (a))
7248             size += DWARF2_ADDR_SIZE;
7249           else
7250             size += DWARF_OFFSET_SIZE;
7251           break;
7252         case dw_val_class_fde_ref:
7253           size += DWARF_OFFSET_SIZE;
7254           break;
7255         case dw_val_class_lbl_id:
7256           size += DWARF2_ADDR_SIZE;
7257           break;
7258         case dw_val_class_lineptr:
7259         case dw_val_class_macptr:
7260           size += DWARF_OFFSET_SIZE;
7261           break;
7262         case dw_val_class_str:
7263           if (AT_string_form (a) == DW_FORM_strp)
7264             size += DWARF_OFFSET_SIZE;
7265           else
7266             size += strlen (a->dw_attr_val.v.val_str->str) + 1;
7267           break;
7268         case dw_val_class_file:
7269           size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
7270           break;
7271         default:
7272           gcc_unreachable ();
7273         }
7274     }
7275
7276   return size;
7277 }
7278
7279 /* Size the debugging information associated with a given DIE.  Visits the
7280    DIE's children recursively.  Updates the global variable next_die_offset, on
7281    each time through.  Uses the current value of next_die_offset to update the
7282    die_offset field in each DIE.  */
7283
7284 static void
7285 calc_die_sizes (dw_die_ref die)
7286 {
7287   dw_die_ref c;
7288
7289   die->die_offset = next_die_offset;
7290   next_die_offset += size_of_die (die);
7291
7292   FOR_EACH_CHILD (die, c, calc_die_sizes (c));
7293
7294   if (die->die_child != NULL)
7295     /* Count the null byte used to terminate sibling lists.  */
7296     next_die_offset += 1;
7297 }
7298
7299 /* Set the marks for a die and its children.  We do this so
7300    that we know whether or not a reference needs to use FORM_ref_addr; only
7301    DIEs in the same CU will be marked.  We used to clear out the offset
7302    and use that as the flag, but ran into ordering problems.  */
7303
7304 static void
7305 mark_dies (dw_die_ref die)
7306 {
7307   dw_die_ref c;
7308
7309   gcc_assert (!die->die_mark);
7310
7311   die->die_mark = 1;
7312   FOR_EACH_CHILD (die, c, mark_dies (c));
7313 }
7314
7315 /* Clear the marks for a die and its children.  */
7316
7317 static void
7318 unmark_dies (dw_die_ref die)
7319 {
7320   dw_die_ref c;
7321
7322   gcc_assert (die->die_mark);
7323
7324   die->die_mark = 0;
7325   FOR_EACH_CHILD (die, c, unmark_dies (c));
7326 }
7327
7328 /* Clear the marks for a die, its children and referred dies.  */
7329
7330 static void
7331 unmark_all_dies (dw_die_ref die)
7332 {
7333   dw_die_ref c;
7334   dw_attr_ref a;
7335   unsigned ix;
7336
7337   if (!die->die_mark)
7338     return;
7339   die->die_mark = 0;
7340
7341   FOR_EACH_CHILD (die, c, unmark_all_dies (c));
7342
7343   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7344     if (AT_class (a) == dw_val_class_die_ref)
7345       unmark_all_dies (AT_ref (a));
7346 }
7347
7348 /* Return the size of the .debug_pubnames or .debug_pubtypes table
7349    generated for the compilation unit.  */
7350
7351 static unsigned long
7352 size_of_pubnames (VEC (pubname_entry, gc) * names)
7353 {
7354   unsigned long size;
7355   unsigned i;
7356   pubname_ref p;
7357
7358   size = DWARF_PUBNAMES_HEADER_SIZE;
7359   for (i = 0; VEC_iterate (pubname_entry, names, i, p); i++)
7360     if (names != pubtype_table
7361         || p->die->die_offset != 0
7362         || !flag_eliminate_unused_debug_types)
7363       size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
7364
7365   size += DWARF_OFFSET_SIZE;
7366   return size;
7367 }
7368
7369 /* Return the size of the information in the .debug_aranges section.  */
7370
7371 static unsigned long
7372 size_of_aranges (void)
7373 {
7374   unsigned long size;
7375
7376   size = DWARF_ARANGES_HEADER_SIZE;
7377
7378   /* Count the address/length pair for this compilation unit.  */
7379   if (text_section_used)
7380     size += 2 * DWARF2_ADDR_SIZE;
7381   if (cold_text_section_used)
7382     size += 2 * DWARF2_ADDR_SIZE;
7383   size += 2 * DWARF2_ADDR_SIZE * arange_table_in_use;
7384
7385   /* Count the two zero words used to terminated the address range table.  */
7386   size += 2 * DWARF2_ADDR_SIZE;
7387   return size;
7388 }
7389 \f
7390 /* Select the encoding of an attribute value.  */
7391
7392 static enum dwarf_form
7393 value_format (dw_attr_ref a)
7394 {
7395   switch (a->dw_attr_val.val_class)
7396     {
7397     case dw_val_class_addr:
7398       return DW_FORM_addr;
7399     case dw_val_class_range_list:
7400     case dw_val_class_offset:
7401     case dw_val_class_loc_list:
7402       switch (DWARF_OFFSET_SIZE)
7403         {
7404         case 4:
7405           return DW_FORM_data4;
7406         case 8:
7407           return DW_FORM_data8;
7408         default:
7409           gcc_unreachable ();
7410         }
7411     case dw_val_class_loc:
7412       switch (constant_size (size_of_locs (AT_loc (a))))
7413         {
7414         case 1:
7415           return DW_FORM_block1;
7416         case 2:
7417           return DW_FORM_block2;
7418         default:
7419           gcc_unreachable ();
7420         }
7421     case dw_val_class_const:
7422       return DW_FORM_sdata;
7423     case dw_val_class_unsigned_const:
7424       switch (constant_size (AT_unsigned (a)))
7425         {
7426         case 1:
7427           return DW_FORM_data1;
7428         case 2:
7429           return DW_FORM_data2;
7430         case 4:
7431           return DW_FORM_data4;
7432         case 8:
7433           return DW_FORM_data8;
7434         default:
7435           gcc_unreachable ();
7436         }
7437     case dw_val_class_long_long:
7438       return DW_FORM_block1;
7439     case dw_val_class_vec:
7440       return DW_FORM_block1;
7441     case dw_val_class_flag:
7442       return DW_FORM_flag;
7443     case dw_val_class_die_ref:
7444       if (AT_ref_external (a))
7445         return DW_FORM_ref_addr;
7446       else
7447         return DW_FORM_ref;
7448     case dw_val_class_fde_ref:
7449       return DW_FORM_data;
7450     case dw_val_class_lbl_id:
7451       return DW_FORM_addr;
7452     case dw_val_class_lineptr:
7453     case dw_val_class_macptr:
7454       return DW_FORM_data;
7455     case dw_val_class_str:
7456       return AT_string_form (a);
7457     case dw_val_class_file:
7458       switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
7459         {
7460         case 1:
7461           return DW_FORM_data1;
7462         case 2:
7463           return DW_FORM_data2;
7464         case 4:
7465           return DW_FORM_data4;
7466         default:
7467           gcc_unreachable ();
7468         }
7469
7470     default:
7471       gcc_unreachable ();
7472     }
7473 }
7474
7475 /* Output the encoding of an attribute value.  */
7476
7477 static void
7478 output_value_format (dw_attr_ref a)
7479 {
7480   enum dwarf_form form = value_format (a);
7481
7482   dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
7483 }
7484
7485 /* Output the .debug_abbrev section which defines the DIE abbreviation
7486    table.  */
7487
7488 static void
7489 output_abbrev_section (void)
7490 {
7491   unsigned long abbrev_id;
7492
7493   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7494     {
7495       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7496       unsigned ix;
7497       dw_attr_ref a_attr;
7498
7499       dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
7500       dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
7501                                    dwarf_tag_name (abbrev->die_tag));
7502
7503       if (abbrev->die_child != NULL)
7504         dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
7505       else
7506         dw2_asm_output_data (1, DW_children_no, "DW_children_no");
7507
7508       for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
7509            ix++)
7510         {
7511           dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
7512                                        dwarf_attr_name (a_attr->dw_attr));
7513           output_value_format (a_attr);
7514         }
7515
7516       dw2_asm_output_data (1, 0, NULL);
7517       dw2_asm_output_data (1, 0, NULL);
7518     }
7519
7520   /* Terminate the table.  */
7521   dw2_asm_output_data (1, 0, NULL);
7522 }
7523
7524 /* Output a symbol we can use to refer to this DIE from another CU.  */
7525
7526 static inline void
7527 output_die_symbol (dw_die_ref die)
7528 {
7529   char *sym = die->die_symbol;
7530
7531   if (sym == 0)
7532     return;
7533
7534   if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
7535     /* We make these global, not weak; if the target doesn't support
7536        .linkonce, it doesn't support combining the sections, so debugging
7537        will break.  */
7538     targetm.asm_out.globalize_label (asm_out_file, sym);
7539
7540   ASM_OUTPUT_LABEL (asm_out_file, sym);
7541 }
7542
7543 /* Return a new location list, given the begin and end range, and the
7544    expression. gensym tells us whether to generate a new internal symbol for
7545    this location list node, which is done for the head of the list only.  */
7546
7547 static inline dw_loc_list_ref
7548 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
7549               const char *section, unsigned int gensym)
7550 {
7551   dw_loc_list_ref retlist = GGC_CNEW (dw_loc_list_node);
7552
7553   retlist->begin = begin;
7554   retlist->end = end;
7555   retlist->expr = expr;
7556   retlist->section = section;
7557   if (gensym)
7558     retlist->ll_symbol = gen_internal_sym ("LLST");
7559
7560   return retlist;
7561 }
7562
7563 /* Add a location description expression to a location list.  */
7564
7565 static inline void
7566 add_loc_descr_to_loc_list (dw_loc_list_ref *list_head, dw_loc_descr_ref descr,
7567                            const char *begin, const char *end,
7568                            const char *section)
7569 {
7570   dw_loc_list_ref *d;
7571
7572   /* Find the end of the chain.  */
7573   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
7574     ;
7575
7576   /* Add a new location list node to the list.  */
7577   *d = new_loc_list (descr, begin, end, section, 0);
7578 }
7579
7580 /* Output the location list given to us.  */
7581
7582 static void
7583 output_loc_list (dw_loc_list_ref list_head)
7584 {
7585   dw_loc_list_ref curr = list_head;
7586
7587   ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
7588
7589   /* Walk the location list, and output each range + expression.  */
7590   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
7591     {
7592       unsigned long size;
7593       /* Don't output an entry that starts and ends at the same address.  */
7594       if (strcmp (curr->begin, curr->end) == 0)
7595         continue;
7596       if (!have_multiple_function_sections)
7597         {
7598           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
7599                                 "Location list begin address (%s)",
7600                                 list_head->ll_symbol);
7601           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
7602                                 "Location list end address (%s)",
7603                                 list_head->ll_symbol);
7604         }
7605       else
7606         {
7607           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
7608                                "Location list begin address (%s)",
7609                                list_head->ll_symbol);
7610           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
7611                                "Location list end address (%s)",
7612                                list_head->ll_symbol);
7613         }
7614       size = size_of_locs (curr->expr);
7615
7616       /* Output the block length for this list of location operations.  */
7617       gcc_assert (size <= 0xffff);
7618       dw2_asm_output_data (2, size, "%s", "Location expression size");
7619
7620       output_loc_sequence (curr->expr);
7621     }
7622
7623   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
7624                        "Location list terminator begin (%s)",
7625                        list_head->ll_symbol);
7626   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
7627                        "Location list terminator end (%s)",
7628                        list_head->ll_symbol);
7629 }
7630
7631 /* Output the DIE and its attributes.  Called recursively to generate
7632    the definitions of each child DIE.  */
7633
7634 static void
7635 output_die (dw_die_ref die)
7636 {
7637   dw_attr_ref a;
7638   dw_die_ref c;
7639   unsigned long size;
7640   unsigned ix;
7641
7642   /* If someone in another CU might refer to us, set up a symbol for
7643      them to point to.  */
7644   if (die->die_symbol)
7645     output_die_symbol (die);
7646
7647   dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (0x%lx) %s)",
7648                                (unsigned long)die->die_offset,
7649                                dwarf_tag_name (die->die_tag));
7650
7651   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7652     {
7653       const char *name = dwarf_attr_name (a->dw_attr);
7654
7655       switch (AT_class (a))
7656         {
7657         case dw_val_class_addr:
7658           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
7659           break;
7660
7661         case dw_val_class_offset:
7662           dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
7663                                "%s", name);
7664           break;
7665
7666         case dw_val_class_range_list:
7667           {
7668             char *p = strchr (ranges_section_label, '\0');
7669
7670             sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
7671                      a->dw_attr_val.v.val_offset);
7672             dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
7673                                    debug_ranges_section, "%s", name);
7674             *p = '\0';
7675           }
7676           break;
7677
7678         case dw_val_class_loc:
7679           size = size_of_locs (AT_loc (a));
7680
7681           /* Output the block length for this list of location operations.  */
7682           dw2_asm_output_data (constant_size (size), size, "%s", name);
7683
7684           output_loc_sequence (AT_loc (a));
7685           break;
7686
7687         case dw_val_class_const:
7688           /* ??? It would be slightly more efficient to use a scheme like is
7689              used for unsigned constants below, but gdb 4.x does not sign
7690              extend.  Gdb 5.x does sign extend.  */
7691           dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
7692           break;
7693
7694         case dw_val_class_unsigned_const:
7695           dw2_asm_output_data (constant_size (AT_unsigned (a)),
7696                                AT_unsigned (a), "%s", name);
7697           break;
7698
7699         case dw_val_class_long_long:
7700           {
7701             unsigned HOST_WIDE_INT first, second;
7702
7703             dw2_asm_output_data (1,
7704                                  2 * HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
7705                                  "%s", name);
7706
7707             if (WORDS_BIG_ENDIAN)
7708               {
7709                 first = a->dw_attr_val.v.val_long_long.hi;
7710                 second = a->dw_attr_val.v.val_long_long.low;
7711               }
7712             else
7713               {
7714                 first = a->dw_attr_val.v.val_long_long.low;
7715                 second = a->dw_attr_val.v.val_long_long.hi;
7716               }
7717
7718             dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
7719                                  first, "long long constant");
7720             dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
7721                                  second, NULL);
7722           }
7723           break;
7724
7725         case dw_val_class_vec:
7726           {
7727             unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
7728             unsigned int len = a->dw_attr_val.v.val_vec.length;
7729             unsigned int i;
7730             unsigned char *p;
7731
7732             dw2_asm_output_data (1, len * elt_size, "%s", name);
7733             if (elt_size > sizeof (HOST_WIDE_INT))
7734               {
7735                 elt_size /= 2;
7736                 len *= 2;
7737               }
7738             for (i = 0, p = a->dw_attr_val.v.val_vec.array;
7739                  i < len;
7740                  i++, p += elt_size)
7741               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
7742                                    "fp or vector constant word %u", i);
7743             break;
7744           }
7745
7746         case dw_val_class_flag:
7747           dw2_asm_output_data (1, AT_flag (a), "%s", name);
7748           break;
7749
7750         case dw_val_class_loc_list:
7751           {
7752             char *sym = AT_loc_list (a)->ll_symbol;
7753
7754             gcc_assert (sym);
7755             dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
7756                                    "%s", name);
7757           }
7758           break;
7759
7760         case dw_val_class_die_ref:
7761           if (AT_ref_external (a))
7762             {
7763               char *sym = AT_ref (a)->die_symbol;
7764
7765               gcc_assert (sym);
7766               dw2_asm_output_offset (DWARF2_ADDR_SIZE, sym, debug_info_section,
7767                                      "%s", name);
7768             }
7769           else
7770             {
7771               gcc_assert (AT_ref (a)->die_offset);
7772               dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
7773                                    "%s", name);
7774             }
7775           break;
7776
7777         case dw_val_class_fde_ref:
7778           {
7779             char l1[20];
7780
7781             ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
7782                                          a->dw_attr_val.v.val_fde_index * 2);
7783             dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
7784                                    "%s", name);
7785           }
7786           break;
7787
7788         case dw_val_class_lbl_id:
7789           dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
7790           break;
7791
7792         case dw_val_class_lineptr:
7793           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
7794                                  debug_line_section, "%s", name);
7795           break;
7796
7797         case dw_val_class_macptr:
7798           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
7799                                  debug_macinfo_section, "%s", name);
7800           break;
7801
7802         case dw_val_class_str:
7803           if (AT_string_form (a) == DW_FORM_strp)
7804             dw2_asm_output_offset (DWARF_OFFSET_SIZE,
7805                                    a->dw_attr_val.v.val_str->label,
7806                                    debug_str_section,
7807                                    "%s: \"%s\"", name, AT_string (a));
7808           else
7809             dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
7810           break;
7811
7812         case dw_val_class_file:
7813           {
7814             int f = maybe_emit_file (a->dw_attr_val.v.val_file);
7815
7816             dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
7817                                  a->dw_attr_val.v.val_file->filename);
7818             break;
7819           }
7820
7821         default:
7822           gcc_unreachable ();
7823         }
7824     }
7825
7826   FOR_EACH_CHILD (die, c, output_die (c));
7827
7828   /* Add null byte to terminate sibling list.  */
7829   if (die->die_child != NULL)
7830     dw2_asm_output_data (1, 0, "end of children of DIE 0x%lx",
7831                          (unsigned long) die->die_offset);
7832 }
7833
7834 /* Output the compilation unit that appears at the beginning of the
7835    .debug_info section, and precedes the DIE descriptions.  */
7836
7837 static void
7838 output_compilation_unit_header (void)
7839 {
7840   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7841     dw2_asm_output_data (4, 0xffffffff,
7842       "Initial length escape value indicating 64-bit DWARF extension");
7843   dw2_asm_output_data (DWARF_OFFSET_SIZE,
7844                        next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
7845                        "Length of Compilation Unit Info");
7846   dw2_asm_output_data (2, DWARF_VERSION, "DWARF version number");
7847   dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
7848                          debug_abbrev_section,
7849                          "Offset Into Abbrev. Section");
7850   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
7851 }
7852
7853 /* Output the compilation unit DIE and its children.  */
7854
7855 static void
7856 output_comp_unit (dw_die_ref die, int output_if_empty)
7857 {
7858   const char *secname;
7859   char *oldsym, *tmp;
7860
7861   /* Unless we are outputting main CU, we may throw away empty ones.  */
7862   if (!output_if_empty && die->die_child == NULL)
7863     return;
7864
7865   /* Even if there are no children of this DIE, we must output the information
7866      about the compilation unit.  Otherwise, on an empty translation unit, we
7867      will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
7868      will then complain when examining the file.  First mark all the DIEs in
7869      this CU so we know which get local refs.  */
7870   mark_dies (die);
7871
7872   build_abbrev_table (die);
7873
7874   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
7875   next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
7876   calc_die_sizes (die);
7877
7878   oldsym = die->die_symbol;
7879   if (oldsym)
7880     {
7881       tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
7882
7883       sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
7884       secname = tmp;
7885       die->die_symbol = NULL;
7886       switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
7887     }
7888   else
7889     switch_to_section (debug_info_section);
7890
7891   /* Output debugging information.  */
7892   output_compilation_unit_header ();
7893   output_die (die);
7894
7895   /* Leave the marks on the main CU, so we can check them in
7896      output_pubnames.  */
7897   if (oldsym)
7898     {
7899       unmark_dies (die);
7900       die->die_symbol = oldsym;
7901     }
7902 }
7903
7904 /* Return the DWARF2/3 pubname associated with a decl.  */
7905
7906 static const char *
7907 dwarf2_name (tree decl, int scope)
7908 {
7909   return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
7910 }
7911
7912 /* Add a new entry to .debug_pubnames if appropriate.  */
7913
7914 static void
7915 add_pubname_string (const char *str, dw_die_ref die)
7916 {
7917   pubname_entry e;
7918
7919   e.die = die;
7920   e.name = xstrdup (str);
7921   VEC_safe_push (pubname_entry, gc, pubname_table, &e);
7922 }
7923
7924 static void
7925 add_pubname (tree decl, dw_die_ref die)
7926 {
7927
7928   if (TREE_PUBLIC (decl))
7929     add_pubname_string (dwarf2_name (decl, 1), die);
7930 }
7931
7932 /* Add a new entry to .debug_pubtypes if appropriate.  */
7933
7934 static void
7935 add_pubtype (tree decl, dw_die_ref die)
7936 {
7937   pubname_entry e;
7938
7939   e.name = NULL;
7940   if ((TREE_PUBLIC (decl)
7941        || die->die_parent == comp_unit_die)
7942       && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
7943     {
7944       e.die = die;
7945       if (TYPE_P (decl))
7946         {
7947           if (TYPE_NAME (decl))
7948             {
7949               if (TREE_CODE (TYPE_NAME (decl)) == IDENTIFIER_NODE)
7950                 e.name = IDENTIFIER_POINTER (TYPE_NAME (decl));
7951               else if (TREE_CODE (TYPE_NAME (decl)) == TYPE_DECL
7952                        && DECL_NAME (TYPE_NAME (decl)))
7953                 e.name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (decl)));
7954               else
7955                e.name = xstrdup ((const char *) get_AT_string (die, DW_AT_name));
7956             }
7957         }
7958       else
7959         e.name = xstrdup (dwarf2_name (decl, 1));
7960
7961       /* If we don't have a name for the type, there's no point in adding
7962          it to the table.  */
7963       if (e.name && e.name[0] != '\0')
7964         VEC_safe_push (pubname_entry, gc, pubtype_table, &e);
7965     }
7966 }
7967
7968 /* Output the public names table used to speed up access to externally
7969    visible names; or the public types table used to find type definitions.  */
7970
7971 static void
7972 output_pubnames (VEC (pubname_entry, gc) * names)
7973 {
7974   unsigned i;
7975   unsigned long pubnames_length = size_of_pubnames (names);
7976   pubname_ref pub;
7977
7978   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7979     dw2_asm_output_data (4, 0xffffffff,
7980       "Initial length escape value indicating 64-bit DWARF extension");
7981   if (names == pubname_table)
7982     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
7983                          "Length of Public Names Info");
7984   else
7985     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
7986                          "Length of Public Type Names Info");
7987   dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
7988   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
7989                          debug_info_section,
7990                          "Offset of Compilation Unit Info");
7991   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
7992                        "Compilation Unit Length");
7993
7994   for (i = 0; VEC_iterate (pubname_entry, names, i, pub); i++)
7995     {
7996       /* We shouldn't see pubnames for DIEs outside of the main CU.  */
7997       if (names == pubname_table)
7998         gcc_assert (pub->die->die_mark);
7999
8000       if (names != pubtype_table
8001           || pub->die->die_offset != 0
8002           || !flag_eliminate_unused_debug_types)
8003         {
8004           dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
8005                                "DIE offset");
8006
8007           dw2_asm_output_nstring (pub->name, -1, "external name");
8008         }
8009     }
8010
8011   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
8012 }
8013
8014 /* Add a new entry to .debug_aranges if appropriate.  */
8015
8016 static void
8017 add_arange (tree decl, dw_die_ref die)
8018 {
8019   if (! DECL_SECTION_NAME (decl))
8020     return;
8021
8022   if (arange_table_in_use == arange_table_allocated)
8023     {
8024       arange_table_allocated += ARANGE_TABLE_INCREMENT;
8025       arange_table = GGC_RESIZEVEC (dw_die_ref, arange_table,
8026                                     arange_table_allocated);
8027       memset (arange_table + arange_table_in_use, 0,
8028               ARANGE_TABLE_INCREMENT * sizeof (dw_die_ref));
8029     }
8030
8031   arange_table[arange_table_in_use++] = die;
8032 }
8033
8034 /* Output the information that goes into the .debug_aranges table.
8035    Namely, define the beginning and ending address range of the
8036    text section generated for this compilation unit.  */
8037
8038 static void
8039 output_aranges (void)
8040 {
8041   unsigned i;
8042   unsigned long aranges_length = size_of_aranges ();
8043
8044   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8045     dw2_asm_output_data (4, 0xffffffff,
8046       "Initial length escape value indicating 64-bit DWARF extension");
8047   dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
8048                        "Length of Address Ranges Info");
8049   dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
8050   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
8051                          debug_info_section,
8052                          "Offset of Compilation Unit Info");
8053   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
8054   dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
8055
8056   /* We need to align to twice the pointer size here.  */
8057   if (DWARF_ARANGES_PAD_SIZE)
8058     {
8059       /* Pad using a 2 byte words so that padding is correct for any
8060          pointer size.  */
8061       dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
8062                            2 * DWARF2_ADDR_SIZE);
8063       for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
8064         dw2_asm_output_data (2, 0, NULL);
8065     }
8066
8067   /* It is necessary not to output these entries if the sections were
8068      not used; if the sections were not used, the length will be 0 and
8069      the address may end up as 0 if the section is discarded by ld
8070      --gc-sections, leaving an invalid (0, 0) entry that can be
8071      confused with the terminator.  */
8072   if (text_section_used)
8073     {
8074       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
8075       dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
8076                             text_section_label, "Length");
8077     }
8078   if (cold_text_section_used)
8079     {
8080       dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
8081                            "Address");
8082       dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
8083                             cold_text_section_label, "Length");
8084     }
8085
8086   for (i = 0; i < arange_table_in_use; i++)
8087     {
8088       dw_die_ref die = arange_table[i];
8089
8090       /* We shouldn't see aranges for DIEs outside of the main CU.  */
8091       gcc_assert (die->die_mark);
8092
8093       if (die->die_tag == DW_TAG_subprogram)
8094         {
8095           dw2_asm_output_addr (DWARF2_ADDR_SIZE, get_AT_low_pc (die),
8096                                "Address");
8097           dw2_asm_output_delta (DWARF2_ADDR_SIZE, get_AT_hi_pc (die),
8098                                 get_AT_low_pc (die), "Length");
8099         }
8100       else
8101         {
8102           /* A static variable; extract the symbol from DW_AT_location.
8103              Note that this code isn't currently hit, as we only emit
8104              aranges for functions (jason 9/23/99).  */
8105           dw_attr_ref a = get_AT (die, DW_AT_location);
8106           dw_loc_descr_ref loc;
8107
8108           gcc_assert (a && AT_class (a) == dw_val_class_loc);
8109
8110           loc = AT_loc (a);
8111           gcc_assert (loc->dw_loc_opc == DW_OP_addr);
8112
8113           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE,
8114                                    loc->dw_loc_oprnd1.v.val_addr, "Address");
8115           dw2_asm_output_data (DWARF2_ADDR_SIZE,
8116                                get_AT_unsigned (die, DW_AT_byte_size),
8117                                "Length");
8118         }
8119     }
8120
8121   /* Output the terminator words.  */
8122   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8123   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8124 }
8125
8126 /* Add a new entry to .debug_ranges.  Return the offset at which it
8127    was placed.  */
8128
8129 static unsigned int
8130 add_ranges_num (int num)
8131 {
8132   unsigned int in_use = ranges_table_in_use;
8133
8134   if (in_use == ranges_table_allocated)
8135     {
8136       ranges_table_allocated += RANGES_TABLE_INCREMENT;
8137       ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
8138                                     ranges_table_allocated);
8139       memset (ranges_table + ranges_table_in_use, 0,
8140               RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
8141     }
8142
8143   ranges_table[in_use].num = num;
8144   ranges_table_in_use = in_use + 1;
8145
8146   return in_use * 2 * DWARF2_ADDR_SIZE;
8147 }
8148
8149 /* Add a new entry to .debug_ranges corresponding to a block, or a
8150    range terminator if BLOCK is NULL.  */
8151
8152 static unsigned int
8153 add_ranges (const_tree block)
8154 {
8155   return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
8156 }
8157
8158 /* Add a new entry to .debug_ranges corresponding to a pair of
8159    labels.  */
8160
8161 static unsigned int
8162 add_ranges_by_labels (const char *begin, const char *end)
8163 {
8164   unsigned int in_use = ranges_by_label_in_use;
8165
8166   if (in_use == ranges_by_label_allocated)
8167     {
8168       ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
8169       ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
8170                                        ranges_by_label,
8171                                        ranges_by_label_allocated);
8172       memset (ranges_by_label + ranges_by_label_in_use, 0,
8173               RANGES_TABLE_INCREMENT
8174               * sizeof (struct dw_ranges_by_label_struct));
8175     }
8176
8177   ranges_by_label[in_use].begin = begin;
8178   ranges_by_label[in_use].end = end;
8179   ranges_by_label_in_use = in_use + 1;
8180
8181   return add_ranges_num (-(int)in_use - 1);
8182 }
8183
8184 static void
8185 output_ranges (void)
8186 {
8187   unsigned i;
8188   static const char *const start_fmt = "Offset 0x%x";
8189   const char *fmt = start_fmt;
8190
8191   for (i = 0; i < ranges_table_in_use; i++)
8192     {
8193       int block_num = ranges_table[i].num;
8194
8195       if (block_num > 0)
8196         {
8197           char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
8198           char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
8199
8200           ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
8201           ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
8202
8203           /* If all code is in the text section, then the compilation
8204              unit base address defaults to DW_AT_low_pc, which is the
8205              base of the text section.  */
8206           if (!have_multiple_function_sections)
8207             {
8208               dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
8209                                     text_section_label,
8210                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
8211               dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
8212                                     text_section_label, NULL);
8213             }
8214
8215           /* Otherwise, the compilation unit base address is zero,
8216              which allows us to use absolute addresses, and not worry
8217              about whether the target supports cross-section
8218              arithmetic.  */
8219           else
8220             {
8221               dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
8222                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
8223               dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
8224             }
8225
8226           fmt = NULL;
8227         }
8228
8229       /* Negative block_num stands for an index into ranges_by_label.  */
8230       else if (block_num < 0)
8231         {
8232           int lab_idx = - block_num - 1;
8233
8234           if (!have_multiple_function_sections)
8235             {
8236               gcc_unreachable ();
8237 #if 0
8238               /* If we ever use add_ranges_by_labels () for a single
8239                  function section, all we have to do is to take out
8240                  the #if 0 above.  */
8241               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
8242                                     ranges_by_label[lab_idx].begin,
8243                                     text_section_label,
8244                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
8245               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
8246                                     ranges_by_label[lab_idx].end,
8247                                     text_section_label, NULL);
8248 #endif
8249             }
8250           else
8251             {
8252               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
8253                                    ranges_by_label[lab_idx].begin,
8254                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
8255               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
8256                                    ranges_by_label[lab_idx].end,
8257                                    NULL);
8258             }
8259         }
8260       else
8261         {
8262           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8263           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8264           fmt = start_fmt;
8265         }
8266     }
8267 }
8268
8269 /* Data structure containing information about input files.  */
8270 struct file_info
8271 {
8272   const char *path;     /* Complete file name.  */
8273   const char *fname;    /* File name part.  */
8274   int length;           /* Length of entire string.  */
8275   struct dwarf_file_data * file_idx;    /* Index in input file table.  */
8276   int dir_idx;          /* Index in directory table.  */
8277 };
8278
8279 /* Data structure containing information about directories with source
8280    files.  */
8281 struct dir_info
8282 {
8283   const char *path;     /* Path including directory name.  */
8284   int length;           /* Path length.  */
8285   int prefix;           /* Index of directory entry which is a prefix.  */
8286   int count;            /* Number of files in this directory.  */
8287   int dir_idx;          /* Index of directory used as base.  */
8288 };
8289
8290 /* Callback function for file_info comparison.  We sort by looking at
8291    the directories in the path.  */
8292
8293 static int
8294 file_info_cmp (const void *p1, const void *p2)
8295 {
8296   const struct file_info *const s1 = (const struct file_info *) p1;
8297   const struct file_info *const s2 = (const struct file_info *) p2;
8298   const unsigned char *cp1;
8299   const unsigned char *cp2;
8300
8301   /* Take care of file names without directories.  We need to make sure that
8302      we return consistent values to qsort since some will get confused if
8303      we return the same value when identical operands are passed in opposite
8304      orders.  So if neither has a directory, return 0 and otherwise return
8305      1 or -1 depending on which one has the directory.  */
8306   if ((s1->path == s1->fname || s2->path == s2->fname))
8307     return (s2->path == s2->fname) - (s1->path == s1->fname);
8308
8309   cp1 = (const unsigned char *) s1->path;
8310   cp2 = (const unsigned char *) s2->path;
8311
8312   while (1)
8313     {
8314       ++cp1;
8315       ++cp2;
8316       /* Reached the end of the first path?  If so, handle like above.  */
8317       if ((cp1 == (const unsigned char *) s1->fname)
8318           || (cp2 == (const unsigned char *) s2->fname))
8319         return ((cp2 == (const unsigned char *) s2->fname)
8320                 - (cp1 == (const unsigned char *) s1->fname));
8321
8322       /* Character of current path component the same?  */
8323       else if (*cp1 != *cp2)
8324         return *cp1 - *cp2;
8325     }
8326 }
8327
8328 struct file_name_acquire_data
8329 {
8330   struct file_info *files;
8331   int used_files;
8332   int max_files;
8333 };
8334
8335 /* Traversal function for the hash table.  */
8336
8337 static int
8338 file_name_acquire (void ** slot, void *data)
8339 {
8340   struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
8341   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
8342   struct file_info *fi;
8343   const char *f;
8344
8345   gcc_assert (fnad->max_files >= d->emitted_number);
8346
8347   if (! d->emitted_number)
8348     return 1;
8349
8350   gcc_assert (fnad->max_files != fnad->used_files);
8351
8352   fi = fnad->files + fnad->used_files++;
8353
8354   /* Skip all leading "./".  */
8355   f = d->filename;
8356   while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
8357     f += 2;
8358
8359   /* Create a new array entry.  */
8360   fi->path = f;
8361   fi->length = strlen (f);
8362   fi->file_idx = d;
8363
8364   /* Search for the file name part.  */
8365   f = strrchr (f, DIR_SEPARATOR);
8366 #if defined (DIR_SEPARATOR_2)
8367   {
8368     char *g = strrchr (fi->path, DIR_SEPARATOR_2);
8369
8370     if (g != NULL)
8371       {
8372         if (f == NULL || f < g)
8373           f = g;
8374       }
8375   }
8376 #endif
8377
8378   fi->fname = f == NULL ? fi->path : f + 1;
8379   return 1;
8380 }
8381
8382 /* Output the directory table and the file name table.  We try to minimize
8383    the total amount of memory needed.  A heuristic is used to avoid large
8384    slowdowns with many input files.  */
8385
8386 static void
8387 output_file_names (void)
8388 {
8389   struct file_name_acquire_data fnad;
8390   int numfiles;
8391   struct file_info *files;
8392   struct dir_info *dirs;
8393   int *saved;
8394   int *savehere;
8395   int *backmap;
8396   int ndirs;
8397   int idx_offset;
8398   int i;
8399   int idx;
8400
8401   if (!last_emitted_file)
8402     {
8403       dw2_asm_output_data (1, 0, "End directory table");
8404       dw2_asm_output_data (1, 0, "End file name table");
8405       return;
8406     }
8407
8408   numfiles = last_emitted_file->emitted_number;
8409
8410   /* Allocate the various arrays we need.  */
8411   files = XALLOCAVEC (struct file_info, numfiles);
8412   dirs = XALLOCAVEC (struct dir_info, numfiles);
8413
8414   fnad.files = files;
8415   fnad.used_files = 0;
8416   fnad.max_files = numfiles;
8417   htab_traverse (file_table, file_name_acquire, &fnad);
8418   gcc_assert (fnad.used_files == fnad.max_files);
8419
8420   qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
8421
8422   /* Find all the different directories used.  */
8423   dirs[0].path = files[0].path;
8424   dirs[0].length = files[0].fname - files[0].path;
8425   dirs[0].prefix = -1;
8426   dirs[0].count = 1;
8427   dirs[0].dir_idx = 0;
8428   files[0].dir_idx = 0;
8429   ndirs = 1;
8430
8431   for (i = 1; i < numfiles; i++)
8432     if (files[i].fname - files[i].path == dirs[ndirs - 1].length
8433         && memcmp (dirs[ndirs - 1].path, files[i].path,
8434                    dirs[ndirs - 1].length) == 0)
8435       {
8436         /* Same directory as last entry.  */
8437         files[i].dir_idx = ndirs - 1;
8438         ++dirs[ndirs - 1].count;
8439       }
8440     else
8441       {
8442         int j;
8443
8444         /* This is a new directory.  */
8445         dirs[ndirs].path = files[i].path;
8446         dirs[ndirs].length = files[i].fname - files[i].path;
8447         dirs[ndirs].count = 1;
8448         dirs[ndirs].dir_idx = ndirs;
8449         files[i].dir_idx = ndirs;
8450
8451         /* Search for a prefix.  */
8452         dirs[ndirs].prefix = -1;
8453         for (j = 0; j < ndirs; j++)
8454           if (dirs[j].length < dirs[ndirs].length
8455               && dirs[j].length > 1
8456               && (dirs[ndirs].prefix == -1
8457                   || dirs[j].length > dirs[dirs[ndirs].prefix].length)
8458               && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
8459             dirs[ndirs].prefix = j;
8460
8461         ++ndirs;
8462       }
8463
8464   /* Now to the actual work.  We have to find a subset of the directories which
8465      allow expressing the file name using references to the directory table
8466      with the least amount of characters.  We do not do an exhaustive search
8467      where we would have to check out every combination of every single
8468      possible prefix.  Instead we use a heuristic which provides nearly optimal
8469      results in most cases and never is much off.  */
8470   saved = XALLOCAVEC (int, ndirs);
8471   savehere = XALLOCAVEC (int, ndirs);
8472
8473   memset (saved, '\0', ndirs * sizeof (saved[0]));
8474   for (i = 0; i < ndirs; i++)
8475     {
8476       int j;
8477       int total;
8478
8479       /* We can always save some space for the current directory.  But this
8480          does not mean it will be enough to justify adding the directory.  */
8481       savehere[i] = dirs[i].length;
8482       total = (savehere[i] - saved[i]) * dirs[i].count;
8483
8484       for (j = i + 1; j < ndirs; j++)
8485         {
8486           savehere[j] = 0;
8487           if (saved[j] < dirs[i].length)
8488             {
8489               /* Determine whether the dirs[i] path is a prefix of the
8490                  dirs[j] path.  */
8491               int k;
8492
8493               k = dirs[j].prefix;
8494               while (k != -1 && k != (int) i)
8495                 k = dirs[k].prefix;
8496
8497               if (k == (int) i)
8498                 {
8499                   /* Yes it is.  We can possibly save some memory by
8500                      writing the filenames in dirs[j] relative to
8501                      dirs[i].  */
8502                   savehere[j] = dirs[i].length;
8503                   total += (savehere[j] - saved[j]) * dirs[j].count;
8504                 }
8505             }
8506         }
8507
8508       /* Check whether we can save enough to justify adding the dirs[i]
8509          directory.  */
8510       if (total > dirs[i].length + 1)
8511         {
8512           /* It's worthwhile adding.  */
8513           for (j = i; j < ndirs; j++)
8514             if (savehere[j] > 0)
8515               {
8516                 /* Remember how much we saved for this directory so far.  */
8517                 saved[j] = savehere[j];
8518
8519                 /* Remember the prefix directory.  */
8520                 dirs[j].dir_idx = i;
8521               }
8522         }
8523     }
8524
8525   /* Emit the directory name table.  */
8526   idx = 1;
8527   idx_offset = dirs[0].length > 0 ? 1 : 0;
8528   for (i = 1 - idx_offset; i < ndirs; i++)
8529     dw2_asm_output_nstring (dirs[i].path, dirs[i].length - 1,
8530                             "Directory Entry: 0x%x", i + idx_offset);
8531
8532   dw2_asm_output_data (1, 0, "End directory table");
8533
8534   /* We have to emit them in the order of emitted_number since that's
8535      used in the debug info generation.  To do this efficiently we
8536      generate a back-mapping of the indices first.  */
8537   backmap = XALLOCAVEC (int, numfiles);
8538   for (i = 0; i < numfiles; i++)
8539     backmap[files[i].file_idx->emitted_number - 1] = i;
8540
8541   /* Now write all the file names.  */
8542   for (i = 0; i < numfiles; i++)
8543     {
8544       int file_idx = backmap[i];
8545       int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
8546
8547       dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
8548                               "File Entry: 0x%x", (unsigned) i + 1);
8549
8550       /* Include directory index.  */
8551       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
8552
8553       /* Modification time.  */
8554       dw2_asm_output_data_uleb128 (0, NULL);
8555
8556       /* File length in bytes.  */
8557       dw2_asm_output_data_uleb128 (0, NULL);
8558     }
8559
8560   dw2_asm_output_data (1, 0, "End file name table");
8561 }
8562
8563
8564 /* Output the source line number correspondence information.  This
8565    information goes into the .debug_line section.  */
8566
8567 static void
8568 output_line_info (void)
8569 {
8570   char l1[20], l2[20], p1[20], p2[20];
8571   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
8572   char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
8573   unsigned opc;
8574   unsigned n_op_args;
8575   unsigned long lt_index;
8576   unsigned long current_line;
8577   long line_offset;
8578   long line_delta;
8579   unsigned long current_file;
8580   unsigned long function;
8581
8582   ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
8583   ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
8584   ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
8585   ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
8586
8587   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8588     dw2_asm_output_data (4, 0xffffffff,
8589       "Initial length escape value indicating 64-bit DWARF extension");
8590   dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
8591                         "Length of Source Line Info");
8592   ASM_OUTPUT_LABEL (asm_out_file, l1);
8593
8594   dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
8595   dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
8596   ASM_OUTPUT_LABEL (asm_out_file, p1);
8597
8598   /* Define the architecture-dependent minimum instruction length (in
8599    bytes).  In this implementation of DWARF, this field is used for
8600    information purposes only.  Since GCC generates assembly language,
8601    we have no a priori knowledge of how many instruction bytes are
8602    generated for each source line, and therefore can use only the
8603    DW_LNE_set_address and DW_LNS_fixed_advance_pc line information
8604    commands.  Accordingly, we fix this as `1', which is "correct
8605    enough" for all architectures, and don't let the target override.  */
8606   dw2_asm_output_data (1, 1,
8607                        "Minimum Instruction Length");
8608
8609   dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
8610                        "Default is_stmt_start flag");
8611   dw2_asm_output_data (1, DWARF_LINE_BASE,
8612                        "Line Base Value (Special Opcodes)");
8613   dw2_asm_output_data (1, DWARF_LINE_RANGE,
8614                        "Line Range Value (Special Opcodes)");
8615   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
8616                        "Special Opcode Base");
8617
8618   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
8619     {
8620       switch (opc)
8621         {
8622         case DW_LNS_advance_pc:
8623         case DW_LNS_advance_line:
8624         case DW_LNS_set_file:
8625         case DW_LNS_set_column:
8626         case DW_LNS_fixed_advance_pc:
8627           n_op_args = 1;
8628           break;
8629         default:
8630           n_op_args = 0;
8631           break;
8632         }
8633
8634       dw2_asm_output_data (1, n_op_args, "opcode: 0x%x has %d args",
8635                            opc, n_op_args);
8636     }
8637
8638   /* Write out the information about the files we use.  */
8639   output_file_names ();
8640   ASM_OUTPUT_LABEL (asm_out_file, p2);
8641
8642   /* We used to set the address register to the first location in the text
8643      section here, but that didn't accomplish anything since we already
8644      have a line note for the opening brace of the first function.  */
8645
8646   /* Generate the line number to PC correspondence table, encoded as
8647      a series of state machine operations.  */
8648   current_file = 1;
8649   current_line = 1;
8650
8651   if (cfun && in_cold_section_p)
8652     strcpy (prev_line_label, crtl->subsections.cold_section_label);
8653   else
8654     strcpy (prev_line_label, text_section_label);
8655   for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
8656     {
8657       dw_line_info_ref line_info = &line_info_table[lt_index];
8658
8659 #if 0
8660       /* Disable this optimization for now; GDB wants to see two line notes
8661          at the beginning of a function so it can find the end of the
8662          prologue.  */
8663
8664       /* Don't emit anything for redundant notes.  Just updating the
8665          address doesn't accomplish anything, because we already assume
8666          that anything after the last address is this line.  */
8667       if (line_info->dw_line_num == current_line
8668           && line_info->dw_file_num == current_file)
8669         continue;
8670 #endif
8671
8672       /* Emit debug info for the address of the current line.
8673
8674          Unfortunately, we have little choice here currently, and must always
8675          use the most general form.  GCC does not know the address delta
8676          itself, so we can't use DW_LNS_advance_pc.  Many ports do have length
8677          attributes which will give an upper bound on the address range.  We
8678          could perhaps use length attributes to determine when it is safe to
8679          use DW_LNS_fixed_advance_pc.  */
8680
8681       ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
8682       if (0)
8683         {
8684           /* This can handle deltas up to 0xffff.  This takes 3 bytes.  */
8685           dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
8686                                "DW_LNS_fixed_advance_pc");
8687           dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
8688         }
8689       else
8690         {
8691           /* This can handle any delta.  This takes
8692              4+DWARF2_ADDR_SIZE bytes.  */
8693           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8694           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8695           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8696           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
8697         }
8698
8699       strcpy (prev_line_label, line_label);
8700
8701       /* Emit debug info for the source file of the current line, if
8702          different from the previous line.  */
8703       if (line_info->dw_file_num != current_file)
8704         {
8705           current_file = line_info->dw_file_num;
8706           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
8707           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
8708         }
8709
8710       /* Emit debug info for the current line number, choosing the encoding
8711          that uses the least amount of space.  */
8712       if (line_info->dw_line_num != current_line)
8713         {
8714           line_offset = line_info->dw_line_num - current_line;
8715           line_delta = line_offset - DWARF_LINE_BASE;
8716           current_line = line_info->dw_line_num;
8717           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
8718             /* This can handle deltas from -10 to 234, using the current
8719                definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.  This
8720                takes 1 byte.  */
8721             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
8722                                  "line %lu", current_line);
8723           else
8724             {
8725               /* This can handle any delta.  This takes at least 4 bytes,
8726                  depending on the value being encoded.  */
8727               dw2_asm_output_data (1, DW_LNS_advance_line,
8728                                    "advance to line %lu", current_line);
8729               dw2_asm_output_data_sleb128 (line_offset, NULL);
8730               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
8731             }
8732         }
8733       else
8734         /* We still need to start a new row, so output a copy insn.  */
8735         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
8736     }
8737
8738   /* Emit debug info for the address of the end of the function.  */
8739   if (0)
8740     {
8741       dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
8742                            "DW_LNS_fixed_advance_pc");
8743       dw2_asm_output_delta (2, text_end_label, prev_line_label, NULL);
8744     }
8745   else
8746     {
8747       dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8748       dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8749       dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8750       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_end_label, NULL);
8751     }
8752
8753   dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
8754   dw2_asm_output_data_uleb128 (1, NULL);
8755   dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
8756
8757   function = 0;
8758   current_file = 1;
8759   current_line = 1;
8760   for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
8761     {
8762       dw_separate_line_info_ref line_info
8763         = &separate_line_info_table[lt_index];
8764
8765 #if 0
8766       /* Don't emit anything for redundant notes.  */
8767       if (line_info->dw_line_num == current_line
8768           && line_info->dw_file_num == current_file
8769           && line_info->function == function)
8770         goto cont;
8771 #endif
8772
8773       /* Emit debug info for the address of the current line.  If this is
8774          a new function, or the first line of a function, then we need
8775          to handle it differently.  */
8776       ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
8777                                    lt_index);
8778       if (function != line_info->function)
8779         {
8780           function = line_info->function;
8781
8782           /* Set the address register to the first line in the function.  */
8783           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8784           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8785           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8786           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
8787         }
8788       else
8789         {
8790           /* ??? See the DW_LNS_advance_pc comment above.  */
8791           if (0)
8792             {
8793               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
8794                                    "DW_LNS_fixed_advance_pc");
8795               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
8796             }
8797           else
8798             {
8799               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8800               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8801               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8802               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
8803             }
8804         }
8805
8806       strcpy (prev_line_label, line_label);
8807
8808       /* Emit debug info for the source file of the current line, if
8809          different from the previous line.  */
8810       if (line_info->dw_file_num != current_file)
8811         {
8812           current_file = line_info->dw_file_num;
8813           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
8814           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
8815         }
8816
8817       /* Emit debug info for the current line number, choosing the encoding
8818          that uses the least amount of space.  */
8819       if (line_info->dw_line_num != current_line)
8820         {
8821           line_offset = line_info->dw_line_num - current_line;
8822           line_delta = line_offset - DWARF_LINE_BASE;
8823           current_line = line_info->dw_line_num;
8824           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
8825             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
8826                                  "line %lu", current_line);
8827           else
8828             {
8829               dw2_asm_output_data (1, DW_LNS_advance_line,
8830                                    "advance to line %lu", current_line);
8831               dw2_asm_output_data_sleb128 (line_offset, NULL);
8832               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
8833             }
8834         }
8835       else
8836         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
8837
8838 #if 0
8839     cont:
8840 #endif
8841
8842       lt_index++;
8843
8844       /* If we're done with a function, end its sequence.  */
8845       if (lt_index == separate_line_info_table_in_use
8846           || separate_line_info_table[lt_index].function != function)
8847         {
8848           current_file = 1;
8849           current_line = 1;
8850
8851           /* Emit debug info for the address of the end of the function.  */
8852           ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
8853           if (0)
8854             {
8855               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
8856                                    "DW_LNS_fixed_advance_pc");
8857               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
8858             }
8859           else
8860             {
8861               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8862               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8863               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8864               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
8865             }
8866
8867           /* Output the marker for the end of this sequence.  */
8868           dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
8869           dw2_asm_output_data_uleb128 (1, NULL);
8870           dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
8871         }
8872     }
8873
8874   /* Output the marker for the end of the line number info.  */
8875   ASM_OUTPUT_LABEL (asm_out_file, l2);
8876 }
8877 \f
8878 /* Given a pointer to a tree node for some base type, return a pointer to
8879    a DIE that describes the given type.
8880
8881    This routine must only be called for GCC type nodes that correspond to
8882    Dwarf base (fundamental) types.  */
8883
8884 static dw_die_ref
8885 base_type_die (tree type)
8886 {
8887   dw_die_ref base_type_result;
8888   enum dwarf_type encoding;
8889
8890   if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
8891     return 0;
8892
8893   switch (TREE_CODE (type))
8894     {
8895     case INTEGER_TYPE:
8896       if (TYPE_STRING_FLAG (type))
8897         {
8898           if (TYPE_UNSIGNED (type))
8899             encoding = DW_ATE_unsigned_char;
8900           else
8901             encoding = DW_ATE_signed_char;
8902         }
8903       else if (TYPE_UNSIGNED (type))
8904         encoding = DW_ATE_unsigned;
8905       else
8906         encoding = DW_ATE_signed;
8907       break;
8908
8909     case REAL_TYPE:
8910       if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
8911         encoding = DW_ATE_decimal_float;
8912       else
8913         encoding = DW_ATE_float;
8914       break;
8915
8916     case FIXED_POINT_TYPE:
8917       if (TYPE_UNSIGNED (type))
8918         encoding = DW_ATE_unsigned_fixed;
8919       else
8920         encoding = DW_ATE_signed_fixed;
8921       break;
8922
8923       /* Dwarf2 doesn't know anything about complex ints, so use
8924          a user defined type for it.  */
8925     case COMPLEX_TYPE:
8926       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
8927         encoding = DW_ATE_complex_float;
8928       else
8929         encoding = DW_ATE_lo_user;
8930       break;
8931
8932     case BOOLEAN_TYPE:
8933       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
8934       encoding = DW_ATE_boolean;
8935       break;
8936
8937     default:
8938       /* No other TREE_CODEs are Dwarf fundamental types.  */
8939       gcc_unreachable ();
8940     }
8941
8942   base_type_result = new_die (DW_TAG_base_type, comp_unit_die, type);
8943
8944   /* This probably indicates a bug.  */
8945   if (! TYPE_NAME (type))
8946     add_name_attribute (base_type_result, "__unknown__");
8947
8948   add_AT_unsigned (base_type_result, DW_AT_byte_size,
8949                    int_size_in_bytes (type));
8950   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
8951
8952   return base_type_result;
8953 }
8954
8955 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
8956    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
8957
8958 static inline int
8959 is_base_type (tree type)
8960 {
8961   switch (TREE_CODE (type))
8962     {
8963     case ERROR_MARK:
8964     case VOID_TYPE:
8965     case INTEGER_TYPE:
8966     case REAL_TYPE:
8967     case FIXED_POINT_TYPE:
8968     case COMPLEX_TYPE:
8969     case BOOLEAN_TYPE:
8970       return 1;
8971
8972     case ARRAY_TYPE:
8973     case RECORD_TYPE:
8974     case UNION_TYPE:
8975     case QUAL_UNION_TYPE:
8976     case ENUMERAL_TYPE:
8977     case FUNCTION_TYPE:
8978     case METHOD_TYPE:
8979     case POINTER_TYPE:
8980     case REFERENCE_TYPE:
8981     case OFFSET_TYPE:
8982     case LANG_TYPE:
8983     case VECTOR_TYPE:
8984       return 0;
8985
8986     default:
8987       gcc_unreachable ();
8988     }
8989
8990   return 0;
8991 }
8992
8993 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
8994    node, return the size in bits for the type if it is a constant, or else
8995    return the alignment for the type if the type's size is not constant, or
8996    else return BITS_PER_WORD if the type actually turns out to be an
8997    ERROR_MARK node.  */
8998
8999 static inline unsigned HOST_WIDE_INT
9000 simple_type_size_in_bits (const_tree type)
9001 {
9002   if (TREE_CODE (type) == ERROR_MARK)
9003     return BITS_PER_WORD;
9004   else if (TYPE_SIZE (type) == NULL_TREE)
9005     return 0;
9006   else if (host_integerp (TYPE_SIZE (type), 1))
9007     return tree_low_cst (TYPE_SIZE (type), 1);
9008   else
9009     return TYPE_ALIGN (type);
9010 }
9011
9012 /* Return true if the debug information for the given type should be
9013    emitted as a subrange type.  */
9014
9015 static inline bool
9016 is_subrange_type (const_tree type)
9017 {
9018   tree subtype = TREE_TYPE (type);
9019
9020   /* Subrange types are identified by the fact that they are integer
9021      types, and that they have a subtype which is either an integer type
9022      or an enumeral type.  */
9023
9024   if (TREE_CODE (type) != INTEGER_TYPE
9025       || subtype == NULL_TREE)
9026     return false;
9027
9028   if (TREE_CODE (subtype) != INTEGER_TYPE
9029       && TREE_CODE (subtype) != ENUMERAL_TYPE
9030       && TREE_CODE (subtype) != BOOLEAN_TYPE)
9031     return false;
9032
9033   if (TREE_CODE (type) == TREE_CODE (subtype)
9034       && int_size_in_bytes (type) == int_size_in_bytes (subtype)
9035       && TYPE_MIN_VALUE (type) != NULL
9036       && TYPE_MIN_VALUE (subtype) != NULL
9037       && tree_int_cst_equal (TYPE_MIN_VALUE (type), TYPE_MIN_VALUE (subtype))
9038       && TYPE_MAX_VALUE (type) != NULL
9039       && TYPE_MAX_VALUE (subtype) != NULL
9040       && tree_int_cst_equal (TYPE_MAX_VALUE (type), TYPE_MAX_VALUE (subtype)))
9041     {
9042       /* The type and its subtype have the same representation.  If in
9043          addition the two types also have the same name, then the given
9044          type is not a subrange type, but rather a plain base type.  */
9045       /* FIXME: brobecker/2004-03-22:
9046          Sizetype INTEGER_CSTs nodes are canonicalized.  It should
9047          therefore be sufficient to check the TYPE_SIZE node pointers
9048          rather than checking the actual size.  Unfortunately, we have
9049          found some cases, such as in the Ada "integer" type, where
9050          this is not the case.  Until this problem is solved, we need to
9051          keep checking the actual size.  */
9052       tree type_name = TYPE_NAME (type);
9053       tree subtype_name = TYPE_NAME (subtype);
9054
9055       if (type_name != NULL && TREE_CODE (type_name) == TYPE_DECL)
9056         type_name = DECL_NAME (type_name);
9057
9058       if (subtype_name != NULL && TREE_CODE (subtype_name) == TYPE_DECL)
9059         subtype_name = DECL_NAME (subtype_name);
9060
9061       if (type_name == subtype_name)
9062         return false;
9063     }
9064
9065   return true;
9066 }
9067
9068 /*  Given a pointer to a tree node for a subrange type, return a pointer
9069     to a DIE that describes the given type.  */
9070
9071 static dw_die_ref
9072 subrange_type_die (tree type, dw_die_ref context_die)
9073 {
9074   dw_die_ref subrange_die;
9075   const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
9076
9077   if (context_die == NULL)
9078     context_die = comp_unit_die;
9079
9080   subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
9081
9082   if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
9083     {
9084       /* The size of the subrange type and its base type do not match,
9085          so we need to generate a size attribute for the subrange type.  */
9086       add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
9087     }
9088
9089   if (TYPE_MIN_VALUE (type) != NULL)
9090     add_bound_info (subrange_die, DW_AT_lower_bound,
9091                     TYPE_MIN_VALUE (type));
9092   if (TYPE_MAX_VALUE (type) != NULL)
9093     add_bound_info (subrange_die, DW_AT_upper_bound,
9094                     TYPE_MAX_VALUE (type));
9095
9096   return subrange_die;
9097 }
9098
9099 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
9100    entry that chains various modifiers in front of the given type.  */
9101
9102 static dw_die_ref
9103 modified_type_die (tree type, int is_const_type, int is_volatile_type,
9104                    dw_die_ref context_die)
9105 {
9106   enum tree_code code = TREE_CODE (type);
9107   dw_die_ref mod_type_die;
9108   dw_die_ref sub_die = NULL;
9109   tree item_type = NULL;
9110   tree qualified_type;
9111   tree name;
9112
9113   if (code == ERROR_MARK)
9114     return NULL;
9115
9116   /* See if we already have the appropriately qualified variant of
9117      this type.  */
9118   qualified_type
9119     = get_qualified_type (type,
9120                           ((is_const_type ? TYPE_QUAL_CONST : 0)
9121                            | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
9122
9123   /* If we do, then we can just use its DIE, if it exists.  */
9124   if (qualified_type)
9125     {
9126       mod_type_die = lookup_type_die (qualified_type);
9127       if (mod_type_die)
9128         return mod_type_die;
9129     }
9130
9131   name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
9132
9133   /* Handle C typedef types.  */
9134   if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name))
9135     {
9136       tree dtype = TREE_TYPE (name);
9137
9138       if (qualified_type == dtype)
9139         {
9140           /* For a named type, use the typedef.  */
9141           gen_type_die (qualified_type, context_die);
9142           return lookup_type_die (qualified_type);
9143         }
9144       else if (is_const_type < TYPE_READONLY (dtype)
9145                || is_volatile_type < TYPE_VOLATILE (dtype)
9146                || (is_const_type <= TYPE_READONLY (dtype)
9147                    && is_volatile_type <= TYPE_VOLATILE (dtype)
9148                    && DECL_ORIGINAL_TYPE (name) != type))
9149         /* cv-unqualified version of named type.  Just use the unnamed
9150            type to which it refers.  */
9151         return modified_type_die (DECL_ORIGINAL_TYPE (name),
9152                                   is_const_type, is_volatile_type,
9153                                   context_die);
9154       /* Else cv-qualified version of named type; fall through.  */
9155     }
9156
9157   if (is_const_type)
9158     {
9159       mod_type_die = new_die (DW_TAG_const_type, comp_unit_die, type);
9160       sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
9161     }
9162   else if (is_volatile_type)
9163     {
9164       mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die, type);
9165       sub_die = modified_type_die (type, 0, 0, context_die);
9166     }
9167   else if (code == POINTER_TYPE)
9168     {
9169       mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die, type);
9170       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
9171                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
9172       item_type = TREE_TYPE (type);
9173     }
9174   else if (code == REFERENCE_TYPE)
9175     {
9176       mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die, type);
9177       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
9178                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
9179       item_type = TREE_TYPE (type);
9180     }
9181   else if (is_subrange_type (type))
9182     {
9183       mod_type_die = subrange_type_die (type, context_die);
9184       item_type = TREE_TYPE (type);
9185     }
9186   else if (is_base_type (type))
9187     mod_type_die = base_type_die (type);
9188   else
9189     {
9190       gen_type_die (type, context_die);
9191
9192       /* We have to get the type_main_variant here (and pass that to the
9193          `lookup_type_die' routine) because the ..._TYPE node we have
9194          might simply be a *copy* of some original type node (where the
9195          copy was created to help us keep track of typedef names) and
9196          that copy might have a different TYPE_UID from the original
9197          ..._TYPE node.  */
9198       if (TREE_CODE (type) != VECTOR_TYPE)
9199         return lookup_type_die (type_main_variant (type));
9200       else
9201         /* Vectors have the debugging information in the type,
9202            not the main variant.  */
9203         return lookup_type_die (type);
9204     }
9205
9206   /* Builtin types don't have a DECL_ORIGINAL_TYPE.  For those,
9207      don't output a DW_TAG_typedef, since there isn't one in the
9208      user's program; just attach a DW_AT_name to the type.  */
9209   if (name
9210       && (TREE_CODE (name) != TYPE_DECL
9211           || (TREE_TYPE (name) == qualified_type && DECL_NAME (name))))
9212     {
9213       if (TREE_CODE (name) == TYPE_DECL)
9214         /* Could just call add_name_and_src_coords_attributes here,
9215            but since this is a builtin type it doesn't have any
9216            useful source coordinates anyway.  */
9217         name = DECL_NAME (name);
9218       add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
9219     }
9220
9221   if (qualified_type)
9222     equate_type_number_to_die (qualified_type, mod_type_die);
9223
9224   if (item_type)
9225     /* We must do this after the equate_type_number_to_die call, in case
9226        this is a recursive type.  This ensures that the modified_type_die
9227        recursion will terminate even if the type is recursive.  Recursive
9228        types are possible in Ada.  */
9229     sub_die = modified_type_die (item_type,
9230                                  TYPE_READONLY (item_type),
9231                                  TYPE_VOLATILE (item_type),
9232                                  context_die);
9233
9234   if (sub_die != NULL)
9235     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
9236
9237   return mod_type_die;
9238 }
9239
9240 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
9241    an enumerated type.  */
9242
9243 static inline int
9244 type_is_enum (const_tree type)
9245 {
9246   return TREE_CODE (type) == ENUMERAL_TYPE;
9247 }
9248
9249 /* Return the DBX register number described by a given RTL node.  */
9250
9251 static unsigned int
9252 dbx_reg_number (const_rtx rtl)
9253 {
9254   unsigned regno = REGNO (rtl);
9255
9256   gcc_assert (regno < FIRST_PSEUDO_REGISTER);
9257
9258 #ifdef LEAF_REG_REMAP
9259   if (current_function_uses_only_leaf_regs)
9260     {
9261       int leaf_reg = LEAF_REG_REMAP (regno);
9262       if (leaf_reg != -1)
9263         regno = (unsigned) leaf_reg;
9264     }
9265 #endif
9266
9267   return DBX_REGISTER_NUMBER (regno);
9268 }
9269
9270 /* Optionally add a DW_OP_piece term to a location description expression.
9271    DW_OP_piece is only added if the location description expression already
9272    doesn't end with DW_OP_piece.  */
9273
9274 static void
9275 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
9276 {
9277   dw_loc_descr_ref loc;
9278
9279   if (*list_head != NULL)
9280     {
9281       /* Find the end of the chain.  */
9282       for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
9283         ;
9284
9285       if (loc->dw_loc_opc != DW_OP_piece)
9286         loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
9287     }
9288 }
9289
9290 /* Return a location descriptor that designates a machine register or
9291    zero if there is none.  */
9292
9293 static dw_loc_descr_ref
9294 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
9295 {
9296   rtx regs;
9297
9298   if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
9299     return 0;
9300
9301   regs = targetm.dwarf_register_span (rtl);
9302
9303   if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
9304     return multiple_reg_loc_descriptor (rtl, regs, initialized);
9305   else
9306     return one_reg_loc_descriptor (dbx_reg_number (rtl), initialized);
9307 }
9308
9309 /* Return a location descriptor that designates a machine register for
9310    a given hard register number.  */
9311
9312 static dw_loc_descr_ref
9313 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
9314 {
9315   dw_loc_descr_ref reg_loc_descr;
9316   if (regno <= 31)
9317     reg_loc_descr = new_loc_descr (DW_OP_reg0 + regno, 0, 0);
9318   else
9319     reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
9320
9321   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
9322     add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9323
9324   return reg_loc_descr;
9325 }
9326
9327 /* Given an RTL of a register, return a location descriptor that
9328    designates a value that spans more than one register.  */
9329
9330 static dw_loc_descr_ref
9331 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
9332                              enum var_init_status initialized)
9333 {
9334   int nregs, size, i;
9335   unsigned reg;
9336   dw_loc_descr_ref loc_result = NULL;
9337
9338   reg = REGNO (rtl);
9339 #ifdef LEAF_REG_REMAP
9340   if (current_function_uses_only_leaf_regs)
9341     {
9342       int leaf_reg = LEAF_REG_REMAP (reg);
9343       if (leaf_reg != -1)
9344         reg = (unsigned) leaf_reg;
9345     }
9346 #endif
9347   gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
9348   nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
9349
9350   /* Simple, contiguous registers.  */
9351   if (regs == NULL_RTX)
9352     {
9353       size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
9354
9355       loc_result = NULL;
9356       while (nregs--)
9357         {
9358           dw_loc_descr_ref t;
9359
9360           t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
9361                                       VAR_INIT_STATUS_INITIALIZED);
9362           add_loc_descr (&loc_result, t);
9363           add_loc_descr_op_piece (&loc_result, size);
9364           ++reg;
9365         }
9366       return loc_result;
9367     }
9368
9369   /* Now onto stupid register sets in non contiguous locations.  */
9370
9371   gcc_assert (GET_CODE (regs) == PARALLEL);
9372
9373   size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
9374   loc_result = NULL;
9375
9376   for (i = 0; i < XVECLEN (regs, 0); ++i)
9377     {
9378       dw_loc_descr_ref t;
9379
9380       t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
9381                                   VAR_INIT_STATUS_INITIALIZED);
9382       add_loc_descr (&loc_result, t);
9383       size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
9384       add_loc_descr_op_piece (&loc_result, size);
9385     }
9386
9387   if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
9388     add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9389   return loc_result;
9390 }
9391
9392 #endif /* DWARF2_DEBUGGING_INFO */
9393
9394 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
9395
9396 /* Return a location descriptor that designates a constant.  */
9397
9398 static dw_loc_descr_ref
9399 int_loc_descriptor (HOST_WIDE_INT i)
9400 {
9401   enum dwarf_location_atom op;
9402
9403   /* Pick the smallest representation of a constant, rather than just
9404      defaulting to the LEB encoding.  */
9405   if (i >= 0)
9406     {
9407       if (i <= 31)
9408         op = DW_OP_lit0 + i;
9409       else if (i <= 0xff)
9410         op = DW_OP_const1u;
9411       else if (i <= 0xffff)
9412         op = DW_OP_const2u;
9413       else if (HOST_BITS_PER_WIDE_INT == 32
9414                || i <= 0xffffffff)
9415         op = DW_OP_const4u;
9416       else
9417         op = DW_OP_constu;
9418     }
9419   else
9420     {
9421       if (i >= -0x80)
9422         op = DW_OP_const1s;
9423       else if (i >= -0x8000)
9424         op = DW_OP_const2s;
9425       else if (HOST_BITS_PER_WIDE_INT == 32
9426                || i >= -0x80000000)
9427         op = DW_OP_const4s;
9428       else
9429         op = DW_OP_consts;
9430     }
9431
9432   return new_loc_descr (op, i, 0);
9433 }
9434 #endif
9435
9436 #ifdef DWARF2_DEBUGGING_INFO
9437
9438 /* Return a location descriptor that designates a base+offset location.  */
9439
9440 static dw_loc_descr_ref
9441 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
9442                  enum var_init_status initialized)
9443 {
9444   unsigned int regno;
9445   dw_loc_descr_ref result;
9446   dw_fde_ref fde = current_fde ();
9447
9448   /* We only use "frame base" when we're sure we're talking about the
9449      post-prologue local stack frame.  We do this by *not* running
9450      register elimination until this point, and recognizing the special
9451      argument pointer and soft frame pointer rtx's.  */
9452   if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
9453     {
9454       rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
9455
9456       if (elim != reg)
9457         {
9458           if (GET_CODE (elim) == PLUS)
9459             {
9460               offset += INTVAL (XEXP (elim, 1));
9461               elim = XEXP (elim, 0);
9462             }
9463           gcc_assert ((SUPPORTS_STACK_ALIGNMENT
9464                        && (elim == hard_frame_pointer_rtx
9465                            || elim == stack_pointer_rtx))
9466                       || elim == (frame_pointer_needed
9467                                   ? hard_frame_pointer_rtx
9468                                   : stack_pointer_rtx));
9469
9470           /* If drap register is used to align stack, use frame
9471              pointer + offset to access stack variables.  If stack
9472              is aligned without drap, use stack pointer + offset to
9473              access stack variables.  */
9474           if (crtl->stack_realign_tried
9475               && cfa.reg == HARD_FRAME_POINTER_REGNUM
9476               && reg == frame_pointer_rtx)
9477             {
9478               int base_reg
9479                 = DWARF_FRAME_REGNUM (cfa.indirect
9480                                       ? HARD_FRAME_POINTER_REGNUM
9481                                       : STACK_POINTER_REGNUM);
9482               if (base_reg <= 31)
9483                 return new_loc_descr (DW_OP_breg0 + base_reg, offset, 0);
9484               else
9485                 return new_loc_descr (DW_OP_bregx, base_reg, offset);
9486             }
9487
9488           offset += frame_pointer_fb_offset;
9489           return new_loc_descr (DW_OP_fbreg, offset, 0);
9490         }
9491     }
9492   else if (fde
9493            && fde->drap_reg != INVALID_REGNUM
9494            && (fde->drap_reg == REGNO (reg)
9495                || fde->vdrap_reg == REGNO (reg)))
9496     {
9497       /* Use cfa+offset to represent the location of arguments passed
9498          on stack when drap is used to align stack.  */
9499       return new_loc_descr (DW_OP_fbreg, offset, 0);
9500     }
9501
9502   regno = dbx_reg_number (reg);
9503   if (regno <= 31)
9504     result = new_loc_descr (DW_OP_breg0 + regno, offset, 0);
9505   else
9506     result = new_loc_descr (DW_OP_bregx, regno, offset);
9507
9508   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
9509     add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9510
9511   return result;
9512 }
9513
9514 /* Return true if this RTL expression describes a base+offset calculation.  */
9515
9516 static inline int
9517 is_based_loc (const_rtx rtl)
9518 {
9519   return (GET_CODE (rtl) == PLUS
9520           && ((REG_P (XEXP (rtl, 0))
9521                && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
9522                && GET_CODE (XEXP (rtl, 1)) == CONST_INT)));
9523 }
9524
9525 /* Return a descriptor that describes the concatenation of N locations
9526    used to form the address of a memory location.  */
9527
9528 static dw_loc_descr_ref
9529 concatn_mem_loc_descriptor (rtx concatn, enum machine_mode mode,
9530                             enum var_init_status initialized)
9531 {
9532   unsigned int i;
9533   dw_loc_descr_ref cc_loc_result = NULL;
9534   unsigned int n = XVECLEN (concatn, 0);
9535
9536   for (i = 0; i < n; ++i)
9537     {
9538       dw_loc_descr_ref ref;
9539       rtx x = XVECEXP (concatn, 0, i);
9540
9541       ref = mem_loc_descriptor (x, mode, VAR_INIT_STATUS_INITIALIZED);
9542       if (ref == NULL)
9543         return NULL;
9544
9545       add_loc_descr (&cc_loc_result, ref);
9546       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
9547     }
9548
9549   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
9550     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9551
9552   return cc_loc_result;
9553 }
9554
9555 /* The following routine converts the RTL for a variable or parameter
9556    (resident in memory) into an equivalent Dwarf representation of a
9557    mechanism for getting the address of that same variable onto the top of a
9558    hypothetical "address evaluation" stack.
9559
9560    When creating memory location descriptors, we are effectively transforming
9561    the RTL for a memory-resident object into its Dwarf postfix expression
9562    equivalent.  This routine recursively descends an RTL tree, turning
9563    it into Dwarf postfix code as it goes.
9564
9565    MODE is the mode of the memory reference, needed to handle some
9566    autoincrement addressing modes.
9567
9568    CAN_USE_FBREG is a flag whether we can use DW_AT_frame_base in the
9569    location list for RTL.
9570
9571    Return 0 if we can't represent the location.  */
9572
9573 static dw_loc_descr_ref
9574 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
9575                     enum var_init_status initialized)
9576 {
9577   dw_loc_descr_ref mem_loc_result = NULL;
9578   enum dwarf_location_atom op;
9579
9580   /* Note that for a dynamically sized array, the location we will generate a
9581      description of here will be the lowest numbered location which is
9582      actually within the array.  That's *not* necessarily the same as the
9583      zeroth element of the array.  */
9584
9585   rtl = targetm.delegitimize_address (rtl);
9586
9587   switch (GET_CODE (rtl))
9588     {
9589     case POST_INC:
9590     case POST_DEC:
9591     case POST_MODIFY:
9592       /* POST_INC and POST_DEC can be handled just like a SUBREG.  So we
9593          just fall into the SUBREG code.  */
9594
9595       /* ... fall through ...  */
9596
9597     case SUBREG:
9598       /* The case of a subreg may arise when we have a local (register)
9599          variable or a formal (register) parameter which doesn't quite fill
9600          up an entire register.  For now, just assume that it is
9601          legitimate to make the Dwarf info refer to the whole register which
9602          contains the given subreg.  */
9603       rtl = XEXP (rtl, 0);
9604
9605       /* ... fall through ...  */
9606
9607     case REG:
9608       /* Whenever a register number forms a part of the description of the
9609          method for calculating the (dynamic) address of a memory resident
9610          object, DWARF rules require the register number be referred to as
9611          a "base register".  This distinction is not based in any way upon
9612          what category of register the hardware believes the given register
9613          belongs to.  This is strictly DWARF terminology we're dealing with
9614          here. Note that in cases where the location of a memory-resident
9615          data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
9616          OP_CONST (0)) the actual DWARF location descriptor that we generate
9617          may just be OP_BASEREG (basereg).  This may look deceptively like
9618          the object in question was allocated to a register (rather than in
9619          memory) so DWARF consumers need to be aware of the subtle
9620          distinction between OP_REG and OP_BASEREG.  */
9621       if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
9622         mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
9623       break;
9624
9625     case MEM:
9626       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
9627                                            VAR_INIT_STATUS_INITIALIZED);
9628       if (mem_loc_result != 0)
9629         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
9630       break;
9631
9632     case LO_SUM:
9633          rtl = XEXP (rtl, 1);
9634
9635       /* ... fall through ...  */
9636
9637     case LABEL_REF:
9638       /* Some ports can transform a symbol ref into a label ref, because
9639          the symbol ref is too far away and has to be dumped into a constant
9640          pool.  */
9641     case CONST:
9642     case SYMBOL_REF:
9643       /* Alternatively, the symbol in the constant pool might be referenced
9644          by a different symbol.  */
9645       if (GET_CODE (rtl) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (rtl))
9646         {
9647           bool marked;
9648           rtx tmp = get_pool_constant_mark (rtl, &marked);
9649
9650           if (GET_CODE (tmp) == SYMBOL_REF)
9651             {
9652               rtl = tmp;
9653               if (CONSTANT_POOL_ADDRESS_P (tmp))
9654                 get_pool_constant_mark (tmp, &marked);
9655               else
9656                 marked = true;
9657             }
9658
9659           /* If all references to this pool constant were optimized away,
9660              it was not output and thus we can't represent it.
9661              FIXME: might try to use DW_OP_const_value here, though
9662              DW_OP_piece complicates it.  */
9663           if (!marked)
9664             return 0;
9665         }
9666
9667       mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
9668       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
9669       mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
9670       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
9671       break;
9672
9673     case PRE_MODIFY:
9674       /* Extract the PLUS expression nested inside and fall into
9675          PLUS code below.  */
9676       rtl = XEXP (rtl, 1);
9677       goto plus;
9678
9679     case PRE_INC:
9680     case PRE_DEC:
9681       /* Turn these into a PLUS expression and fall into the PLUS code
9682          below.  */
9683       rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
9684                           GEN_INT (GET_CODE (rtl) == PRE_INC
9685                                    ? GET_MODE_UNIT_SIZE (mode)
9686                                    : -GET_MODE_UNIT_SIZE (mode)));
9687
9688       /* ... fall through ...  */
9689
9690     case PLUS:
9691     plus:
9692       if (is_based_loc (rtl))
9693         mem_loc_result = based_loc_descr (XEXP (rtl, 0),
9694                                           INTVAL (XEXP (rtl, 1)),
9695                                           VAR_INIT_STATUS_INITIALIZED);
9696       else
9697         {
9698           mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode,
9699                                                VAR_INIT_STATUS_INITIALIZED);
9700           if (mem_loc_result == 0)
9701             break;
9702
9703           if (GET_CODE (XEXP (rtl, 1)) == CONST_INT
9704               && INTVAL (XEXP (rtl, 1)) >= 0)
9705             add_loc_descr (&mem_loc_result,
9706                            new_loc_descr (DW_OP_plus_uconst,
9707                                           INTVAL (XEXP (rtl, 1)), 0));
9708           else
9709             {
9710               add_loc_descr (&mem_loc_result,
9711                              mem_loc_descriptor (XEXP (rtl, 1), mode,
9712                                                  VAR_INIT_STATUS_INITIALIZED));
9713               add_loc_descr (&mem_loc_result,
9714                              new_loc_descr (DW_OP_plus, 0, 0));
9715             }
9716         }
9717       break;
9718
9719     /* If a pseudo-reg is optimized away, it is possible for it to
9720        be replaced with a MEM containing a multiply or shift.  */
9721     case MULT:
9722       op = DW_OP_mul;
9723       goto do_binop;
9724
9725     case ASHIFT:
9726       op = DW_OP_shl;
9727       goto do_binop;
9728
9729     case ASHIFTRT:
9730       op = DW_OP_shra;
9731       goto do_binop;
9732
9733     case LSHIFTRT:
9734       op = DW_OP_shr;
9735       goto do_binop;
9736
9737     do_binop:
9738       {
9739         dw_loc_descr_ref op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
9740                                                    VAR_INIT_STATUS_INITIALIZED);
9741         dw_loc_descr_ref op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
9742                                                    VAR_INIT_STATUS_INITIALIZED);
9743
9744         if (op0 == 0 || op1 == 0)
9745           break;
9746
9747         mem_loc_result = op0;
9748         add_loc_descr (&mem_loc_result, op1);
9749         add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
9750         break;
9751       }
9752
9753     case CONST_INT:
9754       mem_loc_result = int_loc_descriptor (INTVAL (rtl));
9755       break;
9756
9757     case CONCATN:
9758       mem_loc_result = concatn_mem_loc_descriptor (rtl, mode,
9759                                                    VAR_INIT_STATUS_INITIALIZED);
9760       break;
9761
9762     default:
9763       gcc_unreachable ();
9764     }
9765
9766   if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
9767     add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9768
9769   return mem_loc_result;
9770 }
9771
9772 /* Return a descriptor that describes the concatenation of two locations.
9773    This is typically a complex variable.  */
9774
9775 static dw_loc_descr_ref
9776 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
9777 {
9778   dw_loc_descr_ref cc_loc_result = NULL;
9779   dw_loc_descr_ref x0_ref = loc_descriptor (x0, VAR_INIT_STATUS_INITIALIZED);
9780   dw_loc_descr_ref x1_ref = loc_descriptor (x1, VAR_INIT_STATUS_INITIALIZED);
9781
9782   if (x0_ref == 0 || x1_ref == 0)
9783     return 0;
9784
9785   cc_loc_result = x0_ref;
9786   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
9787
9788   add_loc_descr (&cc_loc_result, x1_ref);
9789   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
9790
9791   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
9792     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9793
9794   return cc_loc_result;
9795 }
9796
9797 /* Return a descriptor that describes the concatenation of N
9798    locations.  */
9799
9800 static dw_loc_descr_ref
9801 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
9802 {
9803   unsigned int i;
9804   dw_loc_descr_ref cc_loc_result = NULL;
9805   unsigned int n = XVECLEN (concatn, 0);
9806
9807   for (i = 0; i < n; ++i)
9808     {
9809       dw_loc_descr_ref ref;
9810       rtx x = XVECEXP (concatn, 0, i);
9811
9812       ref = loc_descriptor (x, VAR_INIT_STATUS_INITIALIZED);
9813       if (ref == NULL)
9814         return NULL;
9815
9816       add_loc_descr (&cc_loc_result, ref);
9817       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
9818     }
9819
9820   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
9821     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9822
9823   return cc_loc_result;
9824 }
9825
9826 /* Output a proper Dwarf location descriptor for a variable or parameter
9827    which is either allocated in a register or in a memory location.  For a
9828    register, we just generate an OP_REG and the register number.  For a
9829    memory location we provide a Dwarf postfix expression describing how to
9830    generate the (dynamic) address of the object onto the address stack.
9831
9832    If we don't know how to describe it, return 0.  */
9833
9834 static dw_loc_descr_ref
9835 loc_descriptor (rtx rtl, enum var_init_status initialized)
9836 {
9837   dw_loc_descr_ref loc_result = NULL;
9838
9839   switch (GET_CODE (rtl))
9840     {
9841     case SUBREG:
9842       /* The case of a subreg may arise when we have a local (register)
9843          variable or a formal (register) parameter which doesn't quite fill
9844          up an entire register.  For now, just assume that it is
9845          legitimate to make the Dwarf info refer to the whole register which
9846          contains the given subreg.  */
9847       rtl = SUBREG_REG (rtl);
9848
9849       /* ... fall through ...  */
9850
9851     case REG:
9852       loc_result = reg_loc_descriptor (rtl, initialized);
9853       break;
9854
9855     case MEM:
9856       loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
9857                                        initialized);
9858       break;
9859
9860     case CONCAT:
9861       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
9862                                           initialized);
9863       break;
9864
9865     case CONCATN:
9866       loc_result = concatn_loc_descriptor (rtl, initialized);
9867       break;
9868
9869     case VAR_LOCATION:
9870       /* Single part.  */
9871       if (GET_CODE (XEXP (rtl, 1)) != PARALLEL)
9872         {
9873           loc_result = loc_descriptor (XEXP (XEXP (rtl, 1), 0), initialized);
9874           break;
9875         }
9876
9877       rtl = XEXP (rtl, 1);
9878       /* FALLTHRU */
9879
9880     case PARALLEL:
9881       {
9882         rtvec par_elems = XVEC (rtl, 0);
9883         int num_elem = GET_NUM_ELEM (par_elems);
9884         enum machine_mode mode;
9885         int i;
9886
9887         /* Create the first one, so we have something to add to.  */
9888         loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
9889                                      initialized);
9890         mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
9891         add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
9892         for (i = 1; i < num_elem; i++)
9893           {
9894             dw_loc_descr_ref temp;
9895
9896             temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
9897                                    initialized);
9898             add_loc_descr (&loc_result, temp);
9899             mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
9900             add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
9901           }
9902       }
9903       break;
9904
9905     default:
9906       gcc_unreachable ();
9907     }
9908
9909   return loc_result;
9910 }
9911
9912 /* Similar, but generate the descriptor from trees instead of rtl.  This comes
9913    up particularly with variable length arrays.  WANT_ADDRESS is 2 if this is
9914    a top-level invocation of loc_descriptor_from_tree; is 1 if this is not a
9915    top-level invocation, and we require the address of LOC; is 0 if we require
9916    the value of LOC.  */
9917
9918 static dw_loc_descr_ref
9919 loc_descriptor_from_tree_1 (tree loc, int want_address)
9920 {
9921   dw_loc_descr_ref ret, ret1;
9922   int have_address = 0;
9923   enum dwarf_location_atom op;
9924
9925   /* ??? Most of the time we do not take proper care for sign/zero
9926      extending the values properly.  Hopefully this won't be a real
9927      problem...  */
9928
9929   switch (TREE_CODE (loc))
9930     {
9931     case ERROR_MARK:
9932       return 0;
9933
9934     case PLACEHOLDER_EXPR:
9935       /* This case involves extracting fields from an object to determine the
9936          position of other fields.  We don't try to encode this here.  The
9937          only user of this is Ada, which encodes the needed information using
9938          the names of types.  */
9939       return 0;
9940
9941     case CALL_EXPR:
9942       return 0;
9943
9944     case PREINCREMENT_EXPR:
9945     case PREDECREMENT_EXPR:
9946     case POSTINCREMENT_EXPR:
9947     case POSTDECREMENT_EXPR:
9948       /* There are no opcodes for these operations.  */
9949       return 0;
9950
9951     case ADDR_EXPR:
9952       /* If we already want an address, there's nothing we can do.  */
9953       if (want_address)
9954         return 0;
9955
9956       /* Otherwise, process the argument and look for the address.  */
9957       return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 1);
9958
9959     case VAR_DECL:
9960       if (DECL_THREAD_LOCAL_P (loc))
9961         {
9962           rtx rtl;
9963           unsigned first_op;
9964           unsigned second_op;
9965
9966           if (targetm.have_tls)
9967             {
9968               /* If this is not defined, we have no way to emit the
9969                  data.  */
9970               if (!targetm.asm_out.output_dwarf_dtprel)
9971                 return 0;
9972
9973                /* The way DW_OP_GNU_push_tls_address is specified, we
9974                   can only look up addresses of objects in the current
9975                   module.  */
9976               if (DECL_EXTERNAL (loc))
9977                 return 0;
9978               first_op = INTERNAL_DW_OP_tls_addr;
9979               second_op = DW_OP_GNU_push_tls_address;
9980             }
9981           else
9982             {
9983               if (!targetm.emutls.debug_form_tls_address)
9984                 return 0;
9985               loc = emutls_decl (loc);
9986               first_op = DW_OP_addr;
9987               second_op = DW_OP_form_tls_address;
9988             }
9989
9990           rtl = rtl_for_decl_location (loc);
9991           if (rtl == NULL_RTX)
9992             return 0;
9993
9994           if (!MEM_P (rtl))
9995             return 0;
9996           rtl = XEXP (rtl, 0);
9997           if (! CONSTANT_P (rtl))
9998             return 0;
9999
10000           ret = new_loc_descr (first_op, 0, 0);
10001           ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
10002           ret->dw_loc_oprnd1.v.val_addr = rtl;
10003
10004           ret1 = new_loc_descr (second_op, 0, 0);
10005           add_loc_descr (&ret, ret1);
10006
10007           have_address = 1;
10008           break;
10009         }
10010       /* FALLTHRU */
10011
10012     case PARM_DECL:
10013       if (DECL_HAS_VALUE_EXPR_P (loc))
10014         return loc_descriptor_from_tree_1 (DECL_VALUE_EXPR (loc),
10015                                            want_address);
10016       /* FALLTHRU */
10017
10018     case RESULT_DECL:
10019     case FUNCTION_DECL:
10020       {
10021         rtx rtl = rtl_for_decl_location (loc);
10022
10023         if (rtl == NULL_RTX)
10024           return 0;
10025         else if (GET_CODE (rtl) == CONST_INT)
10026           {
10027             HOST_WIDE_INT val = INTVAL (rtl);
10028             if (TYPE_UNSIGNED (TREE_TYPE (loc)))
10029               val &= GET_MODE_MASK (DECL_MODE (loc));
10030             ret = int_loc_descriptor (val);
10031           }
10032         else if (GET_CODE (rtl) == CONST_STRING)
10033           return 0;
10034         else if (CONSTANT_P (rtl))
10035           {
10036             ret = new_loc_descr (DW_OP_addr, 0, 0);
10037             ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
10038             ret->dw_loc_oprnd1.v.val_addr = rtl;
10039           }
10040         else
10041           {
10042             enum machine_mode mode;
10043
10044             /* Certain constructs can only be represented at top-level.  */
10045             if (want_address == 2)
10046               return loc_descriptor (rtl, VAR_INIT_STATUS_INITIALIZED);
10047
10048             mode = GET_MODE (rtl);
10049             if (MEM_P (rtl))
10050               {
10051                 rtl = XEXP (rtl, 0);
10052                 have_address = 1;
10053               }
10054             ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
10055           }
10056       }
10057       break;
10058
10059     case INDIRECT_REF:
10060       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
10061       have_address = 1;
10062       break;
10063
10064     case COMPOUND_EXPR:
10065       return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), want_address);
10066
10067     CASE_CONVERT:
10068     case VIEW_CONVERT_EXPR:
10069     case SAVE_EXPR:
10070     case MODIFY_EXPR:
10071       return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), want_address);
10072
10073     case COMPONENT_REF:
10074     case BIT_FIELD_REF:
10075     case ARRAY_REF:
10076     case ARRAY_RANGE_REF:
10077       {
10078         tree obj, offset;
10079         HOST_WIDE_INT bitsize, bitpos, bytepos;
10080         enum machine_mode mode;
10081         int volatilep;
10082         int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
10083
10084         obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
10085                                    &unsignedp, &volatilep, false);
10086
10087         if (obj == loc)
10088           return 0;
10089
10090         ret = loc_descriptor_from_tree_1 (obj, 1);
10091         if (ret == 0
10092             || bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
10093           return 0;
10094
10095         if (offset != NULL_TREE)
10096           {
10097             /* Variable offset.  */
10098             add_loc_descr (&ret, loc_descriptor_from_tree_1 (offset, 0));
10099             add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
10100           }
10101
10102         bytepos = bitpos / BITS_PER_UNIT;
10103         if (bytepos > 0)
10104           add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
10105         else if (bytepos < 0)
10106           {
10107             add_loc_descr (&ret, int_loc_descriptor (bytepos));
10108             add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
10109           }
10110
10111         have_address = 1;
10112         break;
10113       }
10114
10115     case INTEGER_CST:
10116       if (host_integerp (loc, 0))
10117         ret = int_loc_descriptor (tree_low_cst (loc, 0));
10118       else
10119         return 0;
10120       break;
10121
10122     case CONSTRUCTOR:
10123       {
10124         /* Get an RTL for this, if something has been emitted.  */
10125         rtx rtl = lookup_constant_def (loc);
10126         enum machine_mode mode;
10127
10128         if (!rtl || !MEM_P (rtl))
10129           return 0;
10130         mode = GET_MODE (rtl);
10131         rtl = XEXP (rtl, 0);
10132         ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
10133         have_address = 1;
10134         break;
10135       }
10136
10137     case TRUTH_AND_EXPR:
10138     case TRUTH_ANDIF_EXPR:
10139     case BIT_AND_EXPR:
10140       op = DW_OP_and;
10141       goto do_binop;
10142
10143     case TRUTH_XOR_EXPR:
10144     case BIT_XOR_EXPR:
10145       op = DW_OP_xor;
10146       goto do_binop;
10147
10148     case TRUTH_OR_EXPR:
10149     case TRUTH_ORIF_EXPR:
10150     case BIT_IOR_EXPR:
10151       op = DW_OP_or;
10152       goto do_binop;
10153
10154     case FLOOR_DIV_EXPR:
10155     case CEIL_DIV_EXPR:
10156     case ROUND_DIV_EXPR:
10157     case TRUNC_DIV_EXPR:
10158       op = DW_OP_div;
10159       goto do_binop;
10160
10161     case MINUS_EXPR:
10162       op = DW_OP_minus;
10163       goto do_binop;
10164
10165     case FLOOR_MOD_EXPR:
10166     case CEIL_MOD_EXPR:
10167     case ROUND_MOD_EXPR:
10168     case TRUNC_MOD_EXPR:
10169       op = DW_OP_mod;
10170       goto do_binop;
10171
10172     case MULT_EXPR:
10173       op = DW_OP_mul;
10174       goto do_binop;
10175
10176     case LSHIFT_EXPR:
10177       op = DW_OP_shl;
10178       goto do_binop;
10179
10180     case RSHIFT_EXPR:
10181       op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
10182       goto do_binop;
10183
10184     case POINTER_PLUS_EXPR:
10185     case PLUS_EXPR:
10186       if (TREE_CODE (TREE_OPERAND (loc, 1)) == INTEGER_CST
10187           && host_integerp (TREE_OPERAND (loc, 1), 0))
10188         {
10189           ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
10190           if (ret == 0)
10191             return 0;
10192
10193           add_loc_descr (&ret,
10194                          new_loc_descr (DW_OP_plus_uconst,
10195                                         tree_low_cst (TREE_OPERAND (loc, 1),
10196                                                       0),
10197                                         0));
10198           break;
10199         }
10200
10201       op = DW_OP_plus;
10202       goto do_binop;
10203
10204     case LE_EXPR:
10205       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
10206         return 0;
10207
10208       op = DW_OP_le;
10209       goto do_binop;
10210
10211     case GE_EXPR:
10212       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
10213         return 0;
10214
10215       op = DW_OP_ge;
10216       goto do_binop;
10217
10218     case LT_EXPR:
10219       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
10220         return 0;
10221
10222       op = DW_OP_lt;
10223       goto do_binop;
10224
10225     case GT_EXPR:
10226       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
10227         return 0;
10228
10229       op = DW_OP_gt;
10230       goto do_binop;
10231
10232     case EQ_EXPR:
10233       op = DW_OP_eq;
10234       goto do_binop;
10235
10236     case NE_EXPR:
10237       op = DW_OP_ne;
10238       goto do_binop;
10239
10240     do_binop:
10241       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
10242       ret1 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
10243       if (ret == 0 || ret1 == 0)
10244         return 0;
10245
10246       add_loc_descr (&ret, ret1);
10247       add_loc_descr (&ret, new_loc_descr (op, 0, 0));
10248       break;
10249
10250     case TRUTH_NOT_EXPR:
10251     case BIT_NOT_EXPR:
10252       op = DW_OP_not;
10253       goto do_unop;
10254
10255     case ABS_EXPR:
10256       op = DW_OP_abs;
10257       goto do_unop;
10258
10259     case NEGATE_EXPR:
10260       op = DW_OP_neg;
10261       goto do_unop;
10262
10263     do_unop:
10264       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
10265       if (ret == 0)
10266         return 0;
10267
10268       add_loc_descr (&ret, new_loc_descr (op, 0, 0));
10269       break;
10270
10271     case MIN_EXPR:
10272     case MAX_EXPR:
10273       {
10274         const enum tree_code code =
10275           TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
10276
10277         loc = build3 (COND_EXPR, TREE_TYPE (loc),
10278                       build2 (code, integer_type_node,
10279                               TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
10280                       TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
10281       }
10282
10283       /* ... fall through ...  */
10284
10285     case COND_EXPR:
10286       {
10287         dw_loc_descr_ref lhs
10288           = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
10289         dw_loc_descr_ref rhs
10290           = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 2), 0);
10291         dw_loc_descr_ref bra_node, jump_node, tmp;
10292
10293         ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
10294         if (ret == 0 || lhs == 0 || rhs == 0)
10295           return 0;
10296
10297         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
10298         add_loc_descr (&ret, bra_node);
10299
10300         add_loc_descr (&ret, rhs);
10301         jump_node = new_loc_descr (DW_OP_skip, 0, 0);
10302         add_loc_descr (&ret, jump_node);
10303
10304         add_loc_descr (&ret, lhs);
10305         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
10306         bra_node->dw_loc_oprnd1.v.val_loc = lhs;
10307
10308         /* ??? Need a node to point the skip at.  Use a nop.  */
10309         tmp = new_loc_descr (DW_OP_nop, 0, 0);
10310         add_loc_descr (&ret, tmp);
10311         jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
10312         jump_node->dw_loc_oprnd1.v.val_loc = tmp;
10313       }
10314       break;
10315
10316     case FIX_TRUNC_EXPR:
10317       return 0;
10318
10319     default:
10320       /* Leave front-end specific codes as simply unknown.  This comes
10321          up, for instance, with the C STMT_EXPR.  */
10322       if ((unsigned int) TREE_CODE (loc)
10323           >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
10324         return 0;
10325
10326 #ifdef ENABLE_CHECKING
10327       /* Otherwise this is a generic code; we should just lists all of
10328          these explicitly.  We forgot one.  */
10329       gcc_unreachable ();
10330 #else
10331       /* In a release build, we want to degrade gracefully: better to
10332          generate incomplete debugging information than to crash.  */
10333       return NULL;
10334 #endif
10335     }
10336
10337   /* Show if we can't fill the request for an address.  */
10338   if (want_address && !have_address)
10339     return 0;
10340
10341   /* If we've got an address and don't want one, dereference.  */
10342   if (!want_address && have_address && ret)
10343     {
10344       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
10345
10346       if (size > DWARF2_ADDR_SIZE || size == -1)
10347         return 0;
10348       else if (size == DWARF2_ADDR_SIZE)
10349         op = DW_OP_deref;
10350       else
10351         op = DW_OP_deref_size;
10352
10353       add_loc_descr (&ret, new_loc_descr (op, size, 0));
10354     }
10355
10356   return ret;
10357 }
10358
10359 static inline dw_loc_descr_ref
10360 loc_descriptor_from_tree (tree loc)
10361 {
10362   return loc_descriptor_from_tree_1 (loc, 2);
10363 }
10364
10365 /* Given a value, round it up to the lowest multiple of `boundary'
10366    which is not less than the value itself.  */
10367
10368 static inline HOST_WIDE_INT
10369 ceiling (HOST_WIDE_INT value, unsigned int boundary)
10370 {
10371   return (((value + boundary - 1) / boundary) * boundary);
10372 }
10373
10374 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
10375    pointer to the declared type for the relevant field variable, or return
10376    `integer_type_node' if the given node turns out to be an
10377    ERROR_MARK node.  */
10378
10379 static inline tree
10380 field_type (const_tree decl)
10381 {
10382   tree type;
10383
10384   if (TREE_CODE (decl) == ERROR_MARK)
10385     return integer_type_node;
10386
10387   type = DECL_BIT_FIELD_TYPE (decl);
10388   if (type == NULL_TREE)
10389     type = TREE_TYPE (decl);
10390
10391   return type;
10392 }
10393
10394 /* Given a pointer to a tree node, return the alignment in bits for
10395    it, or else return BITS_PER_WORD if the node actually turns out to
10396    be an ERROR_MARK node.  */
10397
10398 static inline unsigned
10399 simple_type_align_in_bits (const_tree type)
10400 {
10401   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
10402 }
10403
10404 static inline unsigned
10405 simple_decl_align_in_bits (const_tree decl)
10406 {
10407   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
10408 }
10409
10410 /* Return the result of rounding T up to ALIGN.  */
10411
10412 static inline HOST_WIDE_INT
10413 round_up_to_align (HOST_WIDE_INT t, unsigned int align)
10414 {
10415   /* We must be careful if T is negative because HOST_WIDE_INT can be
10416      either "above" or "below" unsigned int as per the C promotion
10417      rules, depending on the host, thus making the signedness of the
10418      direct multiplication and division unpredictable.  */
10419   unsigned HOST_WIDE_INT u = (unsigned HOST_WIDE_INT) t;
10420
10421   u += align - 1;
10422   u /= align;
10423   u *= align;
10424
10425   return (HOST_WIDE_INT) u;
10426 }
10427
10428 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
10429    lowest addressed byte of the "containing object" for the given FIELD_DECL,
10430    or return 0 if we are unable to determine what that offset is, either
10431    because the argument turns out to be a pointer to an ERROR_MARK node, or
10432    because the offset is actually variable.  (We can't handle the latter case
10433    just yet).  */
10434
10435 static HOST_WIDE_INT
10436 field_byte_offset (const_tree decl)
10437 {
10438   HOST_WIDE_INT object_offset_in_bits;
10439   HOST_WIDE_INT bitpos_int;
10440
10441   if (TREE_CODE (decl) == ERROR_MARK)
10442     return 0;
10443
10444   gcc_assert (TREE_CODE (decl) == FIELD_DECL);
10445
10446   /* We cannot yet cope with fields whose positions are variable, so
10447      for now, when we see such things, we simply return 0.  Someday, we may
10448      be able to handle such cases, but it will be damn difficult.  */
10449   if (! host_integerp (bit_position (decl), 0))
10450     return 0;
10451
10452   bitpos_int = int_bit_position (decl);
10453
10454 #ifdef PCC_BITFIELD_TYPE_MATTERS
10455   if (PCC_BITFIELD_TYPE_MATTERS)
10456     {
10457       tree type;
10458       tree field_size_tree;
10459       HOST_WIDE_INT deepest_bitpos;
10460       unsigned HOST_WIDE_INT field_size_in_bits;
10461       unsigned int type_align_in_bits;
10462       unsigned int decl_align_in_bits;
10463       unsigned HOST_WIDE_INT type_size_in_bits;
10464
10465       type = field_type (decl);
10466       field_size_tree = DECL_SIZE (decl);
10467
10468       /* The size could be unspecified if there was an error, or for
10469          a flexible array member.  */
10470       if (! field_size_tree)
10471         field_size_tree = bitsize_zero_node;
10472
10473       /* If we don't know the size of the field, pretend it's a full word.  */
10474       if (host_integerp (field_size_tree, 1))
10475         field_size_in_bits = tree_low_cst (field_size_tree, 1);
10476       else
10477         field_size_in_bits = BITS_PER_WORD;
10478
10479       type_size_in_bits = simple_type_size_in_bits (type);
10480       type_align_in_bits = simple_type_align_in_bits (type);
10481       decl_align_in_bits = simple_decl_align_in_bits (decl);
10482
10483       /* The GCC front-end doesn't make any attempt to keep track of the
10484          starting bit offset (relative to the start of the containing
10485          structure type) of the hypothetical "containing object" for a
10486          bit-field.  Thus, when computing the byte offset value for the
10487          start of the "containing object" of a bit-field, we must deduce
10488          this information on our own. This can be rather tricky to do in
10489          some cases.  For example, handling the following structure type
10490          definition when compiling for an i386/i486 target (which only
10491          aligns long long's to 32-bit boundaries) can be very tricky:
10492
10493          struct S { int field1; long long field2:31; };
10494
10495          Fortunately, there is a simple rule-of-thumb which can be used
10496          in such cases.  When compiling for an i386/i486, GCC will
10497          allocate 8 bytes for the structure shown above.  It decides to
10498          do this based upon one simple rule for bit-field allocation.
10499          GCC allocates each "containing object" for each bit-field at
10500          the first (i.e. lowest addressed) legitimate alignment boundary
10501          (based upon the required minimum alignment for the declared
10502          type of the field) which it can possibly use, subject to the
10503          condition that there is still enough available space remaining
10504          in the containing object (when allocated at the selected point)
10505          to fully accommodate all of the bits of the bit-field itself.
10506
10507          This simple rule makes it obvious why GCC allocates 8 bytes for
10508          each object of the structure type shown above.  When looking
10509          for a place to allocate the "containing object" for `field2',
10510          the compiler simply tries to allocate a 64-bit "containing
10511          object" at each successive 32-bit boundary (starting at zero)
10512          until it finds a place to allocate that 64- bit field such that
10513          at least 31 contiguous (and previously unallocated) bits remain
10514          within that selected 64 bit field.  (As it turns out, for the
10515          example above, the compiler finds it is OK to allocate the
10516          "containing object" 64-bit field at bit-offset zero within the
10517          structure type.)
10518
10519          Here we attempt to work backwards from the limited set of facts
10520          we're given, and we try to deduce from those facts, where GCC
10521          must have believed that the containing object started (within
10522          the structure type). The value we deduce is then used (by the
10523          callers of this routine) to generate DW_AT_location and
10524          DW_AT_bit_offset attributes for fields (both bit-fields and, in
10525          the case of DW_AT_location, regular fields as well).  */
10526
10527       /* Figure out the bit-distance from the start of the structure to
10528          the "deepest" bit of the bit-field.  */
10529       deepest_bitpos = bitpos_int + field_size_in_bits;
10530
10531       /* This is the tricky part.  Use some fancy footwork to deduce
10532          where the lowest addressed bit of the containing object must
10533          be.  */
10534       object_offset_in_bits = deepest_bitpos - type_size_in_bits;
10535
10536       /* Round up to type_align by default.  This works best for
10537          bitfields.  */
10538       object_offset_in_bits
10539         = round_up_to_align (object_offset_in_bits, type_align_in_bits);
10540
10541       if (object_offset_in_bits > bitpos_int)
10542         {
10543           object_offset_in_bits = deepest_bitpos - type_size_in_bits;
10544
10545           /* Round up to decl_align instead.  */
10546           object_offset_in_bits
10547             = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
10548         }
10549     }
10550   else
10551 #endif
10552     object_offset_in_bits = bitpos_int;
10553
10554   return object_offset_in_bits / BITS_PER_UNIT;
10555 }
10556 \f
10557 /* The following routines define various Dwarf attributes and any data
10558    associated with them.  */
10559
10560 /* Add a location description attribute value to a DIE.
10561
10562    This emits location attributes suitable for whole variables and
10563    whole parameters.  Note that the location attributes for struct fields are
10564    generated by the routine `data_member_location_attribute' below.  */
10565
10566 static inline void
10567 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
10568                              dw_loc_descr_ref descr)
10569 {
10570   if (descr != 0)
10571     add_AT_loc (die, attr_kind, descr);
10572 }
10573
10574 /* Attach the specialized form of location attribute used for data members of
10575    struct and union types.  In the special case of a FIELD_DECL node which
10576    represents a bit-field, the "offset" part of this special location
10577    descriptor must indicate the distance in bytes from the lowest-addressed
10578    byte of the containing struct or union type to the lowest-addressed byte of
10579    the "containing object" for the bit-field.  (See the `field_byte_offset'
10580    function above).
10581
10582    For any given bit-field, the "containing object" is a hypothetical object
10583    (of some integral or enum type) within which the given bit-field lives.  The
10584    type of this hypothetical "containing object" is always the same as the
10585    declared type of the individual bit-field itself (for GCC anyway... the
10586    DWARF spec doesn't actually mandate this).  Note that it is the size (in
10587    bytes) of the hypothetical "containing object" which will be given in the
10588    DW_AT_byte_size attribute for this bit-field.  (See the
10589    `byte_size_attribute' function below.)  It is also used when calculating the
10590    value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
10591    function below.)  */
10592
10593 static void
10594 add_data_member_location_attribute (dw_die_ref die, tree decl)
10595 {
10596   HOST_WIDE_INT offset;
10597   dw_loc_descr_ref loc_descr = 0;
10598
10599   if (TREE_CODE (decl) == TREE_BINFO)
10600     {
10601       /* We're working on the TAG_inheritance for a base class.  */
10602       if (BINFO_VIRTUAL_P (decl) && is_cxx ())
10603         {
10604           /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
10605              aren't at a fixed offset from all (sub)objects of the same
10606              type.  We need to extract the appropriate offset from our
10607              vtable.  The following dwarf expression means
10608
10609                BaseAddr = ObAddr + *((*ObAddr) - Offset)
10610
10611              This is specific to the V3 ABI, of course.  */
10612
10613           dw_loc_descr_ref tmp;
10614
10615           /* Make a copy of the object address.  */
10616           tmp = new_loc_descr (DW_OP_dup, 0, 0);
10617           add_loc_descr (&loc_descr, tmp);
10618
10619           /* Extract the vtable address.  */
10620           tmp = new_loc_descr (DW_OP_deref, 0, 0);
10621           add_loc_descr (&loc_descr, tmp);
10622
10623           /* Calculate the address of the offset.  */
10624           offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
10625           gcc_assert (offset < 0);
10626
10627           tmp = int_loc_descriptor (-offset);
10628           add_loc_descr (&loc_descr, tmp);
10629           tmp = new_loc_descr (DW_OP_minus, 0, 0);
10630           add_loc_descr (&loc_descr, tmp);
10631
10632           /* Extract the offset.  */
10633           tmp = new_loc_descr (DW_OP_deref, 0, 0);
10634           add_loc_descr (&loc_descr, tmp);
10635
10636           /* Add it to the object address.  */
10637           tmp = new_loc_descr (DW_OP_plus, 0, 0);
10638           add_loc_descr (&loc_descr, tmp);
10639         }
10640       else
10641         offset = tree_low_cst (BINFO_OFFSET (decl), 0);
10642     }
10643   else
10644     offset = field_byte_offset (decl);
10645
10646   if (! loc_descr)
10647     {
10648       enum dwarf_location_atom op;
10649
10650       /* The DWARF2 standard says that we should assume that the structure
10651          address is already on the stack, so we can specify a structure field
10652          address by using DW_OP_plus_uconst.  */
10653
10654 #ifdef MIPS_DEBUGGING_INFO
10655       /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
10656          operator correctly.  It works only if we leave the offset on the
10657          stack.  */
10658       op = DW_OP_constu;
10659 #else
10660       op = DW_OP_plus_uconst;
10661 #endif
10662
10663       loc_descr = new_loc_descr (op, offset, 0);
10664     }
10665
10666   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
10667 }
10668
10669 /* Writes integer values to dw_vec_const array.  */
10670
10671 static void
10672 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
10673 {
10674   while (size != 0)
10675     {
10676       *dest++ = val & 0xff;
10677       val >>= 8;
10678       --size;
10679     }
10680 }
10681
10682 /* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
10683
10684 static HOST_WIDE_INT
10685 extract_int (const unsigned char *src, unsigned int size)
10686 {
10687   HOST_WIDE_INT val = 0;
10688
10689   src += size;
10690   while (size != 0)
10691     {
10692       val <<= 8;
10693       val |= *--src & 0xff;
10694       --size;
10695     }
10696   return val;
10697 }
10698
10699 /* Writes floating point values to dw_vec_const array.  */
10700
10701 static void
10702 insert_float (const_rtx rtl, unsigned char *array)
10703 {
10704   REAL_VALUE_TYPE rv;
10705   long val[4];
10706   int i;
10707
10708   REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
10709   real_to_target (val, &rv, GET_MODE (rtl));
10710
10711   /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
10712   for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
10713     {
10714       insert_int (val[i], 4, array);
10715       array += 4;
10716     }
10717 }
10718
10719 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
10720    does not have a "location" either in memory or in a register.  These
10721    things can arise in GNU C when a constant is passed as an actual parameter
10722    to an inlined function.  They can also arise in C++ where declared
10723    constants do not necessarily get memory "homes".  */
10724
10725 static void
10726 add_const_value_attribute (dw_die_ref die, rtx rtl)
10727 {
10728   switch (GET_CODE (rtl))
10729     {
10730     case CONST_INT:
10731       {
10732         HOST_WIDE_INT val = INTVAL (rtl);
10733
10734         if (val < 0)
10735           add_AT_int (die, DW_AT_const_value, val);
10736         else
10737           add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
10738       }
10739       break;
10740
10741     case CONST_DOUBLE:
10742       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
10743          floating-point constant.  A CONST_DOUBLE is used whenever the
10744          constant requires more than one word in order to be adequately
10745          represented.  We output CONST_DOUBLEs as blocks.  */
10746       {
10747         enum machine_mode mode = GET_MODE (rtl);
10748
10749         if (SCALAR_FLOAT_MODE_P (mode))
10750           {
10751             unsigned int length = GET_MODE_SIZE (mode);
10752             unsigned char *array = GGC_NEWVEC (unsigned char, length);
10753
10754             insert_float (rtl, array);
10755             add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
10756           }
10757         else
10758           {
10759             /* ??? We really should be using HOST_WIDE_INT throughout.  */
10760             gcc_assert (HOST_BITS_PER_LONG == HOST_BITS_PER_WIDE_INT);
10761
10762             add_AT_long_long (die, DW_AT_const_value,
10763                               CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
10764           }
10765       }
10766       break;
10767
10768     case CONST_VECTOR:
10769       {
10770         enum machine_mode mode = GET_MODE (rtl);
10771         unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
10772         unsigned int length = CONST_VECTOR_NUNITS (rtl);
10773         unsigned char *array = GGC_NEWVEC (unsigned char, length * elt_size);
10774         unsigned int i;
10775         unsigned char *p;
10776
10777         switch (GET_MODE_CLASS (mode))
10778           {
10779           case MODE_VECTOR_INT:
10780             for (i = 0, p = array; i < length; i++, p += elt_size)
10781               {
10782                 rtx elt = CONST_VECTOR_ELT (rtl, i);
10783                 HOST_WIDE_INT lo, hi;
10784
10785                 switch (GET_CODE (elt))
10786                   {
10787                   case CONST_INT:
10788                     lo = INTVAL (elt);
10789                     hi = -(lo < 0);
10790                     break;
10791
10792                   case CONST_DOUBLE:
10793                     lo = CONST_DOUBLE_LOW (elt);
10794                     hi = CONST_DOUBLE_HIGH (elt);
10795                     break;
10796
10797                   default:
10798                     gcc_unreachable ();
10799                   }
10800
10801                 if (elt_size <= sizeof (HOST_WIDE_INT))
10802                   insert_int (lo, elt_size, p);
10803                 else
10804                   {
10805                     unsigned char *p0 = p;
10806                     unsigned char *p1 = p + sizeof (HOST_WIDE_INT);
10807
10808                     gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
10809                     if (WORDS_BIG_ENDIAN)
10810                       {
10811                         p0 = p1;
10812                         p1 = p;
10813                       }
10814                     insert_int (lo, sizeof (HOST_WIDE_INT), p0);
10815                     insert_int (hi, sizeof (HOST_WIDE_INT), p1);
10816                   }
10817               }
10818             break;
10819
10820           case MODE_VECTOR_FLOAT:
10821             for (i = 0, p = array; i < length; i++, p += elt_size)
10822               {
10823                 rtx elt = CONST_VECTOR_ELT (rtl, i);
10824                 insert_float (elt, p);
10825               }
10826             break;
10827
10828           default:
10829             gcc_unreachable ();
10830           }
10831
10832         add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
10833       }
10834       break;
10835
10836     case CONST_STRING:
10837       add_AT_string (die, DW_AT_const_value, XSTR (rtl, 0));
10838       break;
10839
10840     case SYMBOL_REF:
10841     case LABEL_REF:
10842     case CONST:
10843       add_AT_addr (die, DW_AT_const_value, rtl);
10844       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
10845       break;
10846
10847     case PLUS:
10848       /* In cases where an inlined instance of an inline function is passed
10849          the address of an `auto' variable (which is local to the caller) we
10850          can get a situation where the DECL_RTL of the artificial local
10851          variable (for the inlining) which acts as a stand-in for the
10852          corresponding formal parameter (of the inline function) will look
10853          like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
10854          exactly a compile-time constant expression, but it isn't the address
10855          of the (artificial) local variable either.  Rather, it represents the
10856          *value* which the artificial local variable always has during its
10857          lifetime.  We currently have no way to represent such quasi-constant
10858          values in Dwarf, so for now we just punt and generate nothing.  */
10859       break;
10860
10861     default:
10862       /* No other kinds of rtx should be possible here.  */
10863       gcc_unreachable ();
10864     }
10865
10866 }
10867
10868 /* Determine whether the evaluation of EXPR references any variables
10869    or functions which aren't otherwise used (and therefore may not be
10870    output).  */
10871 static tree
10872 reference_to_unused (tree * tp, int * walk_subtrees,
10873                      void * data ATTRIBUTE_UNUSED)
10874 {
10875   if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
10876     *walk_subtrees = 0;
10877
10878   if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
10879       && ! TREE_ASM_WRITTEN (*tp))
10880     return *tp;
10881   /* ???  The C++ FE emits debug information for using decls, so
10882      putting gcc_unreachable here falls over.  See PR31899.  For now
10883      be conservative.  */
10884   else if (!cgraph_global_info_ready
10885            && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
10886     return *tp;
10887   else if (DECL_P (*tp) && TREE_CODE (*tp) == VAR_DECL)
10888     {
10889       struct varpool_node *node = varpool_node (*tp);
10890       if (!node->needed)
10891         return *tp;
10892     }
10893   else if (DECL_P (*tp) && TREE_CODE (*tp) == FUNCTION_DECL
10894            && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
10895     {
10896       struct cgraph_node *node = cgraph_node (*tp);
10897       if (!node->output)
10898         return *tp;
10899     }
10900   else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
10901     return *tp;
10902
10903   return NULL_TREE;
10904 }
10905
10906 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
10907    for use in a later add_const_value_attribute call.  */
10908
10909 static rtx
10910 rtl_for_decl_init (tree init, tree type)
10911 {
10912   rtx rtl = NULL_RTX;
10913
10914   /* If a variable is initialized with a string constant without embedded
10915      zeros, build CONST_STRING.  */
10916   if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
10917     {
10918       tree enttype = TREE_TYPE (type);
10919       tree domain = TYPE_DOMAIN (type);
10920       enum machine_mode mode = TYPE_MODE (enttype);
10921
10922       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
10923           && domain
10924           && integer_zerop (TYPE_MIN_VALUE (domain))
10925           && compare_tree_int (TYPE_MAX_VALUE (domain),
10926                                TREE_STRING_LENGTH (init) - 1) == 0
10927           && ((size_t) TREE_STRING_LENGTH (init)
10928               == strlen (TREE_STRING_POINTER (init)) + 1))
10929         rtl = gen_rtx_CONST_STRING (VOIDmode,
10930                                     ggc_strdup (TREE_STRING_POINTER (init)));
10931     }
10932   /* Other aggregates, and complex values, could be represented using
10933      CONCAT: FIXME!  */
10934   else if (AGGREGATE_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
10935     ;
10936   /* Vectors only work if their mode is supported by the target.
10937      FIXME: generic vectors ought to work too.  */
10938   else if (TREE_CODE (type) == VECTOR_TYPE && TYPE_MODE (type) == BLKmode)
10939     ;
10940   /* If the initializer is something that we know will expand into an
10941      immediate RTL constant, expand it now.  We must be careful not to
10942      reference variables which won't be output.  */
10943   else if (initializer_constant_valid_p (init, type)
10944            && ! walk_tree (&init, reference_to_unused, NULL, NULL))
10945     {
10946       /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
10947          possible.  */
10948       if (TREE_CODE (type) == VECTOR_TYPE)
10949         switch (TREE_CODE (init))
10950           {
10951           case VECTOR_CST:
10952             break;
10953           case CONSTRUCTOR:
10954             if (TREE_CONSTANT (init))
10955               {
10956                 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
10957                 bool constant_p = true;
10958                 tree value;
10959                 unsigned HOST_WIDE_INT ix;
10960
10961                 /* Even when ctor is constant, it might contain non-*_CST
10962                    elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
10963                    belong into VECTOR_CST nodes.  */
10964                 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
10965                   if (!CONSTANT_CLASS_P (value))
10966                     {
10967                       constant_p = false;
10968                       break;
10969                     }
10970
10971                 if (constant_p)
10972                   {
10973                     init = build_vector_from_ctor (type, elts);
10974                     break;
10975                   }
10976               }
10977             /* FALLTHRU */
10978
10979           default:
10980             return NULL;
10981           }
10982
10983       rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
10984
10985       /* If expand_expr returns a MEM, it wasn't immediate.  */
10986       gcc_assert (!rtl || !MEM_P (rtl));
10987     }
10988
10989   return rtl;
10990 }
10991
10992 /* Generate RTL for the variable DECL to represent its location.  */
10993
10994 static rtx
10995 rtl_for_decl_location (tree decl)
10996 {
10997   rtx rtl;
10998
10999   /* Here we have to decide where we are going to say the parameter "lives"
11000      (as far as the debugger is concerned).  We only have a couple of
11001      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
11002
11003      DECL_RTL normally indicates where the parameter lives during most of the
11004      activation of the function.  If optimization is enabled however, this
11005      could be either NULL or else a pseudo-reg.  Both of those cases indicate
11006      that the parameter doesn't really live anywhere (as far as the code
11007      generation parts of GCC are concerned) during most of the function's
11008      activation.  That will happen (for example) if the parameter is never
11009      referenced within the function.
11010
11011      We could just generate a location descriptor here for all non-NULL
11012      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
11013      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
11014      where DECL_RTL is NULL or is a pseudo-reg.
11015
11016      Note however that we can only get away with using DECL_INCOMING_RTL as
11017      a backup substitute for DECL_RTL in certain limited cases.  In cases
11018      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
11019      we can be sure that the parameter was passed using the same type as it is
11020      declared to have within the function, and that its DECL_INCOMING_RTL
11021      points us to a place where a value of that type is passed.
11022
11023      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
11024      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
11025      because in these cases DECL_INCOMING_RTL points us to a value of some
11026      type which is *different* from the type of the parameter itself.  Thus,
11027      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
11028      such cases, the debugger would end up (for example) trying to fetch a
11029      `float' from a place which actually contains the first part of a
11030      `double'.  That would lead to really incorrect and confusing
11031      output at debug-time.
11032
11033      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
11034      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
11035      are a couple of exceptions however.  On little-endian machines we can
11036      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
11037      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
11038      an integral type that is smaller than TREE_TYPE (decl). These cases arise
11039      when (on a little-endian machine) a non-prototyped function has a
11040      parameter declared to be of type `short' or `char'.  In such cases,
11041      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
11042      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
11043      passed `int' value.  If the debugger then uses that address to fetch
11044      a `short' or a `char' (on a little-endian machine) the result will be
11045      the correct data, so we allow for such exceptional cases below.
11046
11047      Note that our goal here is to describe the place where the given formal
11048      parameter lives during most of the function's activation (i.e. between the
11049      end of the prologue and the start of the epilogue).  We'll do that as best
11050      as we can. Note however that if the given formal parameter is modified
11051      sometime during the execution of the function, then a stack backtrace (at
11052      debug-time) will show the function as having been called with the *new*
11053      value rather than the value which was originally passed in.  This happens
11054      rarely enough that it is not a major problem, but it *is* a problem, and
11055      I'd like to fix it.
11056
11057      A future version of dwarf2out.c may generate two additional attributes for
11058      any given DW_TAG_formal_parameter DIE which will describe the "passed
11059      type" and the "passed location" for the given formal parameter in addition
11060      to the attributes we now generate to indicate the "declared type" and the
11061      "active location" for each parameter.  This additional set of attributes
11062      could be used by debuggers for stack backtraces. Separately, note that
11063      sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
11064      This happens (for example) for inlined-instances of inline function formal
11065      parameters which are never referenced.  This really shouldn't be
11066      happening.  All PARM_DECL nodes should get valid non-NULL
11067      DECL_INCOMING_RTL values.  FIXME.  */
11068
11069   /* Use DECL_RTL as the "location" unless we find something better.  */
11070   rtl = DECL_RTL_IF_SET (decl);
11071
11072   /* When generating abstract instances, ignore everything except
11073      constants, symbols living in memory, and symbols living in
11074      fixed registers.  */
11075   if (! reload_completed)
11076     {
11077       if (rtl
11078           && (CONSTANT_P (rtl)
11079               || (MEM_P (rtl)
11080                   && CONSTANT_P (XEXP (rtl, 0)))
11081               || (REG_P (rtl)
11082                   && TREE_CODE (decl) == VAR_DECL
11083                   && TREE_STATIC (decl))))
11084         {
11085           rtl = targetm.delegitimize_address (rtl);
11086           return rtl;
11087         }
11088       rtl = NULL_RTX;
11089     }
11090   else if (TREE_CODE (decl) == PARM_DECL)
11091     {
11092       if (rtl == NULL_RTX || is_pseudo_reg (rtl))
11093         {
11094           tree declared_type = TREE_TYPE (decl);
11095           tree passed_type = DECL_ARG_TYPE (decl);
11096           enum machine_mode dmode = TYPE_MODE (declared_type);
11097           enum machine_mode pmode = TYPE_MODE (passed_type);
11098
11099           /* This decl represents a formal parameter which was optimized out.
11100              Note that DECL_INCOMING_RTL may be NULL in here, but we handle
11101              all cases where (rtl == NULL_RTX) just below.  */
11102           if (dmode == pmode)
11103             rtl = DECL_INCOMING_RTL (decl);
11104           else if (SCALAR_INT_MODE_P (dmode)
11105                    && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
11106                    && DECL_INCOMING_RTL (decl))
11107             {
11108               rtx inc = DECL_INCOMING_RTL (decl);
11109               if (REG_P (inc))
11110                 rtl = inc;
11111               else if (MEM_P (inc))
11112                 {
11113                   if (BYTES_BIG_ENDIAN)
11114                     rtl = adjust_address_nv (inc, dmode,
11115                                              GET_MODE_SIZE (pmode)
11116                                              - GET_MODE_SIZE (dmode));
11117                   else
11118                     rtl = inc;
11119                 }
11120             }
11121         }
11122
11123       /* If the parm was passed in registers, but lives on the stack, then
11124          make a big endian correction if the mode of the type of the
11125          parameter is not the same as the mode of the rtl.  */
11126       /* ??? This is the same series of checks that are made in dbxout.c before
11127          we reach the big endian correction code there.  It isn't clear if all
11128          of these checks are necessary here, but keeping them all is the safe
11129          thing to do.  */
11130       else if (MEM_P (rtl)
11131                && XEXP (rtl, 0) != const0_rtx
11132                && ! CONSTANT_P (XEXP (rtl, 0))
11133                /* Not passed in memory.  */
11134                && !MEM_P (DECL_INCOMING_RTL (decl))
11135                /* Not passed by invisible reference.  */
11136                && (!REG_P (XEXP (rtl, 0))
11137                    || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
11138                    || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
11139 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
11140                    || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
11141 #endif
11142                      )
11143                /* Big endian correction check.  */
11144                && BYTES_BIG_ENDIAN
11145                && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
11146                && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
11147                    < UNITS_PER_WORD))
11148         {
11149           int offset = (UNITS_PER_WORD
11150                         - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
11151
11152           rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
11153                              plus_constant (XEXP (rtl, 0), offset));
11154         }
11155     }
11156   else if (TREE_CODE (decl) == VAR_DECL
11157            && rtl
11158            && MEM_P (rtl)
11159            && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
11160            && BYTES_BIG_ENDIAN)
11161     {
11162       int rsize = GET_MODE_SIZE (GET_MODE (rtl));
11163       int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
11164
11165       /* If a variable is declared "register" yet is smaller than
11166          a register, then if we store the variable to memory, it
11167          looks like we're storing a register-sized value, when in
11168          fact we are not.  We need to adjust the offset of the
11169          storage location to reflect the actual value's bytes,
11170          else gdb will not be able to display it.  */
11171       if (rsize > dsize)
11172         rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
11173                            plus_constant (XEXP (rtl, 0), rsize-dsize));
11174     }
11175
11176   /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
11177      and will have been substituted directly into all expressions that use it.
11178      C does not have such a concept, but C++ and other languages do.  */
11179   if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
11180     rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
11181
11182   if (rtl)
11183     rtl = targetm.delegitimize_address (rtl);
11184
11185   /* If we don't look past the constant pool, we risk emitting a
11186      reference to a constant pool entry that isn't referenced from
11187      code, and thus is not emitted.  */
11188   if (rtl)
11189     rtl = avoid_constant_pool_reference (rtl);
11190
11191   return rtl;
11192 }
11193
11194 /* We need to figure out what section we should use as the base for the
11195    address ranges where a given location is valid.
11196    1. If this particular DECL has a section associated with it, use that.
11197    2. If this function has a section associated with it, use that.
11198    3. Otherwise, use the text section.
11199    XXX: If you split a variable across multiple sections, we won't notice.  */
11200
11201 static const char *
11202 secname_for_decl (const_tree decl)
11203 {
11204   const char *secname;
11205
11206   if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
11207     {
11208       tree sectree = DECL_SECTION_NAME (decl);
11209       secname = TREE_STRING_POINTER (sectree);
11210     }
11211   else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
11212     {
11213       tree sectree = DECL_SECTION_NAME (current_function_decl);
11214       secname = TREE_STRING_POINTER (sectree);
11215     }
11216   else if (cfun && in_cold_section_p)
11217     secname = crtl->subsections.cold_section_label;
11218   else
11219     secname = text_section_label;
11220
11221   return secname;
11222 }
11223
11224 /* Check whether decl is a Fortran COMMON symbol.  If not, NULL_RTX is returned.
11225    If so, the rtx for the SYMBOL_REF for the COMMON block is returned, and the
11226    value is the offset into the common block for the symbol.  */
11227
11228 static tree
11229 fortran_common (tree decl, HOST_WIDE_INT *value)
11230 {
11231   tree val_expr, cvar;
11232   enum machine_mode mode;
11233   HOST_WIDE_INT bitsize, bitpos;
11234   tree offset;
11235   int volatilep = 0, unsignedp = 0;
11236
11237   /* If the decl isn't a VAR_DECL, or if it isn't public or static, or if
11238      it does not have a value (the offset into the common area), or if it
11239      is thread local (as opposed to global) then it isn't common, and shouldn't
11240      be handled as such.  */
11241   if (TREE_CODE (decl) != VAR_DECL
11242       || !TREE_PUBLIC (decl)
11243       || !TREE_STATIC (decl)
11244       || !DECL_HAS_VALUE_EXPR_P (decl)
11245       || !is_fortran ())
11246     return NULL_TREE;
11247
11248   val_expr = DECL_VALUE_EXPR (decl);
11249   if (TREE_CODE (val_expr) != COMPONENT_REF)
11250     return NULL_TREE;
11251
11252   cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
11253                               &mode, &unsignedp, &volatilep, true);
11254
11255   if (cvar == NULL_TREE
11256       || TREE_CODE (cvar) != VAR_DECL
11257       || DECL_ARTIFICIAL (cvar)
11258       || !TREE_PUBLIC (cvar))
11259     return NULL_TREE;
11260
11261   *value = 0;
11262   if (offset != NULL)
11263     {
11264       if (!host_integerp (offset, 0))
11265         return NULL_TREE;
11266       *value = tree_low_cst (offset, 0);
11267     }
11268   if (bitpos != 0)
11269     *value += bitpos / BITS_PER_UNIT;
11270
11271   return cvar;
11272 }
11273
11274
11275 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
11276    data attribute for a variable or a parameter.  We generate the
11277    DW_AT_const_value attribute only in those cases where the given variable
11278    or parameter does not have a true "location" either in memory or in a
11279    register.  This can happen (for example) when a constant is passed as an
11280    actual argument in a call to an inline function.  (It's possible that
11281    these things can crop up in other ways also.)  Note that one type of
11282    constant value which can be passed into an inlined function is a constant
11283    pointer.  This can happen for example if an actual argument in an inlined
11284    function call evaluates to a compile-time constant address.  */
11285
11286 static void
11287 add_location_or_const_value_attribute (dw_die_ref die, tree decl,
11288                                        enum dwarf_attribute attr)
11289 {
11290   rtx rtl;
11291   dw_loc_descr_ref descr;
11292   var_loc_list *loc_list;
11293   struct var_loc_node *node;
11294   if (TREE_CODE (decl) == ERROR_MARK)
11295     return;
11296
11297   gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
11298               || TREE_CODE (decl) == RESULT_DECL);
11299
11300   /* See if we possibly have multiple locations for this variable.  */
11301   loc_list = lookup_decl_loc (decl);
11302
11303   /* If it truly has multiple locations, the first and last node will
11304      differ.  */
11305   if (loc_list && loc_list->first != loc_list->last)
11306     {
11307       const char *endname, *secname;
11308       dw_loc_list_ref list;
11309       rtx varloc;
11310       enum var_init_status initialized;
11311
11312       /* Now that we know what section we are using for a base,
11313          actually construct the list of locations.
11314          The first location information is what is passed to the
11315          function that creates the location list, and the remaining
11316          locations just get added on to that list.
11317          Note that we only know the start address for a location
11318          (IE location changes), so to build the range, we use
11319          the range [current location start, next location start].
11320          This means we have to special case the last node, and generate
11321          a range of [last location start, end of function label].  */
11322
11323       node = loc_list->first;
11324       varloc = NOTE_VAR_LOCATION (node->var_loc_note);
11325       secname = secname_for_decl (decl);
11326
11327       if (NOTE_VAR_LOCATION_LOC (node->var_loc_note))
11328         initialized = NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
11329       else
11330         initialized = VAR_INIT_STATUS_INITIALIZED;
11331
11332       list = new_loc_list (loc_descriptor (varloc, initialized),
11333                            node->label, node->next->label, secname, 1);
11334       node = node->next;
11335
11336       for (; node->next; node = node->next)
11337         if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
11338           {
11339             /* The variable has a location between NODE->LABEL and
11340                NODE->NEXT->LABEL.  */
11341             enum var_init_status initialized =
11342               NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
11343             varloc = NOTE_VAR_LOCATION (node->var_loc_note);
11344             add_loc_descr_to_loc_list (&list,
11345                                        loc_descriptor (varloc, initialized),
11346                                        node->label, node->next->label, secname);
11347           }
11348
11349       /* If the variable has a location at the last label
11350          it keeps its location until the end of function.  */
11351       if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
11352         {
11353           char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
11354           enum var_init_status initialized =
11355             NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
11356
11357           varloc = NOTE_VAR_LOCATION (node->var_loc_note);
11358           if (!current_function_decl)
11359             endname = text_end_label;
11360           else
11361             {
11362               ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
11363                                            current_function_funcdef_no);
11364               endname = ggc_strdup (label_id);
11365             }
11366           add_loc_descr_to_loc_list (&list,
11367                                      loc_descriptor (varloc, initialized),
11368                                      node->label, endname, secname);
11369         }
11370
11371       /* Finally, add the location list to the DIE, and we are done.  */
11372       add_AT_loc_list (die, attr, list);
11373       return;
11374     }
11375
11376   /* Try to get some constant RTL for this decl, and use that as the value of
11377      the location.  */
11378
11379   rtl = rtl_for_decl_location (decl);
11380   if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING))
11381     {
11382       add_const_value_attribute (die, rtl);
11383       return;
11384     }
11385
11386   /* If we have tried to generate the location otherwise, and it
11387      didn't work out (we wouldn't be here if we did), and we have a one entry
11388      location list, try generating a location from that.  */
11389   if (loc_list && loc_list->first)
11390     {
11391       enum var_init_status status;
11392       node = loc_list->first;
11393       status = NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
11394       descr = loc_descriptor (NOTE_VAR_LOCATION (node->var_loc_note), status);
11395       if (descr)
11396         {
11397           add_AT_location_description (die, attr, descr);
11398           return;
11399         }
11400     }
11401
11402   /* We couldn't get any rtl, so try directly generating the location
11403      description from the tree.  */
11404   descr = loc_descriptor_from_tree (decl);
11405   if (descr)
11406     {
11407       add_AT_location_description (die, attr, descr);
11408       return;
11409     }
11410   /* None of that worked, so it must not really have a location;
11411      try adding a constant value attribute from the DECL_INITIAL.  */
11412   tree_add_const_value_attribute (die, decl);
11413 }
11414
11415 /* If we don't have a copy of this variable in memory for some reason (such
11416    as a C++ member constant that doesn't have an out-of-line definition),
11417    we should tell the debugger about the constant value.  */
11418
11419 static void
11420 tree_add_const_value_attribute (dw_die_ref var_die, tree decl)
11421 {
11422   tree init = DECL_INITIAL (decl);
11423   tree type = TREE_TYPE (decl);
11424   rtx rtl;
11425
11426   if (TREE_READONLY (decl) && ! TREE_THIS_VOLATILE (decl) && init)
11427     /* OK */;
11428   else
11429     return;
11430
11431   rtl = rtl_for_decl_init (init, type);
11432   if (rtl)
11433     add_const_value_attribute (var_die, rtl);
11434 }
11435
11436 /* Convert the CFI instructions for the current function into a
11437    location list.  This is used for DW_AT_frame_base when we targeting
11438    a dwarf2 consumer that does not support the dwarf3
11439    DW_OP_call_frame_cfa.  OFFSET is a constant to be added to all CFA
11440    expressions.  */
11441
11442 static dw_loc_list_ref
11443 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
11444 {
11445   dw_fde_ref fde;
11446   dw_loc_list_ref list, *list_tail;
11447   dw_cfi_ref cfi;
11448   dw_cfa_location last_cfa, next_cfa;
11449   const char *start_label, *last_label, *section;
11450
11451   fde = current_fde ();
11452   gcc_assert (fde != NULL);
11453
11454   section = secname_for_decl (current_function_decl);
11455   list_tail = &list;
11456   list = NULL;
11457
11458   next_cfa.reg = INVALID_REGNUM;
11459   next_cfa.offset = 0;
11460   next_cfa.indirect = 0;
11461   next_cfa.base_offset = 0;
11462
11463   start_label = fde->dw_fde_begin;
11464
11465   /* ??? Bald assumption that the CIE opcode list does not contain
11466      advance opcodes.  */
11467   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
11468     lookup_cfa_1 (cfi, &next_cfa);
11469
11470   last_cfa = next_cfa;
11471   last_label = start_label;
11472
11473   for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
11474     switch (cfi->dw_cfi_opc)
11475       {
11476       case DW_CFA_set_loc:
11477       case DW_CFA_advance_loc1:
11478       case DW_CFA_advance_loc2:
11479       case DW_CFA_advance_loc4:
11480         if (!cfa_equal_p (&last_cfa, &next_cfa))
11481           {
11482             *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
11483                                        start_label, last_label, section,
11484                                        list == NULL);
11485
11486             list_tail = &(*list_tail)->dw_loc_next;
11487             last_cfa = next_cfa;
11488             start_label = last_label;
11489           }
11490         last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
11491         break;
11492
11493       case DW_CFA_advance_loc:
11494         /* The encoding is complex enough that we should never emit this.  */
11495       case DW_CFA_remember_state:
11496       case DW_CFA_restore_state:
11497         /* We don't handle these two in this function.  It would be possible
11498            if it were to be required.  */
11499         gcc_unreachable ();
11500
11501       default:
11502         lookup_cfa_1 (cfi, &next_cfa);
11503         break;
11504       }
11505
11506   if (!cfa_equal_p (&last_cfa, &next_cfa))
11507     {
11508       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
11509                                  start_label, last_label, section,
11510                                  list == NULL);
11511       list_tail = &(*list_tail)->dw_loc_next;
11512       start_label = last_label;
11513     }
11514   *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
11515                              start_label, fde->dw_fde_end, section,
11516                              list == NULL);
11517
11518   return list;
11519 }
11520
11521 /* Compute a displacement from the "steady-state frame pointer" to the
11522    frame base (often the same as the CFA), and store it in
11523    frame_pointer_fb_offset.  OFFSET is added to the displacement
11524    before the latter is negated.  */
11525
11526 static void
11527 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
11528 {
11529   rtx reg, elim;
11530
11531 #ifdef FRAME_POINTER_CFA_OFFSET
11532   reg = frame_pointer_rtx;
11533   offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
11534 #else
11535   reg = arg_pointer_rtx;
11536   offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
11537 #endif
11538
11539   elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
11540   if (GET_CODE (elim) == PLUS)
11541     {
11542       offset += INTVAL (XEXP (elim, 1));
11543       elim = XEXP (elim, 0);
11544     }
11545
11546   gcc_assert ((SUPPORTS_STACK_ALIGNMENT
11547                && (elim == hard_frame_pointer_rtx
11548                    || elim == stack_pointer_rtx))
11549               || elim == (frame_pointer_needed
11550                           ? hard_frame_pointer_rtx
11551                           : stack_pointer_rtx));
11552
11553   frame_pointer_fb_offset = -offset;
11554 }
11555
11556 /* Generate a DW_AT_name attribute given some string value to be included as
11557    the value of the attribute.  */
11558
11559 static void
11560 add_name_attribute (dw_die_ref die, const char *name_string)
11561 {
11562   if (name_string != NULL && *name_string != 0)
11563     {
11564       if (demangle_name_func)
11565         name_string = (*demangle_name_func) (name_string);
11566
11567       add_AT_string (die, DW_AT_name, name_string);
11568     }
11569 }
11570
11571 /* Generate a DW_AT_comp_dir attribute for DIE.  */
11572
11573 static void
11574 add_comp_dir_attribute (dw_die_ref die)
11575 {
11576   const char *wd = get_src_pwd ();
11577   if (wd != NULL)
11578     add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
11579 }
11580
11581 /* Given a tree node describing an array bound (either lower or upper) output
11582    a representation for that bound.  */
11583
11584 static void
11585 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
11586 {
11587   switch (TREE_CODE (bound))
11588     {
11589     case ERROR_MARK:
11590       return;
11591
11592     /* All fixed-bounds are represented by INTEGER_CST nodes.  */
11593     case INTEGER_CST:
11594       if (! host_integerp (bound, 0)
11595           || (bound_attr == DW_AT_lower_bound
11596               && (((is_c_family () || is_java ()) &&  integer_zerop (bound))
11597                   || (is_fortran () && integer_onep (bound)))))
11598         /* Use the default.  */
11599         ;
11600       else
11601         add_AT_unsigned (subrange_die, bound_attr, tree_low_cst (bound, 0));
11602       break;
11603
11604     CASE_CONVERT:
11605     case VIEW_CONVERT_EXPR:
11606       add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
11607       break;
11608
11609     case SAVE_EXPR:
11610       break;
11611
11612     case VAR_DECL:
11613     case PARM_DECL:
11614     case RESULT_DECL:
11615       {
11616         dw_die_ref decl_die = lookup_decl_die (bound);
11617
11618         /* ??? Can this happen, or should the variable have been bound
11619            first?  Probably it can, since I imagine that we try to create
11620            the types of parameters in the order in which they exist in
11621            the list, and won't have created a forward reference to a
11622            later parameter.  */
11623         if (decl_die != NULL)
11624           add_AT_die_ref (subrange_die, bound_attr, decl_die);
11625         break;
11626       }
11627
11628     default:
11629       {
11630         /* Otherwise try to create a stack operation procedure to
11631            evaluate the value of the array bound.  */
11632
11633         dw_die_ref ctx, decl_die;
11634         dw_loc_descr_ref loc;
11635
11636         loc = loc_descriptor_from_tree (bound);
11637         if (loc == NULL)
11638           break;
11639
11640         if (current_function_decl == 0)
11641           ctx = comp_unit_die;
11642         else
11643           ctx = lookup_decl_die (current_function_decl);
11644
11645         decl_die = new_die (DW_TAG_variable, ctx, bound);
11646         add_AT_flag (decl_die, DW_AT_artificial, 1);
11647         add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
11648         add_AT_loc (decl_die, DW_AT_location, loc);
11649
11650         add_AT_die_ref (subrange_die, bound_attr, decl_die);
11651         break;
11652       }
11653     }
11654 }
11655
11656 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
11657    possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
11658    Note that the block of subscript information for an array type also
11659    includes information about the element type of the given array type.  */
11660
11661 static void
11662 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
11663 {
11664   unsigned dimension_number;
11665   tree lower, upper;
11666   dw_die_ref subrange_die;
11667
11668   for (dimension_number = 0;
11669        TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
11670        type = TREE_TYPE (type), dimension_number++)
11671     {
11672       tree domain = TYPE_DOMAIN (type);
11673
11674       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
11675          and (in GNU C only) variable bounds.  Handle all three forms
11676          here.  */
11677       subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
11678       if (domain)
11679         {
11680           /* We have an array type with specified bounds.  */
11681           lower = TYPE_MIN_VALUE (domain);
11682           upper = TYPE_MAX_VALUE (domain);
11683
11684           /* Define the index type.  */
11685           if (TREE_TYPE (domain))
11686             {
11687               /* ??? This is probably an Ada unnamed subrange type.  Ignore the
11688                  TREE_TYPE field.  We can't emit debug info for this
11689                  because it is an unnamed integral type.  */
11690               if (TREE_CODE (domain) == INTEGER_TYPE
11691                   && TYPE_NAME (domain) == NULL_TREE
11692                   && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
11693                   && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
11694                 ;
11695               else
11696                 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
11697                                     type_die);
11698             }
11699
11700           /* ??? If upper is NULL, the array has unspecified length,
11701              but it does have a lower bound.  This happens with Fortran
11702                dimension arr(N:*)
11703              Since the debugger is definitely going to need to know N
11704              to produce useful results, go ahead and output the lower
11705              bound solo, and hope the debugger can cope.  */
11706
11707           add_bound_info (subrange_die, DW_AT_lower_bound, lower);
11708           if (upper)
11709             add_bound_info (subrange_die, DW_AT_upper_bound, upper);
11710         }
11711
11712       /* Otherwise we have an array type with an unspecified length.  The
11713          DWARF-2 spec does not say how to handle this; let's just leave out the
11714          bounds.  */
11715     }
11716 }
11717
11718 static void
11719 add_byte_size_attribute (dw_die_ref die, tree tree_node)
11720 {
11721   unsigned size;
11722
11723   switch (TREE_CODE (tree_node))
11724     {
11725     case ERROR_MARK:
11726       size = 0;
11727       break;
11728     case ENUMERAL_TYPE:
11729     case RECORD_TYPE:
11730     case UNION_TYPE:
11731     case QUAL_UNION_TYPE:
11732       size = int_size_in_bytes (tree_node);
11733       break;
11734     case FIELD_DECL:
11735       /* For a data member of a struct or union, the DW_AT_byte_size is
11736          generally given as the number of bytes normally allocated for an
11737          object of the *declared* type of the member itself.  This is true
11738          even for bit-fields.  */
11739       size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
11740       break;
11741     default:
11742       gcc_unreachable ();
11743     }
11744
11745   /* Note that `size' might be -1 when we get to this point.  If it is, that
11746      indicates that the byte size of the entity in question is variable.  We
11747      have no good way of expressing this fact in Dwarf at the present time,
11748      so just let the -1 pass on through.  */
11749   add_AT_unsigned (die, DW_AT_byte_size, size);
11750 }
11751
11752 /* For a FIELD_DECL node which represents a bit-field, output an attribute
11753    which specifies the distance in bits from the highest order bit of the
11754    "containing object" for the bit-field to the highest order bit of the
11755    bit-field itself.
11756
11757    For any given bit-field, the "containing object" is a hypothetical object
11758    (of some integral or enum type) within which the given bit-field lives.  The
11759    type of this hypothetical "containing object" is always the same as the
11760    declared type of the individual bit-field itself.  The determination of the
11761    exact location of the "containing object" for a bit-field is rather
11762    complicated.  It's handled by the `field_byte_offset' function (above).
11763
11764    Note that it is the size (in bytes) of the hypothetical "containing object"
11765    which will be given in the DW_AT_byte_size attribute for this bit-field.
11766    (See `byte_size_attribute' above).  */
11767
11768 static inline void
11769 add_bit_offset_attribute (dw_die_ref die, tree decl)
11770 {
11771   HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
11772   tree type = DECL_BIT_FIELD_TYPE (decl);
11773   HOST_WIDE_INT bitpos_int;
11774   HOST_WIDE_INT highest_order_object_bit_offset;
11775   HOST_WIDE_INT highest_order_field_bit_offset;
11776   HOST_WIDE_INT unsigned bit_offset;
11777
11778   /* Must be a field and a bit field.  */
11779   gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
11780
11781   /* We can't yet handle bit-fields whose offsets are variable, so if we
11782      encounter such things, just return without generating any attribute
11783      whatsoever.  Likewise for variable or too large size.  */
11784   if (! host_integerp (bit_position (decl), 0)
11785       || ! host_integerp (DECL_SIZE (decl), 1))
11786     return;
11787
11788   bitpos_int = int_bit_position (decl);
11789
11790   /* Note that the bit offset is always the distance (in bits) from the
11791      highest-order bit of the "containing object" to the highest-order bit of
11792      the bit-field itself.  Since the "high-order end" of any object or field
11793      is different on big-endian and little-endian machines, the computation
11794      below must take account of these differences.  */
11795   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
11796   highest_order_field_bit_offset = bitpos_int;
11797
11798   if (! BYTES_BIG_ENDIAN)
11799     {
11800       highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
11801       highest_order_object_bit_offset += simple_type_size_in_bits (type);
11802     }
11803
11804   bit_offset
11805     = (! BYTES_BIG_ENDIAN
11806        ? highest_order_object_bit_offset - highest_order_field_bit_offset
11807        : highest_order_field_bit_offset - highest_order_object_bit_offset);
11808
11809   add_AT_unsigned (die, DW_AT_bit_offset, bit_offset);
11810 }
11811
11812 /* For a FIELD_DECL node which represents a bit field, output an attribute
11813    which specifies the length in bits of the given field.  */
11814
11815 static inline void
11816 add_bit_size_attribute (dw_die_ref die, tree decl)
11817 {
11818   /* Must be a field and a bit field.  */
11819   gcc_assert (TREE_CODE (decl) == FIELD_DECL
11820               && DECL_BIT_FIELD_TYPE (decl));
11821
11822   if (host_integerp (DECL_SIZE (decl), 1))
11823     add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
11824 }
11825
11826 /* If the compiled language is ANSI C, then add a 'prototyped'
11827    attribute, if arg types are given for the parameters of a function.  */
11828
11829 static inline void
11830 add_prototyped_attribute (dw_die_ref die, tree func_type)
11831 {
11832   if (get_AT_unsigned (comp_unit_die, DW_AT_language) == DW_LANG_C89
11833       && TYPE_ARG_TYPES (func_type) != NULL)
11834     add_AT_flag (die, DW_AT_prototyped, 1);
11835 }
11836
11837 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
11838    by looking in either the type declaration or object declaration
11839    equate table.  */
11840
11841 static inline void
11842 add_abstract_origin_attribute (dw_die_ref die, tree origin)
11843 {
11844   dw_die_ref origin_die = NULL;
11845
11846   if (TREE_CODE (origin) != FUNCTION_DECL)
11847     {
11848       /* We may have gotten separated from the block for the inlined
11849          function, if we're in an exception handler or some such; make
11850          sure that the abstract function has been written out.
11851
11852          Doing this for nested functions is wrong, however; functions are
11853          distinct units, and our context might not even be inline.  */
11854       tree fn = origin;
11855
11856       if (TYPE_P (fn))
11857         fn = TYPE_STUB_DECL (fn);
11858
11859       fn = decl_function_context (fn);
11860       if (fn)
11861         dwarf2out_abstract_function (fn);
11862     }
11863
11864   if (DECL_P (origin))
11865     origin_die = lookup_decl_die (origin);
11866   else if (TYPE_P (origin))
11867     origin_die = lookup_type_die (origin);
11868
11869   /* XXX: Functions that are never lowered don't always have correct block
11870      trees (in the case of java, they simply have no block tree, in some other
11871      languages).  For these functions, there is nothing we can really do to
11872      output correct debug info for inlined functions in all cases.  Rather
11873      than die, we'll just produce deficient debug info now, in that we will
11874      have variables without a proper abstract origin.  In the future, when all
11875      functions are lowered, we should re-add a gcc_assert (origin_die)
11876      here.  */
11877
11878   if (origin_die)
11879       add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
11880 }
11881
11882 /* We do not currently support the pure_virtual attribute.  */
11883
11884 static inline void
11885 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
11886 {
11887   if (DECL_VINDEX (func_decl))
11888     {
11889       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
11890
11891       if (host_integerp (DECL_VINDEX (func_decl), 0))
11892         add_AT_loc (die, DW_AT_vtable_elem_location,
11893                     new_loc_descr (DW_OP_constu,
11894                                    tree_low_cst (DECL_VINDEX (func_decl), 0),
11895                                    0));
11896
11897       /* GNU extension: Record what type this method came from originally.  */
11898       if (debug_info_level > DINFO_LEVEL_TERSE)
11899         add_AT_die_ref (die, DW_AT_containing_type,
11900                         lookup_type_die (DECL_CONTEXT (func_decl)));
11901     }
11902 }
11903 \f
11904 /* Add source coordinate attributes for the given decl.  */
11905
11906 static void
11907 add_src_coords_attributes (dw_die_ref die, tree decl)
11908 {
11909   expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
11910
11911   add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
11912   add_AT_unsigned (die, DW_AT_decl_line, s.line);
11913 }
11914
11915 /* Add a DW_AT_name attribute and source coordinate attribute for the
11916    given decl, but only if it actually has a name.  */
11917
11918 static void
11919 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
11920 {
11921   tree decl_name;
11922
11923   decl_name = DECL_NAME (decl);
11924   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
11925     {
11926       add_name_attribute (die, dwarf2_name (decl, 0));
11927       if (! DECL_ARTIFICIAL (decl))
11928         add_src_coords_attributes (die, decl);
11929
11930       if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
11931           && TREE_PUBLIC (decl)
11932           && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
11933           && !DECL_ABSTRACT (decl)
11934           && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
11935           && !is_fortran ())
11936         add_AT_string (die, DW_AT_MIPS_linkage_name,
11937                        IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
11938     }
11939
11940 #ifdef VMS_DEBUGGING_INFO
11941   /* Get the function's name, as described by its RTL.  This may be different
11942      from the DECL_NAME name used in the source file.  */
11943   if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
11944     {
11945       add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
11946                    XEXP (DECL_RTL (decl), 0));
11947       VEC_safe_push (tree, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
11948     }
11949 #endif
11950 }
11951
11952 /* Push a new declaration scope.  */
11953
11954 static void
11955 push_decl_scope (tree scope)
11956 {
11957   VEC_safe_push (tree, gc, decl_scope_table, scope);
11958 }
11959
11960 /* Pop a declaration scope.  */
11961
11962 static inline void
11963 pop_decl_scope (void)
11964 {
11965   VEC_pop (tree, decl_scope_table);
11966 }
11967
11968 /* Return the DIE for the scope that immediately contains this type.
11969    Non-named types get global scope.  Named types nested in other
11970    types get their containing scope if it's open, or global scope
11971    otherwise.  All other types (i.e. function-local named types) get
11972    the current active scope.  */
11973
11974 static dw_die_ref
11975 scope_die_for (tree t, dw_die_ref context_die)
11976 {
11977   dw_die_ref scope_die = NULL;
11978   tree containing_scope;
11979   int i;
11980
11981   /* Non-types always go in the current scope.  */
11982   gcc_assert (TYPE_P (t));
11983
11984   containing_scope = TYPE_CONTEXT (t);
11985
11986   /* Use the containing namespace if it was passed in (for a declaration).  */
11987   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
11988     {
11989       if (context_die == lookup_decl_die (containing_scope))
11990         /* OK */;
11991       else
11992         containing_scope = NULL_TREE;
11993     }
11994
11995   /* Ignore function type "scopes" from the C frontend.  They mean that
11996      a tagged type is local to a parmlist of a function declarator, but
11997      that isn't useful to DWARF.  */
11998   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
11999     containing_scope = NULL_TREE;
12000
12001   if (containing_scope == NULL_TREE)
12002     scope_die = comp_unit_die;
12003   else if (TYPE_P (containing_scope))
12004     {
12005       /* For types, we can just look up the appropriate DIE.  But
12006          first we check to see if we're in the middle of emitting it
12007          so we know where the new DIE should go.  */
12008       for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
12009         if (VEC_index (tree, decl_scope_table, i) == containing_scope)
12010           break;
12011
12012       if (i < 0)
12013         {
12014           gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
12015                       || TREE_ASM_WRITTEN (containing_scope));
12016
12017           /* If none of the current dies are suitable, we get file scope.  */
12018           scope_die = comp_unit_die;
12019         }
12020       else
12021         scope_die = lookup_type_die (containing_scope);
12022     }
12023   else
12024     scope_die = context_die;
12025
12026   return scope_die;
12027 }
12028
12029 /* Returns nonzero if CONTEXT_DIE is internal to a function.  */
12030
12031 static inline int
12032 local_scope_p (dw_die_ref context_die)
12033 {
12034   for (; context_die; context_die = context_die->die_parent)
12035     if (context_die->die_tag == DW_TAG_inlined_subroutine
12036         || context_die->die_tag == DW_TAG_subprogram)
12037       return 1;
12038
12039   return 0;
12040 }
12041
12042 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
12043    whether or not to treat a DIE in this context as a declaration.  */
12044
12045 static inline int
12046 class_or_namespace_scope_p (dw_die_ref context_die)
12047 {
12048   return (context_die
12049           && (context_die->die_tag == DW_TAG_structure_type
12050               || context_die->die_tag == DW_TAG_class_type
12051               || context_die->die_tag == DW_TAG_interface_type
12052               || context_die->die_tag == DW_TAG_union_type
12053               || context_die->die_tag == DW_TAG_namespace));
12054 }
12055
12056 /* Many forms of DIEs require a "type description" attribute.  This
12057    routine locates the proper "type descriptor" die for the type given
12058    by 'type', and adds a DW_AT_type attribute below the given die.  */
12059
12060 static void
12061 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
12062                     int decl_volatile, dw_die_ref context_die)
12063 {
12064   enum tree_code code  = TREE_CODE (type);
12065   dw_die_ref type_die  = NULL;
12066
12067   /* ??? If this type is an unnamed subrange type of an integral, floating-point
12068      or fixed-point type, use the inner type.  This is because we have no
12069      support for unnamed types in base_type_die.  This can happen if this is
12070      an Ada subrange type.  Correct solution is emit a subrange type die.  */
12071   if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
12072       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
12073     type = TREE_TYPE (type), code = TREE_CODE (type);
12074
12075   if (code == ERROR_MARK
12076       /* Handle a special case.  For functions whose return type is void, we
12077          generate *no* type attribute.  (Note that no object may have type
12078          `void', so this only applies to function return types).  */
12079       || code == VOID_TYPE)
12080     return;
12081
12082   type_die = modified_type_die (type,
12083                                 decl_const || TYPE_READONLY (type),
12084                                 decl_volatile || TYPE_VOLATILE (type),
12085                                 context_die);
12086
12087   if (type_die != NULL)
12088     add_AT_die_ref (object_die, DW_AT_type, type_die);
12089 }
12090
12091 /* Given an object die, add the calling convention attribute for the
12092    function call type.  */
12093 static void
12094 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
12095 {
12096   enum dwarf_calling_convention value = DW_CC_normal;
12097
12098   value = targetm.dwarf_calling_convention (TREE_TYPE (decl));
12099
12100   /* DWARF doesn't provide a way to identify a program's source-level
12101      entry point.  DW_AT_calling_convention attributes are only meant
12102      to describe functions' calling conventions.  However, lacking a
12103      better way to signal the Fortran main program, we use this for the
12104      time being, following existing custom.  */
12105   if (is_fortran ()
12106       && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
12107     value = DW_CC_program;
12108
12109   /* Only add the attribute if the backend requests it, and
12110      is not DW_CC_normal.  */
12111   if (value && (value != DW_CC_normal))
12112     add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
12113 }
12114
12115 /* Given a tree pointer to a struct, class, union, or enum type node, return
12116    a pointer to the (string) tag name for the given type, or zero if the type
12117    was declared without a tag.  */
12118
12119 static const char *
12120 type_tag (const_tree type)
12121 {
12122   const char *name = 0;
12123
12124   if (TYPE_NAME (type) != 0)
12125     {
12126       tree t = 0;
12127
12128       /* Find the IDENTIFIER_NODE for the type name.  */
12129       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
12130         t = TYPE_NAME (type);
12131
12132       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
12133          a TYPE_DECL node, regardless of whether or not a `typedef' was
12134          involved.  */
12135       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12136                && ! DECL_IGNORED_P (TYPE_NAME (type)))
12137         {
12138           /* We want to be extra verbose.  Don't call dwarf_name if
12139              DECL_NAME isn't set.  The default hook for decl_printable_name
12140              doesn't like that, and in this context it's correct to return
12141              0, instead of "<anonymous>" or the like.  */
12142           if (DECL_NAME (TYPE_NAME (type)))
12143             name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
12144         }
12145
12146       /* Now get the name as a string, or invent one.  */
12147       if (!name && t != 0)
12148         name = IDENTIFIER_POINTER (t);
12149     }
12150
12151   return (name == 0 || *name == '\0') ? 0 : name;
12152 }
12153
12154 /* Return the type associated with a data member, make a special check
12155    for bit field types.  */
12156
12157 static inline tree
12158 member_declared_type (const_tree member)
12159 {
12160   return (DECL_BIT_FIELD_TYPE (member)
12161           ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
12162 }
12163
12164 /* Get the decl's label, as described by its RTL. This may be different
12165    from the DECL_NAME name used in the source file.  */
12166
12167 #if 0
12168 static const char *
12169 decl_start_label (tree decl)
12170 {
12171   rtx x;
12172   const char *fnname;
12173
12174   x = DECL_RTL (decl);
12175   gcc_assert (MEM_P (x));
12176
12177   x = XEXP (x, 0);
12178   gcc_assert (GET_CODE (x) == SYMBOL_REF);
12179
12180   fnname = XSTR (x, 0);
12181   return fnname;
12182 }
12183 #endif
12184 \f
12185 /* These routines generate the internal representation of the DIE's for
12186    the compilation unit.  Debugging information is collected by walking
12187    the declaration trees passed in from dwarf2out_decl().  */
12188
12189 static void
12190 gen_array_type_die (tree type, dw_die_ref context_die)
12191 {
12192   dw_die_ref scope_die = scope_die_for (type, context_die);
12193   dw_die_ref array_die;
12194
12195   /* GNU compilers represent multidimensional array types as sequences of one
12196      dimensional array types whose element types are themselves array types.
12197      We sometimes squish that down to a single array_type DIE with multiple
12198      subscripts in the Dwarf debugging info.  The draft Dwarf specification
12199      say that we are allowed to do this kind of compression in C, because
12200      there is no difference between an array of arrays and a multidimensional
12201      array.  We don't do this for Ada to remain as close as possible to the
12202      actual representation, which is especially important against the language
12203      flexibilty wrt arrays of variable size.  */
12204
12205   bool collapse_nested_arrays = !is_ada ();
12206   tree element_type;
12207   
12208   /* ??? The SGI dwarf reader fails for array of array of enum types
12209      (e.g. const enum machine_mode insn_operand_mode[2][10]) unless the inner
12210      array type comes before the outer array type.  We thus call gen_type_die
12211      before we new_die and must prevent nested array types collapsing for this
12212      target.  */
12213
12214 #ifdef MIPS_DEBUGGING_INFO
12215   gen_type_die (TREE_TYPE (type), context_die);
12216   collapse_nested_arrays = false;
12217 #endif
12218
12219   array_die = new_die (DW_TAG_array_type, scope_die, type);
12220   add_name_attribute (array_die, type_tag (type));
12221   equate_type_number_to_die (type, array_die);
12222
12223   if (TREE_CODE (type) == VECTOR_TYPE)
12224     {
12225       /* The frontend feeds us a representation for the vector as a struct
12226          containing an array.  Pull out the array type.  */
12227       type = TREE_TYPE (TYPE_FIELDS (TYPE_DEBUG_REPRESENTATION_TYPE (type)));
12228       add_AT_flag (array_die, DW_AT_GNU_vector, 1);
12229     }
12230
12231   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
12232   if (is_fortran ()
12233       && TREE_CODE (type) == ARRAY_TYPE
12234       && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE)
12235     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
12236
12237 #if 0
12238   /* We default the array ordering.  SDB will probably do
12239      the right things even if DW_AT_ordering is not present.  It's not even
12240      an issue until we start to get into multidimensional arrays anyway.  If
12241      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
12242      then we'll have to put the DW_AT_ordering attribute back in.  (But if
12243      and when we find out that we need to put these in, we will only do so
12244      for multidimensional arrays.  */
12245   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
12246 #endif
12247
12248 #ifdef MIPS_DEBUGGING_INFO
12249   /* The SGI compilers handle arrays of unknown bound by setting
12250      AT_declaration and not emitting any subrange DIEs.  */
12251   if (! TYPE_DOMAIN (type))
12252     add_AT_flag (array_die, DW_AT_declaration, 1);
12253   else
12254 #endif
12255     add_subscript_info (array_die, type, collapse_nested_arrays);
12256
12257   /* Add representation of the type of the elements of this array type and
12258      emit the corresponding DIE if we haven't done it already.  */  
12259   element_type = TREE_TYPE (type);
12260   if (collapse_nested_arrays)
12261     while (TREE_CODE (element_type) == ARRAY_TYPE)
12262       element_type = TREE_TYPE (element_type);
12263   
12264 #ifndef MIPS_DEBUGGING_INFO
12265   gen_type_die (element_type, context_die);
12266 #endif
12267
12268   add_type_attribute (array_die, element_type, 0, 0, context_die);
12269
12270   if (get_AT (array_die, DW_AT_name))
12271     add_pubtype (type, array_die);
12272 }
12273
12274 static dw_loc_descr_ref
12275 descr_info_loc (tree val, tree base_decl)
12276 {
12277   HOST_WIDE_INT size;
12278   dw_loc_descr_ref loc, loc2;
12279   enum dwarf_location_atom op;
12280
12281   if (val == base_decl)
12282     return new_loc_descr (DW_OP_push_object_address, 0, 0);
12283
12284   switch (TREE_CODE (val))
12285     {
12286     CASE_CONVERT:
12287       return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
12288     case INTEGER_CST:
12289       if (host_integerp (val, 0))
12290         return int_loc_descriptor (tree_low_cst (val, 0));
12291       break;
12292     case INDIRECT_REF:
12293       size = int_size_in_bytes (TREE_TYPE (val));
12294       if (size < 0)
12295         break;
12296       loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
12297       if (!loc)
12298         break;
12299       if (size == DWARF2_ADDR_SIZE)
12300         add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
12301       else
12302         add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
12303       return loc;
12304     case POINTER_PLUS_EXPR:
12305     case PLUS_EXPR:
12306       if (host_integerp (TREE_OPERAND (val, 1), 1)
12307           && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
12308              < 16384)
12309         {
12310           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
12311           if (!loc)
12312             break;
12313           add_loc_descr (&loc,
12314                          new_loc_descr (DW_OP_plus_uconst,
12315                                         tree_low_cst (TREE_OPERAND (val, 1),
12316                                                       1), 0));
12317         }
12318       else
12319         {
12320           op = DW_OP_plus;
12321         do_binop:
12322           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
12323           if (!loc)
12324             break;
12325           loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
12326           if (!loc2)
12327             break;
12328           add_loc_descr (&loc, loc2);
12329           add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
12330         }
12331       return loc;
12332     case MINUS_EXPR:
12333       op = DW_OP_minus;
12334       goto do_binop;
12335     case MULT_EXPR:
12336       op = DW_OP_mul;
12337       goto do_binop;
12338     case EQ_EXPR:
12339       op = DW_OP_eq;
12340       goto do_binop;
12341     case NE_EXPR:
12342       op = DW_OP_ne;
12343       goto do_binop;
12344     default:
12345       break;
12346     }
12347   return NULL;
12348 }
12349
12350 static void
12351 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
12352                       tree val, tree base_decl)
12353 {
12354   dw_loc_descr_ref loc;
12355
12356   if (host_integerp (val, 0))
12357     {
12358       add_AT_unsigned (die, attr, tree_low_cst (val, 0));
12359       return;
12360     }
12361
12362   loc = descr_info_loc (val, base_decl);
12363   if (!loc)
12364     return;
12365
12366   add_AT_loc (die, attr, loc);
12367 }
12368
12369 /* This routine generates DIE for array with hidden descriptor, details
12370    are filled into *info by a langhook.  */
12371
12372 static void
12373 gen_descr_array_type_die (tree type, struct array_descr_info *info,
12374                           dw_die_ref context_die)
12375 {
12376   dw_die_ref scope_die = scope_die_for (type, context_die);
12377   dw_die_ref array_die;
12378   int dim;
12379
12380   array_die = new_die (DW_TAG_array_type, scope_die, type);
12381   add_name_attribute (array_die, type_tag (type));
12382   equate_type_number_to_die (type, array_die);
12383
12384   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
12385   if (is_fortran ()
12386       && info->ndimensions >= 2)
12387     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
12388
12389   if (info->data_location)
12390     add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
12391                           info->base_decl);
12392   if (info->associated)
12393     add_descr_info_field (array_die, DW_AT_associated, info->associated,
12394                           info->base_decl);
12395   if (info->allocated)
12396     add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
12397                           info->base_decl);
12398
12399   for (dim = 0; dim < info->ndimensions; dim++)
12400     {
12401       dw_die_ref subrange_die
12402         = new_die (DW_TAG_subrange_type, array_die, NULL);
12403
12404       if (info->dimen[dim].lower_bound)
12405         {
12406           /* If it is the default value, omit it.  */
12407           if ((is_c_family () || is_java ())
12408               && integer_zerop (info->dimen[dim].lower_bound))
12409             ;
12410           else if (is_fortran ()
12411                    && integer_onep (info->dimen[dim].lower_bound))
12412             ;
12413           else
12414             add_descr_info_field (subrange_die, DW_AT_lower_bound,
12415                                   info->dimen[dim].lower_bound,
12416                                   info->base_decl);
12417         }
12418       if (info->dimen[dim].upper_bound)
12419         add_descr_info_field (subrange_die, DW_AT_upper_bound,
12420                               info->dimen[dim].upper_bound,
12421                               info->base_decl);
12422       if (info->dimen[dim].stride)
12423         add_descr_info_field (subrange_die, DW_AT_byte_stride,
12424                               info->dimen[dim].stride,
12425                               info->base_decl);
12426     }
12427
12428   gen_type_die (info->element_type, context_die);
12429   add_type_attribute (array_die, info->element_type, 0, 0, context_die);
12430
12431   if (get_AT (array_die, DW_AT_name))
12432     add_pubtype (type, array_die);
12433 }
12434
12435 #if 0
12436 static void
12437 gen_entry_point_die (tree decl, dw_die_ref context_die)
12438 {
12439   tree origin = decl_ultimate_origin (decl);
12440   dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
12441
12442   if (origin != NULL)
12443     add_abstract_origin_attribute (decl_die, origin);
12444   else
12445     {
12446       add_name_and_src_coords_attributes (decl_die, decl);
12447       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
12448                           0, 0, context_die);
12449     }
12450
12451   if (DECL_ABSTRACT (decl))
12452     equate_decl_number_to_die (decl, decl_die);
12453   else
12454     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
12455 }
12456 #endif
12457
12458 /* Walk through the list of incomplete types again, trying once more to
12459    emit full debugging info for them.  */
12460
12461 static void
12462 retry_incomplete_types (void)
12463 {
12464   int i;
12465
12466   for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
12467     gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die);
12468 }
12469
12470 /* Generate a DIE to represent an inlined instance of an enumeration type.  */
12471
12472 static void
12473 gen_inlined_enumeration_type_die (tree type, dw_die_ref context_die)
12474 {
12475   dw_die_ref type_die = new_die (DW_TAG_enumeration_type, context_die, type);
12476
12477   /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
12478      be incomplete and such types are not marked.  */
12479   add_abstract_origin_attribute (type_die, type);
12480 }
12481
12482 /* Determine what tag to use for a record type.  */
12483
12484 static enum dwarf_tag
12485 record_type_tag (tree type)
12486 {
12487   if (! lang_hooks.types.classify_record)
12488     return DW_TAG_structure_type;
12489
12490   switch (lang_hooks.types.classify_record (type))
12491     {
12492     case RECORD_IS_STRUCT:
12493       return DW_TAG_structure_type;
12494
12495     case RECORD_IS_CLASS:
12496       return DW_TAG_class_type;
12497
12498     case RECORD_IS_INTERFACE:
12499       return DW_TAG_interface_type;
12500
12501     default:
12502       gcc_unreachable ();
12503     }
12504 }
12505
12506 /* Generate a DIE to represent an inlined instance of a structure type.  */
12507
12508 static void
12509 gen_inlined_structure_type_die (tree type, dw_die_ref context_die)
12510 {
12511   dw_die_ref type_die = new_die (record_type_tag (type), context_die, type);
12512
12513   /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
12514      be incomplete and such types are not marked.  */
12515   add_abstract_origin_attribute (type_die, type);
12516 }
12517
12518 /* Generate a DIE to represent an inlined instance of a union type.  */
12519
12520 static void
12521 gen_inlined_union_type_die (tree type, dw_die_ref context_die)
12522 {
12523   dw_die_ref type_die = new_die (DW_TAG_union_type, context_die, type);
12524
12525   /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
12526      be incomplete and such types are not marked.  */
12527   add_abstract_origin_attribute (type_die, type);
12528 }
12529
12530 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
12531    include all of the information about the enumeration values also. Each
12532    enumerated type name/value is listed as a child of the enumerated type
12533    DIE.  */
12534
12535 static dw_die_ref
12536 gen_enumeration_type_die (tree type, dw_die_ref context_die)
12537 {
12538   dw_die_ref type_die = lookup_type_die (type);
12539
12540   if (type_die == NULL)
12541     {
12542       type_die = new_die (DW_TAG_enumeration_type,
12543                           scope_die_for (type, context_die), type);
12544       equate_type_number_to_die (type, type_die);
12545       add_name_attribute (type_die, type_tag (type));
12546     }
12547   else if (! TYPE_SIZE (type))
12548     return type_die;
12549   else
12550     remove_AT (type_die, DW_AT_declaration);
12551
12552   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
12553      given enum type is incomplete, do not generate the DW_AT_byte_size
12554      attribute or the DW_AT_element_list attribute.  */
12555   if (TYPE_SIZE (type))
12556     {
12557       tree link;
12558
12559       TREE_ASM_WRITTEN (type) = 1;
12560       add_byte_size_attribute (type_die, type);
12561       if (TYPE_STUB_DECL (type) != NULL_TREE)
12562         add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
12563
12564       /* If the first reference to this type was as the return type of an
12565          inline function, then it may not have a parent.  Fix this now.  */
12566       if (type_die->die_parent == NULL)
12567         add_child_die (scope_die_for (type, context_die), type_die);
12568
12569       for (link = TYPE_VALUES (type);
12570            link != NULL; link = TREE_CHAIN (link))
12571         {
12572           dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
12573           tree value = TREE_VALUE (link);
12574
12575           add_name_attribute (enum_die,
12576                               IDENTIFIER_POINTER (TREE_PURPOSE (link)));
12577
12578           if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
12579             /* DWARF2 does not provide a way of indicating whether or
12580                not enumeration constants are signed or unsigned.  GDB
12581                always assumes the values are signed, so we output all
12582                values as if they were signed.  That means that
12583                enumeration constants with very large unsigned values
12584                will appear to have negative values in the debugger.  */
12585             add_AT_int (enum_die, DW_AT_const_value,
12586                         tree_low_cst (value, tree_int_cst_sgn (value) > 0));
12587         }
12588     }
12589   else
12590     add_AT_flag (type_die, DW_AT_declaration, 1);
12591
12592   if (get_AT (type_die, DW_AT_name))
12593     add_pubtype (type, type_die);
12594
12595   return type_die;
12596 }
12597
12598 /* Generate a DIE to represent either a real live formal parameter decl or to
12599    represent just the type of some formal parameter position in some function
12600    type.
12601
12602    Note that this routine is a bit unusual because its argument may be a
12603    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
12604    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
12605    node.  If it's the former then this function is being called to output a
12606    DIE to represent a formal parameter object (or some inlining thereof).  If
12607    it's the latter, then this function is only being called to output a
12608    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
12609    argument type of some subprogram type.  */
12610
12611 static dw_die_ref
12612 gen_formal_parameter_die (tree node, dw_die_ref context_die)
12613 {
12614   dw_die_ref parm_die
12615     = new_die (DW_TAG_formal_parameter, context_die, node);
12616   tree origin;
12617
12618   switch (TREE_CODE_CLASS (TREE_CODE (node)))
12619     {
12620     case tcc_declaration:
12621       origin = decl_ultimate_origin (node);
12622       if (origin != NULL)
12623         add_abstract_origin_attribute (parm_die, origin);
12624       else
12625         {
12626           tree type = TREE_TYPE (node);
12627           add_name_and_src_coords_attributes (parm_die, node);
12628           if (DECL_BY_REFERENCE (node))
12629             type = TREE_TYPE (type);
12630           add_type_attribute (parm_die, type,
12631                               TREE_READONLY (node),
12632                               TREE_THIS_VOLATILE (node),
12633                               context_die);
12634           if (DECL_ARTIFICIAL (node))
12635             add_AT_flag (parm_die, DW_AT_artificial, 1);
12636         }
12637
12638       equate_decl_number_to_die (node, parm_die);
12639       if (! DECL_ABSTRACT (node))
12640         add_location_or_const_value_attribute (parm_die, node, DW_AT_location);
12641
12642       break;
12643
12644     case tcc_type:
12645       /* We were called with some kind of a ..._TYPE node.  */
12646       add_type_attribute (parm_die, node, 0, 0, context_die);
12647       break;
12648
12649     default:
12650       gcc_unreachable ();
12651     }
12652
12653   return parm_die;
12654 }
12655
12656 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
12657    at the end of an (ANSI prototyped) formal parameters list.  */
12658
12659 static void
12660 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
12661 {
12662   new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
12663 }
12664
12665 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
12666    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
12667    parameters as specified in some function type specification (except for
12668    those which appear as part of a function *definition*).  */
12669
12670 static void
12671 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
12672 {
12673   tree link;
12674   tree formal_type = NULL;
12675   tree first_parm_type;
12676   tree arg;
12677
12678   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
12679     {
12680       arg = DECL_ARGUMENTS (function_or_method_type);
12681       function_or_method_type = TREE_TYPE (function_or_method_type);
12682     }
12683   else
12684     arg = NULL_TREE;
12685
12686   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
12687
12688   /* Make our first pass over the list of formal parameter types and output a
12689      DW_TAG_formal_parameter DIE for each one.  */
12690   for (link = first_parm_type; link; )
12691     {
12692       dw_die_ref parm_die;
12693
12694       formal_type = TREE_VALUE (link);
12695       if (formal_type == void_type_node)
12696         break;
12697
12698       /* Output a (nameless) DIE to represent the formal parameter itself.  */
12699       parm_die = gen_formal_parameter_die (formal_type, context_die);
12700       if ((TREE_CODE (function_or_method_type) == METHOD_TYPE
12701            && link == first_parm_type)
12702           || (arg && DECL_ARTIFICIAL (arg)))
12703         add_AT_flag (parm_die, DW_AT_artificial, 1);
12704
12705       link = TREE_CHAIN (link);
12706       if (arg)
12707         arg = TREE_CHAIN (arg);
12708     }
12709
12710   /* If this function type has an ellipsis, add a
12711      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
12712   if (formal_type != void_type_node)
12713     gen_unspecified_parameters_die (function_or_method_type, context_die);
12714
12715   /* Make our second (and final) pass over the list of formal parameter types
12716      and output DIEs to represent those types (as necessary).  */
12717   for (link = TYPE_ARG_TYPES (function_or_method_type);
12718        link && TREE_VALUE (link);
12719        link = TREE_CHAIN (link))
12720     gen_type_die (TREE_VALUE (link), context_die);
12721 }
12722
12723 /* We want to generate the DIE for TYPE so that we can generate the
12724    die for MEMBER, which has been defined; we will need to refer back
12725    to the member declaration nested within TYPE.  If we're trying to
12726    generate minimal debug info for TYPE, processing TYPE won't do the
12727    trick; we need to attach the member declaration by hand.  */
12728
12729 static void
12730 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
12731 {
12732   gen_type_die (type, context_die);
12733
12734   /* If we're trying to avoid duplicate debug info, we may not have
12735      emitted the member decl for this function.  Emit it now.  */
12736   if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
12737       && ! lookup_decl_die (member))
12738     {
12739       dw_die_ref type_die;
12740       gcc_assert (!decl_ultimate_origin (member));
12741
12742       push_decl_scope (type);
12743       type_die = lookup_type_die (type);
12744       if (TREE_CODE (member) == FUNCTION_DECL)
12745         gen_subprogram_die (member, type_die);
12746       else if (TREE_CODE (member) == FIELD_DECL)
12747         {
12748           /* Ignore the nameless fields that are used to skip bits but handle
12749              C++ anonymous unions and structs.  */
12750           if (DECL_NAME (member) != NULL_TREE
12751               || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
12752               || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
12753             {
12754               gen_type_die (member_declared_type (member), type_die);
12755               gen_field_die (member, type_die);
12756             }
12757         }
12758       else
12759         gen_variable_die (member, type_die);
12760
12761       pop_decl_scope ();
12762     }
12763 }
12764
12765 /* Generate the DWARF2 info for the "abstract" instance of a function which we
12766    may later generate inlined and/or out-of-line instances of.  */
12767
12768 static void
12769 dwarf2out_abstract_function (tree decl)
12770 {
12771   dw_die_ref old_die;
12772   tree save_fn;
12773   tree context;
12774   int was_abstract = DECL_ABSTRACT (decl);
12775
12776   /* Make sure we have the actual abstract inline, not a clone.  */
12777   decl = DECL_ORIGIN (decl);
12778
12779   old_die = lookup_decl_die (decl);
12780   if (old_die && get_AT (old_die, DW_AT_inline))
12781     /* We've already generated the abstract instance.  */
12782     return;
12783
12784   /* Be sure we've emitted the in-class declaration DIE (if any) first, so
12785      we don't get confused by DECL_ABSTRACT.  */
12786   if (debug_info_level > DINFO_LEVEL_TERSE)
12787     {
12788       context = decl_class_context (decl);
12789       if (context)
12790         gen_type_die_for_member
12791           (context, decl, decl_function_context (decl) ? NULL : comp_unit_die);
12792     }
12793
12794   /* Pretend we've just finished compiling this function.  */
12795   save_fn = current_function_decl;
12796   current_function_decl = decl;
12797   push_cfun (DECL_STRUCT_FUNCTION (decl));
12798
12799   set_decl_abstract_flags (decl, 1);
12800   dwarf2out_decl (decl);
12801   if (! was_abstract)
12802     set_decl_abstract_flags (decl, 0);
12803
12804   current_function_decl = save_fn;
12805   pop_cfun ();
12806 }
12807
12808 /* Helper function of premark_used_types() which gets called through
12809    htab_traverse_resize().
12810
12811    Marks the DIE of a given type in *SLOT as perennial, so it never gets
12812    marked as unused by prune_unused_types.  */
12813 static int
12814 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
12815 {
12816   tree type;
12817   dw_die_ref die;
12818
12819   type = (tree) *slot;
12820   die = lookup_type_die (type);
12821   if (die != NULL)
12822     die->die_perennial_p = 1;
12823   return 1;
12824 }
12825
12826 /* Mark all members of used_types_hash as perennial.  */
12827 static void
12828 premark_used_types (void)
12829 {
12830   if (cfun && cfun->used_types_hash)
12831     htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
12832 }
12833
12834 /* Generate a DIE to represent a declared function (either file-scope or
12835    block-local).  */
12836
12837 static void
12838 gen_subprogram_die (tree decl, dw_die_ref context_die)
12839 {
12840   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
12841   tree origin = decl_ultimate_origin (decl);
12842   dw_die_ref subr_die;
12843   tree fn_arg_types;
12844   tree outer_scope;
12845   dw_die_ref old_die = lookup_decl_die (decl);
12846   int declaration = (current_function_decl != decl
12847                      || class_or_namespace_scope_p (context_die));
12848
12849   premark_used_types ();
12850
12851   /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
12852      started to generate the abstract instance of an inline, decided to output
12853      its containing class, and proceeded to emit the declaration of the inline
12854      from the member list for the class.  If so, DECLARATION takes priority;
12855      we'll get back to the abstract instance when done with the class.  */
12856
12857   /* The class-scope declaration DIE must be the primary DIE.  */
12858   if (origin && declaration && class_or_namespace_scope_p (context_die))
12859     {
12860       origin = NULL;
12861       gcc_assert (!old_die);
12862     }
12863
12864   /* Now that the C++ front end lazily declares artificial member fns, we
12865      might need to retrofit the declaration into its class.  */
12866   if (!declaration && !origin && !old_die
12867       && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
12868       && !class_or_namespace_scope_p (context_die)
12869       && debug_info_level > DINFO_LEVEL_TERSE)
12870     old_die = force_decl_die (decl);
12871
12872   if (origin != NULL)
12873     {
12874       gcc_assert (!declaration || local_scope_p (context_die));
12875
12876       /* Fixup die_parent for the abstract instance of a nested
12877          inline function.  */
12878       if (old_die && old_die->die_parent == NULL)
12879         add_child_die (context_die, old_die);
12880
12881       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
12882       add_abstract_origin_attribute (subr_die, origin);
12883     }
12884   else if (old_die)
12885     {
12886       expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
12887       struct dwarf_file_data * file_index = lookup_filename (s.file);
12888
12889       if (!get_AT_flag (old_die, DW_AT_declaration)
12890           /* We can have a normal definition following an inline one in the
12891              case of redefinition of GNU C extern inlines.
12892              It seems reasonable to use AT_specification in this case.  */
12893           && !get_AT (old_die, DW_AT_inline))
12894         {
12895           /* Detect and ignore this case, where we are trying to output
12896              something we have already output.  */
12897           return;
12898         }
12899
12900       /* If the definition comes from the same place as the declaration,
12901          maybe use the old DIE.  We always want the DIE for this function
12902          that has the *_pc attributes to be under comp_unit_die so the
12903          debugger can find it.  We also need to do this for abstract
12904          instances of inlines, since the spec requires the out-of-line copy
12905          to have the same parent.  For local class methods, this doesn't
12906          apply; we just use the old DIE.  */
12907       if ((old_die->die_parent == comp_unit_die || context_die == NULL)
12908           && (DECL_ARTIFICIAL (decl)
12909               || (get_AT_file (old_die, DW_AT_decl_file) == file_index
12910                   && (get_AT_unsigned (old_die, DW_AT_decl_line)
12911                       == (unsigned) s.line))))
12912         {
12913           subr_die = old_die;
12914
12915           /* Clear out the declaration attribute and the formal parameters.
12916              Do not remove all children, because it is possible that this
12917              declaration die was forced using force_decl_die(). In such
12918              cases die that forced declaration die (e.g. TAG_imported_module)
12919              is one of the children that we do not want to remove.  */
12920           remove_AT (subr_die, DW_AT_declaration);
12921           remove_child_TAG (subr_die, DW_TAG_formal_parameter);
12922         }
12923       else
12924         {
12925           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
12926           add_AT_specification (subr_die, old_die);
12927           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
12928             add_AT_file (subr_die, DW_AT_decl_file, file_index);
12929           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
12930             add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
12931         }
12932     }
12933   else
12934     {
12935       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
12936
12937       if (TREE_PUBLIC (decl))
12938         add_AT_flag (subr_die, DW_AT_external, 1);
12939
12940       add_name_and_src_coords_attributes (subr_die, decl);
12941       if (debug_info_level > DINFO_LEVEL_TERSE)
12942         {
12943           add_prototyped_attribute (subr_die, TREE_TYPE (decl));
12944           add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
12945                               0, 0, context_die);
12946         }
12947
12948       add_pure_or_virtual_attribute (subr_die, decl);
12949       if (DECL_ARTIFICIAL (decl))
12950         add_AT_flag (subr_die, DW_AT_artificial, 1);
12951
12952       if (TREE_PROTECTED (decl))
12953         add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_protected);
12954       else if (TREE_PRIVATE (decl))
12955         add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_private);
12956     }
12957
12958   if (declaration)
12959     {
12960       if (!old_die || !get_AT (old_die, DW_AT_inline))
12961         {
12962           add_AT_flag (subr_die, DW_AT_declaration, 1);
12963
12964           /* The first time we see a member function, it is in the context of
12965              the class to which it belongs.  We make sure of this by emitting
12966              the class first.  The next time is the definition, which is
12967              handled above.  The two may come from the same source text.
12968
12969              Note that force_decl_die() forces function declaration die. It is
12970              later reused to represent definition.  */
12971           equate_decl_number_to_die (decl, subr_die);
12972         }
12973     }
12974   else if (DECL_ABSTRACT (decl))
12975     {
12976       if (DECL_DECLARED_INLINE_P (decl))
12977         {
12978           if (cgraph_function_possibly_inlined_p (decl))
12979             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
12980           else
12981             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
12982         }
12983       else
12984         {
12985           if (cgraph_function_possibly_inlined_p (decl))
12986             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
12987           else
12988             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
12989         }
12990
12991       if (DECL_DECLARED_INLINE_P (decl)
12992           && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
12993         add_AT_flag (subr_die, DW_AT_artificial, 1);
12994
12995       equate_decl_number_to_die (decl, subr_die);
12996     }
12997   else if (!DECL_EXTERNAL (decl))
12998     {
12999       HOST_WIDE_INT cfa_fb_offset;
13000
13001       if (!old_die || !get_AT (old_die, DW_AT_inline))
13002         equate_decl_number_to_die (decl, subr_die);
13003
13004       if (!flag_reorder_blocks_and_partition)
13005         {
13006           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
13007                                        current_function_funcdef_no);
13008           add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
13009           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
13010                                        current_function_funcdef_no);
13011           add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
13012
13013           add_pubname (decl, subr_die);
13014           add_arange (decl, subr_die);
13015         }
13016       else
13017         {  /* Do nothing for now; maybe need to duplicate die, one for
13018               hot section and one for cold section, then use the hot/cold
13019               section begin/end labels to generate the aranges...  */
13020           /*
13021             add_AT_lbl_id (subr_die, DW_AT_low_pc, hot_section_label);
13022             add_AT_lbl_id (subr_die, DW_AT_high_pc, hot_section_end_label);
13023             add_AT_lbl_id (subr_die, DW_AT_lo_user, unlikely_section_label);
13024             add_AT_lbl_id (subr_die, DW_AT_hi_user, cold_section_end_label);
13025
13026             add_pubname (decl, subr_die);
13027             add_arange (decl, subr_die);
13028             add_arange (decl, subr_die);
13029            */
13030         }
13031
13032 #ifdef MIPS_DEBUGGING_INFO
13033       /* Add a reference to the FDE for this routine.  */
13034       add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
13035 #endif
13036
13037       cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
13038
13039       /* We define the "frame base" as the function's CFA.  This is more
13040          convenient for several reasons: (1) It's stable across the prologue
13041          and epilogue, which makes it better than just a frame pointer,
13042          (2) With dwarf3, there exists a one-byte encoding that allows us
13043          to reference the .debug_frame data by proxy, but failing that,
13044          (3) We can at least reuse the code inspection and interpretation
13045          code that determines the CFA position at various points in the
13046          function.  */
13047       /* ??? Use some command-line or configury switch to enable the use
13048          of dwarf3 DW_OP_call_frame_cfa.  At present there are no dwarf
13049          consumers that understand it; fall back to "pure" dwarf2 and
13050          convert the CFA data into a location list.  */
13051       {
13052         dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
13053         if (list->dw_loc_next)
13054           add_AT_loc_list (subr_die, DW_AT_frame_base, list);
13055         else
13056           add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
13057       }
13058
13059       /* Compute a displacement from the "steady-state frame pointer" to
13060          the CFA.  The former is what all stack slots and argument slots
13061          will reference in the rtl; the later is what we've told the
13062          debugger about.  We'll need to adjust all frame_base references
13063          by this displacement.  */
13064       compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
13065
13066       if (cfun->static_chain_decl)
13067         add_AT_location_description (subr_die, DW_AT_static_link,
13068                  loc_descriptor_from_tree (cfun->static_chain_decl));
13069     }
13070
13071   /* Now output descriptions of the arguments for this function. This gets
13072      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
13073      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
13074      `...' at the end of the formal parameter list.  In order to find out if
13075      there was a trailing ellipsis or not, we must instead look at the type
13076      associated with the FUNCTION_DECL.  This will be a node of type
13077      FUNCTION_TYPE. If the chain of type nodes hanging off of this
13078      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
13079      an ellipsis at the end.  */
13080
13081   /* In the case where we are describing a mere function declaration, all we
13082      need to do here (and all we *can* do here) is to describe the *types* of
13083      its formal parameters.  */
13084   if (debug_info_level <= DINFO_LEVEL_TERSE)
13085     ;
13086   else if (declaration)
13087     gen_formal_types_die (decl, subr_die);
13088   else
13089     {
13090       /* Generate DIEs to represent all known formal parameters.  */
13091       tree arg_decls = DECL_ARGUMENTS (decl);
13092       tree parm;
13093
13094       /* When generating DIEs, generate the unspecified_parameters DIE
13095          instead if we come across the arg "__builtin_va_alist" */
13096       for (parm = arg_decls; parm; parm = TREE_CHAIN (parm))
13097         if (TREE_CODE (parm) == PARM_DECL)
13098           {
13099             if (DECL_NAME (parm)
13100                 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (parm)),
13101                             "__builtin_va_alist"))
13102               gen_unspecified_parameters_die (parm, subr_die);
13103             else
13104               gen_decl_die (parm, subr_die);
13105           }
13106
13107       /* Decide whether we need an unspecified_parameters DIE at the end.
13108          There are 2 more cases to do this for: 1) the ansi ... declaration -
13109          this is detectable when the end of the arg list is not a
13110          void_type_node 2) an unprototyped function declaration (not a
13111          definition).  This just means that we have no info about the
13112          parameters at all.  */
13113       fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
13114       if (fn_arg_types != NULL)
13115         {
13116           /* This is the prototyped case, check for....  */
13117           if (TREE_VALUE (tree_last (fn_arg_types)) != void_type_node)
13118             gen_unspecified_parameters_die (decl, subr_die);
13119         }
13120       else if (DECL_INITIAL (decl) == NULL_TREE)
13121         gen_unspecified_parameters_die (decl, subr_die);
13122     }
13123
13124   /* Output Dwarf info for all of the stuff within the body of the function
13125      (if it has one - it may be just a declaration).  */
13126   outer_scope = DECL_INITIAL (decl);
13127
13128   /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
13129      a function.  This BLOCK actually represents the outermost binding contour
13130      for the function, i.e. the contour in which the function's formal
13131      parameters and labels get declared. Curiously, it appears that the front
13132      end doesn't actually put the PARM_DECL nodes for the current function onto
13133      the BLOCK_VARS list for this outer scope, but are strung off of the
13134      DECL_ARGUMENTS list for the function instead.
13135
13136      The BLOCK_VARS list for the `outer_scope' does provide us with a list of
13137      the LABEL_DECL nodes for the function however, and we output DWARF info
13138      for those in decls_for_scope.  Just within the `outer_scope' there will be
13139      a BLOCK node representing the function's outermost pair of curly braces,
13140      and any blocks used for the base and member initializers of a C++
13141      constructor function.  */
13142   if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
13143     {
13144       /* Emit a DW_TAG_variable DIE for a named return value.  */
13145       if (DECL_NAME (DECL_RESULT (decl)))
13146         gen_decl_die (DECL_RESULT (decl), subr_die);
13147
13148       current_function_has_inlines = 0;
13149       decls_for_scope (outer_scope, subr_die, 0);
13150
13151 #if 0 && defined (MIPS_DEBUGGING_INFO)
13152       if (current_function_has_inlines)
13153         {
13154           add_AT_flag (subr_die, DW_AT_MIPS_has_inlines, 1);
13155           if (! comp_unit_has_inlines)
13156             {
13157               add_AT_flag (comp_unit_die, DW_AT_MIPS_has_inlines, 1);
13158               comp_unit_has_inlines = 1;
13159             }
13160         }
13161 #endif
13162     }
13163   /* Add the calling convention attribute if requested.  */
13164   add_calling_convention_attribute (subr_die, decl);
13165
13166 }
13167
13168 /* Generate a DIE to represent a declared data object.  */
13169
13170 static void
13171 gen_variable_die (tree decl, dw_die_ref context_die)
13172 {
13173   HOST_WIDE_INT off;
13174   tree com_decl;
13175   dw_die_ref var_die;
13176   tree origin = decl_ultimate_origin (decl);
13177   dw_die_ref old_die = lookup_decl_die (decl);
13178   int declaration = (DECL_EXTERNAL (decl)
13179                      /* If DECL is COMDAT and has not actually been
13180                         emitted, we cannot take its address; there
13181                         might end up being no definition anywhere in
13182                         the program.  For example, consider the C++
13183                         test case:
13184
13185                           template <class T>
13186                           struct S { static const int i = 7; };
13187
13188                           template <class T>
13189                           const int S<T>::i;
13190
13191                           int f() { return S<int>::i; }
13192
13193                         Here, S<int>::i is not DECL_EXTERNAL, but no
13194                         definition is required, so the compiler will
13195                         not emit a definition.  */
13196                      || (TREE_CODE (decl) == VAR_DECL
13197                          && DECL_COMDAT (decl) && !TREE_ASM_WRITTEN (decl))
13198                      || class_or_namespace_scope_p (context_die));
13199
13200   com_decl = fortran_common (decl, &off);
13201
13202   /* Symbol in common gets emitted as a child of the common block, in the form
13203      of a data member.
13204
13205      ??? This creates a new common block die for every common block symbol.
13206      Better to share same common block die for all symbols in that block.  */
13207   if (com_decl)
13208     {
13209       tree field;
13210       dw_die_ref com_die;
13211       const char *cnam = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
13212       dw_loc_descr_ref loc = loc_descriptor_from_tree (com_decl);
13213
13214       field = TREE_OPERAND (DECL_VALUE_EXPR (decl), 0);
13215       var_die = new_die (DW_TAG_common_block, context_die, decl);
13216       add_name_and_src_coords_attributes (var_die, field);
13217       add_AT_flag (var_die, DW_AT_external, 1);
13218       add_AT_loc (var_die, DW_AT_location, loc);
13219       com_die = new_die (DW_TAG_member, var_die, decl);
13220       add_name_and_src_coords_attributes (com_die, decl);
13221       add_type_attribute (com_die, TREE_TYPE (decl), TREE_READONLY (decl),
13222                           TREE_THIS_VOLATILE (decl), context_die);
13223       add_AT_loc (com_die, DW_AT_data_member_location,
13224                   int_loc_descriptor (off));
13225       add_pubname_string (cnam, var_die); /* ??? needed? */
13226       return;
13227     }
13228
13229   var_die = new_die (DW_TAG_variable, context_die, decl);
13230
13231   if (origin != NULL)
13232     add_abstract_origin_attribute (var_die, origin);
13233
13234   /* Loop unrolling can create multiple blocks that refer to the same
13235      static variable, so we must test for the DW_AT_declaration flag.
13236
13237      ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
13238      copy decls and set the DECL_ABSTRACT flag on them instead of
13239      sharing them.
13240
13241      ??? Duplicated blocks have been rewritten to use .debug_ranges.
13242
13243      ??? The declare_in_namespace support causes us to get two DIEs for one
13244      variable, both of which are declarations.  We want to avoid considering
13245      one to be a specification, so we must test that this DIE is not a
13246      declaration.  */
13247   else if (old_die && TREE_STATIC (decl) && ! declaration
13248            && get_AT_flag (old_die, DW_AT_declaration) == 1)
13249     {
13250       /* This is a definition of a C++ class level static.  */
13251       add_AT_specification (var_die, old_die);
13252       if (DECL_NAME (decl))
13253         {
13254           expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
13255           struct dwarf_file_data * file_index = lookup_filename (s.file);
13256
13257           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
13258             add_AT_file (var_die, DW_AT_decl_file, file_index);
13259
13260           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
13261             add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
13262         }
13263     }
13264   else
13265     {
13266       tree type = TREE_TYPE (decl);
13267       if ((TREE_CODE (decl) == PARM_DECL
13268            || TREE_CODE (decl) == RESULT_DECL)
13269           && DECL_BY_REFERENCE (decl))
13270         type = TREE_TYPE (type);
13271
13272       add_name_and_src_coords_attributes (var_die, decl);
13273       add_type_attribute (var_die, type, TREE_READONLY (decl),
13274                           TREE_THIS_VOLATILE (decl), context_die);
13275
13276       if (TREE_PUBLIC (decl))
13277         add_AT_flag (var_die, DW_AT_external, 1);
13278
13279       if (DECL_ARTIFICIAL (decl))
13280         add_AT_flag (var_die, DW_AT_artificial, 1);
13281
13282       if (TREE_PROTECTED (decl))
13283         add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_protected);
13284       else if (TREE_PRIVATE (decl))
13285         add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_private);
13286     }
13287
13288   if (declaration)
13289     add_AT_flag (var_die, DW_AT_declaration, 1);
13290
13291   if (DECL_ABSTRACT (decl) || declaration)
13292     equate_decl_number_to_die (decl, var_die);
13293
13294   if (! declaration && ! DECL_ABSTRACT (decl))
13295     {
13296       add_location_or_const_value_attribute (var_die, decl, DW_AT_location);
13297       add_pubname (decl, var_die);
13298     }
13299   else
13300     tree_add_const_value_attribute (var_die, decl);
13301 }
13302
13303 /* Generate a DIE to represent a label identifier.  */
13304
13305 static void
13306 gen_label_die (tree decl, dw_die_ref context_die)
13307 {
13308   tree origin = decl_ultimate_origin (decl);
13309   dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
13310   rtx insn;
13311   char label[MAX_ARTIFICIAL_LABEL_BYTES];
13312
13313   if (origin != NULL)
13314     add_abstract_origin_attribute (lbl_die, origin);
13315   else
13316     add_name_and_src_coords_attributes (lbl_die, decl);
13317
13318   if (DECL_ABSTRACT (decl))
13319     equate_decl_number_to_die (decl, lbl_die);
13320   else
13321     {
13322       insn = DECL_RTL_IF_SET (decl);
13323
13324       /* Deleted labels are programmer specified labels which have been
13325          eliminated because of various optimizations.  We still emit them
13326          here so that it is possible to put breakpoints on them.  */
13327       if (insn
13328           && (LABEL_P (insn)
13329               || ((NOTE_P (insn)
13330                    && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
13331         {
13332           /* When optimization is enabled (via -O) some parts of the compiler
13333              (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
13334              represent source-level labels which were explicitly declared by
13335              the user.  This really shouldn't be happening though, so catch
13336              it if it ever does happen.  */
13337           gcc_assert (!INSN_DELETED_P (insn));
13338
13339           ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
13340           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
13341         }
13342     }
13343 }
13344
13345 /* A helper function for gen_inlined_subroutine_die.  Add source coordinate
13346    attributes to the DIE for a block STMT, to describe where the inlined
13347    function was called from.  This is similar to add_src_coords_attributes.  */
13348
13349 static inline void
13350 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
13351 {
13352   expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
13353
13354   add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
13355   add_AT_unsigned (die, DW_AT_call_line, s.line);
13356 }
13357
13358
13359 /* If STMT's abstract origin is a function declaration and STMT's
13360    first subblock's abstract origin is the function's outermost block,
13361    then we're looking at the main entry point.  */
13362 static bool
13363 is_inlined_entry_point (const_tree stmt)
13364 {
13365   tree decl, block;
13366
13367   if (!stmt || TREE_CODE (stmt) != BLOCK)
13368     return false;
13369
13370   decl = block_ultimate_origin (stmt);
13371
13372   if (!decl || TREE_CODE (decl) != FUNCTION_DECL)
13373     return false;
13374
13375   block = BLOCK_SUBBLOCKS (stmt);
13376
13377   if (block)
13378     {
13379       if (TREE_CODE (block) != BLOCK)
13380         return false;
13381
13382       block = block_ultimate_origin (block);
13383     }
13384
13385   return block == DECL_INITIAL (decl);
13386 }
13387
13388 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
13389    Add low_pc and high_pc attributes to the DIE for a block STMT.  */
13390
13391 static inline void
13392 add_high_low_attributes (tree stmt, dw_die_ref die)
13393 {
13394   char label[MAX_ARTIFICIAL_LABEL_BYTES];
13395
13396   if (BLOCK_FRAGMENT_CHAIN (stmt))
13397     {
13398       tree chain;
13399
13400       if (is_inlined_entry_point (stmt))
13401         {
13402           ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
13403                                        BLOCK_NUMBER (stmt));
13404           add_AT_lbl_id (die, DW_AT_entry_pc, label);
13405         }
13406
13407       add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
13408
13409       chain = BLOCK_FRAGMENT_CHAIN (stmt);
13410       do
13411         {
13412           add_ranges (chain);
13413           chain = BLOCK_FRAGMENT_CHAIN (chain);
13414         }
13415       while (chain);
13416       add_ranges (NULL);
13417     }
13418   else
13419     {
13420       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
13421                                    BLOCK_NUMBER (stmt));
13422       add_AT_lbl_id (die, DW_AT_low_pc, label);
13423       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
13424                                    BLOCK_NUMBER (stmt));
13425       add_AT_lbl_id (die, DW_AT_high_pc, label);
13426     }
13427 }
13428
13429 /* Generate a DIE for a lexical block.  */
13430
13431 static void
13432 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
13433 {
13434   dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
13435
13436   if (! BLOCK_ABSTRACT (stmt))
13437     add_high_low_attributes (stmt, stmt_die);
13438
13439   decls_for_scope (stmt, stmt_die, depth);
13440 }
13441
13442 /* Generate a DIE for an inlined subprogram.  */
13443
13444 static void
13445 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
13446 {
13447   tree decl = block_ultimate_origin (stmt);
13448
13449   /* Emit info for the abstract instance first, if we haven't yet.  We
13450      must emit this even if the block is abstract, otherwise when we
13451      emit the block below (or elsewhere), we may end up trying to emit
13452      a die whose origin die hasn't been emitted, and crashing.  */
13453   dwarf2out_abstract_function (decl);
13454
13455   if (! BLOCK_ABSTRACT (stmt))
13456     {
13457       dw_die_ref subr_die
13458         = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
13459
13460       add_abstract_origin_attribute (subr_die, decl);
13461       add_high_low_attributes (stmt, subr_die);
13462       add_call_src_coords_attributes (stmt, subr_die);
13463
13464       decls_for_scope (stmt, subr_die, depth);
13465       current_function_has_inlines = 1;
13466     }
13467   else
13468     /* We may get here if we're the outer block of function A that was
13469        inlined into function B that was inlined into function C.  When
13470        generating debugging info for C, dwarf2out_abstract_function(B)
13471        would mark all inlined blocks as abstract, including this one.
13472        So, we wouldn't (and shouldn't) expect labels to be generated
13473        for this one.  Instead, just emit debugging info for
13474        declarations within the block.  This is particularly important
13475        in the case of initializers of arguments passed from B to us:
13476        if they're statement expressions containing declarations, we
13477        wouldn't generate dies for their abstract variables, and then,
13478        when generating dies for the real variables, we'd die (pun
13479        intended :-)  */
13480     gen_lexical_block_die (stmt, context_die, depth);
13481 }
13482
13483 /* Generate a DIE for a field in a record, or structure.  */
13484
13485 static void
13486 gen_field_die (tree decl, dw_die_ref context_die)
13487 {
13488   dw_die_ref decl_die;
13489
13490   if (TREE_TYPE (decl) == error_mark_node)
13491     return;
13492
13493   decl_die = new_die (DW_TAG_member, context_die, decl);
13494   add_name_and_src_coords_attributes (decl_die, decl);
13495   add_type_attribute (decl_die, member_declared_type (decl),
13496                       TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
13497                       context_die);
13498
13499   if (DECL_BIT_FIELD_TYPE (decl))
13500     {
13501       add_byte_size_attribute (decl_die, decl);
13502       add_bit_size_attribute (decl_die, decl);
13503       add_bit_offset_attribute (decl_die, decl);
13504     }
13505
13506   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
13507     add_data_member_location_attribute (decl_die, decl);
13508
13509   if (DECL_ARTIFICIAL (decl))
13510     add_AT_flag (decl_die, DW_AT_artificial, 1);
13511
13512   if (TREE_PROTECTED (decl))
13513     add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_protected);
13514   else if (TREE_PRIVATE (decl))
13515     add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_private);
13516
13517   /* Equate decl number to die, so that we can look up this decl later on.  */
13518   equate_decl_number_to_die (decl, decl_die);
13519 }
13520
13521 #if 0
13522 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
13523    Use modified_type_die instead.
13524    We keep this code here just in case these types of DIEs may be needed to
13525    represent certain things in other languages (e.g. Pascal) someday.  */
13526
13527 static void
13528 gen_pointer_type_die (tree type, dw_die_ref context_die)
13529 {
13530   dw_die_ref ptr_die
13531     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
13532
13533   equate_type_number_to_die (type, ptr_die);
13534   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
13535   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
13536 }
13537
13538 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
13539    Use modified_type_die instead.
13540    We keep this code here just in case these types of DIEs may be needed to
13541    represent certain things in other languages (e.g. Pascal) someday.  */
13542
13543 static void
13544 gen_reference_type_die (tree type, dw_die_ref context_die)
13545 {
13546   dw_die_ref ref_die
13547     = new_die (DW_TAG_reference_type, scope_die_for (type, context_die), type);
13548
13549   equate_type_number_to_die (type, ref_die);
13550   add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
13551   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
13552 }
13553 #endif
13554
13555 /* Generate a DIE for a pointer to a member type.  */
13556
13557 static void
13558 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
13559 {
13560   dw_die_ref ptr_die
13561     = new_die (DW_TAG_ptr_to_member_type,
13562                scope_die_for (type, context_die), type);
13563
13564   equate_type_number_to_die (type, ptr_die);
13565   add_AT_die_ref (ptr_die, DW_AT_containing_type,
13566                   lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
13567   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
13568 }
13569
13570 /* Generate the DIE for the compilation unit.  */
13571
13572 static dw_die_ref
13573 gen_compile_unit_die (const char *filename)
13574 {
13575   dw_die_ref die;
13576   char producer[250];
13577   const char *language_string = lang_hooks.name;
13578   int language;
13579
13580   die = new_die (DW_TAG_compile_unit, NULL, NULL);
13581
13582   if (filename)
13583     {
13584       add_name_attribute (die, filename);
13585       /* Don't add cwd for <built-in>.  */
13586       if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
13587         add_comp_dir_attribute (die);
13588     }
13589
13590   sprintf (producer, "%s %s", language_string, version_string);
13591
13592 #ifdef MIPS_DEBUGGING_INFO
13593   /* The MIPS/SGI compilers place the 'cc' command line options in the producer
13594      string.  The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
13595      not appear in the producer string, the debugger reaches the conclusion
13596      that the object file is stripped and has no debugging information.
13597      To get the MIPS/SGI debugger to believe that there is debugging
13598      information in the object file, we add a -g to the producer string.  */
13599   if (debug_info_level > DINFO_LEVEL_TERSE)
13600     strcat (producer, " -g");
13601 #endif
13602
13603   add_AT_string (die, DW_AT_producer, producer);
13604
13605   if (strcmp (language_string, "GNU C++") == 0)
13606     language = DW_LANG_C_plus_plus;
13607   else if (strcmp (language_string, "GNU Ada") == 0)
13608     language = DW_LANG_Ada95;
13609   else if (strcmp (language_string, "GNU F77") == 0)
13610     language = DW_LANG_Fortran77;
13611   else if (strcmp (language_string, "GNU Fortran") == 0)
13612     language = DW_LANG_Fortran95;
13613   else if (strcmp (language_string, "GNU Pascal") == 0)
13614     language = DW_LANG_Pascal83;
13615   else if (strcmp (language_string, "GNU Java") == 0)
13616     language = DW_LANG_Java;
13617   else if (strcmp (language_string, "GNU Objective-C") == 0)
13618     language = DW_LANG_ObjC;
13619   else if (strcmp (language_string, "GNU Objective-C++") == 0)
13620     language = DW_LANG_ObjC_plus_plus;
13621   else
13622     language = DW_LANG_C89;
13623
13624   add_AT_unsigned (die, DW_AT_language, language);
13625   return die;
13626 }
13627
13628 /* Generate the DIE for a base class.  */
13629
13630 static void
13631 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
13632 {
13633   dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
13634
13635   add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
13636   add_data_member_location_attribute (die, binfo);
13637
13638   if (BINFO_VIRTUAL_P (binfo))
13639     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
13640
13641   if (access == access_public_node)
13642     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
13643   else if (access == access_protected_node)
13644     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
13645 }
13646
13647 /* Generate a DIE for a class member.  */
13648
13649 static void
13650 gen_member_die (tree type, dw_die_ref context_die)
13651 {
13652   tree member;
13653   tree binfo = TYPE_BINFO (type);
13654   dw_die_ref child;
13655
13656   /* If this is not an incomplete type, output descriptions of each of its
13657      members. Note that as we output the DIEs necessary to represent the
13658      members of this record or union type, we will also be trying to output
13659      DIEs to represent the *types* of those members. However the `type'
13660      function (above) will specifically avoid generating type DIEs for member
13661      types *within* the list of member DIEs for this (containing) type except
13662      for those types (of members) which are explicitly marked as also being
13663      members of this (containing) type themselves.  The g++ front- end can
13664      force any given type to be treated as a member of some other (containing)
13665      type by setting the TYPE_CONTEXT of the given (member) type to point to
13666      the TREE node representing the appropriate (containing) type.  */
13667
13668   /* First output info about the base classes.  */
13669   if (binfo)
13670     {
13671       VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
13672       int i;
13673       tree base;
13674
13675       for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
13676         gen_inheritance_die (base,
13677                              (accesses ? VEC_index (tree, accesses, i)
13678                               : access_public_node), context_die);
13679     }
13680
13681   /* Now output info about the data members and type members.  */
13682   for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member))
13683     {
13684       /* If we thought we were generating minimal debug info for TYPE
13685          and then changed our minds, some of the member declarations
13686          may have already been defined.  Don't define them again, but
13687          do put them in the right order.  */
13688
13689       child = lookup_decl_die (member);
13690       if (child)
13691         splice_child_die (context_die, child);
13692       else
13693         gen_decl_die (member, context_die);
13694     }
13695
13696   /* Now output info about the function members (if any).  */
13697   for (member = TYPE_METHODS (type); member; member = TREE_CHAIN (member))
13698     {
13699       /* Don't include clones in the member list.  */
13700       if (DECL_ABSTRACT_ORIGIN (member))
13701         continue;
13702
13703       child = lookup_decl_die (member);
13704       if (child)
13705         splice_child_die (context_die, child);
13706       else
13707         gen_decl_die (member, context_die);
13708     }
13709 }
13710
13711 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
13712    is set, we pretend that the type was never defined, so we only get the
13713    member DIEs needed by later specification DIEs.  */
13714
13715 static void
13716 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
13717                                 enum debug_info_usage usage)
13718 {
13719   dw_die_ref type_die = lookup_type_die (type);
13720   dw_die_ref scope_die = 0;
13721   int nested = 0;
13722   int complete = (TYPE_SIZE (type)
13723                   && (! TYPE_STUB_DECL (type)
13724                       || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
13725   int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
13726   complete = complete && should_emit_struct_debug (type, usage);
13727
13728   if (type_die && ! complete)
13729     return;
13730
13731   if (TYPE_CONTEXT (type) != NULL_TREE
13732       && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
13733           || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
13734     nested = 1;
13735
13736   scope_die = scope_die_for (type, context_die);
13737
13738   if (! type_die || (nested && scope_die == comp_unit_die))
13739     /* First occurrence of type or toplevel definition of nested class.  */
13740     {
13741       dw_die_ref old_die = type_die;
13742
13743       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
13744                           ? record_type_tag (type) : DW_TAG_union_type,
13745                           scope_die, type);
13746       equate_type_number_to_die (type, type_die);
13747       if (old_die)
13748         add_AT_specification (type_die, old_die);
13749       else
13750         add_name_attribute (type_die, type_tag (type));
13751     }
13752   else
13753     remove_AT (type_die, DW_AT_declaration);
13754
13755   /* If this type has been completed, then give it a byte_size attribute and
13756      then give a list of members.  */
13757   if (complete && !ns_decl)
13758     {
13759       /* Prevent infinite recursion in cases where the type of some member of
13760          this type is expressed in terms of this type itself.  */
13761       TREE_ASM_WRITTEN (type) = 1;
13762       add_byte_size_attribute (type_die, type);
13763       if (TYPE_STUB_DECL (type) != NULL_TREE)
13764         add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
13765
13766       /* If the first reference to this type was as the return type of an
13767          inline function, then it may not have a parent.  Fix this now.  */
13768       if (type_die->die_parent == NULL)
13769         add_child_die (scope_die, type_die);
13770
13771       push_decl_scope (type);
13772       gen_member_die (type, type_die);
13773       pop_decl_scope ();
13774
13775       /* GNU extension: Record what type our vtable lives in.  */
13776       if (TYPE_VFIELD (type))
13777         {
13778           tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
13779
13780           gen_type_die (vtype, context_die);
13781           add_AT_die_ref (type_die, DW_AT_containing_type,
13782                           lookup_type_die (vtype));
13783         }
13784     }
13785   else
13786     {
13787       add_AT_flag (type_die, DW_AT_declaration, 1);
13788
13789       /* We don't need to do this for function-local types.  */
13790       if (TYPE_STUB_DECL (type)
13791           && ! decl_function_context (TYPE_STUB_DECL (type)))
13792         VEC_safe_push (tree, gc, incomplete_types, type);
13793     }
13794
13795   if (get_AT (type_die, DW_AT_name))
13796     add_pubtype (type, type_die);
13797 }
13798
13799 /* Generate a DIE for a subroutine _type_.  */
13800
13801 static void
13802 gen_subroutine_type_die (tree type, dw_die_ref context_die)
13803 {
13804   tree return_type = TREE_TYPE (type);
13805   dw_die_ref subr_die
13806     = new_die (DW_TAG_subroutine_type,
13807                scope_die_for (type, context_die), type);
13808
13809   equate_type_number_to_die (type, subr_die);
13810   add_prototyped_attribute (subr_die, type);
13811   add_type_attribute (subr_die, return_type, 0, 0, context_die);
13812   gen_formal_types_die (type, subr_die);
13813
13814   if (get_AT (subr_die, DW_AT_name))
13815     add_pubtype (type, subr_die);
13816 }
13817
13818 /* Generate a DIE for a type definition.  */
13819
13820 static void
13821 gen_typedef_die (tree decl, dw_die_ref context_die)
13822 {
13823   dw_die_ref type_die;
13824   tree origin;
13825
13826   if (TREE_ASM_WRITTEN (decl))
13827     return;
13828
13829   TREE_ASM_WRITTEN (decl) = 1;
13830   type_die = new_die (DW_TAG_typedef, context_die, decl);
13831   origin = decl_ultimate_origin (decl);
13832   if (origin != NULL)
13833     add_abstract_origin_attribute (type_die, origin);
13834   else
13835     {
13836       tree type;
13837
13838       add_name_and_src_coords_attributes (type_die, decl);
13839       if (DECL_ORIGINAL_TYPE (decl))
13840         {
13841           type = DECL_ORIGINAL_TYPE (decl);
13842
13843           gcc_assert (type != TREE_TYPE (decl));
13844           equate_type_number_to_die (TREE_TYPE (decl), type_die);
13845         }
13846       else
13847         type = TREE_TYPE (decl);
13848
13849       add_type_attribute (type_die, type, TREE_READONLY (decl),
13850                           TREE_THIS_VOLATILE (decl), context_die);
13851     }
13852
13853   if (DECL_ABSTRACT (decl))
13854     equate_decl_number_to_die (decl, type_die);
13855
13856   if (get_AT (type_die, DW_AT_name))
13857     add_pubtype (decl, type_die);
13858 }
13859
13860 /* Generate a type description DIE.  */
13861
13862 static void
13863 gen_type_die_with_usage (tree type, dw_die_ref context_die,
13864                                 enum debug_info_usage usage)
13865 {
13866   int need_pop;
13867   struct array_descr_info info;
13868
13869   if (type == NULL_TREE || type == error_mark_node)
13870     return;
13871
13872   if (TYPE_NAME (type) && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
13873       && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
13874     {
13875       if (TREE_ASM_WRITTEN (type))
13876         return;
13877
13878       /* Prevent broken recursion; we can't hand off to the same type.  */
13879       gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
13880
13881       TREE_ASM_WRITTEN (type) = 1;
13882       gen_decl_die (TYPE_NAME (type), context_die);
13883       return;
13884     }
13885
13886   /* If this is an array type with hidden descriptor, handle it first.  */
13887   if (!TREE_ASM_WRITTEN (type)
13888       && lang_hooks.types.get_array_descr_info
13889       && lang_hooks.types.get_array_descr_info (type, &info))
13890     {
13891       gen_descr_array_type_die (type, &info, context_die);
13892       TREE_ASM_WRITTEN (type) = 1;
13893       return;
13894     }
13895
13896   /* We are going to output a DIE to represent the unqualified version
13897      of this type (i.e. without any const or volatile qualifiers) so
13898      get the main variant (i.e. the unqualified version) of this type
13899      now.  (Vectors are special because the debugging info is in the
13900      cloned type itself).  */
13901   if (TREE_CODE (type) != VECTOR_TYPE)
13902     type = type_main_variant (type);
13903
13904   if (TREE_ASM_WRITTEN (type))
13905     return;
13906
13907   switch (TREE_CODE (type))
13908     {
13909     case ERROR_MARK:
13910       break;
13911
13912     case POINTER_TYPE:
13913     case REFERENCE_TYPE:
13914       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
13915          ensures that the gen_type_die recursion will terminate even if the
13916          type is recursive.  Recursive types are possible in Ada.  */
13917       /* ??? We could perhaps do this for all types before the switch
13918          statement.  */
13919       TREE_ASM_WRITTEN (type) = 1;
13920
13921       /* For these types, all that is required is that we output a DIE (or a
13922          set of DIEs) to represent the "basis" type.  */
13923       gen_type_die_with_usage (TREE_TYPE (type), context_die,
13924                                 DINFO_USAGE_IND_USE);
13925       break;
13926
13927     case OFFSET_TYPE:
13928       /* This code is used for C++ pointer-to-data-member types.
13929          Output a description of the relevant class type.  */
13930       gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
13931                                         DINFO_USAGE_IND_USE);
13932
13933       /* Output a description of the type of the object pointed to.  */
13934       gen_type_die_with_usage (TREE_TYPE (type), context_die,
13935                                         DINFO_USAGE_IND_USE);
13936
13937       /* Now output a DIE to represent this pointer-to-data-member type
13938          itself.  */
13939       gen_ptr_to_mbr_type_die (type, context_die);
13940       break;
13941
13942     case FUNCTION_TYPE:
13943       /* Force out return type (in case it wasn't forced out already).  */
13944       gen_type_die_with_usage (TREE_TYPE (type), context_die,
13945                                         DINFO_USAGE_DIR_USE);
13946       gen_subroutine_type_die (type, context_die);
13947       break;
13948
13949     case METHOD_TYPE:
13950       /* Force out return type (in case it wasn't forced out already).  */
13951       gen_type_die_with_usage (TREE_TYPE (type), context_die,
13952                                         DINFO_USAGE_DIR_USE);
13953       gen_subroutine_type_die (type, context_die);
13954       break;
13955
13956     case ARRAY_TYPE:
13957       gen_array_type_die (type, context_die);
13958       break;
13959
13960     case VECTOR_TYPE:
13961       gen_array_type_die (type, context_die);
13962       break;
13963
13964     case ENUMERAL_TYPE:
13965     case RECORD_TYPE:
13966     case UNION_TYPE:
13967     case QUAL_UNION_TYPE:
13968       /* If this is a nested type whose containing class hasn't been written
13969          out yet, writing it out will cover this one, too.  This does not apply
13970          to instantiations of member class templates; they need to be added to
13971          the containing class as they are generated.  FIXME: This hurts the
13972          idea of combining type decls from multiple TUs, since we can't predict
13973          what set of template instantiations we'll get.  */
13974       if (TYPE_CONTEXT (type)
13975           && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
13976           && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
13977         {
13978           gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
13979
13980           if (TREE_ASM_WRITTEN (type))
13981             return;
13982
13983           /* If that failed, attach ourselves to the stub.  */
13984           push_decl_scope (TYPE_CONTEXT (type));
13985           context_die = lookup_type_die (TYPE_CONTEXT (type));
13986           need_pop = 1;
13987         }
13988       else
13989         {
13990           declare_in_namespace (type, context_die);
13991           need_pop = 0;
13992         }
13993
13994       if (TREE_CODE (type) == ENUMERAL_TYPE)
13995         {
13996           /* This might have been written out by the call to
13997              declare_in_namespace.  */
13998           if (!TREE_ASM_WRITTEN (type))
13999             gen_enumeration_type_die (type, context_die);
14000         }
14001       else
14002         gen_struct_or_union_type_die (type, context_die, usage);
14003
14004       if (need_pop)
14005         pop_decl_scope ();
14006
14007       /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
14008          it up if it is ever completed.  gen_*_type_die will set it for us
14009          when appropriate.  */
14010       return;
14011
14012     case VOID_TYPE:
14013     case INTEGER_TYPE:
14014     case REAL_TYPE:
14015     case FIXED_POINT_TYPE:
14016     case COMPLEX_TYPE:
14017     case BOOLEAN_TYPE:
14018       /* No DIEs needed for fundamental types.  */
14019       break;
14020
14021     case LANG_TYPE:
14022       /* No Dwarf representation currently defined.  */
14023       break;
14024
14025     default:
14026       gcc_unreachable ();
14027     }
14028
14029   TREE_ASM_WRITTEN (type) = 1;
14030 }
14031
14032 static void
14033 gen_type_die (tree type, dw_die_ref context_die)
14034 {
14035   gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
14036 }
14037
14038 /* Generate a DIE for a tagged type instantiation.  */
14039
14040 static void
14041 gen_tagged_type_instantiation_die (tree type, dw_die_ref context_die)
14042 {
14043   if (type == NULL_TREE || type == error_mark_node)
14044     return;
14045
14046   /* We are going to output a DIE to represent the unqualified version of
14047      this type (i.e. without any const or volatile qualifiers) so make sure
14048      that we have the main variant (i.e. the unqualified version) of this
14049      type now.  */
14050   gcc_assert (type == type_main_variant (type));
14051
14052   /* Do not check TREE_ASM_WRITTEN (type) as it may not be set if this is
14053      an instance of an unresolved type.  */
14054
14055   switch (TREE_CODE (type))
14056     {
14057     case ERROR_MARK:
14058       break;
14059
14060     case ENUMERAL_TYPE:
14061       gen_inlined_enumeration_type_die (type, context_die);
14062       break;
14063
14064     case RECORD_TYPE:
14065       gen_inlined_structure_type_die (type, context_die);
14066       break;
14067
14068     case UNION_TYPE:
14069     case QUAL_UNION_TYPE:
14070       gen_inlined_union_type_die (type, context_die);
14071       break;
14072
14073     default:
14074       gcc_unreachable ();
14075     }
14076 }
14077
14078 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
14079    things which are local to the given block.  */
14080
14081 static void
14082 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
14083 {
14084   int must_output_die = 0;
14085   tree origin;
14086   tree decl;
14087   enum tree_code origin_code;
14088
14089   /* Ignore blocks that are NULL.  */
14090   if (stmt == NULL_TREE)
14091     return;
14092
14093   /* If the block is one fragment of a non-contiguous block, do not
14094      process the variables, since they will have been done by the
14095      origin block.  Do process subblocks.  */
14096   if (BLOCK_FRAGMENT_ORIGIN (stmt))
14097     {
14098       tree sub;
14099
14100       for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
14101         gen_block_die (sub, context_die, depth + 1);
14102
14103       return;
14104     }
14105
14106   /* Determine the "ultimate origin" of this block.  This block may be an
14107      inlined instance of an inlined instance of inline function, so we have
14108      to trace all of the way back through the origin chain to find out what
14109      sort of node actually served as the original seed for the creation of
14110      the current block.  */
14111   origin = block_ultimate_origin (stmt);
14112   origin_code = (origin != NULL) ? TREE_CODE (origin) : ERROR_MARK;
14113
14114   /* Determine if we need to output any Dwarf DIEs at all to represent this
14115      block.  */
14116   if (origin_code == FUNCTION_DECL)
14117     /* The outer scopes for inlinings *must* always be represented.  We
14118        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
14119     must_output_die = 1;
14120   else
14121     {
14122       /* In the case where the current block represents an inlining of the
14123          "body block" of an inline function, we must *NOT* output any DIE for
14124          this block because we have already output a DIE to represent the whole
14125          inlined function scope and the "body block" of any function doesn't
14126          really represent a different scope according to ANSI C rules.  So we
14127          check here to make sure that this block does not represent a "body
14128          block inlining" before trying to set the MUST_OUTPUT_DIE flag.  */
14129       if (! is_body_block (origin ? origin : stmt))
14130         {
14131           /* Determine if this block directly contains any "significant"
14132              local declarations which we will need to output DIEs for.  */
14133           if (debug_info_level > DINFO_LEVEL_TERSE)
14134             /* We are not in terse mode so *any* local declaration counts
14135                as being a "significant" one.  */
14136             must_output_die = (BLOCK_VARS (stmt) != NULL
14137                                && (TREE_USED (stmt)
14138                                    || TREE_ASM_WRITTEN (stmt)
14139                                    || BLOCK_ABSTRACT (stmt)));
14140           else
14141             /* We are in terse mode, so only local (nested) function
14142                definitions count as "significant" local declarations.  */
14143             for (decl = BLOCK_VARS (stmt);
14144                  decl != NULL; decl = TREE_CHAIN (decl))
14145               if (TREE_CODE (decl) == FUNCTION_DECL
14146                   && DECL_INITIAL (decl))
14147                 {
14148                   must_output_die = 1;
14149                   break;
14150                 }
14151         }
14152     }
14153
14154   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
14155      DIE for any block which contains no significant local declarations at
14156      all.  Rather, in such cases we just call `decls_for_scope' so that any
14157      needed Dwarf info for any sub-blocks will get properly generated. Note
14158      that in terse mode, our definition of what constitutes a "significant"
14159      local declaration gets restricted to include only inlined function
14160      instances and local (nested) function definitions.  */
14161   if (must_output_die)
14162     {
14163       if (origin_code == FUNCTION_DECL)
14164         gen_inlined_subroutine_die (stmt, context_die, depth);
14165       else
14166         gen_lexical_block_die (stmt, context_die, depth);
14167     }
14168   else
14169     decls_for_scope (stmt, context_die, depth);
14170 }
14171
14172 /* Generate all of the decls declared within a given scope and (recursively)
14173    all of its sub-blocks.  */
14174
14175 static void
14176 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
14177 {
14178   tree decl;
14179   tree subblocks;
14180
14181   /* Ignore NULL blocks.  */
14182   if (stmt == NULL_TREE)
14183     return;
14184
14185   if (TREE_USED (stmt))
14186     {
14187       /* Output the DIEs to represent all of the data objects and typedefs
14188          declared directly within this block but not within any nested
14189          sub-blocks.  Also, nested function and tag DIEs have been
14190          generated with a parent of NULL; fix that up now.  */
14191       for (decl = BLOCK_VARS (stmt); decl != NULL; decl = TREE_CHAIN (decl))
14192         {
14193           dw_die_ref die;
14194
14195           if (TREE_CODE (decl) == FUNCTION_DECL)
14196             die = lookup_decl_die (decl);
14197           else if (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl))
14198             die = lookup_type_die (TREE_TYPE (decl));
14199           else
14200             die = NULL;
14201
14202           if (die != NULL && die->die_parent == NULL)
14203             add_child_die (context_die, die);
14204           /* Do not produce debug information for static variables since
14205              these might be optimized out.  We are called for these later
14206              in varpool_analyze_pending_decls.
14207
14208              But *do* produce it for Fortran COMMON variables because,
14209              even though they are static, their names can differ depending
14210              on the scope, which we need to preserve.  */
14211           if (TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl)
14212               && !(is_fortran () && TREE_PUBLIC (decl)))
14213             ;
14214           else
14215             gen_decl_die (decl, context_die);
14216         }
14217     }
14218
14219   /* If we're at -g1, we're not interested in subblocks.  */
14220   if (debug_info_level <= DINFO_LEVEL_TERSE)
14221     return;
14222
14223   /* Output the DIEs to represent all sub-blocks (and the items declared
14224      therein) of this block.  */
14225   for (subblocks = BLOCK_SUBBLOCKS (stmt);
14226        subblocks != NULL;
14227        subblocks = BLOCK_CHAIN (subblocks))
14228     gen_block_die (subblocks, context_die, depth + 1);
14229 }
14230
14231 /* Is this a typedef we can avoid emitting?  */
14232
14233 static inline int
14234 is_redundant_typedef (const_tree decl)
14235 {
14236   if (TYPE_DECL_IS_STUB (decl))
14237     return 1;
14238
14239   if (DECL_ARTIFICIAL (decl)
14240       && DECL_CONTEXT (decl)
14241       && is_tagged_type (DECL_CONTEXT (decl))
14242       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
14243       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
14244     /* Also ignore the artificial member typedef for the class name.  */
14245     return 1;
14246
14247   return 0;
14248 }
14249
14250 /* Returns the DIE for a context.  */
14251
14252 static inline dw_die_ref
14253 get_context_die (tree context)
14254 {
14255   if (context)
14256     {
14257       /* Find die that represents this context.  */
14258       if (TYPE_P (context))
14259         return force_type_die (context);
14260       else
14261         return force_decl_die (context);
14262     }
14263   return comp_unit_die;
14264 }
14265
14266 /* Returns the DIE for decl.  A DIE will always be returned.  */
14267
14268 static dw_die_ref
14269 force_decl_die (tree decl)
14270 {
14271   dw_die_ref decl_die;
14272   unsigned saved_external_flag;
14273   tree save_fn = NULL_TREE;
14274   decl_die = lookup_decl_die (decl);
14275   if (!decl_die)
14276     {
14277       dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
14278
14279       decl_die = lookup_decl_die (decl);
14280       if (decl_die)
14281         return decl_die;
14282
14283       switch (TREE_CODE (decl))
14284         {
14285         case FUNCTION_DECL:
14286           /* Clear current_function_decl, so that gen_subprogram_die thinks
14287              that this is a declaration. At this point, we just want to force
14288              declaration die.  */
14289           save_fn = current_function_decl;
14290           current_function_decl = NULL_TREE;
14291           gen_subprogram_die (decl, context_die);
14292           current_function_decl = save_fn;
14293           break;
14294
14295         case VAR_DECL:
14296           /* Set external flag to force declaration die. Restore it after
14297            gen_decl_die() call.  */
14298           saved_external_flag = DECL_EXTERNAL (decl);
14299           DECL_EXTERNAL (decl) = 1;
14300           gen_decl_die (decl, context_die);
14301           DECL_EXTERNAL (decl) = saved_external_flag;
14302           break;
14303
14304         case NAMESPACE_DECL:
14305           dwarf2out_decl (decl);
14306           break;
14307
14308         default:
14309           gcc_unreachable ();
14310         }
14311
14312       /* We should be able to find the DIE now.  */
14313       if (!decl_die)
14314         decl_die = lookup_decl_die (decl);
14315       gcc_assert (decl_die);
14316     }
14317
14318   return decl_die;
14319 }
14320
14321 /* Returns the DIE for TYPE, that must not be a base type.  A DIE is
14322    always returned.  */
14323
14324 static dw_die_ref
14325 force_type_die (tree type)
14326 {
14327   dw_die_ref type_die;
14328
14329   type_die = lookup_type_die (type);
14330   if (!type_die)
14331     {
14332       dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
14333
14334       type_die = modified_type_die (type, TYPE_READONLY (type),
14335                                     TYPE_VOLATILE (type), context_die);
14336       gcc_assert (type_die);
14337     }
14338   return type_die;
14339 }
14340
14341 /* Force out any required namespaces to be able to output DECL,
14342    and return the new context_die for it, if it's changed.  */
14343
14344 static dw_die_ref
14345 setup_namespace_context (tree thing, dw_die_ref context_die)
14346 {
14347   tree context = (DECL_P (thing)
14348                   ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
14349   if (context && TREE_CODE (context) == NAMESPACE_DECL)
14350     /* Force out the namespace.  */
14351     context_die = force_decl_die (context);
14352
14353   return context_die;
14354 }
14355
14356 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
14357    type) within its namespace, if appropriate.
14358
14359    For compatibility with older debuggers, namespace DIEs only contain
14360    declarations; all definitions are emitted at CU scope.  */
14361
14362 static void
14363 declare_in_namespace (tree thing, dw_die_ref context_die)
14364 {
14365   dw_die_ref ns_context;
14366
14367   if (debug_info_level <= DINFO_LEVEL_TERSE)
14368     return;
14369
14370   /* If this decl is from an inlined function, then don't try to emit it in its
14371      namespace, as we will get confused.  It would have already been emitted
14372      when the abstract instance of the inline function was emitted anyways.  */
14373   if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
14374     return;
14375
14376   ns_context = setup_namespace_context (thing, context_die);
14377
14378   if (ns_context != context_die)
14379     {
14380       if (DECL_P (thing))
14381         gen_decl_die (thing, ns_context);
14382       else
14383         gen_type_die (thing, ns_context);
14384     }
14385 }
14386
14387 /* Generate a DIE for a namespace or namespace alias.  */
14388
14389 static void
14390 gen_namespace_die (tree decl)
14391 {
14392   dw_die_ref context_die = setup_namespace_context (decl, comp_unit_die);
14393
14394   /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
14395      they are an alias of.  */
14396   if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
14397     {
14398       /* Output a real namespace.  */
14399       dw_die_ref namespace_die
14400         = new_die (DW_TAG_namespace, context_die, decl);
14401       add_name_and_src_coords_attributes (namespace_die, decl);
14402       equate_decl_number_to_die (decl, namespace_die);
14403     }
14404   else
14405     {
14406       /* Output a namespace alias.  */
14407
14408       /* Force out the namespace we are an alias of, if necessary.  */
14409       dw_die_ref origin_die
14410         = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
14411
14412       /* Now create the namespace alias DIE.  */
14413       dw_die_ref namespace_die
14414         = new_die (DW_TAG_imported_declaration, context_die, decl);
14415       add_name_and_src_coords_attributes (namespace_die, decl);
14416       add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
14417       equate_decl_number_to_die (decl, namespace_die);
14418     }
14419 }
14420
14421 /* Generate Dwarf debug information for a decl described by DECL.  */
14422
14423 static void
14424 gen_decl_die (tree decl, dw_die_ref context_die)
14425 {
14426   tree origin;
14427
14428   if (DECL_P (decl) && DECL_IGNORED_P (decl))
14429     return;
14430
14431   switch (TREE_CODE (decl))
14432     {
14433     case ERROR_MARK:
14434       break;
14435
14436     case CONST_DECL:
14437       /* The individual enumerators of an enum type get output when we output
14438          the Dwarf representation of the relevant enum type itself.  */
14439       break;
14440
14441     case FUNCTION_DECL:
14442       /* Don't output any DIEs to represent mere function declarations,
14443          unless they are class members or explicit block externs.  */
14444       if (DECL_INITIAL (decl) == NULL_TREE && DECL_CONTEXT (decl) == NULL_TREE
14445           && (current_function_decl == NULL_TREE || DECL_ARTIFICIAL (decl)))
14446         break;
14447
14448 #if 0
14449       /* FIXME */
14450       /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
14451          on local redeclarations of global functions.  That seems broken.  */
14452       if (current_function_decl != decl)
14453         /* This is only a declaration.  */;
14454 #endif
14455
14456       /* If we're emitting a clone, emit info for the abstract instance.  */
14457       if (DECL_ORIGIN (decl) != decl)
14458         dwarf2out_abstract_function (DECL_ABSTRACT_ORIGIN (decl));
14459
14460       /* If we're emitting an out-of-line copy of an inline function,
14461          emit info for the abstract instance and set up to refer to it.  */
14462       else if (cgraph_function_possibly_inlined_p (decl)
14463                && ! DECL_ABSTRACT (decl)
14464                && ! class_or_namespace_scope_p (context_die)
14465                /* dwarf2out_abstract_function won't emit a die if this is just
14466                   a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
14467                   that case, because that works only if we have a die.  */
14468                && DECL_INITIAL (decl) != NULL_TREE)
14469         {
14470           dwarf2out_abstract_function (decl);
14471           set_decl_origin_self (decl);
14472         }
14473
14474       /* Otherwise we're emitting the primary DIE for this decl.  */
14475       else if (debug_info_level > DINFO_LEVEL_TERSE)
14476         {
14477           /* Before we describe the FUNCTION_DECL itself, make sure that we
14478              have described its return type.  */
14479           gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
14480
14481           /* And its virtual context.  */
14482           if (DECL_VINDEX (decl) != NULL_TREE)
14483             gen_type_die (DECL_CONTEXT (decl), context_die);
14484
14485           /* And its containing type.  */
14486           origin = decl_class_context (decl);
14487           if (origin != NULL_TREE)
14488             gen_type_die_for_member (origin, decl, context_die);
14489
14490           /* And its containing namespace.  */
14491           declare_in_namespace (decl, context_die);
14492         }
14493
14494       /* Now output a DIE to represent the function itself.  */
14495       gen_subprogram_die (decl, context_die);
14496       break;
14497
14498     case TYPE_DECL:
14499       /* If we are in terse mode, don't generate any DIEs to represent any
14500          actual typedefs.  */
14501       if (debug_info_level <= DINFO_LEVEL_TERSE)
14502         break;
14503
14504       /* In the special case of a TYPE_DECL node representing the declaration
14505          of some type tag, if the given TYPE_DECL is marked as having been
14506          instantiated from some other (original) TYPE_DECL node (e.g. one which
14507          was generated within the original definition of an inline function) we
14508          have to generate a special (abbreviated) DW_TAG_structure_type,
14509          DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  */
14510       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE
14511           && is_tagged_type (TREE_TYPE (decl)))
14512         {
14513           gen_tagged_type_instantiation_die (TREE_TYPE (decl), context_die);
14514           break;
14515         }
14516
14517       if (is_redundant_typedef (decl))
14518         gen_type_die (TREE_TYPE (decl), context_die);
14519       else
14520         /* Output a DIE to represent the typedef itself.  */
14521         gen_typedef_die (decl, context_die);
14522       break;
14523
14524     case LABEL_DECL:
14525       if (debug_info_level >= DINFO_LEVEL_NORMAL)
14526         gen_label_die (decl, context_die);
14527       break;
14528
14529     case VAR_DECL:
14530     case RESULT_DECL:
14531       /* If we are in terse mode, don't generate any DIEs to represent any
14532          variable declarations or definitions.  */
14533       if (debug_info_level <= DINFO_LEVEL_TERSE)
14534         break;
14535
14536       /* If this is the global definition of the Fortran COMMON block, we don't
14537          need to do anything.  Syntactically, the block itself has no identity,
14538          just its constituent identifiers.  */
14539       if (TREE_CODE (decl) == VAR_DECL
14540           && TREE_PUBLIC (decl)
14541           && TREE_STATIC (decl)
14542           && is_fortran ()
14543           && !DECL_HAS_VALUE_EXPR_P (decl))
14544         break;
14545
14546       /* Output any DIEs that are needed to specify the type of this data
14547          object.  */
14548       if (TREE_CODE (decl) == RESULT_DECL && DECL_BY_REFERENCE (decl))
14549         gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
14550       else
14551         gen_type_die (TREE_TYPE (decl), context_die);
14552
14553       /* And its containing type.  */
14554       origin = decl_class_context (decl);
14555       if (origin != NULL_TREE)
14556         gen_type_die_for_member (origin, decl, context_die);
14557
14558       /* And its containing namespace.  */
14559       declare_in_namespace (decl, context_die);
14560
14561       /* Now output the DIE to represent the data object itself.  This gets
14562          complicated because of the possibility that the VAR_DECL really
14563          represents an inlined instance of a formal parameter for an inline
14564          function.  */
14565       origin = decl_ultimate_origin (decl);
14566       if (origin != NULL_TREE && TREE_CODE (origin) == PARM_DECL)
14567         gen_formal_parameter_die (decl, context_die);
14568       else
14569         gen_variable_die (decl, context_die);
14570       break;
14571
14572     case FIELD_DECL:
14573       /* Ignore the nameless fields that are used to skip bits but handle C++
14574          anonymous unions and structs.  */
14575       if (DECL_NAME (decl) != NULL_TREE
14576           || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
14577           || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
14578         {
14579           gen_type_die (member_declared_type (decl), context_die);
14580           gen_field_die (decl, context_die);
14581         }
14582       break;
14583
14584     case PARM_DECL:
14585       if (DECL_BY_REFERENCE (decl))
14586         gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
14587       else
14588         gen_type_die (TREE_TYPE (decl), context_die);
14589       gen_formal_parameter_die (decl, context_die);
14590       break;
14591
14592     case NAMESPACE_DECL:
14593       gen_namespace_die (decl);
14594       break;
14595
14596     default:
14597       /* Probably some frontend-internal decl.  Assume we don't care.  */
14598       gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
14599       break;
14600     }
14601 }
14602 \f
14603 /* Output debug information for global decl DECL.  Called from toplev.c after
14604    compilation proper has finished.  */
14605
14606 static void
14607 dwarf2out_global_decl (tree decl)
14608 {
14609   /* Output DWARF2 information for file-scope tentative data object
14610      declarations, file-scope (extern) function declarations (which
14611      had no corresponding body) and file-scope tagged type declarations
14612      and definitions which have not yet been forced out.
14613
14614      Ignore the global decl of any Fortran COMMON blocks which also
14615      wind up here though they have already been described in the local
14616      scope for the procedures using them.  */
14617   if (TREE_CODE (decl) == VAR_DECL
14618       && TREE_PUBLIC (decl) && TREE_STATIC (decl) && is_fortran ())
14619     return;
14620
14621   if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
14622     dwarf2out_decl (decl);
14623 }
14624
14625 /* Output debug information for type decl DECL.  Called from toplev.c
14626    and from language front ends (to record built-in types).  */
14627 static void
14628 dwarf2out_type_decl (tree decl, int local)
14629 {
14630   if (!local)
14631     dwarf2out_decl (decl);
14632 }
14633
14634 /* Output debug information for imported module or decl.  */
14635
14636 static void
14637 dwarf2out_imported_module_or_decl (tree decl, tree context)
14638 {
14639   dw_die_ref imported_die, at_import_die;
14640   dw_die_ref scope_die;
14641   expanded_location xloc;
14642
14643   if (debug_info_level <= DINFO_LEVEL_TERSE)
14644     return;
14645
14646   gcc_assert (decl);
14647
14648   /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
14649      We need decl DIE for reference and scope die. First, get DIE for the decl
14650      itself.  */
14651
14652   /* Get the scope die for decl context. Use comp_unit_die for global module
14653      or decl. If die is not found for non globals, force new die.  */
14654   if (context
14655       && TYPE_P (context)
14656       && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
14657     return;
14658   scope_die = get_context_die (context);
14659
14660   /* For TYPE_DECL or CONST_DECL, lookup TREE_TYPE.  */
14661   if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
14662     {
14663       if (is_base_type (TREE_TYPE (decl)))
14664         at_import_die = base_type_die (TREE_TYPE (decl));
14665       else
14666         at_import_die = force_type_die (TREE_TYPE (decl));
14667       /* For namespace N { typedef void T; } using N::T; base_type_die
14668          returns NULL, but DW_TAG_imported_declaration requires
14669          the DW_AT_import tag.  Force creation of DW_TAG_typedef.  */
14670       if (!at_import_die)
14671         {
14672           gcc_assert (TREE_CODE (decl) == TYPE_DECL);
14673           gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
14674           at_import_die = lookup_type_die (TREE_TYPE (decl));
14675           gcc_assert (at_import_die);
14676         }
14677     }
14678   else
14679     {
14680       at_import_die = lookup_decl_die (decl);
14681       if (!at_import_die)
14682         {
14683           /* If we're trying to avoid duplicate debug info, we may not have
14684              emitted the member decl for this field.  Emit it now.  */
14685           if (TREE_CODE (decl) == FIELD_DECL)
14686             {
14687               tree type = DECL_CONTEXT (decl);
14688
14689               if (TYPE_CONTEXT (type)
14690                   && TYPE_P (TYPE_CONTEXT (type))
14691                   && !should_emit_struct_debug (TYPE_CONTEXT (type),
14692                                                 DINFO_USAGE_DIR_USE))
14693                 return;
14694               gen_type_die_for_member (type, decl,
14695                                        get_context_die (TYPE_CONTEXT (type)));
14696             }
14697           at_import_die = force_decl_die (decl);
14698         }
14699     }
14700
14701   /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
14702   if (TREE_CODE (decl) == NAMESPACE_DECL)
14703     imported_die = new_die (DW_TAG_imported_module, scope_die, context);
14704   else
14705     imported_die = new_die (DW_TAG_imported_declaration, scope_die, context);
14706
14707   xloc = expand_location (input_location);
14708   add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
14709   add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
14710   add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
14711 }
14712
14713 /* Write the debugging output for DECL.  */
14714
14715 void
14716 dwarf2out_decl (tree decl)
14717 {
14718   dw_die_ref context_die = comp_unit_die;
14719
14720   switch (TREE_CODE (decl))
14721     {
14722     case ERROR_MARK:
14723       return;
14724
14725     case FUNCTION_DECL:
14726       /* What we would really like to do here is to filter out all mere
14727          file-scope declarations of file-scope functions which are never
14728          referenced later within this translation unit (and keep all of ones
14729          that *are* referenced later on) but we aren't clairvoyant, so we have
14730          no idea which functions will be referenced in the future (i.e. later
14731          on within the current translation unit). So here we just ignore all
14732          file-scope function declarations which are not also definitions.  If
14733          and when the debugger needs to know something about these functions,
14734          it will have to hunt around and find the DWARF information associated
14735          with the definition of the function.
14736
14737          We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
14738          nodes represent definitions and which ones represent mere
14739          declarations.  We have to check DECL_INITIAL instead. That's because
14740          the C front-end supports some weird semantics for "extern inline"
14741          function definitions.  These can get inlined within the current
14742          translation unit (and thus, we need to generate Dwarf info for their
14743          abstract instances so that the Dwarf info for the concrete inlined
14744          instances can have something to refer to) but the compiler never
14745          generates any out-of-lines instances of such things (despite the fact
14746          that they *are* definitions).
14747
14748          The important point is that the C front-end marks these "extern
14749          inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
14750          them anyway. Note that the C++ front-end also plays some similar games
14751          for inline function definitions appearing within include files which
14752          also contain `#pragma interface' pragmas.  */
14753       if (DECL_INITIAL (decl) == NULL_TREE)
14754         return;
14755
14756       /* If we're a nested function, initially use a parent of NULL; if we're
14757          a plain function, this will be fixed up in decls_for_scope.  If
14758          we're a method, it will be ignored, since we already have a DIE.  */
14759       if (decl_function_context (decl)
14760           /* But if we're in terse mode, we don't care about scope.  */
14761           && debug_info_level > DINFO_LEVEL_TERSE)
14762         context_die = NULL;
14763       break;
14764
14765     case VAR_DECL:
14766       /* Ignore this VAR_DECL if it refers to a file-scope extern data object
14767          declaration and if the declaration was never even referenced from
14768          within this entire compilation unit.  We suppress these DIEs in
14769          order to save space in the .debug section (by eliminating entries
14770          which are probably useless).  Note that we must not suppress
14771          block-local extern declarations (whether used or not) because that
14772          would screw-up the debugger's name lookup mechanism and cause it to
14773          miss things which really ought to be in scope at a given point.  */
14774       if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
14775         return;
14776
14777       /* For local statics lookup proper context die.  */
14778       if (TREE_STATIC (decl) && decl_function_context (decl))
14779         context_die = lookup_decl_die (DECL_CONTEXT (decl));
14780
14781       /* If we are in terse mode, don't generate any DIEs to represent any
14782          variable declarations or definitions.  */
14783       if (debug_info_level <= DINFO_LEVEL_TERSE)
14784         return;
14785       break;
14786
14787     case NAMESPACE_DECL:
14788       if (debug_info_level <= DINFO_LEVEL_TERSE)
14789         return;
14790       if (lookup_decl_die (decl) != NULL)
14791         return;
14792       break;
14793
14794     case TYPE_DECL:
14795       /* Don't emit stubs for types unless they are needed by other DIEs.  */
14796       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
14797         return;
14798
14799       /* Don't bother trying to generate any DIEs to represent any of the
14800          normal built-in types for the language we are compiling.  */
14801       if (DECL_IS_BUILTIN (decl))
14802         {
14803           /* OK, we need to generate one for `bool' so GDB knows what type
14804              comparisons have.  */
14805           if (is_cxx ()
14806               && TREE_CODE (TREE_TYPE (decl)) == BOOLEAN_TYPE
14807               && ! DECL_IGNORED_P (decl))
14808             modified_type_die (TREE_TYPE (decl), 0, 0, NULL);
14809
14810           return;
14811         }
14812
14813       /* If we are in terse mode, don't generate any DIEs for types.  */
14814       if (debug_info_level <= DINFO_LEVEL_TERSE)
14815         return;
14816
14817       /* If we're a function-scope tag, initially use a parent of NULL;
14818          this will be fixed up in decls_for_scope.  */
14819       if (decl_function_context (decl))
14820         context_die = NULL;
14821
14822       break;
14823
14824     default:
14825       return;
14826     }
14827
14828   gen_decl_die (decl, context_die);
14829 }
14830
14831 /* Output a marker (i.e. a label) for the beginning of the generated code for
14832    a lexical block.  */
14833
14834 static void
14835 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
14836                        unsigned int blocknum)
14837 {
14838   switch_to_section (current_function_section ());
14839   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
14840 }
14841
14842 /* Output a marker (i.e. a label) for the end of the generated code for a
14843    lexical block.  */
14844
14845 static void
14846 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
14847 {
14848   switch_to_section (current_function_section ());
14849   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
14850 }
14851
14852 /* Returns nonzero if it is appropriate not to emit any debugging
14853    information for BLOCK, because it doesn't contain any instructions.
14854
14855    Don't allow this for blocks with nested functions or local classes
14856    as we would end up with orphans, and in the presence of scheduling
14857    we may end up calling them anyway.  */
14858
14859 static bool
14860 dwarf2out_ignore_block (const_tree block)
14861 {
14862   tree decl;
14863
14864   for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
14865     if (TREE_CODE (decl) == FUNCTION_DECL
14866         || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
14867       return 0;
14868
14869   return 1;
14870 }
14871
14872 /* Hash table routines for file_hash.  */
14873
14874 static int
14875 file_table_eq (const void *p1_p, const void *p2_p)
14876 {
14877   const struct dwarf_file_data *const p1 =
14878     (const struct dwarf_file_data *) p1_p;
14879   const char *const p2 = (const char *) p2_p;
14880   return strcmp (p1->filename, p2) == 0;
14881 }
14882
14883 static hashval_t
14884 file_table_hash (const void *p_p)
14885 {
14886   const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
14887   return htab_hash_string (p->filename);
14888 }
14889
14890 /* Lookup FILE_NAME (in the list of filenames that we know about here in
14891    dwarf2out.c) and return its "index".  The index of each (known) filename is
14892    just a unique number which is associated with only that one filename.  We
14893    need such numbers for the sake of generating labels (in the .debug_sfnames
14894    section) and references to those files numbers (in the .debug_srcinfo
14895    and.debug_macinfo sections).  If the filename given as an argument is not
14896    found in our current list, add it to the list and assign it the next
14897    available unique index number.  In order to speed up searches, we remember
14898    the index of the filename was looked up last.  This handles the majority of
14899    all searches.  */
14900
14901 static struct dwarf_file_data *
14902 lookup_filename (const char *file_name)
14903 {
14904   void ** slot;
14905   struct dwarf_file_data * created;
14906
14907   /* Check to see if the file name that was searched on the previous
14908      call matches this file name.  If so, return the index.  */
14909   if (file_table_last_lookup
14910       && (file_name == file_table_last_lookup->filename
14911           || strcmp (file_table_last_lookup->filename, file_name) == 0))
14912     return file_table_last_lookup;
14913
14914   /* Didn't match the previous lookup, search the table.  */
14915   slot = htab_find_slot_with_hash (file_table, file_name,
14916                                    htab_hash_string (file_name), INSERT);
14917   if (*slot)
14918     return (struct dwarf_file_data *) *slot;
14919
14920   created = GGC_NEW (struct dwarf_file_data);
14921   created->filename = file_name;
14922   created->emitted_number = 0;
14923   *slot = created;
14924   return created;
14925 }
14926
14927 /* If the assembler will construct the file table, then translate the compiler
14928    internal file table number into the assembler file table number, and emit
14929    a .file directive if we haven't already emitted one yet.  The file table
14930    numbers are different because we prune debug info for unused variables and
14931    types, which may include filenames.  */
14932
14933 static int
14934 maybe_emit_file (struct dwarf_file_data * fd)
14935 {
14936   if (! fd->emitted_number)
14937     {
14938       if (last_emitted_file)
14939         fd->emitted_number = last_emitted_file->emitted_number + 1;
14940       else
14941         fd->emitted_number = 1;
14942       last_emitted_file = fd;
14943
14944       if (DWARF2_ASM_LINE_DEBUG_INFO)
14945         {
14946           fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
14947           output_quoted_string (asm_out_file,
14948                                 remap_debug_filename (fd->filename));
14949           fputc ('\n', asm_out_file);
14950         }
14951     }
14952
14953   return fd->emitted_number;
14954 }
14955
14956 /* Called by the final INSN scan whenever we see a var location.  We
14957    use it to drop labels in the right places, and throw the location in
14958    our lookup table.  */
14959
14960 static void
14961 dwarf2out_var_location (rtx loc_note)
14962 {
14963   char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
14964   struct var_loc_node *newloc;
14965   rtx prev_insn;
14966   static rtx last_insn;
14967   static const char *last_label;
14968   tree decl;
14969
14970   if (!DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
14971     return;
14972   prev_insn = PREV_INSN (loc_note);
14973
14974   newloc = GGC_CNEW (struct var_loc_node);
14975   /* If the insn we processed last time is the previous insn
14976      and it is also a var location note, use the label we emitted
14977      last time.  */
14978   if (last_insn != NULL_RTX
14979       && last_insn == prev_insn
14980       && NOTE_P (prev_insn)
14981       && NOTE_KIND (prev_insn) == NOTE_INSN_VAR_LOCATION)
14982     {
14983       newloc->label = last_label;
14984     }
14985   else
14986     {
14987       ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
14988       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
14989       loclabel_num++;
14990       newloc->label = ggc_strdup (loclabel);
14991     }
14992   newloc->var_loc_note = loc_note;
14993   newloc->next = NULL;
14994
14995   if (cfun && in_cold_section_p)
14996     newloc->section_label = crtl->subsections.cold_section_label;
14997   else
14998     newloc->section_label = text_section_label;
14999
15000   last_insn = loc_note;
15001   last_label = newloc->label;
15002   decl = NOTE_VAR_LOCATION_DECL (loc_note);
15003   add_var_loc_to_decl (decl, newloc);
15004 }
15005
15006 /* We need to reset the locations at the beginning of each
15007    function. We can't do this in the end_function hook, because the
15008    declarations that use the locations won't have been output when
15009    that hook is called.  Also compute have_multiple_function_sections here.  */
15010
15011 static void
15012 dwarf2out_begin_function (tree fun)
15013 {
15014   htab_empty (decl_loc_table);
15015
15016   if (function_section (fun) != text_section)
15017     have_multiple_function_sections = true;
15018
15019   dwarf2out_note_section_used ();
15020 }
15021
15022 /* Output a label to mark the beginning of a source code line entry
15023    and record information relating to this source line, in
15024    'line_info_table' for later output of the .debug_line section.  */
15025
15026 static void
15027 dwarf2out_source_line (unsigned int line, const char *filename)
15028 {
15029   if (debug_info_level >= DINFO_LEVEL_NORMAL
15030       && line != 0)
15031     {
15032       int file_num = maybe_emit_file (lookup_filename (filename));
15033
15034       switch_to_section (current_function_section ());
15035
15036       /* If requested, emit something human-readable.  */
15037       if (flag_debug_asm)
15038         fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
15039                  filename, line);
15040
15041       if (DWARF2_ASM_LINE_DEBUG_INFO)
15042         {
15043           /* Emit the .loc directive understood by GNU as.  */
15044           fprintf (asm_out_file, "\t.loc %d %d 0\n", file_num, line);
15045
15046           /* Indicate that line number info exists.  */
15047           line_info_table_in_use++;
15048         }
15049       else if (function_section (current_function_decl) != text_section)
15050         {
15051           dw_separate_line_info_ref line_info;
15052           targetm.asm_out.internal_label (asm_out_file,
15053                                           SEPARATE_LINE_CODE_LABEL,
15054                                           separate_line_info_table_in_use);
15055
15056           /* Expand the line info table if necessary.  */
15057           if (separate_line_info_table_in_use
15058               == separate_line_info_table_allocated)
15059             {
15060               separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
15061               separate_line_info_table
15062                 = GGC_RESIZEVEC (dw_separate_line_info_entry,
15063                                  separate_line_info_table,
15064                                  separate_line_info_table_allocated);
15065               memset (separate_line_info_table
15066                        + separate_line_info_table_in_use,
15067                       0,
15068                       (LINE_INFO_TABLE_INCREMENT
15069                        * sizeof (dw_separate_line_info_entry)));
15070             }
15071
15072           /* Add the new entry at the end of the line_info_table.  */
15073           line_info
15074             = &separate_line_info_table[separate_line_info_table_in_use++];
15075           line_info->dw_file_num = file_num;
15076           line_info->dw_line_num = line;
15077           line_info->function = current_function_funcdef_no;
15078         }
15079       else
15080         {
15081           dw_line_info_ref line_info;
15082
15083           targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL,
15084                                      line_info_table_in_use);
15085
15086           /* Expand the line info table if necessary.  */
15087           if (line_info_table_in_use == line_info_table_allocated)
15088             {
15089               line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
15090               line_info_table
15091                 = GGC_RESIZEVEC (dw_line_info_entry, line_info_table,
15092                                  line_info_table_allocated);
15093               memset (line_info_table + line_info_table_in_use, 0,
15094                       LINE_INFO_TABLE_INCREMENT * sizeof (dw_line_info_entry));
15095             }
15096
15097           /* Add the new entry at the end of the line_info_table.  */
15098           line_info = &line_info_table[line_info_table_in_use++];
15099           line_info->dw_file_num = file_num;
15100           line_info->dw_line_num = line;
15101         }
15102     }
15103 }
15104
15105 /* Record the beginning of a new source file.  */
15106
15107 static void
15108 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
15109 {
15110   if (flag_eliminate_dwarf2_dups)
15111     {
15112       /* Record the beginning of the file for break_out_includes.  */
15113       dw_die_ref bincl_die;
15114
15115       bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die, NULL);
15116       add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
15117     }
15118
15119   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
15120     {
15121       int file_num = maybe_emit_file (lookup_filename (filename));
15122
15123       switch_to_section (debug_macinfo_section);
15124       dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
15125       dw2_asm_output_data_uleb128 (lineno, "Included from line number %d",
15126                                    lineno);
15127
15128       dw2_asm_output_data_uleb128 (file_num, "file %s", filename);
15129     }
15130 }
15131
15132 /* Record the end of a source file.  */
15133
15134 static void
15135 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
15136 {
15137   if (flag_eliminate_dwarf2_dups)
15138     /* Record the end of the file for break_out_includes.  */
15139     new_die (DW_TAG_GNU_EINCL, comp_unit_die, NULL);
15140
15141   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
15142     {
15143       switch_to_section (debug_macinfo_section);
15144       dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
15145     }
15146 }
15147
15148 /* Called from debug_define in toplev.c.  The `buffer' parameter contains
15149    the tail part of the directive line, i.e. the part which is past the
15150    initial whitespace, #, whitespace, directive-name, whitespace part.  */
15151
15152 static void
15153 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
15154                   const char *buffer ATTRIBUTE_UNUSED)
15155 {
15156   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
15157     {
15158       switch_to_section (debug_macinfo_section);
15159       dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
15160       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
15161       dw2_asm_output_nstring (buffer, -1, "The macro");
15162     }
15163 }
15164
15165 /* Called from debug_undef in toplev.c.  The `buffer' parameter contains
15166    the tail part of the directive line, i.e. the part which is past the
15167    initial whitespace, #, whitespace, directive-name, whitespace part.  */
15168
15169 static void
15170 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
15171                  const char *buffer ATTRIBUTE_UNUSED)
15172 {
15173   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
15174     {
15175       switch_to_section (debug_macinfo_section);
15176       dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
15177       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
15178       dw2_asm_output_nstring (buffer, -1, "The macro");
15179     }
15180 }
15181
15182 /* Set up for Dwarf output at the start of compilation.  */
15183
15184 static void
15185 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
15186 {
15187   /* Allocate the file_table.  */
15188   file_table = htab_create_ggc (50, file_table_hash,
15189                                 file_table_eq, NULL);
15190
15191   /* Allocate the decl_die_table.  */
15192   decl_die_table = htab_create_ggc (10, decl_die_table_hash,
15193                                     decl_die_table_eq, NULL);
15194
15195   /* Allocate the decl_loc_table.  */
15196   decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
15197                                     decl_loc_table_eq, NULL);
15198
15199   /* Allocate the initial hunk of the decl_scope_table.  */
15200   decl_scope_table = VEC_alloc (tree, gc, 256);
15201
15202   /* Allocate the initial hunk of the abbrev_die_table.  */
15203   abbrev_die_table = GGC_CNEWVEC (dw_die_ref, ABBREV_DIE_TABLE_INCREMENT);
15204   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
15205   /* Zero-th entry is allocated, but unused.  */
15206   abbrev_die_table_in_use = 1;
15207
15208   /* Allocate the initial hunk of the line_info_table.  */
15209   line_info_table = GGC_CNEWVEC (dw_line_info_entry, LINE_INFO_TABLE_INCREMENT);
15210   line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
15211
15212   /* Zero-th entry is allocated, but unused.  */
15213   line_info_table_in_use = 1;
15214
15215   /* Allocate the pubtypes and pubnames vectors.  */
15216   pubname_table = VEC_alloc (pubname_entry, gc, 32);
15217   pubtype_table = VEC_alloc (pubname_entry, gc, 32);
15218
15219   /* Generate the initial DIE for the .debug section.  Note that the (string)
15220      value given in the DW_AT_name attribute of the DW_TAG_compile_unit DIE
15221      will (typically) be a relative pathname and that this pathname should be
15222      taken as being relative to the directory from which the compiler was
15223      invoked when the given (base) source file was compiled.  We will fill
15224      in this value in dwarf2out_finish.  */
15225   comp_unit_die = gen_compile_unit_die (NULL);
15226
15227   incomplete_types = VEC_alloc (tree, gc, 64);
15228
15229   used_rtx_array = VEC_alloc (rtx, gc, 32);
15230
15231   debug_info_section = get_section (DEBUG_INFO_SECTION,
15232                                     SECTION_DEBUG, NULL);
15233   debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
15234                                       SECTION_DEBUG, NULL);
15235   debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
15236                                        SECTION_DEBUG, NULL);
15237   debug_macinfo_section = get_section (DEBUG_MACINFO_SECTION,
15238                                        SECTION_DEBUG, NULL);
15239   debug_line_section = get_section (DEBUG_LINE_SECTION,
15240                                     SECTION_DEBUG, NULL);
15241   debug_loc_section = get_section (DEBUG_LOC_SECTION,
15242                                    SECTION_DEBUG, NULL);
15243   debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
15244                                         SECTION_DEBUG, NULL);
15245 #ifdef DEBUG_PUBTYPES_SECTION
15246   debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
15247                                         SECTION_DEBUG, NULL);
15248 #endif
15249   debug_str_section = get_section (DEBUG_STR_SECTION,
15250                                    DEBUG_STR_SECTION_FLAGS, NULL);
15251   debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
15252                                       SECTION_DEBUG, NULL);
15253   debug_frame_section = get_section (DEBUG_FRAME_SECTION,
15254                                      SECTION_DEBUG, NULL);
15255
15256   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
15257   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
15258                                DEBUG_ABBREV_SECTION_LABEL, 0);
15259   ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
15260   ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
15261                                COLD_TEXT_SECTION_LABEL, 0);
15262   ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
15263
15264   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
15265                                DEBUG_INFO_SECTION_LABEL, 0);
15266   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
15267                                DEBUG_LINE_SECTION_LABEL, 0);
15268   ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
15269                                DEBUG_RANGES_SECTION_LABEL, 0);
15270   switch_to_section (debug_abbrev_section);
15271   ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
15272   switch_to_section (debug_info_section);
15273   ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
15274   switch_to_section (debug_line_section);
15275   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
15276
15277   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
15278     {
15279       switch_to_section (debug_macinfo_section);
15280       ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
15281                                    DEBUG_MACINFO_SECTION_LABEL, 0);
15282       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
15283     }
15284
15285   switch_to_section (text_section);
15286   ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
15287   if (flag_reorder_blocks_and_partition)
15288     {
15289       cold_text_section = unlikely_text_section ();
15290       switch_to_section (cold_text_section);
15291       ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
15292     }
15293 }
15294
15295 /* A helper function for dwarf2out_finish called through
15296    ht_forall.  Emit one queued .debug_str string.  */
15297
15298 static int
15299 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
15300 {
15301   struct indirect_string_node *node = (struct indirect_string_node *) *h;
15302
15303   if (node->form == DW_FORM_strp)
15304     {
15305       switch_to_section (debug_str_section);
15306       ASM_OUTPUT_LABEL (asm_out_file, node->label);
15307       assemble_string (node->str, strlen (node->str) + 1);
15308     }
15309
15310   return 1;
15311 }
15312
15313 #if ENABLE_ASSERT_CHECKING
15314 /* Verify that all marks are clear.  */
15315
15316 static void
15317 verify_marks_clear (dw_die_ref die)
15318 {
15319   dw_die_ref c;
15320
15321   gcc_assert (! die->die_mark);
15322   FOR_EACH_CHILD (die, c, verify_marks_clear (c));
15323 }
15324 #endif /* ENABLE_ASSERT_CHECKING */
15325
15326 /* Clear the marks for a die and its children.
15327    Be cool if the mark isn't set.  */
15328
15329 static void
15330 prune_unmark_dies (dw_die_ref die)
15331 {
15332   dw_die_ref c;
15333
15334   if (die->die_mark)
15335     die->die_mark = 0;
15336   FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
15337 }
15338
15339 /* Given DIE that we're marking as used, find any other dies
15340    it references as attributes and mark them as used.  */
15341
15342 static void
15343 prune_unused_types_walk_attribs (dw_die_ref die)
15344 {
15345   dw_attr_ref a;
15346   unsigned ix;
15347
15348   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
15349     {
15350       if (a->dw_attr_val.val_class == dw_val_class_die_ref)
15351         {
15352           /* A reference to another DIE.
15353              Make sure that it will get emitted.  */
15354           prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
15355         }
15356       /* Set the string's refcount to 0 so that prune_unused_types_mark
15357          accounts properly for it.  */
15358       if (AT_class (a) == dw_val_class_str)
15359         a->dw_attr_val.v.val_str->refcount = 0;
15360     }
15361 }
15362
15363
15364 /* Mark DIE as being used.  If DOKIDS is true, then walk down
15365    to DIE's children.  */
15366
15367 static void
15368 prune_unused_types_mark (dw_die_ref die, int dokids)
15369 {
15370   dw_die_ref c;
15371
15372   if (die->die_mark == 0)
15373     {
15374       /* We haven't done this node yet.  Mark it as used.  */
15375       die->die_mark = 1;
15376
15377       /* We also have to mark its parents as used.
15378          (But we don't want to mark our parents' kids due to this.)  */
15379       if (die->die_parent)
15380         prune_unused_types_mark (die->die_parent, 0);
15381
15382       /* Mark any referenced nodes.  */
15383       prune_unused_types_walk_attribs (die);
15384
15385       /* If this node is a specification,
15386          also mark the definition, if it exists.  */
15387       if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
15388         prune_unused_types_mark (die->die_definition, 1);
15389     }
15390
15391   if (dokids && die->die_mark != 2)
15392     {
15393       /* We need to walk the children, but haven't done so yet.
15394          Remember that we've walked the kids.  */
15395       die->die_mark = 2;
15396
15397       /* If this is an array type, we need to make sure our
15398          kids get marked, even if they're types.  */
15399       if (die->die_tag == DW_TAG_array_type)
15400         FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
15401       else
15402         FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
15403     }
15404 }
15405
15406
15407 /* Walk the tree DIE and mark types that we actually use.  */
15408
15409 static void
15410 prune_unused_types_walk (dw_die_ref die)
15411 {
15412   dw_die_ref c;
15413
15414   /* Don't do anything if this node is already marked.  */
15415   if (die->die_mark)
15416     return;
15417
15418   switch (die->die_tag)
15419     {
15420     case DW_TAG_const_type:
15421     case DW_TAG_packed_type:
15422     case DW_TAG_pointer_type:
15423     case DW_TAG_reference_type:
15424     case DW_TAG_volatile_type:
15425     case DW_TAG_typedef:
15426     case DW_TAG_array_type:
15427     case DW_TAG_structure_type:
15428     case DW_TAG_union_type:
15429     case DW_TAG_class_type:
15430     case DW_TAG_interface_type:
15431     case DW_TAG_friend:
15432     case DW_TAG_variant_part:
15433     case DW_TAG_enumeration_type:
15434     case DW_TAG_subroutine_type:
15435     case DW_TAG_string_type:
15436     case DW_TAG_set_type:
15437     case DW_TAG_subrange_type:
15438     case DW_TAG_ptr_to_member_type:
15439     case DW_TAG_file_type:
15440       if (die->die_perennial_p)
15441         break;
15442
15443       /* It's a type node --- don't mark it.  */
15444       return;
15445
15446     default:
15447       /* Mark everything else.  */
15448       break;
15449   }
15450
15451   die->die_mark = 1;
15452
15453   /* Now, mark any dies referenced from here.  */
15454   prune_unused_types_walk_attribs (die);
15455
15456   /* Mark children.  */
15457   FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
15458 }
15459
15460 /* Increment the string counts on strings referred to from DIE's
15461    attributes.  */
15462
15463 static void
15464 prune_unused_types_update_strings (dw_die_ref die)
15465 {
15466   dw_attr_ref a;
15467   unsigned ix;
15468
15469   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
15470     if (AT_class (a) == dw_val_class_str)
15471       {
15472         struct indirect_string_node *s = a->dw_attr_val.v.val_str;
15473         s->refcount++;
15474         /* Avoid unnecessarily putting strings that are used less than
15475            twice in the hash table.  */
15476         if (s->refcount
15477             == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
15478           {
15479             void ** slot;
15480             slot = htab_find_slot_with_hash (debug_str_hash, s->str,
15481                                              htab_hash_string (s->str),
15482                                              INSERT);
15483             gcc_assert (*slot == NULL);
15484             *slot = s;
15485           }
15486       }
15487 }
15488
15489 /* Remove from the tree DIE any dies that aren't marked.  */
15490
15491 static void
15492 prune_unused_types_prune (dw_die_ref die)
15493 {
15494   dw_die_ref c;
15495
15496   gcc_assert (die->die_mark);
15497   prune_unused_types_update_strings (die);
15498
15499   if (! die->die_child)
15500     return;
15501
15502   c = die->die_child;
15503   do {
15504     dw_die_ref prev = c;
15505     for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
15506       if (c == die->die_child)
15507         {
15508           /* No marked children between 'prev' and the end of the list.  */
15509           if (prev == c)
15510             /* No marked children at all.  */
15511             die->die_child = NULL;
15512           else
15513             {
15514               prev->die_sib = c->die_sib;
15515               die->die_child = prev;
15516             }
15517           return;
15518         }
15519
15520     if (c != prev->die_sib)
15521       prev->die_sib = c;
15522     prune_unused_types_prune (c);
15523   } while (c != die->die_child);
15524 }
15525
15526
15527 /* Remove dies representing declarations that we never use.  */
15528
15529 static void
15530 prune_unused_types (void)
15531 {
15532   unsigned int i;
15533   limbo_die_node *node;
15534   pubname_ref pub;
15535
15536 #if ENABLE_ASSERT_CHECKING
15537   /* All the marks should already be clear.  */
15538   verify_marks_clear (comp_unit_die);
15539   for (node = limbo_die_list; node; node = node->next)
15540     verify_marks_clear (node->die);
15541 #endif /* ENABLE_ASSERT_CHECKING */
15542
15543   /* Set the mark on nodes that are actually used.  */
15544   prune_unused_types_walk (comp_unit_die);
15545   for (node = limbo_die_list; node; node = node->next)
15546     prune_unused_types_walk (node->die);
15547
15548   /* Also set the mark on nodes referenced from the
15549      pubname_table or arange_table.  */
15550   for (i = 0; VEC_iterate (pubname_entry, pubname_table, i, pub); i++)
15551     prune_unused_types_mark (pub->die, 1);
15552   for (i = 0; i < arange_table_in_use; i++)
15553     prune_unused_types_mark (arange_table[i], 1);
15554
15555   /* Get rid of nodes that aren't marked; and update the string counts.  */
15556   if (debug_str_hash)
15557     htab_empty (debug_str_hash);
15558   prune_unused_types_prune (comp_unit_die);
15559   for (node = limbo_die_list; node; node = node->next)
15560     prune_unused_types_prune (node->die);
15561
15562   /* Leave the marks clear.  */
15563   prune_unmark_dies (comp_unit_die);
15564   for (node = limbo_die_list; node; node = node->next)
15565     prune_unmark_dies (node->die);
15566 }
15567
15568 /* Set the parameter to true if there are any relative pathnames in
15569    the file table.  */
15570 static int
15571 file_table_relative_p (void ** slot, void *param)
15572 {
15573   bool *p = (bool *) param;
15574   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
15575   if (!IS_ABSOLUTE_PATH (d->filename))
15576     {
15577       *p = true;
15578       return 0;
15579     }
15580   return 1;
15581 }
15582
15583 /* Output stuff that dwarf requires at the end of every file,
15584    and generate the DWARF-2 debugging info.  */
15585
15586 static void
15587 dwarf2out_finish (const char *filename)
15588 {
15589   limbo_die_node *node, *next_node;
15590   dw_die_ref die = 0;
15591
15592   /* Add the name for the main input file now.  We delayed this from
15593      dwarf2out_init to avoid complications with PCH.  */
15594   add_name_attribute (comp_unit_die, remap_debug_filename (filename));
15595   if (!IS_ABSOLUTE_PATH (filename))
15596     add_comp_dir_attribute (comp_unit_die);
15597   else if (get_AT (comp_unit_die, DW_AT_comp_dir) == NULL)
15598     {
15599       bool p = false;
15600       htab_traverse (file_table, file_table_relative_p, &p);
15601       if (p)
15602         add_comp_dir_attribute (comp_unit_die);
15603     }
15604
15605   /* Traverse the limbo die list, and add parent/child links.  The only
15606      dies without parents that should be here are concrete instances of
15607      inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
15608      For concrete instances, we can get the parent die from the abstract
15609      instance.  */
15610   for (node = limbo_die_list; node; node = next_node)
15611     {
15612       next_node = node->next;
15613       die = node->die;
15614
15615       if (die->die_parent == NULL)
15616         {
15617           dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
15618
15619           if (origin)
15620             add_child_die (origin->die_parent, die);
15621           else if (die == comp_unit_die)
15622             ;
15623           else if (errorcount > 0 || sorrycount > 0)
15624             /* It's OK to be confused by errors in the input.  */
15625             add_child_die (comp_unit_die, die);
15626           else
15627             {
15628               /* In certain situations, the lexical block containing a
15629                  nested function can be optimized away, which results
15630                  in the nested function die being orphaned.  Likewise
15631                  with the return type of that nested function.  Force
15632                  this to be a child of the containing function.
15633
15634                  It may happen that even the containing function got fully
15635                  inlined and optimized out.  In that case we are lost and
15636                  assign the empty child.  This should not be big issue as
15637                  the function is likely unreachable too.  */
15638               tree context = NULL_TREE;
15639
15640               gcc_assert (node->created_for);
15641
15642               if (DECL_P (node->created_for))
15643                 context = DECL_CONTEXT (node->created_for);
15644               else if (TYPE_P (node->created_for))
15645                 context = TYPE_CONTEXT (node->created_for);
15646
15647               gcc_assert (context
15648                           && (TREE_CODE (context) == FUNCTION_DECL
15649                               || TREE_CODE (context) == NAMESPACE_DECL));
15650
15651               origin = lookup_decl_die (context);
15652               if (origin)
15653                 add_child_die (origin, die);
15654               else
15655                 add_child_die (comp_unit_die, die);
15656             }
15657         }
15658     }
15659
15660   limbo_die_list = NULL;
15661
15662   /* Walk through the list of incomplete types again, trying once more to
15663      emit full debugging info for them.  */
15664   retry_incomplete_types ();
15665
15666   if (flag_eliminate_unused_debug_types)
15667     prune_unused_types ();
15668
15669   /* Generate separate CUs for each of the include files we've seen.
15670      They will go into limbo_die_list.  */
15671   if (flag_eliminate_dwarf2_dups)
15672     break_out_includes (comp_unit_die);
15673
15674   /* Traverse the DIE's and add add sibling attributes to those DIE's
15675      that have children.  */
15676   add_sibling_attributes (comp_unit_die);
15677   for (node = limbo_die_list; node; node = node->next)
15678     add_sibling_attributes (node->die);
15679
15680   /* Output a terminator label for the .text section.  */
15681   switch_to_section (text_section);
15682   targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
15683   if (flag_reorder_blocks_and_partition)
15684     {
15685       switch_to_section (unlikely_text_section ());
15686       targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
15687     }
15688
15689   /* We can only use the low/high_pc attributes if all of the code was
15690      in .text.  */
15691   if (!have_multiple_function_sections)
15692     {
15693       add_AT_lbl_id (comp_unit_die, DW_AT_low_pc, text_section_label);
15694       add_AT_lbl_id (comp_unit_die, DW_AT_high_pc, text_end_label);
15695     }
15696
15697   else
15698     {
15699       unsigned fde_idx = 0;
15700
15701       /* We need to give .debug_loc and .debug_ranges an appropriate
15702          "base address".  Use zero so that these addresses become
15703          absolute.  Historically, we've emitted the unexpected
15704          DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
15705          Emit both to give time for other tools to adapt.  */
15706       add_AT_addr (comp_unit_die, DW_AT_low_pc, const0_rtx);
15707       add_AT_addr (comp_unit_die, DW_AT_entry_pc, const0_rtx);
15708
15709       add_AT_range_list (comp_unit_die, DW_AT_ranges,
15710                          add_ranges_by_labels (text_section_label,
15711                                                text_end_label));
15712       if (flag_reorder_blocks_and_partition)
15713         add_ranges_by_labels (cold_text_section_label,
15714                               cold_end_label);
15715
15716       for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
15717         {
15718           dw_fde_ref fde = &fde_table[fde_idx];
15719
15720           if (fde->dw_fde_switched_sections)
15721             {
15722               add_ranges_by_labels (fde->dw_fde_hot_section_label,
15723                                     fde->dw_fde_hot_section_end_label);
15724               add_ranges_by_labels (fde->dw_fde_unlikely_section_label,
15725                                     fde->dw_fde_unlikely_section_end_label);
15726             }
15727           else
15728             add_ranges_by_labels (fde->dw_fde_begin,
15729                                   fde->dw_fde_end);
15730         }
15731
15732       add_ranges (NULL);
15733     }
15734
15735   /* Output location list section if necessary.  */
15736   if (have_location_lists)
15737     {
15738       /* Output the location lists info.  */
15739       switch_to_section (debug_loc_section);
15740       ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
15741                                    DEBUG_LOC_SECTION_LABEL, 0);
15742       ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
15743       output_location_lists (die);
15744     }
15745
15746   if (debug_info_level >= DINFO_LEVEL_NORMAL)
15747     add_AT_lineptr (comp_unit_die, DW_AT_stmt_list,
15748                     debug_line_section_label);
15749
15750   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
15751     add_AT_macptr (comp_unit_die, DW_AT_macro_info, macinfo_section_label);
15752
15753   /* Output all of the compilation units.  We put the main one last so that
15754      the offsets are available to output_pubnames.  */
15755   for (node = limbo_die_list; node; node = node->next)
15756     output_comp_unit (node->die, 0);
15757
15758   output_comp_unit (comp_unit_die, 0);
15759
15760   /* Output the abbreviation table.  */
15761   switch_to_section (debug_abbrev_section);
15762   output_abbrev_section ();
15763
15764   /* Output public names table if necessary.  */
15765   if (!VEC_empty (pubname_entry, pubname_table))
15766     {
15767       switch_to_section (debug_pubnames_section);
15768       output_pubnames (pubname_table);
15769     }
15770
15771 #ifdef DEBUG_PUBTYPES_SECTION
15772   /* Output public types table if necessary.  */
15773   if (!VEC_empty (pubname_entry, pubtype_table))
15774     {
15775       switch_to_section (debug_pubtypes_section);
15776       output_pubnames (pubtype_table);
15777     }
15778 #endif
15779
15780   /* Output the address range information.  We only put functions in the arange
15781      table, so don't write it out if we don't have any.  */
15782   if (fde_table_in_use)
15783     {
15784       switch_to_section (debug_aranges_section);
15785       output_aranges ();
15786     }
15787
15788   /* Output ranges section if necessary.  */
15789   if (ranges_table_in_use)
15790     {
15791       switch_to_section (debug_ranges_section);
15792       ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
15793       output_ranges ();
15794     }
15795
15796   /* Output the source line correspondence table.  We must do this
15797      even if there is no line information.  Otherwise, on an empty
15798      translation unit, we will generate a present, but empty,
15799      .debug_info section.  IRIX 6.5 `nm' will then complain when
15800      examining the file.  This is done late so that any filenames
15801      used by the debug_info section are marked as 'used'.  */
15802   if (! DWARF2_ASM_LINE_DEBUG_INFO)
15803     {
15804       switch_to_section (debug_line_section);
15805       output_line_info ();
15806     }
15807
15808   /* Have to end the macro section.  */
15809   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
15810     {
15811       switch_to_section (debug_macinfo_section);
15812       dw2_asm_output_data (1, 0, "End compilation unit");
15813     }
15814
15815   /* If we emitted any DW_FORM_strp form attribute, output the string
15816      table too.  */
15817   if (debug_str_hash)
15818     htab_traverse (debug_str_hash, output_indirect_string, NULL);
15819 }
15820 #else
15821
15822 /* This should never be used, but its address is needed for comparisons.  */
15823 const struct gcc_debug_hooks dwarf2_debug_hooks;
15824
15825 #endif /* DWARF2_DEBUGGING_INFO */
15826
15827 #include "gt-dwarf2out.h"