Import gcc-4.1.2.
[dragonfly.git] / contrib / gcc-4.1 / 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 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 2, 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 COPYING.  If not, write to the Free
22 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
23 02110-1301, USA.  */
24
25 /* TODO: Emit .debug_line header even when there are no functions, since
26            the file numbers are used by .debug_info.  Alternately, leave
27            out locations for types and decls.
28          Avoid talking about ctors and op= for PODs.
29          Factor out common prologue sequences into multiple CIEs.  */
30
31 /* The first part of this file deals with the DWARF 2 frame unwind
32    information, which is also used by the GCC efficient exception handling
33    mechanism.  The second part, controlled only by an #ifdef
34    DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
35    information.  */
36
37 #include "config.h"
38 #include "system.h"
39 #include "coretypes.h"
40 #include "tm.h"
41 #include "tree.h"
42 #include "version.h"
43 #include "flags.h"
44 #include "real.h"
45 #include "rtl.h"
46 #include "hard-reg-set.h"
47 #include "regs.h"
48 #include "insn-config.h"
49 #include "reload.h"
50 #include "function.h"
51 #include "output.h"
52 #include "expr.h"
53 #include "libfuncs.h"
54 #include "except.h"
55 #include "dwarf2.h"
56 #include "dwarf2out.h"
57 #include "dwarf2asm.h"
58 #include "toplev.h"
59 #include "varray.h"
60 #include "ggc.h"
61 #include "md5.h"
62 #include "tm_p.h"
63 #include "diagnostic.h"
64 #include "debug.h"
65 #include "target.h"
66 #include "langhooks.h"
67 #include "hashtab.h"
68 #include "cgraph.h"
69 #include "input.h"
70
71 #ifdef DWARF2_DEBUGGING_INFO
72 static void dwarf2out_source_line (unsigned int, const char *);
73 #endif
74
75 /* DWARF2 Abbreviation Glossary:
76    CFA = Canonical Frame Address
77            a fixed address on the stack which identifies a call frame.
78            We define it to be the value of SP just before the call insn.
79            The CFA register and offset, which may change during the course
80            of the function, are used to calculate its value at runtime.
81    CFI = Call Frame Instruction
82            an instruction for the DWARF2 abstract machine
83    CIE = Common Information Entry
84            information describing information common to one or more FDEs
85    DIE = Debugging Information Entry
86    FDE = Frame Description Entry
87            information describing the stack call frame, in particular,
88            how to restore registers
89
90    DW_CFA_... = DWARF2 CFA call frame instruction
91    DW_TAG_... = DWARF2 DIE tag */
92
93 #ifndef DWARF2_FRAME_INFO
94 # ifdef DWARF2_DEBUGGING_INFO
95 #  define DWARF2_FRAME_INFO \
96   (write_symbols == DWARF2_DEBUG || write_symbols == VMS_AND_DWARF2_DEBUG)
97 # else
98 #  define DWARF2_FRAME_INFO 0
99 # endif
100 #endif
101
102 /* Decide whether we want to emit frame unwind information for the current
103    translation unit.  */
104
105 int
106 dwarf2out_do_frame (void)
107 {
108   /* We want to emit correct CFA location expressions or lists, so we
109      have to return true if we're going to output debug info, even if
110      we're not going to output frame or unwind info.  */
111   return (write_symbols == DWARF2_DEBUG
112           || write_symbols == VMS_AND_DWARF2_DEBUG
113           || DWARF2_FRAME_INFO
114 #ifdef DWARF2_UNWIND_INFO
115           || (DWARF2_UNWIND_INFO
116               && (flag_unwind_tables
117                   || (flag_exceptions && ! USING_SJLJ_EXCEPTIONS)))
118 #endif
119           );
120 }
121
122 /* The size of the target's pointer type.  */
123 #ifndef PTR_SIZE
124 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
125 #endif
126
127 /* Various versions of targetm.eh_frame_section.  Note these must appear
128    outside the DWARF2_DEBUGGING_INFO || DWARF2_UNWIND_INFO macro guards.  */
129
130 /* Version of targetm.eh_frame_section for systems with named sections.  */
131 void
132 named_section_eh_frame_section (void)
133 {
134 #ifdef EH_FRAME_SECTION_NAME
135   int flags;
136
137   if (EH_TABLES_CAN_BE_READ_ONLY)
138     {
139       int fde_encoding;
140       int per_encoding;
141       int lsda_encoding;
142
143       fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
144       per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
145       lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
146       flags = (! flag_pic
147                || ((fde_encoding & 0x70) != DW_EH_PE_absptr
148                    && (fde_encoding & 0x70) != DW_EH_PE_aligned
149                    && (per_encoding & 0x70) != DW_EH_PE_absptr
150                    && (per_encoding & 0x70) != DW_EH_PE_aligned
151                    && (lsda_encoding & 0x70) != DW_EH_PE_absptr
152                    && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
153               ? 0 : SECTION_WRITE;
154     }
155   else
156     flags = SECTION_WRITE;
157   named_section_flags (EH_FRAME_SECTION_NAME, flags);
158 #endif
159 }
160
161 /* Version of targetm.eh_frame_section for systems using collect2.  */
162 void
163 collect2_eh_frame_section (void)
164 {
165   tree label = get_file_function_name ('F');
166
167   data_section ();
168   ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
169   targetm.asm_out.globalize_label (asm_out_file, IDENTIFIER_POINTER (label));
170   ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
171 }
172
173 /* Default version of targetm.eh_frame_section.  */
174 void
175 default_eh_frame_section (void)
176 {
177 #ifdef EH_FRAME_SECTION_NAME
178   named_section_eh_frame_section ();
179 #else
180   collect2_eh_frame_section ();
181 #endif
182 }
183
184 DEF_VEC_P(rtx);
185 DEF_VEC_ALLOC_P(rtx,gc);
186
187 /* Array of RTXes referenced by the debugging information, which therefore
188    must be kept around forever.  */
189 static GTY(()) VEC(rtx,gc) *used_rtx_array;
190
191 /* A pointer to the base of a list of incomplete types which might be
192    completed at some later time.  incomplete_types_list needs to be a
193    VEC(tree,gc) because we want to tell the garbage collector about
194    it.  */
195 static GTY(()) VEC(tree,gc) *incomplete_types;
196
197 /* A pointer to the base of a table of references to declaration
198    scopes.  This table is a display which tracks the nesting
199    of declaration scopes at the current scope and containing
200    scopes.  This table is used to find the proper place to
201    define type declaration DIE's.  */
202 static GTY(()) VEC(tree,gc) *decl_scope_table;
203
204 /* How to start an assembler comment.  */
205 #ifndef ASM_COMMENT_START
206 #define ASM_COMMENT_START ";#"
207 #endif
208
209 typedef struct dw_cfi_struct *dw_cfi_ref;
210 typedef struct dw_fde_struct *dw_fde_ref;
211 typedef union  dw_cfi_oprnd_struct *dw_cfi_oprnd_ref;
212
213 /* Call frames are described using a sequence of Call Frame
214    Information instructions.  The register number, offset
215    and address fields are provided as possible operands;
216    their use is selected by the opcode field.  */
217
218 enum dw_cfi_oprnd_type {
219   dw_cfi_oprnd_unused,
220   dw_cfi_oprnd_reg_num,
221   dw_cfi_oprnd_offset,
222   dw_cfi_oprnd_addr,
223   dw_cfi_oprnd_loc
224 };
225
226 typedef union dw_cfi_oprnd_struct GTY(())
227 {
228   unsigned int GTY ((tag ("dw_cfi_oprnd_reg_num"))) dw_cfi_reg_num;
229   HOST_WIDE_INT GTY ((tag ("dw_cfi_oprnd_offset"))) dw_cfi_offset;
230   const char * GTY ((tag ("dw_cfi_oprnd_addr"))) dw_cfi_addr;
231   struct dw_loc_descr_struct * GTY ((tag ("dw_cfi_oprnd_loc"))) dw_cfi_loc;
232 }
233 dw_cfi_oprnd;
234
235 typedef struct dw_cfi_struct GTY(())
236 {
237   dw_cfi_ref dw_cfi_next;
238   enum dwarf_call_frame_info dw_cfi_opc;
239   dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd1_desc (%1.dw_cfi_opc)")))
240     dw_cfi_oprnd1;
241   dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd2_desc (%1.dw_cfi_opc)")))
242     dw_cfi_oprnd2;
243 }
244 dw_cfi_node;
245
246 /* This is how we define the location of the CFA. We use to handle it
247    as REG + OFFSET all the time,  but now it can be more complex.
248    It can now be either REG + CFA_OFFSET or *(REG + BASE_OFFSET) + CFA_OFFSET.
249    Instead of passing around REG and OFFSET, we pass a copy
250    of this structure.  */
251 typedef struct cfa_loc GTY(())
252 {
253   HOST_WIDE_INT offset;
254   HOST_WIDE_INT base_offset;
255   unsigned int reg;
256   int indirect;            /* 1 if CFA is accessed via a dereference.  */
257 } dw_cfa_location;
258
259 /* All call frame descriptions (FDE's) in the GCC generated DWARF
260    refer to a single Common Information Entry (CIE), defined at
261    the beginning of the .debug_frame section.  This use of a single
262    CIE obviates the need to keep track of multiple CIE's
263    in the DWARF generation routines below.  */
264
265 typedef struct dw_fde_struct GTY(())
266 {
267   tree decl;
268   const char *dw_fde_begin;
269   const char *dw_fde_current_label;
270   const char *dw_fde_end;
271   const char *dw_fde_hot_section_label;
272   const char *dw_fde_hot_section_end_label;
273   const char *dw_fde_unlikely_section_label;
274   const char *dw_fde_unlikely_section_end_label;
275   bool dw_fde_switched_sections;
276   dw_cfi_ref dw_fde_cfi;
277   unsigned funcdef_number;
278   unsigned all_throwers_are_sibcalls : 1;
279   unsigned nothrow : 1;
280   unsigned uses_eh_lsda : 1;
281 }
282 dw_fde_node;
283
284 /* Maximum size (in bytes) of an artificially generated label.  */
285 #define MAX_ARTIFICIAL_LABEL_BYTES      30
286
287 /* The size of addresses as they appear in the Dwarf 2 data.
288    Some architectures use word addresses to refer to code locations,
289    but Dwarf 2 info always uses byte addresses.  On such machines,
290    Dwarf 2 addresses need to be larger than the architecture's
291    pointers.  */
292 #ifndef DWARF2_ADDR_SIZE
293 #define DWARF2_ADDR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
294 #endif
295
296 /* The size in bytes of a DWARF field indicating an offset or length
297    relative to a debug info section, specified to be 4 bytes in the
298    DWARF-2 specification.  The SGI/MIPS ABI defines it to be the same
299    as PTR_SIZE.  */
300
301 #ifndef DWARF_OFFSET_SIZE
302 #define DWARF_OFFSET_SIZE 4
303 #endif
304
305 /* According to the (draft) DWARF 3 specification, the initial length
306    should either be 4 or 12 bytes.  When it's 12 bytes, the first 4
307    bytes are 0xffffffff, followed by the length stored in the next 8
308    bytes.
309
310    However, the SGI/MIPS ABI uses an initial length which is equal to
311    DWARF_OFFSET_SIZE.  It is defined (elsewhere) accordingly.  */
312
313 #ifndef DWARF_INITIAL_LENGTH_SIZE
314 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
315 #endif
316
317 #define DWARF_VERSION 2
318
319 /* Round SIZE up to the nearest BOUNDARY.  */
320 #define DWARF_ROUND(SIZE,BOUNDARY) \
321   ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
322
323 /* Offsets recorded in opcodes are a multiple of this alignment factor.  */
324 #ifndef DWARF_CIE_DATA_ALIGNMENT
325 #ifdef STACK_GROWS_DOWNWARD
326 #define DWARF_CIE_DATA_ALIGNMENT (-((int) UNITS_PER_WORD))
327 #else
328 #define DWARF_CIE_DATA_ALIGNMENT ((int) UNITS_PER_WORD)
329 #endif
330 #endif
331
332 /* A pointer to the base of a table that contains frame description
333    information for each routine.  */
334 static GTY((length ("fde_table_allocated"))) dw_fde_ref fde_table;
335
336 /* Number of elements currently allocated for fde_table.  */
337 static GTY(()) unsigned fde_table_allocated;
338
339 /* Number of elements in fde_table currently in use.  */
340 static GTY(()) unsigned fde_table_in_use;
341
342 /* Size (in elements) of increments by which we may expand the
343    fde_table.  */
344 #define FDE_TABLE_INCREMENT 256
345
346 /* A list of call frame insns for the CIE.  */
347 static GTY(()) dw_cfi_ref cie_cfi_head;
348
349 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
350 /* Some DWARF extensions (e.g., MIPS/SGI) implement a subprogram
351    attribute that accelerates the lookup of the FDE associated
352    with the subprogram.  This variable holds the table index of the FDE
353    associated with the current function (body) definition.  */
354 static unsigned current_funcdef_fde;
355 #endif
356
357 struct indirect_string_node GTY(())
358 {
359   const char *str;
360   unsigned int refcount;
361   unsigned int form;
362   char *label;
363 };
364
365 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
366
367 static GTY(()) int dw2_string_counter;
368 static GTY(()) unsigned long dwarf2out_cfi_label_num;
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 static void initial_return_save (rtx);
383 static HOST_WIDE_INT stack_adjust_offset (rtx);
384 static void output_cfi (dw_cfi_ref, dw_fde_ref, int);
385 static void output_call_frame_info (int);
386 static void dwarf2out_stack_adjust (rtx, bool);
387 static void flush_queued_reg_saves (void);
388 static bool clobbers_queued_reg_save (rtx);
389 static void dwarf2out_frame_debug_expr (rtx, const char *);
390
391 /* Support for complex CFA locations.  */
392 static void output_cfa_loc (dw_cfi_ref);
393 static void get_cfa_from_loc_descr (dw_cfa_location *,
394                                     struct dw_loc_descr_struct *);
395 static struct dw_loc_descr_struct *build_cfa_loc
396  (dw_cfa_location *);
397 static void def_cfa_1 (const char *, dw_cfa_location *);
398
399 /* How to start an assembler comment.  */
400 #ifndef ASM_COMMENT_START
401 #define ASM_COMMENT_START ";#"
402 #endif
403
404 /* Data and reference forms for relocatable data.  */
405 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
406 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
407
408 #ifndef DEBUG_FRAME_SECTION
409 #define DEBUG_FRAME_SECTION     ".debug_frame"
410 #endif
411
412 #ifndef FUNC_BEGIN_LABEL
413 #define FUNC_BEGIN_LABEL        "LFB"
414 #endif
415
416 #ifndef FUNC_END_LABEL
417 #define FUNC_END_LABEL          "LFE"
418 #endif
419
420 #ifndef FRAME_BEGIN_LABEL
421 #define FRAME_BEGIN_LABEL       "Lframe"
422 #endif
423 #define CIE_AFTER_SIZE_LABEL    "LSCIE"
424 #define CIE_END_LABEL           "LECIE"
425 #define FDE_LABEL               "LSFDE"
426 #define FDE_AFTER_SIZE_LABEL    "LASFDE"
427 #define FDE_END_LABEL           "LEFDE"
428 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
429 #define LINE_NUMBER_END_LABEL   "LELT"
430 #define LN_PROLOG_AS_LABEL      "LASLTP"
431 #define LN_PROLOG_END_LABEL     "LELTP"
432 #define DIE_LABEL_PREFIX        "DW"
433
434 /* The DWARF 2 CFA column which tracks the return address.  Normally this
435    is the column for PC, or the first column after all of the hard
436    registers.  */
437 #ifndef DWARF_FRAME_RETURN_COLUMN
438 #ifdef PC_REGNUM
439 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGNUM (PC_REGNUM)
440 #else
441 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGISTERS
442 #endif
443 #endif
444
445 /* The mapping from gcc register number to DWARF 2 CFA column number.  By
446    default, we just provide columns for all registers.  */
447 #ifndef DWARF_FRAME_REGNUM
448 #define DWARF_FRAME_REGNUM(REG) DBX_REGISTER_NUMBER (REG)
449 #endif
450 \f
451 /* Hook used by __throw.  */
452
453 rtx
454 expand_builtin_dwarf_sp_column (void)
455 {
456   return GEN_INT (DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM));
457 }
458
459 /* Return a pointer to a copy of the section string name S with all
460    attributes stripped off, and an asterisk prepended (for assemble_name).  */
461
462 static inline char *
463 stripattributes (const char *s)
464 {
465   char *stripped = xmalloc (strlen (s) + 2);
466   char *p = stripped;
467
468   *p++ = '*';
469
470   while (*s && *s != ',')
471     *p++ = *s++;
472
473   *p = '\0';
474   return stripped;
475 }
476
477 /* Generate code to initialize the register size table.  */
478
479 void
480 expand_builtin_init_dwarf_reg_sizes (tree address)
481 {
482   int i;
483   enum machine_mode mode = TYPE_MODE (char_type_node);
484   rtx addr = expand_expr (address, NULL_RTX, VOIDmode, 0);
485   rtx mem = gen_rtx_MEM (BLKmode, addr);
486   bool wrote_return_column = false;
487
488   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
489     if (DWARF_FRAME_REGNUM (i) < DWARF_FRAME_REGISTERS)
490       {
491         HOST_WIDE_INT offset = DWARF_FRAME_REGNUM (i) * GET_MODE_SIZE (mode);
492         enum machine_mode save_mode = reg_raw_mode[i];
493         HOST_WIDE_INT size;
494
495         if (HARD_REGNO_CALL_PART_CLOBBERED (i, save_mode))
496           save_mode = choose_hard_reg_mode (i, 1, true);
497         if (DWARF_FRAME_REGNUM (i) == DWARF_FRAME_RETURN_COLUMN)
498           {
499             if (save_mode == VOIDmode)
500               continue;
501             wrote_return_column = true;
502           }
503         size = GET_MODE_SIZE (save_mode);
504         if (offset < 0)
505           continue;
506
507         emit_move_insn (adjust_address (mem, mode, offset),
508                         gen_int_mode (size, mode));
509       }
510
511 #ifdef DWARF_ALT_FRAME_RETURN_COLUMN
512   gcc_assert (wrote_return_column);
513   i = DWARF_ALT_FRAME_RETURN_COLUMN;
514   wrote_return_column = false;
515 #else
516   i = DWARF_FRAME_RETURN_COLUMN;
517 #endif
518
519   if (! wrote_return_column)
520     {
521       enum machine_mode save_mode = Pmode;
522       HOST_WIDE_INT offset = i * GET_MODE_SIZE (mode);
523       HOST_WIDE_INT size = GET_MODE_SIZE (save_mode);
524       emit_move_insn (adjust_address (mem, mode, offset), GEN_INT (size));
525     }
526 }
527
528 /* Convert a DWARF call frame info. operation to its string name */
529
530 static const char *
531 dwarf_cfi_name (unsigned int cfi_opc)
532 {
533   switch (cfi_opc)
534     {
535     case DW_CFA_advance_loc:
536       return "DW_CFA_advance_loc";
537     case DW_CFA_offset:
538       return "DW_CFA_offset";
539     case DW_CFA_restore:
540       return "DW_CFA_restore";
541     case DW_CFA_nop:
542       return "DW_CFA_nop";
543     case DW_CFA_set_loc:
544       return "DW_CFA_set_loc";
545     case DW_CFA_advance_loc1:
546       return "DW_CFA_advance_loc1";
547     case DW_CFA_advance_loc2:
548       return "DW_CFA_advance_loc2";
549     case DW_CFA_advance_loc4:
550       return "DW_CFA_advance_loc4";
551     case DW_CFA_offset_extended:
552       return "DW_CFA_offset_extended";
553     case DW_CFA_restore_extended:
554       return "DW_CFA_restore_extended";
555     case DW_CFA_undefined:
556       return "DW_CFA_undefined";
557     case DW_CFA_same_value:
558       return "DW_CFA_same_value";
559     case DW_CFA_register:
560       return "DW_CFA_register";
561     case DW_CFA_remember_state:
562       return "DW_CFA_remember_state";
563     case DW_CFA_restore_state:
564       return "DW_CFA_restore_state";
565     case DW_CFA_def_cfa:
566       return "DW_CFA_def_cfa";
567     case DW_CFA_def_cfa_register:
568       return "DW_CFA_def_cfa_register";
569     case DW_CFA_def_cfa_offset:
570       return "DW_CFA_def_cfa_offset";
571
572     /* DWARF 3 */
573     case DW_CFA_def_cfa_expression:
574       return "DW_CFA_def_cfa_expression";
575     case DW_CFA_expression:
576       return "DW_CFA_expression";
577     case DW_CFA_offset_extended_sf:
578       return "DW_CFA_offset_extended_sf";
579     case DW_CFA_def_cfa_sf:
580       return "DW_CFA_def_cfa_sf";
581     case DW_CFA_def_cfa_offset_sf:
582       return "DW_CFA_def_cfa_offset_sf";
583
584     /* SGI/MIPS specific */
585     case DW_CFA_MIPS_advance_loc8:
586       return "DW_CFA_MIPS_advance_loc8";
587
588     /* GNU extensions */
589     case DW_CFA_GNU_window_save:
590       return "DW_CFA_GNU_window_save";
591     case DW_CFA_GNU_args_size:
592       return "DW_CFA_GNU_args_size";
593     case DW_CFA_GNU_negative_offset_extended:
594       return "DW_CFA_GNU_negative_offset_extended";
595
596     default:
597       return "DW_CFA_<unknown>";
598     }
599 }
600
601 /* Return a pointer to a newly allocated Call Frame Instruction.  */
602
603 static inline dw_cfi_ref
604 new_cfi (void)
605 {
606   dw_cfi_ref cfi = ggc_alloc (sizeof (dw_cfi_node));
607
608   cfi->dw_cfi_next = NULL;
609   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = 0;
610   cfi->dw_cfi_oprnd2.dw_cfi_reg_num = 0;
611
612   return cfi;
613 }
614
615 /* Add a Call Frame Instruction to list of instructions.  */
616
617 static inline void
618 add_cfi (dw_cfi_ref *list_head, dw_cfi_ref cfi)
619 {
620   dw_cfi_ref *p;
621
622   /* Find the end of the chain.  */
623   for (p = list_head; (*p) != NULL; p = &(*p)->dw_cfi_next)
624     ;
625
626   *p = cfi;
627 }
628
629 /* Generate a new label for the CFI info to refer to.  */
630
631 char *
632 dwarf2out_cfi_label (void)
633 {
634   static char label[20];
635
636   ASM_GENERATE_INTERNAL_LABEL (label, "LCFI", dwarf2out_cfi_label_num++);
637   ASM_OUTPUT_LABEL (asm_out_file, label);
638   return label;
639 }
640
641 /* Add CFI to the current fde at the PC value indicated by LABEL if specified,
642    or to the CIE if LABEL is NULL.  */
643
644 static void
645 add_fde_cfi (const char *label, dw_cfi_ref cfi)
646 {
647   if (label)
648     {
649       dw_fde_ref fde = &fde_table[fde_table_in_use - 1];
650
651       if (*label == 0)
652         label = dwarf2out_cfi_label ();
653
654       if (fde->dw_fde_current_label == NULL
655           || strcmp (label, fde->dw_fde_current_label) != 0)
656         {
657           dw_cfi_ref xcfi;
658
659           label = xstrdup (label);
660
661           /* Set the location counter to the new label.  */
662           xcfi = new_cfi ();
663           /* If we have a current label, advance from there, otherwise
664              set the location directly using set_loc.  */
665           xcfi->dw_cfi_opc = fde->dw_fde_current_label
666                              ? DW_CFA_advance_loc4
667                              : DW_CFA_set_loc;
668           xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
669           add_cfi (&fde->dw_fde_cfi, xcfi);
670
671           fde->dw_fde_current_label = label;
672         }
673
674       add_cfi (&fde->dw_fde_cfi, cfi);
675     }
676
677   else
678     add_cfi (&cie_cfi_head, cfi);
679 }
680
681 /* Subroutine of lookup_cfa.  */
682
683 static void
684 lookup_cfa_1 (dw_cfi_ref cfi, dw_cfa_location *loc)
685 {
686   switch (cfi->dw_cfi_opc)
687     {
688     case DW_CFA_def_cfa_offset:
689       loc->offset = cfi->dw_cfi_oprnd1.dw_cfi_offset;
690       break;
691     case DW_CFA_def_cfa_offset_sf:
692       loc->offset
693         = cfi->dw_cfi_oprnd1.dw_cfi_offset * DWARF_CIE_DATA_ALIGNMENT;
694       break;
695     case DW_CFA_def_cfa_register:
696       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
697       break;
698     case DW_CFA_def_cfa:
699       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
700       loc->offset = cfi->dw_cfi_oprnd2.dw_cfi_offset;
701       break;
702     case DW_CFA_def_cfa_sf:
703       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
704       loc->offset
705         = cfi->dw_cfi_oprnd2.dw_cfi_offset * DWARF_CIE_DATA_ALIGNMENT;
706       break;
707     case DW_CFA_def_cfa_expression:
708       get_cfa_from_loc_descr (loc, cfi->dw_cfi_oprnd1.dw_cfi_loc);
709       break;
710     default:
711       break;
712     }
713 }
714
715 /* Find the previous value for the CFA.  */
716
717 static void
718 lookup_cfa (dw_cfa_location *loc)
719 {
720   dw_cfi_ref cfi;
721
722   loc->reg = INVALID_REGNUM;
723   loc->offset = 0;
724   loc->indirect = 0;
725   loc->base_offset = 0;
726
727   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
728     lookup_cfa_1 (cfi, loc);
729
730   if (fde_table_in_use)
731     {
732       dw_fde_ref fde = &fde_table[fde_table_in_use - 1];
733       for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
734         lookup_cfa_1 (cfi, loc);
735     }
736 }
737
738 /* The current rule for calculating the DWARF2 canonical frame address.  */
739 static dw_cfa_location cfa;
740
741 /* The register used for saving registers to the stack, and its offset
742    from the CFA.  */
743 static dw_cfa_location cfa_store;
744
745 /* The running total of the size of arguments pushed onto the stack.  */
746 static HOST_WIDE_INT args_size;
747
748 /* The last args_size we actually output.  */
749 static HOST_WIDE_INT old_args_size;
750
751 /* Entry point to update the canonical frame address (CFA).
752    LABEL is passed to add_fde_cfi.  The value of CFA is now to be
753    calculated from REG+OFFSET.  */
754
755 void
756 dwarf2out_def_cfa (const char *label, unsigned int reg, HOST_WIDE_INT offset)
757 {
758   dw_cfa_location loc;
759   loc.indirect = 0;
760   loc.base_offset = 0;
761   loc.reg = reg;
762   loc.offset = offset;
763   def_cfa_1 (label, &loc);
764 }
765
766 /* Determine if two dw_cfa_location structures define the same data.  */
767
768 static bool
769 cfa_equal_p (const dw_cfa_location *loc1, const dw_cfa_location *loc2)
770 {
771   return (loc1->reg == loc2->reg
772           && loc1->offset == loc2->offset
773           && loc1->indirect == loc2->indirect
774           && (loc1->indirect == 0
775               || loc1->base_offset == loc2->base_offset));
776 }
777
778 /* This routine does the actual work.  The CFA is now calculated from
779    the dw_cfa_location structure.  */
780
781 static void
782 def_cfa_1 (const char *label, dw_cfa_location *loc_p)
783 {
784   dw_cfi_ref cfi;
785   dw_cfa_location old_cfa, loc;
786
787   cfa = *loc_p;
788   loc = *loc_p;
789
790   if (cfa_store.reg == loc.reg && loc.indirect == 0)
791     cfa_store.offset = loc.offset;
792
793   loc.reg = DWARF_FRAME_REGNUM (loc.reg);
794   lookup_cfa (&old_cfa);
795
796   /* If nothing changed, no need to issue any call frame instructions.  */
797   if (cfa_equal_p (&loc, &old_cfa))
798     return;
799
800   cfi = new_cfi ();
801
802   if (loc.reg == old_cfa.reg && !loc.indirect)
803     {
804       /* Construct a "DW_CFA_def_cfa_offset <offset>" instruction, indicating
805          the CFA register did not change but the offset did.  */
806       if (loc.offset < 0)
807         {
808           HOST_WIDE_INT f_offset = loc.offset / DWARF_CIE_DATA_ALIGNMENT;
809           gcc_assert (f_offset * DWARF_CIE_DATA_ALIGNMENT == loc.offset);
810
811           cfi->dw_cfi_opc = DW_CFA_def_cfa_offset_sf;
812           cfi->dw_cfi_oprnd1.dw_cfi_offset = f_offset;
813         }
814       else
815         {
816           cfi->dw_cfi_opc = DW_CFA_def_cfa_offset;
817           cfi->dw_cfi_oprnd1.dw_cfi_offset = loc.offset;
818         }
819     }
820
821 #ifndef MIPS_DEBUGGING_INFO  /* SGI dbx thinks this means no offset.  */
822   else if (loc.offset == old_cfa.offset
823            && old_cfa.reg != INVALID_REGNUM
824            && !loc.indirect)
825     {
826       /* Construct a "DW_CFA_def_cfa_register <register>" instruction,
827          indicating the CFA register has changed to <register> but the
828          offset has not changed.  */
829       cfi->dw_cfi_opc = DW_CFA_def_cfa_register;
830       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
831     }
832 #endif
833
834   else if (loc.indirect == 0)
835     {
836       /* Construct a "DW_CFA_def_cfa <register> <offset>" instruction,
837          indicating the CFA register has changed to <register> with
838          the specified offset.  */
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_sf;
845           cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
846           cfi->dw_cfi_oprnd2.dw_cfi_offset = f_offset;
847         }
848       else
849         {
850           cfi->dw_cfi_opc = DW_CFA_def_cfa;
851           cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
852           cfi->dw_cfi_oprnd2.dw_cfi_offset = loc.offset;
853         }
854     }
855   else
856     {
857       /* Construct a DW_CFA_def_cfa_expression instruction to
858          calculate the CFA using a full location expression since no
859          register-offset pair is available.  */
860       struct dw_loc_descr_struct *loc_list;
861
862       cfi->dw_cfi_opc = DW_CFA_def_cfa_expression;
863       loc_list = build_cfa_loc (&loc);
864       cfi->dw_cfi_oprnd1.dw_cfi_loc = loc_list;
865     }
866
867   add_fde_cfi (label, cfi);
868 }
869
870 /* Add the CFI for saving a register.  REG is the CFA column number.
871    LABEL is passed to add_fde_cfi.
872    If SREG is -1, the register is saved at OFFSET from the CFA;
873    otherwise it is saved in SREG.  */
874
875 static void
876 reg_save (const char *label, unsigned int reg, unsigned int sreg, HOST_WIDE_INT offset)
877 {
878   dw_cfi_ref cfi = new_cfi ();
879
880   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
881
882   if (sreg == INVALID_REGNUM)
883     {
884       if (reg & ~0x3f)
885         /* The register number won't fit in 6 bits, so we have to use
886            the long form.  */
887         cfi->dw_cfi_opc = DW_CFA_offset_extended;
888       else
889         cfi->dw_cfi_opc = DW_CFA_offset;
890
891 #ifdef ENABLE_CHECKING
892       {
893         /* If we get an offset that is not a multiple of
894            DWARF_CIE_DATA_ALIGNMENT, there is either a bug in the
895            definition of DWARF_CIE_DATA_ALIGNMENT, or a bug in the machine
896            description.  */
897         HOST_WIDE_INT check_offset = offset / DWARF_CIE_DATA_ALIGNMENT;
898
899         gcc_assert (check_offset * DWARF_CIE_DATA_ALIGNMENT == offset);
900       }
901 #endif
902       offset /= DWARF_CIE_DATA_ALIGNMENT;
903       if (offset < 0)
904         cfi->dw_cfi_opc = DW_CFA_offset_extended_sf;
905
906       cfi->dw_cfi_oprnd2.dw_cfi_offset = offset;
907     }
908   else if (sreg == reg)
909     cfi->dw_cfi_opc = DW_CFA_same_value;
910   else
911     {
912       cfi->dw_cfi_opc = DW_CFA_register;
913       cfi->dw_cfi_oprnd2.dw_cfi_reg_num = sreg;
914     }
915
916   add_fde_cfi (label, cfi);
917 }
918
919 /* Add the CFI for saving a register window.  LABEL is passed to reg_save.
920    This CFI tells the unwinder that it needs to restore the window registers
921    from the previous frame's window save area.
922
923    ??? Perhaps we should note in the CIE where windows are saved (instead of
924    assuming 0(cfa)) and what registers are in the window.  */
925
926 void
927 dwarf2out_window_save (const char *label)
928 {
929   dw_cfi_ref cfi = new_cfi ();
930
931   cfi->dw_cfi_opc = DW_CFA_GNU_window_save;
932   add_fde_cfi (label, cfi);
933 }
934
935 /* Add a CFI to update the running total of the size of arguments
936    pushed onto the stack.  */
937
938 void
939 dwarf2out_args_size (const char *label, HOST_WIDE_INT size)
940 {
941   dw_cfi_ref cfi;
942
943   if (size == old_args_size)
944     return;
945
946   old_args_size = size;
947
948   cfi = new_cfi ();
949   cfi->dw_cfi_opc = DW_CFA_GNU_args_size;
950   cfi->dw_cfi_oprnd1.dw_cfi_offset = size;
951   add_fde_cfi (label, cfi);
952 }
953
954 /* Entry point for saving a register to the stack.  REG is the GCC register
955    number.  LABEL and OFFSET are passed to reg_save.  */
956
957 void
958 dwarf2out_reg_save (const char *label, unsigned int reg, HOST_WIDE_INT offset)
959 {
960   reg_save (label, DWARF_FRAME_REGNUM (reg), INVALID_REGNUM, offset);
961 }
962
963 /* Entry point for saving the return address in the stack.
964    LABEL and OFFSET are passed to reg_save.  */
965
966 void
967 dwarf2out_return_save (const char *label, HOST_WIDE_INT offset)
968 {
969   reg_save (label, DWARF_FRAME_RETURN_COLUMN, INVALID_REGNUM, offset);
970 }
971
972 /* Entry point for saving the return address in a register.
973    LABEL and SREG are passed to reg_save.  */
974
975 void
976 dwarf2out_return_reg (const char *label, unsigned int sreg)
977 {
978   reg_save (label, DWARF_FRAME_RETURN_COLUMN, DWARF_FRAME_REGNUM (sreg), 0);
979 }
980
981 /* Record the initial position of the return address.  RTL is
982    INCOMING_RETURN_ADDR_RTX.  */
983
984 static void
985 initial_return_save (rtx rtl)
986 {
987   unsigned int reg = INVALID_REGNUM;
988   HOST_WIDE_INT offset = 0;
989
990   switch (GET_CODE (rtl))
991     {
992     case REG:
993       /* RA is in a register.  */
994       reg = DWARF_FRAME_REGNUM (REGNO (rtl));
995       break;
996
997     case MEM:
998       /* RA is on the stack.  */
999       rtl = XEXP (rtl, 0);
1000       switch (GET_CODE (rtl))
1001         {
1002         case REG:
1003           gcc_assert (REGNO (rtl) == STACK_POINTER_REGNUM);
1004           offset = 0;
1005           break;
1006
1007         case PLUS:
1008           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1009           offset = INTVAL (XEXP (rtl, 1));
1010           break;
1011
1012         case MINUS:
1013           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1014           offset = -INTVAL (XEXP (rtl, 1));
1015           break;
1016
1017         default:
1018           gcc_unreachable ();
1019         }
1020
1021       break;
1022
1023     case PLUS:
1024       /* The return address is at some offset from any value we can
1025          actually load.  For instance, on the SPARC it is in %i7+8. Just
1026          ignore the offset for now; it doesn't matter for unwinding frames.  */
1027       gcc_assert (GET_CODE (XEXP (rtl, 1)) == CONST_INT);
1028       initial_return_save (XEXP (rtl, 0));
1029       return;
1030
1031     default:
1032       gcc_unreachable ();
1033     }
1034
1035   if (reg != DWARF_FRAME_RETURN_COLUMN)
1036     reg_save (NULL, DWARF_FRAME_RETURN_COLUMN, reg, offset - cfa.offset);
1037 }
1038
1039 /* Given a SET, calculate the amount of stack adjustment it
1040    contains.  */
1041
1042 static HOST_WIDE_INT
1043 stack_adjust_offset (rtx pattern)
1044 {
1045   rtx src = SET_SRC (pattern);
1046   rtx dest = SET_DEST (pattern);
1047   HOST_WIDE_INT offset = 0;
1048   enum rtx_code code;
1049
1050   if (dest == stack_pointer_rtx)
1051     {
1052       /* (set (reg sp) (plus (reg sp) (const_int))) */
1053       code = GET_CODE (src);
1054       if (! (code == PLUS || code == MINUS)
1055           || XEXP (src, 0) != stack_pointer_rtx
1056           || GET_CODE (XEXP (src, 1)) != CONST_INT)
1057         return 0;
1058
1059       offset = INTVAL (XEXP (src, 1));
1060       if (code == PLUS)
1061         offset = -offset;
1062     }
1063   else if (MEM_P (dest))
1064     {
1065       /* (set (mem (pre_dec (reg sp))) (foo)) */
1066       src = XEXP (dest, 0);
1067       code = GET_CODE (src);
1068
1069       switch (code)
1070         {
1071         case PRE_MODIFY:
1072         case POST_MODIFY:
1073           if (XEXP (src, 0) == stack_pointer_rtx)
1074             {
1075               rtx val = XEXP (XEXP (src, 1), 1);
1076               /* We handle only adjustments by constant amount.  */
1077               gcc_assert (GET_CODE (XEXP (src, 1)) == PLUS
1078                           && GET_CODE (val) == CONST_INT);
1079               offset = -INTVAL (val);
1080               break;
1081             }
1082           return 0;
1083
1084         case PRE_DEC:
1085         case POST_DEC:
1086           if (XEXP (src, 0) == stack_pointer_rtx)
1087             {
1088               offset = GET_MODE_SIZE (GET_MODE (dest));
1089               break;
1090             }
1091           return 0;
1092
1093         case PRE_INC:
1094         case POST_INC:
1095           if (XEXP (src, 0) == stack_pointer_rtx)
1096             {
1097               offset = -GET_MODE_SIZE (GET_MODE (dest));
1098               break;
1099             }
1100           return 0;
1101
1102         default:
1103           return 0;
1104         }
1105     }
1106   else
1107     return 0;
1108
1109   return offset;
1110 }
1111
1112 /* Check INSN to see if it looks like a push or a stack adjustment, and
1113    make a note of it if it does.  EH uses this information to find out how
1114    much extra space it needs to pop off the stack.  */
1115
1116 static void
1117 dwarf2out_stack_adjust (rtx insn, bool after_p)
1118 {
1119   HOST_WIDE_INT offset;
1120   const char *label;
1121   int i;
1122
1123   /* Don't handle epilogues at all.  Certainly it would be wrong to do so
1124      with this function.  Proper support would require all frame-related
1125      insns to be marked, and to be able to handle saving state around
1126      epilogues textually in the middle of the function.  */
1127   if (prologue_epilogue_contains (insn) || sibcall_epilogue_contains (insn))
1128     return;
1129
1130   /* If only calls can throw, and we have a frame pointer,
1131      save up adjustments until we see the CALL_INSN.  */
1132   if (!flag_asynchronous_unwind_tables && cfa.reg != STACK_POINTER_REGNUM)
1133     {
1134       if (CALL_P (insn) && !after_p)
1135         {
1136           /* Extract the size of the args from the CALL rtx itself.  */
1137           insn = PATTERN (insn);
1138           if (GET_CODE (insn) == PARALLEL)
1139             insn = XVECEXP (insn, 0, 0);
1140           if (GET_CODE (insn) == SET)
1141             insn = SET_SRC (insn);
1142           gcc_assert (GET_CODE (insn) == CALL);
1143           dwarf2out_args_size ("", INTVAL (XEXP (insn, 1)));
1144         }
1145       return;
1146     }
1147
1148   if (CALL_P (insn) && !after_p)
1149     {
1150       if (!flag_asynchronous_unwind_tables)
1151         dwarf2out_args_size ("", args_size);
1152       return;
1153     }
1154   else if (BARRIER_P (insn))
1155     {
1156       /* When we see a BARRIER, we know to reset args_size to 0.  Usually
1157          the compiler will have already emitted a stack adjustment, but
1158          doesn't bother for calls to noreturn functions.  */
1159 #ifdef STACK_GROWS_DOWNWARD
1160       offset = -args_size;
1161 #else
1162       offset = args_size;
1163 #endif
1164     }
1165   else if (GET_CODE (PATTERN (insn)) == SET)
1166     offset = stack_adjust_offset (PATTERN (insn));
1167   else if (GET_CODE (PATTERN (insn)) == PARALLEL
1168            || GET_CODE (PATTERN (insn)) == SEQUENCE)
1169     {
1170       /* There may be stack adjustments inside compound insns.  Search
1171          for them.  */
1172       for (offset = 0, i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1173         if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1174           offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i));
1175     }
1176   else
1177     return;
1178
1179   if (offset == 0)
1180     return;
1181
1182   if (cfa.reg == STACK_POINTER_REGNUM)
1183     cfa.offset += offset;
1184
1185 #ifndef STACK_GROWS_DOWNWARD
1186   offset = -offset;
1187 #endif
1188
1189   args_size += offset;
1190   if (args_size < 0)
1191     args_size = 0;
1192
1193   label = dwarf2out_cfi_label ();
1194   def_cfa_1 (label, &cfa);
1195   if (flag_asynchronous_unwind_tables)
1196     dwarf2out_args_size (label, args_size);
1197 }
1198
1199 #endif
1200
1201 /* We delay emitting a register save until either (a) we reach the end
1202    of the prologue or (b) the register is clobbered.  This clusters
1203    register saves so that there are fewer pc advances.  */
1204
1205 struct queued_reg_save GTY(())
1206 {
1207   struct queued_reg_save *next;
1208   rtx reg;
1209   HOST_WIDE_INT cfa_offset;
1210   rtx saved_reg;
1211 };
1212
1213 static GTY(()) struct queued_reg_save *queued_reg_saves;
1214
1215 /* The caller's ORIG_REG is saved in SAVED_IN_REG.  */
1216 struct reg_saved_in_data GTY(()) {
1217   rtx orig_reg;
1218   rtx saved_in_reg;
1219 };
1220
1221 /* A list of registers saved in other registers.
1222    The list intentionally has a small maximum capacity of 4; if your
1223    port needs more than that, you might consider implementing a
1224    more efficient data structure.  */
1225 static GTY(()) struct reg_saved_in_data regs_saved_in_regs[4];
1226 static GTY(()) size_t num_regs_saved_in_regs;
1227
1228 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
1229 static const char *last_reg_save_label;
1230
1231 /* Add an entry to QUEUED_REG_SAVES saying that REG is now saved at
1232    SREG, or if SREG is NULL then it is saved at OFFSET to the CFA.  */
1233
1234 static void
1235 queue_reg_save (const char *label, rtx reg, rtx sreg, HOST_WIDE_INT offset)
1236 {
1237   struct queued_reg_save *q;
1238
1239   /* Duplicates waste space, but it's also necessary to remove them
1240      for correctness, since the queue gets output in reverse
1241      order.  */
1242   for (q = queued_reg_saves; q != NULL; q = q->next)
1243     if (REGNO (q->reg) == REGNO (reg))
1244       break;
1245
1246   if (q == NULL)
1247     {
1248       q = ggc_alloc (sizeof (*q));
1249       q->next = queued_reg_saves;
1250       queued_reg_saves = q;
1251     }
1252
1253   q->reg = reg;
1254   q->cfa_offset = offset;
1255   q->saved_reg = sreg;
1256
1257   last_reg_save_label = label;
1258 }
1259
1260 /* Output all the entries in QUEUED_REG_SAVES.  */
1261
1262 static void
1263 flush_queued_reg_saves (void)
1264 {
1265   struct queued_reg_save *q;
1266
1267   for (q = queued_reg_saves; q; q = q->next)
1268     {
1269       size_t i;
1270       unsigned int reg, sreg;
1271
1272       for (i = 0; i < num_regs_saved_in_regs; i++)
1273         if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (q->reg))
1274           break;
1275       if (q->saved_reg && i == num_regs_saved_in_regs)
1276         {
1277           gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1278           num_regs_saved_in_regs++;
1279         }
1280       if (i != num_regs_saved_in_regs)
1281         {
1282           regs_saved_in_regs[i].orig_reg = q->reg;
1283           regs_saved_in_regs[i].saved_in_reg = q->saved_reg;
1284         }
1285
1286       reg = DWARF_FRAME_REGNUM (REGNO (q->reg));
1287       if (q->saved_reg)
1288         sreg = DWARF_FRAME_REGNUM (REGNO (q->saved_reg));
1289       else
1290         sreg = INVALID_REGNUM;
1291       reg_save (last_reg_save_label, reg, sreg, q->cfa_offset);
1292     }
1293
1294   queued_reg_saves = NULL;
1295   last_reg_save_label = NULL;
1296 }
1297
1298 /* Does INSN clobber any register which QUEUED_REG_SAVES lists a saved
1299    location for?  Or, does it clobber a register which we've previously
1300    said that some other register is saved in, and for which we now
1301    have a new location for?  */
1302
1303 static bool
1304 clobbers_queued_reg_save (rtx insn)
1305 {
1306   struct queued_reg_save *q;
1307
1308   for (q = queued_reg_saves; q; q = q->next)
1309     {
1310       size_t i;
1311       if (modified_in_p (q->reg, insn))
1312         return true;
1313       for (i = 0; i < num_regs_saved_in_regs; i++)
1314         if (REGNO (q->reg) == REGNO (regs_saved_in_regs[i].orig_reg)
1315             && modified_in_p (regs_saved_in_regs[i].saved_in_reg, insn))
1316           return true;
1317     }
1318
1319   return false;
1320 }
1321
1322 /* Entry point for saving the first register into the second.  */
1323
1324 void
1325 dwarf2out_reg_save_reg (const char *label, rtx reg, rtx sreg)
1326 {
1327   size_t i;
1328   unsigned int regno, sregno;
1329
1330   for (i = 0; i < num_regs_saved_in_regs; i++)
1331     if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (reg))
1332       break;
1333   if (i == num_regs_saved_in_regs)
1334     {
1335       gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1336       num_regs_saved_in_regs++;
1337     }
1338   regs_saved_in_regs[i].orig_reg = reg;
1339   regs_saved_in_regs[i].saved_in_reg = sreg;
1340
1341   regno = DWARF_FRAME_REGNUM (REGNO (reg));
1342   sregno = DWARF_FRAME_REGNUM (REGNO (sreg));
1343   reg_save (label, regno, sregno, 0);
1344 }
1345
1346 /* What register, if any, is currently saved in REG?  */
1347
1348 static rtx
1349 reg_saved_in (rtx reg)
1350 {
1351   unsigned int regn = REGNO (reg);
1352   size_t i;
1353   struct queued_reg_save *q;
1354
1355   for (q = queued_reg_saves; q; q = q->next)
1356     if (q->saved_reg && regn == REGNO (q->saved_reg))
1357       return q->reg;
1358
1359   for (i = 0; i < num_regs_saved_in_regs; i++)
1360     if (regs_saved_in_regs[i].saved_in_reg
1361         && regn == REGNO (regs_saved_in_regs[i].saved_in_reg))
1362       return regs_saved_in_regs[i].orig_reg;
1363
1364   return NULL_RTX;
1365 }
1366
1367
1368 /* A temporary register holding an integral value used in adjusting SP
1369    or setting up the store_reg.  The "offset" field holds the integer
1370    value, not an offset.  */
1371 static dw_cfa_location cfa_temp;
1372
1373 /* Record call frame debugging information for an expression EXPR,
1374    which either sets SP or FP (adjusting how we calculate the frame
1375    address) or saves a register to the stack or another register.
1376    LABEL indicates the address of EXPR.
1377
1378    This function encodes a state machine mapping rtxes to actions on
1379    cfa, cfa_store, and cfa_temp.reg.  We describe these rules so
1380    users need not read the source code.
1381
1382   The High-Level Picture
1383
1384   Changes in the register we use to calculate the CFA: Currently we
1385   assume that if you copy the CFA register into another register, we
1386   should take the other one as the new CFA register; this seems to
1387   work pretty well.  If it's wrong for some target, it's simple
1388   enough not to set RTX_FRAME_RELATED_P on the insn in question.
1389
1390   Changes in the register we use for saving registers to the stack:
1391   This is usually SP, but not always.  Again, we deduce that if you
1392   copy SP into another register (and SP is not the CFA register),
1393   then the new register is the one we will be using for register
1394   saves.  This also seems to work.
1395
1396   Register saves: There's not much guesswork about this one; if
1397   RTX_FRAME_RELATED_P is set on an insn which modifies memory, it's a
1398   register save, and the register used to calculate the destination
1399   had better be the one we think we're using for this purpose.
1400   It's also assumed that a copy from a call-saved register to another
1401   register is saving that register if RTX_FRAME_RELATED_P is set on
1402   that instruction.  If the copy is from a call-saved register to
1403   the *same* register, that means that the register is now the same
1404   value as in the caller.
1405
1406   Except: If the register being saved is the CFA register, and the
1407   offset is nonzero, we are saving the CFA, so we assume we have to
1408   use DW_CFA_def_cfa_expression.  If the offset is 0, we assume that
1409   the intent is to save the value of SP from the previous frame.
1410
1411   In addition, if a register has previously been saved to a different
1412   register,
1413
1414   Invariants / Summaries of Rules
1415
1416   cfa          current rule for calculating the CFA.  It usually
1417                consists of a register and an offset.
1418   cfa_store    register used by prologue code to save things to the stack
1419                cfa_store.offset is the offset from the value of
1420                cfa_store.reg to the actual CFA
1421   cfa_temp     register holding an integral value.  cfa_temp.offset
1422                stores the value, which will be used to adjust the
1423                stack pointer.  cfa_temp is also used like cfa_store,
1424                to track stores to the stack via fp or a temp reg.
1425
1426   Rules  1- 4: Setting a register's value to cfa.reg or an expression
1427                with cfa.reg as the first operand changes the cfa.reg and its
1428                cfa.offset.  Rule 1 and 4 also set cfa_temp.reg and
1429                cfa_temp.offset.
1430
1431   Rules  6- 9: Set a non-cfa.reg register value to a constant or an
1432                expression yielding a constant.  This sets cfa_temp.reg
1433                and cfa_temp.offset.
1434
1435   Rule 5:      Create a new register cfa_store used to save items to the
1436                stack.
1437
1438   Rules 10-14: Save a register to the stack.  Define offset as the
1439                difference of the original location and cfa_store's
1440                location (or cfa_temp's location if cfa_temp is used).
1441
1442   The Rules
1443
1444   "{a,b}" indicates a choice of a xor b.
1445   "<reg>:cfa.reg" indicates that <reg> must equal cfa.reg.
1446
1447   Rule 1:
1448   (set <reg1> <reg2>:cfa.reg)
1449   effects: cfa.reg = <reg1>
1450            cfa.offset unchanged
1451            cfa_temp.reg = <reg1>
1452            cfa_temp.offset = cfa.offset
1453
1454   Rule 2:
1455   (set sp ({minus,plus,losum} {sp,fp}:cfa.reg
1456                               {<const_int>,<reg>:cfa_temp.reg}))
1457   effects: cfa.reg = sp if fp used
1458            cfa.offset += {+/- <const_int>, cfa_temp.offset} if cfa.reg==sp
1459            cfa_store.offset += {+/- <const_int>, cfa_temp.offset}
1460              if cfa_store.reg==sp
1461
1462   Rule 3:
1463   (set fp ({minus,plus,losum} <reg>:cfa.reg <const_int>))
1464   effects: cfa.reg = fp
1465            cfa_offset += +/- <const_int>
1466
1467   Rule 4:
1468   (set <reg1> ({plus,losum} <reg2>:cfa.reg <const_int>))
1469   constraints: <reg1> != fp
1470                <reg1> != sp
1471   effects: cfa.reg = <reg1>
1472            cfa_temp.reg = <reg1>
1473            cfa_temp.offset = cfa.offset
1474
1475   Rule 5:
1476   (set <reg1> (plus <reg2>:cfa_temp.reg sp:cfa.reg))
1477   constraints: <reg1> != fp
1478                <reg1> != sp
1479   effects: cfa_store.reg = <reg1>
1480            cfa_store.offset = cfa.offset - cfa_temp.offset
1481
1482   Rule 6:
1483   (set <reg> <const_int>)
1484   effects: cfa_temp.reg = <reg>
1485            cfa_temp.offset = <const_int>
1486
1487   Rule 7:
1488   (set <reg1>:cfa_temp.reg (ior <reg2>:cfa_temp.reg <const_int>))
1489   effects: cfa_temp.reg = <reg1>
1490            cfa_temp.offset |= <const_int>
1491
1492   Rule 8:
1493   (set <reg> (high <exp>))
1494   effects: none
1495
1496   Rule 9:
1497   (set <reg> (lo_sum <exp> <const_int>))
1498   effects: cfa_temp.reg = <reg>
1499            cfa_temp.offset = <const_int>
1500
1501   Rule 10:
1502   (set (mem (pre_modify sp:cfa_store (???? <reg1> <const_int>))) <reg2>)
1503   effects: cfa_store.offset -= <const_int>
1504            cfa.offset = cfa_store.offset if cfa.reg == sp
1505            cfa.reg = sp
1506            cfa.base_offset = -cfa_store.offset
1507
1508   Rule 11:
1509   (set (mem ({pre_inc,pre_dec} sp:cfa_store.reg)) <reg>)
1510   effects: cfa_store.offset += -/+ mode_size(mem)
1511            cfa.offset = cfa_store.offset if cfa.reg == sp
1512            cfa.reg = sp
1513            cfa.base_offset = -cfa_store.offset
1514
1515   Rule 12:
1516   (set (mem ({minus,plus,losum} <reg1>:{cfa_store,cfa_temp} <const_int>))
1517
1518        <reg2>)
1519   effects: cfa.reg = <reg1>
1520            cfa.base_offset = -/+ <const_int> - {cfa_store,cfa_temp}.offset
1521
1522   Rule 13:
1523   (set (mem <reg1>:{cfa_store,cfa_temp}) <reg2>)
1524   effects: cfa.reg = <reg1>
1525            cfa.base_offset = -{cfa_store,cfa_temp}.offset
1526
1527   Rule 14:
1528   (set (mem (postinc <reg1>:cfa_temp <const_int>)) <reg2>)
1529   effects: cfa.reg = <reg1>
1530            cfa.base_offset = -cfa_temp.offset
1531            cfa_temp.offset -= mode_size(mem)
1532
1533   Rule 15:
1534   (set <reg> {unspec, unspec_volatile})
1535   effects: target-dependent  */
1536
1537 static void
1538 dwarf2out_frame_debug_expr (rtx expr, const char *label)
1539 {
1540   rtx src, dest;
1541   HOST_WIDE_INT offset;
1542
1543   /* If RTX_FRAME_RELATED_P is set on a PARALLEL, process each member of
1544      the PARALLEL independently. The first element is always processed if
1545      it is a SET. This is for backward compatibility.   Other elements
1546      are processed only if they are SETs and the RTX_FRAME_RELATED_P
1547      flag is set in them.  */
1548   if (GET_CODE (expr) == PARALLEL || GET_CODE (expr) == SEQUENCE)
1549     {
1550       int par_index;
1551       int limit = XVECLEN (expr, 0);
1552
1553       for (par_index = 0; par_index < limit; par_index++)
1554         if (GET_CODE (XVECEXP (expr, 0, par_index)) == SET
1555             && (RTX_FRAME_RELATED_P (XVECEXP (expr, 0, par_index))
1556                 || par_index == 0))
1557           dwarf2out_frame_debug_expr (XVECEXP (expr, 0, par_index), label);
1558
1559       return;
1560     }
1561
1562   gcc_assert (GET_CODE (expr) == SET);
1563
1564   src = SET_SRC (expr);
1565   dest = SET_DEST (expr);
1566
1567   if (REG_P (src))
1568     {
1569       rtx rsi = reg_saved_in (src);
1570       if (rsi)
1571         src = rsi;
1572     }
1573
1574   switch (GET_CODE (dest))
1575     {
1576     case REG:
1577       switch (GET_CODE (src))
1578         {
1579           /* Setting FP from SP.  */
1580         case REG:
1581           if (cfa.reg == (unsigned) REGNO (src))
1582             {
1583               /* Rule 1 */
1584               /* Update the CFA rule wrt SP or FP.  Make sure src is
1585                  relative to the current CFA register.
1586
1587                  We used to require that dest be either SP or FP, but the
1588                  ARM copies SP to a temporary register, and from there to
1589                  FP.  So we just rely on the backends to only set
1590                  RTX_FRAME_RELATED_P on appropriate insns.  */
1591               cfa.reg = REGNO (dest);
1592               cfa_temp.reg = cfa.reg;
1593               cfa_temp.offset = cfa.offset;
1594             }
1595           else
1596             {
1597               /* Saving a register in a register.  */
1598               gcc_assert (!fixed_regs [REGNO (dest)]
1599                           /* For the SPARC and its register window.  */
1600                           || (DWARF_FRAME_REGNUM (REGNO (src))
1601                               == DWARF_FRAME_RETURN_COLUMN));
1602               queue_reg_save (label, src, dest, 0);
1603             }
1604           break;
1605
1606         case PLUS:
1607         case MINUS:
1608         case LO_SUM:
1609           if (dest == stack_pointer_rtx)
1610             {
1611               /* Rule 2 */
1612               /* Adjusting SP.  */
1613               switch (GET_CODE (XEXP (src, 1)))
1614                 {
1615                 case CONST_INT:
1616                   offset = INTVAL (XEXP (src, 1));
1617                   break;
1618                 case REG:
1619                   gcc_assert ((unsigned) REGNO (XEXP (src, 1))
1620                               == cfa_temp.reg);
1621                   offset = cfa_temp.offset;
1622                   break;
1623                 default:
1624                   gcc_unreachable ();
1625                 }
1626
1627               if (XEXP (src, 0) == hard_frame_pointer_rtx)
1628                 {
1629                   /* Restoring SP from FP in the epilogue.  */
1630                   gcc_assert (cfa.reg == (unsigned) HARD_FRAME_POINTER_REGNUM);
1631                   cfa.reg = STACK_POINTER_REGNUM;
1632                 }
1633               else if (GET_CODE (src) == LO_SUM)
1634                 /* Assume we've set the source reg of the LO_SUM from sp.  */
1635                 ;
1636               else
1637                 gcc_assert (XEXP (src, 0) == stack_pointer_rtx);
1638
1639               if (GET_CODE (src) != MINUS)
1640                 offset = -offset;
1641               if (cfa.reg == STACK_POINTER_REGNUM)
1642                 cfa.offset += offset;
1643               if (cfa_store.reg == STACK_POINTER_REGNUM)
1644                 cfa_store.offset += offset;
1645             }
1646           else if (dest == hard_frame_pointer_rtx)
1647             {
1648               /* Rule 3 */
1649               /* Either setting the FP from an offset of the SP,
1650                  or adjusting the FP */
1651               gcc_assert (frame_pointer_needed);
1652
1653               gcc_assert (REG_P (XEXP (src, 0))
1654                           && (unsigned) REGNO (XEXP (src, 0)) == cfa.reg
1655                           && GET_CODE (XEXP (src, 1)) == CONST_INT);
1656               offset = INTVAL (XEXP (src, 1));
1657               if (GET_CODE (src) != MINUS)
1658                 offset = -offset;
1659               cfa.offset += offset;
1660               cfa.reg = HARD_FRAME_POINTER_REGNUM;
1661             }
1662           else
1663             {
1664               gcc_assert (GET_CODE (src) != MINUS);
1665
1666               /* Rule 4 */
1667               if (REG_P (XEXP (src, 0))
1668                   && REGNO (XEXP (src, 0)) == cfa.reg
1669                   && GET_CODE (XEXP (src, 1)) == CONST_INT)
1670                 {
1671                   /* Setting a temporary CFA register that will be copied
1672                      into the FP later on.  */
1673                   offset = - INTVAL (XEXP (src, 1));
1674                   cfa.offset += offset;
1675                   cfa.reg = REGNO (dest);
1676                   /* Or used to save regs to the stack.  */
1677                   cfa_temp.reg = cfa.reg;
1678                   cfa_temp.offset = cfa.offset;
1679                 }
1680
1681               /* Rule 5 */
1682               else if (REG_P (XEXP (src, 0))
1683                        && REGNO (XEXP (src, 0)) == cfa_temp.reg
1684                        && XEXP (src, 1) == stack_pointer_rtx)
1685                 {
1686                   /* Setting a scratch register that we will use instead
1687                      of SP for saving registers to the stack.  */
1688                   gcc_assert (cfa.reg == STACK_POINTER_REGNUM);
1689                   cfa_store.reg = REGNO (dest);
1690                   cfa_store.offset = cfa.offset - cfa_temp.offset;
1691                 }
1692
1693               /* Rule 9 */
1694               else if (GET_CODE (src) == LO_SUM
1695                        && GET_CODE (XEXP (src, 1)) == CONST_INT)
1696                 {
1697                   cfa_temp.reg = REGNO (dest);
1698                   cfa_temp.offset = INTVAL (XEXP (src, 1));
1699                 }
1700               else
1701                 gcc_unreachable ();
1702             }
1703           break;
1704
1705           /* Rule 6 */
1706         case CONST_INT:
1707           cfa_temp.reg = REGNO (dest);
1708           cfa_temp.offset = INTVAL (src);
1709           break;
1710
1711           /* Rule 7 */
1712         case IOR:
1713           gcc_assert (REG_P (XEXP (src, 0))
1714                       && (unsigned) REGNO (XEXP (src, 0)) == cfa_temp.reg
1715                       && GET_CODE (XEXP (src, 1)) == CONST_INT);
1716
1717           if ((unsigned) REGNO (dest) != cfa_temp.reg)
1718             cfa_temp.reg = REGNO (dest);
1719           cfa_temp.offset |= INTVAL (XEXP (src, 1));
1720           break;
1721
1722           /* Skip over HIGH, assuming it will be followed by a LO_SUM,
1723              which will fill in all of the bits.  */
1724           /* Rule 8 */
1725         case HIGH:
1726           break;
1727
1728           /* Rule 15 */
1729         case UNSPEC:
1730         case UNSPEC_VOLATILE:
1731           gcc_assert (targetm.dwarf_handle_frame_unspec);
1732           targetm.dwarf_handle_frame_unspec (label, expr, XINT (src, 1));
1733           return;
1734
1735         default:
1736           gcc_unreachable ();
1737         }
1738
1739       def_cfa_1 (label, &cfa);
1740       break;
1741
1742     case MEM:
1743       gcc_assert (REG_P (src));
1744
1745       /* Saving a register to the stack.  Make sure dest is relative to the
1746          CFA register.  */
1747       switch (GET_CODE (XEXP (dest, 0)))
1748         {
1749           /* Rule 10 */
1750           /* With a push.  */
1751         case PRE_MODIFY:
1752           /* We can't handle variable size modifications.  */
1753           gcc_assert (GET_CODE (XEXP (XEXP (XEXP (dest, 0), 1), 1))
1754                       == CONST_INT);
1755           offset = -INTVAL (XEXP (XEXP (XEXP (dest, 0), 1), 1));
1756
1757           gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
1758                       && cfa_store.reg == STACK_POINTER_REGNUM);
1759
1760           cfa_store.offset += offset;
1761           if (cfa.reg == STACK_POINTER_REGNUM)
1762             cfa.offset = cfa_store.offset;
1763
1764           offset = -cfa_store.offset;
1765           break;
1766
1767           /* Rule 11 */
1768         case PRE_INC:
1769         case PRE_DEC:
1770           offset = GET_MODE_SIZE (GET_MODE (dest));
1771           if (GET_CODE (XEXP (dest, 0)) == PRE_INC)
1772             offset = -offset;
1773
1774           gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
1775                       && cfa_store.reg == STACK_POINTER_REGNUM);
1776
1777           cfa_store.offset += offset;
1778           if (cfa.reg == STACK_POINTER_REGNUM)
1779             cfa.offset = cfa_store.offset;
1780
1781           offset = -cfa_store.offset;
1782           break;
1783
1784           /* Rule 12 */
1785           /* With an offset.  */
1786         case PLUS:
1787         case MINUS:
1788         case LO_SUM:
1789           {
1790             int regno;
1791
1792             gcc_assert (GET_CODE (XEXP (XEXP (dest, 0), 1)) == CONST_INT);
1793             offset = INTVAL (XEXP (XEXP (dest, 0), 1));
1794             if (GET_CODE (XEXP (dest, 0)) == MINUS)
1795               offset = -offset;
1796
1797             regno = REGNO (XEXP (XEXP (dest, 0), 0));
1798
1799             if (cfa_store.reg == (unsigned) regno)
1800               offset -= cfa_store.offset;
1801             else
1802               {
1803                 gcc_assert (cfa_temp.reg == (unsigned) regno);
1804                 offset -= cfa_temp.offset;
1805               }
1806           }
1807           break;
1808
1809           /* Rule 13 */
1810           /* Without an offset.  */
1811         case REG:
1812           {
1813             int regno = REGNO (XEXP (dest, 0));
1814
1815             if (cfa_store.reg == (unsigned) regno)
1816               offset = -cfa_store.offset;
1817             else
1818               {
1819                 gcc_assert (cfa_temp.reg == (unsigned) regno);
1820                 offset = -cfa_temp.offset;
1821               }
1822           }
1823           break;
1824
1825           /* Rule 14 */
1826         case POST_INC:
1827           gcc_assert (cfa_temp.reg
1828                       == (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)));
1829           offset = -cfa_temp.offset;
1830           cfa_temp.offset -= GET_MODE_SIZE (GET_MODE (dest));
1831           break;
1832
1833         default:
1834           gcc_unreachable ();
1835         }
1836
1837       if (REGNO (src) != STACK_POINTER_REGNUM
1838           && REGNO (src) != HARD_FRAME_POINTER_REGNUM
1839           && (unsigned) REGNO (src) == cfa.reg)
1840         {
1841           /* We're storing the current CFA reg into the stack.  */
1842
1843           if (cfa.offset == 0)
1844             {
1845               /* If the source register is exactly the CFA, assume
1846                  we're saving SP like any other register; this happens
1847                  on the ARM.  */
1848               def_cfa_1 (label, &cfa);
1849               queue_reg_save (label, stack_pointer_rtx, NULL_RTX, offset);
1850               break;
1851             }
1852           else
1853             {
1854               /* Otherwise, we'll need to look in the stack to
1855                  calculate the CFA.  */
1856               rtx x = XEXP (dest, 0);
1857
1858               if (!REG_P (x))
1859                 x = XEXP (x, 0);
1860               gcc_assert (REG_P (x));
1861
1862               cfa.reg = REGNO (x);
1863               cfa.base_offset = offset;
1864               cfa.indirect = 1;
1865               def_cfa_1 (label, &cfa);
1866               break;
1867             }
1868         }
1869
1870       def_cfa_1 (label, &cfa);
1871       queue_reg_save (label, src, NULL_RTX, offset);
1872       break;
1873
1874     default:
1875       gcc_unreachable ();
1876     }
1877 }
1878
1879 /* Record call frame debugging information for INSN, which either
1880    sets SP or FP (adjusting how we calculate the frame address) or saves a
1881    register to the stack.  If INSN is NULL_RTX, initialize our state.
1882
1883    If AFTER_P is false, we're being called before the insn is emitted,
1884    otherwise after.  Call instructions get invoked twice.  */
1885
1886 void
1887 dwarf2out_frame_debug (rtx insn, bool after_p)
1888 {
1889   const char *label;
1890   rtx src;
1891
1892   if (insn == NULL_RTX)
1893     {
1894       size_t i;
1895
1896       /* Flush any queued register saves.  */
1897       flush_queued_reg_saves ();
1898
1899       /* Set up state for generating call frame debug info.  */
1900       lookup_cfa (&cfa);
1901       gcc_assert (cfa.reg
1902                   == (unsigned long)DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM));
1903
1904       cfa.reg = STACK_POINTER_REGNUM;
1905       cfa_store = cfa;
1906       cfa_temp.reg = -1;
1907       cfa_temp.offset = 0;
1908
1909       for (i = 0; i < num_regs_saved_in_regs; i++)
1910         {
1911           regs_saved_in_regs[i].orig_reg = NULL_RTX;
1912           regs_saved_in_regs[i].saved_in_reg = NULL_RTX;
1913         }
1914       num_regs_saved_in_regs = 0;
1915       return;
1916     }
1917
1918   if (!NONJUMP_INSN_P (insn) || clobbers_queued_reg_save (insn))
1919     flush_queued_reg_saves ();
1920
1921   if (! RTX_FRAME_RELATED_P (insn))
1922     {
1923       if (!ACCUMULATE_OUTGOING_ARGS)
1924         dwarf2out_stack_adjust (insn, after_p);
1925       return;
1926     }
1927
1928   label = dwarf2out_cfi_label ();
1929   src = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
1930   if (src)
1931     insn = XEXP (src, 0);
1932   else
1933     insn = PATTERN (insn);
1934
1935   dwarf2out_frame_debug_expr (insn, label);
1936 }
1937
1938 #endif
1939
1940 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used.  */
1941 static enum dw_cfi_oprnd_type dw_cfi_oprnd1_desc
1942  (enum dwarf_call_frame_info cfi);
1943
1944 static enum dw_cfi_oprnd_type
1945 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
1946 {
1947   switch (cfi)
1948     {
1949     case DW_CFA_nop:
1950     case DW_CFA_GNU_window_save:
1951       return dw_cfi_oprnd_unused;
1952
1953     case DW_CFA_set_loc:
1954     case DW_CFA_advance_loc1:
1955     case DW_CFA_advance_loc2:
1956     case DW_CFA_advance_loc4:
1957     case DW_CFA_MIPS_advance_loc8:
1958       return dw_cfi_oprnd_addr;
1959
1960     case DW_CFA_offset:
1961     case DW_CFA_offset_extended:
1962     case DW_CFA_def_cfa:
1963     case DW_CFA_offset_extended_sf:
1964     case DW_CFA_def_cfa_sf:
1965     case DW_CFA_restore_extended:
1966     case DW_CFA_undefined:
1967     case DW_CFA_same_value:
1968     case DW_CFA_def_cfa_register:
1969     case DW_CFA_register:
1970       return dw_cfi_oprnd_reg_num;
1971
1972     case DW_CFA_def_cfa_offset:
1973     case DW_CFA_GNU_args_size:
1974     case DW_CFA_def_cfa_offset_sf:
1975       return dw_cfi_oprnd_offset;
1976
1977     case DW_CFA_def_cfa_expression:
1978     case DW_CFA_expression:
1979       return dw_cfi_oprnd_loc;
1980
1981     default:
1982       gcc_unreachable ();
1983     }
1984 }
1985
1986 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used.  */
1987 static enum dw_cfi_oprnd_type dw_cfi_oprnd2_desc
1988  (enum dwarf_call_frame_info cfi);
1989
1990 static enum dw_cfi_oprnd_type
1991 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
1992 {
1993   switch (cfi)
1994     {
1995     case DW_CFA_def_cfa:
1996     case DW_CFA_def_cfa_sf:
1997     case DW_CFA_offset:
1998     case DW_CFA_offset_extended_sf:
1999     case DW_CFA_offset_extended:
2000       return dw_cfi_oprnd_offset;
2001
2002     case DW_CFA_register:
2003       return dw_cfi_oprnd_reg_num;
2004
2005     default:
2006       return dw_cfi_oprnd_unused;
2007     }
2008 }
2009
2010 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
2011
2012 /* Map register numbers held in the call frame info that gcc has
2013    collected using DWARF_FRAME_REGNUM to those that should be output in
2014    .debug_frame and .eh_frame.  */
2015 #ifndef DWARF2_FRAME_REG_OUT
2016 #define DWARF2_FRAME_REG_OUT(REGNO, FOR_EH) (REGNO)
2017 #endif
2018
2019 /* Output a Call Frame Information opcode and its operand(s).  */
2020
2021 static void
2022 output_cfi (dw_cfi_ref cfi, dw_fde_ref fde, int for_eh)
2023 {
2024   unsigned long r;
2025   if (cfi->dw_cfi_opc == DW_CFA_advance_loc)
2026     dw2_asm_output_data (1, (cfi->dw_cfi_opc
2027                              | (cfi->dw_cfi_oprnd1.dw_cfi_offset & 0x3f)),
2028                          "DW_CFA_advance_loc " HOST_WIDE_INT_PRINT_HEX,
2029                          cfi->dw_cfi_oprnd1.dw_cfi_offset);
2030   else if (cfi->dw_cfi_opc == DW_CFA_offset)
2031     {
2032       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2033       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
2034                            "DW_CFA_offset, column 0x%lx", r);
2035       dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
2036     }
2037   else if (cfi->dw_cfi_opc == DW_CFA_restore)
2038     {
2039       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2040       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
2041                            "DW_CFA_restore, column 0x%lx", r);
2042     }
2043   else
2044     {
2045       dw2_asm_output_data (1, cfi->dw_cfi_opc,
2046                            "%s", dwarf_cfi_name (cfi->dw_cfi_opc));
2047
2048       switch (cfi->dw_cfi_opc)
2049         {
2050         case DW_CFA_set_loc:
2051           if (for_eh)
2052             dw2_asm_output_encoded_addr_rtx (
2053                 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0),
2054                 gen_rtx_SYMBOL_REF (Pmode, cfi->dw_cfi_oprnd1.dw_cfi_addr),
2055                 false, NULL);
2056           else
2057             dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2058                                  cfi->dw_cfi_oprnd1.dw_cfi_addr, NULL);
2059           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2060           break;
2061
2062         case DW_CFA_advance_loc1:
2063           dw2_asm_output_delta (1, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2064                                 fde->dw_fde_current_label, NULL);
2065           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2066           break;
2067
2068         case DW_CFA_advance_loc2:
2069           dw2_asm_output_delta (2, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2070                                 fde->dw_fde_current_label, NULL);
2071           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2072           break;
2073
2074         case DW_CFA_advance_loc4:
2075           dw2_asm_output_delta (4, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2076                                 fde->dw_fde_current_label, NULL);
2077           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2078           break;
2079
2080         case DW_CFA_MIPS_advance_loc8:
2081           dw2_asm_output_delta (8, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2082                                 fde->dw_fde_current_label, NULL);
2083           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2084           break;
2085
2086         case DW_CFA_offset_extended:
2087         case DW_CFA_def_cfa:
2088           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2089           dw2_asm_output_data_uleb128 (r, NULL);
2090           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
2091           break;
2092
2093         case DW_CFA_offset_extended_sf:
2094         case DW_CFA_def_cfa_sf:
2095           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2096           dw2_asm_output_data_uleb128 (r, NULL);
2097           dw2_asm_output_data_sleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
2098           break;
2099
2100         case DW_CFA_restore_extended:
2101         case DW_CFA_undefined:
2102         case DW_CFA_same_value:
2103         case DW_CFA_def_cfa_register:
2104           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2105           dw2_asm_output_data_uleb128 (r, NULL);
2106           break;
2107
2108         case DW_CFA_register:
2109           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2110           dw2_asm_output_data_uleb128 (r, NULL);
2111           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, for_eh);
2112           dw2_asm_output_data_uleb128 (r, NULL);
2113           break;
2114
2115         case DW_CFA_def_cfa_offset:
2116         case DW_CFA_GNU_args_size:
2117           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
2118           break;
2119
2120         case DW_CFA_def_cfa_offset_sf:
2121           dw2_asm_output_data_sleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
2122           break;
2123
2124         case DW_CFA_GNU_window_save:
2125           break;
2126
2127         case DW_CFA_def_cfa_expression:
2128         case DW_CFA_expression:
2129           output_cfa_loc (cfi);
2130           break;
2131
2132         case DW_CFA_GNU_negative_offset_extended:
2133           /* Obsoleted by DW_CFA_offset_extended_sf.  */
2134           gcc_unreachable ();
2135
2136         default:
2137           break;
2138         }
2139     }
2140 }
2141
2142 /* Output the call frame information used to record information
2143    that relates to calculating the frame pointer, and records the
2144    location of saved registers.  */
2145
2146 static void
2147 output_call_frame_info (int for_eh)
2148 {
2149   unsigned int i;
2150   dw_fde_ref fde;
2151   dw_cfi_ref cfi;
2152   char l1[20], l2[20], section_start_label[20];
2153   bool any_lsda_needed = false;
2154   char augmentation[6];
2155   int augmentation_size;
2156   int fde_encoding = DW_EH_PE_absptr;
2157   int per_encoding = DW_EH_PE_absptr;
2158   int lsda_encoding = DW_EH_PE_absptr;
2159   int return_reg;
2160
2161   /* Don't emit a CIE if there won't be any FDEs.  */
2162   if (fde_table_in_use == 0)
2163     return;
2164
2165   /* If we make FDEs linkonce, we may have to emit an empty label for
2166      an FDE that wouldn't otherwise be emitted.  We want to avoid
2167      having an FDE kept around when the function it refers to is
2168      discarded.  Example where this matters: a primary function
2169      template in C++ requires EH information, but an explicit
2170      specialization doesn't.  */
2171   if (TARGET_USES_WEAK_UNWIND_INFO
2172       && ! flag_asynchronous_unwind_tables
2173       && for_eh)
2174     for (i = 0; i < fde_table_in_use; i++)
2175       if ((fde_table[i].nothrow || fde_table[i].all_throwers_are_sibcalls)
2176           && !fde_table[i].uses_eh_lsda
2177           && ! DECL_WEAK (fde_table[i].decl))
2178         targetm.asm_out.unwind_label (asm_out_file, fde_table[i].decl,
2179                                       for_eh, /* empty */ 1);
2180
2181   /* If we don't have any functions we'll want to unwind out of, don't
2182      emit any EH unwind information.  Note that if exceptions aren't
2183      enabled, we won't have collected nothrow information, and if we
2184      asked for asynchronous tables, we always want this info.  */
2185   if (for_eh)
2186     {
2187       bool any_eh_needed = !flag_exceptions || flag_asynchronous_unwind_tables;
2188
2189       for (i = 0; i < fde_table_in_use; i++)
2190         if (fde_table[i].uses_eh_lsda)
2191           any_eh_needed = any_lsda_needed = true;
2192         else if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
2193           any_eh_needed = true;
2194         else if (! fde_table[i].nothrow
2195                  && ! fde_table[i].all_throwers_are_sibcalls)
2196           any_eh_needed = true;
2197
2198       if (! any_eh_needed)
2199         return;
2200     }
2201
2202   /* We're going to be generating comments, so turn on app.  */
2203   if (flag_debug_asm)
2204     app_enable ();
2205
2206   if (for_eh)
2207     targetm.asm_out.eh_frame_section ();
2208   else
2209     named_section_flags (DEBUG_FRAME_SECTION, SECTION_DEBUG);
2210
2211   ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
2212   ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
2213
2214   /* Output the CIE.  */
2215   ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
2216   ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
2217   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
2218                         "Length of Common Information Entry");
2219   ASM_OUTPUT_LABEL (asm_out_file, l1);
2220
2221   /* Now that the CIE pointer is PC-relative for EH,
2222      use 0 to identify the CIE.  */
2223   dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
2224                        (for_eh ? 0 : DW_CIE_ID),
2225                        "CIE Identifier Tag");
2226
2227   dw2_asm_output_data (1, DW_CIE_VERSION, "CIE Version");
2228
2229   augmentation[0] = 0;
2230   augmentation_size = 0;
2231   if (for_eh)
2232     {
2233       char *p;
2234
2235       /* Augmentation:
2236          z      Indicates that a uleb128 is present to size the
2237                 augmentation section.
2238          L      Indicates the encoding (and thus presence) of
2239                 an LSDA pointer in the FDE augmentation.
2240          R      Indicates a non-default pointer encoding for
2241                 FDE code pointers.
2242          P      Indicates the presence of an encoding + language
2243                 personality routine in the CIE augmentation.  */
2244
2245       fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
2246       per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
2247       lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
2248
2249       p = augmentation + 1;
2250       if (eh_personality_libfunc)
2251         {
2252           *p++ = 'P';
2253           augmentation_size += 1 + size_of_encoded_value (per_encoding);
2254         }
2255       if (any_lsda_needed)
2256         {
2257           *p++ = 'L';
2258           augmentation_size += 1;
2259         }
2260       if (fde_encoding != DW_EH_PE_absptr)
2261         {
2262           *p++ = 'R';
2263           augmentation_size += 1;
2264         }
2265       if (p > augmentation + 1)
2266         {
2267           augmentation[0] = 'z';
2268           *p = '\0';
2269         }
2270
2271       /* Ug.  Some platforms can't do unaligned dynamic relocations at all.  */
2272       if (eh_personality_libfunc && per_encoding == DW_EH_PE_aligned)
2273         {
2274           int offset = (  4             /* Length */
2275                         + 4             /* CIE Id */
2276                         + 1             /* CIE version */
2277                         + strlen (augmentation) + 1     /* Augmentation */
2278                         + size_of_uleb128 (1)           /* Code alignment */
2279                         + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
2280                         + 1             /* RA column */
2281                         + 1             /* Augmentation size */
2282                         + 1             /* Personality encoding */ );
2283           int pad = -offset & (PTR_SIZE - 1);
2284
2285           augmentation_size += pad;
2286
2287           /* Augmentations should be small, so there's scarce need to
2288              iterate for a solution.  Die if we exceed one uleb128 byte.  */
2289           gcc_assert (size_of_uleb128 (augmentation_size) == 1);
2290         }
2291     }
2292
2293   dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
2294   dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
2295   dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
2296                                "CIE Data Alignment Factor");
2297
2298   return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
2299   if (DW_CIE_VERSION == 1)
2300     dw2_asm_output_data (1, return_reg, "CIE RA Column");
2301   else
2302     dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
2303
2304   if (augmentation[0])
2305     {
2306       dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
2307       if (eh_personality_libfunc)
2308         {
2309           dw2_asm_output_data (1, per_encoding, "Personality (%s)",
2310                                eh_data_format_name (per_encoding));
2311           dw2_asm_output_encoded_addr_rtx (per_encoding,
2312                                            eh_personality_libfunc,
2313                                            true, NULL);
2314         }
2315
2316       if (any_lsda_needed)
2317         dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
2318                              eh_data_format_name (lsda_encoding));
2319
2320       if (fde_encoding != DW_EH_PE_absptr)
2321         dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
2322                              eh_data_format_name (fde_encoding));
2323     }
2324
2325   for (cfi = cie_cfi_head; cfi != NULL; cfi = cfi->dw_cfi_next)
2326     output_cfi (cfi, NULL, for_eh);
2327
2328   /* Pad the CIE out to an address sized boundary.  */
2329   ASM_OUTPUT_ALIGN (asm_out_file,
2330                     floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
2331   ASM_OUTPUT_LABEL (asm_out_file, l2);
2332
2333   /* Loop through all of the FDE's.  */
2334   for (i = 0; i < fde_table_in_use; i++)
2335     {
2336       fde = &fde_table[i];
2337
2338       /* Don't emit EH unwind info for leaf functions that don't need it.  */
2339       if (for_eh && !flag_asynchronous_unwind_tables && flag_exceptions
2340           && (fde->nothrow || fde->all_throwers_are_sibcalls)
2341           && ! (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
2342           && !fde->uses_eh_lsda)
2343         continue;
2344
2345       targetm.asm_out.unwind_label (asm_out_file, fde->decl, for_eh, /* empty */ 0);
2346       targetm.asm_out.internal_label (asm_out_file, FDE_LABEL, for_eh + i * 2);
2347       ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + i * 2);
2348       ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + i * 2);
2349       dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
2350                             "FDE Length");
2351       ASM_OUTPUT_LABEL (asm_out_file, l1);
2352
2353       if (for_eh)
2354         dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
2355       else
2356         dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
2357                                "FDE CIE offset");
2358
2359       if (for_eh)
2360         {
2361           rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, fde->dw_fde_begin);
2362           SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
2363           dw2_asm_output_encoded_addr_rtx (fde_encoding,
2364                                            sym_ref,
2365                                            false,
2366                                            "FDE initial location");
2367           if (fde->dw_fde_switched_sections)
2368             {
2369               rtx sym_ref2 = gen_rtx_SYMBOL_REF (Pmode, 
2370                                       fde->dw_fde_unlikely_section_label);
2371               rtx sym_ref3= gen_rtx_SYMBOL_REF (Pmode, 
2372                                       fde->dw_fde_hot_section_label);
2373               SYMBOL_REF_FLAGS (sym_ref2) |= SYMBOL_FLAG_LOCAL;
2374               SYMBOL_REF_FLAGS (sym_ref3) |= SYMBOL_FLAG_LOCAL;
2375               dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref3, false,
2376                                                "FDE initial location");
2377               dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
2378                                     fde->dw_fde_hot_section_end_label,
2379                                     fde->dw_fde_hot_section_label,
2380                                     "FDE address range");
2381               dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref2, false,
2382                                                "FDE initial location");
2383               dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
2384                                     fde->dw_fde_unlikely_section_end_label,
2385                                     fde->dw_fde_unlikely_section_label,
2386                                     "FDE address range");
2387             }
2388           else
2389             dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
2390                                   fde->dw_fde_end, fde->dw_fde_begin,
2391                                   "FDE address range");
2392         }
2393       else
2394         {
2395           dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
2396                                "FDE initial location");
2397           if (fde->dw_fde_switched_sections)
2398             {
2399               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2400                                    fde->dw_fde_hot_section_label,
2401                                    "FDE initial location");
2402               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
2403                                     fde->dw_fde_hot_section_end_label,
2404                                     fde->dw_fde_hot_section_label,
2405                                     "FDE address range");
2406               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2407                                    fde->dw_fde_unlikely_section_label,
2408                                    "FDE initial location");
2409               dw2_asm_output_delta (DWARF2_ADDR_SIZE, 
2410                                     fde->dw_fde_unlikely_section_end_label,
2411                                     fde->dw_fde_unlikely_section_label,
2412                                     "FDE address range");
2413             }
2414           else
2415             dw2_asm_output_delta (DWARF2_ADDR_SIZE,
2416                                   fde->dw_fde_end, fde->dw_fde_begin,
2417                                   "FDE address range");
2418         }
2419
2420       if (augmentation[0])
2421         {
2422           if (any_lsda_needed)
2423             {
2424               int size = size_of_encoded_value (lsda_encoding);
2425
2426               if (lsda_encoding == DW_EH_PE_aligned)
2427                 {
2428                   int offset = (  4             /* Length */
2429                                 + 4             /* CIE offset */
2430                                 + 2 * size_of_encoded_value (fde_encoding)
2431                                 + 1             /* Augmentation size */ );
2432                   int pad = -offset & (PTR_SIZE - 1);
2433
2434                   size += pad;
2435                   gcc_assert (size_of_uleb128 (size) == 1);
2436                 }
2437
2438               dw2_asm_output_data_uleb128 (size, "Augmentation size");
2439
2440               if (fde->uses_eh_lsda)
2441                 {
2442                   ASM_GENERATE_INTERNAL_LABEL (l1, "LLSDA",
2443                                                fde->funcdef_number);
2444                   dw2_asm_output_encoded_addr_rtx (
2445                         lsda_encoding, gen_rtx_SYMBOL_REF (Pmode, l1),
2446                         false, "Language Specific Data Area");
2447                 }
2448               else
2449                 {
2450                   if (lsda_encoding == DW_EH_PE_aligned)
2451                     ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
2452                   dw2_asm_output_data
2453                     (size_of_encoded_value (lsda_encoding), 0,
2454                      "Language Specific Data Area (none)");
2455                 }
2456             }
2457           else
2458             dw2_asm_output_data_uleb128 (0, "Augmentation size");
2459         }
2460
2461       /* Loop through the Call Frame Instructions associated with
2462          this FDE.  */
2463       fde->dw_fde_current_label = fde->dw_fde_begin;
2464       for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
2465         output_cfi (cfi, fde, for_eh);
2466
2467       /* Pad the FDE out to an address sized boundary.  */
2468       ASM_OUTPUT_ALIGN (asm_out_file,
2469                         floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
2470       ASM_OUTPUT_LABEL (asm_out_file, l2);
2471     }
2472
2473   if (for_eh && targetm.terminate_dw2_eh_frame_info)
2474     dw2_asm_output_data (4, 0, "End of Table");
2475 #ifdef MIPS_DEBUGGING_INFO
2476   /* Work around Irix 6 assembler bug whereby labels at the end of a section
2477      get a value of 0.  Putting .align 0 after the label fixes it.  */
2478   ASM_OUTPUT_ALIGN (asm_out_file, 0);
2479 #endif
2480
2481   /* Turn off app to make assembly quicker.  */
2482   if (flag_debug_asm)
2483     app_disable ();
2484 }
2485
2486 /* Output a marker (i.e. a label) for the beginning of a function, before
2487    the prologue.  */
2488
2489 void
2490 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
2491                           const char *file ATTRIBUTE_UNUSED)
2492 {
2493   char label[MAX_ARTIFICIAL_LABEL_BYTES];
2494   char * dup_label;
2495   dw_fde_ref fde;
2496
2497   current_function_func_begin_label = NULL;
2498
2499 #ifdef TARGET_UNWIND_INFO
2500   /* ??? current_function_func_begin_label is also used by except.c
2501      for call-site information.  We must emit this label if it might
2502      be used.  */
2503   if ((! flag_exceptions || USING_SJLJ_EXCEPTIONS)
2504       && ! dwarf2out_do_frame ())
2505     return;
2506 #else
2507   if (! dwarf2out_do_frame ())
2508     return;
2509 #endif
2510
2511   function_section (current_function_decl);
2512   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
2513                                current_function_funcdef_no);
2514   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
2515                           current_function_funcdef_no);
2516   dup_label = xstrdup (label);
2517   current_function_func_begin_label = dup_label;
2518
2519 #ifdef TARGET_UNWIND_INFO
2520   /* We can elide the fde allocation if we're not emitting debug info.  */
2521   if (! dwarf2out_do_frame ())
2522     return;
2523 #endif
2524
2525   /* Expand the fde table if necessary.  */
2526   if (fde_table_in_use == fde_table_allocated)
2527     {
2528       fde_table_allocated += FDE_TABLE_INCREMENT;
2529       fde_table = ggc_realloc (fde_table,
2530                                fde_table_allocated * sizeof (dw_fde_node));
2531       memset (fde_table + fde_table_in_use, 0,
2532               FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
2533     }
2534
2535   /* Record the FDE associated with this function.  */
2536   current_funcdef_fde = fde_table_in_use;
2537
2538   /* Add the new FDE at the end of the fde_table.  */
2539   fde = &fde_table[fde_table_in_use++];
2540   fde->decl = current_function_decl;
2541   fde->dw_fde_begin = dup_label;
2542   fde->dw_fde_current_label = dup_label;
2543   fde->dw_fde_hot_section_label = NULL;
2544   fde->dw_fde_hot_section_end_label = NULL;
2545   fde->dw_fde_unlikely_section_label = NULL;
2546   fde->dw_fde_unlikely_section_end_label = NULL;
2547   fde->dw_fde_switched_sections = false;
2548   fde->dw_fde_end = NULL;
2549   fde->dw_fde_cfi = NULL;
2550   fde->funcdef_number = current_function_funcdef_no;
2551   fde->nothrow = TREE_NOTHROW (current_function_decl);
2552   fde->uses_eh_lsda = cfun->uses_eh_lsda;
2553   fde->all_throwers_are_sibcalls = cfun->all_throwers_are_sibcalls;
2554
2555   args_size = old_args_size = 0;
2556
2557   /* We only want to output line number information for the genuine dwarf2
2558      prologue case, not the eh frame case.  */
2559 #ifdef DWARF2_DEBUGGING_INFO
2560   if (file)
2561     dwarf2out_source_line (line, file);
2562 #endif
2563 }
2564
2565 /* Output a marker (i.e. a label) for the absolute end of the generated code
2566    for a function definition.  This gets called *after* the epilogue code has
2567    been generated.  */
2568
2569 void
2570 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
2571                         const char *file ATTRIBUTE_UNUSED)
2572 {
2573   dw_fde_ref fde;
2574   char label[MAX_ARTIFICIAL_LABEL_BYTES];
2575
2576   /* Output a label to mark the endpoint of the code generated for this
2577      function.  */
2578   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
2579                                current_function_funcdef_no);
2580   ASM_OUTPUT_LABEL (asm_out_file, label);
2581   fde = &fde_table[fde_table_in_use - 1];
2582   fde->dw_fde_end = xstrdup (label);
2583 }
2584
2585 void
2586 dwarf2out_frame_init (void)
2587 {
2588   /* Allocate the initial hunk of the fde_table.  */
2589   fde_table = ggc_alloc_cleared (FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
2590   fde_table_allocated = FDE_TABLE_INCREMENT;
2591   fde_table_in_use = 0;
2592
2593   /* Generate the CFA instructions common to all FDE's.  Do it now for the
2594      sake of lookup_cfa.  */
2595
2596   /* On entry, the Canonical Frame Address is at SP.  */
2597   dwarf2out_def_cfa (NULL, STACK_POINTER_REGNUM, INCOMING_FRAME_SP_OFFSET);
2598
2599 #ifdef DWARF2_UNWIND_INFO
2600   if (DWARF2_UNWIND_INFO)
2601     initial_return_save (INCOMING_RETURN_ADDR_RTX);
2602 #endif
2603 }
2604
2605 void
2606 dwarf2out_frame_finish (void)
2607 {
2608   /* Output call frame information.  */
2609   if (DWARF2_FRAME_INFO)
2610     output_call_frame_info (0);
2611
2612 #ifndef TARGET_UNWIND_INFO
2613   /* Output another copy for the unwinder.  */
2614   if (! USING_SJLJ_EXCEPTIONS && (flag_unwind_tables || flag_exceptions))
2615     output_call_frame_info (1);
2616 #endif
2617 }
2618 #endif
2619 \f
2620 /* And now, the subset of the debugging information support code necessary
2621    for emitting location expressions.  */
2622
2623 /* We need some way to distinguish DW_OP_addr with a direct symbol
2624    relocation from DW_OP_addr with a dtp-relative symbol relocation.  */
2625 #define INTERNAL_DW_OP_tls_addr         (0x100 + DW_OP_addr)
2626
2627
2628 typedef struct dw_val_struct *dw_val_ref;
2629 typedef struct die_struct *dw_die_ref;
2630 typedef struct dw_loc_descr_struct *dw_loc_descr_ref;
2631 typedef struct dw_loc_list_struct *dw_loc_list_ref;
2632
2633 /* Each DIE may have a series of attribute/value pairs.  Values
2634    can take on several forms.  The forms that are used in this
2635    implementation are listed below.  */
2636
2637 enum dw_val_class
2638 {
2639   dw_val_class_addr,
2640   dw_val_class_offset,
2641   dw_val_class_loc,
2642   dw_val_class_loc_list,
2643   dw_val_class_range_list,
2644   dw_val_class_const,
2645   dw_val_class_unsigned_const,
2646   dw_val_class_long_long,
2647   dw_val_class_vec,
2648   dw_val_class_flag,
2649   dw_val_class_die_ref,
2650   dw_val_class_fde_ref,
2651   dw_val_class_lbl_id,
2652   dw_val_class_lbl_offset,
2653   dw_val_class_str
2654 };
2655
2656 /* Describe a double word constant value.  */
2657 /* ??? Every instance of long_long in the code really means CONST_DOUBLE.  */
2658
2659 typedef struct dw_long_long_struct GTY(())
2660 {
2661   unsigned long hi;
2662   unsigned long low;
2663 }
2664 dw_long_long_const;
2665
2666 /* Describe a floating point constant value, or a vector constant value.  */
2667
2668 typedef struct dw_vec_struct GTY(())
2669 {
2670   unsigned char * GTY((length ("%h.length"))) array;
2671   unsigned length;
2672   unsigned elt_size;
2673 }
2674 dw_vec_const;
2675
2676 /* The dw_val_node describes an attribute's value, as it is
2677    represented internally.  */
2678
2679 typedef struct dw_val_struct GTY(())
2680 {
2681   enum dw_val_class val_class;
2682   union dw_val_struct_union
2683     {
2684       rtx GTY ((tag ("dw_val_class_addr"))) val_addr;
2685       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_offset"))) val_offset;
2686       dw_loc_list_ref GTY ((tag ("dw_val_class_loc_list"))) val_loc_list;
2687       dw_loc_descr_ref GTY ((tag ("dw_val_class_loc"))) val_loc;
2688       HOST_WIDE_INT GTY ((default)) val_int;
2689       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_unsigned_const"))) val_unsigned;
2690       dw_long_long_const GTY ((tag ("dw_val_class_long_long"))) val_long_long;
2691       dw_vec_const GTY ((tag ("dw_val_class_vec"))) val_vec;
2692       struct dw_val_die_union
2693         {
2694           dw_die_ref die;
2695           int external;
2696         } GTY ((tag ("dw_val_class_die_ref"))) val_die_ref;
2697       unsigned GTY ((tag ("dw_val_class_fde_ref"))) val_fde_index;
2698       struct indirect_string_node * GTY ((tag ("dw_val_class_str"))) val_str;
2699       char * GTY ((tag ("dw_val_class_lbl_id"))) val_lbl_id;
2700       unsigned char GTY ((tag ("dw_val_class_flag"))) val_flag;
2701     }
2702   GTY ((desc ("%1.val_class"))) v;
2703 }
2704 dw_val_node;
2705
2706 /* Locations in memory are described using a sequence of stack machine
2707    operations.  */
2708
2709 typedef struct dw_loc_descr_struct GTY(())
2710 {
2711   dw_loc_descr_ref dw_loc_next;
2712   enum dwarf_location_atom dw_loc_opc;
2713   dw_val_node dw_loc_oprnd1;
2714   dw_val_node dw_loc_oprnd2;
2715   int dw_loc_addr;
2716 }
2717 dw_loc_descr_node;
2718
2719 /* Location lists are ranges + location descriptions for that range,
2720    so you can track variables that are in different places over
2721    their entire life.  */
2722 typedef struct dw_loc_list_struct GTY(())
2723 {
2724   dw_loc_list_ref dw_loc_next;
2725   const char *begin; /* Label for begin address of range */
2726   const char *end;  /* Label for end address of range */
2727   char *ll_symbol; /* Label for beginning of location list.
2728                       Only on head of list */
2729   const char *section; /* Section this loclist is relative to */
2730   dw_loc_descr_ref expr;
2731 } dw_loc_list_node;
2732
2733 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
2734
2735 static const char *dwarf_stack_op_name (unsigned);
2736 static dw_loc_descr_ref new_loc_descr (enum dwarf_location_atom,
2737                                        unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT);
2738 static void add_loc_descr (dw_loc_descr_ref *, dw_loc_descr_ref);
2739 static unsigned long size_of_loc_descr (dw_loc_descr_ref);
2740 static unsigned long size_of_locs (dw_loc_descr_ref);
2741 static void output_loc_operands (dw_loc_descr_ref);
2742 static void output_loc_sequence (dw_loc_descr_ref);
2743
2744 /* Convert a DWARF stack opcode into its string name.  */
2745
2746 static const char *
2747 dwarf_stack_op_name (unsigned int op)
2748 {
2749   switch (op)
2750     {
2751     case DW_OP_addr:
2752     case INTERNAL_DW_OP_tls_addr:
2753       return "DW_OP_addr";
2754     case DW_OP_deref:
2755       return "DW_OP_deref";
2756     case DW_OP_const1u:
2757       return "DW_OP_const1u";
2758     case DW_OP_const1s:
2759       return "DW_OP_const1s";
2760     case DW_OP_const2u:
2761       return "DW_OP_const2u";
2762     case DW_OP_const2s:
2763       return "DW_OP_const2s";
2764     case DW_OP_const4u:
2765       return "DW_OP_const4u";
2766     case DW_OP_const4s:
2767       return "DW_OP_const4s";
2768     case DW_OP_const8u:
2769       return "DW_OP_const8u";
2770     case DW_OP_const8s:
2771       return "DW_OP_const8s";
2772     case DW_OP_constu:
2773       return "DW_OP_constu";
2774     case DW_OP_consts:
2775       return "DW_OP_consts";
2776     case DW_OP_dup:
2777       return "DW_OP_dup";
2778     case DW_OP_drop:
2779       return "DW_OP_drop";
2780     case DW_OP_over:
2781       return "DW_OP_over";
2782     case DW_OP_pick:
2783       return "DW_OP_pick";
2784     case DW_OP_swap:
2785       return "DW_OP_swap";
2786     case DW_OP_rot:
2787       return "DW_OP_rot";
2788     case DW_OP_xderef:
2789       return "DW_OP_xderef";
2790     case DW_OP_abs:
2791       return "DW_OP_abs";
2792     case DW_OP_and:
2793       return "DW_OP_and";
2794     case DW_OP_div:
2795       return "DW_OP_div";
2796     case DW_OP_minus:
2797       return "DW_OP_minus";
2798     case DW_OP_mod:
2799       return "DW_OP_mod";
2800     case DW_OP_mul:
2801       return "DW_OP_mul";
2802     case DW_OP_neg:
2803       return "DW_OP_neg";
2804     case DW_OP_not:
2805       return "DW_OP_not";
2806     case DW_OP_or:
2807       return "DW_OP_or";
2808     case DW_OP_plus:
2809       return "DW_OP_plus";
2810     case DW_OP_plus_uconst:
2811       return "DW_OP_plus_uconst";
2812     case DW_OP_shl:
2813       return "DW_OP_shl";
2814     case DW_OP_shr:
2815       return "DW_OP_shr";
2816     case DW_OP_shra:
2817       return "DW_OP_shra";
2818     case DW_OP_xor:
2819       return "DW_OP_xor";
2820     case DW_OP_bra:
2821       return "DW_OP_bra";
2822     case DW_OP_eq:
2823       return "DW_OP_eq";
2824     case DW_OP_ge:
2825       return "DW_OP_ge";
2826     case DW_OP_gt:
2827       return "DW_OP_gt";
2828     case DW_OP_le:
2829       return "DW_OP_le";
2830     case DW_OP_lt:
2831       return "DW_OP_lt";
2832     case DW_OP_ne:
2833       return "DW_OP_ne";
2834     case DW_OP_skip:
2835       return "DW_OP_skip";
2836     case DW_OP_lit0:
2837       return "DW_OP_lit0";
2838     case DW_OP_lit1:
2839       return "DW_OP_lit1";
2840     case DW_OP_lit2:
2841       return "DW_OP_lit2";
2842     case DW_OP_lit3:
2843       return "DW_OP_lit3";
2844     case DW_OP_lit4:
2845       return "DW_OP_lit4";
2846     case DW_OP_lit5:
2847       return "DW_OP_lit5";
2848     case DW_OP_lit6:
2849       return "DW_OP_lit6";
2850     case DW_OP_lit7:
2851       return "DW_OP_lit7";
2852     case DW_OP_lit8:
2853       return "DW_OP_lit8";
2854     case DW_OP_lit9:
2855       return "DW_OP_lit9";
2856     case DW_OP_lit10:
2857       return "DW_OP_lit10";
2858     case DW_OP_lit11:
2859       return "DW_OP_lit11";
2860     case DW_OP_lit12:
2861       return "DW_OP_lit12";
2862     case DW_OP_lit13:
2863       return "DW_OP_lit13";
2864     case DW_OP_lit14:
2865       return "DW_OP_lit14";
2866     case DW_OP_lit15:
2867       return "DW_OP_lit15";
2868     case DW_OP_lit16:
2869       return "DW_OP_lit16";
2870     case DW_OP_lit17:
2871       return "DW_OP_lit17";
2872     case DW_OP_lit18:
2873       return "DW_OP_lit18";
2874     case DW_OP_lit19:
2875       return "DW_OP_lit19";
2876     case DW_OP_lit20:
2877       return "DW_OP_lit20";
2878     case DW_OP_lit21:
2879       return "DW_OP_lit21";
2880     case DW_OP_lit22:
2881       return "DW_OP_lit22";
2882     case DW_OP_lit23:
2883       return "DW_OP_lit23";
2884     case DW_OP_lit24:
2885       return "DW_OP_lit24";
2886     case DW_OP_lit25:
2887       return "DW_OP_lit25";
2888     case DW_OP_lit26:
2889       return "DW_OP_lit26";
2890     case DW_OP_lit27:
2891       return "DW_OP_lit27";
2892     case DW_OP_lit28:
2893       return "DW_OP_lit28";
2894     case DW_OP_lit29:
2895       return "DW_OP_lit29";
2896     case DW_OP_lit30:
2897       return "DW_OP_lit30";
2898     case DW_OP_lit31:
2899       return "DW_OP_lit31";
2900     case DW_OP_reg0:
2901       return "DW_OP_reg0";
2902     case DW_OP_reg1:
2903       return "DW_OP_reg1";
2904     case DW_OP_reg2:
2905       return "DW_OP_reg2";
2906     case DW_OP_reg3:
2907       return "DW_OP_reg3";
2908     case DW_OP_reg4:
2909       return "DW_OP_reg4";
2910     case DW_OP_reg5:
2911       return "DW_OP_reg5";
2912     case DW_OP_reg6:
2913       return "DW_OP_reg6";
2914     case DW_OP_reg7:
2915       return "DW_OP_reg7";
2916     case DW_OP_reg8:
2917       return "DW_OP_reg8";
2918     case DW_OP_reg9:
2919       return "DW_OP_reg9";
2920     case DW_OP_reg10:
2921       return "DW_OP_reg10";
2922     case DW_OP_reg11:
2923       return "DW_OP_reg11";
2924     case DW_OP_reg12:
2925       return "DW_OP_reg12";
2926     case DW_OP_reg13:
2927       return "DW_OP_reg13";
2928     case DW_OP_reg14:
2929       return "DW_OP_reg14";
2930     case DW_OP_reg15:
2931       return "DW_OP_reg15";
2932     case DW_OP_reg16:
2933       return "DW_OP_reg16";
2934     case DW_OP_reg17:
2935       return "DW_OP_reg17";
2936     case DW_OP_reg18:
2937       return "DW_OP_reg18";
2938     case DW_OP_reg19:
2939       return "DW_OP_reg19";
2940     case DW_OP_reg20:
2941       return "DW_OP_reg20";
2942     case DW_OP_reg21:
2943       return "DW_OP_reg21";
2944     case DW_OP_reg22:
2945       return "DW_OP_reg22";
2946     case DW_OP_reg23:
2947       return "DW_OP_reg23";
2948     case DW_OP_reg24:
2949       return "DW_OP_reg24";
2950     case DW_OP_reg25:
2951       return "DW_OP_reg25";
2952     case DW_OP_reg26:
2953       return "DW_OP_reg26";
2954     case DW_OP_reg27:
2955       return "DW_OP_reg27";
2956     case DW_OP_reg28:
2957       return "DW_OP_reg28";
2958     case DW_OP_reg29:
2959       return "DW_OP_reg29";
2960     case DW_OP_reg30:
2961       return "DW_OP_reg30";
2962     case DW_OP_reg31:
2963       return "DW_OP_reg31";
2964     case DW_OP_breg0:
2965       return "DW_OP_breg0";
2966     case DW_OP_breg1:
2967       return "DW_OP_breg1";
2968     case DW_OP_breg2:
2969       return "DW_OP_breg2";
2970     case DW_OP_breg3:
2971       return "DW_OP_breg3";
2972     case DW_OP_breg4:
2973       return "DW_OP_breg4";
2974     case DW_OP_breg5:
2975       return "DW_OP_breg5";
2976     case DW_OP_breg6:
2977       return "DW_OP_breg6";
2978     case DW_OP_breg7:
2979       return "DW_OP_breg7";
2980     case DW_OP_breg8:
2981       return "DW_OP_breg8";
2982     case DW_OP_breg9:
2983       return "DW_OP_breg9";
2984     case DW_OP_breg10:
2985       return "DW_OP_breg10";
2986     case DW_OP_breg11:
2987       return "DW_OP_breg11";
2988     case DW_OP_breg12:
2989       return "DW_OP_breg12";
2990     case DW_OP_breg13:
2991       return "DW_OP_breg13";
2992     case DW_OP_breg14:
2993       return "DW_OP_breg14";
2994     case DW_OP_breg15:
2995       return "DW_OP_breg15";
2996     case DW_OP_breg16:
2997       return "DW_OP_breg16";
2998     case DW_OP_breg17:
2999       return "DW_OP_breg17";
3000     case DW_OP_breg18:
3001       return "DW_OP_breg18";
3002     case DW_OP_breg19:
3003       return "DW_OP_breg19";
3004     case DW_OP_breg20:
3005       return "DW_OP_breg20";
3006     case DW_OP_breg21:
3007       return "DW_OP_breg21";
3008     case DW_OP_breg22:
3009       return "DW_OP_breg22";
3010     case DW_OP_breg23:
3011       return "DW_OP_breg23";
3012     case DW_OP_breg24:
3013       return "DW_OP_breg24";
3014     case DW_OP_breg25:
3015       return "DW_OP_breg25";
3016     case DW_OP_breg26:
3017       return "DW_OP_breg26";
3018     case DW_OP_breg27:
3019       return "DW_OP_breg27";
3020     case DW_OP_breg28:
3021       return "DW_OP_breg28";
3022     case DW_OP_breg29:
3023       return "DW_OP_breg29";
3024     case DW_OP_breg30:
3025       return "DW_OP_breg30";
3026     case DW_OP_breg31:
3027       return "DW_OP_breg31";
3028     case DW_OP_regx:
3029       return "DW_OP_regx";
3030     case DW_OP_fbreg:
3031       return "DW_OP_fbreg";
3032     case DW_OP_bregx:
3033       return "DW_OP_bregx";
3034     case DW_OP_piece:
3035       return "DW_OP_piece";
3036     case DW_OP_deref_size:
3037       return "DW_OP_deref_size";
3038     case DW_OP_xderef_size:
3039       return "DW_OP_xderef_size";
3040     case DW_OP_nop:
3041       return "DW_OP_nop";
3042     case DW_OP_push_object_address:
3043       return "DW_OP_push_object_address";
3044     case DW_OP_call2:
3045       return "DW_OP_call2";
3046     case DW_OP_call4:
3047       return "DW_OP_call4";
3048     case DW_OP_call_ref:
3049       return "DW_OP_call_ref";
3050     case DW_OP_GNU_push_tls_address:
3051       return "DW_OP_GNU_push_tls_address";
3052     default:
3053       return "OP_<unknown>";
3054     }
3055 }
3056
3057 /* Return a pointer to a newly allocated location description.  Location
3058    descriptions are simple expression terms that can be strung
3059    together to form more complicated location (address) descriptions.  */
3060
3061 static inline dw_loc_descr_ref
3062 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
3063                unsigned HOST_WIDE_INT oprnd2)
3064 {
3065   dw_loc_descr_ref descr = ggc_alloc_cleared (sizeof (dw_loc_descr_node));
3066
3067   descr->dw_loc_opc = op;
3068   descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
3069   descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
3070   descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
3071   descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
3072
3073   return descr;
3074 }
3075
3076 /* Add a location description term to a location description expression.  */
3077
3078 static inline void
3079 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
3080 {
3081   dw_loc_descr_ref *d;
3082
3083   /* Find the end of the chain.  */
3084   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
3085     ;
3086
3087   *d = descr;
3088 }
3089
3090 /* Return the size of a location descriptor.  */
3091
3092 static unsigned long
3093 size_of_loc_descr (dw_loc_descr_ref loc)
3094 {
3095   unsigned long size = 1;
3096
3097   switch (loc->dw_loc_opc)
3098     {
3099     case DW_OP_addr:
3100     case INTERNAL_DW_OP_tls_addr:
3101       size += DWARF2_ADDR_SIZE;
3102       break;
3103     case DW_OP_const1u:
3104     case DW_OP_const1s:
3105       size += 1;
3106       break;
3107     case DW_OP_const2u:
3108     case DW_OP_const2s:
3109       size += 2;
3110       break;
3111     case DW_OP_const4u:
3112     case DW_OP_const4s:
3113       size += 4;
3114       break;
3115     case DW_OP_const8u:
3116     case DW_OP_const8s:
3117       size += 8;
3118       break;
3119     case DW_OP_constu:
3120       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3121       break;
3122     case DW_OP_consts:
3123       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3124       break;
3125     case DW_OP_pick:
3126       size += 1;
3127       break;
3128     case DW_OP_plus_uconst:
3129       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3130       break;
3131     case DW_OP_skip:
3132     case DW_OP_bra:
3133       size += 2;
3134       break;
3135     case DW_OP_breg0:
3136     case DW_OP_breg1:
3137     case DW_OP_breg2:
3138     case DW_OP_breg3:
3139     case DW_OP_breg4:
3140     case DW_OP_breg5:
3141     case DW_OP_breg6:
3142     case DW_OP_breg7:
3143     case DW_OP_breg8:
3144     case DW_OP_breg9:
3145     case DW_OP_breg10:
3146     case DW_OP_breg11:
3147     case DW_OP_breg12:
3148     case DW_OP_breg13:
3149     case DW_OP_breg14:
3150     case DW_OP_breg15:
3151     case DW_OP_breg16:
3152     case DW_OP_breg17:
3153     case DW_OP_breg18:
3154     case DW_OP_breg19:
3155     case DW_OP_breg20:
3156     case DW_OP_breg21:
3157     case DW_OP_breg22:
3158     case DW_OP_breg23:
3159     case DW_OP_breg24:
3160     case DW_OP_breg25:
3161     case DW_OP_breg26:
3162     case DW_OP_breg27:
3163     case DW_OP_breg28:
3164     case DW_OP_breg29:
3165     case DW_OP_breg30:
3166     case DW_OP_breg31:
3167       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3168       break;
3169     case DW_OP_regx:
3170       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3171       break;
3172     case DW_OP_fbreg:
3173       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3174       break;
3175     case DW_OP_bregx:
3176       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3177       size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
3178       break;
3179     case DW_OP_piece:
3180       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3181       break;
3182     case DW_OP_deref_size:
3183     case DW_OP_xderef_size:
3184       size += 1;
3185       break;
3186     case DW_OP_call2:
3187       size += 2;
3188       break;
3189     case DW_OP_call4:
3190       size += 4;
3191       break;
3192     case DW_OP_call_ref:
3193       size += DWARF2_ADDR_SIZE;
3194       break;
3195     default:
3196       break;
3197     }
3198
3199   return size;
3200 }
3201
3202 /* Return the size of a series of location descriptors.  */
3203
3204 static unsigned long
3205 size_of_locs (dw_loc_descr_ref loc)
3206 {
3207   unsigned long size;
3208
3209   for (size = 0; loc != NULL; loc = loc->dw_loc_next)
3210     {
3211       loc->dw_loc_addr = size;
3212       size += size_of_loc_descr (loc);
3213     }
3214
3215   return size;
3216 }
3217
3218 /* Output location description stack opcode's operands (if any).  */
3219
3220 static void
3221 output_loc_operands (dw_loc_descr_ref loc)
3222 {
3223   dw_val_ref val1 = &loc->dw_loc_oprnd1;
3224   dw_val_ref val2 = &loc->dw_loc_oprnd2;
3225
3226   switch (loc->dw_loc_opc)
3227     {
3228 #ifdef DWARF2_DEBUGGING_INFO
3229     case DW_OP_addr:
3230       dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
3231       break;
3232     case DW_OP_const2u:
3233     case DW_OP_const2s:
3234       dw2_asm_output_data (2, val1->v.val_int, NULL);
3235       break;
3236     case DW_OP_const4u:
3237     case DW_OP_const4s:
3238       dw2_asm_output_data (4, val1->v.val_int, NULL);
3239       break;
3240     case DW_OP_const8u:
3241     case DW_OP_const8s:
3242       gcc_assert (HOST_BITS_PER_LONG >= 64);
3243       dw2_asm_output_data (8, val1->v.val_int, NULL);
3244       break;
3245     case DW_OP_skip:
3246     case DW_OP_bra:
3247       {
3248         int offset;
3249
3250         gcc_assert (val1->val_class == dw_val_class_loc);
3251         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
3252
3253         dw2_asm_output_data (2, offset, NULL);
3254       }
3255       break;
3256 #else
3257     case DW_OP_addr:
3258     case DW_OP_const2u:
3259     case DW_OP_const2s:
3260     case DW_OP_const4u:
3261     case DW_OP_const4s:
3262     case DW_OP_const8u:
3263     case DW_OP_const8s:
3264     case DW_OP_skip:
3265     case DW_OP_bra:
3266       /* We currently don't make any attempt to make sure these are
3267          aligned properly like we do for the main unwind info, so
3268          don't support emitting things larger than a byte if we're
3269          only doing unwinding.  */
3270       gcc_unreachable ();
3271 #endif
3272     case DW_OP_const1u:
3273     case DW_OP_const1s:
3274       dw2_asm_output_data (1, val1->v.val_int, NULL);
3275       break;
3276     case DW_OP_constu:
3277       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3278       break;
3279     case DW_OP_consts:
3280       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
3281       break;
3282     case DW_OP_pick:
3283       dw2_asm_output_data (1, val1->v.val_int, NULL);
3284       break;
3285     case DW_OP_plus_uconst:
3286       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3287       break;
3288     case DW_OP_breg0:
3289     case DW_OP_breg1:
3290     case DW_OP_breg2:
3291     case DW_OP_breg3:
3292     case DW_OP_breg4:
3293     case DW_OP_breg5:
3294     case DW_OP_breg6:
3295     case DW_OP_breg7:
3296     case DW_OP_breg8:
3297     case DW_OP_breg9:
3298     case DW_OP_breg10:
3299     case DW_OP_breg11:
3300     case DW_OP_breg12:
3301     case DW_OP_breg13:
3302     case DW_OP_breg14:
3303     case DW_OP_breg15:
3304     case DW_OP_breg16:
3305     case DW_OP_breg17:
3306     case DW_OP_breg18:
3307     case DW_OP_breg19:
3308     case DW_OP_breg20:
3309     case DW_OP_breg21:
3310     case DW_OP_breg22:
3311     case DW_OP_breg23:
3312     case DW_OP_breg24:
3313     case DW_OP_breg25:
3314     case DW_OP_breg26:
3315     case DW_OP_breg27:
3316     case DW_OP_breg28:
3317     case DW_OP_breg29:
3318     case DW_OP_breg30:
3319     case DW_OP_breg31:
3320       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
3321       break;
3322     case DW_OP_regx:
3323       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3324       break;
3325     case DW_OP_fbreg:
3326       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
3327       break;
3328     case DW_OP_bregx:
3329       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3330       dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
3331       break;
3332     case DW_OP_piece:
3333       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3334       break;
3335     case DW_OP_deref_size:
3336     case DW_OP_xderef_size:
3337       dw2_asm_output_data (1, val1->v.val_int, NULL);
3338       break;
3339
3340     case INTERNAL_DW_OP_tls_addr:
3341       if (targetm.asm_out.output_dwarf_dtprel)
3342         {
3343           targetm.asm_out.output_dwarf_dtprel (asm_out_file,
3344                                                DWARF2_ADDR_SIZE,
3345                                                val1->v.val_addr);
3346           fputc ('\n', asm_out_file);
3347         }
3348       else
3349         gcc_unreachable ();
3350       break;
3351
3352     default:
3353       /* Other codes have no operands.  */
3354       break;
3355     }
3356 }
3357
3358 /* Output a sequence of location operations.  */
3359
3360 static void
3361 output_loc_sequence (dw_loc_descr_ref loc)
3362 {
3363   for (; loc != NULL; loc = loc->dw_loc_next)
3364     {
3365       /* Output the opcode.  */
3366       dw2_asm_output_data (1, loc->dw_loc_opc,
3367                            "%s", dwarf_stack_op_name (loc->dw_loc_opc));
3368
3369       /* Output the operand(s) (if any).  */
3370       output_loc_operands (loc);
3371     }
3372 }
3373
3374 /* This routine will generate the correct assembly data for a location
3375    description based on a cfi entry with a complex address.  */
3376
3377 static void
3378 output_cfa_loc (dw_cfi_ref cfi)
3379 {
3380   dw_loc_descr_ref loc;
3381   unsigned long size;
3382
3383   /* Output the size of the block.  */
3384   loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
3385   size = size_of_locs (loc);
3386   dw2_asm_output_data_uleb128 (size, NULL);
3387
3388   /* Now output the operations themselves.  */
3389   output_loc_sequence (loc);
3390 }
3391
3392 /* This function builds a dwarf location descriptor sequence from
3393    a dw_cfa_location.  */
3394
3395 static struct dw_loc_descr_struct *
3396 build_cfa_loc (dw_cfa_location *cfa)
3397 {
3398   struct dw_loc_descr_struct *head, *tmp;
3399
3400   if (cfa->indirect)
3401     {
3402       if (cfa->base_offset)
3403         {
3404           if (cfa->reg <= 31)
3405             head = new_loc_descr (DW_OP_breg0 + cfa->reg, cfa->base_offset, 0);
3406           else
3407             head = new_loc_descr (DW_OP_bregx, cfa->reg, cfa->base_offset);
3408         }
3409       else if (cfa->reg <= 31)
3410         head = new_loc_descr (DW_OP_reg0 + cfa->reg, 0, 0);
3411       else
3412         head = new_loc_descr (DW_OP_regx, cfa->reg, 0);
3413
3414       head->dw_loc_oprnd1.val_class = dw_val_class_const;
3415       tmp = new_loc_descr (DW_OP_deref, 0, 0);
3416       add_loc_descr (&head, tmp);
3417       if (cfa->offset != 0)
3418         {
3419           tmp = new_loc_descr (DW_OP_plus_uconst, cfa->offset, 0);
3420           add_loc_descr (&head, tmp);
3421         }
3422     }
3423   else
3424     {
3425       if (cfa->offset == 0)
3426         if (cfa->reg <= 31)
3427           head = new_loc_descr (DW_OP_reg0 + cfa->reg, 0, 0);
3428         else
3429           head = new_loc_descr (DW_OP_regx, cfa->reg, 0);
3430       else if (cfa->reg <= 31)
3431         head = new_loc_descr (DW_OP_breg0 + cfa->reg, cfa->offset, 0);
3432       else
3433         head = new_loc_descr (DW_OP_bregx, cfa->reg, cfa->offset);
3434     }
3435
3436   return head;
3437 }
3438
3439 /* This function fills in aa dw_cfa_location structure from a dwarf location
3440    descriptor sequence.  */
3441
3442 static void
3443 get_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_struct *loc)
3444 {
3445   struct dw_loc_descr_struct *ptr;
3446   cfa->offset = 0;
3447   cfa->base_offset = 0;
3448   cfa->indirect = 0;
3449   cfa->reg = -1;
3450
3451   for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
3452     {
3453       enum dwarf_location_atom op = ptr->dw_loc_opc;
3454
3455       switch (op)
3456         {
3457         case DW_OP_reg0:
3458         case DW_OP_reg1:
3459         case DW_OP_reg2:
3460         case DW_OP_reg3:
3461         case DW_OP_reg4:
3462         case DW_OP_reg5:
3463         case DW_OP_reg6:
3464         case DW_OP_reg7:
3465         case DW_OP_reg8:
3466         case DW_OP_reg9:
3467         case DW_OP_reg10:
3468         case DW_OP_reg11:
3469         case DW_OP_reg12:
3470         case DW_OP_reg13:
3471         case DW_OP_reg14:
3472         case DW_OP_reg15:
3473         case DW_OP_reg16:
3474         case DW_OP_reg17:
3475         case DW_OP_reg18:
3476         case DW_OP_reg19:
3477         case DW_OP_reg20:
3478         case DW_OP_reg21:
3479         case DW_OP_reg22:
3480         case DW_OP_reg23:
3481         case DW_OP_reg24:
3482         case DW_OP_reg25:
3483         case DW_OP_reg26:
3484         case DW_OP_reg27:
3485         case DW_OP_reg28:
3486         case DW_OP_reg29:
3487         case DW_OP_reg30:
3488         case DW_OP_reg31:
3489           cfa->reg = op - DW_OP_reg0;
3490           break;
3491         case DW_OP_regx:
3492           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
3493           break;
3494         case DW_OP_breg0:
3495         case DW_OP_breg1:
3496         case DW_OP_breg2:
3497         case DW_OP_breg3:
3498         case DW_OP_breg4:
3499         case DW_OP_breg5:
3500         case DW_OP_breg6:
3501         case DW_OP_breg7:
3502         case DW_OP_breg8:
3503         case DW_OP_breg9:
3504         case DW_OP_breg10:
3505         case DW_OP_breg11:
3506         case DW_OP_breg12:
3507         case DW_OP_breg13:
3508         case DW_OP_breg14:
3509         case DW_OP_breg15:
3510         case DW_OP_breg16:
3511         case DW_OP_breg17:
3512         case DW_OP_breg18:
3513         case DW_OP_breg19:
3514         case DW_OP_breg20:
3515         case DW_OP_breg21:
3516         case DW_OP_breg22:
3517         case DW_OP_breg23:
3518         case DW_OP_breg24:
3519         case DW_OP_breg25:
3520         case DW_OP_breg26:
3521         case DW_OP_breg27:
3522         case DW_OP_breg28:
3523         case DW_OP_breg29:
3524         case DW_OP_breg30:
3525         case DW_OP_breg31:
3526           cfa->reg = op - DW_OP_breg0;
3527           cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
3528           break;
3529         case DW_OP_bregx:
3530           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
3531           cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
3532           break;
3533         case DW_OP_deref:
3534           cfa->indirect = 1;
3535           break;
3536         case DW_OP_plus_uconst:
3537           cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
3538           break;
3539         default:
3540           internal_error ("DW_LOC_OP %s not implemented",
3541                           dwarf_stack_op_name (ptr->dw_loc_opc));
3542         }
3543     }
3544 }
3545 #endif /* .debug_frame support */
3546 \f
3547 /* And now, the support for symbolic debugging information.  */
3548 #ifdef DWARF2_DEBUGGING_INFO
3549
3550 /* .debug_str support.  */
3551 static int output_indirect_string (void **, void *);
3552
3553 static void dwarf2out_init (const char *);
3554 static void dwarf2out_finish (const char *);
3555 static void dwarf2out_define (unsigned int, const char *);
3556 static void dwarf2out_undef (unsigned int, const char *);
3557 static void dwarf2out_start_source_file (unsigned, const char *);
3558 static void dwarf2out_end_source_file (unsigned);
3559 static void dwarf2out_begin_block (unsigned, unsigned);
3560 static void dwarf2out_end_block (unsigned, unsigned);
3561 static bool dwarf2out_ignore_block (tree);
3562 static void dwarf2out_global_decl (tree);
3563 static void dwarf2out_type_decl (tree, int);
3564 static void dwarf2out_imported_module_or_decl (tree, tree);
3565 static void dwarf2out_abstract_function (tree);
3566 static void dwarf2out_var_location (rtx);
3567 static void dwarf2out_begin_function (tree);
3568
3569 /* The debug hooks structure.  */
3570
3571 const struct gcc_debug_hooks dwarf2_debug_hooks =
3572 {
3573   dwarf2out_init,
3574   dwarf2out_finish,
3575   dwarf2out_define,
3576   dwarf2out_undef,
3577   dwarf2out_start_source_file,
3578   dwarf2out_end_source_file,
3579   dwarf2out_begin_block,
3580   dwarf2out_end_block,
3581   dwarf2out_ignore_block,
3582   dwarf2out_source_line,
3583   dwarf2out_begin_prologue,
3584   debug_nothing_int_charstar,   /* end_prologue */
3585   dwarf2out_end_epilogue,
3586   dwarf2out_begin_function,
3587   debug_nothing_int,            /* end_function */
3588   dwarf2out_decl,               /* function_decl */
3589   dwarf2out_global_decl,
3590   dwarf2out_type_decl,          /* type_decl */
3591   dwarf2out_imported_module_or_decl,
3592   debug_nothing_tree,           /* deferred_inline_function */
3593   /* The DWARF 2 backend tries to reduce debugging bloat by not
3594      emitting the abstract description of inline functions until
3595      something tries to reference them.  */
3596   dwarf2out_abstract_function,  /* outlining_inline_function */
3597   debug_nothing_rtx,            /* label */
3598   debug_nothing_int,            /* handle_pch */
3599   dwarf2out_var_location,
3600   dwarf2out_switch_text_section,
3601   1                             /* start_end_main_source_file */
3602 };
3603 #endif
3604 \f
3605 /* NOTE: In the comments in this file, many references are made to
3606    "Debugging Information Entries".  This term is abbreviated as `DIE'
3607    throughout the remainder of this file.  */
3608
3609 /* An internal representation of the DWARF output is built, and then
3610    walked to generate the DWARF debugging info.  The walk of the internal
3611    representation is done after the entire program has been compiled.
3612    The types below are used to describe the internal representation.  */
3613
3614 /* Various DIE's use offsets relative to the beginning of the
3615    .debug_info section to refer to each other.  */
3616
3617 typedef long int dw_offset;
3618
3619 /* Define typedefs here to avoid circular dependencies.  */
3620
3621 typedef struct dw_attr_struct *dw_attr_ref;
3622 typedef struct dw_line_info_struct *dw_line_info_ref;
3623 typedef struct dw_separate_line_info_struct *dw_separate_line_info_ref;
3624 typedef struct pubname_struct *pubname_ref;
3625 typedef struct dw_ranges_struct *dw_ranges_ref;
3626
3627 /* Each entry in the line_info_table maintains the file and
3628    line number associated with the label generated for that
3629    entry.  The label gives the PC value associated with
3630    the line number entry.  */
3631
3632 typedef struct dw_line_info_struct GTY(())
3633 {
3634   unsigned long dw_file_num;
3635   unsigned long dw_line_num;
3636 }
3637 dw_line_info_entry;
3638
3639 /* Line information for functions in separate sections; each one gets its
3640    own sequence.  */
3641 typedef struct dw_separate_line_info_struct GTY(())
3642 {
3643   unsigned long dw_file_num;
3644   unsigned long dw_line_num;
3645   unsigned long function;
3646 }
3647 dw_separate_line_info_entry;
3648
3649 /* Each DIE attribute has a field specifying the attribute kind,
3650    a link to the next attribute in the chain, and an attribute value.
3651    Attributes are typically linked below the DIE they modify.  */
3652
3653 typedef struct dw_attr_struct GTY(())
3654 {
3655   enum dwarf_attribute dw_attr;
3656   dw_attr_ref dw_attr_next;
3657   dw_val_node dw_attr_val;
3658 }
3659 dw_attr_node;
3660
3661 /* The Debugging Information Entry (DIE) structure */
3662
3663 typedef struct die_struct GTY(())
3664 {
3665   enum dwarf_tag die_tag;
3666   char *die_symbol;
3667   dw_attr_ref die_attr;
3668   dw_die_ref die_parent;
3669   dw_die_ref die_child;
3670   dw_die_ref die_sib;
3671   dw_die_ref die_definition; /* ref from a specification to its definition */
3672   dw_offset die_offset;
3673   unsigned long die_abbrev;
3674   int die_mark;
3675   unsigned int decl_id;
3676 }
3677 die_node;
3678
3679 /* The pubname structure */
3680
3681 typedef struct pubname_struct GTY(())
3682 {
3683   dw_die_ref die;
3684   char *name;
3685 }
3686 pubname_entry;
3687
3688 struct dw_ranges_struct GTY(())
3689 {
3690   int block_num;
3691 };
3692
3693 /* The limbo die list structure.  */
3694 typedef struct limbo_die_struct GTY(())
3695 {
3696   dw_die_ref die;
3697   tree created_for;
3698   struct limbo_die_struct *next;
3699 }
3700 limbo_die_node;
3701
3702 /* How to start an assembler comment.  */
3703 #ifndef ASM_COMMENT_START
3704 #define ASM_COMMENT_START ";#"
3705 #endif
3706
3707 /* Define a macro which returns nonzero for a TYPE_DECL which was
3708    implicitly generated for a tagged type.
3709
3710    Note that unlike the gcc front end (which generates a NULL named
3711    TYPE_DECL node for each complete tagged type, each array type, and
3712    each function type node created) the g++ front end generates a
3713    _named_ TYPE_DECL node for each tagged type node created.
3714    These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
3715    generate a DW_TAG_typedef DIE for them.  */
3716
3717 #define TYPE_DECL_IS_STUB(decl)                         \
3718   (DECL_NAME (decl) == NULL_TREE                        \
3719    || (DECL_ARTIFICIAL (decl)                           \
3720        && is_tagged_type (TREE_TYPE (decl))             \
3721        && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl)))  \
3722            /* This is necessary for stub decls that     \
3723               appear in nested inline functions.  */    \
3724            || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
3725                && (decl_ultimate_origin (decl)          \
3726                    == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
3727
3728 /* Information concerning the compilation unit's programming
3729    language, and compiler version.  */
3730
3731 /* Fixed size portion of the DWARF compilation unit header.  */
3732 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
3733   (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
3734
3735 /* Fixed size portion of public names info.  */
3736 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
3737
3738 /* Fixed size portion of the address range info.  */
3739 #define DWARF_ARANGES_HEADER_SIZE                                       \
3740   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4,      \
3741                 DWARF2_ADDR_SIZE * 2)                                   \
3742    - DWARF_INITIAL_LENGTH_SIZE)
3743
3744 /* Size of padding portion in the address range info.  It must be
3745    aligned to twice the pointer size.  */
3746 #define DWARF_ARANGES_PAD_SIZE \
3747   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3748                 DWARF2_ADDR_SIZE * 2) \
3749    - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
3750
3751 /* Use assembler line directives if available.  */
3752 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
3753 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
3754 #define DWARF2_ASM_LINE_DEBUG_INFO 1
3755 #else
3756 #define DWARF2_ASM_LINE_DEBUG_INFO 0
3757 #endif
3758 #endif
3759
3760 /* Minimum line offset in a special line info. opcode.
3761    This value was chosen to give a reasonable range of values.  */
3762 #define DWARF_LINE_BASE  -10
3763
3764 /* First special line opcode - leave room for the standard opcodes.  */
3765 #define DWARF_LINE_OPCODE_BASE  10
3766
3767 /* Range of line offsets in a special line info. opcode.  */
3768 #define DWARF_LINE_RANGE  (254-DWARF_LINE_OPCODE_BASE+1)
3769
3770 /* Flag that indicates the initial value of the is_stmt_start flag.
3771    In the present implementation, we do not mark any lines as
3772    the beginning of a source statement, because that information
3773    is not made available by the GCC front-end.  */
3774 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
3775
3776 #ifdef DWARF2_DEBUGGING_INFO
3777 /* This location is used by calc_die_sizes() to keep track
3778    the offset of each DIE within the .debug_info section.  */
3779 static unsigned long next_die_offset;
3780 #endif
3781
3782 /* Record the root of the DIE's built for the current compilation unit.  */
3783 static GTY(()) dw_die_ref comp_unit_die;
3784
3785 /* A list of DIEs with a NULL parent waiting to be relocated.  */
3786 static GTY(()) limbo_die_node *limbo_die_list;
3787
3788 /* Filenames referenced by this compilation unit.  */
3789 static GTY(()) varray_type file_table;
3790 static GTY(()) varray_type file_table_emitted;
3791 static GTY(()) size_t file_table_last_lookup_index;
3792
3793 /* A hash table of references to DIE's that describe declarations.
3794    The key is a DECL_UID() which is a unique number identifying each decl.  */
3795 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
3796
3797 /* Node of the variable location list.  */
3798 struct var_loc_node GTY ((chain_next ("%h.next")))
3799 {
3800   rtx GTY (()) var_loc_note;
3801   const char * GTY (()) label;
3802   const char * GTY (()) section_label;
3803   struct var_loc_node * GTY (()) next;
3804 };
3805
3806 /* Variable location list.  */
3807 struct var_loc_list_def GTY (())
3808 {
3809   struct var_loc_node * GTY (()) first;
3810
3811   /* Do not mark the last element of the chained list because
3812      it is marked through the chain.  */
3813   struct var_loc_node * GTY ((skip ("%h"))) last;
3814
3815   /* DECL_UID of the variable decl.  */
3816   unsigned int decl_id;
3817 };
3818 typedef struct var_loc_list_def var_loc_list;
3819
3820
3821 /* Table of decl location linked lists.  */
3822 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
3823
3824 /* A pointer to the base of a list of references to DIE's that
3825    are uniquely identified by their tag, presence/absence of
3826    children DIE's, and list of attribute/value pairs.  */
3827 static GTY((length ("abbrev_die_table_allocated")))
3828   dw_die_ref *abbrev_die_table;
3829
3830 /* Number of elements currently allocated for abbrev_die_table.  */
3831 static GTY(()) unsigned abbrev_die_table_allocated;
3832
3833 /* Number of elements in type_die_table currently in use.  */
3834 static GTY(()) unsigned abbrev_die_table_in_use;
3835
3836 /* Size (in elements) of increments by which we may expand the
3837    abbrev_die_table.  */
3838 #define ABBREV_DIE_TABLE_INCREMENT 256
3839
3840 /* A pointer to the base of a table that contains line information
3841    for each source code line in .text in the compilation unit.  */
3842 static GTY((length ("line_info_table_allocated")))
3843      dw_line_info_ref line_info_table;
3844
3845 /* Number of elements currently allocated for line_info_table.  */
3846 static GTY(()) unsigned line_info_table_allocated;
3847
3848 /* Number of elements in line_info_table currently in use.  */
3849 static GTY(()) unsigned line_info_table_in_use;
3850
3851 /* True if the compilation unit contains more than one .text section.  */
3852 static GTY(()) bool have_switched_text_section = false;
3853
3854 /* A pointer to the base of a table that contains line information
3855    for each source code line outside of .text in the compilation unit.  */
3856 static GTY ((length ("separate_line_info_table_allocated")))
3857      dw_separate_line_info_ref separate_line_info_table;
3858
3859 /* Number of elements currently allocated for separate_line_info_table.  */
3860 static GTY(()) unsigned separate_line_info_table_allocated;
3861
3862 /* Number of elements in separate_line_info_table currently in use.  */
3863 static GTY(()) unsigned separate_line_info_table_in_use;
3864
3865 /* Size (in elements) of increments by which we may expand the
3866    line_info_table.  */
3867 #define LINE_INFO_TABLE_INCREMENT 1024
3868
3869 /* A pointer to the base of a table that contains a list of publicly
3870    accessible names.  */
3871 static GTY ((length ("pubname_table_allocated"))) pubname_ref pubname_table;
3872
3873 /* Number of elements currently allocated for pubname_table.  */
3874 static GTY(()) unsigned pubname_table_allocated;
3875
3876 /* Number of elements in pubname_table currently in use.  */
3877 static GTY(()) unsigned pubname_table_in_use;
3878
3879 /* Size (in elements) of increments by which we may expand the
3880    pubname_table.  */
3881 #define PUBNAME_TABLE_INCREMENT 64
3882
3883 /* Array of dies for which we should generate .debug_arange info.  */
3884 static GTY((length ("arange_table_allocated"))) dw_die_ref *arange_table;
3885
3886 /* Number of elements currently allocated for arange_table.  */
3887 static GTY(()) unsigned arange_table_allocated;
3888
3889 /* Number of elements in arange_table currently in use.  */
3890 static GTY(()) unsigned arange_table_in_use;
3891
3892 /* Size (in elements) of increments by which we may expand the
3893    arange_table.  */
3894 #define ARANGE_TABLE_INCREMENT 64
3895
3896 /* Array of dies for which we should generate .debug_ranges info.  */
3897 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
3898
3899 /* Number of elements currently allocated for ranges_table.  */
3900 static GTY(()) unsigned ranges_table_allocated;
3901
3902 /* Number of elements in ranges_table currently in use.  */
3903 static GTY(()) unsigned ranges_table_in_use;
3904
3905 /* Size (in elements) of increments by which we may expand the
3906    ranges_table.  */
3907 #define RANGES_TABLE_INCREMENT 64
3908
3909 /* Whether we have location lists that need outputting */
3910 static GTY(()) unsigned have_location_lists;
3911
3912 /* Unique label counter.  */
3913 static GTY(()) unsigned int loclabel_num;
3914
3915 #ifdef DWARF2_DEBUGGING_INFO
3916 /* Record whether the function being analyzed contains inlined functions.  */
3917 static int current_function_has_inlines;
3918 #endif
3919 #if 0 && defined (MIPS_DEBUGGING_INFO)
3920 static int comp_unit_has_inlines;
3921 #endif
3922
3923 /* Number of file tables emitted in maybe_emit_file().  */
3924 static GTY(()) int emitcount = 0;
3925
3926 /* Number of internal labels generated by gen_internal_sym().  */
3927 static GTY(()) int label_num;
3928
3929 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
3930 void
3931 dwarf2out_switch_text_section (void)
3932 {
3933   dw_fde_ref fde;
3934
3935   gcc_assert (cfun);
3936
3937   fde = &fde_table[fde_table_in_use - 1];
3938   fde->dw_fde_switched_sections = true;
3939   fde->dw_fde_hot_section_label = cfun->hot_section_label;
3940   fde->dw_fde_hot_section_end_label = cfun->hot_section_end_label;
3941   fde->dw_fde_unlikely_section_label = cfun->cold_section_label;
3942   fde->dw_fde_unlikely_section_end_label = cfun->cold_section_end_label;
3943   have_switched_text_section = true;
3944
3945   /* Reset the current label on switching text sections, so that we
3946      don't attempt to advance_loc4 between labels in different sections.  */
3947   fde->dw_fde_current_label = NULL;
3948 }
3949
3950 #endif
3951
3952 #ifdef DWARF2_DEBUGGING_INFO
3953
3954 /* Offset from the "steady-state frame pointer" to the CFA,
3955    within the current function.  */
3956 static HOST_WIDE_INT frame_pointer_cfa_offset;
3957
3958 /* Forward declarations for functions defined in this file.  */
3959
3960 static int is_pseudo_reg (rtx);
3961 static tree type_main_variant (tree);
3962 static int is_tagged_type (tree);
3963 static const char *dwarf_tag_name (unsigned);
3964 static const char *dwarf_attr_name (unsigned);
3965 static const char *dwarf_form_name (unsigned);
3966 static tree decl_ultimate_origin (tree);
3967 static tree block_ultimate_origin (tree);
3968 static tree decl_class_context (tree);
3969 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
3970 static inline enum dw_val_class AT_class (dw_attr_ref);
3971 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3972 static inline unsigned AT_flag (dw_attr_ref);
3973 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3974 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
3975 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3976 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
3977 static void add_AT_long_long (dw_die_ref, enum dwarf_attribute, unsigned long,
3978                               unsigned long);
3979 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3980                                unsigned int, unsigned char *);
3981 static hashval_t debug_str_do_hash (const void *);
3982 static int debug_str_eq (const void *, const void *);
3983 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3984 static inline const char *AT_string (dw_attr_ref);
3985 static int AT_string_form (dw_attr_ref);
3986 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3987 static void add_AT_specification (dw_die_ref, dw_die_ref);
3988 static inline dw_die_ref AT_ref (dw_attr_ref);
3989 static inline int AT_ref_external (dw_attr_ref);
3990 static inline void set_AT_ref_external (dw_attr_ref, int);
3991 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
3992 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3993 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
3994 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3995                              dw_loc_list_ref);
3996 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
3997 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
3998 static inline rtx AT_addr (dw_attr_ref);
3999 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
4000 static void add_AT_lbl_offset (dw_die_ref, enum dwarf_attribute, const char *);
4001 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
4002                            unsigned HOST_WIDE_INT);
4003 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
4004                                unsigned long);
4005 static inline const char *AT_lbl (dw_attr_ref);
4006 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
4007 static const char *get_AT_low_pc (dw_die_ref);
4008 static const char *get_AT_hi_pc (dw_die_ref);
4009 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
4010 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
4011 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
4012 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
4013 static bool is_c_family (void);
4014 static bool is_cxx (void);
4015 static bool is_java (void);
4016 static bool is_fortran (void);
4017 static bool is_ada (void);
4018 static void remove_AT (dw_die_ref, enum dwarf_attribute);
4019 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
4020 static inline void free_die (dw_die_ref);
4021 static void remove_children (dw_die_ref);
4022 static void add_child_die (dw_die_ref, dw_die_ref);
4023 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
4024 static dw_die_ref lookup_type_die (tree);
4025 static void equate_type_number_to_die (tree, dw_die_ref);
4026 static hashval_t decl_die_table_hash (const void *);
4027 static int decl_die_table_eq (const void *, const void *);
4028 static dw_die_ref lookup_decl_die (tree);
4029 static hashval_t decl_loc_table_hash (const void *);
4030 static int decl_loc_table_eq (const void *, const void *);
4031 static var_loc_list *lookup_decl_loc (tree);
4032 static void equate_decl_number_to_die (tree, dw_die_ref);
4033 static void add_var_loc_to_decl (tree, struct var_loc_node *);
4034 static void print_spaces (FILE *);
4035 static void print_die (dw_die_ref, FILE *);
4036 static void print_dwarf_line_table (FILE *);
4037 static void reverse_die_lists (dw_die_ref);
4038 static void reverse_all_dies (dw_die_ref);
4039 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
4040 static dw_die_ref pop_compile_unit (dw_die_ref);
4041 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
4042 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
4043 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
4044 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
4045 static int same_dw_val_p (dw_val_node *, dw_val_node *, int *);
4046 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
4047 static int same_die_p (dw_die_ref, dw_die_ref, int *);
4048 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
4049 static void compute_section_prefix (dw_die_ref);
4050 static int is_type_die (dw_die_ref);
4051 static int is_comdat_die (dw_die_ref);
4052 static int is_symbol_die (dw_die_ref);
4053 static void assign_symbol_names (dw_die_ref);
4054 static void break_out_includes (dw_die_ref);
4055 static hashval_t htab_cu_hash (const void *);
4056 static int htab_cu_eq (const void *, const void *);
4057 static void htab_cu_del (void *);
4058 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
4059 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
4060 static void add_sibling_attributes (dw_die_ref);
4061 static void build_abbrev_table (dw_die_ref);
4062 static void output_location_lists (dw_die_ref);
4063 static int constant_size (long unsigned);
4064 static unsigned long size_of_die (dw_die_ref);
4065 static void calc_die_sizes (dw_die_ref);
4066 static void mark_dies (dw_die_ref);
4067 static void unmark_dies (dw_die_ref);
4068 static void unmark_all_dies (dw_die_ref);
4069 static unsigned long size_of_pubnames (void);
4070 static unsigned long size_of_aranges (void);
4071 static enum dwarf_form value_format (dw_attr_ref);
4072 static void output_value_format (dw_attr_ref);
4073 static void output_abbrev_section (void);
4074 static void output_die_symbol (dw_die_ref);
4075 static void output_die (dw_die_ref);
4076 static void output_compilation_unit_header (void);
4077 static void output_comp_unit (dw_die_ref, int);
4078 static const char *dwarf2_name (tree, int);
4079 static void add_pubname (tree, dw_die_ref);
4080 static void output_pubnames (void);
4081 static void add_arange (tree, dw_die_ref);
4082 static void output_aranges (void);
4083 static unsigned int add_ranges (tree);
4084 static void output_ranges (void);
4085 static void output_line_info (void);
4086 static void output_file_names (void);
4087 static dw_die_ref base_type_die (tree);
4088 static tree root_type (tree);
4089 static int is_base_type (tree);
4090 static bool is_subrange_type (tree);
4091 static dw_die_ref subrange_type_die (tree, dw_die_ref);
4092 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
4093 static int type_is_enum (tree);
4094 static unsigned int dbx_reg_number (rtx);
4095 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
4096 static dw_loc_descr_ref reg_loc_descriptor (rtx);
4097 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int);
4098 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx);
4099 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
4100 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT);
4101 static int is_based_loc (rtx);
4102 static dw_loc_descr_ref mem_loc_descriptor (rtx, enum machine_mode mode);
4103 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx);
4104 static dw_loc_descr_ref loc_descriptor (rtx);
4105 static dw_loc_descr_ref loc_descriptor_from_tree_1 (tree, int);
4106 static dw_loc_descr_ref loc_descriptor_from_tree (tree);
4107 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
4108 static tree field_type (tree);
4109 static unsigned int simple_type_align_in_bits (tree);
4110 static unsigned int simple_decl_align_in_bits (tree);
4111 static unsigned HOST_WIDE_INT simple_type_size_in_bits (tree);
4112 static HOST_WIDE_INT field_byte_offset (tree);
4113 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
4114                                          dw_loc_descr_ref);
4115 static void add_data_member_location_attribute (dw_die_ref, tree);
4116 static void add_const_value_attribute (dw_die_ref, rtx);
4117 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
4118 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
4119 static void insert_float (rtx, unsigned char *);
4120 static rtx rtl_for_decl_location (tree);
4121 static void add_location_or_const_value_attribute (dw_die_ref, tree,
4122                                                    enum dwarf_attribute);
4123 static void tree_add_const_value_attribute (dw_die_ref, tree);
4124 static void add_name_attribute (dw_die_ref, const char *);
4125 static void add_comp_dir_attribute (dw_die_ref);
4126 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
4127 static void add_subscript_info (dw_die_ref, tree);
4128 static void add_byte_size_attribute (dw_die_ref, tree);
4129 static void add_bit_offset_attribute (dw_die_ref, tree);
4130 static void add_bit_size_attribute (dw_die_ref, tree);
4131 static void add_prototyped_attribute (dw_die_ref, tree);
4132 static void add_abstract_origin_attribute (dw_die_ref, tree);
4133 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
4134 static void add_src_coords_attributes (dw_die_ref, tree);
4135 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
4136 static void push_decl_scope (tree);
4137 static void pop_decl_scope (void);
4138 static dw_die_ref scope_die_for (tree, dw_die_ref);
4139 static inline int local_scope_p (dw_die_ref);
4140 static inline int class_or_namespace_scope_p (dw_die_ref);
4141 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
4142 static void add_calling_convention_attribute (dw_die_ref, tree);
4143 static const char *type_tag (tree);
4144 static tree member_declared_type (tree);
4145 #if 0
4146 static const char *decl_start_label (tree);
4147 #endif
4148 static void gen_array_type_die (tree, dw_die_ref);
4149 #if 0
4150 static void gen_entry_point_die (tree, dw_die_ref);
4151 #endif
4152 static void gen_inlined_enumeration_type_die (tree, dw_die_ref);
4153 static void gen_inlined_structure_type_die (tree, dw_die_ref);
4154 static void gen_inlined_union_type_die (tree, dw_die_ref);
4155 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
4156 static dw_die_ref gen_formal_parameter_die (tree, dw_die_ref);
4157 static void gen_unspecified_parameters_die (tree, dw_die_ref);
4158 static void gen_formal_types_die (tree, dw_die_ref);
4159 static void gen_subprogram_die (tree, dw_die_ref);
4160 static void gen_variable_die (tree, dw_die_ref);
4161 static void gen_label_die (tree, dw_die_ref);
4162 static void gen_lexical_block_die (tree, dw_die_ref, int);
4163 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
4164 static void gen_field_die (tree, dw_die_ref);
4165 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
4166 static dw_die_ref gen_compile_unit_die (const char *);
4167 static void gen_string_type_die (tree, dw_die_ref);
4168 static void gen_inheritance_die (tree, tree, dw_die_ref);
4169 static void gen_member_die (tree, dw_die_ref);
4170 static void gen_struct_or_union_type_die (tree, dw_die_ref);
4171 static void gen_subroutine_type_die (tree, dw_die_ref);
4172 static void gen_typedef_die (tree, dw_die_ref);
4173 static void gen_type_die (tree, dw_die_ref);
4174 static void gen_tagged_type_instantiation_die (tree, dw_die_ref);
4175 static void gen_block_die (tree, dw_die_ref, int);
4176 static void decls_for_scope (tree, dw_die_ref, int);
4177 static int is_redundant_typedef (tree);
4178 static void gen_namespace_die (tree);
4179 static void gen_decl_die (tree, dw_die_ref);
4180 static dw_die_ref force_decl_die (tree);
4181 static dw_die_ref force_type_die (tree);
4182 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
4183 static void declare_in_namespace (tree, dw_die_ref);
4184 static unsigned lookup_filename (const char *);
4185 static void init_file_table (void);
4186 static void retry_incomplete_types (void);
4187 static void gen_type_die_for_member (tree, tree, dw_die_ref);
4188 static void splice_child_die (dw_die_ref, dw_die_ref);
4189 static int file_info_cmp (const void *, const void *);
4190 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
4191                                      const char *, const char *, unsigned);
4192 static void add_loc_descr_to_loc_list (dw_loc_list_ref *, dw_loc_descr_ref,
4193                                        const char *, const char *,
4194                                        const char *);
4195 static void output_loc_list (dw_loc_list_ref);
4196 static char *gen_internal_sym (const char *);
4197
4198 static void prune_unmark_dies (dw_die_ref);
4199 static void prune_unused_types_mark (dw_die_ref, int);
4200 static void prune_unused_types_walk (dw_die_ref);
4201 static void prune_unused_types_walk_attribs (dw_die_ref);
4202 static void prune_unused_types_prune (dw_die_ref);
4203 static void prune_unused_types (void);
4204 static int maybe_emit_file (int);
4205
4206 /* Section names used to hold DWARF debugging information.  */
4207 #ifndef DEBUG_INFO_SECTION
4208 #define DEBUG_INFO_SECTION      ".debug_info"
4209 #endif
4210 #ifndef DEBUG_ABBREV_SECTION
4211 #define DEBUG_ABBREV_SECTION    ".debug_abbrev"
4212 #endif
4213 #ifndef DEBUG_ARANGES_SECTION
4214 #define DEBUG_ARANGES_SECTION   ".debug_aranges"
4215 #endif
4216 #ifndef DEBUG_MACINFO_SECTION
4217 #define DEBUG_MACINFO_SECTION   ".debug_macinfo"
4218 #endif
4219 #ifndef DEBUG_LINE_SECTION
4220 #define DEBUG_LINE_SECTION      ".debug_line"
4221 #endif
4222 #ifndef DEBUG_LOC_SECTION
4223 #define DEBUG_LOC_SECTION       ".debug_loc"
4224 #endif
4225 #ifndef DEBUG_PUBNAMES_SECTION
4226 #define DEBUG_PUBNAMES_SECTION  ".debug_pubnames"
4227 #endif
4228 #ifndef DEBUG_STR_SECTION
4229 #define DEBUG_STR_SECTION       ".debug_str"
4230 #endif
4231 #ifndef DEBUG_RANGES_SECTION
4232 #define DEBUG_RANGES_SECTION    ".debug_ranges"
4233 #endif
4234
4235 /* Standard ELF section names for compiled code and data.  */
4236 #ifndef TEXT_SECTION_NAME
4237 #define TEXT_SECTION_NAME       ".text"
4238 #endif
4239
4240 /* Section flags for .debug_str section.  */
4241 #define DEBUG_STR_SECTION_FLAGS \
4242   (HAVE_GAS_SHF_MERGE && flag_merge_constants                   \
4243    ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1        \
4244    : SECTION_DEBUG)
4245
4246 /* Labels we insert at beginning sections we can reference instead of
4247    the section names themselves.  */
4248
4249 #ifndef TEXT_SECTION_LABEL
4250 #define TEXT_SECTION_LABEL              "Ltext"
4251 #endif
4252 #ifndef COLD_TEXT_SECTION_LABEL
4253 #define COLD_TEXT_SECTION_LABEL         "Ltext_cold"
4254 #endif
4255 #ifndef DEBUG_LINE_SECTION_LABEL
4256 #define DEBUG_LINE_SECTION_LABEL        "Ldebug_line"
4257 #endif
4258 #ifndef DEBUG_INFO_SECTION_LABEL
4259 #define DEBUG_INFO_SECTION_LABEL        "Ldebug_info"
4260 #endif
4261 #ifndef DEBUG_ABBREV_SECTION_LABEL
4262 #define DEBUG_ABBREV_SECTION_LABEL      "Ldebug_abbrev"
4263 #endif
4264 #ifndef DEBUG_LOC_SECTION_LABEL
4265 #define DEBUG_LOC_SECTION_LABEL         "Ldebug_loc"
4266 #endif
4267 #ifndef DEBUG_RANGES_SECTION_LABEL
4268 #define DEBUG_RANGES_SECTION_LABEL      "Ldebug_ranges"
4269 #endif
4270 #ifndef DEBUG_MACINFO_SECTION_LABEL
4271 #define DEBUG_MACINFO_SECTION_LABEL     "Ldebug_macinfo"
4272 #endif
4273
4274 /* Definitions of defaults for formats and names of various special
4275    (artificial) labels which may be generated within this file (when the -g
4276    options is used and DWARF2_DEBUGGING_INFO is in effect.
4277    If necessary, these may be overridden from within the tm.h file, but
4278    typically, overriding these defaults is unnecessary.  */
4279
4280 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4281 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4282 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4283 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES]; 
4284 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4285 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4286 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4287 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4288 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4289 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
4290
4291 #ifndef TEXT_END_LABEL
4292 #define TEXT_END_LABEL          "Letext"
4293 #endif
4294 #ifndef COLD_END_LABEL
4295 #define COLD_END_LABEL          "Letext_cold"
4296 #endif
4297 #ifndef BLOCK_BEGIN_LABEL
4298 #define BLOCK_BEGIN_LABEL       "LBB"
4299 #endif
4300 #ifndef BLOCK_END_LABEL
4301 #define BLOCK_END_LABEL         "LBE"
4302 #endif
4303 #ifndef LINE_CODE_LABEL
4304 #define LINE_CODE_LABEL         "LM"
4305 #endif
4306 #ifndef SEPARATE_LINE_CODE_LABEL
4307 #define SEPARATE_LINE_CODE_LABEL        "LSM"
4308 #endif
4309 \f
4310 /* We allow a language front-end to designate a function that is to be
4311    called to "demangle" any name before it is put into a DIE.  */
4312
4313 static const char *(*demangle_name_func) (const char *);
4314
4315 void
4316 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
4317 {
4318   demangle_name_func = func;
4319 }
4320
4321 /* Test if rtl node points to a pseudo register.  */
4322
4323 static inline int
4324 is_pseudo_reg (rtx rtl)
4325 {
4326   return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
4327           || (GET_CODE (rtl) == SUBREG
4328               && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
4329 }
4330
4331 /* Return a reference to a type, with its const and volatile qualifiers
4332    removed.  */
4333
4334 static inline tree
4335 type_main_variant (tree type)
4336 {
4337   type = TYPE_MAIN_VARIANT (type);
4338
4339   /* ??? There really should be only one main variant among any group of
4340      variants of a given type (and all of the MAIN_VARIANT values for all
4341      members of the group should point to that one type) but sometimes the C
4342      front-end messes this up for array types, so we work around that bug
4343      here.  */
4344   if (TREE_CODE (type) == ARRAY_TYPE)
4345     while (type != TYPE_MAIN_VARIANT (type))
4346       type = TYPE_MAIN_VARIANT (type);
4347
4348   return type;
4349 }
4350
4351 /* Return nonzero if the given type node represents a tagged type.  */
4352
4353 static inline int
4354 is_tagged_type (tree type)
4355 {
4356   enum tree_code code = TREE_CODE (type);
4357
4358   return (code == RECORD_TYPE || code == UNION_TYPE
4359           || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
4360 }
4361
4362 /* Convert a DIE tag into its string name.  */
4363
4364 static const char *
4365 dwarf_tag_name (unsigned int tag)
4366 {
4367   switch (tag)
4368     {
4369     case DW_TAG_padding:
4370       return "DW_TAG_padding";
4371     case DW_TAG_array_type:
4372       return "DW_TAG_array_type";
4373     case DW_TAG_class_type:
4374       return "DW_TAG_class_type";
4375     case DW_TAG_entry_point:
4376       return "DW_TAG_entry_point";
4377     case DW_TAG_enumeration_type:
4378       return "DW_TAG_enumeration_type";
4379     case DW_TAG_formal_parameter:
4380       return "DW_TAG_formal_parameter";
4381     case DW_TAG_imported_declaration:
4382       return "DW_TAG_imported_declaration";
4383     case DW_TAG_label:
4384       return "DW_TAG_label";
4385     case DW_TAG_lexical_block:
4386       return "DW_TAG_lexical_block";
4387     case DW_TAG_member:
4388       return "DW_TAG_member";
4389     case DW_TAG_pointer_type:
4390       return "DW_TAG_pointer_type";
4391     case DW_TAG_reference_type:
4392       return "DW_TAG_reference_type";
4393     case DW_TAG_compile_unit:
4394       return "DW_TAG_compile_unit";
4395     case DW_TAG_string_type:
4396       return "DW_TAG_string_type";
4397     case DW_TAG_structure_type:
4398       return "DW_TAG_structure_type";
4399     case DW_TAG_subroutine_type:
4400       return "DW_TAG_subroutine_type";
4401     case DW_TAG_typedef:
4402       return "DW_TAG_typedef";
4403     case DW_TAG_union_type:
4404       return "DW_TAG_union_type";
4405     case DW_TAG_unspecified_parameters:
4406       return "DW_TAG_unspecified_parameters";
4407     case DW_TAG_variant:
4408       return "DW_TAG_variant";
4409     case DW_TAG_common_block:
4410       return "DW_TAG_common_block";
4411     case DW_TAG_common_inclusion:
4412       return "DW_TAG_common_inclusion";
4413     case DW_TAG_inheritance:
4414       return "DW_TAG_inheritance";
4415     case DW_TAG_inlined_subroutine:
4416       return "DW_TAG_inlined_subroutine";
4417     case DW_TAG_module:
4418       return "DW_TAG_module";
4419     case DW_TAG_ptr_to_member_type:
4420       return "DW_TAG_ptr_to_member_type";
4421     case DW_TAG_set_type:
4422       return "DW_TAG_set_type";
4423     case DW_TAG_subrange_type:
4424       return "DW_TAG_subrange_type";
4425     case DW_TAG_with_stmt:
4426       return "DW_TAG_with_stmt";
4427     case DW_TAG_access_declaration:
4428       return "DW_TAG_access_declaration";
4429     case DW_TAG_base_type:
4430       return "DW_TAG_base_type";
4431     case DW_TAG_catch_block:
4432       return "DW_TAG_catch_block";
4433     case DW_TAG_const_type:
4434       return "DW_TAG_const_type";
4435     case DW_TAG_constant:
4436       return "DW_TAG_constant";
4437     case DW_TAG_enumerator:
4438       return "DW_TAG_enumerator";
4439     case DW_TAG_file_type:
4440       return "DW_TAG_file_type";
4441     case DW_TAG_friend:
4442       return "DW_TAG_friend";
4443     case DW_TAG_namelist:
4444       return "DW_TAG_namelist";
4445     case DW_TAG_namelist_item:
4446       return "DW_TAG_namelist_item";
4447     case DW_TAG_namespace:
4448       return "DW_TAG_namespace";
4449     case DW_TAG_packed_type:
4450       return "DW_TAG_packed_type";
4451     case DW_TAG_subprogram:
4452       return "DW_TAG_subprogram";
4453     case DW_TAG_template_type_param:
4454       return "DW_TAG_template_type_param";
4455     case DW_TAG_template_value_param:
4456       return "DW_TAG_template_value_param";
4457     case DW_TAG_thrown_type:
4458       return "DW_TAG_thrown_type";
4459     case DW_TAG_try_block:
4460       return "DW_TAG_try_block";
4461     case DW_TAG_variant_part:
4462       return "DW_TAG_variant_part";
4463     case DW_TAG_variable:
4464       return "DW_TAG_variable";
4465     case DW_TAG_volatile_type:
4466       return "DW_TAG_volatile_type";
4467     case DW_TAG_imported_module:
4468       return "DW_TAG_imported_module";
4469     case DW_TAG_MIPS_loop:
4470       return "DW_TAG_MIPS_loop";
4471     case DW_TAG_format_label:
4472       return "DW_TAG_format_label";
4473     case DW_TAG_function_template:
4474       return "DW_TAG_function_template";
4475     case DW_TAG_class_template:
4476       return "DW_TAG_class_template";
4477     case DW_TAG_GNU_BINCL:
4478       return "DW_TAG_GNU_BINCL";
4479     case DW_TAG_GNU_EINCL:
4480       return "DW_TAG_GNU_EINCL";
4481     default:
4482       return "DW_TAG_<unknown>";
4483     }
4484 }
4485
4486 /* Convert a DWARF attribute code into its string name.  */
4487
4488 static const char *
4489 dwarf_attr_name (unsigned int attr)
4490 {
4491   switch (attr)
4492     {
4493     case DW_AT_sibling:
4494       return "DW_AT_sibling";
4495     case DW_AT_location:
4496       return "DW_AT_location";
4497     case DW_AT_name:
4498       return "DW_AT_name";
4499     case DW_AT_ordering:
4500       return "DW_AT_ordering";
4501     case DW_AT_subscr_data:
4502       return "DW_AT_subscr_data";
4503     case DW_AT_byte_size:
4504       return "DW_AT_byte_size";
4505     case DW_AT_bit_offset:
4506       return "DW_AT_bit_offset";
4507     case DW_AT_bit_size:
4508       return "DW_AT_bit_size";
4509     case DW_AT_element_list:
4510       return "DW_AT_element_list";
4511     case DW_AT_stmt_list:
4512       return "DW_AT_stmt_list";
4513     case DW_AT_low_pc:
4514       return "DW_AT_low_pc";
4515     case DW_AT_high_pc:
4516       return "DW_AT_high_pc";
4517     case DW_AT_language:
4518       return "DW_AT_language";
4519     case DW_AT_member:
4520       return "DW_AT_member";
4521     case DW_AT_discr:
4522       return "DW_AT_discr";
4523     case DW_AT_discr_value:
4524       return "DW_AT_discr_value";
4525     case DW_AT_visibility:
4526       return "DW_AT_visibility";
4527     case DW_AT_import:
4528       return "DW_AT_import";
4529     case DW_AT_string_length:
4530       return "DW_AT_string_length";
4531     case DW_AT_common_reference:
4532       return "DW_AT_common_reference";
4533     case DW_AT_comp_dir:
4534       return "DW_AT_comp_dir";
4535     case DW_AT_const_value:
4536       return "DW_AT_const_value";
4537     case DW_AT_containing_type:
4538       return "DW_AT_containing_type";
4539     case DW_AT_default_value:
4540       return "DW_AT_default_value";
4541     case DW_AT_inline:
4542       return "DW_AT_inline";
4543     case DW_AT_is_optional:
4544       return "DW_AT_is_optional";
4545     case DW_AT_lower_bound:
4546       return "DW_AT_lower_bound";
4547     case DW_AT_producer:
4548       return "DW_AT_producer";
4549     case DW_AT_prototyped:
4550       return "DW_AT_prototyped";
4551     case DW_AT_return_addr:
4552       return "DW_AT_return_addr";
4553     case DW_AT_start_scope:
4554       return "DW_AT_start_scope";
4555     case DW_AT_stride_size:
4556       return "DW_AT_stride_size";
4557     case DW_AT_upper_bound:
4558       return "DW_AT_upper_bound";
4559     case DW_AT_abstract_origin:
4560       return "DW_AT_abstract_origin";
4561     case DW_AT_accessibility:
4562       return "DW_AT_accessibility";
4563     case DW_AT_address_class:
4564       return "DW_AT_address_class";
4565     case DW_AT_artificial:
4566       return "DW_AT_artificial";
4567     case DW_AT_base_types:
4568       return "DW_AT_base_types";
4569     case DW_AT_calling_convention:
4570       return "DW_AT_calling_convention";
4571     case DW_AT_count:
4572       return "DW_AT_count";
4573     case DW_AT_data_member_location:
4574       return "DW_AT_data_member_location";
4575     case DW_AT_decl_column:
4576       return "DW_AT_decl_column";
4577     case DW_AT_decl_file:
4578       return "DW_AT_decl_file";
4579     case DW_AT_decl_line:
4580       return "DW_AT_decl_line";
4581     case DW_AT_declaration:
4582       return "DW_AT_declaration";
4583     case DW_AT_discr_list:
4584       return "DW_AT_discr_list";
4585     case DW_AT_encoding:
4586       return "DW_AT_encoding";
4587     case DW_AT_external:
4588       return "DW_AT_external";
4589     case DW_AT_frame_base:
4590       return "DW_AT_frame_base";
4591     case DW_AT_friend:
4592       return "DW_AT_friend";
4593     case DW_AT_identifier_case:
4594       return "DW_AT_identifier_case";
4595     case DW_AT_macro_info:
4596       return "DW_AT_macro_info";
4597     case DW_AT_namelist_items:
4598       return "DW_AT_namelist_items";
4599     case DW_AT_priority:
4600       return "DW_AT_priority";
4601     case DW_AT_segment:
4602       return "DW_AT_segment";
4603     case DW_AT_specification:
4604       return "DW_AT_specification";
4605     case DW_AT_static_link:
4606       return "DW_AT_static_link";
4607     case DW_AT_type:
4608       return "DW_AT_type";
4609     case DW_AT_use_location:
4610       return "DW_AT_use_location";
4611     case DW_AT_variable_parameter:
4612       return "DW_AT_variable_parameter";
4613     case DW_AT_virtuality:
4614       return "DW_AT_virtuality";
4615     case DW_AT_vtable_elem_location:
4616       return "DW_AT_vtable_elem_location";
4617
4618     case DW_AT_allocated:
4619       return "DW_AT_allocated";
4620     case DW_AT_associated:
4621       return "DW_AT_associated";
4622     case DW_AT_data_location:
4623       return "DW_AT_data_location";
4624     case DW_AT_stride:
4625       return "DW_AT_stride";
4626     case DW_AT_entry_pc:
4627       return "DW_AT_entry_pc";
4628     case DW_AT_use_UTF8:
4629       return "DW_AT_use_UTF8";
4630     case DW_AT_extension:
4631       return "DW_AT_extension";
4632     case DW_AT_ranges:
4633       return "DW_AT_ranges";
4634     case DW_AT_trampoline:
4635       return "DW_AT_trampoline";
4636     case DW_AT_call_column:
4637       return "DW_AT_call_column";
4638     case DW_AT_call_file:
4639       return "DW_AT_call_file";
4640     case DW_AT_call_line:
4641       return "DW_AT_call_line";
4642
4643     case DW_AT_MIPS_fde:
4644       return "DW_AT_MIPS_fde";
4645     case DW_AT_MIPS_loop_begin:
4646       return "DW_AT_MIPS_loop_begin";
4647     case DW_AT_MIPS_tail_loop_begin:
4648       return "DW_AT_MIPS_tail_loop_begin";
4649     case DW_AT_MIPS_epilog_begin:
4650       return "DW_AT_MIPS_epilog_begin";
4651     case DW_AT_MIPS_loop_unroll_factor:
4652       return "DW_AT_MIPS_loop_unroll_factor";
4653     case DW_AT_MIPS_software_pipeline_depth:
4654       return "DW_AT_MIPS_software_pipeline_depth";
4655     case DW_AT_MIPS_linkage_name:
4656       return "DW_AT_MIPS_linkage_name";
4657     case DW_AT_MIPS_stride:
4658       return "DW_AT_MIPS_stride";
4659     case DW_AT_MIPS_abstract_name:
4660       return "DW_AT_MIPS_abstract_name";
4661     case DW_AT_MIPS_clone_origin:
4662       return "DW_AT_MIPS_clone_origin";
4663     case DW_AT_MIPS_has_inlines:
4664       return "DW_AT_MIPS_has_inlines";
4665
4666     case DW_AT_sf_names:
4667       return "DW_AT_sf_names";
4668     case DW_AT_src_info:
4669       return "DW_AT_src_info";
4670     case DW_AT_mac_info:
4671       return "DW_AT_mac_info";
4672     case DW_AT_src_coords:
4673       return "DW_AT_src_coords";
4674     case DW_AT_body_begin:
4675       return "DW_AT_body_begin";
4676     case DW_AT_body_end:
4677       return "DW_AT_body_end";
4678     case DW_AT_GNU_vector:
4679       return "DW_AT_GNU_vector";
4680
4681     case DW_AT_VMS_rtnbeg_pd_address:
4682       return "DW_AT_VMS_rtnbeg_pd_address";
4683
4684     default:
4685       return "DW_AT_<unknown>";
4686     }
4687 }
4688
4689 /* Convert a DWARF value form code into its string name.  */
4690
4691 static const char *
4692 dwarf_form_name (unsigned int form)
4693 {
4694   switch (form)
4695     {
4696     case DW_FORM_addr:
4697       return "DW_FORM_addr";
4698     case DW_FORM_block2:
4699       return "DW_FORM_block2";
4700     case DW_FORM_block4:
4701       return "DW_FORM_block4";
4702     case DW_FORM_data2:
4703       return "DW_FORM_data2";
4704     case DW_FORM_data4:
4705       return "DW_FORM_data4";
4706     case DW_FORM_data8:
4707       return "DW_FORM_data8";
4708     case DW_FORM_string:
4709       return "DW_FORM_string";
4710     case DW_FORM_block:
4711       return "DW_FORM_block";
4712     case DW_FORM_block1:
4713       return "DW_FORM_block1";
4714     case DW_FORM_data1:
4715       return "DW_FORM_data1";
4716     case DW_FORM_flag:
4717       return "DW_FORM_flag";
4718     case DW_FORM_sdata:
4719       return "DW_FORM_sdata";
4720     case DW_FORM_strp:
4721       return "DW_FORM_strp";
4722     case DW_FORM_udata:
4723       return "DW_FORM_udata";
4724     case DW_FORM_ref_addr:
4725       return "DW_FORM_ref_addr";
4726     case DW_FORM_ref1:
4727       return "DW_FORM_ref1";
4728     case DW_FORM_ref2:
4729       return "DW_FORM_ref2";
4730     case DW_FORM_ref4:
4731       return "DW_FORM_ref4";
4732     case DW_FORM_ref8:
4733       return "DW_FORM_ref8";
4734     case DW_FORM_ref_udata:
4735       return "DW_FORM_ref_udata";
4736     case DW_FORM_indirect:
4737       return "DW_FORM_indirect";
4738     default:
4739       return "DW_FORM_<unknown>";
4740     }
4741 }
4742 \f
4743 /* Determine the "ultimate origin" of a decl.  The decl may be an inlined
4744    instance of an inlined instance of a decl which is local to an inline
4745    function, so we have to trace all of the way back through the origin chain
4746    to find out what sort of node actually served as the original seed for the
4747    given block.  */
4748
4749 static tree
4750 decl_ultimate_origin (tree decl)
4751 {
4752   if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
4753     return NULL_TREE;
4754
4755   /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
4756      nodes in the function to point to themselves; ignore that if
4757      we're trying to output the abstract instance of this function.  */
4758   if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
4759     return NULL_TREE;
4760
4761   /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
4762      most distant ancestor, this should never happen.  */
4763   gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
4764
4765   return DECL_ABSTRACT_ORIGIN (decl);
4766 }
4767
4768 /* Determine the "ultimate origin" of a block.  The block may be an inlined
4769    instance of an inlined instance of a block which is local to an inline
4770    function, so we have to trace all of the way back through the origin chain
4771    to find out what sort of node actually served as the original seed for the
4772    given block.  */
4773
4774 static tree
4775 block_ultimate_origin (tree block)
4776 {
4777   tree immediate_origin = BLOCK_ABSTRACT_ORIGIN (block);
4778
4779   /* output_inline_function sets BLOCK_ABSTRACT_ORIGIN for all the
4780      nodes in the function to point to themselves; ignore that if
4781      we're trying to output the abstract instance of this function.  */
4782   if (BLOCK_ABSTRACT (block) && immediate_origin == block)
4783     return NULL_TREE;
4784
4785   if (immediate_origin == NULL_TREE)
4786     return NULL_TREE;
4787   else
4788     {
4789       tree ret_val;
4790       tree lookahead = immediate_origin;
4791
4792       do
4793         {
4794           ret_val = lookahead;
4795           lookahead = (TREE_CODE (ret_val) == BLOCK
4796                        ? BLOCK_ABSTRACT_ORIGIN (ret_val) : NULL);
4797         }
4798       while (lookahead != NULL && lookahead != ret_val);
4799       
4800       /* The block's abstract origin chain may not be the *ultimate* origin of
4801          the block. It could lead to a DECL that has an abstract origin set.
4802          If so, we want that DECL's abstract origin (which is what DECL_ORIGIN
4803          will give us if it has one).  Note that DECL's abstract origins are
4804          supposed to be the most distant ancestor (or so decl_ultimate_origin
4805          claims), so we don't need to loop following the DECL origins.  */
4806       if (DECL_P (ret_val))
4807         return DECL_ORIGIN (ret_val);
4808
4809       return ret_val;
4810     }
4811 }
4812
4813 /* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
4814    of a virtual function may refer to a base class, so we check the 'this'
4815    parameter.  */
4816
4817 static tree
4818 decl_class_context (tree decl)
4819 {
4820   tree context = NULL_TREE;
4821
4822   if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
4823     context = DECL_CONTEXT (decl);
4824   else
4825     context = TYPE_MAIN_VARIANT
4826       (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
4827
4828   if (context && !TYPE_P (context))
4829     context = NULL_TREE;
4830
4831   return context;
4832 }
4833 \f
4834 /* Add an attribute/value pair to a DIE.  We build the lists up in reverse
4835    addition order, and correct that in reverse_all_dies.  */
4836
4837 static inline void
4838 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
4839 {
4840   if (die != NULL && attr != NULL)
4841     {
4842       attr->dw_attr_next = die->die_attr;
4843       die->die_attr = attr;
4844     }
4845 }
4846
4847 static inline enum dw_val_class
4848 AT_class (dw_attr_ref a)
4849 {
4850   return a->dw_attr_val.val_class;
4851 }
4852
4853 /* Add a flag value attribute to a DIE.  */
4854
4855 static inline void
4856 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
4857 {
4858   dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4859
4860   attr->dw_attr_next = NULL;
4861   attr->dw_attr = attr_kind;
4862   attr->dw_attr_val.val_class = dw_val_class_flag;
4863   attr->dw_attr_val.v.val_flag = flag;
4864   add_dwarf_attr (die, attr);
4865 }
4866
4867 static inline unsigned
4868 AT_flag (dw_attr_ref a)
4869 {
4870   gcc_assert (a && AT_class (a) == dw_val_class_flag);
4871   return a->dw_attr_val.v.val_flag;
4872 }
4873
4874 /* Add a signed integer attribute value to a DIE.  */
4875
4876 static inline void
4877 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
4878 {
4879   dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4880
4881   attr->dw_attr_next = NULL;
4882   attr->dw_attr = attr_kind;
4883   attr->dw_attr_val.val_class = dw_val_class_const;
4884   attr->dw_attr_val.v.val_int = int_val;
4885   add_dwarf_attr (die, attr);
4886 }
4887
4888 static inline HOST_WIDE_INT
4889 AT_int (dw_attr_ref a)
4890 {
4891   gcc_assert (a && AT_class (a) == dw_val_class_const);
4892   return a->dw_attr_val.v.val_int;
4893 }
4894
4895 /* Add an unsigned integer attribute value to a DIE.  */
4896
4897 static inline void
4898 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
4899                  unsigned HOST_WIDE_INT unsigned_val)
4900 {
4901   dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4902
4903   attr->dw_attr_next = NULL;
4904   attr->dw_attr = attr_kind;
4905   attr->dw_attr_val.val_class = dw_val_class_unsigned_const;
4906   attr->dw_attr_val.v.val_unsigned = unsigned_val;
4907   add_dwarf_attr (die, attr);
4908 }
4909
4910 static inline unsigned HOST_WIDE_INT
4911 AT_unsigned (dw_attr_ref a)
4912 {
4913   gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
4914   return a->dw_attr_val.v.val_unsigned;
4915 }
4916
4917 /* Add an unsigned double integer attribute value to a DIE.  */
4918
4919 static inline void
4920 add_AT_long_long (dw_die_ref die, enum dwarf_attribute attr_kind,
4921                   long unsigned int val_hi, long unsigned int val_low)
4922 {
4923   dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4924
4925   attr->dw_attr_next = NULL;
4926   attr->dw_attr = attr_kind;
4927   attr->dw_attr_val.val_class = dw_val_class_long_long;
4928   attr->dw_attr_val.v.val_long_long.hi = val_hi;
4929   attr->dw_attr_val.v.val_long_long.low = val_low;
4930   add_dwarf_attr (die, attr);
4931 }
4932
4933 /* Add a floating point attribute value to a DIE and return it.  */
4934
4935 static inline void
4936 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
4937             unsigned int length, unsigned int elt_size, unsigned char *array)
4938 {
4939   dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4940
4941   attr->dw_attr_next = NULL;
4942   attr->dw_attr = attr_kind;
4943   attr->dw_attr_val.val_class = dw_val_class_vec;
4944   attr->dw_attr_val.v.val_vec.length = length;
4945   attr->dw_attr_val.v.val_vec.elt_size = elt_size;
4946   attr->dw_attr_val.v.val_vec.array = array;
4947   add_dwarf_attr (die, attr);
4948 }
4949
4950 /* Hash and equality functions for debug_str_hash.  */
4951
4952 static hashval_t
4953 debug_str_do_hash (const void *x)
4954 {
4955   return htab_hash_string (((const struct indirect_string_node *)x)->str);
4956 }
4957
4958 static int
4959 debug_str_eq (const void *x1, const void *x2)
4960 {
4961   return strcmp ((((const struct indirect_string_node *)x1)->str),
4962                  (const char *)x2) == 0;
4963 }
4964
4965 /* Add a string attribute value to a DIE.  */
4966
4967 static inline void
4968 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4969 {
4970   dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4971   struct indirect_string_node *node;
4972   void **slot;
4973
4974   if (! debug_str_hash)
4975     debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
4976                                       debug_str_eq, NULL);
4977
4978   slot = htab_find_slot_with_hash (debug_str_hash, str,
4979                                    htab_hash_string (str), INSERT);
4980   if (*slot == NULL)
4981     *slot = ggc_alloc_cleared (sizeof (struct indirect_string_node));
4982   node = (struct indirect_string_node *) *slot;
4983   node->str = ggc_strdup (str);
4984   node->refcount++;
4985
4986   attr->dw_attr_next = NULL;
4987   attr->dw_attr = attr_kind;
4988   attr->dw_attr_val.val_class = dw_val_class_str;
4989   attr->dw_attr_val.v.val_str = node;
4990   add_dwarf_attr (die, attr);
4991 }
4992
4993 static inline const char *
4994 AT_string (dw_attr_ref a)
4995 {
4996   gcc_assert (a && AT_class (a) == dw_val_class_str);
4997   return a->dw_attr_val.v.val_str->str;
4998 }
4999
5000 /* Find out whether a string should be output inline in DIE
5001    or out-of-line in .debug_str section.  */
5002
5003 static int
5004 AT_string_form (dw_attr_ref a)
5005 {
5006   struct indirect_string_node *node;
5007   unsigned int len;
5008   char label[32];
5009
5010   gcc_assert (a && AT_class (a) == dw_val_class_str);
5011
5012   node = a->dw_attr_val.v.val_str;
5013   if (node->form)
5014     return node->form;
5015
5016   len = strlen (node->str) + 1;
5017
5018   /* If the string is shorter or equal to the size of the reference, it is
5019      always better to put it inline.  */
5020   if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
5021     return node->form = DW_FORM_string;
5022
5023   /* If we cannot expect the linker to merge strings in .debug_str
5024      section, only put it into .debug_str if it is worth even in this
5025      single module.  */
5026   if ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) == 0
5027       && (len - DWARF_OFFSET_SIZE) * node->refcount <= len)
5028     return node->form = DW_FORM_string;
5029
5030   ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
5031   ++dw2_string_counter;
5032   node->label = xstrdup (label);
5033
5034   return node->form = DW_FORM_strp;
5035 }
5036
5037 /* Add a DIE reference attribute value to a DIE.  */
5038
5039 static inline void
5040 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
5041 {
5042   dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
5043
5044   attr->dw_attr_next = NULL;
5045   attr->dw_attr = attr_kind;
5046   attr->dw_attr_val.val_class = dw_val_class_die_ref;
5047   attr->dw_attr_val.v.val_die_ref.die = targ_die;
5048   attr->dw_attr_val.v.val_die_ref.external = 0;
5049   add_dwarf_attr (die, attr);
5050 }
5051
5052 /* Add an AT_specification attribute to a DIE, and also make the back
5053    pointer from the specification to the definition.  */
5054
5055 static inline void
5056 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
5057 {
5058   add_AT_die_ref (die, DW_AT_specification, targ_die);
5059   gcc_assert (!targ_die->die_definition);
5060   targ_die->die_definition = die;
5061 }
5062
5063 static inline dw_die_ref
5064 AT_ref (dw_attr_ref a)
5065 {
5066   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
5067   return a->dw_attr_val.v.val_die_ref.die;
5068 }
5069
5070 static inline int
5071 AT_ref_external (dw_attr_ref a)
5072 {
5073   if (a && AT_class (a) == dw_val_class_die_ref)
5074     return a->dw_attr_val.v.val_die_ref.external;
5075
5076   return 0;
5077 }
5078
5079 static inline void
5080 set_AT_ref_external (dw_attr_ref a, int i)
5081 {
5082   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
5083   a->dw_attr_val.v.val_die_ref.external = i;
5084 }
5085
5086 /* Add an FDE reference attribute value to a DIE.  */
5087
5088 static inline void
5089 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
5090 {
5091   dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
5092
5093   attr->dw_attr_next = NULL;
5094   attr->dw_attr = attr_kind;
5095   attr->dw_attr_val.val_class = dw_val_class_fde_ref;
5096   attr->dw_attr_val.v.val_fde_index = targ_fde;
5097   add_dwarf_attr (die, attr);
5098 }
5099
5100 /* Add a location description attribute value to a DIE.  */
5101
5102 static inline void
5103 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
5104 {
5105   dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
5106
5107   attr->dw_attr_next = NULL;
5108   attr->dw_attr = attr_kind;
5109   attr->dw_attr_val.val_class = dw_val_class_loc;
5110   attr->dw_attr_val.v.val_loc = loc;
5111   add_dwarf_attr (die, attr);
5112 }
5113
5114 static inline dw_loc_descr_ref
5115 AT_loc (dw_attr_ref a)
5116 {
5117   gcc_assert (a && AT_class (a) == dw_val_class_loc);
5118   return a->dw_attr_val.v.val_loc;
5119 }
5120
5121 static inline void
5122 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
5123 {
5124   dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
5125
5126   attr->dw_attr_next = NULL;
5127   attr->dw_attr = attr_kind;
5128   attr->dw_attr_val.val_class = dw_val_class_loc_list;
5129   attr->dw_attr_val.v.val_loc_list = loc_list;
5130   add_dwarf_attr (die, attr);
5131   have_location_lists = 1;
5132 }
5133
5134 static inline dw_loc_list_ref
5135 AT_loc_list (dw_attr_ref a)
5136 {
5137   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
5138   return a->dw_attr_val.v.val_loc_list;
5139 }
5140
5141 /* Add an address constant attribute value to a DIE.  */
5142
5143 static inline void
5144 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
5145 {
5146   dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
5147
5148   attr->dw_attr_next = NULL;
5149   attr->dw_attr = attr_kind;
5150   attr->dw_attr_val.val_class = dw_val_class_addr;
5151   attr->dw_attr_val.v.val_addr = addr;
5152   add_dwarf_attr (die, attr);
5153 }
5154
5155 static inline rtx
5156 AT_addr (dw_attr_ref a)
5157 {
5158   gcc_assert (a && AT_class (a) == dw_val_class_addr);
5159   return a->dw_attr_val.v.val_addr;
5160 }
5161
5162 /* Add a label identifier attribute value to a DIE.  */
5163
5164 static inline void
5165 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
5166 {
5167   dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
5168
5169   attr->dw_attr_next = NULL;
5170   attr->dw_attr = attr_kind;
5171   attr->dw_attr_val.val_class = dw_val_class_lbl_id;
5172   attr->dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
5173   add_dwarf_attr (die, attr);
5174 }
5175
5176 /* Add a section offset attribute value to a DIE.  */
5177
5178 static inline void
5179 add_AT_lbl_offset (dw_die_ref die, enum dwarf_attribute attr_kind, const char *label)
5180 {
5181   dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
5182
5183   attr->dw_attr_next = NULL;
5184   attr->dw_attr = attr_kind;
5185   attr->dw_attr_val.val_class = dw_val_class_lbl_offset;
5186   attr->dw_attr_val.v.val_lbl_id = xstrdup (label);
5187   add_dwarf_attr (die, attr);
5188 }
5189
5190 /* Add an offset attribute value to a DIE.  */
5191
5192 static inline void
5193 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
5194                unsigned HOST_WIDE_INT offset)
5195 {
5196   dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
5197
5198   attr->dw_attr_next = NULL;
5199   attr->dw_attr = attr_kind;
5200   attr->dw_attr_val.val_class = dw_val_class_offset;
5201   attr->dw_attr_val.v.val_offset = offset;
5202   add_dwarf_attr (die, attr);
5203 }
5204
5205 /* Add an range_list attribute value to a DIE.  */
5206
5207 static void
5208 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
5209                    long unsigned int offset)
5210 {
5211   dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
5212
5213   attr->dw_attr_next = NULL;
5214   attr->dw_attr = attr_kind;
5215   attr->dw_attr_val.val_class = dw_val_class_range_list;
5216   attr->dw_attr_val.v.val_offset = offset;
5217   add_dwarf_attr (die, attr);
5218 }
5219
5220 static inline const char *
5221 AT_lbl (dw_attr_ref a)
5222 {
5223   gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
5224                     || AT_class (a) == dw_val_class_lbl_offset));
5225   return a->dw_attr_val.v.val_lbl_id;
5226 }
5227
5228 /* Get the attribute of type attr_kind.  */
5229
5230 static dw_attr_ref
5231 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5232 {
5233   dw_attr_ref a;
5234   dw_die_ref spec = NULL;
5235
5236   if (die != NULL)
5237     {
5238       for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
5239         if (a->dw_attr == attr_kind)
5240           return a;
5241         else if (a->dw_attr == DW_AT_specification
5242                  || a->dw_attr == DW_AT_abstract_origin)
5243           spec = AT_ref (a);
5244
5245       if (spec)
5246         return get_AT (spec, attr_kind);
5247     }
5248
5249   return NULL;
5250 }
5251
5252 /* Return the "low pc" attribute value, typically associated with a subprogram
5253    DIE.  Return null if the "low pc" attribute is either not present, or if it
5254    cannot be represented as an assembler label identifier.  */
5255
5256 static inline const char *
5257 get_AT_low_pc (dw_die_ref die)
5258 {
5259   dw_attr_ref a = get_AT (die, DW_AT_low_pc);
5260
5261   return a ? AT_lbl (a) : NULL;
5262 }
5263
5264 /* Return the "high pc" attribute value, typically associated with a subprogram
5265    DIE.  Return null if the "high pc" attribute is either not present, or if it
5266    cannot be represented as an assembler label identifier.  */
5267
5268 static inline const char *
5269 get_AT_hi_pc (dw_die_ref die)
5270 {
5271   dw_attr_ref a = get_AT (die, DW_AT_high_pc);
5272
5273   return a ? AT_lbl (a) : NULL;
5274 }
5275
5276 /* Return the value of the string attribute designated by ATTR_KIND, or
5277    NULL if it is not present.  */
5278
5279 static inline const char *
5280 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
5281 {
5282   dw_attr_ref a = get_AT (die, attr_kind);
5283
5284   return a ? AT_string (a) : NULL;
5285 }
5286
5287 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
5288    if it is not present.  */
5289
5290 static inline int
5291 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
5292 {
5293   dw_attr_ref a = get_AT (die, attr_kind);
5294
5295   return a ? AT_flag (a) : 0;
5296 }
5297
5298 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
5299    if it is not present.  */
5300
5301 static inline unsigned
5302 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
5303 {
5304   dw_attr_ref a = get_AT (die, attr_kind);
5305
5306   return a ? AT_unsigned (a) : 0;
5307 }
5308
5309 static inline dw_die_ref
5310 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
5311 {
5312   dw_attr_ref a = get_AT (die, attr_kind);
5313
5314   return a ? AT_ref (a) : NULL;
5315 }
5316
5317 /* Return TRUE if the language is C or C++.  */
5318
5319 static inline bool
5320 is_c_family (void)
5321 {
5322   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5323
5324   return (lang == DW_LANG_C || lang == DW_LANG_C89
5325           || lang == DW_LANG_C_plus_plus);
5326 }
5327
5328 /* Return TRUE if the language is C++.  */
5329
5330 static inline bool
5331 is_cxx (void)
5332 {
5333   return (get_AT_unsigned (comp_unit_die, DW_AT_language)
5334           == DW_LANG_C_plus_plus);
5335 }
5336
5337 /* Return TRUE if the language is Fortran.  */
5338
5339 static inline bool
5340 is_fortran (void)
5341 {
5342   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5343
5344   return (lang == DW_LANG_Fortran77
5345           || lang == DW_LANG_Fortran90
5346           || lang == DW_LANG_Fortran95);
5347 }
5348
5349 /* Return TRUE if the language is Java.  */
5350
5351 static inline bool
5352 is_java (void)
5353 {
5354   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5355
5356   return lang == DW_LANG_Java;
5357 }
5358
5359 /* Return TRUE if the language is Ada.  */
5360
5361 static inline bool
5362 is_ada (void)
5363 {
5364   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5365
5366   return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
5367 }
5368
5369 /* Free up the memory used by A.  */
5370
5371 static inline void free_AT (dw_attr_ref);
5372 static inline void
5373 free_AT (dw_attr_ref a)
5374 {
5375   if (AT_class (a) == dw_val_class_str)
5376     if (a->dw_attr_val.v.val_str->refcount)
5377       a->dw_attr_val.v.val_str->refcount--;
5378 }
5379
5380 /* Remove the specified attribute if present.  */
5381
5382 static void
5383 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5384 {
5385   dw_attr_ref *p;
5386   dw_attr_ref removed = NULL;
5387
5388   if (die != NULL)
5389     {
5390       for (p = &(die->die_attr); *p; p = &((*p)->dw_attr_next))
5391         if ((*p)->dw_attr == attr_kind)
5392           {
5393             removed = *p;
5394             *p = (*p)->dw_attr_next;
5395             break;
5396           }
5397
5398       if (removed != 0)
5399         free_AT (removed);
5400     }
5401 }
5402
5403 /* Remove child die whose die_tag is specified tag.  */
5404
5405 static void
5406 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
5407 {
5408   dw_die_ref current, prev, next;
5409   current = die->die_child;
5410   prev = NULL;
5411   while (current != NULL)
5412     {
5413       if (current->die_tag == tag)
5414         {
5415           next = current->die_sib;
5416           if (prev == NULL)
5417             die->die_child = next;
5418           else
5419             prev->die_sib = next;
5420           free_die (current);
5421           current = next;
5422         }
5423       else
5424         {
5425           prev = current;
5426           current = current->die_sib;
5427         }
5428     }
5429 }
5430
5431 /* Free up the memory used by DIE.  */
5432
5433 static inline void
5434 free_die (dw_die_ref die)
5435 {
5436   remove_children (die);
5437 }
5438
5439 /* Discard the children of this DIE.  */
5440
5441 static void
5442 remove_children (dw_die_ref die)
5443 {
5444   dw_die_ref child_die = die->die_child;
5445
5446   die->die_child = NULL;
5447
5448   while (child_die != NULL)
5449     {
5450       dw_die_ref tmp_die = child_die;
5451       dw_attr_ref a;
5452
5453       child_die = child_die->die_sib;
5454
5455       for (a = tmp_die->die_attr; a != NULL;)
5456         {
5457           dw_attr_ref tmp_a = a;
5458
5459           a = a->dw_attr_next;
5460           free_AT (tmp_a);
5461         }
5462
5463       free_die (tmp_die);
5464     }
5465 }
5466
5467 /* Add a child DIE below its parent.  We build the lists up in reverse
5468    addition order, and correct that in reverse_all_dies.  */
5469
5470 static inline void
5471 add_child_die (dw_die_ref die, dw_die_ref child_die)
5472 {
5473   if (die != NULL && child_die != NULL)
5474     {
5475       gcc_assert (die != child_die);
5476
5477       child_die->die_parent = die;
5478       child_die->die_sib = die->die_child;
5479       die->die_child = child_die;
5480     }
5481 }
5482
5483 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
5484    is the specification, to the front of PARENT's list of children.  */
5485
5486 static void
5487 splice_child_die (dw_die_ref parent, dw_die_ref child)
5488 {
5489   dw_die_ref *p;
5490
5491   /* We want the declaration DIE from inside the class, not the
5492      specification DIE at toplevel.  */
5493   if (child->die_parent != parent)
5494     {
5495       dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
5496
5497       if (tmp)
5498         child = tmp;
5499     }
5500
5501   gcc_assert (child->die_parent == parent
5502               || (child->die_parent
5503                   == get_AT_ref (parent, DW_AT_specification)));
5504
5505   for (p = &(child->die_parent->die_child); *p; p = &((*p)->die_sib))
5506     if (*p == child)
5507       {
5508         *p = child->die_sib;
5509         break;
5510       }
5511
5512   child->die_parent = parent;
5513   child->die_sib = parent->die_child;
5514   parent->die_child = child;
5515 }
5516
5517 /* Return a pointer to a newly created DIE node.  */
5518
5519 static inline dw_die_ref
5520 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
5521 {
5522   dw_die_ref die = ggc_alloc_cleared (sizeof (die_node));
5523
5524   die->die_tag = tag_value;
5525
5526   if (parent_die != NULL)
5527     add_child_die (parent_die, die);
5528   else
5529     {
5530       limbo_die_node *limbo_node;
5531
5532       limbo_node = ggc_alloc_cleared (sizeof (limbo_die_node));
5533       limbo_node->die = die;
5534       limbo_node->created_for = t;
5535       limbo_node->next = limbo_die_list;
5536       limbo_die_list = limbo_node;
5537     }
5538
5539   return die;
5540 }
5541
5542 /* Return the DIE associated with the given type specifier.  */
5543
5544 static inline dw_die_ref
5545 lookup_type_die (tree type)
5546 {
5547   return TYPE_SYMTAB_DIE (type);
5548 }
5549
5550 /* Equate a DIE to a given type specifier.  */
5551
5552 static inline void
5553 equate_type_number_to_die (tree type, dw_die_ref type_die)
5554 {
5555   TYPE_SYMTAB_DIE (type) = type_die;
5556 }
5557
5558 /* Returns a hash value for X (which really is a die_struct).  */
5559
5560 static hashval_t
5561 decl_die_table_hash (const void *x)
5562 {
5563   return (hashval_t) ((const dw_die_ref) x)->decl_id;
5564 }
5565
5566 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
5567
5568 static int
5569 decl_die_table_eq (const void *x, const void *y)
5570 {
5571   return (((const dw_die_ref) x)->decl_id == DECL_UID ((const tree) y));
5572 }
5573
5574 /* Return the DIE associated with a given declaration.  */
5575
5576 static inline dw_die_ref
5577 lookup_decl_die (tree decl)
5578 {
5579   return htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
5580 }
5581
5582 /* Returns a hash value for X (which really is a var_loc_list).  */
5583
5584 static hashval_t
5585 decl_loc_table_hash (const void *x)
5586 {
5587   return (hashval_t) ((const var_loc_list *) x)->decl_id;
5588 }
5589
5590 /* Return nonzero if decl_id of var_loc_list X is the same as
5591    UID of decl *Y.  */
5592
5593 static int
5594 decl_loc_table_eq (const void *x, const void *y)
5595 {
5596   return (((const var_loc_list *) x)->decl_id == DECL_UID ((const tree) y));
5597 }
5598
5599 /* Return the var_loc list associated with a given declaration.  */
5600
5601 static inline var_loc_list *
5602 lookup_decl_loc (tree decl)
5603 {
5604   return htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
5605 }
5606
5607 /* Equate a DIE to a particular declaration.  */
5608
5609 static void
5610 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5611 {
5612   unsigned int decl_id = DECL_UID (decl);
5613   void **slot;
5614
5615   slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
5616   *slot = decl_die;
5617   decl_die->decl_id = decl_id;
5618 }
5619
5620 /* Add a variable location node to the linked list for DECL.  */
5621
5622 static void
5623 add_var_loc_to_decl (tree decl, struct var_loc_node *loc)
5624 {
5625   unsigned int decl_id = DECL_UID (decl);
5626   var_loc_list *temp;
5627   void **slot;
5628
5629   slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
5630   if (*slot == NULL)
5631     {
5632       temp = ggc_alloc_cleared (sizeof (var_loc_list));
5633       temp->decl_id = decl_id;
5634       *slot = temp;
5635     }
5636   else
5637     temp = *slot;
5638
5639   if (temp->last)
5640     {
5641       /* If the current location is the same as the end of the list,
5642          we have nothing to do.  */
5643       if (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->last->var_loc_note),
5644                         NOTE_VAR_LOCATION_LOC (loc->var_loc_note)))
5645         {
5646           /* Add LOC to the end of list and update LAST.  */
5647           temp->last->next = loc;
5648           temp->last = loc;
5649         }
5650     }
5651   /* Do not add empty location to the beginning of the list.  */
5652   else if (NOTE_VAR_LOCATION_LOC (loc->var_loc_note) != NULL_RTX)
5653     {
5654       temp->first = loc;
5655       temp->last = loc;
5656     }
5657 }
5658 \f
5659 /* Keep track of the number of spaces used to indent the
5660    output of the debugging routines that print the structure of
5661    the DIE internal representation.  */
5662 static int print_indent;
5663
5664 /* Indent the line the number of spaces given by print_indent.  */
5665
5666 static inline void
5667 print_spaces (FILE *outfile)
5668 {
5669   fprintf (outfile, "%*s", print_indent, "");
5670 }
5671
5672 /* Print the information associated with a given DIE, and its children.
5673    This routine is a debugging aid only.  */
5674
5675 static void
5676 print_die (dw_die_ref die, FILE *outfile)
5677 {
5678   dw_attr_ref a;
5679   dw_die_ref c;
5680
5681   print_spaces (outfile);
5682   fprintf (outfile, "DIE %4lu: %s\n",
5683            die->die_offset, dwarf_tag_name (die->die_tag));
5684   print_spaces (outfile);
5685   fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
5686   fprintf (outfile, " offset: %lu\n", die->die_offset);
5687
5688   for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
5689     {
5690       print_spaces (outfile);
5691       fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
5692
5693       switch (AT_class (a))
5694         {
5695         case dw_val_class_addr:
5696           fprintf (outfile, "address");
5697           break;
5698         case dw_val_class_offset:
5699           fprintf (outfile, "offset");
5700           break;
5701         case dw_val_class_loc:
5702           fprintf (outfile, "location descriptor");
5703           break;
5704         case dw_val_class_loc_list:
5705           fprintf (outfile, "location list -> label:%s",
5706                    AT_loc_list (a)->ll_symbol);
5707           break;
5708         case dw_val_class_range_list:
5709           fprintf (outfile, "range list");
5710           break;
5711         case dw_val_class_const:
5712           fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
5713           break;
5714         case dw_val_class_unsigned_const:
5715           fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
5716           break;
5717         case dw_val_class_long_long:
5718           fprintf (outfile, "constant (%lu,%lu)",
5719                    a->dw_attr_val.v.val_long_long.hi,
5720                    a->dw_attr_val.v.val_long_long.low);
5721           break;
5722         case dw_val_class_vec:
5723           fprintf (outfile, "floating-point or vector constant");
5724           break;
5725         case dw_val_class_flag:
5726           fprintf (outfile, "%u", AT_flag (a));
5727           break;
5728         case dw_val_class_die_ref:
5729           if (AT_ref (a) != NULL)
5730             {
5731               if (AT_ref (a)->die_symbol)
5732                 fprintf (outfile, "die -> label: %s", AT_ref (a)->die_symbol);
5733               else
5734                 fprintf (outfile, "die -> %lu", AT_ref (a)->die_offset);
5735             }
5736           else
5737             fprintf (outfile, "die -> <null>");
5738           break;
5739         case dw_val_class_lbl_id:
5740         case dw_val_class_lbl_offset:
5741           fprintf (outfile, "label: %s", AT_lbl (a));
5742           break;
5743         case dw_val_class_str:
5744           if (AT_string (a) != NULL)
5745             fprintf (outfile, "\"%s\"", AT_string (a));
5746           else
5747             fprintf (outfile, "<null>");
5748           break;
5749         default:
5750           break;
5751         }
5752
5753       fprintf (outfile, "\n");
5754     }
5755
5756   if (die->die_child != NULL)
5757     {
5758       print_indent += 4;
5759       for (c = die->die_child; c != NULL; c = c->die_sib)
5760         print_die (c, outfile);
5761
5762       print_indent -= 4;
5763     }
5764   if (print_indent == 0)
5765     fprintf (outfile, "\n");
5766 }
5767
5768 /* Print the contents of the source code line number correspondence table.
5769    This routine is a debugging aid only.  */
5770
5771 static void
5772 print_dwarf_line_table (FILE *outfile)
5773 {
5774   unsigned i;
5775   dw_line_info_ref line_info;
5776
5777   fprintf (outfile, "\n\nDWARF source line information\n");
5778   for (i = 1; i < line_info_table_in_use; i++)
5779     {
5780       line_info = &line_info_table[i];
5781       fprintf (outfile, "%5d: ", i);
5782       fprintf (outfile, "%-20s",
5783                VARRAY_CHAR_PTR (file_table, line_info->dw_file_num));
5784       fprintf (outfile, "%6ld", line_info->dw_line_num);
5785       fprintf (outfile, "\n");
5786     }
5787
5788   fprintf (outfile, "\n\n");
5789 }
5790
5791 /* Print the information collected for a given DIE.  */
5792
5793 void
5794 debug_dwarf_die (dw_die_ref die)
5795 {
5796   print_die (die, stderr);
5797 }
5798
5799 /* Print all DWARF information collected for the compilation unit.
5800    This routine is a debugging aid only.  */
5801
5802 void
5803 debug_dwarf (void)
5804 {
5805   print_indent = 0;
5806   print_die (comp_unit_die, stderr);
5807   if (! DWARF2_ASM_LINE_DEBUG_INFO)
5808     print_dwarf_line_table (stderr);
5809 }
5810 \f
5811 /* We build up the lists of children and attributes by pushing new ones
5812    onto the beginning of the list.  Reverse the lists for DIE so that
5813    they are in order of addition.  */
5814
5815 static void
5816 reverse_die_lists (dw_die_ref die)
5817 {
5818   dw_die_ref c, cp, cn;
5819   dw_attr_ref a, ap, an;
5820
5821   for (a = die->die_attr, ap = 0; a; a = an)
5822     {
5823       an = a->dw_attr_next;
5824       a->dw_attr_next = ap;
5825       ap = a;
5826     }
5827
5828   die->die_attr = ap;
5829
5830   for (c = die->die_child, cp = 0; c; c = cn)
5831     {
5832       cn = c->die_sib;
5833       c->die_sib = cp;
5834       cp = c;
5835     }
5836
5837   die->die_child = cp;
5838 }
5839
5840 /* reverse_die_lists only reverses the single die you pass it. Since we used to
5841    reverse all dies in add_sibling_attributes, which runs through all the dies,
5842    it would reverse all the dies.  Now, however, since we don't call
5843    reverse_die_lists in add_sibling_attributes, we need a routine to
5844    recursively reverse all the dies. This is that routine.  */
5845
5846 static void
5847 reverse_all_dies (dw_die_ref die)
5848 {
5849   dw_die_ref c;
5850
5851   reverse_die_lists (die);
5852
5853   for (c = die->die_child; c; c = c->die_sib)
5854     reverse_all_dies (c);
5855 }
5856
5857 /* Start a new compilation unit DIE for an include file.  OLD_UNIT is the CU
5858    for the enclosing include file, if any.  BINCL_DIE is the DW_TAG_GNU_BINCL
5859    DIE that marks the start of the DIEs for this include file.  */
5860
5861 static dw_die_ref
5862 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5863 {
5864   const char *filename = get_AT_string (bincl_die, DW_AT_name);
5865   dw_die_ref new_unit = gen_compile_unit_die (filename);
5866
5867   new_unit->die_sib = old_unit;
5868   return new_unit;
5869 }
5870
5871 /* Close an include-file CU and reopen the enclosing one.  */
5872
5873 static dw_die_ref
5874 pop_compile_unit (dw_die_ref old_unit)
5875 {
5876   dw_die_ref new_unit = old_unit->die_sib;
5877
5878   old_unit->die_sib = NULL;
5879   return new_unit;
5880 }
5881
5882 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5883 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5884
5885 /* Calculate the checksum of a location expression.  */
5886
5887 static inline void
5888 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5889 {
5890   CHECKSUM (loc->dw_loc_opc);
5891   CHECKSUM (loc->dw_loc_oprnd1);
5892   CHECKSUM (loc->dw_loc_oprnd2);
5893 }
5894
5895 /* Calculate the checksum of an attribute.  */
5896
5897 static void
5898 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
5899 {
5900   dw_loc_descr_ref loc;
5901   rtx r;
5902
5903   CHECKSUM (at->dw_attr);
5904
5905   /* We don't care about differences in file numbering.  */
5906   if (at->dw_attr == DW_AT_decl_file
5907       /* Or that this was compiled with a different compiler snapshot; if
5908          the output is the same, that's what matters.  */
5909       || at->dw_attr == DW_AT_producer)
5910     return;
5911
5912   switch (AT_class (at))
5913     {
5914     case dw_val_class_const:
5915       CHECKSUM (at->dw_attr_val.v.val_int);
5916       break;
5917     case dw_val_class_unsigned_const:
5918       CHECKSUM (at->dw_attr_val.v.val_unsigned);
5919       break;
5920     case dw_val_class_long_long:
5921       CHECKSUM (at->dw_attr_val.v.val_long_long);
5922       break;
5923     case dw_val_class_vec:
5924       CHECKSUM (at->dw_attr_val.v.val_vec);
5925       break;
5926     case dw_val_class_flag:
5927       CHECKSUM (at->dw_attr_val.v.val_flag);
5928       break;
5929     case dw_val_class_str:
5930       CHECKSUM_STRING (AT_string (at));
5931       break;
5932
5933     case dw_val_class_addr:
5934       r = AT_addr (at);
5935       gcc_assert (GET_CODE (r) == SYMBOL_REF);
5936       CHECKSUM_STRING (XSTR (r, 0));
5937       break;
5938
5939     case dw_val_class_offset:
5940       CHECKSUM (at->dw_attr_val.v.val_offset);
5941       break;
5942
5943     case dw_val_class_loc:
5944       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5945         loc_checksum (loc, ctx);
5946       break;
5947
5948     case dw_val_class_die_ref:
5949       die_checksum (AT_ref (at), ctx, mark);
5950       break;
5951
5952     case dw_val_class_fde_ref:
5953     case dw_val_class_lbl_id:
5954     case dw_val_class_lbl_offset:
5955       break;
5956
5957     default:
5958       break;
5959     }
5960 }
5961
5962 /* Calculate the checksum of a DIE.  */
5963
5964 static void
5965 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5966 {
5967   dw_die_ref c;
5968   dw_attr_ref a;
5969
5970   /* To avoid infinite recursion.  */
5971   if (die->die_mark)
5972     {
5973       CHECKSUM (die->die_mark);
5974       return;
5975     }
5976   die->die_mark = ++(*mark);
5977
5978   CHECKSUM (die->die_tag);
5979
5980   for (a = die->die_attr; a; a = a->dw_attr_next)
5981     attr_checksum (a, ctx, mark);
5982
5983   for (c = die->die_child; c; c = c->die_sib)
5984     die_checksum (c, ctx, mark);
5985 }
5986
5987 #undef CHECKSUM
5988 #undef CHECKSUM_STRING
5989
5990 /* Do the location expressions look same?  */
5991 static inline int
5992 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
5993 {
5994   return loc1->dw_loc_opc == loc2->dw_loc_opc
5995          && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
5996          && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
5997 }
5998
5999 /* Do the values look the same?  */
6000 static int
6001 same_dw_val_p (dw_val_node *v1, dw_val_node *v2, int *mark)
6002 {
6003   dw_loc_descr_ref loc1, loc2;
6004   rtx r1, r2;
6005
6006   if (v1->val_class != v2->val_class)
6007     return 0;
6008
6009   switch (v1->val_class)
6010     {
6011     case dw_val_class_const:
6012       return v1->v.val_int == v2->v.val_int;
6013     case dw_val_class_unsigned_const:
6014       return v1->v.val_unsigned == v2->v.val_unsigned;
6015     case dw_val_class_long_long:
6016       return v1->v.val_long_long.hi == v2->v.val_long_long.hi
6017              && v1->v.val_long_long.low == v2->v.val_long_long.low;
6018     case dw_val_class_vec:
6019       if (v1->v.val_vec.length != v2->v.val_vec.length
6020           || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6021         return 0;
6022       if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6023                   v1->v.val_vec.length * v1->v.val_vec.elt_size))
6024         return 0;
6025       return 1;
6026     case dw_val_class_flag:
6027       return v1->v.val_flag == v2->v.val_flag;
6028     case dw_val_class_str:
6029       return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
6030
6031     case dw_val_class_addr:
6032       r1 = v1->v.val_addr;
6033       r2 = v2->v.val_addr;
6034       if (GET_CODE (r1) != GET_CODE (r2))
6035         return 0;
6036       gcc_assert (GET_CODE (r1) == SYMBOL_REF);
6037       return !strcmp (XSTR (r1, 0), XSTR (r2, 0));
6038
6039     case dw_val_class_offset:
6040       return v1->v.val_offset == v2->v.val_offset;
6041
6042     case dw_val_class_loc:
6043       for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6044            loc1 && loc2;
6045            loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6046         if (!same_loc_p (loc1, loc2, mark))
6047           return 0;
6048       return !loc1 && !loc2;
6049
6050     case dw_val_class_die_ref:
6051       return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6052
6053     case dw_val_class_fde_ref:
6054     case dw_val_class_lbl_id:
6055     case dw_val_class_lbl_offset:
6056       return 1;
6057
6058     default:
6059       return 1;
6060     }
6061 }
6062
6063 /* Do the attributes look the same?  */
6064
6065 static int
6066 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
6067 {
6068   if (at1->dw_attr != at2->dw_attr)
6069     return 0;
6070
6071   /* We don't care about differences in file numbering.  */
6072   if (at1->dw_attr == DW_AT_decl_file
6073       /* Or that this was compiled with a different compiler snapshot; if
6074          the output is the same, that's what matters.  */
6075       || at1->dw_attr == DW_AT_producer)
6076     return 1;
6077
6078   return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6079 }
6080
6081 /* Do the dies look the same?  */
6082
6083 static int
6084 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6085 {
6086   dw_die_ref c1, c2;
6087   dw_attr_ref a1, a2;
6088
6089   /* To avoid infinite recursion.  */
6090   if (die1->die_mark)
6091     return die1->die_mark == die2->die_mark;
6092   die1->die_mark = die2->die_mark = ++(*mark);
6093
6094   if (die1->die_tag != die2->die_tag)
6095     return 0;
6096
6097   for (a1 = die1->die_attr, a2 = die2->die_attr;
6098        a1 && a2;
6099        a1 = a1->dw_attr_next, a2 = a2->dw_attr_next)
6100     if (!same_attr_p (a1, a2, mark))
6101       return 0;
6102   if (a1 || a2)
6103     return 0;
6104
6105   for (c1 = die1->die_child, c2 = die2->die_child;
6106        c1 && c2;
6107        c1 = c1->die_sib, c2 = c2->die_sib)
6108     if (!same_die_p (c1, c2, mark))
6109       return 0;
6110   if (c1 || c2)
6111     return 0;
6112
6113   return 1;
6114 }
6115
6116 /* Do the dies look the same?  Wrapper around same_die_p.  */
6117
6118 static int
6119 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6120 {
6121   int mark = 0;
6122   int ret = same_die_p (die1, die2, &mark);
6123
6124   unmark_all_dies (die1);
6125   unmark_all_dies (die2);
6126
6127   return ret;
6128 }
6129
6130 /* The prefix to attach to symbols on DIEs in the current comdat debug
6131    info section.  */
6132 static char *comdat_symbol_id;
6133
6134 /* The index of the current symbol within the current comdat CU.  */
6135 static unsigned int comdat_symbol_number;
6136
6137 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6138    children, and set comdat_symbol_id accordingly.  */
6139
6140 static void
6141 compute_section_prefix (dw_die_ref unit_die)
6142 {
6143   const char *die_name = get_AT_string (unit_die, DW_AT_name);
6144   const char *base = die_name ? lbasename (die_name) : "anonymous";
6145   char *name = alloca (strlen (base) + 64);
6146   char *p;
6147   int i, mark;
6148   unsigned char checksum[16];
6149   struct md5_ctx ctx;
6150
6151   /* Compute the checksum of the DIE, then append part of it as hex digits to
6152      the name filename of the unit.  */
6153
6154   md5_init_ctx (&ctx);
6155   mark = 0;
6156   die_checksum (unit_die, &ctx, &mark);
6157   unmark_all_dies (unit_die);
6158   md5_finish_ctx (&ctx, checksum);
6159
6160   sprintf (name, "%s.", base);
6161   clean_symbol_name (name);
6162
6163   p = name + strlen (name);
6164   for (i = 0; i < 4; i++)
6165     {
6166       sprintf (p, "%.2x", checksum[i]);
6167       p += 2;
6168     }
6169
6170   comdat_symbol_id = unit_die->die_symbol = xstrdup (name);
6171   comdat_symbol_number = 0;
6172 }
6173
6174 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P.  */
6175
6176 static int
6177 is_type_die (dw_die_ref die)
6178 {
6179   switch (die->die_tag)
6180     {
6181     case DW_TAG_array_type:
6182     case DW_TAG_class_type:
6183     case DW_TAG_enumeration_type:
6184     case DW_TAG_pointer_type:
6185     case DW_TAG_reference_type:
6186     case DW_TAG_string_type:
6187     case DW_TAG_structure_type:
6188     case DW_TAG_subroutine_type:
6189     case DW_TAG_union_type:
6190     case DW_TAG_ptr_to_member_type:
6191     case DW_TAG_set_type:
6192     case DW_TAG_subrange_type:
6193     case DW_TAG_base_type:
6194     case DW_TAG_const_type:
6195     case DW_TAG_file_type:
6196     case DW_TAG_packed_type:
6197     case DW_TAG_volatile_type:
6198     case DW_TAG_typedef:
6199       return 1;
6200     default:
6201       return 0;
6202     }
6203 }
6204
6205 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6206    Basically, we want to choose the bits that are likely to be shared between
6207    compilations (types) and leave out the bits that are specific to individual
6208    compilations (functions).  */
6209
6210 static int
6211 is_comdat_die (dw_die_ref c)
6212 {
6213   /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6214      we do for stabs.  The advantage is a greater likelihood of sharing between
6215      objects that don't include headers in the same order (and therefore would
6216      put the base types in a different comdat).  jason 8/28/00 */
6217
6218   if (c->die_tag == DW_TAG_base_type)
6219     return 0;
6220
6221   if (c->die_tag == DW_TAG_pointer_type
6222       || c->die_tag == DW_TAG_reference_type
6223       || c->die_tag == DW_TAG_const_type
6224       || c->die_tag == DW_TAG_volatile_type)
6225     {
6226       dw_die_ref t = get_AT_ref (c, DW_AT_type);
6227
6228       return t ? is_comdat_die (t) : 0;
6229     }
6230
6231   return is_type_die (c);
6232 }
6233
6234 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6235    compilation unit.  */
6236
6237 static int
6238 is_symbol_die (dw_die_ref c)
6239 {
6240   return (is_type_die (c)
6241           || (get_AT (c, DW_AT_declaration)
6242               && !get_AT (c, DW_AT_specification))
6243           || c->die_tag == DW_TAG_namespace);
6244 }
6245
6246 static char *
6247 gen_internal_sym (const char *prefix)
6248 {
6249   char buf[256];
6250
6251   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6252   return xstrdup (buf);
6253 }
6254
6255 /* Assign symbols to all worthy DIEs under DIE.  */
6256
6257 static void
6258 assign_symbol_names (dw_die_ref die)
6259 {
6260   dw_die_ref c;
6261
6262   if (is_symbol_die (die))
6263     {
6264       if (comdat_symbol_id)
6265         {
6266           char *p = alloca (strlen (comdat_symbol_id) + 64);
6267
6268           sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6269                    comdat_symbol_id, comdat_symbol_number++);
6270           die->die_symbol = xstrdup (p);
6271         }
6272       else
6273         die->die_symbol = gen_internal_sym ("LDIE");
6274     }
6275
6276   for (c = die->die_child; c != NULL; c = c->die_sib)
6277     assign_symbol_names (c);
6278 }
6279
6280 struct cu_hash_table_entry
6281 {
6282   dw_die_ref cu;
6283   unsigned min_comdat_num, max_comdat_num;
6284   struct cu_hash_table_entry *next;
6285 };
6286
6287 /* Routines to manipulate hash table of CUs.  */
6288 static hashval_t
6289 htab_cu_hash (const void *of)
6290 {
6291   const struct cu_hash_table_entry *entry = of;
6292
6293   return htab_hash_string (entry->cu->die_symbol);
6294 }
6295
6296 static int
6297 htab_cu_eq (const void *of1, const void *of2)
6298 {
6299   const struct cu_hash_table_entry *entry1 = of1;
6300   const struct die_struct *entry2 = of2;
6301
6302   return !strcmp (entry1->cu->die_symbol, entry2->die_symbol);
6303 }
6304
6305 static void
6306 htab_cu_del (void *what)
6307 {
6308   struct cu_hash_table_entry *next, *entry = what;
6309
6310   while (entry)
6311     {
6312       next = entry->next;
6313       free (entry);
6314       entry = next;
6315     }
6316 }
6317
6318 /* Check whether we have already seen this CU and set up SYM_NUM
6319    accordingly.  */
6320 static int
6321 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
6322 {
6323   struct cu_hash_table_entry dummy;
6324   struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6325
6326   dummy.max_comdat_num = 0;
6327
6328   slot = (struct cu_hash_table_entry **)
6329     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
6330         INSERT);
6331   entry = *slot;
6332
6333   for (; entry; last = entry, entry = entry->next)
6334     {
6335       if (same_die_p_wrap (cu, entry->cu))
6336         break;
6337     }
6338
6339   if (entry)
6340     {
6341       *sym_num = entry->min_comdat_num;
6342       return 1;
6343     }
6344
6345   entry = xcalloc (1, sizeof (struct cu_hash_table_entry));
6346   entry->cu = cu;
6347   entry->min_comdat_num = *sym_num = last->max_comdat_num;
6348   entry->next = *slot;
6349   *slot = entry;
6350
6351   return 0;
6352 }
6353
6354 /* Record SYM_NUM to record of CU in HTABLE.  */
6355 static void
6356 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
6357 {
6358   struct cu_hash_table_entry **slot, *entry;
6359
6360   slot = (struct cu_hash_table_entry **)
6361     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
6362         NO_INSERT);
6363   entry = *slot;
6364
6365   entry->max_comdat_num = sym_num;
6366 }
6367
6368 /* Traverse the DIE (which is always comp_unit_die), and set up
6369    additional compilation units for each of the include files we see
6370    bracketed by BINCL/EINCL.  */
6371
6372 static void
6373 break_out_includes (dw_die_ref die)
6374 {
6375   dw_die_ref *ptr;
6376   dw_die_ref unit = NULL;
6377   limbo_die_node *node, **pnode;
6378   htab_t cu_hash_table;
6379
6380   for (ptr = &(die->die_child); *ptr;)
6381     {
6382       dw_die_ref c = *ptr;
6383
6384       if (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6385           || (unit && is_comdat_die (c)))
6386         {
6387           /* This DIE is for a secondary CU; remove it from the main one.  */
6388           *ptr = c->die_sib;
6389
6390           if (c->die_tag == DW_TAG_GNU_BINCL)
6391             {
6392               unit = push_new_compile_unit (unit, c);
6393               free_die (c);
6394             }
6395           else if (c->die_tag == DW_TAG_GNU_EINCL)
6396             {
6397               unit = pop_compile_unit (unit);
6398               free_die (c);
6399             }
6400           else
6401             add_child_die (unit, c);
6402         }
6403       else
6404         {
6405           /* Leave this DIE in the main CU.  */
6406           ptr = &(c->die_sib);
6407           continue;
6408         }
6409     }
6410
6411 #if 0
6412   /* We can only use this in debugging, since the frontend doesn't check
6413      to make sure that we leave every include file we enter.  */
6414   gcc_assert (!unit);
6415 #endif
6416
6417   assign_symbol_names (die);
6418   cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
6419   for (node = limbo_die_list, pnode = &limbo_die_list;
6420        node;
6421        node = node->next)
6422     {
6423       int is_dupl;
6424
6425       compute_section_prefix (node->die);
6426       is_dupl = check_duplicate_cu (node->die, cu_hash_table,
6427                         &comdat_symbol_number);
6428       assign_symbol_names (node->die);
6429       if (is_dupl)
6430         *pnode = node->next;
6431       else
6432         {
6433           pnode = &node->next;
6434           record_comdat_symbol_number (node->die, cu_hash_table,
6435                 comdat_symbol_number);
6436         }
6437     }
6438   htab_delete (cu_hash_table);
6439 }
6440
6441 /* Traverse the DIE and add a sibling attribute if it may have the
6442    effect of speeding up access to siblings.  To save some space,
6443    avoid generating sibling attributes for DIE's without children.  */
6444
6445 static void
6446 add_sibling_attributes (dw_die_ref die)
6447 {
6448   dw_die_ref c;
6449
6450   if (die->die_tag != DW_TAG_compile_unit
6451       && die->die_sib && die->die_child != NULL)
6452     /* Add the sibling link to the front of the attribute list.  */
6453     add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
6454
6455   for (c = die->die_child; c != NULL; c = c->die_sib)
6456     add_sibling_attributes (c);
6457 }
6458
6459 /* Output all location lists for the DIE and its children.  */
6460
6461 static void
6462 output_location_lists (dw_die_ref die)
6463 {
6464   dw_die_ref c;
6465   dw_attr_ref d_attr;
6466
6467   for (d_attr = die->die_attr; d_attr; d_attr = d_attr->dw_attr_next)
6468     if (AT_class (d_attr) == dw_val_class_loc_list)
6469       output_loc_list (AT_loc_list (d_attr));
6470
6471   for (c = die->die_child; c != NULL; c = c->die_sib)
6472     output_location_lists (c);
6473
6474 }
6475
6476 /* The format of each DIE (and its attribute value pairs) is encoded in an
6477    abbreviation table.  This routine builds the abbreviation table and assigns
6478    a unique abbreviation id for each abbreviation entry.  The children of each
6479    die are visited recursively.  */
6480
6481 static void
6482 build_abbrev_table (dw_die_ref die)
6483 {
6484   unsigned long abbrev_id;
6485   unsigned int n_alloc;
6486   dw_die_ref c;
6487   dw_attr_ref d_attr, a_attr;
6488
6489   /* Scan the DIE references, and mark as external any that refer to
6490      DIEs from other CUs (i.e. those which are not marked).  */
6491   for (d_attr = die->die_attr; d_attr; d_attr = d_attr->dw_attr_next)
6492     if (AT_class (d_attr) == dw_val_class_die_ref
6493         && AT_ref (d_attr)->die_mark == 0)
6494       {
6495         gcc_assert (AT_ref (d_attr)->die_symbol);
6496
6497         set_AT_ref_external (d_attr, 1);
6498       }
6499
6500   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
6501     {
6502       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
6503
6504       if (abbrev->die_tag == die->die_tag)
6505         {
6506           if ((abbrev->die_child != NULL) == (die->die_child != NULL))
6507             {
6508               a_attr = abbrev->die_attr;
6509               d_attr = die->die_attr;
6510
6511               while (a_attr != NULL && d_attr != NULL)
6512                 {
6513                   if ((a_attr->dw_attr != d_attr->dw_attr)
6514                       || (value_format (a_attr) != value_format (d_attr)))
6515                     break;
6516
6517                   a_attr = a_attr->dw_attr_next;
6518                   d_attr = d_attr->dw_attr_next;
6519                 }
6520
6521               if (a_attr == NULL && d_attr == NULL)
6522                 break;
6523             }
6524         }
6525     }
6526
6527   if (abbrev_id >= abbrev_die_table_in_use)
6528     {
6529       if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
6530         {
6531           n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
6532           abbrev_die_table = ggc_realloc (abbrev_die_table,
6533                                           sizeof (dw_die_ref) * n_alloc);
6534
6535           memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
6536                  (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
6537           abbrev_die_table_allocated = n_alloc;
6538         }
6539
6540       ++abbrev_die_table_in_use;
6541       abbrev_die_table[abbrev_id] = die;
6542     }
6543
6544   die->die_abbrev = abbrev_id;
6545   for (c = die->die_child; c != NULL; c = c->die_sib)
6546     build_abbrev_table (c);
6547 }
6548 \f
6549 /* Return the power-of-two number of bytes necessary to represent VALUE.  */
6550
6551 static int
6552 constant_size (long unsigned int value)
6553 {
6554   int log;
6555
6556   if (value == 0)
6557     log = 0;
6558   else
6559     log = floor_log2 (value);
6560
6561   log = log / 8;
6562   log = 1 << (floor_log2 (log) + 1);
6563
6564   return log;
6565 }
6566
6567 /* Return the size of a DIE as it is represented in the
6568    .debug_info section.  */
6569
6570 static unsigned long
6571 size_of_die (dw_die_ref die)
6572 {
6573   unsigned long size = 0;
6574   dw_attr_ref a;
6575
6576   size += size_of_uleb128 (die->die_abbrev);
6577   for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
6578     {
6579       switch (AT_class (a))
6580         {
6581         case dw_val_class_addr:
6582           size += DWARF2_ADDR_SIZE;
6583           break;
6584         case dw_val_class_offset:
6585           size += DWARF_OFFSET_SIZE;
6586           break;
6587         case dw_val_class_loc:
6588           {
6589             unsigned long lsize = size_of_locs (AT_loc (a));
6590
6591             /* Block length.  */
6592             size += constant_size (lsize);
6593             size += lsize;
6594           }
6595           break;
6596         case dw_val_class_loc_list:
6597           size += DWARF_OFFSET_SIZE;
6598           break;
6599         case dw_val_class_range_list:
6600           size += DWARF_OFFSET_SIZE;
6601           break;
6602         case dw_val_class_const:
6603           size += size_of_sleb128 (AT_int (a));
6604           break;
6605         case dw_val_class_unsigned_const:
6606           size += constant_size (AT_unsigned (a));
6607           break;
6608         case dw_val_class_long_long:
6609           size += 1 + 2*HOST_BITS_PER_LONG/HOST_BITS_PER_CHAR; /* block */
6610           break;
6611         case dw_val_class_vec:
6612           size += 1 + (a->dw_attr_val.v.val_vec.length
6613                        * a->dw_attr_val.v.val_vec.elt_size); /* block */
6614           break;
6615         case dw_val_class_flag:
6616           size += 1;
6617           break;
6618         case dw_val_class_die_ref:
6619           if (AT_ref_external (a))
6620             size += DWARF2_ADDR_SIZE;
6621           else
6622             size += DWARF_OFFSET_SIZE;
6623           break;
6624         case dw_val_class_fde_ref:
6625           size += DWARF_OFFSET_SIZE;
6626           break;
6627         case dw_val_class_lbl_id:
6628           size += DWARF2_ADDR_SIZE;
6629           break;
6630         case dw_val_class_lbl_offset:
6631           size += DWARF_OFFSET_SIZE;
6632           break;
6633         case dw_val_class_str:
6634           if (AT_string_form (a) == DW_FORM_strp)
6635             size += DWARF_OFFSET_SIZE;
6636           else
6637             size += strlen (a->dw_attr_val.v.val_str->str) + 1;
6638           break;
6639         default:
6640           gcc_unreachable ();
6641         }
6642     }
6643
6644   return size;
6645 }
6646
6647 /* Size the debugging information associated with a given DIE.  Visits the
6648    DIE's children recursively.  Updates the global variable next_die_offset, on
6649    each time through.  Uses the current value of next_die_offset to update the
6650    die_offset field in each DIE.  */
6651
6652 static void
6653 calc_die_sizes (dw_die_ref die)
6654 {
6655   dw_die_ref c;
6656
6657   die->die_offset = next_die_offset;
6658   next_die_offset += size_of_die (die);
6659
6660   for (c = die->die_child; c != NULL; c = c->die_sib)
6661     calc_die_sizes (c);
6662
6663   if (die->die_child != NULL)
6664     /* Count the null byte used to terminate sibling lists.  */
6665     next_die_offset += 1;
6666 }
6667
6668 /* Set the marks for a die and its children.  We do this so
6669    that we know whether or not a reference needs to use FORM_ref_addr; only
6670    DIEs in the same CU will be marked.  We used to clear out the offset
6671    and use that as the flag, but ran into ordering problems.  */
6672
6673 static void
6674 mark_dies (dw_die_ref die)
6675 {
6676   dw_die_ref c;
6677
6678   gcc_assert (!die->die_mark);
6679
6680   die->die_mark = 1;
6681   for (c = die->die_child; c; c = c->die_sib)
6682     mark_dies (c);
6683 }
6684
6685 /* Clear the marks for a die and its children.  */
6686
6687 static void
6688 unmark_dies (dw_die_ref die)
6689 {
6690   dw_die_ref c;
6691
6692   gcc_assert (die->die_mark);
6693
6694   die->die_mark = 0;
6695   for (c = die->die_child; c; c = c->die_sib)
6696     unmark_dies (c);
6697 }
6698
6699 /* Clear the marks for a die, its children and referred dies.  */
6700
6701 static void
6702 unmark_all_dies (dw_die_ref die)
6703 {
6704   dw_die_ref c;
6705   dw_attr_ref a;
6706
6707   if (!die->die_mark)
6708     return;
6709   die->die_mark = 0;
6710
6711   for (c = die->die_child; c; c = c->die_sib)
6712     unmark_all_dies (c);
6713
6714   for (a = die->die_attr; a; a = a->dw_attr_next)
6715     if (AT_class (a) == dw_val_class_die_ref)
6716       unmark_all_dies (AT_ref (a));
6717 }
6718
6719 /* Return the size of the .debug_pubnames table  generated for the
6720    compilation unit.  */
6721
6722 static unsigned long
6723 size_of_pubnames (void)
6724 {
6725   unsigned long size;
6726   unsigned i;
6727
6728   size = DWARF_PUBNAMES_HEADER_SIZE;
6729   for (i = 0; i < pubname_table_in_use; i++)
6730     {
6731       pubname_ref p = &pubname_table[i];
6732       size += DWARF_OFFSET_SIZE + strlen (p->name) + 1;
6733     }
6734
6735   size += DWARF_OFFSET_SIZE;
6736   return size;
6737 }
6738
6739 /* Return the size of the information in the .debug_aranges section.  */
6740
6741 static unsigned long
6742 size_of_aranges (void)
6743 {
6744   unsigned long size;
6745
6746   size = DWARF_ARANGES_HEADER_SIZE;
6747
6748   /* Count the address/length pair for this compilation unit.  */
6749   size += 2 * DWARF2_ADDR_SIZE;
6750   size += 2 * DWARF2_ADDR_SIZE * arange_table_in_use;
6751
6752   /* Count the two zero words used to terminated the address range table.  */
6753   size += 2 * DWARF2_ADDR_SIZE;
6754   return size;
6755 }
6756 \f
6757 /* Select the encoding of an attribute value.  */
6758
6759 static enum dwarf_form
6760 value_format (dw_attr_ref a)
6761 {
6762   switch (a->dw_attr_val.val_class)
6763     {
6764     case dw_val_class_addr:
6765       return DW_FORM_addr;
6766     case dw_val_class_range_list:
6767     case dw_val_class_offset:
6768       switch (DWARF_OFFSET_SIZE)
6769         {
6770         case 4:
6771           return DW_FORM_data4;
6772         case 8:
6773           return DW_FORM_data8;
6774         default:
6775           gcc_unreachable ();
6776         }
6777     case dw_val_class_loc_list:
6778       /* FIXME: Could be DW_FORM_data8, with a > 32 bit size
6779          .debug_loc section */
6780       return DW_FORM_data4;
6781     case dw_val_class_loc:
6782       switch (constant_size (size_of_locs (AT_loc (a))))
6783         {
6784         case 1:
6785           return DW_FORM_block1;
6786         case 2:
6787           return DW_FORM_block2;
6788         default:
6789           gcc_unreachable ();
6790         }
6791     case dw_val_class_const:
6792       return DW_FORM_sdata;
6793     case dw_val_class_unsigned_const:
6794       switch (constant_size (AT_unsigned (a)))
6795         {
6796         case 1:
6797           return DW_FORM_data1;
6798         case 2:
6799           return DW_FORM_data2;
6800         case 4:
6801           return DW_FORM_data4;
6802         case 8:
6803           return DW_FORM_data8;
6804         default:
6805           gcc_unreachable ();
6806         }
6807     case dw_val_class_long_long:
6808       return DW_FORM_block1;
6809     case dw_val_class_vec:
6810       return DW_FORM_block1;
6811     case dw_val_class_flag:
6812       return DW_FORM_flag;
6813     case dw_val_class_die_ref:
6814       if (AT_ref_external (a))
6815         return DW_FORM_ref_addr;
6816       else
6817         return DW_FORM_ref;
6818     case dw_val_class_fde_ref:
6819       return DW_FORM_data;
6820     case dw_val_class_lbl_id:
6821       return DW_FORM_addr;
6822     case dw_val_class_lbl_offset:
6823       return DW_FORM_data;
6824     case dw_val_class_str:
6825       return AT_string_form (a);
6826
6827     default:
6828       gcc_unreachable ();
6829     }
6830 }
6831
6832 /* Output the encoding of an attribute value.  */
6833
6834 static void
6835 output_value_format (dw_attr_ref a)
6836 {
6837   enum dwarf_form form = value_format (a);
6838
6839   dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
6840 }
6841
6842 /* Output the .debug_abbrev section which defines the DIE abbreviation
6843    table.  */
6844
6845 static void
6846 output_abbrev_section (void)
6847 {
6848   unsigned long abbrev_id;
6849
6850   dw_attr_ref a_attr;
6851
6852   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
6853     {
6854       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
6855
6856       dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
6857       dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
6858                                    dwarf_tag_name (abbrev->die_tag));
6859
6860       if (abbrev->die_child != NULL)
6861         dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
6862       else
6863         dw2_asm_output_data (1, DW_children_no, "DW_children_no");
6864
6865       for (a_attr = abbrev->die_attr; a_attr != NULL;
6866            a_attr = a_attr->dw_attr_next)
6867         {
6868           dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
6869                                        dwarf_attr_name (a_attr->dw_attr));
6870           output_value_format (a_attr);
6871         }
6872
6873       dw2_asm_output_data (1, 0, NULL);
6874       dw2_asm_output_data (1, 0, NULL);
6875     }
6876
6877   /* Terminate the table.  */
6878   dw2_asm_output_data (1, 0, NULL);
6879 }
6880
6881 /* Output a symbol we can use to refer to this DIE from another CU.  */
6882
6883 static inline void
6884 output_die_symbol (dw_die_ref die)
6885 {
6886   char *sym = die->die_symbol;
6887
6888   if (sym == 0)
6889     return;
6890
6891   if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
6892     /* We make these global, not weak; if the target doesn't support
6893        .linkonce, it doesn't support combining the sections, so debugging
6894        will break.  */
6895     targetm.asm_out.globalize_label (asm_out_file, sym);
6896
6897   ASM_OUTPUT_LABEL (asm_out_file, sym);
6898 }
6899
6900 /* Return a new location list, given the begin and end range, and the
6901    expression. gensym tells us whether to generate a new internal symbol for
6902    this location list node, which is done for the head of the list only.  */
6903
6904 static inline dw_loc_list_ref
6905 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
6906               const char *section, unsigned int gensym)
6907 {
6908   dw_loc_list_ref retlist = ggc_alloc_cleared (sizeof (dw_loc_list_node));
6909
6910   retlist->begin = begin;
6911   retlist->end = end;
6912   retlist->expr = expr;
6913   retlist->section = section;
6914   if (gensym)
6915     retlist->ll_symbol = gen_internal_sym ("LLST");
6916
6917   return retlist;
6918 }
6919
6920 /* Add a location description expression to a location list.  */
6921
6922 static inline void
6923 add_loc_descr_to_loc_list (dw_loc_list_ref *list_head, dw_loc_descr_ref descr,
6924                            const char *begin, const char *end,
6925                            const char *section)
6926 {
6927   dw_loc_list_ref *d;
6928
6929   /* Find the end of the chain.  */
6930   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
6931     ;
6932
6933   /* Add a new location list node to the list.  */
6934   *d = new_loc_list (descr, begin, end, section, 0);
6935 }
6936
6937 /* Output the location list given to us.  */
6938
6939 static void
6940 output_loc_list (dw_loc_list_ref list_head)
6941 {
6942   dw_loc_list_ref curr = list_head;
6943
6944   ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
6945
6946   /* Walk the location list, and output each range + expression.  */
6947   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
6948     {
6949       unsigned long size;
6950       if (!separate_line_info_table_in_use && !have_switched_text_section)
6951         {
6952           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
6953                                 "Location list begin address (%s)",
6954                                 list_head->ll_symbol);
6955           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
6956                                 "Location list end address (%s)",
6957                                 list_head->ll_symbol);
6958         }
6959       else
6960         {
6961           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
6962                                "Location list begin address (%s)",
6963                                list_head->ll_symbol);
6964           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
6965                                "Location list end address (%s)",
6966                                list_head->ll_symbol);
6967         }
6968       size = size_of_locs (curr->expr);
6969
6970       /* Output the block length for this list of location operations.  */
6971       gcc_assert (size <= 0xffff);
6972       dw2_asm_output_data (2, size, "%s", "Location expression size");
6973
6974       output_loc_sequence (curr->expr);
6975     }
6976
6977   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
6978                        "Location list terminator begin (%s)",
6979                        list_head->ll_symbol);
6980   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
6981                        "Location list terminator end (%s)",
6982                        list_head->ll_symbol);
6983 }
6984
6985 /* Output the DIE and its attributes.  Called recursively to generate
6986    the definitions of each child DIE.  */
6987
6988 static void
6989 output_die (dw_die_ref die)
6990 {
6991   dw_attr_ref a;
6992   dw_die_ref c;
6993   unsigned long size;
6994
6995   /* If someone in another CU might refer to us, set up a symbol for
6996      them to point to.  */
6997   if (die->die_symbol)
6998     output_die_symbol (die);
6999
7000   dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (0x%lx) %s)",
7001                                die->die_offset, dwarf_tag_name (die->die_tag));
7002
7003   for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
7004     {
7005       const char *name = dwarf_attr_name (a->dw_attr);
7006
7007       switch (AT_class (a))
7008         {
7009         case dw_val_class_addr:
7010           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
7011           break;
7012
7013         case dw_val_class_offset:
7014           dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
7015                                "%s", name);
7016           break;
7017
7018         case dw_val_class_range_list:
7019           {
7020             char *p = strchr (ranges_section_label, '\0');
7021
7022             sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
7023                      a->dw_attr_val.v.val_offset);
7024             dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
7025                                    "%s", name);
7026             *p = '\0';
7027           }
7028           break;
7029
7030         case dw_val_class_loc:
7031           size = size_of_locs (AT_loc (a));
7032
7033           /* Output the block length for this list of location operations.  */
7034           dw2_asm_output_data (constant_size (size), size, "%s", name);
7035
7036           output_loc_sequence (AT_loc (a));
7037           break;
7038
7039         case dw_val_class_const:
7040           /* ??? It would be slightly more efficient to use a scheme like is
7041              used for unsigned constants below, but gdb 4.x does not sign
7042              extend.  Gdb 5.x does sign extend.  */
7043           dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
7044           break;
7045
7046         case dw_val_class_unsigned_const:
7047           dw2_asm_output_data (constant_size (AT_unsigned (a)),
7048                                AT_unsigned (a), "%s", name);
7049           break;
7050
7051         case dw_val_class_long_long:
7052           {
7053             unsigned HOST_WIDE_INT first, second;
7054
7055             dw2_asm_output_data (1,
7056                                  2 * HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
7057                                  "%s", name);
7058
7059             if (WORDS_BIG_ENDIAN)
7060               {
7061                 first = a->dw_attr_val.v.val_long_long.hi;
7062                 second = a->dw_attr_val.v.val_long_long.low;
7063               }
7064             else
7065               {
7066                 first = a->dw_attr_val.v.val_long_long.low;
7067                 second = a->dw_attr_val.v.val_long_long.hi;
7068               }
7069
7070             dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
7071                                  first, "long long constant");
7072             dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
7073                                  second, NULL);
7074           }
7075           break;
7076
7077         case dw_val_class_vec:
7078           {
7079             unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
7080             unsigned int len = a->dw_attr_val.v.val_vec.length;
7081             unsigned int i;
7082             unsigned char *p;
7083
7084             dw2_asm_output_data (1, len * elt_size, "%s", name);
7085             if (elt_size > sizeof (HOST_WIDE_INT))
7086               {
7087                 elt_size /= 2;
7088                 len *= 2;
7089               }
7090             for (i = 0, p = a->dw_attr_val.v.val_vec.array;
7091                  i < len;
7092                  i++, p += elt_size)
7093               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
7094                                    "fp or vector constant word %u", i);
7095             break;
7096           }
7097
7098         case dw_val_class_flag:
7099           dw2_asm_output_data (1, AT_flag (a), "%s", name);
7100           break;
7101
7102         case dw_val_class_loc_list:
7103           {
7104             char *sym = AT_loc_list (a)->ll_symbol;
7105
7106             gcc_assert (sym);
7107             dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, "%s", name);
7108           }
7109           break;
7110
7111         case dw_val_class_die_ref:
7112           if (AT_ref_external (a))
7113             {
7114               char *sym = AT_ref (a)->die_symbol;
7115
7116               gcc_assert (sym);
7117               dw2_asm_output_offset (DWARF2_ADDR_SIZE, sym, "%s", name);
7118             }
7119           else
7120             {
7121               gcc_assert (AT_ref (a)->die_offset);
7122               dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
7123                                    "%s", name);
7124             }
7125           break;
7126
7127         case dw_val_class_fde_ref:
7128           {
7129             char l1[20];
7130
7131             ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
7132                                          a->dw_attr_val.v.val_fde_index * 2);
7133             dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, "%s", name);
7134           }
7135           break;
7136
7137         case dw_val_class_lbl_id:
7138           dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
7139           break;
7140
7141         case dw_val_class_lbl_offset:
7142           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a), "%s", name);
7143           break;
7144
7145         case dw_val_class_str:
7146           if (AT_string_form (a) == DW_FORM_strp)
7147             dw2_asm_output_offset (DWARF_OFFSET_SIZE,
7148                                    a->dw_attr_val.v.val_str->label,
7149                                    "%s: \"%s\"", name, AT_string (a));
7150           else
7151             dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
7152           break;
7153
7154         default:
7155           gcc_unreachable ();
7156         }
7157     }
7158
7159   for (c = die->die_child; c != NULL; c = c->die_sib)
7160     output_die (c);
7161
7162   /* Add null byte to terminate sibling list.  */
7163   if (die->die_child != NULL)
7164     dw2_asm_output_data (1, 0, "end of children of DIE 0x%lx",
7165                          die->die_offset);
7166 }
7167
7168 /* Output the compilation unit that appears at the beginning of the
7169    .debug_info section, and precedes the DIE descriptions.  */
7170
7171 static void
7172 output_compilation_unit_header (void)
7173 {
7174   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7175     dw2_asm_output_data (4, 0xffffffff,
7176       "Initial length escape value indicating 64-bit DWARF extension");
7177   dw2_asm_output_data (DWARF_OFFSET_SIZE,
7178                        next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
7179                        "Length of Compilation Unit Info");
7180   dw2_asm_output_data (2, DWARF_VERSION, "DWARF version number");
7181   dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
7182                          "Offset Into Abbrev. Section");
7183   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
7184 }
7185
7186 /* Output the compilation unit DIE and its children.  */
7187
7188 static void
7189 output_comp_unit (dw_die_ref die, int output_if_empty)
7190 {
7191   const char *secname;
7192   char *oldsym, *tmp;
7193
7194   /* Unless we are outputting main CU, we may throw away empty ones.  */
7195   if (!output_if_empty && die->die_child == NULL)
7196     return;
7197
7198   /* Even if there are no children of this DIE, we must output the information
7199      about the compilation unit.  Otherwise, on an empty translation unit, we
7200      will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
7201      will then complain when examining the file.  First mark all the DIEs in
7202      this CU so we know which get local refs.  */
7203   mark_dies (die);
7204
7205   build_abbrev_table (die);
7206
7207   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
7208   next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
7209   calc_die_sizes (die);
7210
7211   oldsym = die->die_symbol;
7212   if (oldsym)
7213     {
7214       tmp = alloca (strlen (oldsym) + 24);
7215
7216       sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
7217       secname = tmp;
7218       die->die_symbol = NULL;
7219     }
7220   else
7221     secname = (const char *) DEBUG_INFO_SECTION;
7222
7223   /* Output debugging information.  */
7224   named_section_flags (secname, SECTION_DEBUG);
7225   output_compilation_unit_header ();
7226   output_die (die);
7227
7228   /* Leave the marks on the main CU, so we can check them in
7229      output_pubnames.  */
7230   if (oldsym)
7231     {
7232       unmark_dies (die);
7233       die->die_symbol = oldsym;
7234     }
7235 }
7236
7237 /* The DWARF2 pubname for a nested thingy looks like "A::f".  The
7238    output of lang_hooks.decl_printable_name for C++ looks like
7239    "A::f(int)".  Let's drop the argument list, and maybe the scope.  */
7240
7241 static const char *
7242 dwarf2_name (tree decl, int scope)
7243 {
7244   return lang_hooks.decl_printable_name (decl, scope ? 1 : 0);
7245 }
7246
7247 /* Add a new entry to .debug_pubnames if appropriate.  */
7248
7249 static void
7250 add_pubname (tree decl, dw_die_ref die)
7251 {
7252   pubname_ref p;
7253
7254   if (! TREE_PUBLIC (decl))
7255     return;
7256
7257   if (pubname_table_in_use == pubname_table_allocated)
7258     {
7259       pubname_table_allocated += PUBNAME_TABLE_INCREMENT;
7260       pubname_table
7261         = ggc_realloc (pubname_table,
7262                        (pubname_table_allocated * sizeof (pubname_entry)));
7263       memset (pubname_table + pubname_table_in_use, 0,
7264               PUBNAME_TABLE_INCREMENT * sizeof (pubname_entry));
7265     }
7266
7267   p = &pubname_table[pubname_table_in_use++];
7268   p->die = die;
7269   p->name = xstrdup (dwarf2_name (decl, 1));
7270 }
7271
7272 /* Output the public names table used to speed up access to externally
7273    visible names.  For now, only generate entries for externally
7274    visible procedures.  */
7275
7276 static void
7277 output_pubnames (void)
7278 {
7279   unsigned i;
7280   unsigned long pubnames_length = size_of_pubnames ();
7281
7282   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7283     dw2_asm_output_data (4, 0xffffffff,
7284       "Initial length escape value indicating 64-bit DWARF extension");
7285   dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
7286                        "Length of Public Names Info");
7287   dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
7288   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
7289                          "Offset of Compilation Unit Info");
7290   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
7291                        "Compilation Unit Length");
7292
7293   for (i = 0; i < pubname_table_in_use; i++)
7294     {
7295       pubname_ref pub = &pubname_table[i];
7296
7297       /* We shouldn't see pubnames for DIEs outside of the main CU.  */
7298       gcc_assert (pub->die->die_mark);
7299
7300       dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
7301                            "DIE offset");
7302
7303       dw2_asm_output_nstring (pub->name, -1, "external name");
7304     }
7305
7306   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
7307 }
7308
7309 /* Add a new entry to .debug_aranges if appropriate.  */
7310
7311 static void
7312 add_arange (tree decl, dw_die_ref die)
7313 {
7314   if (! DECL_SECTION_NAME (decl))
7315     return;
7316
7317   if (arange_table_in_use == arange_table_allocated)
7318     {
7319       arange_table_allocated += ARANGE_TABLE_INCREMENT;
7320       arange_table = ggc_realloc (arange_table,
7321                                   (arange_table_allocated
7322                                    * sizeof (dw_die_ref)));
7323       memset (arange_table + arange_table_in_use, 0,
7324               ARANGE_TABLE_INCREMENT * sizeof (dw_die_ref));
7325     }
7326
7327   arange_table[arange_table_in_use++] = die;
7328 }
7329
7330 /* Output the information that goes into the .debug_aranges table.
7331    Namely, define the beginning and ending address range of the
7332    text section generated for this compilation unit.  */
7333
7334 static void
7335 output_aranges (void)
7336 {
7337   unsigned i;
7338   unsigned long aranges_length = size_of_aranges ();
7339
7340   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7341     dw2_asm_output_data (4, 0xffffffff,
7342       "Initial length escape value indicating 64-bit DWARF extension");
7343   dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
7344                        "Length of Address Ranges Info");
7345   dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
7346   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
7347                          "Offset of Compilation Unit Info");
7348   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
7349   dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
7350
7351   /* We need to align to twice the pointer size here.  */
7352   if (DWARF_ARANGES_PAD_SIZE)
7353     {
7354       /* Pad using a 2 byte words so that padding is correct for any
7355          pointer size.  */
7356       dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
7357                            2 * DWARF2_ADDR_SIZE);
7358       for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
7359         dw2_asm_output_data (2, 0, NULL);
7360     }
7361
7362   dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
7363   dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
7364                         text_section_label, "Length");
7365   if (flag_reorder_blocks_and_partition)
7366     {
7367       dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label, 
7368                            "Address");
7369       dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
7370                             cold_text_section_label, "Length");
7371     }
7372
7373   for (i = 0; i < arange_table_in_use; i++)
7374     {
7375       dw_die_ref die = arange_table[i];
7376
7377       /* We shouldn't see aranges for DIEs outside of the main CU.  */
7378       gcc_assert (die->die_mark);
7379
7380       if (die->die_tag == DW_TAG_subprogram)
7381         {
7382           dw2_asm_output_addr (DWARF2_ADDR_SIZE, get_AT_low_pc (die),
7383                                "Address");
7384           dw2_asm_output_delta (DWARF2_ADDR_SIZE, get_AT_hi_pc (die),
7385                                 get_AT_low_pc (die), "Length");
7386         }
7387       else
7388         {
7389           /* A static variable; extract the symbol from DW_AT_location.
7390              Note that this code isn't currently hit, as we only emit
7391              aranges for functions (jason 9/23/99).  */
7392           dw_attr_ref a = get_AT (die, DW_AT_location);
7393           dw_loc_descr_ref loc;
7394
7395           gcc_assert (a && AT_class (a) == dw_val_class_loc);
7396
7397           loc = AT_loc (a);
7398           gcc_assert (loc->dw_loc_opc == DW_OP_addr);
7399
7400           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE,
7401                                    loc->dw_loc_oprnd1.v.val_addr, "Address");
7402           dw2_asm_output_data (DWARF2_ADDR_SIZE,
7403                                get_AT_unsigned (die, DW_AT_byte_size),
7404                                "Length");
7405         }
7406     }
7407
7408   /* Output the terminator words.  */
7409   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7410   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7411 }
7412
7413 /* Add a new entry to .debug_ranges.  Return the offset at which it
7414    was placed.  */
7415
7416 static unsigned int
7417 add_ranges (tree block)
7418 {
7419   unsigned int in_use = ranges_table_in_use;
7420
7421   if (in_use == ranges_table_allocated)
7422     {
7423       ranges_table_allocated += RANGES_TABLE_INCREMENT;
7424       ranges_table
7425         = ggc_realloc (ranges_table, (ranges_table_allocated
7426                                       * sizeof (struct dw_ranges_struct)));
7427       memset (ranges_table + ranges_table_in_use, 0,
7428               RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
7429     }
7430
7431   ranges_table[in_use].block_num = (block ? BLOCK_NUMBER (block) : 0);
7432   ranges_table_in_use = in_use + 1;
7433
7434   return in_use * 2 * DWARF2_ADDR_SIZE;
7435 }
7436
7437 static void
7438 output_ranges (void)
7439 {
7440   unsigned i;
7441   static const char *const start_fmt = "Offset 0x%x";
7442   const char *fmt = start_fmt;
7443
7444   for (i = 0; i < ranges_table_in_use; i++)
7445     {
7446       int block_num = ranges_table[i].block_num;
7447
7448       if (block_num)
7449         {
7450           char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
7451           char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
7452
7453           ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
7454           ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
7455
7456           /* If all code is in the text section, then the compilation
7457              unit base address defaults to DW_AT_low_pc, which is the
7458              base of the text section.  */
7459           if (!separate_line_info_table_in_use && !have_switched_text_section)
7460             {
7461               dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
7462                                     text_section_label,
7463                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
7464               dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
7465                                     text_section_label, NULL);
7466             }
7467
7468           /* Otherwise, we add a DW_AT_entry_pc attribute to force the
7469              compilation unit base address to zero, which allows us to
7470              use absolute addresses, and not worry about whether the
7471              target supports cross-section arithmetic.  */
7472           else
7473             {
7474               dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
7475                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
7476               dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
7477             }
7478
7479           fmt = NULL;
7480         }
7481       else
7482         {
7483           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7484           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7485           fmt = start_fmt;
7486         }
7487     }
7488 }
7489
7490 /* Data structure containing information about input files.  */
7491 struct file_info
7492 {
7493   char *path;           /* Complete file name.  */
7494   char *fname;          /* File name part.  */
7495   int length;           /* Length of entire string.  */
7496   int file_idx;         /* Index in input file table.  */
7497   int dir_idx;          /* Index in directory table.  */
7498 };
7499
7500 /* Data structure containing information about directories with source
7501    files.  */
7502 struct dir_info
7503 {
7504   char *path;           /* Path including directory name.  */
7505   int length;           /* Path length.  */
7506   int prefix;           /* Index of directory entry which is a prefix.  */
7507   int count;            /* Number of files in this directory.  */
7508   int dir_idx;          /* Index of directory used as base.  */
7509   int used;             /* Used in the end?  */
7510 };
7511
7512 /* Callback function for file_info comparison.  We sort by looking at
7513    the directories in the path.  */
7514
7515 static int
7516 file_info_cmp (const void *p1, const void *p2)
7517 {
7518   const struct file_info *s1 = p1;
7519   const struct file_info *s2 = p2;
7520   unsigned char *cp1;
7521   unsigned char *cp2;
7522
7523   /* Take care of file names without directories.  We need to make sure that
7524      we return consistent values to qsort since some will get confused if
7525      we return the same value when identical operands are passed in opposite
7526      orders.  So if neither has a directory, return 0 and otherwise return
7527      1 or -1 depending on which one has the directory.  */
7528   if ((s1->path == s1->fname || s2->path == s2->fname))
7529     return (s2->path == s2->fname) - (s1->path == s1->fname);
7530
7531   cp1 = (unsigned char *) s1->path;
7532   cp2 = (unsigned char *) s2->path;
7533
7534   while (1)
7535     {
7536       ++cp1;
7537       ++cp2;
7538       /* Reached the end of the first path?  If so, handle like above.  */
7539       if ((cp1 == (unsigned char *) s1->fname)
7540           || (cp2 == (unsigned char *) s2->fname))
7541         return ((cp2 == (unsigned char *) s2->fname)
7542                 - (cp1 == (unsigned char *) s1->fname));
7543
7544       /* Character of current path component the same?  */
7545       else if (*cp1 != *cp2)
7546         return *cp1 - *cp2;
7547     }
7548 }
7549
7550 /* Output the directory table and the file name table.  We try to minimize
7551    the total amount of memory needed.  A heuristic is used to avoid large
7552    slowdowns with many input files.  */
7553
7554 static void
7555 output_file_names (void)
7556 {
7557   struct file_info *files;
7558   struct dir_info *dirs;
7559   int *saved;
7560   int *savehere;
7561   int *backmap;
7562   size_t ndirs;
7563   int idx_offset;
7564   size_t i;
7565   int idx;
7566
7567   /* Handle the case where file_table is empty.  */
7568   if (VARRAY_ACTIVE_SIZE (file_table) <= 1)
7569     {
7570       dw2_asm_output_data (1, 0, "End directory table");
7571       dw2_asm_output_data (1, 0, "End file name table");
7572       return;
7573     }
7574
7575   /* Allocate the various arrays we need.  */
7576   files = alloca (VARRAY_ACTIVE_SIZE (file_table) * sizeof (struct file_info));
7577   dirs = alloca (VARRAY_ACTIVE_SIZE (file_table) * sizeof (struct dir_info));
7578
7579   /* Sort the file names.  */
7580   for (i = 1; i < VARRAY_ACTIVE_SIZE (file_table); i++)
7581     {
7582       char *f;
7583
7584       /* Skip all leading "./".  */
7585       f = VARRAY_CHAR_PTR (file_table, i);
7586       while (f[0] == '.' && f[1] == '/')
7587         f += 2;
7588
7589       /* Create a new array entry.  */
7590       files[i].path = f;
7591       files[i].length = strlen (f);
7592       files[i].file_idx = i;
7593
7594       /* Search for the file name part.  */
7595       f = strrchr (f, '/');
7596       files[i].fname = f == NULL ? files[i].path : f + 1;
7597     }
7598
7599   qsort (files + 1, VARRAY_ACTIVE_SIZE (file_table) - 1,
7600          sizeof (files[0]), file_info_cmp);
7601
7602   /* Find all the different directories used.  */
7603   dirs[0].path = files[1].path;
7604   dirs[0].length = files[1].fname - files[1].path;
7605   dirs[0].prefix = -1;
7606   dirs[0].count = 1;
7607   dirs[0].dir_idx = 0;
7608   dirs[0].used = 0;
7609   files[1].dir_idx = 0;
7610   ndirs = 1;
7611
7612   for (i = 2; i < VARRAY_ACTIVE_SIZE (file_table); i++)
7613     if (files[i].fname - files[i].path == dirs[ndirs - 1].length
7614         && memcmp (dirs[ndirs - 1].path, files[i].path,
7615                    dirs[ndirs - 1].length) == 0)
7616       {
7617         /* Same directory as last entry.  */
7618         files[i].dir_idx = ndirs - 1;
7619         ++dirs[ndirs - 1].count;
7620       }
7621     else
7622       {
7623         size_t j;
7624
7625         /* This is a new directory.  */
7626         dirs[ndirs].path = files[i].path;
7627         dirs[ndirs].length = files[i].fname - files[i].path;
7628         dirs[ndirs].count = 1;
7629         dirs[ndirs].dir_idx = ndirs;
7630         dirs[ndirs].used = 0;
7631         files[i].dir_idx = ndirs;
7632
7633         /* Search for a prefix.  */
7634         dirs[ndirs].prefix = -1;
7635         for (j = 0; j < ndirs; j++)
7636           if (dirs[j].length < dirs[ndirs].length
7637               && dirs[j].length > 1
7638               && (dirs[ndirs].prefix == -1
7639                   || dirs[j].length > dirs[dirs[ndirs].prefix].length)
7640               && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
7641             dirs[ndirs].prefix = j;
7642
7643         ++ndirs;
7644       }
7645
7646   /* Now to the actual work.  We have to find a subset of the directories which
7647      allow expressing the file name using references to the directory table
7648      with the least amount of characters.  We do not do an exhaustive search
7649      where we would have to check out every combination of every single
7650      possible prefix.  Instead we use a heuristic which provides nearly optimal
7651      results in most cases and never is much off.  */
7652   saved = alloca (ndirs * sizeof (int));
7653   savehere = alloca (ndirs * sizeof (int));
7654
7655   memset (saved, '\0', ndirs * sizeof (saved[0]));
7656   for (i = 0; i < ndirs; i++)
7657     {
7658       size_t j;
7659       int total;
7660
7661       /* We can always save some space for the current directory.  But this
7662          does not mean it will be enough to justify adding the directory.  */
7663       savehere[i] = dirs[i].length;
7664       total = (savehere[i] - saved[i]) * dirs[i].count;
7665
7666       for (j = i + 1; j < ndirs; j++)
7667         {
7668           savehere[j] = 0;
7669           if (saved[j] < dirs[i].length)
7670             {
7671               /* Determine whether the dirs[i] path is a prefix of the
7672                  dirs[j] path.  */
7673               int k;
7674
7675               k = dirs[j].prefix;
7676               while (k != -1 && k != (int) i)
7677                 k = dirs[k].prefix;
7678
7679               if (k == (int) i)
7680                 {
7681                   /* Yes it is.  We can possibly safe some memory but
7682                      writing the filenames in dirs[j] relative to
7683                      dirs[i].  */
7684                   savehere[j] = dirs[i].length;
7685                   total += (savehere[j] - saved[j]) * dirs[j].count;
7686                 }
7687             }
7688         }
7689
7690       /* Check whether we can safe enough to justify adding the dirs[i]
7691          directory.  */
7692       if (total > dirs[i].length + 1)
7693         {
7694           /* It's worthwhile adding.  */
7695           for (j = i; j < ndirs; j++)
7696             if (savehere[j] > 0)
7697               {
7698                 /* Remember how much we saved for this directory so far.  */
7699                 saved[j] = savehere[j];
7700
7701                 /* Remember the prefix directory.  */
7702                 dirs[j].dir_idx = i;
7703               }
7704         }
7705     }
7706
7707   /* We have to emit them in the order they appear in the file_table array
7708      since the index is used in the debug info generation.  To do this
7709      efficiently we generate a back-mapping of the indices first.  */
7710   backmap = alloca (VARRAY_ACTIVE_SIZE (file_table) * sizeof (int));
7711   for (i = 1; i < VARRAY_ACTIVE_SIZE (file_table); i++)
7712     {
7713       backmap[files[i].file_idx] = i;
7714
7715       /* Mark this directory as used.  */
7716       dirs[dirs[files[i].dir_idx].dir_idx].used = 1;
7717     }
7718
7719   /* That was it.  We are ready to emit the information.  First emit the
7720      directory name table.  We have to make sure the first actually emitted
7721      directory name has index one; zero is reserved for the current working
7722      directory.  Make sure we do not confuse these indices with the one for the
7723      constructed table (even though most of the time they are identical).  */
7724   idx = 1;
7725   idx_offset = dirs[0].length > 0 ? 1 : 0;
7726   for (i = 1 - idx_offset; i < ndirs; i++)
7727     if (dirs[i].used != 0)
7728       {
7729         dirs[i].used = idx++;
7730         dw2_asm_output_nstring (dirs[i].path, dirs[i].length - 1,
7731                                 "Directory Entry: 0x%x", dirs[i].used);
7732       }
7733
7734   dw2_asm_output_data (1, 0, "End directory table");
7735
7736   /* Correct the index for the current working directory entry if it
7737      exists.  */
7738   if (idx_offset == 0)
7739     dirs[0].used = 0;
7740
7741   /* Now write all the file names.  */
7742   for (i = 1; i < VARRAY_ACTIVE_SIZE (file_table); i++)
7743     {
7744       int file_idx = backmap[i];
7745       int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
7746
7747       dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
7748                               "File Entry: 0x%lx", (unsigned long) i);
7749
7750       /* Include directory index.  */
7751       dw2_asm_output_data_uleb128 (dirs[dir_idx].used, NULL);
7752
7753       /* Modification time.  */
7754       dw2_asm_output_data_uleb128 (0, NULL);
7755
7756       /* File length in bytes.  */
7757       dw2_asm_output_data_uleb128 (0, NULL);
7758     }
7759
7760   dw2_asm_output_data (1, 0, "End file name table");
7761 }
7762
7763
7764 /* Output the source line number correspondence information.  This
7765    information goes into the .debug_line section.  */
7766
7767 static void
7768 output_line_info (void)
7769 {
7770   char l1[20], l2[20], p1[20], p2[20];
7771   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
7772   char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
7773   unsigned opc;
7774   unsigned n_op_args;
7775   unsigned long lt_index;
7776   unsigned long current_line;
7777   long line_offset;
7778   long line_delta;
7779   unsigned long current_file;
7780   unsigned long function;
7781
7782   ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
7783   ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
7784   ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
7785   ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
7786
7787   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7788     dw2_asm_output_data (4, 0xffffffff,
7789       "Initial length escape value indicating 64-bit DWARF extension");
7790   dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
7791                         "Length of Source Line Info");
7792   ASM_OUTPUT_LABEL (asm_out_file, l1);
7793
7794   dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
7795   dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
7796   ASM_OUTPUT_LABEL (asm_out_file, p1);
7797
7798   /* Define the architecture-dependent minimum instruction length (in
7799    bytes).  In this implementation of DWARF, this field is used for
7800    information purposes only.  Since GCC generates assembly language,
7801    we have no a priori knowledge of how many instruction bytes are
7802    generated for each source line, and therefore can use only the
7803    DW_LNE_set_address and DW_LNS_fixed_advance_pc line information
7804    commands.  Accordingly, we fix this as `1', which is "correct
7805    enough" for all architectures, and don't let the target override.  */
7806   dw2_asm_output_data (1, 1,
7807                        "Minimum Instruction Length");
7808
7809   dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
7810                        "Default is_stmt_start flag");
7811   dw2_asm_output_data (1, DWARF_LINE_BASE,
7812                        "Line Base Value (Special Opcodes)");
7813   dw2_asm_output_data (1, DWARF_LINE_RANGE,
7814                        "Line Range Value (Special Opcodes)");
7815   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
7816                        "Special Opcode Base");
7817
7818   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
7819     {
7820       switch (opc)
7821         {
7822         case DW_LNS_advance_pc:
7823         case DW_LNS_advance_line:
7824         case DW_LNS_set_file:
7825         case DW_LNS_set_column:
7826         case DW_LNS_fixed_advance_pc:
7827           n_op_args = 1;
7828           break;
7829         default:
7830           n_op_args = 0;
7831           break;
7832         }
7833
7834       dw2_asm_output_data (1, n_op_args, "opcode: 0x%x has %d args",
7835                            opc, n_op_args);
7836     }
7837
7838   /* Write out the information about the files we use.  */
7839   output_file_names ();
7840   ASM_OUTPUT_LABEL (asm_out_file, p2);
7841
7842   /* We used to set the address register to the first location in the text
7843      section here, but that didn't accomplish anything since we already
7844      have a line note for the opening brace of the first function.  */
7845
7846   /* Generate the line number to PC correspondence table, encoded as
7847      a series of state machine operations.  */
7848   current_file = 1;
7849   current_line = 1;
7850
7851   if (cfun
7852       && (last_text_section == in_unlikely_executed_text
7853           || (last_text_section == in_named
7854               && last_text_section_name == cfun->unlikely_text_section_name)))
7855     strcpy (prev_line_label, cfun->cold_section_label);
7856   else
7857     strcpy (prev_line_label, text_section_label);
7858   for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
7859     {
7860       dw_line_info_ref line_info = &line_info_table[lt_index];
7861
7862 #if 0
7863       /* Disable this optimization for now; GDB wants to see two line notes
7864          at the beginning of a function so it can find the end of the
7865          prologue.  */
7866
7867       /* Don't emit anything for redundant notes.  Just updating the
7868          address doesn't accomplish anything, because we already assume
7869          that anything after the last address is this line.  */
7870       if (line_info->dw_line_num == current_line
7871           && line_info->dw_file_num == current_file)
7872         continue;
7873 #endif
7874
7875       /* Emit debug info for the address of the current line.
7876
7877          Unfortunately, we have little choice here currently, and must always
7878          use the most general form.  GCC does not know the address delta
7879          itself, so we can't use DW_LNS_advance_pc.  Many ports do have length
7880          attributes which will give an upper bound on the address range.  We
7881          could perhaps use length attributes to determine when it is safe to
7882          use DW_LNS_fixed_advance_pc.  */
7883
7884       ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
7885       if (0)
7886         {
7887           /* This can handle deltas up to 0xffff.  This takes 3 bytes.  */
7888           dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
7889                                "DW_LNS_fixed_advance_pc");
7890           dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
7891         }
7892       else
7893         {
7894           /* This can handle any delta.  This takes
7895              4+DWARF2_ADDR_SIZE bytes.  */
7896           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
7897           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
7898           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
7899           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
7900         }
7901
7902       strcpy (prev_line_label, line_label);
7903
7904       /* Emit debug info for the source file of the current line, if
7905          different from the previous line.  */
7906       if (line_info->dw_file_num != current_file)
7907         {
7908           current_file = line_info->dw_file_num;
7909           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
7910           dw2_asm_output_data_uleb128 (current_file, "(\"%s\")",
7911                                        VARRAY_CHAR_PTR (file_table,
7912                                                         current_file));
7913         }
7914
7915       /* Emit debug info for the current line number, choosing the encoding
7916          that uses the least amount of space.  */
7917       if (line_info->dw_line_num != current_line)
7918         {
7919           line_offset = line_info->dw_line_num - current_line;
7920           line_delta = line_offset - DWARF_LINE_BASE;
7921           current_line = line_info->dw_line_num;
7922           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
7923             /* This can handle deltas from -10 to 234, using the current
7924                definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.  This
7925                takes 1 byte.  */
7926             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
7927                                  "line %lu", current_line);
7928           else
7929             {
7930               /* This can handle any delta.  This takes at least 4 bytes,
7931                  depending on the value being encoded.  */
7932               dw2_asm_output_data (1, DW_LNS_advance_line,
7933                                    "advance to line %lu", current_line);
7934               dw2_asm_output_data_sleb128 (line_offset, NULL);
7935               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
7936             }
7937         }
7938       else
7939         /* We still need to start a new row, so output a copy insn.  */
7940         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
7941     }
7942
7943   /* Emit debug info for the address of the end of the function.  */
7944   if (0)
7945     {
7946       dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
7947                            "DW_LNS_fixed_advance_pc");
7948       dw2_asm_output_delta (2, text_end_label, prev_line_label, NULL);
7949     }
7950   else
7951     {
7952       dw2_asm_output_data (1, 0, "DW_LNE_set_address");
7953       dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
7954       dw2_asm_output_data (1, DW_LNE_set_address, NULL);
7955       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_end_label, NULL);
7956     }
7957
7958   dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
7959   dw2_asm_output_data_uleb128 (1, NULL);
7960   dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
7961
7962   function = 0;
7963   current_file = 1;
7964   current_line = 1;
7965   for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
7966     {
7967       dw_separate_line_info_ref line_info
7968         = &separate_line_info_table[lt_index];
7969
7970 #if 0
7971       /* Don't emit anything for redundant notes.  */
7972       if (line_info->dw_line_num == current_line
7973           && line_info->dw_file_num == current_file
7974           && line_info->function == function)
7975         goto cont;
7976 #endif
7977
7978       /* Emit debug info for the address of the current line.  If this is
7979          a new function, or the first line of a function, then we need
7980          to handle it differently.  */
7981       ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
7982                                    lt_index);
7983       if (function != line_info->function)
7984         {
7985           function = line_info->function;
7986
7987           /* Set the address register to the first line in the function.  */
7988           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
7989           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
7990           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
7991           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
7992         }
7993       else
7994         {
7995           /* ??? See the DW_LNS_advance_pc comment above.  */
7996           if (0)
7997             {
7998               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
7999                                    "DW_LNS_fixed_advance_pc");
8000               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
8001             }
8002           else
8003             {
8004               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8005               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8006               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8007               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
8008             }
8009         }
8010
8011       strcpy (prev_line_label, line_label);
8012
8013       /* Emit debug info for the source file of the current line, if
8014          different from the previous line.  */
8015       if (line_info->dw_file_num != current_file)
8016         {
8017           current_file = line_info->dw_file_num;
8018           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
8019           dw2_asm_output_data_uleb128 (current_file, "(\"%s\")",
8020                                        VARRAY_CHAR_PTR (file_table,
8021                                                         current_file));
8022         }
8023
8024       /* Emit debug info for the current line number, choosing the encoding
8025          that uses the least amount of space.  */
8026       if (line_info->dw_line_num != current_line)
8027         {
8028           line_offset = line_info->dw_line_num - current_line;
8029           line_delta = line_offset - DWARF_LINE_BASE;
8030           current_line = line_info->dw_line_num;
8031           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
8032             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
8033                                  "line %lu", current_line);
8034           else
8035             {
8036               dw2_asm_output_data (1, DW_LNS_advance_line,
8037                                    "advance to line %lu", current_line);
8038               dw2_asm_output_data_sleb128 (line_offset, NULL);
8039               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
8040             }
8041         }
8042       else
8043         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
8044
8045 #if 0
8046     cont:
8047 #endif
8048
8049       lt_index++;
8050
8051       /* If we're done with a function, end its sequence.  */
8052       if (lt_index == separate_line_info_table_in_use
8053           || separate_line_info_table[lt_index].function != function)
8054         {
8055           current_file = 1;
8056           current_line = 1;
8057
8058           /* Emit debug info for the address of the end of the function.  */
8059           ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
8060           if (0)
8061             {
8062               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
8063                                    "DW_LNS_fixed_advance_pc");
8064               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
8065             }
8066           else
8067             {
8068               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8069               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8070               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8071               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
8072             }
8073
8074           /* Output the marker for the end of this sequence.  */
8075           dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
8076           dw2_asm_output_data_uleb128 (1, NULL);
8077           dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
8078         }
8079     }
8080
8081   /* Output the marker for the end of the line number info.  */
8082   ASM_OUTPUT_LABEL (asm_out_file, l2);
8083 }
8084 \f
8085 /* Given a pointer to a tree node for some base type, return a pointer to
8086    a DIE that describes the given type.
8087
8088    This routine must only be called for GCC type nodes that correspond to
8089    Dwarf base (fundamental) types.  */
8090
8091 static dw_die_ref
8092 base_type_die (tree type)
8093 {
8094   dw_die_ref base_type_result;
8095   const char *type_name;
8096   enum dwarf_type encoding;
8097   tree name = TYPE_NAME (type);
8098
8099   if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
8100     return 0;
8101
8102   if (name)
8103     {
8104       if (TREE_CODE (name) == TYPE_DECL)
8105         name = DECL_NAME (name);
8106
8107       type_name = IDENTIFIER_POINTER (name);
8108     }
8109   else
8110     type_name = "__unknown__";
8111
8112   switch (TREE_CODE (type))
8113     {
8114     case INTEGER_TYPE:
8115       /* Carefully distinguish the C character types, without messing
8116          up if the language is not C. Note that we check only for the names
8117          that contain spaces; other names might occur by coincidence in other
8118          languages.  */
8119       if (! (TYPE_PRECISION (type) == CHAR_TYPE_SIZE
8120              && (TYPE_MAIN_VARIANT (type) == char_type_node
8121                  || ! strcmp (type_name, "signed char")
8122                  || ! strcmp (type_name, "unsigned char"))))
8123         {
8124           if (TYPE_UNSIGNED (type))
8125             encoding = DW_ATE_unsigned;
8126           else
8127             encoding = DW_ATE_signed;
8128           break;
8129         }
8130       /* else fall through.  */
8131
8132     case CHAR_TYPE:
8133       /* GNU Pascal/Ada CHAR type.  Not used in C.  */
8134       if (TYPE_UNSIGNED (type))
8135         encoding = DW_ATE_unsigned_char;
8136       else
8137         encoding = DW_ATE_signed_char;
8138       break;
8139
8140     case REAL_TYPE:
8141       encoding = DW_ATE_float;
8142       break;
8143
8144       /* Dwarf2 doesn't know anything about complex ints, so use
8145          a user defined type for it.  */
8146     case COMPLEX_TYPE:
8147       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
8148         encoding = DW_ATE_complex_float;
8149       else
8150         encoding = DW_ATE_lo_user;
8151       break;
8152
8153     case BOOLEAN_TYPE:
8154       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
8155       encoding = DW_ATE_boolean;
8156       break;
8157
8158     default:
8159       /* No other TREE_CODEs are Dwarf fundamental types.  */
8160       gcc_unreachable ();
8161     }
8162
8163   base_type_result = new_die (DW_TAG_base_type, comp_unit_die, type);
8164   if (demangle_name_func)
8165     type_name = (*demangle_name_func) (type_name);
8166
8167   add_AT_string (base_type_result, DW_AT_name, type_name);
8168   add_AT_unsigned (base_type_result, DW_AT_byte_size,
8169                    int_size_in_bytes (type));
8170   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
8171
8172   return base_type_result;
8173 }
8174
8175 /* Given a pointer to an arbitrary ..._TYPE tree node, return a pointer to
8176    the Dwarf "root" type for the given input type.  The Dwarf "root" type of
8177    a given type is generally the same as the given type, except that if the
8178    given type is a pointer or reference type, then the root type of the given
8179    type is the root type of the "basis" type for the pointer or reference
8180    type.  (This definition of the "root" type is recursive.) Also, the root
8181    type of a `const' qualified type or a `volatile' qualified type is the
8182    root type of the given type without the qualifiers.  */
8183
8184 static tree
8185 root_type (tree type)
8186 {
8187   if (TREE_CODE (type) == ERROR_MARK)
8188     return error_mark_node;
8189
8190   switch (TREE_CODE (type))
8191     {
8192     case ERROR_MARK:
8193       return error_mark_node;
8194
8195     case POINTER_TYPE:
8196     case REFERENCE_TYPE:
8197       return type_main_variant (root_type (TREE_TYPE (type)));
8198
8199     default:
8200       return type_main_variant (type);
8201     }
8202 }
8203
8204 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
8205    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
8206
8207 static inline int
8208 is_base_type (tree type)
8209 {
8210   switch (TREE_CODE (type))
8211     {
8212     case ERROR_MARK:
8213     case VOID_TYPE:
8214     case INTEGER_TYPE:
8215     case REAL_TYPE:
8216     case COMPLEX_TYPE:
8217     case BOOLEAN_TYPE:
8218     case CHAR_TYPE:
8219       return 1;
8220
8221     case ARRAY_TYPE:
8222     case RECORD_TYPE:
8223     case UNION_TYPE:
8224     case QUAL_UNION_TYPE:
8225     case ENUMERAL_TYPE:
8226     case FUNCTION_TYPE:
8227     case METHOD_TYPE:
8228     case POINTER_TYPE:
8229     case REFERENCE_TYPE:
8230     case OFFSET_TYPE:
8231     case LANG_TYPE:
8232     case VECTOR_TYPE:
8233       return 0;
8234
8235     default:
8236       gcc_unreachable ();
8237     }
8238
8239   return 0;
8240 }
8241
8242 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
8243    node, return the size in bits for the type if it is a constant, or else
8244    return the alignment for the type if the type's size is not constant, or
8245    else return BITS_PER_WORD if the type actually turns out to be an
8246    ERROR_MARK node.  */
8247
8248 static inline unsigned HOST_WIDE_INT
8249 simple_type_size_in_bits (tree type)
8250 {
8251   if (TREE_CODE (type) == ERROR_MARK)
8252     return BITS_PER_WORD;
8253   else if (TYPE_SIZE (type) == NULL_TREE)
8254     return 0;
8255   else if (host_integerp (TYPE_SIZE (type), 1))
8256     return tree_low_cst (TYPE_SIZE (type), 1);
8257   else
8258     return TYPE_ALIGN (type);
8259 }
8260
8261 /* Return true if the debug information for the given type should be
8262    emitted as a subrange type.  */
8263
8264 static inline bool
8265 is_subrange_type (tree type)
8266 {
8267   tree subtype = TREE_TYPE (type);
8268
8269   /* Subrange types are identified by the fact that they are integer
8270      types, and that they have a subtype which is either an integer type
8271      or an enumeral type.  */
8272
8273   if (TREE_CODE (type) != INTEGER_TYPE
8274       || subtype == NULL_TREE)
8275     return false;
8276
8277   if (TREE_CODE (subtype) != INTEGER_TYPE
8278       && TREE_CODE (subtype) != ENUMERAL_TYPE)
8279     return false;
8280
8281   if (TREE_CODE (type) == TREE_CODE (subtype)
8282       && int_size_in_bytes (type) == int_size_in_bytes (subtype)
8283       && TYPE_MIN_VALUE (type) != NULL
8284       && TYPE_MIN_VALUE (subtype) != NULL
8285       && tree_int_cst_equal (TYPE_MIN_VALUE (type), TYPE_MIN_VALUE (subtype))
8286       && TYPE_MAX_VALUE (type) != NULL
8287       && TYPE_MAX_VALUE (subtype) != NULL
8288       && tree_int_cst_equal (TYPE_MAX_VALUE (type), TYPE_MAX_VALUE (subtype)))
8289     {
8290       /* The type and its subtype have the same representation.  If in
8291          addition the two types also have the same name, then the given
8292          type is not a subrange type, but rather a plain base type.  */
8293       /* FIXME: brobecker/2004-03-22:
8294          Sizetype INTEGER_CSTs nodes are canonicalized.  It should
8295          therefore be sufficient to check the TYPE_SIZE node pointers
8296          rather than checking the actual size.  Unfortunately, we have
8297          found some cases, such as in the Ada "integer" type, where
8298          this is not the case.  Until this problem is solved, we need to
8299          keep checking the actual size.  */
8300       tree type_name = TYPE_NAME (type);
8301       tree subtype_name = TYPE_NAME (subtype);
8302
8303       if (type_name != NULL && TREE_CODE (type_name) == TYPE_DECL)
8304         type_name = DECL_NAME (type_name);
8305
8306       if (subtype_name != NULL && TREE_CODE (subtype_name) == TYPE_DECL)
8307         subtype_name = DECL_NAME (subtype_name);
8308
8309       if (type_name == subtype_name)
8310         return false;
8311     }
8312
8313   return true;
8314 }
8315
8316 /*  Given a pointer to a tree node for a subrange type, return a pointer
8317     to a DIE that describes the given type.  */
8318
8319 static dw_die_ref
8320 subrange_type_die (tree type, dw_die_ref context_die)
8321 {
8322   dw_die_ref subtype_die;
8323   dw_die_ref subrange_die;
8324   tree name = TYPE_NAME (type);
8325   const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
8326   tree subtype = TREE_TYPE (type);
8327
8328   if (context_die == NULL)
8329     context_die = comp_unit_die;
8330
8331   if (TREE_CODE (subtype) == ENUMERAL_TYPE)
8332     subtype_die = gen_enumeration_type_die (subtype, context_die);
8333   else
8334     subtype_die = base_type_die (subtype);
8335
8336   subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
8337
8338   if (name != NULL)
8339     {
8340       if (TREE_CODE (name) == TYPE_DECL)
8341         name = DECL_NAME (name);
8342       add_name_attribute (subrange_die, IDENTIFIER_POINTER (name));
8343     }
8344
8345   if (int_size_in_bytes (subtype) != size_in_bytes)
8346     {
8347       /* The size of the subrange type and its base type do not match,
8348          so we need to generate a size attribute for the subrange type.  */
8349       add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
8350     }
8351
8352   if (TYPE_MIN_VALUE (type) != NULL)
8353     add_bound_info (subrange_die, DW_AT_lower_bound,
8354                     TYPE_MIN_VALUE (type));
8355   if (TYPE_MAX_VALUE (type) != NULL)
8356     add_bound_info (subrange_die, DW_AT_upper_bound,
8357                     TYPE_MAX_VALUE (type));
8358   add_AT_die_ref (subrange_die, DW_AT_type, subtype_die);
8359
8360   return subrange_die;
8361 }
8362
8363 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
8364    entry that chains various modifiers in front of the given type.  */
8365
8366 static dw_die_ref
8367 modified_type_die (tree type, int is_const_type, int is_volatile_type,
8368                    dw_die_ref context_die)
8369 {
8370   enum tree_code code = TREE_CODE (type);
8371   dw_die_ref mod_type_die = NULL;
8372   dw_die_ref sub_die = NULL;
8373   tree item_type = NULL;
8374
8375   if (code != ERROR_MARK)
8376     {
8377       tree qualified_type;
8378
8379       /* See if we already have the appropriately qualified variant of
8380          this type.  */
8381       qualified_type
8382         = get_qualified_type (type,
8383                               ((is_const_type ? TYPE_QUAL_CONST : 0)
8384                                | (is_volatile_type
8385                                   ? TYPE_QUAL_VOLATILE : 0)));
8386
8387       /* If we do, then we can just use its DIE, if it exists.  */
8388       if (qualified_type)
8389         {
8390           mod_type_die = lookup_type_die (qualified_type);
8391           if (mod_type_die)
8392             return mod_type_die;
8393         }
8394
8395       /* Handle C typedef types.  */
8396       if (qualified_type && TYPE_NAME (qualified_type)
8397           && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL
8398           && DECL_ORIGINAL_TYPE (TYPE_NAME (qualified_type)))
8399         {
8400           tree type_name = TYPE_NAME (qualified_type);
8401           tree dtype = TREE_TYPE (type_name);
8402
8403           if (qualified_type == dtype)
8404             {
8405               /* For a named type, use the typedef.  */
8406               gen_type_die (qualified_type, context_die);
8407               mod_type_die = lookup_type_die (qualified_type);
8408             }
8409           else if (is_const_type < TYPE_READONLY (dtype)
8410                    || is_volatile_type < TYPE_VOLATILE (dtype))
8411             /* cv-unqualified version of named type.  Just use the unnamed
8412                type to which it refers.  */
8413             mod_type_die
8414               = modified_type_die (DECL_ORIGINAL_TYPE (type_name),
8415                                    is_const_type, is_volatile_type,
8416                                    context_die);
8417
8418           /* Else cv-qualified version of named type; fall through.  */
8419         }
8420
8421       if (mod_type_die)
8422         /* OK.  */
8423         ;
8424       else if (is_const_type)
8425         {
8426           mod_type_die = new_die (DW_TAG_const_type, comp_unit_die, type);
8427           sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
8428         }
8429       else if (is_volatile_type)
8430         {
8431           mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die, type);
8432           sub_die = modified_type_die (type, 0, 0, context_die);
8433         }
8434       else if (code == POINTER_TYPE)
8435         {
8436           mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die, type);
8437           add_AT_unsigned (mod_type_die, DW_AT_byte_size,
8438                            simple_type_size_in_bits (type) / BITS_PER_UNIT);
8439 #if 0
8440           add_AT_unsigned (mod_type_die, DW_AT_address_class, 0);
8441 #endif
8442           item_type = TREE_TYPE (type);
8443         }
8444       else if (code == REFERENCE_TYPE)
8445         {
8446           mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die, type);
8447           add_AT_unsigned (mod_type_die, DW_AT_byte_size,
8448                            simple_type_size_in_bits (type) / BITS_PER_UNIT);
8449 #if 0
8450           add_AT_unsigned (mod_type_die, DW_AT_address_class, 0);
8451 #endif
8452           item_type = TREE_TYPE (type);
8453         }
8454       else if (is_subrange_type (type))
8455         mod_type_die = subrange_type_die (type, context_die);
8456       else if (is_base_type (type))
8457         mod_type_die = base_type_die (type);
8458       else
8459         {
8460           gen_type_die (type, context_die);
8461
8462           /* We have to get the type_main_variant here (and pass that to the
8463              `lookup_type_die' routine) because the ..._TYPE node we have
8464              might simply be a *copy* of some original type node (where the
8465              copy was created to help us keep track of typedef names) and
8466              that copy might have a different TYPE_UID from the original
8467              ..._TYPE node.  */
8468           if (TREE_CODE (type) != VECTOR_TYPE)
8469             mod_type_die = lookup_type_die (type_main_variant (type));
8470           else
8471             /* Vectors have the debugging information in the type,
8472                not the main variant.  */
8473             mod_type_die = lookup_type_die (type);
8474           gcc_assert (mod_type_die);
8475         }
8476
8477       /* We want to equate the qualified type to the die below.  */
8478       type = qualified_type;
8479     }
8480
8481   if (type)
8482     equate_type_number_to_die (type, mod_type_die);
8483   if (item_type)
8484     /* We must do this after the equate_type_number_to_die call, in case
8485        this is a recursive type.  This ensures that the modified_type_die
8486        recursion will terminate even if the type is recursive.  Recursive
8487        types are possible in Ada.  */
8488     sub_die = modified_type_die (item_type,
8489                                  TYPE_READONLY (item_type),
8490                                  TYPE_VOLATILE (item_type),
8491                                  context_die);
8492
8493   if (sub_die != NULL)
8494     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
8495
8496   return mod_type_die;
8497 }
8498
8499 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
8500    an enumerated type.  */
8501
8502 static inline int
8503 type_is_enum (tree type)
8504 {
8505   return TREE_CODE (type) == ENUMERAL_TYPE;
8506 }
8507
8508 /* Return the DBX register number described by a given RTL node.  */
8509
8510 static unsigned int
8511 dbx_reg_number (rtx rtl)
8512 {
8513   unsigned regno = REGNO (rtl);
8514
8515   gcc_assert (regno < FIRST_PSEUDO_REGISTER);
8516
8517 #ifdef LEAF_REG_REMAP
8518   if (current_function_uses_only_leaf_regs)
8519     {
8520       int leaf_reg = LEAF_REG_REMAP (regno);
8521       if (leaf_reg != -1)
8522         regno = (unsigned) leaf_reg;
8523     }
8524 #endif
8525
8526   return DBX_REGISTER_NUMBER (regno);
8527 }
8528
8529 /* Optionally add a DW_OP_piece term to a location description expression.
8530    DW_OP_piece is only added if the location description expression already
8531    doesn't end with DW_OP_piece.  */
8532
8533 static void
8534 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
8535 {
8536   dw_loc_descr_ref loc;
8537
8538   if (*list_head != NULL)
8539     {
8540       /* Find the end of the chain.  */
8541       for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
8542         ;
8543
8544       if (loc->dw_loc_opc != DW_OP_piece)
8545         loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
8546     }
8547 }
8548
8549 /* Return a location descriptor that designates a machine register or
8550    zero if there is none.  */
8551
8552 static dw_loc_descr_ref
8553 reg_loc_descriptor (rtx rtl)
8554 {
8555   rtx regs;
8556
8557   if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
8558     return 0;
8559
8560   regs = targetm.dwarf_register_span (rtl);
8561
8562   if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
8563     return multiple_reg_loc_descriptor (rtl, regs);
8564   else
8565     return one_reg_loc_descriptor (dbx_reg_number (rtl));
8566 }
8567
8568 /* Return a location descriptor that designates a machine register for
8569    a given hard register number.  */
8570
8571 static dw_loc_descr_ref
8572 one_reg_loc_descriptor (unsigned int regno)
8573 {
8574   if (regno <= 31)
8575     return new_loc_descr (DW_OP_reg0 + regno, 0, 0);
8576   else
8577     return new_loc_descr (DW_OP_regx, regno, 0);
8578 }
8579
8580 /* Given an RTL of a register, return a location descriptor that
8581    designates a value that spans more than one register.  */
8582
8583 static dw_loc_descr_ref
8584 multiple_reg_loc_descriptor (rtx rtl, rtx regs)
8585 {
8586   int nregs, size, i;
8587   unsigned reg;
8588   dw_loc_descr_ref loc_result = NULL;
8589
8590   reg = REGNO (rtl);
8591 #ifdef LEAF_REG_REMAP
8592   if (current_function_uses_only_leaf_regs)
8593     {
8594       int leaf_reg = LEAF_REG_REMAP (reg);
8595       if (leaf_reg != -1)
8596         reg = (unsigned) leaf_reg;
8597     }
8598 #endif
8599   gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
8600   nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
8601
8602   /* Simple, contiguous registers.  */
8603   if (regs == NULL_RTX)
8604     {
8605       size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
8606
8607       loc_result = NULL;
8608       while (nregs--)
8609         {
8610           dw_loc_descr_ref t;
8611
8612           t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg));
8613           add_loc_descr (&loc_result, t);
8614           add_loc_descr_op_piece (&loc_result, size);
8615           ++reg;
8616         }
8617       return loc_result;
8618     }
8619
8620   /* Now onto stupid register sets in non contiguous locations.  */
8621
8622   gcc_assert (GET_CODE (regs) == PARALLEL);
8623
8624   size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
8625   loc_result = NULL;
8626
8627   for (i = 0; i < XVECLEN (regs, 0); ++i)
8628     {
8629       dw_loc_descr_ref t;
8630
8631       t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)));
8632       add_loc_descr (&loc_result, t);
8633       size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
8634       add_loc_descr_op_piece (&loc_result, size);
8635     }
8636   return loc_result;
8637 }
8638
8639 /* Return a location descriptor that designates a constant.  */
8640
8641 static dw_loc_descr_ref
8642 int_loc_descriptor (HOST_WIDE_INT i)
8643 {
8644   enum dwarf_location_atom op;
8645
8646   /* Pick the smallest representation of a constant, rather than just
8647      defaulting to the LEB encoding.  */
8648   if (i >= 0)
8649     {
8650       if (i <= 31)
8651         op = DW_OP_lit0 + i;
8652       else if (i <= 0xff)
8653         op = DW_OP_const1u;
8654       else if (i <= 0xffff)
8655         op = DW_OP_const2u;
8656       else if (HOST_BITS_PER_WIDE_INT == 32
8657                || i <= 0xffffffff)
8658         op = DW_OP_const4u;
8659       else
8660         op = DW_OP_constu;
8661     }
8662   else
8663     {
8664       if (i >= -0x80)
8665         op = DW_OP_const1s;
8666       else if (i >= -0x8000)
8667         op = DW_OP_const2s;
8668       else if (HOST_BITS_PER_WIDE_INT == 32
8669                || i >= -0x80000000)
8670         op = DW_OP_const4s;
8671       else
8672         op = DW_OP_consts;
8673     }
8674
8675   return new_loc_descr (op, i, 0);
8676 }
8677
8678 /* Return a location descriptor that designates a base+offset location.  */
8679
8680 static dw_loc_descr_ref
8681 based_loc_descr (rtx reg, HOST_WIDE_INT offset)
8682 {
8683   unsigned int regno;
8684
8685   /* We only use "frame base" when we're sure we're talking about the
8686      post-prologue local stack frame.  We do this by *not* running
8687      register elimination until this point, and recognizing the special
8688      argument pointer and soft frame pointer rtx's.  */
8689   if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
8690     {
8691       rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
8692
8693       if (elim != reg)
8694         {
8695           if (GET_CODE (elim) == PLUS)
8696             {
8697               offset += INTVAL (XEXP (elim, 1));
8698               elim = XEXP (elim, 0);
8699             }
8700           gcc_assert (elim == (frame_pointer_needed ? hard_frame_pointer_rtx
8701                       : stack_pointer_rtx));
8702           offset += frame_pointer_cfa_offset;
8703
8704           return new_loc_descr (DW_OP_fbreg, offset, 0);
8705         }
8706     }
8707
8708   regno = dbx_reg_number (reg);
8709   if (regno <= 31)
8710     return new_loc_descr (DW_OP_breg0 + regno, offset, 0);
8711   else
8712     return new_loc_descr (DW_OP_bregx, regno, offset);
8713 }
8714
8715 /* Return true if this RTL expression describes a base+offset calculation.  */
8716
8717 static inline int
8718 is_based_loc (rtx rtl)
8719 {
8720   return (GET_CODE (rtl) == PLUS
8721           && ((REG_P (XEXP (rtl, 0))
8722                && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
8723                && GET_CODE (XEXP (rtl, 1)) == CONST_INT)));
8724 }
8725
8726 /* The following routine converts the RTL for a variable or parameter
8727    (resident in memory) into an equivalent Dwarf representation of a
8728    mechanism for getting the address of that same variable onto the top of a
8729    hypothetical "address evaluation" stack.
8730
8731    When creating memory location descriptors, we are effectively transforming
8732    the RTL for a memory-resident object into its Dwarf postfix expression
8733    equivalent.  This routine recursively descends an RTL tree, turning
8734    it into Dwarf postfix code as it goes.
8735
8736    MODE is the mode of the memory reference, needed to handle some
8737    autoincrement addressing modes.
8738
8739    CAN_USE_FBREG is a flag whether we can use DW_AT_frame_base in the
8740    location list for RTL.
8741
8742    Return 0 if we can't represent the location.  */
8743
8744 static dw_loc_descr_ref
8745 mem_loc_descriptor (rtx rtl, enum machine_mode mode)
8746 {
8747   dw_loc_descr_ref mem_loc_result = NULL;
8748   enum dwarf_location_atom op;
8749
8750   /* Note that for a dynamically sized array, the location we will generate a
8751      description of here will be the lowest numbered location which is
8752      actually within the array.  That's *not* necessarily the same as the
8753      zeroth element of the array.  */
8754
8755   rtl = targetm.delegitimize_address (rtl);
8756
8757   switch (GET_CODE (rtl))
8758     {
8759     case POST_INC:
8760     case POST_DEC:
8761     case POST_MODIFY:
8762       /* POST_INC and POST_DEC can be handled just like a SUBREG.  So we
8763          just fall into the SUBREG code.  */
8764
8765       /* ... fall through ...  */
8766
8767     case SUBREG:
8768       /* The case of a subreg may arise when we have a local (register)
8769          variable or a formal (register) parameter which doesn't quite fill
8770          up an entire register.  For now, just assume that it is
8771          legitimate to make the Dwarf info refer to the whole register which
8772          contains the given subreg.  */
8773       rtl = XEXP (rtl, 0);
8774
8775       /* ... fall through ...  */
8776
8777     case REG:
8778       /* Whenever a register number forms a part of the description of the
8779          method for calculating the (dynamic) address of a memory resident
8780          object, DWARF rules require the register number be referred to as
8781          a "base register".  This distinction is not based in any way upon
8782          what category of register the hardware believes the given register
8783          belongs to.  This is strictly DWARF terminology we're dealing with
8784          here. Note that in cases where the location of a memory-resident
8785          data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
8786          OP_CONST (0)) the actual DWARF location descriptor that we generate
8787          may just be OP_BASEREG (basereg).  This may look deceptively like
8788          the object in question was allocated to a register (rather than in
8789          memory) so DWARF consumers need to be aware of the subtle
8790          distinction between OP_REG and OP_BASEREG.  */
8791       if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
8792         mem_loc_result = based_loc_descr (rtl, 0);
8793       break;
8794
8795     case MEM:
8796       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl));
8797       if (mem_loc_result != 0)
8798         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
8799       break;
8800
8801     case LO_SUM:
8802          rtl = XEXP (rtl, 1);
8803
8804       /* ... fall through ...  */
8805
8806     case LABEL_REF:
8807       /* Some ports can transform a symbol ref into a label ref, because
8808          the symbol ref is too far away and has to be dumped into a constant
8809          pool.  */
8810     case CONST:
8811     case SYMBOL_REF:
8812       /* Alternatively, the symbol in the constant pool might be referenced
8813          by a different symbol.  */
8814       if (GET_CODE (rtl) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (rtl))
8815         {
8816           bool marked;
8817           rtx tmp = get_pool_constant_mark (rtl, &marked);
8818
8819           if (GET_CODE (tmp) == SYMBOL_REF)
8820             {
8821               rtl = tmp;
8822               if (CONSTANT_POOL_ADDRESS_P (tmp))
8823                 get_pool_constant_mark (tmp, &marked);
8824               else
8825                 marked = true;
8826             }
8827
8828           /* If all references to this pool constant were optimized away,
8829              it was not output and thus we can't represent it.
8830              FIXME: might try to use DW_OP_const_value here, though
8831              DW_OP_piece complicates it.  */
8832           if (!marked)
8833             return 0;
8834         }
8835
8836       mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
8837       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
8838       mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
8839       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
8840       break;
8841
8842     case PRE_MODIFY:
8843       /* Extract the PLUS expression nested inside and fall into
8844          PLUS code below.  */
8845       rtl = XEXP (rtl, 1);
8846       goto plus;
8847
8848     case PRE_INC:
8849     case PRE_DEC:
8850       /* Turn these into a PLUS expression and fall into the PLUS code
8851          below.  */
8852       rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
8853                           GEN_INT (GET_CODE (rtl) == PRE_INC
8854                                    ? GET_MODE_UNIT_SIZE (mode)
8855                                    : -GET_MODE_UNIT_SIZE (mode)));
8856
8857       /* ... fall through ...  */
8858
8859     case PLUS:
8860     plus:
8861       if (is_based_loc (rtl))
8862         mem_loc_result = based_loc_descr (XEXP (rtl, 0),
8863                                           INTVAL (XEXP (rtl, 1)));
8864       else
8865         {
8866           mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode);
8867           if (mem_loc_result == 0)
8868             break;
8869
8870           if (GET_CODE (XEXP (rtl, 1)) == CONST_INT
8871               && INTVAL (XEXP (rtl, 1)) >= 0)
8872             add_loc_descr (&mem_loc_result,
8873                            new_loc_descr (DW_OP_plus_uconst,
8874                                           INTVAL (XEXP (rtl, 1)), 0));
8875           else
8876             {
8877               add_loc_descr (&mem_loc_result,
8878                              mem_loc_descriptor (XEXP (rtl, 1), mode));
8879               add_loc_descr (&mem_loc_result,
8880                              new_loc_descr (DW_OP_plus, 0, 0));
8881             }
8882         }
8883       break;
8884
8885     /* If a pseudo-reg is optimized away, it is possible for it to
8886        be replaced with a MEM containing a multiply or shift.  */
8887     case MULT:
8888       op = DW_OP_mul;
8889       goto do_binop;
8890
8891     case ASHIFT:
8892       op = DW_OP_shl;
8893       goto do_binop;
8894
8895     case ASHIFTRT:
8896       op = DW_OP_shra;
8897       goto do_binop;
8898
8899     case LSHIFTRT:
8900       op = DW_OP_shr;
8901       goto do_binop;
8902
8903     do_binop:
8904       {
8905         dw_loc_descr_ref op0 = mem_loc_descriptor (XEXP (rtl, 0), mode);
8906         dw_loc_descr_ref op1 = mem_loc_descriptor (XEXP (rtl, 1), mode);
8907
8908         if (op0 == 0 || op1 == 0)
8909           break;
8910
8911         mem_loc_result = op0;
8912         add_loc_descr (&mem_loc_result, op1);
8913         add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
8914         break;
8915       }
8916
8917     case CONST_INT:
8918       mem_loc_result = int_loc_descriptor (INTVAL (rtl));
8919       break;
8920
8921     default:
8922       gcc_unreachable ();
8923     }
8924
8925   return mem_loc_result;
8926 }
8927
8928 /* Return a descriptor that describes the concatenation of two locations.
8929    This is typically a complex variable.  */
8930
8931 static dw_loc_descr_ref
8932 concat_loc_descriptor (rtx x0, rtx x1)
8933 {
8934   dw_loc_descr_ref cc_loc_result = NULL;
8935   dw_loc_descr_ref x0_ref = loc_descriptor (x0);
8936   dw_loc_descr_ref x1_ref = loc_descriptor (x1);
8937
8938   if (x0_ref == 0 || x1_ref == 0)
8939     return 0;
8940
8941   cc_loc_result = x0_ref;
8942   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
8943
8944   add_loc_descr (&cc_loc_result, x1_ref);
8945   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
8946
8947   return cc_loc_result;
8948 }
8949
8950 /* Output a proper Dwarf location descriptor for a variable or parameter
8951    which is either allocated in a register or in a memory location.  For a
8952    register, we just generate an OP_REG and the register number.  For a
8953    memory location we provide a Dwarf postfix expression describing how to
8954    generate the (dynamic) address of the object onto the address stack.
8955
8956    If we don't know how to describe it, return 0.  */
8957
8958 static dw_loc_descr_ref
8959 loc_descriptor (rtx rtl)
8960 {
8961   dw_loc_descr_ref loc_result = NULL;
8962
8963   switch (GET_CODE (rtl))
8964     {
8965     case SUBREG:
8966       /* The case of a subreg may arise when we have a local (register)
8967          variable or a formal (register) parameter which doesn't quite fill
8968          up an entire register.  For now, just assume that it is
8969          legitimate to make the Dwarf info refer to the whole register which
8970          contains the given subreg.  */
8971       rtl = SUBREG_REG (rtl);
8972
8973       /* ... fall through ...  */
8974
8975     case REG:
8976       loc_result = reg_loc_descriptor (rtl);
8977       break;
8978
8979     case MEM:
8980       loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl));
8981       break;
8982
8983     case CONCAT:
8984       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1));
8985       break;
8986
8987     case VAR_LOCATION:
8988       /* Single part.  */
8989       if (GET_CODE (XEXP (rtl, 1)) != PARALLEL)
8990         {
8991           loc_result = loc_descriptor (XEXP (XEXP (rtl, 1), 0));
8992           break;
8993         }
8994
8995       rtl = XEXP (rtl, 1);
8996       /* FALLTHRU */
8997
8998     case PARALLEL:
8999       {
9000         rtvec par_elems = XVEC (rtl, 0);
9001         int num_elem = GET_NUM_ELEM (par_elems);
9002         enum machine_mode mode;
9003         int i;
9004
9005         /* Create the first one, so we have something to add to.  */
9006         loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0));
9007         mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
9008         add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
9009         for (i = 1; i < num_elem; i++)
9010           {
9011             dw_loc_descr_ref temp;
9012
9013             temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0));
9014             add_loc_descr (&loc_result, temp);
9015             mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
9016             add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
9017           }
9018       }
9019       break;
9020
9021     default:
9022       gcc_unreachable ();
9023     }
9024
9025   return loc_result;
9026 }
9027
9028 /* Similar, but generate the descriptor from trees instead of rtl.  This comes
9029    up particularly with variable length arrays.  WANT_ADDRESS is 2 if this is
9030    a top-level invocation of loc_descriptor_from_tree; is 1 if this is not a
9031    top-level invocation, and we require the address of LOC; is 0 if we require
9032    the value of LOC.  */
9033
9034 static dw_loc_descr_ref
9035 loc_descriptor_from_tree_1 (tree loc, int want_address)
9036 {
9037   dw_loc_descr_ref ret, ret1;
9038   int have_address = 0;
9039   int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
9040   enum dwarf_location_atom op;
9041
9042   /* ??? Most of the time we do not take proper care for sign/zero
9043      extending the values properly.  Hopefully this won't be a real
9044      problem...  */
9045
9046   switch (TREE_CODE (loc))
9047     {
9048     case ERROR_MARK:
9049       return 0;
9050
9051     case PLACEHOLDER_EXPR:
9052       /* This case involves extracting fields from an object to determine the
9053          position of other fields.  We don't try to encode this here.  The
9054          only user of this is Ada, which encodes the needed information using
9055          the names of types.  */
9056       return 0;
9057
9058     case CALL_EXPR:
9059       return 0;
9060
9061     case PREINCREMENT_EXPR:
9062     case PREDECREMENT_EXPR:
9063     case POSTINCREMENT_EXPR:
9064     case POSTDECREMENT_EXPR:
9065       /* There are no opcodes for these operations.  */
9066       return 0;
9067
9068     case ADDR_EXPR:
9069       /* If we already want an address, there's nothing we can do.  */
9070       if (want_address)
9071         return 0;
9072
9073       /* Otherwise, process the argument and look for the address.  */
9074       return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 1);
9075
9076     case VAR_DECL:
9077       if (DECL_THREAD_LOCAL_P (loc))
9078         {
9079           rtx rtl;
9080
9081           /* If this is not defined, we have no way to emit the data.  */
9082           if (!targetm.asm_out.output_dwarf_dtprel)
9083             return 0;
9084
9085           /* The way DW_OP_GNU_push_tls_address is specified, we can only
9086              look up addresses of objects in the current module.  */
9087           if (DECL_EXTERNAL (loc))
9088             return 0;
9089
9090           rtl = rtl_for_decl_location (loc);
9091           if (rtl == NULL_RTX)
9092             return 0;
9093
9094           if (!MEM_P (rtl))
9095             return 0;
9096           rtl = XEXP (rtl, 0);
9097           if (! CONSTANT_P (rtl))
9098             return 0;
9099
9100           ret = new_loc_descr (INTERNAL_DW_OP_tls_addr, 0, 0);
9101           ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
9102           ret->dw_loc_oprnd1.v.val_addr = rtl;
9103
9104           ret1 = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
9105           add_loc_descr (&ret, ret1);
9106
9107           have_address = 1;
9108           break;
9109         }
9110       /* FALLTHRU */
9111
9112     case PARM_DECL:
9113       if (DECL_HAS_VALUE_EXPR_P (loc))
9114         return loc_descriptor_from_tree_1 (DECL_VALUE_EXPR (loc),
9115                                            want_address);
9116       /* FALLTHRU */
9117
9118     case RESULT_DECL:
9119     case FUNCTION_DECL:
9120       {
9121         rtx rtl = rtl_for_decl_location (loc);
9122
9123         if (rtl == NULL_RTX)
9124           return 0;
9125         else if (GET_CODE (rtl) == CONST_INT)
9126           {
9127             HOST_WIDE_INT val = INTVAL (rtl);
9128             if (TYPE_UNSIGNED (TREE_TYPE (loc)))
9129               val &= GET_MODE_MASK (DECL_MODE (loc));
9130             ret = int_loc_descriptor (val);
9131           }
9132         else if (GET_CODE (rtl) == CONST_STRING)
9133           return 0;
9134         else if (CONSTANT_P (rtl))
9135           {
9136             ret = new_loc_descr (DW_OP_addr, 0, 0);
9137             ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
9138             ret->dw_loc_oprnd1.v.val_addr = rtl;
9139           }
9140         else
9141           {
9142             enum machine_mode mode;
9143
9144             /* Certain constructs can only be represented at top-level.  */
9145             if (want_address == 2)
9146               return loc_descriptor (rtl);
9147
9148             mode = GET_MODE (rtl);
9149             if (MEM_P (rtl))
9150               {
9151                 rtl = XEXP (rtl, 0);
9152                 have_address = 1;
9153               }
9154             ret = mem_loc_descriptor (rtl, mode);
9155           }
9156       }
9157       break;
9158
9159     case INDIRECT_REF:
9160       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9161       have_address = 1;
9162       break;
9163
9164     case COMPOUND_EXPR:
9165       return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), want_address);
9166
9167     case NOP_EXPR:
9168     case CONVERT_EXPR:
9169     case NON_LVALUE_EXPR:
9170     case VIEW_CONVERT_EXPR:
9171     case SAVE_EXPR:
9172     case MODIFY_EXPR:
9173       return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), want_address);
9174
9175     case COMPONENT_REF:
9176     case BIT_FIELD_REF:
9177     case ARRAY_REF:
9178     case ARRAY_RANGE_REF:
9179       {
9180         tree obj, offset;
9181         HOST_WIDE_INT bitsize, bitpos, bytepos;
9182         enum machine_mode mode;
9183         int volatilep;
9184
9185         obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
9186                                    &unsignedp, &volatilep, false);
9187
9188         if (obj == loc)
9189           return 0;
9190
9191         ret = loc_descriptor_from_tree_1 (obj, 1);
9192         if (ret == 0
9193             || bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
9194           return 0;
9195
9196         if (offset != NULL_TREE)
9197           {
9198             /* Variable offset.  */
9199             add_loc_descr (&ret, loc_descriptor_from_tree_1 (offset, 0));
9200             add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
9201           }
9202
9203         bytepos = bitpos / BITS_PER_UNIT;
9204         if (bytepos > 0)
9205           add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
9206         else if (bytepos < 0)
9207           {
9208             add_loc_descr (&ret, int_loc_descriptor (bytepos));
9209             add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
9210           }
9211
9212         have_address = 1;
9213         break;
9214       }
9215
9216     case INTEGER_CST:
9217       if (host_integerp (loc, 0))
9218         ret = int_loc_descriptor (tree_low_cst (loc, 0));
9219       else
9220         return 0;
9221       break;
9222
9223     case CONSTRUCTOR:
9224       {
9225         /* Get an RTL for this, if something has been emitted.  */
9226         rtx rtl = lookup_constant_def (loc);
9227         enum machine_mode mode;
9228
9229         if (!rtl || !MEM_P (rtl))
9230           return 0;
9231         mode = GET_MODE (rtl);
9232         rtl = XEXP (rtl, 0);
9233         ret = mem_loc_descriptor (rtl, mode);
9234         have_address = 1;
9235         break;
9236       }
9237
9238     case TRUTH_AND_EXPR:
9239     case TRUTH_ANDIF_EXPR:
9240     case BIT_AND_EXPR:
9241       op = DW_OP_and;
9242       goto do_binop;
9243
9244     case TRUTH_XOR_EXPR:
9245     case BIT_XOR_EXPR:
9246       op = DW_OP_xor;
9247       goto do_binop;
9248
9249     case TRUTH_OR_EXPR:
9250     case TRUTH_ORIF_EXPR:
9251     case BIT_IOR_EXPR:
9252       op = DW_OP_or;
9253       goto do_binop;
9254
9255     case FLOOR_DIV_EXPR:
9256     case CEIL_DIV_EXPR:
9257     case ROUND_DIV_EXPR:
9258     case TRUNC_DIV_EXPR:
9259       op = DW_OP_div;
9260       goto do_binop;
9261
9262     case MINUS_EXPR:
9263       op = DW_OP_minus;
9264       goto do_binop;
9265
9266     case FLOOR_MOD_EXPR:
9267     case CEIL_MOD_EXPR:
9268     case ROUND_MOD_EXPR:
9269     case TRUNC_MOD_EXPR:
9270       op = DW_OP_mod;
9271       goto do_binop;
9272
9273     case MULT_EXPR:
9274       op = DW_OP_mul;
9275       goto do_binop;
9276
9277     case LSHIFT_EXPR:
9278       op = DW_OP_shl;
9279       goto do_binop;
9280
9281     case RSHIFT_EXPR:
9282       op = (unsignedp ? DW_OP_shr : DW_OP_shra);
9283       goto do_binop;
9284
9285     case PLUS_EXPR:
9286       if (TREE_CODE (TREE_OPERAND (loc, 1)) == INTEGER_CST
9287           && host_integerp (TREE_OPERAND (loc, 1), 0))
9288         {
9289           ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9290           if (ret == 0)
9291             return 0;
9292
9293           add_loc_descr (&ret,
9294                          new_loc_descr (DW_OP_plus_uconst,
9295                                         tree_low_cst (TREE_OPERAND (loc, 1),
9296                                                       0),
9297                                         0));
9298           break;
9299         }
9300
9301       op = DW_OP_plus;
9302       goto do_binop;
9303
9304     case LE_EXPR:
9305       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9306         return 0;
9307
9308       op = DW_OP_le;
9309       goto do_binop;
9310
9311     case GE_EXPR:
9312       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9313         return 0;
9314
9315       op = DW_OP_ge;
9316       goto do_binop;
9317
9318     case LT_EXPR:
9319       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9320         return 0;
9321
9322       op = DW_OP_lt;
9323       goto do_binop;
9324
9325     case GT_EXPR:
9326       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9327         return 0;
9328
9329       op = DW_OP_gt;
9330       goto do_binop;
9331
9332     case EQ_EXPR:
9333       op = DW_OP_eq;
9334       goto do_binop;
9335
9336     case NE_EXPR:
9337       op = DW_OP_ne;
9338       goto do_binop;
9339
9340     do_binop:
9341       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9342       ret1 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
9343       if (ret == 0 || ret1 == 0)
9344         return 0;
9345
9346       add_loc_descr (&ret, ret1);
9347       add_loc_descr (&ret, new_loc_descr (op, 0, 0));
9348       break;
9349
9350     case TRUTH_NOT_EXPR:
9351     case BIT_NOT_EXPR:
9352       op = DW_OP_not;
9353       goto do_unop;
9354
9355     case ABS_EXPR:
9356       op = DW_OP_abs;
9357       goto do_unop;
9358
9359     case NEGATE_EXPR:
9360       op = DW_OP_neg;
9361       goto do_unop;
9362
9363     do_unop:
9364       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9365       if (ret == 0)
9366         return 0;
9367
9368       add_loc_descr (&ret, new_loc_descr (op, 0, 0));
9369       break;
9370
9371     case MIN_EXPR:
9372     case MAX_EXPR:
9373       {
9374         const enum tree_code code =
9375           TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
9376
9377         loc = build3 (COND_EXPR, TREE_TYPE (loc),
9378                       build2 (code, integer_type_node,
9379                               TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
9380                       TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
9381       }
9382
9383       /* ... fall through ...  */
9384
9385     case COND_EXPR:
9386       {
9387         dw_loc_descr_ref lhs
9388           = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
9389         dw_loc_descr_ref rhs
9390           = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 2), 0);
9391         dw_loc_descr_ref bra_node, jump_node, tmp;
9392
9393         ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9394         if (ret == 0 || lhs == 0 || rhs == 0)
9395           return 0;
9396
9397         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
9398         add_loc_descr (&ret, bra_node);
9399
9400         add_loc_descr (&ret, rhs);
9401         jump_node = new_loc_descr (DW_OP_skip, 0, 0);
9402         add_loc_descr (&ret, jump_node);
9403
9404         add_loc_descr (&ret, lhs);
9405         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
9406         bra_node->dw_loc_oprnd1.v.val_loc = lhs;
9407
9408         /* ??? Need a node to point the skip at.  Use a nop.  */
9409         tmp = new_loc_descr (DW_OP_nop, 0, 0);
9410         add_loc_descr (&ret, tmp);
9411         jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
9412         jump_node->dw_loc_oprnd1.v.val_loc = tmp;
9413       }
9414       break;
9415
9416     case FIX_TRUNC_EXPR:
9417     case FIX_CEIL_EXPR:
9418     case FIX_FLOOR_EXPR:
9419     case FIX_ROUND_EXPR:
9420       return 0;
9421
9422     default:
9423       /* Leave front-end specific codes as simply unknown.  This comes
9424          up, for instance, with the C STMT_EXPR.  */
9425       if ((unsigned int) TREE_CODE (loc)
9426           >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
9427         return 0;
9428
9429 #ifdef ENABLE_CHECKING
9430       /* Otherwise this is a generic code; we should just lists all of
9431          these explicitly.  We forgot one.  */
9432       gcc_unreachable ();
9433 #else
9434       /* In a release build, we want to degrade gracefully: better to
9435          generate incomplete debugging information than to crash.  */
9436       return NULL;
9437 #endif
9438     }
9439
9440   /* Show if we can't fill the request for an address.  */
9441   if (want_address && !have_address)
9442     return 0;
9443
9444   /* If we've got an address and don't want one, dereference.  */
9445   if (!want_address && have_address && ret)
9446     {
9447       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
9448
9449       if (size > DWARF2_ADDR_SIZE || size == -1)
9450         return 0;
9451       else if (size == DWARF2_ADDR_SIZE)
9452         op = DW_OP_deref;
9453       else
9454         op = DW_OP_deref_size;
9455
9456       add_loc_descr (&ret, new_loc_descr (op, size, 0));
9457     }
9458
9459   return ret;
9460 }
9461
9462 static inline dw_loc_descr_ref
9463 loc_descriptor_from_tree (tree loc)
9464 {
9465   return loc_descriptor_from_tree_1 (loc, 2);
9466 }
9467
9468 /* Given a value, round it up to the lowest multiple of `boundary'
9469    which is not less than the value itself.  */
9470
9471 static inline HOST_WIDE_INT
9472 ceiling (HOST_WIDE_INT value, unsigned int boundary)
9473 {
9474   return (((value + boundary - 1) / boundary) * boundary);
9475 }
9476
9477 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
9478    pointer to the declared type for the relevant field variable, or return
9479    `integer_type_node' if the given node turns out to be an
9480    ERROR_MARK node.  */
9481
9482 static inline tree
9483 field_type (tree decl)
9484 {
9485   tree type;
9486
9487   if (TREE_CODE (decl) == ERROR_MARK)
9488     return integer_type_node;
9489
9490   type = DECL_BIT_FIELD_TYPE (decl);
9491   if (type == NULL_TREE)
9492     type = TREE_TYPE (decl);
9493
9494   return type;
9495 }
9496
9497 /* Given a pointer to a tree node, return the alignment in bits for
9498    it, or else return BITS_PER_WORD if the node actually turns out to
9499    be an ERROR_MARK node.  */
9500
9501 static inline unsigned
9502 simple_type_align_in_bits (tree type)
9503 {
9504   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
9505 }
9506
9507 static inline unsigned
9508 simple_decl_align_in_bits (tree decl)
9509 {
9510   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
9511 }
9512
9513 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
9514    lowest addressed byte of the "containing object" for the given FIELD_DECL,
9515    or return 0 if we are unable to determine what that offset is, either
9516    because the argument turns out to be a pointer to an ERROR_MARK node, or
9517    because the offset is actually variable.  (We can't handle the latter case
9518    just yet).  */
9519
9520 static HOST_WIDE_INT
9521 field_byte_offset (tree decl)
9522 {
9523   unsigned int type_align_in_bits;
9524   unsigned int decl_align_in_bits;
9525   unsigned HOST_WIDE_INT type_size_in_bits;
9526   HOST_WIDE_INT object_offset_in_bits;
9527   tree type;
9528   tree field_size_tree;
9529   HOST_WIDE_INT bitpos_int;
9530   HOST_WIDE_INT deepest_bitpos;
9531   unsigned HOST_WIDE_INT field_size_in_bits;
9532
9533   if (TREE_CODE (decl) == ERROR_MARK)
9534     return 0;
9535
9536   gcc_assert (TREE_CODE (decl) == FIELD_DECL);
9537
9538   type = field_type (decl);
9539   field_size_tree = DECL_SIZE (decl);
9540
9541   /* The size could be unspecified if there was an error, or for
9542      a flexible array member.  */
9543   if (! field_size_tree)
9544     field_size_tree = bitsize_zero_node;
9545
9546   /* We cannot yet cope with fields whose positions are variable, so
9547      for now, when we see such things, we simply return 0.  Someday, we may
9548      be able to handle such cases, but it will be damn difficult.  */
9549   if (! host_integerp (bit_position (decl), 0))
9550     return 0;
9551
9552   bitpos_int = int_bit_position (decl);
9553
9554   /* If we don't know the size of the field, pretend it's a full word.  */
9555   if (host_integerp (field_size_tree, 1))
9556     field_size_in_bits = tree_low_cst (field_size_tree, 1);
9557   else
9558     field_size_in_bits = BITS_PER_WORD;
9559
9560   type_size_in_bits = simple_type_size_in_bits (type);
9561   type_align_in_bits = simple_type_align_in_bits (type);
9562   decl_align_in_bits = simple_decl_align_in_bits (decl);
9563
9564   /* The GCC front-end doesn't make any attempt to keep track of the starting
9565      bit offset (relative to the start of the containing structure type) of the
9566      hypothetical "containing object" for a bit-field.  Thus, when computing
9567      the byte offset value for the start of the "containing object" of a
9568      bit-field, we must deduce this information on our own. This can be rather
9569      tricky to do in some cases.  For example, handling the following structure
9570      type definition when compiling for an i386/i486 target (which only aligns
9571      long long's to 32-bit boundaries) can be very tricky:
9572
9573          struct S { int field1; long long field2:31; };
9574
9575      Fortunately, there is a simple rule-of-thumb which can be used in such
9576      cases.  When compiling for an i386/i486, GCC will allocate 8 bytes for the
9577      structure shown above.  It decides to do this based upon one simple rule
9578      for bit-field allocation.  GCC allocates each "containing object" for each
9579      bit-field at the first (i.e. lowest addressed) legitimate alignment
9580      boundary (based upon the required minimum alignment for the declared type
9581      of the field) which it can possibly use, subject to the condition that
9582      there is still enough available space remaining in the containing object
9583      (when allocated at the selected point) to fully accommodate all of the
9584      bits of the bit-field itself.
9585
9586      This simple rule makes it obvious why GCC allocates 8 bytes for each
9587      object of the structure type shown above.  When looking for a place to
9588      allocate the "containing object" for `field2', the compiler simply tries
9589      to allocate a 64-bit "containing object" at each successive 32-bit
9590      boundary (starting at zero) until it finds a place to allocate that 64-
9591      bit field such that at least 31 contiguous (and previously unallocated)
9592      bits remain within that selected 64 bit field.  (As it turns out, for the
9593      example above, the compiler finds it is OK to allocate the "containing
9594      object" 64-bit field at bit-offset zero within the structure type.)
9595
9596      Here we attempt to work backwards from the limited set of facts we're
9597      given, and we try to deduce from those facts, where GCC must have believed
9598      that the containing object started (within the structure type). The value
9599      we deduce is then used (by the callers of this routine) to generate
9600      DW_AT_location and DW_AT_bit_offset attributes for fields (both bit-fields
9601      and, in the case of DW_AT_location, regular fields as well).  */
9602
9603   /* Figure out the bit-distance from the start of the structure to the
9604      "deepest" bit of the bit-field.  */
9605   deepest_bitpos = bitpos_int + field_size_in_bits;
9606
9607   /* This is the tricky part.  Use some fancy footwork to deduce where the
9608      lowest addressed bit of the containing object must be.  */
9609   object_offset_in_bits = deepest_bitpos - type_size_in_bits;
9610
9611   /* Round up to type_align by default.  This works best for bitfields.  */
9612   object_offset_in_bits += type_align_in_bits - 1;
9613   object_offset_in_bits /= type_align_in_bits;
9614   object_offset_in_bits *= type_align_in_bits;
9615
9616   if (object_offset_in_bits > bitpos_int)
9617     {
9618       /* Sigh, the decl must be packed.  */
9619       object_offset_in_bits = deepest_bitpos - type_size_in_bits;
9620
9621       /* Round up to decl_align instead.  */
9622       object_offset_in_bits += decl_align_in_bits - 1;
9623       object_offset_in_bits /= decl_align_in_bits;
9624       object_offset_in_bits *= decl_align_in_bits;
9625     }
9626
9627   return object_offset_in_bits / BITS_PER_UNIT;
9628 }
9629 \f
9630 /* The following routines define various Dwarf attributes and any data
9631    associated with them.  */
9632
9633 /* Add a location description attribute value to a DIE.
9634
9635    This emits location attributes suitable for whole variables and
9636    whole parameters.  Note that the location attributes for struct fields are
9637    generated by the routine `data_member_location_attribute' below.  */
9638
9639 static inline void
9640 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
9641                              dw_loc_descr_ref descr)
9642 {
9643   if (descr != 0)
9644     add_AT_loc (die, attr_kind, descr);
9645 }
9646
9647 /* Attach the specialized form of location attribute used for data members of
9648    struct and union types.  In the special case of a FIELD_DECL node which
9649    represents a bit-field, the "offset" part of this special location
9650    descriptor must indicate the distance in bytes from the lowest-addressed
9651    byte of the containing struct or union type to the lowest-addressed byte of
9652    the "containing object" for the bit-field.  (See the `field_byte_offset'
9653    function above).
9654
9655    For any given bit-field, the "containing object" is a hypothetical object
9656    (of some integral or enum type) within which the given bit-field lives.  The
9657    type of this hypothetical "containing object" is always the same as the
9658    declared type of the individual bit-field itself (for GCC anyway... the
9659    DWARF spec doesn't actually mandate this).  Note that it is the size (in
9660    bytes) of the hypothetical "containing object" which will be given in the
9661    DW_AT_byte_size attribute for this bit-field.  (See the
9662    `byte_size_attribute' function below.)  It is also used when calculating the
9663    value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
9664    function below.)  */
9665
9666 static void
9667 add_data_member_location_attribute (dw_die_ref die, tree decl)
9668 {
9669   HOST_WIDE_INT offset;
9670   dw_loc_descr_ref loc_descr = 0;
9671
9672   if (TREE_CODE (decl) == TREE_BINFO)
9673     {
9674       /* We're working on the TAG_inheritance for a base class.  */
9675       if (BINFO_VIRTUAL_P (decl) && is_cxx ())
9676         {
9677           /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
9678              aren't at a fixed offset from all (sub)objects of the same
9679              type.  We need to extract the appropriate offset from our
9680              vtable.  The following dwarf expression means
9681
9682                BaseAddr = ObAddr + *((*ObAddr) - Offset)
9683
9684              This is specific to the V3 ABI, of course.  */
9685
9686           dw_loc_descr_ref tmp;
9687
9688           /* Make a copy of the object address.  */
9689           tmp = new_loc_descr (DW_OP_dup, 0, 0);
9690           add_loc_descr (&loc_descr, tmp);
9691
9692           /* Extract the vtable address.  */
9693           tmp = new_loc_descr (DW_OP_deref, 0, 0);
9694           add_loc_descr (&loc_descr, tmp);
9695
9696           /* Calculate the address of the offset.  */
9697           offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
9698           gcc_assert (offset < 0);
9699
9700           tmp = int_loc_descriptor (-offset);
9701           add_loc_descr (&loc_descr, tmp);
9702           tmp = new_loc_descr (DW_OP_minus, 0, 0);
9703           add_loc_descr (&loc_descr, tmp);
9704
9705           /* Extract the offset.  */
9706           tmp = new_loc_descr (DW_OP_deref, 0, 0);
9707           add_loc_descr (&loc_descr, tmp);
9708
9709           /* Add it to the object address.  */
9710           tmp = new_loc_descr (DW_OP_plus, 0, 0);
9711           add_loc_descr (&loc_descr, tmp);
9712         }
9713       else
9714         offset = tree_low_cst (BINFO_OFFSET (decl), 0);
9715     }
9716   else
9717     offset = field_byte_offset (decl);
9718
9719   if (! loc_descr)
9720     {
9721       enum dwarf_location_atom op;
9722
9723       /* The DWARF2 standard says that we should assume that the structure
9724          address is already on the stack, so we can specify a structure field
9725          address by using DW_OP_plus_uconst.  */
9726
9727 #ifdef MIPS_DEBUGGING_INFO
9728       /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
9729          operator correctly.  It works only if we leave the offset on the
9730          stack.  */
9731       op = DW_OP_constu;
9732 #else
9733       op = DW_OP_plus_uconst;
9734 #endif
9735
9736       loc_descr = new_loc_descr (op, offset, 0);
9737     }
9738
9739   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
9740 }
9741
9742 /* Writes integer values to dw_vec_const array.  */
9743
9744 static void
9745 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
9746 {
9747   while (size != 0)
9748     {
9749       *dest++ = val & 0xff;
9750       val >>= 8;
9751       --size;
9752     }
9753 }
9754
9755 /* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
9756
9757 static HOST_WIDE_INT
9758 extract_int (const unsigned char *src, unsigned int size)
9759 {
9760   HOST_WIDE_INT val = 0;
9761
9762   src += size;
9763   while (size != 0)
9764     {
9765       val <<= 8;
9766       val |= *--src & 0xff;
9767       --size;
9768     }
9769   return val;
9770 }
9771
9772 /* Writes floating point values to dw_vec_const array.  */
9773
9774 static void
9775 insert_float (rtx rtl, unsigned char *array)
9776 {
9777   REAL_VALUE_TYPE rv;
9778   long val[4];
9779   int i;
9780
9781   REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
9782   real_to_target (val, &rv, GET_MODE (rtl));
9783
9784   /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
9785   for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
9786     {
9787       insert_int (val[i], 4, array);
9788       array += 4;
9789     }
9790 }
9791
9792 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
9793    does not have a "location" either in memory or in a register.  These
9794    things can arise in GNU C when a constant is passed as an actual parameter
9795    to an inlined function.  They can also arise in C++ where declared
9796    constants do not necessarily get memory "homes".  */
9797
9798 static void
9799 add_const_value_attribute (dw_die_ref die, rtx rtl)
9800 {
9801   switch (GET_CODE (rtl))
9802     {
9803     case CONST_INT:
9804       {
9805         HOST_WIDE_INT val = INTVAL (rtl);
9806
9807         if (val < 0)
9808           add_AT_int (die, DW_AT_const_value, val);
9809         else
9810           add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
9811       }
9812       break;
9813
9814     case CONST_DOUBLE:
9815       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
9816          floating-point constant.  A CONST_DOUBLE is used whenever the
9817          constant requires more than one word in order to be adequately
9818          represented.  We output CONST_DOUBLEs as blocks.  */
9819       {
9820         enum machine_mode mode = GET_MODE (rtl);
9821
9822         if (GET_MODE_CLASS (mode) == MODE_FLOAT)
9823           {
9824             unsigned int length = GET_MODE_SIZE (mode);
9825             unsigned char *array = ggc_alloc (length);
9826
9827             insert_float (rtl, array);
9828             add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
9829           }
9830         else
9831           {
9832             /* ??? We really should be using HOST_WIDE_INT throughout.  */
9833             gcc_assert (HOST_BITS_PER_LONG == HOST_BITS_PER_WIDE_INT);
9834
9835             add_AT_long_long (die, DW_AT_const_value,
9836                               CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
9837           }
9838       }
9839       break;
9840
9841     case CONST_VECTOR:
9842       {
9843         enum machine_mode mode = GET_MODE (rtl);
9844         unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
9845         unsigned int length = CONST_VECTOR_NUNITS (rtl);
9846         unsigned char *array = ggc_alloc (length * elt_size);
9847         unsigned int i;
9848         unsigned char *p;
9849
9850         switch (GET_MODE_CLASS (mode))
9851           {
9852           case MODE_VECTOR_INT:
9853             for (i = 0, p = array; i < length; i++, p += elt_size)
9854               {
9855                 rtx elt = CONST_VECTOR_ELT (rtl, i);
9856                 HOST_WIDE_INT lo, hi;
9857
9858                 switch (GET_CODE (elt))
9859                   {
9860                   case CONST_INT:
9861                     lo = INTVAL (elt);
9862                     hi = -(lo < 0);
9863                     break;
9864
9865                   case CONST_DOUBLE:
9866                     lo = CONST_DOUBLE_LOW (elt);
9867                     hi = CONST_DOUBLE_HIGH (elt);
9868                     break;
9869
9870                   default:
9871                     gcc_unreachable ();
9872                   }
9873
9874                 if (elt_size <= sizeof (HOST_WIDE_INT))
9875                   insert_int (lo, elt_size, p);
9876                 else
9877                   {
9878                     unsigned char *p0 = p;
9879                     unsigned char *p1 = p + sizeof (HOST_WIDE_INT);
9880
9881                     gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
9882                     if (WORDS_BIG_ENDIAN)
9883                       {
9884                         p0 = p1;
9885                         p1 = p;
9886                       }
9887                     insert_int (lo, sizeof (HOST_WIDE_INT), p0);
9888                     insert_int (hi, sizeof (HOST_WIDE_INT), p1);
9889                   }
9890               }
9891             break;
9892
9893           case MODE_VECTOR_FLOAT:
9894             for (i = 0, p = array; i < length; i++, p += elt_size)
9895               {
9896                 rtx elt = CONST_VECTOR_ELT (rtl, i);
9897                 insert_float (elt, p);
9898               }
9899             break;
9900
9901           default:
9902             gcc_unreachable ();
9903           }
9904
9905         add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
9906       }
9907       break;
9908
9909     case CONST_STRING:
9910       add_AT_string (die, DW_AT_const_value, XSTR (rtl, 0));
9911       break;
9912
9913     case SYMBOL_REF:
9914     case LABEL_REF:
9915     case CONST:
9916       add_AT_addr (die, DW_AT_const_value, rtl);
9917       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
9918       break;
9919
9920     case PLUS:
9921       /* In cases where an inlined instance of an inline function is passed
9922          the address of an `auto' variable (which is local to the caller) we
9923          can get a situation where the DECL_RTL of the artificial local
9924          variable (for the inlining) which acts as a stand-in for the
9925          corresponding formal parameter (of the inline function) will look
9926          like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
9927          exactly a compile-time constant expression, but it isn't the address
9928          of the (artificial) local variable either.  Rather, it represents the
9929          *value* which the artificial local variable always has during its
9930          lifetime.  We currently have no way to represent such quasi-constant
9931          values in Dwarf, so for now we just punt and generate nothing.  */
9932       break;
9933
9934     default:
9935       /* No other kinds of rtx should be possible here.  */
9936       gcc_unreachable ();
9937     }
9938
9939 }
9940
9941 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
9942    for use in a later add_const_value_attribute call.  */
9943
9944 static rtx
9945 rtl_for_decl_init (tree init, tree type)
9946 {
9947   rtx rtl = NULL_RTX;
9948
9949   /* If a variable is initialized with a string constant without embedded
9950      zeros, build CONST_STRING.  */
9951   if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
9952     {
9953       tree enttype = TREE_TYPE (type);
9954       tree domain = TYPE_DOMAIN (type);
9955       enum machine_mode mode = TYPE_MODE (enttype);
9956
9957       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
9958           && domain
9959           && integer_zerop (TYPE_MIN_VALUE (domain))
9960           && compare_tree_int (TYPE_MAX_VALUE (domain),
9961                                TREE_STRING_LENGTH (init) - 1) == 0
9962           && ((size_t) TREE_STRING_LENGTH (init)
9963               == strlen (TREE_STRING_POINTER (init)) + 1))
9964         rtl = gen_rtx_CONST_STRING (VOIDmode,
9965                                     ggc_strdup (TREE_STRING_POINTER (init)));
9966     }
9967   /* If the initializer is something that we know will expand into an
9968      immediate RTL constant, expand it now.  Expanding anything else
9969      tends to produce unresolved symbols; see debug/5770 and c++/6381.  */
9970   /* Aggregate, vector, and complex types may contain constructors that may
9971      result in code being generated when expand_expr is called, so we can't
9972      handle them here.  Integer and float are useful and safe types to handle
9973      here.  */
9974   else if ((INTEGRAL_TYPE_P (type) || SCALAR_FLOAT_TYPE_P (type))
9975            && initializer_constant_valid_p (init, type) == null_pointer_node)
9976     {
9977       rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
9978
9979       /* If expand_expr returns a MEM, it wasn't immediate.  */
9980       gcc_assert (!rtl || !MEM_P (rtl));
9981     }
9982
9983   return rtl;
9984 }
9985
9986 /* Generate RTL for the variable DECL to represent its location.  */
9987
9988 static rtx
9989 rtl_for_decl_location (tree decl)
9990 {
9991   rtx rtl;
9992
9993   /* Here we have to decide where we are going to say the parameter "lives"
9994      (as far as the debugger is concerned).  We only have a couple of
9995      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
9996
9997      DECL_RTL normally indicates where the parameter lives during most of the
9998      activation of the function.  If optimization is enabled however, this
9999      could be either NULL or else a pseudo-reg.  Both of those cases indicate
10000      that the parameter doesn't really live anywhere (as far as the code
10001      generation parts of GCC are concerned) during most of the function's
10002      activation.  That will happen (for example) if the parameter is never
10003      referenced within the function.
10004
10005      We could just generate a location descriptor here for all non-NULL
10006      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
10007      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
10008      where DECL_RTL is NULL or is a pseudo-reg.
10009
10010      Note however that we can only get away with using DECL_INCOMING_RTL as
10011      a backup substitute for DECL_RTL in certain limited cases.  In cases
10012      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
10013      we can be sure that the parameter was passed using the same type as it is
10014      declared to have within the function, and that its DECL_INCOMING_RTL
10015      points us to a place where a value of that type is passed.
10016
10017      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
10018      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
10019      because in these cases DECL_INCOMING_RTL points us to a value of some
10020      type which is *different* from the type of the parameter itself.  Thus,
10021      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
10022      such cases, the debugger would end up (for example) trying to fetch a
10023      `float' from a place which actually contains the first part of a
10024      `double'.  That would lead to really incorrect and confusing
10025      output at debug-time.
10026
10027      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
10028      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
10029      are a couple of exceptions however.  On little-endian machines we can
10030      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
10031      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
10032      an integral type that is smaller than TREE_TYPE (decl). These cases arise
10033      when (on a little-endian machine) a non-prototyped function has a
10034      parameter declared to be of type `short' or `char'.  In such cases,
10035      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
10036      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
10037      passed `int' value.  If the debugger then uses that address to fetch
10038      a `short' or a `char' (on a little-endian machine) the result will be
10039      the correct data, so we allow for such exceptional cases below.
10040
10041      Note that our goal here is to describe the place where the given formal
10042      parameter lives during most of the function's activation (i.e. between the
10043      end of the prologue and the start of the epilogue).  We'll do that as best
10044      as we can. Note however that if the given formal parameter is modified
10045      sometime during the execution of the function, then a stack backtrace (at
10046      debug-time) will show the function as having been called with the *new*
10047      value rather than the value which was originally passed in.  This happens
10048      rarely enough that it is not a major problem, but it *is* a problem, and
10049      I'd like to fix it.
10050
10051      A future version of dwarf2out.c may generate two additional attributes for
10052      any given DW_TAG_formal_parameter DIE which will describe the "passed
10053      type" and the "passed location" for the given formal parameter in addition
10054      to the attributes we now generate to indicate the "declared type" and the
10055      "active location" for each parameter.  This additional set of attributes
10056      could be used by debuggers for stack backtraces. Separately, note that
10057      sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
10058      This happens (for example) for inlined-instances of inline function formal
10059      parameters which are never referenced.  This really shouldn't be
10060      happening.  All PARM_DECL nodes should get valid non-NULL
10061      DECL_INCOMING_RTL values.  FIXME.  */
10062
10063   /* Use DECL_RTL as the "location" unless we find something better.  */
10064   rtl = DECL_RTL_IF_SET (decl);
10065
10066   /* When generating abstract instances, ignore everything except
10067      constants, symbols living in memory, and symbols living in
10068      fixed registers.  */
10069   if (! reload_completed)
10070     {
10071       if (rtl
10072           && (CONSTANT_P (rtl)
10073               || (MEM_P (rtl)
10074                   && CONSTANT_P (XEXP (rtl, 0)))
10075               || (REG_P (rtl)
10076                   && TREE_CODE (decl) == VAR_DECL
10077                   && TREE_STATIC (decl))))
10078         {
10079           rtl = targetm.delegitimize_address (rtl);
10080           return rtl;
10081         }
10082       rtl = NULL_RTX;
10083     }
10084   else if (TREE_CODE (decl) == PARM_DECL)
10085     {
10086       if (rtl == NULL_RTX || is_pseudo_reg (rtl))
10087         {
10088           tree declared_type = TREE_TYPE (decl);
10089           tree passed_type = DECL_ARG_TYPE (decl);
10090           enum machine_mode dmode = TYPE_MODE (declared_type);
10091           enum machine_mode pmode = TYPE_MODE (passed_type);
10092
10093           /* This decl represents a formal parameter which was optimized out.
10094              Note that DECL_INCOMING_RTL may be NULL in here, but we handle
10095              all cases where (rtl == NULL_RTX) just below.  */
10096           if (dmode == pmode)
10097             rtl = DECL_INCOMING_RTL (decl);
10098           else if (SCALAR_INT_MODE_P (dmode)
10099                    && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
10100                    && DECL_INCOMING_RTL (decl))
10101             {
10102               rtx inc = DECL_INCOMING_RTL (decl);
10103               if (REG_P (inc))
10104                 rtl = inc;
10105               else if (MEM_P (inc))
10106                 {
10107                   if (BYTES_BIG_ENDIAN)
10108                     rtl = adjust_address_nv (inc, dmode,
10109                                              GET_MODE_SIZE (pmode)
10110                                              - GET_MODE_SIZE (dmode));
10111                   else
10112                     rtl = inc;
10113                 }
10114             }
10115         }
10116
10117       /* If the parm was passed in registers, but lives on the stack, then
10118          make a big endian correction if the mode of the type of the
10119          parameter is not the same as the mode of the rtl.  */
10120       /* ??? This is the same series of checks that are made in dbxout.c before
10121          we reach the big endian correction code there.  It isn't clear if all
10122          of these checks are necessary here, but keeping them all is the safe
10123          thing to do.  */
10124       else if (MEM_P (rtl)
10125                && XEXP (rtl, 0) != const0_rtx
10126                && ! CONSTANT_P (XEXP (rtl, 0))
10127                /* Not passed in memory.  */
10128                && !MEM_P (DECL_INCOMING_RTL (decl))
10129                /* Not passed by invisible reference.  */
10130                && (!REG_P (XEXP (rtl, 0))
10131                    || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
10132                    || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
10133 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
10134                    || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
10135 #endif
10136                      )
10137                /* Big endian correction check.  */
10138                && BYTES_BIG_ENDIAN
10139                && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
10140                && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
10141                    < UNITS_PER_WORD))
10142         {
10143           int offset = (UNITS_PER_WORD
10144                         - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
10145
10146           rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
10147                              plus_constant (XEXP (rtl, 0), offset));
10148         }
10149     }
10150   else if (TREE_CODE (decl) == VAR_DECL
10151            && rtl
10152            && MEM_P (rtl)
10153            && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
10154            && BYTES_BIG_ENDIAN)
10155     {
10156       int rsize = GET_MODE_SIZE (GET_MODE (rtl));
10157       int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
10158
10159       /* If a variable is declared "register" yet is smaller than
10160          a register, then if we store the variable to memory, it
10161          looks like we're storing a register-sized value, when in
10162          fact we are not.  We need to adjust the offset of the
10163          storage location to reflect the actual value's bytes,
10164          else gdb will not be able to display it.  */
10165       if (rsize > dsize)
10166         rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
10167                            plus_constant (XEXP (rtl, 0), rsize-dsize));
10168     }
10169
10170   /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
10171      and will have been substituted directly into all expressions that use it.
10172      C does not have such a concept, but C++ and other languages do.  */
10173   if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
10174     rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
10175
10176   if (rtl)
10177     rtl = targetm.delegitimize_address (rtl);
10178
10179   /* If we don't look past the constant pool, we risk emitting a
10180      reference to a constant pool entry that isn't referenced from
10181      code, and thus is not emitted.  */
10182   if (rtl)
10183     rtl = avoid_constant_pool_reference (rtl);
10184
10185   return rtl;
10186 }
10187
10188 /* We need to figure out what section we should use as the base for the
10189    address ranges where a given location is valid.
10190    1. If this particular DECL has a section associated with it, use that.
10191    2. If this function has a section associated with it, use that.
10192    3. Otherwise, use the text section.
10193    XXX: If you split a variable across multiple sections, we won't notice.  */
10194
10195 static const char *
10196 secname_for_decl (tree decl)
10197 {
10198   const char *secname;
10199
10200   if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
10201     {
10202       tree sectree = DECL_SECTION_NAME (decl);
10203       secname = TREE_STRING_POINTER (sectree);
10204     }
10205   else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
10206     {
10207       tree sectree = DECL_SECTION_NAME (current_function_decl);
10208       secname = TREE_STRING_POINTER (sectree);
10209     }
10210   else if (cfun
10211            && (last_text_section == in_unlikely_executed_text
10212                || (last_text_section == in_named
10213                    && last_text_section_name
10214                       == cfun->unlikely_text_section_name)))
10215     secname = cfun->cold_section_label;
10216   else
10217     secname = text_section_label;
10218
10219   return secname;
10220 }
10221
10222 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
10223    data attribute for a variable or a parameter.  We generate the
10224    DW_AT_const_value attribute only in those cases where the given variable
10225    or parameter does not have a true "location" either in memory or in a
10226    register.  This can happen (for example) when a constant is passed as an
10227    actual argument in a call to an inline function.  (It's possible that
10228    these things can crop up in other ways also.)  Note that one type of
10229    constant value which can be passed into an inlined function is a constant
10230    pointer.  This can happen for example if an actual argument in an inlined
10231    function call evaluates to a compile-time constant address.  */
10232
10233 static void
10234 add_location_or_const_value_attribute (dw_die_ref die, tree decl,
10235                                        enum dwarf_attribute attr)
10236 {
10237   rtx rtl;
10238   dw_loc_descr_ref descr;
10239   var_loc_list *loc_list;
10240   struct var_loc_node *node;
10241   if (TREE_CODE (decl) == ERROR_MARK)
10242     return;
10243
10244   gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
10245               || TREE_CODE (decl) == RESULT_DECL);
10246              
10247   /* See if we possibly have multiple locations for this variable.  */
10248   loc_list = lookup_decl_loc (decl);
10249
10250   /* If it truly has multiple locations, the first and last node will
10251      differ.  */
10252   if (loc_list && loc_list->first != loc_list->last)
10253     {
10254       const char *endname, *secname;
10255       dw_loc_list_ref list;
10256       rtx varloc;
10257
10258       /* Now that we know what section we are using for a base,
10259          actually construct the list of locations.
10260          The first location information is what is passed to the
10261          function that creates the location list, and the remaining
10262          locations just get added on to that list.
10263          Note that we only know the start address for a location
10264          (IE location changes), so to build the range, we use
10265          the range [current location start, next location start].
10266          This means we have to special case the last node, and generate
10267          a range of [last location start, end of function label].  */
10268
10269       node = loc_list->first;
10270       varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10271       secname = secname_for_decl (decl);
10272
10273       list = new_loc_list (loc_descriptor (varloc),
10274                            node->label, node->next->label, secname, 1);
10275       node = node->next;
10276
10277       for (; node->next; node = node->next)
10278         if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
10279           {
10280             /* The variable has a location between NODE->LABEL and
10281                NODE->NEXT->LABEL.  */
10282             varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10283             add_loc_descr_to_loc_list (&list, loc_descriptor (varloc),
10284                                        node->label, node->next->label, secname);
10285           }
10286
10287       /* If the variable has a location at the last label
10288          it keeps its location until the end of function.  */
10289       if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
10290         {
10291           char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
10292
10293           varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10294           if (!current_function_decl)
10295             endname = text_end_label;
10296           else
10297             {
10298               ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
10299                                            current_function_funcdef_no);
10300               endname = ggc_strdup (label_id);
10301             }
10302           add_loc_descr_to_loc_list (&list, loc_descriptor (varloc),
10303                                      node->label, endname, secname);
10304         }
10305
10306       /* Finally, add the location list to the DIE, and we are done.  */
10307       add_AT_loc_list (die, attr, list);
10308       return;
10309     }
10310
10311   /* Try to get some constant RTL for this decl, and use that as the value of
10312      the location.  */
10313   
10314   rtl = rtl_for_decl_location (decl);
10315   if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING))
10316     {
10317       add_const_value_attribute (die, rtl);
10318       return;
10319     }
10320   
10321   /* If we have tried to generate the location otherwise, and it
10322      didn't work out (we wouldn't be here if we did), and we have a one entry
10323      location list, try generating a location from that.  */
10324   if (loc_list && loc_list->first)
10325     {
10326       node = loc_list->first;
10327       descr = loc_descriptor (NOTE_VAR_LOCATION (node->var_loc_note));
10328       if (descr)
10329         {
10330           add_AT_location_description (die, attr, descr);
10331           return;
10332         }
10333     }
10334
10335   /* We couldn't get any rtl, so try directly generating the location
10336      description from the tree.  */
10337   descr = loc_descriptor_from_tree (decl);
10338   if (descr)
10339     {
10340       add_AT_location_description (die, attr, descr);
10341       return;
10342     }
10343 }
10344
10345 /* If we don't have a copy of this variable in memory for some reason (such
10346    as a C++ member constant that doesn't have an out-of-line definition),
10347    we should tell the debugger about the constant value.  */
10348
10349 static void
10350 tree_add_const_value_attribute (dw_die_ref var_die, tree decl)
10351 {
10352   tree init = DECL_INITIAL (decl);
10353   tree type = TREE_TYPE (decl);
10354   rtx rtl;
10355
10356   if (TREE_READONLY (decl) && ! TREE_THIS_VOLATILE (decl) && init)
10357     /* OK */;
10358   else
10359     return;
10360
10361   rtl = rtl_for_decl_init (init, type);
10362   if (rtl)
10363     add_const_value_attribute (var_die, rtl);
10364 }
10365
10366 /* Convert the CFI instructions for the current function into a location
10367    list.  This is used for DW_AT_frame_base when we targeting a dwarf2
10368    consumer that does not support the dwarf3 DW_OP_call_frame_cfa.  */
10369
10370 static dw_loc_list_ref
10371 convert_cfa_to_loc_list (void)
10372 {
10373   dw_fde_ref fde;
10374   dw_loc_list_ref list, *list_tail;
10375   dw_cfi_ref cfi;
10376   dw_cfa_location last_cfa, next_cfa;
10377   const char *start_label, *last_label, *section;
10378
10379   fde = &fde_table[fde_table_in_use - 1];
10380
10381   section = secname_for_decl (current_function_decl);
10382   list_tail = &list;
10383   list = NULL;
10384
10385   next_cfa.reg = INVALID_REGNUM;
10386   next_cfa.offset = 0;
10387   next_cfa.indirect = 0;
10388   next_cfa.base_offset = 0;
10389
10390   start_label = fde->dw_fde_begin;
10391
10392   /* ??? Bald assumption that the CIE opcode list does not contain
10393      advance opcodes.  */
10394   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
10395     lookup_cfa_1 (cfi, &next_cfa);
10396
10397   last_cfa = next_cfa;
10398   last_label = start_label;
10399
10400   for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
10401     switch (cfi->dw_cfi_opc)
10402       {
10403       case DW_CFA_set_loc:
10404       case DW_CFA_advance_loc1:
10405       case DW_CFA_advance_loc2:
10406       case DW_CFA_advance_loc4:
10407         if (!cfa_equal_p (&last_cfa, &next_cfa))
10408           {
10409             *list_tail = new_loc_list (build_cfa_loc (&last_cfa), start_label,
10410                                        last_label, section, list == NULL);
10411
10412             list_tail = &(*list_tail)->dw_loc_next;
10413             last_cfa = next_cfa;
10414             start_label = last_label;
10415           }
10416         last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
10417         break;
10418
10419       case DW_CFA_advance_loc:
10420         /* The encoding is complex enough that we should never emit this.  */
10421       case DW_CFA_remember_state:
10422       case DW_CFA_restore_state:
10423         /* We don't handle these two in this function.  It would be possible
10424            if it were to be required.  */
10425         gcc_unreachable ();
10426
10427       default:
10428         lookup_cfa_1 (cfi, &next_cfa);
10429         break;
10430       }
10431
10432   if (!cfa_equal_p (&last_cfa, &next_cfa))
10433     {
10434       *list_tail = new_loc_list (build_cfa_loc (&last_cfa), start_label,
10435                                  last_label, section, list == NULL);
10436       list_tail = &(*list_tail)->dw_loc_next;
10437       start_label = last_label;
10438     }
10439   *list_tail = new_loc_list (build_cfa_loc (&next_cfa), start_label,
10440                              fde->dw_fde_end, section, list == NULL);
10441
10442   return list;
10443 }
10444
10445 /* Compute a displacement from the "steady-state frame pointer" to
10446    the CFA, and store it in frame_pointer_cfa_offset.  */
10447
10448 static void
10449 compute_frame_pointer_to_cfa_displacement (void)
10450 {
10451   HOST_WIDE_INT offset;
10452   rtx reg, elim;
10453
10454 #ifdef FRAME_POINTER_CFA_OFFSET
10455   reg = frame_pointer_rtx;
10456   offset = FRAME_POINTER_CFA_OFFSET (current_function_decl);
10457 #else
10458   reg = arg_pointer_rtx;
10459   offset = ARG_POINTER_CFA_OFFSET (current_function_decl);
10460 #endif
10461
10462   elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
10463   if (GET_CODE (elim) == PLUS)
10464     {
10465       offset += INTVAL (XEXP (elim, 1));
10466       elim = XEXP (elim, 0);
10467     }
10468   gcc_assert (elim == (frame_pointer_needed ? hard_frame_pointer_rtx
10469                        : stack_pointer_rtx));
10470
10471   frame_pointer_cfa_offset = -offset;
10472 }
10473
10474 /* Generate a DW_AT_name attribute given some string value to be included as
10475    the value of the attribute.  */
10476
10477 static void
10478 add_name_attribute (dw_die_ref die, const char *name_string)
10479 {
10480   if (name_string != NULL && *name_string != 0)
10481     {
10482       if (demangle_name_func)
10483         name_string = (*demangle_name_func) (name_string);
10484
10485       add_AT_string (die, DW_AT_name, name_string);
10486     }
10487 }
10488
10489 /* Generate a DW_AT_comp_dir attribute for DIE.  */
10490
10491 static void
10492 add_comp_dir_attribute (dw_die_ref die)
10493 {
10494   const char *wd = get_src_pwd ();
10495   if (wd != NULL)
10496     add_AT_string (die, DW_AT_comp_dir, wd);
10497 }
10498
10499 /* Given a tree node describing an array bound (either lower or upper) output
10500    a representation for that bound.  */
10501
10502 static void
10503 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
10504 {
10505   switch (TREE_CODE (bound))
10506     {
10507     case ERROR_MARK:
10508       return;
10509
10510     /* All fixed-bounds are represented by INTEGER_CST nodes.  */
10511     case INTEGER_CST:
10512       if (! host_integerp (bound, 0)
10513           || (bound_attr == DW_AT_lower_bound
10514               && (((is_c_family () || is_java ()) &&  integer_zerop (bound))
10515                   || (is_fortran () && integer_onep (bound)))))
10516         /* Use the default.  */
10517         ;
10518       else
10519         add_AT_unsigned (subrange_die, bound_attr, tree_low_cst (bound, 0));
10520       break;
10521
10522     case CONVERT_EXPR:
10523     case NOP_EXPR:
10524     case NON_LVALUE_EXPR:
10525     case VIEW_CONVERT_EXPR:
10526       add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
10527       break;
10528
10529     case SAVE_EXPR:
10530       break;
10531
10532     case VAR_DECL:
10533     case PARM_DECL:
10534     case RESULT_DECL:
10535       {
10536         dw_die_ref decl_die = lookup_decl_die (bound);
10537
10538         /* ??? Can this happen, or should the variable have been bound
10539            first?  Probably it can, since I imagine that we try to create
10540            the types of parameters in the order in which they exist in
10541            the list, and won't have created a forward reference to a
10542            later parameter.  */
10543         if (decl_die != NULL)
10544           add_AT_die_ref (subrange_die, bound_attr, decl_die);
10545         break;
10546       }
10547
10548     default:
10549       {
10550         /* Otherwise try to create a stack operation procedure to
10551            evaluate the value of the array bound.  */
10552
10553         dw_die_ref ctx, decl_die;
10554         dw_loc_descr_ref loc;
10555
10556         loc = loc_descriptor_from_tree (bound);
10557         if (loc == NULL)
10558           break;
10559
10560         if (current_function_decl == 0)
10561           ctx = comp_unit_die;
10562         else
10563           ctx = lookup_decl_die (current_function_decl);
10564
10565         decl_die = new_die (DW_TAG_variable, ctx, bound);
10566         add_AT_flag (decl_die, DW_AT_artificial, 1);
10567         add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
10568         add_AT_loc (decl_die, DW_AT_location, loc);
10569
10570         add_AT_die_ref (subrange_die, bound_attr, decl_die);
10571         break;
10572       }
10573     }
10574 }
10575
10576 /* Note that the block of subscript information for an array type also
10577    includes information about the element type of type given array type.  */
10578
10579 static void
10580 add_subscript_info (dw_die_ref type_die, tree type)
10581 {
10582 #ifndef MIPS_DEBUGGING_INFO
10583   unsigned dimension_number;
10584 #endif
10585   tree lower, upper;
10586   dw_die_ref subrange_die;
10587
10588   /* The GNU compilers represent multidimensional array types as sequences of
10589      one dimensional array types whose element types are themselves array
10590      types.  Here we squish that down, so that each multidimensional array
10591      type gets only one array_type DIE in the Dwarf debugging info. The draft
10592      Dwarf specification say that we are allowed to do this kind of
10593      compression in C (because there is no difference between an array or
10594      arrays and a multidimensional array in C) but for other source languages
10595      (e.g. Ada) we probably shouldn't do this.  */
10596
10597   /* ??? The SGI dwarf reader fails for multidimensional arrays with a
10598      const enum type.  E.g. const enum machine_mode insn_operand_mode[2][10].
10599      We work around this by disabling this feature.  See also
10600      gen_array_type_die.  */
10601 #ifndef MIPS_DEBUGGING_INFO
10602   for (dimension_number = 0;
10603        TREE_CODE (type) == ARRAY_TYPE;
10604        type = TREE_TYPE (type), dimension_number++)
10605 #endif
10606     {
10607       tree domain = TYPE_DOMAIN (type);
10608
10609       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
10610          and (in GNU C only) variable bounds.  Handle all three forms
10611          here.  */
10612       subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
10613       if (domain)
10614         {
10615           /* We have an array type with specified bounds.  */
10616           lower = TYPE_MIN_VALUE (domain);
10617           upper = TYPE_MAX_VALUE (domain);
10618
10619           /* Define the index type.  */
10620           if (TREE_TYPE (domain))
10621             {
10622               /* ??? This is probably an Ada unnamed subrange type.  Ignore the
10623                  TREE_TYPE field.  We can't emit debug info for this
10624                  because it is an unnamed integral type.  */
10625               if (TREE_CODE (domain) == INTEGER_TYPE
10626                   && TYPE_NAME (domain) == NULL_TREE
10627                   && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
10628                   && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
10629                 ;
10630               else
10631                 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
10632                                     type_die);
10633             }
10634
10635           /* ??? If upper is NULL, the array has unspecified length,
10636              but it does have a lower bound.  This happens with Fortran
10637                dimension arr(N:*)
10638              Since the debugger is definitely going to need to know N
10639              to produce useful results, go ahead and output the lower
10640              bound solo, and hope the debugger can cope.  */
10641
10642           add_bound_info (subrange_die, DW_AT_lower_bound, lower);
10643           if (upper)
10644             add_bound_info (subrange_die, DW_AT_upper_bound, upper);
10645         }
10646
10647       /* Otherwise we have an array type with an unspecified length.  The
10648          DWARF-2 spec does not say how to handle this; let's just leave out the
10649          bounds.  */
10650     }
10651 }
10652
10653 static void
10654 add_byte_size_attribute (dw_die_ref die, tree tree_node)
10655 {
10656   unsigned size;
10657
10658   switch (TREE_CODE (tree_node))
10659     {
10660     case ERROR_MARK:
10661       size = 0;
10662       break;
10663     case ENUMERAL_TYPE:
10664     case RECORD_TYPE:
10665     case UNION_TYPE:
10666     case QUAL_UNION_TYPE:
10667       size = int_size_in_bytes (tree_node);
10668       break;
10669     case FIELD_DECL:
10670       /* For a data member of a struct or union, the DW_AT_byte_size is
10671          generally given as the number of bytes normally allocated for an
10672          object of the *declared* type of the member itself.  This is true
10673          even for bit-fields.  */
10674       size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
10675       break;
10676     default:
10677       gcc_unreachable ();
10678     }
10679
10680   /* Note that `size' might be -1 when we get to this point.  If it is, that
10681      indicates that the byte size of the entity in question is variable.  We
10682      have no good way of expressing this fact in Dwarf at the present time,
10683      so just let the -1 pass on through.  */
10684   add_AT_unsigned (die, DW_AT_byte_size, size);
10685 }
10686
10687 /* For a FIELD_DECL node which represents a bit-field, output an attribute
10688    which specifies the distance in bits from the highest order bit of the
10689    "containing object" for the bit-field to the highest order bit of the
10690    bit-field itself.
10691
10692    For any given bit-field, the "containing object" is a hypothetical object
10693    (of some integral or enum type) within which the given bit-field lives.  The
10694    type of this hypothetical "containing object" is always the same as the
10695    declared type of the individual bit-field itself.  The determination of the
10696    exact location of the "containing object" for a bit-field is rather
10697    complicated.  It's handled by the `field_byte_offset' function (above).
10698
10699    Note that it is the size (in bytes) of the hypothetical "containing object"
10700    which will be given in the DW_AT_byte_size attribute for this bit-field.
10701    (See `byte_size_attribute' above).  */
10702
10703 static inline void
10704 add_bit_offset_attribute (dw_die_ref die, tree decl)
10705 {
10706   HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
10707   tree type = DECL_BIT_FIELD_TYPE (decl);
10708   HOST_WIDE_INT bitpos_int;
10709   HOST_WIDE_INT highest_order_object_bit_offset;
10710   HOST_WIDE_INT highest_order_field_bit_offset;
10711   HOST_WIDE_INT unsigned bit_offset;
10712
10713   /* Must be a field and a bit field.  */
10714   gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
10715
10716   /* We can't yet handle bit-fields whose offsets are variable, so if we
10717      encounter such things, just return without generating any attribute
10718      whatsoever.  Likewise for variable or too large size.  */
10719   if (! host_integerp (bit_position (decl), 0)
10720       || ! host_integerp (DECL_SIZE (decl), 1))
10721     return;
10722
10723   bitpos_int = int_bit_position (decl);
10724
10725   /* Note that the bit offset is always the distance (in bits) from the
10726      highest-order bit of the "containing object" to the highest-order bit of
10727      the bit-field itself.  Since the "high-order end" of any object or field
10728      is different on big-endian and little-endian machines, the computation
10729      below must take account of these differences.  */
10730   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
10731   highest_order_field_bit_offset = bitpos_int;
10732
10733   if (! BYTES_BIG_ENDIAN)
10734     {
10735       highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
10736       highest_order_object_bit_offset += simple_type_size_in_bits (type);
10737     }
10738
10739   bit_offset
10740     = (! BYTES_BIG_ENDIAN
10741        ? highest_order_object_bit_offset - highest_order_field_bit_offset
10742        : highest_order_field_bit_offset - highest_order_object_bit_offset);
10743
10744   add_AT_unsigned (die, DW_AT_bit_offset, bit_offset);
10745 }
10746
10747 /* For a FIELD_DECL node which represents a bit field, output an attribute
10748    which specifies the length in bits of the given field.  */
10749
10750 static inline void
10751 add_bit_size_attribute (dw_die_ref die, tree decl)
10752 {
10753   /* Must be a field and a bit field.  */
10754   gcc_assert (TREE_CODE (decl) == FIELD_DECL
10755               && DECL_BIT_FIELD_TYPE (decl));
10756
10757   if (host_integerp (DECL_SIZE (decl), 1))
10758     add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
10759 }
10760
10761 /* If the compiled language is ANSI C, then add a 'prototyped'
10762    attribute, if arg types are given for the parameters of a function.  */
10763
10764 static inline void
10765 add_prototyped_attribute (dw_die_ref die, tree func_type)
10766 {
10767   if (get_AT_unsigned (comp_unit_die, DW_AT_language) == DW_LANG_C89
10768       && TYPE_ARG_TYPES (func_type) != NULL)
10769     add_AT_flag (die, DW_AT_prototyped, 1);
10770 }
10771
10772 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
10773    by looking in either the type declaration or object declaration
10774    equate table.  */
10775
10776 static inline void
10777 add_abstract_origin_attribute (dw_die_ref die, tree origin)
10778 {
10779   dw_die_ref origin_die = NULL;
10780
10781   if (TREE_CODE (origin) != FUNCTION_DECL)
10782     {
10783       /* We may have gotten separated from the block for the inlined
10784          function, if we're in an exception handler or some such; make
10785          sure that the abstract function has been written out.
10786
10787          Doing this for nested functions is wrong, however; functions are
10788          distinct units, and our context might not even be inline.  */
10789       tree fn = origin;
10790
10791       if (TYPE_P (fn))
10792         fn = TYPE_STUB_DECL (fn);
10793       
10794       fn = decl_function_context (fn);
10795       if (fn)
10796         dwarf2out_abstract_function (fn);
10797     }
10798
10799   if (DECL_P (origin))
10800     origin_die = lookup_decl_die (origin);
10801   else if (TYPE_P (origin))
10802     origin_die = lookup_type_die (origin);
10803
10804   /* XXX: Functions that are never lowered don't always have correct block
10805      trees (in the case of java, they simply have no block tree, in some other
10806      languages).  For these functions, there is nothing we can really do to
10807      output correct debug info for inlined functions in all cases.  Rather
10808      than die, we'll just produce deficient debug info now, in that we will
10809      have variables without a proper abstract origin.  In the future, when all
10810      functions are lowered, we should re-add a gcc_assert (origin_die)
10811      here.  */
10812
10813   if (origin_die)
10814       add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
10815 }
10816
10817 /* We do not currently support the pure_virtual attribute.  */
10818
10819 static inline void
10820 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
10821 {
10822   if (DECL_VINDEX (func_decl))
10823     {
10824       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
10825
10826       if (host_integerp (DECL_VINDEX (func_decl), 0))
10827         add_AT_loc (die, DW_AT_vtable_elem_location,
10828                     new_loc_descr (DW_OP_constu,
10829                                    tree_low_cst (DECL_VINDEX (func_decl), 0),
10830                                    0));
10831
10832       /* GNU extension: Record what type this method came from originally.  */
10833       if (debug_info_level > DINFO_LEVEL_TERSE)
10834         add_AT_die_ref (die, DW_AT_containing_type,
10835                         lookup_type_die (DECL_CONTEXT (func_decl)));
10836     }
10837 }
10838 \f
10839 /* Add source coordinate attributes for the given decl.  */
10840
10841 static void
10842 add_src_coords_attributes (dw_die_ref die, tree decl)
10843 {
10844   expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
10845   unsigned file_index = lookup_filename (s.file);
10846
10847   add_AT_unsigned (die, DW_AT_decl_file, file_index);
10848   add_AT_unsigned (die, DW_AT_decl_line, s.line);
10849 }
10850
10851 /* Add a DW_AT_name attribute and source coordinate attribute for the
10852    given decl, but only if it actually has a name.  */
10853
10854 static void
10855 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
10856 {
10857   tree decl_name;
10858
10859   decl_name = DECL_NAME (decl);
10860   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
10861     {
10862       add_name_attribute (die, dwarf2_name (decl, 0));
10863       if (! DECL_ARTIFICIAL (decl))
10864         add_src_coords_attributes (die, decl);
10865
10866       if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
10867           && TREE_PUBLIC (decl)
10868           && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
10869           && !DECL_ABSTRACT (decl)
10870           && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl)))
10871         add_AT_string (die, DW_AT_MIPS_linkage_name,
10872                        IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
10873     }
10874
10875 #ifdef VMS_DEBUGGING_INFO
10876   /* Get the function's name, as described by its RTL.  This may be different
10877      from the DECL_NAME name used in the source file.  */
10878   if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
10879     {
10880       add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
10881                    XEXP (DECL_RTL (decl), 0));
10882       VEC_safe_push (tree, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
10883     }
10884 #endif
10885 }
10886
10887 /* Push a new declaration scope.  */
10888
10889 static void
10890 push_decl_scope (tree scope)
10891 {
10892   VEC_safe_push (tree, gc, decl_scope_table, scope);
10893 }
10894
10895 /* Pop a declaration scope.  */
10896
10897 static inline void
10898 pop_decl_scope (void)
10899 {
10900   VEC_pop (tree, decl_scope_table);
10901 }
10902
10903 /* Return the DIE for the scope that immediately contains this type.
10904    Non-named types get global scope.  Named types nested in other
10905    types get their containing scope if it's open, or global scope
10906    otherwise.  All other types (i.e. function-local named types) get
10907    the current active scope.  */
10908
10909 static dw_die_ref
10910 scope_die_for (tree t, dw_die_ref context_die)
10911 {
10912   dw_die_ref scope_die = NULL;
10913   tree containing_scope;
10914   int i;
10915
10916   /* Non-types always go in the current scope.  */
10917   gcc_assert (TYPE_P (t));
10918
10919   containing_scope = TYPE_CONTEXT (t);
10920
10921   /* Use the containing namespace if it was passed in (for a declaration).  */
10922   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
10923     {
10924       if (context_die == lookup_decl_die (containing_scope))
10925         /* OK */;
10926       else
10927         containing_scope = NULL_TREE;
10928     }
10929
10930   /* Ignore function type "scopes" from the C frontend.  They mean that
10931      a tagged type is local to a parmlist of a function declarator, but
10932      that isn't useful to DWARF.  */
10933   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
10934     containing_scope = NULL_TREE;
10935
10936   if (containing_scope == NULL_TREE)
10937     scope_die = comp_unit_die;
10938   else if (TYPE_P (containing_scope))
10939     {
10940       /* For types, we can just look up the appropriate DIE.  But
10941          first we check to see if we're in the middle of emitting it
10942          so we know where the new DIE should go.  */
10943       for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
10944         if (VEC_index (tree, decl_scope_table, i) == containing_scope)
10945           break;
10946
10947       if (i < 0)
10948         {
10949           gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
10950                       || TREE_ASM_WRITTEN (containing_scope));
10951
10952           /* If none of the current dies are suitable, we get file scope.  */
10953           scope_die = comp_unit_die;
10954         }
10955       else
10956         scope_die = lookup_type_die (containing_scope);
10957     }
10958   else
10959     scope_die = context_die;
10960
10961   return scope_die;
10962 }
10963
10964 /* Returns nonzero if CONTEXT_DIE is internal to a function.  */
10965
10966 static inline int
10967 local_scope_p (dw_die_ref context_die)
10968 {
10969   for (; context_die; context_die = context_die->die_parent)
10970     if (context_die->die_tag == DW_TAG_inlined_subroutine
10971         || context_die->die_tag == DW_TAG_subprogram)
10972       return 1;
10973
10974   return 0;
10975 }
10976
10977 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
10978    whether or not to treat a DIE in this context as a declaration.  */
10979
10980 static inline int
10981 class_or_namespace_scope_p (dw_die_ref context_die)
10982 {
10983   return (context_die
10984           && (context_die->die_tag == DW_TAG_structure_type
10985               || context_die->die_tag == DW_TAG_union_type
10986               || context_die->die_tag == DW_TAG_namespace));
10987 }
10988
10989 /* Many forms of DIEs require a "type description" attribute.  This
10990    routine locates the proper "type descriptor" die for the type given
10991    by 'type', and adds a DW_AT_type attribute below the given die.  */
10992
10993 static void
10994 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
10995                     int decl_volatile, dw_die_ref context_die)
10996 {
10997   enum tree_code code  = TREE_CODE (type);
10998   dw_die_ref type_die  = NULL;
10999
11000   /* ??? If this type is an unnamed subrange type of an integral or
11001      floating-point type, use the inner type.  This is because we have no
11002      support for unnamed types in base_type_die.  This can happen if this is
11003      an Ada subrange type.  Correct solution is emit a subrange type die.  */
11004   if ((code == INTEGER_TYPE || code == REAL_TYPE)
11005       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
11006     type = TREE_TYPE (type), code = TREE_CODE (type);
11007
11008   if (code == ERROR_MARK
11009       /* Handle a special case.  For functions whose return type is void, we
11010          generate *no* type attribute.  (Note that no object may have type
11011          `void', so this only applies to function return types).  */
11012       || code == VOID_TYPE)
11013     return;
11014
11015   type_die = modified_type_die (type,
11016                                 decl_const || TYPE_READONLY (type),
11017                                 decl_volatile || TYPE_VOLATILE (type),
11018                                 context_die);
11019
11020   if (type_die != NULL)
11021     add_AT_die_ref (object_die, DW_AT_type, type_die);
11022 }
11023
11024 /* Given an object die, add the calling convention attribute for the
11025    function call type.  */
11026 static void
11027 add_calling_convention_attribute (dw_die_ref subr_die, tree type)
11028 {
11029   enum dwarf_calling_convention value = DW_CC_normal;
11030
11031   value = targetm.dwarf_calling_convention (type);
11032
11033   /* Only add the attribute if the backend requests it, and
11034      is not DW_CC_normal.  */
11035   if (value && (value != DW_CC_normal))
11036     add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
11037 }
11038
11039 /* Given a tree pointer to a struct, class, union, or enum type node, return
11040    a pointer to the (string) tag name for the given type, or zero if the type
11041    was declared without a tag.  */
11042
11043 static const char *
11044 type_tag (tree type)
11045 {
11046   const char *name = 0;
11047
11048   if (TYPE_NAME (type) != 0)
11049     {
11050       tree t = 0;
11051
11052       /* Find the IDENTIFIER_NODE for the type name.  */
11053       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
11054         t = TYPE_NAME (type);
11055
11056       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
11057          a TYPE_DECL node, regardless of whether or not a `typedef' was
11058          involved.  */
11059       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
11060                && ! DECL_IGNORED_P (TYPE_NAME (type)))
11061         t = DECL_NAME (TYPE_NAME (type));
11062
11063       /* Now get the name as a string, or invent one.  */
11064       if (t != 0)
11065         name = IDENTIFIER_POINTER (t);
11066     }
11067
11068   return (name == 0 || *name == '\0') ? 0 : name;
11069 }
11070
11071 /* Return the type associated with a data member, make a special check
11072    for bit field types.  */
11073
11074 static inline tree
11075 member_declared_type (tree member)
11076 {
11077   return (DECL_BIT_FIELD_TYPE (member)
11078           ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
11079 }
11080
11081 /* Get the decl's label, as described by its RTL. This may be different
11082    from the DECL_NAME name used in the source file.  */
11083
11084 #if 0
11085 static const char *
11086 decl_start_label (tree decl)
11087 {
11088   rtx x;
11089   const char *fnname;
11090
11091   x = DECL_RTL (decl);
11092   gcc_assert (MEM_P (x));
11093
11094   x = XEXP (x, 0);
11095   gcc_assert (GET_CODE (x) == SYMBOL_REF);
11096
11097   fnname = XSTR (x, 0);
11098   return fnname;
11099 }
11100 #endif
11101 \f
11102 /* These routines generate the internal representation of the DIE's for
11103    the compilation unit.  Debugging information is collected by walking
11104    the declaration trees passed in from dwarf2out_decl().  */
11105
11106 static void
11107 gen_array_type_die (tree type, dw_die_ref context_die)
11108 {
11109   dw_die_ref scope_die = scope_die_for (type, context_die);
11110   dw_die_ref array_die;
11111   tree element_type;
11112
11113   /* ??? The SGI dwarf reader fails for array of array of enum types unless
11114      the inner array type comes before the outer array type.  Thus we must
11115      call gen_type_die before we call new_die.  See below also.  */
11116 #ifdef MIPS_DEBUGGING_INFO
11117   gen_type_die (TREE_TYPE (type), context_die);
11118 #endif
11119
11120   array_die = new_die (DW_TAG_array_type, scope_die, type);
11121   add_name_attribute (array_die, type_tag (type));
11122   equate_type_number_to_die (type, array_die);
11123
11124   if (TREE_CODE (type) == VECTOR_TYPE)
11125     {
11126       /* The frontend feeds us a representation for the vector as a struct
11127          containing an array.  Pull out the array type.  */
11128       type = TREE_TYPE (TYPE_FIELDS (TYPE_DEBUG_REPRESENTATION_TYPE (type)));
11129       add_AT_flag (array_die, DW_AT_GNU_vector, 1);
11130     }
11131
11132 #if 0
11133   /* We default the array ordering.  SDB will probably do
11134      the right things even if DW_AT_ordering is not present.  It's not even
11135      an issue until we start to get into multidimensional arrays anyway.  If
11136      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
11137      then we'll have to put the DW_AT_ordering attribute back in.  (But if
11138      and when we find out that we need to put these in, we will only do so
11139      for multidimensional arrays.  */
11140   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
11141 #endif
11142
11143 #ifdef MIPS_DEBUGGING_INFO
11144   /* The SGI compilers handle arrays of unknown bound by setting
11145      AT_declaration and not emitting any subrange DIEs.  */
11146   if (! TYPE_DOMAIN (type))
11147     add_AT_flag (array_die, DW_AT_declaration, 1);
11148   else
11149 #endif
11150     add_subscript_info (array_die, type);
11151
11152   /* Add representation of the type of the elements of this array type.  */
11153   element_type = TREE_TYPE (type);
11154
11155   /* ??? The SGI dwarf reader fails for multidimensional arrays with a
11156      const enum type.  E.g. const enum machine_mode insn_operand_mode[2][10].
11157      We work around this by disabling this feature.  See also
11158      add_subscript_info.  */
11159 #ifndef MIPS_DEBUGGING_INFO
11160   while (TREE_CODE (element_type) == ARRAY_TYPE)
11161     element_type = TREE_TYPE (element_type);
11162
11163   gen_type_die (element_type, context_die);
11164 #endif
11165
11166   add_type_attribute (array_die, element_type, 0, 0, context_die);
11167 }
11168
11169 #if 0
11170 static void
11171 gen_entry_point_die (tree decl, dw_die_ref context_die)
11172 {
11173   tree origin = decl_ultimate_origin (decl);
11174   dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
11175
11176   if (origin != NULL)
11177     add_abstract_origin_attribute (decl_die, origin);
11178   else
11179     {
11180       add_name_and_src_coords_attributes (decl_die, decl);
11181       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
11182                           0, 0, context_die);
11183     }
11184
11185   if (DECL_ABSTRACT (decl))
11186     equate_decl_number_to_die (decl, decl_die);
11187   else
11188     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
11189 }
11190 #endif
11191
11192 /* Walk through the list of incomplete types again, trying once more to
11193    emit full debugging info for them.  */
11194
11195 static void
11196 retry_incomplete_types (void)
11197 {
11198   int i;
11199
11200   for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
11201     gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die);
11202 }
11203
11204 /* Generate a DIE to represent an inlined instance of an enumeration type.  */
11205
11206 static void
11207 gen_inlined_enumeration_type_die (tree type, dw_die_ref context_die)
11208 {
11209   dw_die_ref type_die = new_die (DW_TAG_enumeration_type, context_die, type);
11210
11211   /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
11212      be incomplete and such types are not marked.  */
11213   add_abstract_origin_attribute (type_die, type);
11214 }
11215
11216 /* Generate a DIE to represent an inlined instance of a structure type.  */
11217
11218 static void
11219 gen_inlined_structure_type_die (tree type, dw_die_ref context_die)
11220 {
11221   dw_die_ref type_die = new_die (DW_TAG_structure_type, context_die, type);
11222
11223   /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
11224      be incomplete and such types are not marked.  */
11225   add_abstract_origin_attribute (type_die, type);
11226 }
11227
11228 /* Generate a DIE to represent an inlined instance of a union type.  */
11229
11230 static void
11231 gen_inlined_union_type_die (tree type, dw_die_ref context_die)
11232 {
11233   dw_die_ref type_die = new_die (DW_TAG_union_type, context_die, type);
11234
11235   /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
11236      be incomplete and such types are not marked.  */
11237   add_abstract_origin_attribute (type_die, type);
11238 }
11239
11240 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
11241    include all of the information about the enumeration values also. Each
11242    enumerated type name/value is listed as a child of the enumerated type
11243    DIE.  */
11244
11245 static dw_die_ref
11246 gen_enumeration_type_die (tree type, dw_die_ref context_die)
11247 {
11248   dw_die_ref type_die = lookup_type_die (type);
11249
11250   if (type_die == NULL)
11251     {
11252       type_die = new_die (DW_TAG_enumeration_type,
11253                           scope_die_for (type, context_die), type);
11254       equate_type_number_to_die (type, type_die);
11255       add_name_attribute (type_die, type_tag (type));
11256     }
11257   else if (! TYPE_SIZE (type))
11258     return type_die;
11259   else
11260     remove_AT (type_die, DW_AT_declaration);
11261
11262   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
11263      given enum type is incomplete, do not generate the DW_AT_byte_size
11264      attribute or the DW_AT_element_list attribute.  */
11265   if (TYPE_SIZE (type))
11266     {
11267       tree link;
11268
11269       TREE_ASM_WRITTEN (type) = 1;
11270       add_byte_size_attribute (type_die, type);
11271       if (TYPE_STUB_DECL (type) != NULL_TREE)
11272         add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
11273
11274       /* If the first reference to this type was as the return type of an
11275          inline function, then it may not have a parent.  Fix this now.  */
11276       if (type_die->die_parent == NULL)
11277         add_child_die (scope_die_for (type, context_die), type_die);
11278
11279       for (link = TYPE_VALUES (type);
11280            link != NULL; link = TREE_CHAIN (link))
11281         {
11282           dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
11283           tree value = TREE_VALUE (link);
11284
11285           add_name_attribute (enum_die,
11286                               IDENTIFIER_POINTER (TREE_PURPOSE (link)));
11287
11288           if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
11289             /* DWARF2 does not provide a way of indicating whether or
11290                not enumeration constants are signed or unsigned.  GDB
11291                always assumes the values are signed, so we output all
11292                values as if they were signed.  That means that
11293                enumeration constants with very large unsigned values
11294                will appear to have negative values in the debugger.  */
11295             add_AT_int (enum_die, DW_AT_const_value,
11296                         tree_low_cst (value, tree_int_cst_sgn (value) > 0));
11297         }
11298     }
11299   else
11300     add_AT_flag (type_die, DW_AT_declaration, 1);
11301
11302   return type_die;
11303 }
11304
11305 /* Generate a DIE to represent either a real live formal parameter decl or to
11306    represent just the type of some formal parameter position in some function
11307    type.
11308
11309    Note that this routine is a bit unusual because its argument may be a
11310    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
11311    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
11312    node.  If it's the former then this function is being called to output a
11313    DIE to represent a formal parameter object (or some inlining thereof).  If
11314    it's the latter, then this function is only being called to output a
11315    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
11316    argument type of some subprogram type.  */
11317
11318 static dw_die_ref
11319 gen_formal_parameter_die (tree node, dw_die_ref context_die)
11320 {
11321   dw_die_ref parm_die
11322     = new_die (DW_TAG_formal_parameter, context_die, node);
11323   tree origin;
11324
11325   switch (TREE_CODE_CLASS (TREE_CODE (node)))
11326     {
11327     case tcc_declaration:
11328       origin = decl_ultimate_origin (node);
11329       if (origin != NULL)
11330         add_abstract_origin_attribute (parm_die, origin);
11331       else
11332         {
11333           add_name_and_src_coords_attributes (parm_die, node);
11334           add_type_attribute (parm_die, TREE_TYPE (node),
11335                               TREE_READONLY (node),
11336                               TREE_THIS_VOLATILE (node),
11337                               context_die);
11338           if (DECL_ARTIFICIAL (node))
11339             add_AT_flag (parm_die, DW_AT_artificial, 1);
11340         }
11341
11342       equate_decl_number_to_die (node, parm_die);
11343       if (! DECL_ABSTRACT (node))
11344         add_location_or_const_value_attribute (parm_die, node, DW_AT_location);
11345
11346       break;
11347
11348     case tcc_type:
11349       /* We were called with some kind of a ..._TYPE node.  */
11350       add_type_attribute (parm_die, node, 0, 0, context_die);
11351       break;
11352
11353     default:
11354       gcc_unreachable ();
11355     }
11356
11357   return parm_die;
11358 }
11359
11360 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
11361    at the end of an (ANSI prototyped) formal parameters list.  */
11362
11363 static void
11364 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
11365 {
11366   new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
11367 }
11368
11369 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
11370    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
11371    parameters as specified in some function type specification (except for
11372    those which appear as part of a function *definition*).  */
11373
11374 static void
11375 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
11376 {
11377   tree link;
11378   tree formal_type = NULL;
11379   tree first_parm_type;
11380   tree arg;
11381
11382   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
11383     {
11384       arg = DECL_ARGUMENTS (function_or_method_type);
11385       function_or_method_type = TREE_TYPE (function_or_method_type);
11386     }
11387   else
11388     arg = NULL_TREE;
11389
11390   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
11391
11392   /* Make our first pass over the list of formal parameter types and output a
11393      DW_TAG_formal_parameter DIE for each one.  */
11394   for (link = first_parm_type; link; )
11395     {
11396       dw_die_ref parm_die;
11397
11398       formal_type = TREE_VALUE (link);
11399       if (formal_type == void_type_node)
11400         break;
11401
11402       /* Output a (nameless) DIE to represent the formal parameter itself.  */
11403       parm_die = gen_formal_parameter_die (formal_type, context_die);
11404       if ((TREE_CODE (function_or_method_type) == METHOD_TYPE
11405            && link == first_parm_type)
11406           || (arg && DECL_ARTIFICIAL (arg)))
11407         add_AT_flag (parm_die, DW_AT_artificial, 1);
11408
11409       link = TREE_CHAIN (link);
11410       if (arg)
11411         arg = TREE_CHAIN (arg);
11412     }
11413
11414   /* If this function type has an ellipsis, add a
11415      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
11416   if (formal_type != void_type_node)
11417     gen_unspecified_parameters_die (function_or_method_type, context_die);
11418
11419   /* Make our second (and final) pass over the list of formal parameter types
11420      and output DIEs to represent those types (as necessary).  */
11421   for (link = TYPE_ARG_TYPES (function_or_method_type);
11422        link && TREE_VALUE (link);
11423        link = TREE_CHAIN (link))
11424     gen_type_die (TREE_VALUE (link), context_die);
11425 }
11426
11427 /* We want to generate the DIE for TYPE so that we can generate the
11428    die for MEMBER, which has been defined; we will need to refer back
11429    to the member declaration nested within TYPE.  If we're trying to
11430    generate minimal debug info for TYPE, processing TYPE won't do the
11431    trick; we need to attach the member declaration by hand.  */
11432
11433 static void
11434 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
11435 {
11436   gen_type_die (type, context_die);
11437
11438   /* If we're trying to avoid duplicate debug info, we may not have
11439      emitted the member decl for this function.  Emit it now.  */
11440   if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
11441       && ! lookup_decl_die (member))
11442     {
11443       dw_die_ref type_die;
11444       gcc_assert (!decl_ultimate_origin (member));
11445
11446       push_decl_scope (type);
11447       type_die = lookup_type_die (type);
11448       if (TREE_CODE (member) == FUNCTION_DECL)
11449         gen_subprogram_die (member, type_die);
11450       else if (TREE_CODE (member) == FIELD_DECL)
11451         {
11452           /* Ignore the nameless fields that are used to skip bits but handle
11453              C++ anonymous unions and structs.  */
11454           if (DECL_NAME (member) != NULL_TREE
11455               || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
11456               || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
11457             {
11458               gen_type_die (member_declared_type (member), type_die);
11459               gen_field_die (member, type_die);
11460             }
11461         }
11462       else
11463         gen_variable_die (member, type_die);
11464
11465       pop_decl_scope ();
11466     }
11467 }
11468
11469 /* Generate the DWARF2 info for the "abstract" instance of a function which we
11470    may later generate inlined and/or out-of-line instances of.  */
11471
11472 static void
11473 dwarf2out_abstract_function (tree decl)
11474 {
11475   dw_die_ref old_die;
11476   tree save_fn;
11477   tree context;
11478   int was_abstract = DECL_ABSTRACT (decl);
11479
11480   /* Make sure we have the actual abstract inline, not a clone.  */
11481   decl = DECL_ORIGIN (decl);
11482
11483   old_die = lookup_decl_die (decl);
11484   if (old_die && get_AT (old_die, DW_AT_inline))
11485     /* We've already generated the abstract instance.  */
11486     return;
11487
11488   /* Be sure we've emitted the in-class declaration DIE (if any) first, so
11489      we don't get confused by DECL_ABSTRACT.  */
11490   if (debug_info_level > DINFO_LEVEL_TERSE)
11491     {
11492       context = decl_class_context (decl);
11493       if (context)
11494         gen_type_die_for_member
11495           (context, decl, decl_function_context (decl) ? NULL : comp_unit_die);
11496     }
11497
11498   /* Pretend we've just finished compiling this function.  */
11499   save_fn = current_function_decl;
11500   current_function_decl = decl;
11501
11502   set_decl_abstract_flags (decl, 1);
11503   dwarf2out_decl (decl);
11504   if (! was_abstract)
11505     set_decl_abstract_flags (decl, 0);
11506
11507   current_function_decl = save_fn;
11508 }
11509
11510 /* Generate a DIE to represent a declared function (either file-scope or
11511    block-local).  */
11512
11513 static void
11514 gen_subprogram_die (tree decl, dw_die_ref context_die)
11515 {
11516   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
11517   tree origin = decl_ultimate_origin (decl);
11518   dw_die_ref subr_die;
11519   tree fn_arg_types;
11520   tree outer_scope;
11521   dw_die_ref old_die = lookup_decl_die (decl);
11522   int declaration = (current_function_decl != decl
11523                      || class_or_namespace_scope_p (context_die));
11524
11525   /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
11526      started to generate the abstract instance of an inline, decided to output
11527      its containing class, and proceeded to emit the declaration of the inline
11528      from the member list for the class.  If so, DECLARATION takes priority;
11529      we'll get back to the abstract instance when done with the class.  */
11530
11531   /* The class-scope declaration DIE must be the primary DIE.  */
11532   if (origin && declaration && class_or_namespace_scope_p (context_die))
11533     {
11534       origin = NULL;
11535       gcc_assert (!old_die);
11536     }
11537
11538   /* Now that the C++ front end lazily declares artificial member fns, we
11539      might need to retrofit the declaration into its class.  */
11540   if (!declaration && !origin && !old_die
11541       && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
11542       && !class_or_namespace_scope_p (context_die)
11543       && debug_info_level > DINFO_LEVEL_TERSE)
11544     old_die = force_decl_die (decl);
11545
11546   if (origin != NULL)
11547     {
11548       gcc_assert (!declaration || local_scope_p (context_die));
11549
11550       /* Fixup die_parent for the abstract instance of a nested
11551          inline function.  */
11552       if (old_die && old_die->die_parent == NULL)
11553         add_child_die (context_die, old_die);
11554
11555       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
11556       add_abstract_origin_attribute (subr_die, origin);
11557     }
11558   else if (old_die)
11559     {
11560       expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
11561       unsigned file_index = lookup_filename (s.file);
11562
11563       if (!get_AT_flag (old_die, DW_AT_declaration)
11564           /* We can have a normal definition following an inline one in the
11565              case of redefinition of GNU C extern inlines.
11566              It seems reasonable to use AT_specification in this case.  */
11567           && !get_AT (old_die, DW_AT_inline))
11568         {
11569           /* Detect and ignore this case, where we are trying to output
11570              something we have already output.  */
11571           return;
11572         }
11573
11574       /* If the definition comes from the same place as the declaration,
11575          maybe use the old DIE.  We always want the DIE for this function
11576          that has the *_pc attributes to be under comp_unit_die so the
11577          debugger can find it.  We also need to do this for abstract
11578          instances of inlines, since the spec requires the out-of-line copy
11579          to have the same parent.  For local class methods, this doesn't
11580          apply; we just use the old DIE.  */
11581       if ((old_die->die_parent == comp_unit_die || context_die == NULL)
11582           && (DECL_ARTIFICIAL (decl)
11583               || (get_AT_unsigned (old_die, DW_AT_decl_file) == file_index
11584                   && (get_AT_unsigned (old_die, DW_AT_decl_line)
11585                       == (unsigned) s.line))))
11586         {
11587           subr_die = old_die;
11588
11589           /* Clear out the declaration attribute and the formal parameters.
11590              Do not remove all children, because it is possible that this
11591              declaration die was forced using force_decl_die(). In such
11592              cases die that forced declaration die (e.g. TAG_imported_module)
11593              is one of the children that we do not want to remove.  */
11594           remove_AT (subr_die, DW_AT_declaration);
11595           remove_child_TAG (subr_die, DW_TAG_formal_parameter);
11596         }
11597       else
11598         {
11599           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
11600           add_AT_specification (subr_die, old_die);
11601           if (get_AT_unsigned (old_die, DW_AT_decl_file) != file_index)
11602             add_AT_unsigned (subr_die, DW_AT_decl_file, file_index);
11603           if (get_AT_unsigned (old_die, DW_AT_decl_line)
11604               != (unsigned) s.line)
11605             add_AT_unsigned
11606               (subr_die, DW_AT_decl_line, s.line);
11607         }
11608     }
11609   else
11610     {
11611       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
11612
11613       if (TREE_PUBLIC (decl))
11614         add_AT_flag (subr_die, DW_AT_external, 1);
11615
11616       add_name_and_src_coords_attributes (subr_die, decl);
11617       if (debug_info_level > DINFO_LEVEL_TERSE)
11618         {
11619           add_prototyped_attribute (subr_die, TREE_TYPE (decl));
11620           add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
11621                               0, 0, context_die);
11622         }
11623
11624       add_pure_or_virtual_attribute (subr_die, decl);
11625       if (DECL_ARTIFICIAL (decl))
11626         add_AT_flag (subr_die, DW_AT_artificial, 1);
11627
11628       if (TREE_PROTECTED (decl))
11629         add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_protected);
11630       else if (TREE_PRIVATE (decl))
11631         add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_private);
11632     }
11633
11634   if (declaration)
11635     {
11636       if (!old_die || !get_AT (old_die, DW_AT_inline))
11637         {
11638           add_AT_flag (subr_die, DW_AT_declaration, 1);
11639
11640           /* The first time we see a member function, it is in the context of
11641              the class to which it belongs.  We make sure of this by emitting
11642              the class first.  The next time is the definition, which is
11643              handled above.  The two may come from the same source text.
11644
11645              Note that force_decl_die() forces function declaration die. It is
11646              later reused to represent definition.  */
11647           equate_decl_number_to_die (decl, subr_die);
11648         }
11649     }
11650   else if (DECL_ABSTRACT (decl))
11651     {
11652       if (DECL_DECLARED_INLINE_P (decl))
11653         {
11654           if (cgraph_function_possibly_inlined_p (decl))
11655             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
11656           else
11657             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
11658         }
11659       else
11660         {
11661           if (cgraph_function_possibly_inlined_p (decl))
11662             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
11663           else
11664             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
11665         }
11666
11667       equate_decl_number_to_die (decl, subr_die);
11668     }
11669   else if (!DECL_EXTERNAL (decl))
11670     {
11671       if (!old_die || !get_AT (old_die, DW_AT_inline))
11672         equate_decl_number_to_die (decl, subr_die);
11673
11674       if (!flag_reorder_blocks_and_partition)
11675         {
11676           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
11677                                        current_function_funcdef_no);
11678           add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
11679           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
11680                                        current_function_funcdef_no);
11681           add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
11682           
11683           add_pubname (decl, subr_die);
11684           add_arange (decl, subr_die);
11685         }
11686       else
11687         {  /* Do nothing for now; maybe need to duplicate die, one for
11688               hot section and ond for cold section, then use the hot/cold
11689               section begin/end labels to generate the aranges...  */
11690           /*
11691             add_AT_lbl_id (subr_die, DW_AT_low_pc, hot_section_label);
11692             add_AT_lbl_id (subr_die, DW_AT_high_pc, hot_section_end_label);
11693             add_AT_lbl_id (subr_die, DW_AT_lo_user, unlikely_section_label);
11694             add_AT_lbl_id (subr_die, DW_AT_hi_user, cold_section_end_label);
11695
11696             add_pubname (decl, subr_die);
11697             add_arange (decl, subr_die);
11698             add_arange (decl, subr_die);
11699            */
11700         }
11701
11702 #ifdef MIPS_DEBUGGING_INFO
11703       /* Add a reference to the FDE for this routine.  */
11704       add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
11705 #endif
11706
11707       /* We define the "frame base" as the function's CFA.  This is more
11708          convenient for several reasons: (1) It's stable across the prologue
11709          and epilogue, which makes it better than just a frame pointer,
11710          (2) With dwarf3, there exists a one-byte encoding that allows us
11711          to reference the .debug_frame data by proxy, but failing that,
11712          (3) We can at least reuse the code inspection and interpretation
11713          code that determines the CFA position at various points in the
11714          function.  */
11715       /* ??? Use some command-line or configury switch to enable the use
11716          of dwarf3 DW_OP_call_frame_cfa.  At present there are no dwarf
11717          consumers that understand it; fall back to "pure" dwarf2 and
11718          convert the CFA data into a location list.  */
11719       {
11720         dw_loc_list_ref list = convert_cfa_to_loc_list ();
11721         if (list->dw_loc_next)
11722           add_AT_loc_list (subr_die, DW_AT_frame_base, list);
11723         else
11724           add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
11725       }
11726
11727       /* Compute a displacement from the "steady-state frame pointer" to
11728          the CFA.  The former is what all stack slots and argument slots
11729          will reference in the rtl; the later is what we've told the 
11730          debugger about.  We'll need to adjust all frame_base references
11731          by this displacement.  */
11732       compute_frame_pointer_to_cfa_displacement ();
11733
11734       if (cfun->static_chain_decl)
11735         add_AT_location_description (subr_die, DW_AT_static_link,
11736                  loc_descriptor_from_tree (cfun->static_chain_decl));
11737     }
11738
11739   /* Now output descriptions of the arguments for this function. This gets
11740      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
11741      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
11742      `...' at the end of the formal parameter list.  In order to find out if
11743      there was a trailing ellipsis or not, we must instead look at the type
11744      associated with the FUNCTION_DECL.  This will be a node of type
11745      FUNCTION_TYPE. If the chain of type nodes hanging off of this
11746      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
11747      an ellipsis at the end.  */
11748
11749   /* In the case where we are describing a mere function declaration, all we
11750      need to do here (and all we *can* do here) is to describe the *types* of
11751      its formal parameters.  */
11752   if (debug_info_level <= DINFO_LEVEL_TERSE)
11753     ;
11754   else if (declaration)
11755     gen_formal_types_die (decl, subr_die);
11756   else
11757     {
11758       /* Generate DIEs to represent all known formal parameters.  */
11759       tree arg_decls = DECL_ARGUMENTS (decl);
11760       tree parm;
11761
11762       /* When generating DIEs, generate the unspecified_parameters DIE
11763          instead if we come across the arg "__builtin_va_alist" */
11764       for (parm = arg_decls; parm; parm = TREE_CHAIN (parm))
11765         if (TREE_CODE (parm) == PARM_DECL)
11766           {
11767             if (DECL_NAME (parm)
11768                 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (parm)),
11769                             "__builtin_va_alist"))
11770               gen_unspecified_parameters_die (parm, subr_die);
11771             else
11772               gen_decl_die (parm, subr_die);
11773           }
11774
11775       /* Decide whether we need an unspecified_parameters DIE at the end.
11776          There are 2 more cases to do this for: 1) the ansi ... declaration -
11777          this is detectable when the end of the arg list is not a
11778          void_type_node 2) an unprototyped function declaration (not a
11779          definition).  This just means that we have no info about the
11780          parameters at all.  */
11781       fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
11782       if (fn_arg_types != NULL)
11783         {
11784           /* This is the prototyped case, check for....  */
11785           if (TREE_VALUE (tree_last (fn_arg_types)) != void_type_node)
11786             gen_unspecified_parameters_die (decl, subr_die);
11787         }
11788       else if (DECL_INITIAL (decl) == NULL_TREE)
11789         gen_unspecified_parameters_die (decl, subr_die);
11790     }
11791
11792   /* Output Dwarf info for all of the stuff within the body of the function
11793      (if it has one - it may be just a declaration).  */
11794   outer_scope = DECL_INITIAL (decl);
11795
11796   /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
11797      a function.  This BLOCK actually represents the outermost binding contour
11798      for the function, i.e. the contour in which the function's formal
11799      parameters and labels get declared. Curiously, it appears that the front
11800      end doesn't actually put the PARM_DECL nodes for the current function onto
11801      the BLOCK_VARS list for this outer scope, but are strung off of the
11802      DECL_ARGUMENTS list for the function instead.
11803
11804      The BLOCK_VARS list for the `outer_scope' does provide us with a list of
11805      the LABEL_DECL nodes for the function however, and we output DWARF info
11806      for those in decls_for_scope.  Just within the `outer_scope' there will be
11807      a BLOCK node representing the function's outermost pair of curly braces,
11808      and any blocks used for the base and member initializers of a C++
11809      constructor function.  */
11810   if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
11811     {
11812       /* Emit a DW_TAG_variable DIE for a named return value.  */
11813       if (DECL_NAME (DECL_RESULT (decl)))
11814         gen_decl_die (DECL_RESULT (decl), subr_die);
11815
11816       current_function_has_inlines = 0;
11817       decls_for_scope (outer_scope, subr_die, 0);
11818
11819 #if 0 && defined (MIPS_DEBUGGING_INFO)
11820       if (current_function_has_inlines)
11821         {
11822           add_AT_flag (subr_die, DW_AT_MIPS_has_inlines, 1);
11823           if (! comp_unit_has_inlines)
11824             {
11825               add_AT_flag (comp_unit_die, DW_AT_MIPS_has_inlines, 1);
11826               comp_unit_has_inlines = 1;
11827             }
11828         }
11829 #endif
11830     }
11831   /* Add the calling convention attribute if requested.  */
11832   add_calling_convention_attribute (subr_die, TREE_TYPE (decl));
11833
11834 }
11835
11836 /* Generate a DIE to represent a declared data object.  */
11837
11838 static void
11839 gen_variable_die (tree decl, dw_die_ref context_die)
11840 {
11841   tree origin = decl_ultimate_origin (decl);
11842   dw_die_ref var_die = new_die (DW_TAG_variable, context_die, decl);
11843
11844   dw_die_ref old_die = lookup_decl_die (decl);
11845   int declaration = (DECL_EXTERNAL (decl)
11846                      /* If DECL is COMDAT and has not actually been
11847                         emitted, we cannot take its address; there
11848                         might end up being no definition anywhere in
11849                         the program.  For example, consider the C++
11850                         test case:
11851
11852                           template <class T>
11853                           struct S { static const int i = 7; };
11854
11855                           template <class T>
11856                           const int S<T>::i;
11857
11858                           int f() { return S<int>::i; }
11859                           
11860                         Here, S<int>::i is not DECL_EXTERNAL, but no
11861                         definition is required, so the compiler will
11862                         not emit a definition.  */  
11863                      || (TREE_CODE (decl) == VAR_DECL
11864                          && DECL_COMDAT (decl) && !TREE_ASM_WRITTEN (decl))
11865                      || class_or_namespace_scope_p (context_die));
11866
11867   if (origin != NULL)
11868     add_abstract_origin_attribute (var_die, origin);
11869
11870   /* Loop unrolling can create multiple blocks that refer to the same
11871      static variable, so we must test for the DW_AT_declaration flag.
11872
11873      ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
11874      copy decls and set the DECL_ABSTRACT flag on them instead of
11875      sharing them.
11876
11877      ??? Duplicated blocks have been rewritten to use .debug_ranges.
11878
11879      ??? The declare_in_namespace support causes us to get two DIEs for one
11880      variable, both of which are declarations.  We want to avoid considering
11881      one to be a specification, so we must test that this DIE is not a
11882      declaration.  */
11883   else if (old_die && TREE_STATIC (decl) && ! declaration
11884            && get_AT_flag (old_die, DW_AT_declaration) == 1)
11885     {
11886       /* This is a definition of a C++ class level static.  */
11887       add_AT_specification (var_die, old_die);
11888       if (DECL_NAME (decl))
11889         {
11890           expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
11891           unsigned file_index = lookup_filename (s.file);
11892
11893           if (get_AT_unsigned (old_die, DW_AT_decl_file) != file_index)
11894             add_AT_unsigned (var_die, DW_AT_decl_file, file_index);
11895
11896           if (get_AT_unsigned (old_die, DW_AT_decl_line)
11897               != (unsigned) s.line)
11898
11899             add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
11900         }
11901     }
11902   else
11903     {
11904       add_name_and_src_coords_attributes (var_die, decl);
11905       add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
11906                           TREE_THIS_VOLATILE (decl), context_die);
11907
11908       if (TREE_PUBLIC (decl))
11909         add_AT_flag (var_die, DW_AT_external, 1);
11910
11911       if (DECL_ARTIFICIAL (decl))
11912         add_AT_flag (var_die, DW_AT_artificial, 1);
11913
11914       if (TREE_PROTECTED (decl))
11915         add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_protected);
11916       else if (TREE_PRIVATE (decl))
11917         add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_private);
11918     }
11919
11920   if (declaration)
11921     add_AT_flag (var_die, DW_AT_declaration, 1);
11922
11923   if (DECL_ABSTRACT (decl) || declaration)
11924     equate_decl_number_to_die (decl, var_die);
11925
11926   if (! declaration && ! DECL_ABSTRACT (decl))
11927     {
11928       add_location_or_const_value_attribute (var_die, decl, DW_AT_location);
11929       add_pubname (decl, var_die);
11930     }
11931   else
11932     tree_add_const_value_attribute (var_die, decl);
11933 }
11934
11935 /* Generate a DIE to represent a label identifier.  */
11936
11937 static void
11938 gen_label_die (tree decl, dw_die_ref context_die)
11939 {
11940   tree origin = decl_ultimate_origin (decl);
11941   dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
11942   rtx insn;
11943   char label[MAX_ARTIFICIAL_LABEL_BYTES];
11944
11945   if (origin != NULL)
11946     add_abstract_origin_attribute (lbl_die, origin);
11947   else
11948     add_name_and_src_coords_attributes (lbl_die, decl);
11949
11950   if (DECL_ABSTRACT (decl))
11951     equate_decl_number_to_die (decl, lbl_die);
11952   else
11953     {
11954       insn = DECL_RTL_IF_SET (decl);
11955
11956       /* Deleted labels are programmer specified labels which have been
11957          eliminated because of various optimizations.  We still emit them
11958          here so that it is possible to put breakpoints on them.  */
11959       if (insn
11960           && (LABEL_P (insn)
11961               || ((NOTE_P (insn)
11962                    && NOTE_LINE_NUMBER (insn) == NOTE_INSN_DELETED_LABEL))))
11963         {
11964           /* When optimization is enabled (via -O) some parts of the compiler
11965              (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
11966              represent source-level labels which were explicitly declared by
11967              the user.  This really shouldn't be happening though, so catch
11968              it if it ever does happen.  */
11969           gcc_assert (!INSN_DELETED_P (insn));
11970
11971           ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
11972           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
11973         }
11974     }
11975 }
11976
11977 /* A helper function for gen_inlined_subroutine_die.  Add source coordinate
11978    attributes to the DIE for a block STMT, to describe where the inlined
11979    function was called from.  This is similar to add_src_coords_attributes.  */
11980
11981 static inline void
11982 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
11983 {
11984   expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
11985   unsigned file_index = lookup_filename (s.file);
11986
11987   add_AT_unsigned (die, DW_AT_call_file, file_index);
11988   add_AT_unsigned (die, DW_AT_call_line, s.line);
11989 }
11990
11991 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
11992    Add low_pc and high_pc attributes to the DIE for a block STMT.  */
11993
11994 static inline void
11995 add_high_low_attributes (tree stmt, dw_die_ref die)
11996 {
11997   char label[MAX_ARTIFICIAL_LABEL_BYTES];
11998
11999   if (BLOCK_FRAGMENT_CHAIN (stmt))
12000     {
12001       tree chain;
12002
12003       add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
12004
12005       chain = BLOCK_FRAGMENT_CHAIN (stmt);
12006       do
12007         {
12008           add_ranges (chain);
12009           chain = BLOCK_FRAGMENT_CHAIN (chain);
12010         }
12011       while (chain);
12012       add_ranges (NULL);
12013     }
12014   else
12015     {
12016       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
12017                                    BLOCK_NUMBER (stmt));
12018       add_AT_lbl_id (die, DW_AT_low_pc, label);
12019       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
12020                                    BLOCK_NUMBER (stmt));
12021       add_AT_lbl_id (die, DW_AT_high_pc, label);
12022     }
12023 }
12024
12025 /* Generate a DIE for a lexical block.  */
12026
12027 static void
12028 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
12029 {
12030   dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
12031
12032   if (! BLOCK_ABSTRACT (stmt))
12033     add_high_low_attributes (stmt, stmt_die);
12034
12035   decls_for_scope (stmt, stmt_die, depth);
12036 }
12037
12038 /* Generate a DIE for an inlined subprogram.  */
12039
12040 static void
12041 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
12042 {
12043   tree decl = block_ultimate_origin (stmt);
12044
12045   /* Emit info for the abstract instance first, if we haven't yet.  We
12046      must emit this even if the block is abstract, otherwise when we
12047      emit the block below (or elsewhere), we may end up trying to emit
12048      a die whose origin die hasn't been emitted, and crashing.  */
12049   dwarf2out_abstract_function (decl);
12050
12051   if (! BLOCK_ABSTRACT (stmt))
12052     {
12053       dw_die_ref subr_die
12054         = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
12055
12056       add_abstract_origin_attribute (subr_die, decl);
12057       add_high_low_attributes (stmt, subr_die);
12058       add_call_src_coords_attributes (stmt, subr_die);
12059
12060       decls_for_scope (stmt, subr_die, depth);
12061       current_function_has_inlines = 1;
12062     }
12063   else
12064     /* We may get here if we're the outer block of function A that was
12065        inlined into function B that was inlined into function C.  When
12066        generating debugging info for C, dwarf2out_abstract_function(B)
12067        would mark all inlined blocks as abstract, including this one.
12068        So, we wouldn't (and shouldn't) expect labels to be generated
12069        for this one.  Instead, just emit debugging info for
12070        declarations within the block.  This is particularly important
12071        in the case of initializers of arguments passed from B to us:
12072        if they're statement expressions containing declarations, we
12073        wouldn't generate dies for their abstract variables, and then,
12074        when generating dies for the real variables, we'd die (pun
12075        intended :-)  */
12076     gen_lexical_block_die (stmt, context_die, depth);
12077 }
12078
12079 /* Generate a DIE for a field in a record, or structure.  */
12080
12081 static void
12082 gen_field_die (tree decl, dw_die_ref context_die)
12083 {
12084   dw_die_ref decl_die;
12085
12086   if (TREE_TYPE (decl) == error_mark_node)
12087     return;
12088
12089   decl_die = new_die (DW_TAG_member, context_die, decl);
12090   add_name_and_src_coords_attributes (decl_die, decl);
12091   add_type_attribute (decl_die, member_declared_type (decl),
12092                       TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
12093                       context_die);
12094
12095   if (DECL_BIT_FIELD_TYPE (decl))
12096     {
12097       add_byte_size_attribute (decl_die, decl);
12098       add_bit_size_attribute (decl_die, decl);
12099       add_bit_offset_attribute (decl_die, decl);
12100     }
12101
12102   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
12103     add_data_member_location_attribute (decl_die, decl);
12104
12105   if (DECL_ARTIFICIAL (decl))
12106     add_AT_flag (decl_die, DW_AT_artificial, 1);
12107
12108   if (TREE_PROTECTED (decl))
12109     add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_protected);
12110   else if (TREE_PRIVATE (decl))
12111     add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_private);
12112
12113   /* Equate decl number to die, so that we can look up this decl later on.  */
12114   equate_decl_number_to_die (decl, decl_die);
12115 }
12116
12117 #if 0
12118 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
12119    Use modified_type_die instead.
12120    We keep this code here just in case these types of DIEs may be needed to
12121    represent certain things in other languages (e.g. Pascal) someday.  */
12122
12123 static void
12124 gen_pointer_type_die (tree type, dw_die_ref context_die)
12125 {
12126   dw_die_ref ptr_die
12127     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
12128
12129   equate_type_number_to_die (type, ptr_die);
12130   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
12131   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
12132 }
12133
12134 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
12135    Use modified_type_die instead.
12136    We keep this code here just in case these types of DIEs may be needed to
12137    represent certain things in other languages (e.g. Pascal) someday.  */
12138
12139 static void
12140 gen_reference_type_die (tree type, dw_die_ref context_die)
12141 {
12142   dw_die_ref ref_die
12143     = new_die (DW_TAG_reference_type, scope_die_for (type, context_die), type);
12144
12145   equate_type_number_to_die (type, ref_die);
12146   add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
12147   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
12148 }
12149 #endif
12150
12151 /* Generate a DIE for a pointer to a member type.  */
12152
12153 static void
12154 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
12155 {
12156   dw_die_ref ptr_die
12157     = new_die (DW_TAG_ptr_to_member_type,
12158                scope_die_for (type, context_die), type);
12159
12160   equate_type_number_to_die (type, ptr_die);
12161   add_AT_die_ref (ptr_die, DW_AT_containing_type,
12162                   lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
12163   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
12164 }
12165
12166 /* Generate the DIE for the compilation unit.  */
12167
12168 static dw_die_ref
12169 gen_compile_unit_die (const char *filename)
12170 {
12171   dw_die_ref die;
12172   char producer[250];
12173   const char *language_string = lang_hooks.name;
12174   int language;
12175
12176   die = new_die (DW_TAG_compile_unit, NULL, NULL);
12177
12178   if (filename)
12179     {
12180       add_name_attribute (die, filename);
12181       /* Don't add cwd for <built-in>.  */
12182       if (filename[0] != DIR_SEPARATOR && filename[0] != '<')
12183         add_comp_dir_attribute (die);
12184     }
12185
12186   sprintf (producer, "%s %s", language_string, version_string);
12187
12188 #ifdef MIPS_DEBUGGING_INFO
12189   /* The MIPS/SGI compilers place the 'cc' command line options in the producer
12190      string.  The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
12191      not appear in the producer string, the debugger reaches the conclusion
12192      that the object file is stripped and has no debugging information.
12193      To get the MIPS/SGI debugger to believe that there is debugging
12194      information in the object file, we add a -g to the producer string.  */
12195   if (debug_info_level > DINFO_LEVEL_TERSE)
12196     strcat (producer, " -g");
12197 #endif
12198
12199   add_AT_string (die, DW_AT_producer, producer);
12200
12201   if (strcmp (language_string, "GNU C++") == 0)
12202     language = DW_LANG_C_plus_plus;
12203   else if (strcmp (language_string, "GNU Ada") == 0)
12204     language = DW_LANG_Ada95;
12205   else if (strcmp (language_string, "GNU F77") == 0)
12206     language = DW_LANG_Fortran77;
12207   else if (strcmp (language_string, "GNU F95") == 0)
12208     language = DW_LANG_Fortran95;
12209   else if (strcmp (language_string, "GNU Pascal") == 0)
12210     language = DW_LANG_Pascal83;
12211   else if (strcmp (language_string, "GNU Java") == 0)
12212     language = DW_LANG_Java;
12213   else
12214     language = DW_LANG_C89;
12215
12216   add_AT_unsigned (die, DW_AT_language, language);
12217   return die;
12218 }
12219
12220 /* Generate a DIE for a string type.  */
12221
12222 static void
12223 gen_string_type_die (tree type, dw_die_ref context_die)
12224 {
12225   dw_die_ref type_die
12226     = new_die (DW_TAG_string_type, scope_die_for (type, context_die), type);
12227
12228   equate_type_number_to_die (type, type_die);
12229
12230   /* ??? Fudge the string length attribute for now.
12231      TODO: add string length info.  */
12232 #if 0
12233   string_length_attribute (TYPE_MAX_VALUE (TYPE_DOMAIN (type)));
12234   bound_representation (upper_bound, 0, 'u');
12235 #endif
12236 }
12237
12238 /* Generate the DIE for a base class.  */
12239
12240 static void
12241 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
12242 {
12243   dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
12244
12245   add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
12246   add_data_member_location_attribute (die, binfo);
12247
12248   if (BINFO_VIRTUAL_P (binfo))
12249     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
12250
12251   if (access == access_public_node)
12252     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
12253   else if (access == access_protected_node)
12254     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
12255 }
12256
12257 /* Generate a DIE for a class member.  */
12258
12259 static void
12260 gen_member_die (tree type, dw_die_ref context_die)
12261 {
12262   tree member;
12263   tree binfo = TYPE_BINFO (type);
12264   dw_die_ref child;
12265
12266   /* If this is not an incomplete type, output descriptions of each of its
12267      members. Note that as we output the DIEs necessary to represent the
12268      members of this record or union type, we will also be trying to output
12269      DIEs to represent the *types* of those members. However the `type'
12270      function (above) will specifically avoid generating type DIEs for member
12271      types *within* the list of member DIEs for this (containing) type except
12272      for those types (of members) which are explicitly marked as also being
12273      members of this (containing) type themselves.  The g++ front- end can
12274      force any given type to be treated as a member of some other (containing)
12275      type by setting the TYPE_CONTEXT of the given (member) type to point to
12276      the TREE node representing the appropriate (containing) type.  */
12277
12278   /* First output info about the base classes.  */
12279   if (binfo)
12280     {
12281       VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
12282       int i;
12283       tree base;
12284
12285       for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
12286         gen_inheritance_die (base,
12287                              (accesses ? VEC_index (tree, accesses, i)
12288                               : access_public_node), context_die);
12289     }
12290
12291   /* Now output info about the data members and type members.  */
12292   for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member))
12293     {
12294       /* If we thought we were generating minimal debug info for TYPE
12295          and then changed our minds, some of the member declarations
12296          may have already been defined.  Don't define them again, but
12297          do put them in the right order.  */
12298
12299       child = lookup_decl_die (member);
12300       if (child)
12301         splice_child_die (context_die, child);
12302       else
12303         gen_decl_die (member, context_die);
12304     }
12305
12306   /* Now output info about the function members (if any).  */
12307   for (member = TYPE_METHODS (type); member; member = TREE_CHAIN (member))
12308     {
12309       /* Don't include clones in the member list.  */
12310       if (DECL_ABSTRACT_ORIGIN (member))
12311         continue;
12312
12313       child = lookup_decl_die (member);
12314       if (child)
12315         splice_child_die (context_die, child);
12316       else
12317         gen_decl_die (member, context_die);
12318     }
12319 }
12320
12321 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
12322    is set, we pretend that the type was never defined, so we only get the
12323    member DIEs needed by later specification DIEs.  */
12324
12325 static void
12326 gen_struct_or_union_type_die (tree type, dw_die_ref context_die)
12327 {
12328   dw_die_ref type_die = lookup_type_die (type);
12329   dw_die_ref scope_die = 0;
12330   int nested = 0;
12331   int complete = (TYPE_SIZE (type)
12332                   && (! TYPE_STUB_DECL (type)
12333                       || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
12334   int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
12335
12336   if (type_die && ! complete)
12337     return;
12338
12339   if (TYPE_CONTEXT (type) != NULL_TREE
12340       && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
12341           || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
12342     nested = 1;
12343
12344   scope_die = scope_die_for (type, context_die);
12345
12346   if (! type_die || (nested && scope_die == comp_unit_die))
12347     /* First occurrence of type or toplevel definition of nested class.  */
12348     {
12349       dw_die_ref old_die = type_die;
12350
12351       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
12352                           ? DW_TAG_structure_type : DW_TAG_union_type,
12353                           scope_die, type);
12354       equate_type_number_to_die (type, type_die);
12355       if (old_die)
12356         add_AT_specification (type_die, old_die);
12357       else
12358         add_name_attribute (type_die, type_tag (type));
12359     }
12360   else
12361     remove_AT (type_die, DW_AT_declaration);
12362
12363   /* If this type has been completed, then give it a byte_size attribute and
12364      then give a list of members.  */
12365   if (complete && !ns_decl)
12366     {
12367       /* Prevent infinite recursion in cases where the type of some member of
12368          this type is expressed in terms of this type itself.  */
12369       TREE_ASM_WRITTEN (type) = 1;
12370       add_byte_size_attribute (type_die, type);
12371       if (TYPE_STUB_DECL (type) != NULL_TREE)
12372         add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
12373
12374       /* If the first reference to this type was as the return type of an
12375          inline function, then it may not have a parent.  Fix this now.  */
12376       if (type_die->die_parent == NULL)
12377         add_child_die (scope_die, type_die);
12378
12379       push_decl_scope (type);
12380       gen_member_die (type, type_die);
12381       pop_decl_scope ();
12382
12383       /* GNU extension: Record what type our vtable lives in.  */
12384       if (TYPE_VFIELD (type))
12385         {
12386           tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
12387
12388           gen_type_die (vtype, context_die);
12389           add_AT_die_ref (type_die, DW_AT_containing_type,
12390                           lookup_type_die (vtype));
12391         }
12392     }
12393   else
12394     {
12395       add_AT_flag (type_die, DW_AT_declaration, 1);
12396
12397       /* We don't need to do this for function-local types.  */
12398       if (TYPE_STUB_DECL (type)
12399           && ! decl_function_context (TYPE_STUB_DECL (type)))
12400         VEC_safe_push (tree, gc, incomplete_types, type);
12401     }
12402 }
12403
12404 /* Generate a DIE for a subroutine _type_.  */
12405
12406 static void
12407 gen_subroutine_type_die (tree type, dw_die_ref context_die)
12408 {
12409   tree return_type = TREE_TYPE (type);
12410   dw_die_ref subr_die
12411     = new_die (DW_TAG_subroutine_type,
12412                scope_die_for (type, context_die), type);
12413
12414   equate_type_number_to_die (type, subr_die);
12415   add_prototyped_attribute (subr_die, type);
12416   add_type_attribute (subr_die, return_type, 0, 0, context_die);
12417   gen_formal_types_die (type, subr_die);
12418 }
12419
12420 /* Generate a DIE for a type definition.  */
12421
12422 static void
12423 gen_typedef_die (tree decl, dw_die_ref context_die)
12424 {
12425   dw_die_ref type_die;
12426   tree origin;
12427
12428   if (TREE_ASM_WRITTEN (decl))
12429     return;
12430
12431   TREE_ASM_WRITTEN (decl) = 1;
12432   type_die = new_die (DW_TAG_typedef, context_die, decl);
12433   origin = decl_ultimate_origin (decl);
12434   if (origin != NULL)
12435     add_abstract_origin_attribute (type_die, origin);
12436   else
12437     {
12438       tree type;
12439
12440       add_name_and_src_coords_attributes (type_die, decl);
12441       if (DECL_ORIGINAL_TYPE (decl))
12442         {
12443           type = DECL_ORIGINAL_TYPE (decl);
12444
12445           gcc_assert (type != TREE_TYPE (decl));
12446           equate_type_number_to_die (TREE_TYPE (decl), type_die);
12447         }
12448       else
12449         type = TREE_TYPE (decl);
12450
12451       add_type_attribute (type_die, type, TREE_READONLY (decl),
12452                           TREE_THIS_VOLATILE (decl), context_die);
12453     }
12454
12455   if (DECL_ABSTRACT (decl))
12456     equate_decl_number_to_die (decl, type_die);
12457 }
12458
12459 /* Generate a type description DIE.  */
12460
12461 static void
12462 gen_type_die (tree type, dw_die_ref context_die)
12463 {
12464   int need_pop;
12465
12466   if (type == NULL_TREE || type == error_mark_node)
12467     return;
12468
12469   if (TYPE_NAME (type) && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12470       && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
12471     {
12472       if (TREE_ASM_WRITTEN (type))
12473         return;
12474
12475       /* Prevent broken recursion; we can't hand off to the same type.  */
12476       gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
12477
12478       TREE_ASM_WRITTEN (type) = 1;
12479       gen_decl_die (TYPE_NAME (type), context_die);
12480       return;
12481     }
12482
12483   /* We are going to output a DIE to represent the unqualified version
12484      of this type (i.e. without any const or volatile qualifiers) so
12485      get the main variant (i.e. the unqualified version) of this type
12486      now.  (Vectors are special because the debugging info is in the
12487      cloned type itself).  */
12488   if (TREE_CODE (type) != VECTOR_TYPE)
12489     type = type_main_variant (type);
12490
12491   if (TREE_ASM_WRITTEN (type))
12492     return;
12493
12494   switch (TREE_CODE (type))
12495     {
12496     case ERROR_MARK:
12497       break;
12498
12499     case POINTER_TYPE:
12500     case REFERENCE_TYPE:
12501       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
12502          ensures that the gen_type_die recursion will terminate even if the
12503          type is recursive.  Recursive types are possible in Ada.  */
12504       /* ??? We could perhaps do this for all types before the switch
12505          statement.  */
12506       TREE_ASM_WRITTEN (type) = 1;
12507
12508       /* For these types, all that is required is that we output a DIE (or a
12509          set of DIEs) to represent the "basis" type.  */
12510       gen_type_die (TREE_TYPE (type), context_die);
12511       break;
12512
12513     case OFFSET_TYPE:
12514       /* This code is used for C++ pointer-to-data-member types.
12515          Output a description of the relevant class type.  */
12516       gen_type_die (TYPE_OFFSET_BASETYPE (type), context_die);
12517
12518       /* Output a description of the type of the object pointed to.  */
12519       gen_type_die (TREE_TYPE (type), context_die);
12520
12521       /* Now output a DIE to represent this pointer-to-data-member type
12522          itself.  */
12523       gen_ptr_to_mbr_type_die (type, context_die);
12524       break;
12525
12526     case FUNCTION_TYPE:
12527       /* Force out return type (in case it wasn't forced out already).  */
12528       gen_type_die (TREE_TYPE (type), context_die);
12529       gen_subroutine_type_die (type, context_die);
12530       break;
12531
12532     case METHOD_TYPE:
12533       /* Force out return type (in case it wasn't forced out already).  */
12534       gen_type_die (TREE_TYPE (type), context_die);
12535       gen_subroutine_type_die (type, context_die);
12536       break;
12537
12538     case ARRAY_TYPE:
12539       if (TYPE_STRING_FLAG (type) && TREE_CODE (TREE_TYPE (type)) == CHAR_TYPE)
12540         {
12541           gen_type_die (TREE_TYPE (type), context_die);
12542           gen_string_type_die (type, context_die);
12543         }
12544       else
12545         gen_array_type_die (type, context_die);
12546       break;
12547
12548     case VECTOR_TYPE:
12549       gen_array_type_die (type, context_die);
12550       break;
12551
12552     case ENUMERAL_TYPE:
12553     case RECORD_TYPE:
12554     case UNION_TYPE:
12555     case QUAL_UNION_TYPE:
12556       /* If this is a nested type whose containing class hasn't been written
12557          out yet, writing it out will cover this one, too.  This does not apply
12558          to instantiations of member class templates; they need to be added to
12559          the containing class as they are generated.  FIXME: This hurts the
12560          idea of combining type decls from multiple TUs, since we can't predict
12561          what set of template instantiations we'll get.  */
12562       if (TYPE_CONTEXT (type)
12563           && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
12564           && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
12565         {
12566           gen_type_die (TYPE_CONTEXT (type), context_die);
12567
12568           if (TREE_ASM_WRITTEN (type))
12569             return;
12570
12571           /* If that failed, attach ourselves to the stub.  */
12572           push_decl_scope (TYPE_CONTEXT (type));
12573           context_die = lookup_type_die (TYPE_CONTEXT (type));
12574           need_pop = 1;
12575         }
12576       else
12577         {
12578           declare_in_namespace (type, context_die);
12579           need_pop = 0;
12580         }
12581
12582       if (TREE_CODE (type) == ENUMERAL_TYPE)
12583         {
12584           /* This might have been written out by the call to
12585              declare_in_namespace.  */
12586           if (!TREE_ASM_WRITTEN (type))
12587             gen_enumeration_type_die (type, context_die);
12588         }
12589       else
12590         gen_struct_or_union_type_die (type, context_die);
12591
12592       if (need_pop)
12593         pop_decl_scope ();
12594
12595       /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
12596          it up if it is ever completed.  gen_*_type_die will set it for us
12597          when appropriate.  */
12598       return;
12599
12600     case VOID_TYPE:
12601     case INTEGER_TYPE:
12602     case REAL_TYPE:
12603     case COMPLEX_TYPE:
12604     case BOOLEAN_TYPE:
12605     case CHAR_TYPE:
12606       /* No DIEs needed for fundamental types.  */
12607       break;
12608
12609     case LANG_TYPE:
12610       /* No Dwarf representation currently defined.  */
12611       break;
12612
12613     default:
12614       gcc_unreachable ();
12615     }
12616
12617   TREE_ASM_WRITTEN (type) = 1;
12618 }
12619
12620 /* Generate a DIE for a tagged type instantiation.  */
12621
12622 static void
12623 gen_tagged_type_instantiation_die (tree type, dw_die_ref context_die)
12624 {
12625   if (type == NULL_TREE || type == error_mark_node)
12626     return;
12627
12628   /* We are going to output a DIE to represent the unqualified version of
12629      this type (i.e. without any const or volatile qualifiers) so make sure
12630      that we have the main variant (i.e. the unqualified version) of this
12631      type now.  */
12632   gcc_assert (type == type_main_variant (type));
12633
12634   /* Do not check TREE_ASM_WRITTEN (type) as it may not be set if this is
12635      an instance of an unresolved type.  */
12636
12637   switch (TREE_CODE (type))
12638     {
12639     case ERROR_MARK:
12640       break;
12641
12642     case ENUMERAL_TYPE:
12643       gen_inlined_enumeration_type_die (type, context_die);
12644       break;
12645
12646     case RECORD_TYPE:
12647       gen_inlined_structure_type_die (type, context_die);
12648       break;
12649
12650     case UNION_TYPE:
12651     case QUAL_UNION_TYPE:
12652       gen_inlined_union_type_die (type, context_die);
12653       break;
12654
12655     default:
12656       gcc_unreachable ();
12657     }
12658 }
12659
12660 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
12661    things which are local to the given block.  */
12662
12663 static void
12664 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
12665 {
12666   int must_output_die = 0;
12667   tree origin;
12668   tree decl;
12669   enum tree_code origin_code;
12670
12671   /* Ignore blocks that are NULL.  */
12672   if (stmt == NULL_TREE)
12673     return;
12674
12675   /* If the block is one fragment of a non-contiguous block, do not
12676      process the variables, since they will have been done by the
12677      origin block.  Do process subblocks.  */
12678   if (BLOCK_FRAGMENT_ORIGIN (stmt))
12679     {
12680       tree sub;
12681
12682       for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
12683         gen_block_die (sub, context_die, depth + 1);
12684
12685       return;
12686     }
12687
12688   /* Determine the "ultimate origin" of this block.  This block may be an
12689      inlined instance of an inlined instance of inline function, so we have
12690      to trace all of the way back through the origin chain to find out what
12691      sort of node actually served as the original seed for the creation of
12692      the current block.  */
12693   origin = block_ultimate_origin (stmt);
12694   origin_code = (origin != NULL) ? TREE_CODE (origin) : ERROR_MARK;
12695
12696   /* Determine if we need to output any Dwarf DIEs at all to represent this
12697      block.  */
12698   if (origin_code == FUNCTION_DECL)
12699     /* The outer scopes for inlinings *must* always be represented.  We
12700        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
12701     must_output_die = 1;
12702   else
12703     {
12704       /* In the case where the current block represents an inlining of the
12705          "body block" of an inline function, we must *NOT* output any DIE for
12706          this block because we have already output a DIE to represent the whole
12707          inlined function scope and the "body block" of any function doesn't
12708          really represent a different scope according to ANSI C rules.  So we
12709          check here to make sure that this block does not represent a "body
12710          block inlining" before trying to set the MUST_OUTPUT_DIE flag.  */
12711       if (! is_body_block (origin ? origin : stmt))
12712         {
12713           /* Determine if this block directly contains any "significant"
12714              local declarations which we will need to output DIEs for.  */
12715           if (debug_info_level > DINFO_LEVEL_TERSE)
12716             /* We are not in terse mode so *any* local declaration counts
12717                as being a "significant" one.  */
12718             must_output_die = (BLOCK_VARS (stmt) != NULL 
12719                                && (TREE_USED (stmt) 
12720                                    || TREE_ASM_WRITTEN (stmt)
12721                                    || BLOCK_ABSTRACT (stmt)));
12722           else
12723             /* We are in terse mode, so only local (nested) function
12724                definitions count as "significant" local declarations.  */
12725             for (decl = BLOCK_VARS (stmt);
12726                  decl != NULL; decl = TREE_CHAIN (decl))
12727               if (TREE_CODE (decl) == FUNCTION_DECL
12728                   && DECL_INITIAL (decl))
12729                 {
12730                   must_output_die = 1;
12731                   break;
12732                 }
12733         }
12734     }
12735
12736   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
12737      DIE for any block which contains no significant local declarations at
12738      all.  Rather, in such cases we just call `decls_for_scope' so that any
12739      needed Dwarf info for any sub-blocks will get properly generated. Note
12740      that in terse mode, our definition of what constitutes a "significant"
12741      local declaration gets restricted to include only inlined function
12742      instances and local (nested) function definitions.  */
12743   if (must_output_die)
12744     {
12745       if (origin_code == FUNCTION_DECL)
12746         gen_inlined_subroutine_die (stmt, context_die, depth);
12747       else
12748         gen_lexical_block_die (stmt, context_die, depth);
12749     }
12750   else
12751     decls_for_scope (stmt, context_die, depth);
12752 }
12753
12754 /* Generate all of the decls declared within a given scope and (recursively)
12755    all of its sub-blocks.  */
12756
12757 static void
12758 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
12759 {
12760   tree decl;
12761   tree subblocks;
12762
12763   /* Ignore NULL blocks.  */
12764   if (stmt == NULL_TREE)
12765     return;
12766
12767   if (TREE_USED (stmt))
12768     {
12769       /* Output the DIEs to represent all of the data objects and typedefs
12770          declared directly within this block but not within any nested
12771          sub-blocks.  Also, nested function and tag DIEs have been
12772          generated with a parent of NULL; fix that up now.  */
12773       for (decl = BLOCK_VARS (stmt); decl != NULL; decl = TREE_CHAIN (decl))
12774         {
12775           dw_die_ref die;
12776           
12777           if (TREE_CODE (decl) == FUNCTION_DECL)
12778             die = lookup_decl_die (decl);
12779           else if (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl))
12780             die = lookup_type_die (TREE_TYPE (decl));
12781           else
12782             die = NULL;
12783           
12784           if (die != NULL && die->die_parent == NULL)
12785             add_child_die (context_die, die);
12786           /* Do not produce debug information for static variables since
12787              these might be optimized out.  We are called for these later
12788              in cgraph_varpool_analyze_pending_decls. */
12789           if (TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
12790             ;
12791           else
12792             gen_decl_die (decl, context_die);
12793         }
12794     }
12795
12796   /* If we're at -g1, we're not interested in subblocks.  */
12797   if (debug_info_level <= DINFO_LEVEL_TERSE)
12798     return;
12799
12800   /* Output the DIEs to represent all sub-blocks (and the items declared
12801      therein) of this block.  */
12802   for (subblocks = BLOCK_SUBBLOCKS (stmt);
12803        subblocks != NULL;
12804        subblocks = BLOCK_CHAIN (subblocks))
12805     gen_block_die (subblocks, context_die, depth + 1);
12806 }
12807
12808 /* Is this a typedef we can avoid emitting?  */
12809
12810 static inline int
12811 is_redundant_typedef (tree decl)
12812 {
12813   if (TYPE_DECL_IS_STUB (decl))
12814     return 1;
12815
12816   if (DECL_ARTIFICIAL (decl)
12817       && DECL_CONTEXT (decl)
12818       && is_tagged_type (DECL_CONTEXT (decl))
12819       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
12820       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
12821     /* Also ignore the artificial member typedef for the class name.  */
12822     return 1;
12823
12824   return 0;
12825 }
12826
12827 /* Returns the DIE for decl.  A DIE will always be returned.  */
12828
12829 static dw_die_ref
12830 force_decl_die (tree decl)
12831 {
12832   dw_die_ref decl_die;
12833   unsigned saved_external_flag;
12834   tree save_fn = NULL_TREE;
12835   decl_die = lookup_decl_die (decl);
12836   if (!decl_die)
12837     {
12838       dw_die_ref context_die;
12839       tree decl_context = DECL_CONTEXT (decl);
12840       if (decl_context)
12841         {
12842           /* Find die that represents this context.  */
12843           if (TYPE_P (decl_context))
12844             context_die = force_type_die (decl_context);
12845           else
12846             context_die = force_decl_die (decl_context);
12847         }
12848       else
12849         context_die = comp_unit_die;
12850
12851       decl_die = lookup_decl_die (decl);
12852       if (decl_die)
12853         return decl_die;
12854
12855       switch (TREE_CODE (decl))
12856         {
12857         case FUNCTION_DECL:
12858           /* Clear current_function_decl, so that gen_subprogram_die thinks
12859              that this is a declaration. At this point, we just want to force
12860              declaration die.  */
12861           save_fn = current_function_decl;
12862           current_function_decl = NULL_TREE;
12863           gen_subprogram_die (decl, context_die);
12864           current_function_decl = save_fn;
12865           break;
12866
12867         case VAR_DECL:
12868           /* Set external flag to force declaration die. Restore it after
12869            gen_decl_die() call.  */
12870           saved_external_flag = DECL_EXTERNAL (decl);
12871           DECL_EXTERNAL (decl) = 1;
12872           gen_decl_die (decl, context_die);
12873           DECL_EXTERNAL (decl) = saved_external_flag;
12874           break;
12875
12876         case NAMESPACE_DECL:
12877           dwarf2out_decl (decl);
12878           break;
12879
12880         default:
12881           gcc_unreachable ();
12882         }
12883
12884       /* We should be able to find the DIE now.  */
12885       if (!decl_die)
12886         decl_die = lookup_decl_die (decl);
12887       gcc_assert (decl_die);
12888     }
12889
12890   return decl_die;
12891 }
12892
12893 /* Returns the DIE for TYPE.  A DIE is always returned.  */
12894
12895 static dw_die_ref
12896 force_type_die (tree type)
12897 {
12898   dw_die_ref type_die;
12899
12900   type_die = lookup_type_die (type);
12901   if (!type_die)
12902     {
12903       dw_die_ref context_die;
12904       if (TYPE_CONTEXT (type))
12905         {
12906           if (TYPE_P (TYPE_CONTEXT (type)))
12907             context_die = force_type_die (TYPE_CONTEXT (type));
12908           else
12909             context_die = force_decl_die (TYPE_CONTEXT (type));
12910         }
12911       else
12912         context_die = comp_unit_die;
12913
12914       type_die = lookup_type_die (type);
12915       if (type_die)
12916         return type_die;
12917       gen_type_die (type, context_die);
12918       type_die = lookup_type_die (type);
12919       gcc_assert (type_die);
12920     }
12921   return type_die;
12922 }
12923
12924 /* Force out any required namespaces to be able to output DECL,
12925    and return the new context_die for it, if it's changed.  */
12926
12927 static dw_die_ref
12928 setup_namespace_context (tree thing, dw_die_ref context_die)
12929 {
12930   tree context = (DECL_P (thing)
12931                   ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
12932   if (context && TREE_CODE (context) == NAMESPACE_DECL)
12933     /* Force out the namespace.  */
12934     context_die = force_decl_die (context);
12935
12936   return context_die;
12937 }
12938
12939 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
12940    type) within its namespace, if appropriate.
12941
12942    For compatibility with older debuggers, namespace DIEs only contain
12943    declarations; all definitions are emitted at CU scope.  */
12944
12945 static void
12946 declare_in_namespace (tree thing, dw_die_ref context_die)
12947 {
12948   dw_die_ref ns_context;
12949
12950   if (debug_info_level <= DINFO_LEVEL_TERSE)
12951     return;
12952
12953   /* If this decl is from an inlined function, then don't try to emit it in its
12954      namespace, as we will get confused.  It would have already been emitted
12955      when the abstract instance of the inline function was emitted anyways.  */
12956   if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
12957     return;
12958
12959   ns_context = setup_namespace_context (thing, context_die);
12960
12961   if (ns_context != context_die)
12962     {
12963       if (DECL_P (thing))
12964         gen_decl_die (thing, ns_context);
12965       else
12966         gen_type_die (thing, ns_context);
12967     }
12968 }
12969
12970 /* Generate a DIE for a namespace or namespace alias.  */
12971
12972 static void
12973 gen_namespace_die (tree decl)
12974 {
12975   dw_die_ref context_die = setup_namespace_context (decl, comp_unit_die);
12976
12977   /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
12978      they are an alias of.  */
12979   if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
12980     {
12981       /* Output a real namespace.  */
12982       dw_die_ref namespace_die
12983         = new_die (DW_TAG_namespace, context_die, decl);
12984       add_name_and_src_coords_attributes (namespace_die, decl);
12985       equate_decl_number_to_die (decl, namespace_die);
12986     }
12987   else
12988     {
12989       /* Output a namespace alias.  */
12990
12991       /* Force out the namespace we are an alias of, if necessary.  */
12992       dw_die_ref origin_die
12993         = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
12994
12995       /* Now create the namespace alias DIE.  */
12996       dw_die_ref namespace_die
12997         = new_die (DW_TAG_imported_declaration, context_die, decl);
12998       add_name_and_src_coords_attributes (namespace_die, decl);
12999       add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
13000       equate_decl_number_to_die (decl, namespace_die);
13001     }
13002 }
13003
13004 /* Generate Dwarf debug information for a decl described by DECL.  */
13005
13006 static void
13007 gen_decl_die (tree decl, dw_die_ref context_die)
13008 {
13009   tree origin;
13010
13011   if (DECL_P (decl) && DECL_IGNORED_P (decl))
13012     return;
13013
13014   switch (TREE_CODE (decl))
13015     {
13016     case ERROR_MARK:
13017       break;
13018
13019     case CONST_DECL:
13020       /* The individual enumerators of an enum type get output when we output
13021          the Dwarf representation of the relevant enum type itself.  */
13022       break;
13023
13024     case FUNCTION_DECL:
13025       /* Don't output any DIEs to represent mere function declarations,
13026          unless they are class members or explicit block externs.  */
13027       if (DECL_INITIAL (decl) == NULL_TREE && DECL_CONTEXT (decl) == NULL_TREE
13028           && (current_function_decl == NULL_TREE || DECL_ARTIFICIAL (decl)))
13029         break;
13030
13031 #if 0
13032       /* FIXME */
13033       /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
13034          on local redeclarations of global functions.  That seems broken.  */
13035       if (current_function_decl != decl)
13036         /* This is only a declaration.  */;
13037 #endif
13038
13039       /* If we're emitting a clone, emit info for the abstract instance.  */
13040       if (DECL_ORIGIN (decl) != decl)
13041         dwarf2out_abstract_function (DECL_ABSTRACT_ORIGIN (decl));
13042
13043       /* If we're emitting an out-of-line copy of an inline function,
13044          emit info for the abstract instance and set up to refer to it.  */
13045       else if (cgraph_function_possibly_inlined_p (decl)
13046                && ! DECL_ABSTRACT (decl)
13047                && ! class_or_namespace_scope_p (context_die)
13048                /* dwarf2out_abstract_function won't emit a die if this is just
13049                   a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
13050                   that case, because that works only if we have a die.  */
13051                && DECL_INITIAL (decl) != NULL_TREE)
13052         {
13053           dwarf2out_abstract_function (decl);
13054           set_decl_origin_self (decl);
13055         }
13056
13057       /* Otherwise we're emitting the primary DIE for this decl.  */
13058       else if (debug_info_level > DINFO_LEVEL_TERSE)
13059         {
13060           /* Before we describe the FUNCTION_DECL itself, make sure that we
13061              have described its return type.  */
13062           gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
13063
13064           /* And its virtual context.  */
13065           if (DECL_VINDEX (decl) != NULL_TREE)
13066             gen_type_die (DECL_CONTEXT (decl), context_die);
13067
13068           /* And its containing type.  */
13069           origin = decl_class_context (decl);
13070           if (origin != NULL_TREE)
13071             gen_type_die_for_member (origin, decl, context_die);
13072
13073           /* And its containing namespace.  */
13074           declare_in_namespace (decl, context_die);
13075         }
13076
13077       /* Now output a DIE to represent the function itself.  */
13078       gen_subprogram_die (decl, context_die);
13079       break;
13080
13081     case TYPE_DECL:
13082       /* If we are in terse mode, don't generate any DIEs to represent any
13083          actual typedefs.  */
13084       if (debug_info_level <= DINFO_LEVEL_TERSE)
13085         break;
13086
13087       /* In the special case of a TYPE_DECL node representing the declaration
13088          of some type tag, if the given TYPE_DECL is marked as having been
13089          instantiated from some other (original) TYPE_DECL node (e.g. one which
13090          was generated within the original definition of an inline function) we
13091          have to generate a special (abbreviated) DW_TAG_structure_type,
13092          DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  */
13093       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
13094         {
13095           gen_tagged_type_instantiation_die (TREE_TYPE (decl), context_die);
13096           break;
13097         }
13098
13099       if (is_redundant_typedef (decl))
13100         gen_type_die (TREE_TYPE (decl), context_die);
13101       else
13102         /* Output a DIE to represent the typedef itself.  */
13103         gen_typedef_die (decl, context_die);
13104       break;
13105
13106     case LABEL_DECL:
13107       if (debug_info_level >= DINFO_LEVEL_NORMAL)
13108         gen_label_die (decl, context_die);
13109       break;
13110
13111     case VAR_DECL:
13112     case RESULT_DECL:
13113       /* If we are in terse mode, don't generate any DIEs to represent any
13114          variable declarations or definitions.  */
13115       if (debug_info_level <= DINFO_LEVEL_TERSE)
13116         break;
13117
13118       /* Output any DIEs that are needed to specify the type of this data
13119          object.  */
13120       gen_type_die (TREE_TYPE (decl), context_die);
13121
13122       /* And its containing type.  */
13123       origin = decl_class_context (decl);
13124       if (origin != NULL_TREE)
13125         gen_type_die_for_member (origin, decl, context_die);
13126
13127       /* And its containing namespace.  */
13128       declare_in_namespace (decl, context_die);
13129
13130       /* Now output the DIE to represent the data object itself.  This gets
13131          complicated because of the possibility that the VAR_DECL really
13132          represents an inlined instance of a formal parameter for an inline
13133          function.  */
13134       origin = decl_ultimate_origin (decl);
13135       if (origin != NULL_TREE && TREE_CODE (origin) == PARM_DECL)
13136         gen_formal_parameter_die (decl, context_die);
13137       else
13138         gen_variable_die (decl, context_die);
13139       break;
13140
13141     case FIELD_DECL:
13142       /* Ignore the nameless fields that are used to skip bits but handle C++
13143          anonymous unions and structs.  */
13144       if (DECL_NAME (decl) != NULL_TREE
13145           || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
13146           || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
13147         {
13148           gen_type_die (member_declared_type (decl), context_die);
13149           gen_field_die (decl, context_die);
13150         }
13151       break;
13152
13153     case PARM_DECL:
13154       gen_type_die (TREE_TYPE (decl), context_die);
13155       gen_formal_parameter_die (decl, context_die);
13156       break;
13157
13158     case NAMESPACE_DECL:
13159       gen_namespace_die (decl);
13160       break;
13161
13162     default:
13163       /* Probably some frontend-internal decl.  Assume we don't care.  */
13164       gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
13165       break;
13166     }
13167 }
13168 \f
13169 /* Add Ada "use" clause information for SGI Workshop debugger.  */
13170
13171 void
13172 dwarf2out_add_library_unit_info (const char *filename, const char *context_list)
13173 {
13174   unsigned int file_index;
13175
13176   if (filename != NULL)
13177     {
13178       dw_die_ref unit_die = new_die (DW_TAG_module, comp_unit_die, NULL);
13179       tree context_list_decl
13180         = build_decl (LABEL_DECL, get_identifier (context_list),
13181                       void_type_node);
13182
13183       TREE_PUBLIC (context_list_decl) = TRUE;
13184       add_name_attribute (unit_die, context_list);
13185       file_index = lookup_filename (filename);
13186       add_AT_unsigned (unit_die, DW_AT_decl_file, file_index);
13187       add_pubname (context_list_decl, unit_die);
13188     }
13189 }
13190
13191 /* Output debug information for global decl DECL.  Called from toplev.c after
13192    compilation proper has finished.  */
13193
13194 static void
13195 dwarf2out_global_decl (tree decl)
13196 {
13197   /* Output DWARF2 information for file-scope tentative data object
13198      declarations, file-scope (extern) function declarations (which had no
13199      corresponding body) and file-scope tagged type declarations and
13200      definitions which have not yet been forced out.  */
13201   if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
13202     dwarf2out_decl (decl);
13203 }
13204
13205 /* Output debug information for type decl DECL.  Called from toplev.c
13206    and from language front ends (to record built-in types).  */
13207 static void
13208 dwarf2out_type_decl (tree decl, int local)
13209 {
13210   if (!local)
13211     dwarf2out_decl (decl);
13212 }
13213
13214 /* Output debug information for imported module or decl.  */
13215
13216 static void
13217 dwarf2out_imported_module_or_decl (tree decl, tree context)
13218 {
13219   dw_die_ref imported_die, at_import_die;
13220   dw_die_ref scope_die;
13221   unsigned file_index;
13222   expanded_location xloc;
13223
13224   if (debug_info_level <= DINFO_LEVEL_TERSE)
13225     return;
13226
13227   gcc_assert (decl);
13228
13229   /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
13230      We need decl DIE for reference and scope die. First, get DIE for the decl
13231      itself.  */
13232
13233   /* Get the scope die for decl context. Use comp_unit_die for global module
13234      or decl. If die is not found for non globals, force new die.  */
13235   if (!context)
13236     scope_die = comp_unit_die;
13237   else if (TYPE_P (context))
13238     scope_die = force_type_die (context);
13239   else
13240     scope_die = force_decl_die (context);
13241
13242   /* For TYPE_DECL or CONST_DECL, lookup TREE_TYPE.  */
13243   if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
13244     at_import_die = force_type_die (TREE_TYPE (decl));
13245   else
13246     {
13247       at_import_die = lookup_decl_die (decl);
13248       if (!at_import_die)
13249         {
13250           /* If we're trying to avoid duplicate debug info, we may not have
13251              emitted the member decl for this field.  Emit it now.  */
13252           if (TREE_CODE (decl) == FIELD_DECL)
13253             {
13254               tree type = DECL_CONTEXT (decl);
13255               dw_die_ref type_context_die;
13256
13257               if (TYPE_CONTEXT (type))
13258                 if (TYPE_P (TYPE_CONTEXT (type)))
13259                   type_context_die = force_type_die (TYPE_CONTEXT (type));
13260               else
13261                 type_context_die = force_decl_die (TYPE_CONTEXT (type));
13262               else
13263                 type_context_die = comp_unit_die;
13264               gen_type_die_for_member (type, decl, type_context_die);
13265             }
13266           at_import_die = force_decl_die (decl);
13267         }
13268     }
13269
13270   /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
13271   if (TREE_CODE (decl) == NAMESPACE_DECL)
13272     imported_die = new_die (DW_TAG_imported_module, scope_die, context);
13273   else
13274     imported_die = new_die (DW_TAG_imported_declaration, scope_die, context);
13275
13276   xloc = expand_location (input_location);
13277   file_index = lookup_filename (xloc.file);
13278   add_AT_unsigned (imported_die, DW_AT_decl_file, file_index);
13279   add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
13280   add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
13281 }
13282
13283 /* Write the debugging output for DECL.  */
13284
13285 void
13286 dwarf2out_decl (tree decl)
13287 {
13288   dw_die_ref context_die = comp_unit_die;
13289
13290   switch (TREE_CODE (decl))
13291     {
13292     case ERROR_MARK:
13293       return;
13294
13295     case FUNCTION_DECL:
13296       /* What we would really like to do here is to filter out all mere
13297          file-scope declarations of file-scope functions which are never
13298          referenced later within this translation unit (and keep all of ones
13299          that *are* referenced later on) but we aren't clairvoyant, so we have
13300          no idea which functions will be referenced in the future (i.e. later
13301          on within the current translation unit). So here we just ignore all
13302          file-scope function declarations which are not also definitions.  If
13303          and when the debugger needs to know something about these functions,
13304          it will have to hunt around and find the DWARF information associated
13305          with the definition of the function.
13306
13307          We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
13308          nodes represent definitions and which ones represent mere
13309          declarations.  We have to check DECL_INITIAL instead. That's because
13310          the C front-end supports some weird semantics for "extern inline"
13311          function definitions.  These can get inlined within the current
13312          translation unit (and thus, we need to generate Dwarf info for their
13313          abstract instances so that the Dwarf info for the concrete inlined
13314          instances can have something to refer to) but the compiler never
13315          generates any out-of-lines instances of such things (despite the fact
13316          that they *are* definitions).
13317
13318          The important point is that the C front-end marks these "extern
13319          inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
13320          them anyway. Note that the C++ front-end also plays some similar games
13321          for inline function definitions appearing within include files which
13322          also contain `#pragma interface' pragmas.  */
13323       if (DECL_INITIAL (decl) == NULL_TREE)
13324         return;
13325
13326       /* If we're a nested function, initially use a parent of NULL; if we're
13327          a plain function, this will be fixed up in decls_for_scope.  If
13328          we're a method, it will be ignored, since we already have a DIE.  */
13329       if (decl_function_context (decl)
13330           /* But if we're in terse mode, we don't care about scope.  */
13331           && debug_info_level > DINFO_LEVEL_TERSE)
13332         context_die = NULL;
13333       break;
13334
13335     case VAR_DECL:
13336       /* Ignore this VAR_DECL if it refers to a file-scope extern data object
13337          declaration and if the declaration was never even referenced from
13338          within this entire compilation unit.  We suppress these DIEs in
13339          order to save space in the .debug section (by eliminating entries
13340          which are probably useless).  Note that we must not suppress
13341          block-local extern declarations (whether used or not) because that
13342          would screw-up the debugger's name lookup mechanism and cause it to
13343          miss things which really ought to be in scope at a given point.  */
13344       if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
13345         return;
13346
13347       /* For local statics lookup proper context die.  */
13348       if (TREE_STATIC (decl) && decl_function_context (decl))
13349         context_die = lookup_decl_die (DECL_CONTEXT (decl));
13350
13351       /* If we are in terse mode, don't generate any DIEs to represent any
13352          variable declarations or definitions.  */
13353       if (debug_info_level <= DINFO_LEVEL_TERSE)
13354         return;
13355       break;
13356
13357     case NAMESPACE_DECL:
13358       if (debug_info_level <= DINFO_LEVEL_TERSE)
13359         return;
13360       if (lookup_decl_die (decl) != NULL)
13361         return;
13362       break;
13363
13364     case TYPE_DECL:
13365       /* Don't emit stubs for types unless they are needed by other DIEs.  */
13366       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
13367         return;
13368
13369       /* Don't bother trying to generate any DIEs to represent any of the
13370          normal built-in types for the language we are compiling.  */
13371       if (DECL_IS_BUILTIN (decl))
13372         {
13373           /* OK, we need to generate one for `bool' so GDB knows what type
13374              comparisons have.  */
13375           if ((get_AT_unsigned (comp_unit_die, DW_AT_language)
13376                == DW_LANG_C_plus_plus)
13377               && TREE_CODE (TREE_TYPE (decl)) == BOOLEAN_TYPE
13378               && ! DECL_IGNORED_P (decl))
13379             modified_type_die (TREE_TYPE (decl), 0, 0, NULL);
13380
13381           return;
13382         }
13383
13384       /* If we are in terse mode, don't generate any DIEs for types.  */
13385       if (debug_info_level <= DINFO_LEVEL_TERSE)
13386         return;
13387
13388       /* If we're a function-scope tag, initially use a parent of NULL;
13389          this will be fixed up in decls_for_scope.  */
13390       if (decl_function_context (decl))
13391         context_die = NULL;
13392
13393       break;
13394
13395     default:
13396       return;
13397     }
13398
13399   gen_decl_die (decl, context_die);
13400 }
13401
13402 /* Output a marker (i.e. a label) for the beginning of the generated code for
13403    a lexical block.  */
13404
13405 static void
13406 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
13407                        unsigned int blocknum)
13408 {
13409   current_function_section (current_function_decl);
13410   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
13411 }
13412
13413 /* Output a marker (i.e. a label) for the end of the generated code for a
13414    lexical block.  */
13415
13416 static void
13417 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
13418 {
13419   current_function_section (current_function_decl);
13420   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
13421 }
13422
13423 /* Returns nonzero if it is appropriate not to emit any debugging
13424    information for BLOCK, because it doesn't contain any instructions.
13425
13426    Don't allow this for blocks with nested functions or local classes
13427    as we would end up with orphans, and in the presence of scheduling
13428    we may end up calling them anyway.  */
13429
13430 static bool
13431 dwarf2out_ignore_block (tree block)
13432 {
13433   tree decl;
13434
13435   for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
13436     if (TREE_CODE (decl) == FUNCTION_DECL
13437         || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
13438       return 0;
13439
13440   return 1;
13441 }
13442
13443 /* Lookup FILE_NAME (in the list of filenames that we know about here in
13444    dwarf2out.c) and return its "index".  The index of each (known) filename is
13445    just a unique number which is associated with only that one filename.  We
13446    need such numbers for the sake of generating labels (in the .debug_sfnames
13447    section) and references to those files numbers (in the .debug_srcinfo
13448    and.debug_macinfo sections).  If the filename given as an argument is not
13449    found in our current list, add it to the list and assign it the next
13450    available unique index number.  In order to speed up searches, we remember
13451    the index of the filename was looked up last.  This handles the majority of
13452    all searches.  */
13453
13454 static unsigned
13455 lookup_filename (const char *file_name)
13456 {
13457   size_t i, n;
13458   char *save_file_name;
13459
13460   /* Check to see if the file name that was searched on the previous
13461      call matches this file name.  If so, return the index.  */
13462   if (file_table_last_lookup_index != 0)
13463     {
13464       const char *last
13465         = VARRAY_CHAR_PTR (file_table, file_table_last_lookup_index);
13466       if (strcmp (file_name, last) == 0)
13467         return file_table_last_lookup_index;
13468     }
13469
13470   /* Didn't match the previous lookup, search the table.  */
13471   n = VARRAY_ACTIVE_SIZE (file_table);
13472   for (i = 1; i < n; i++)
13473     if (strcmp (file_name, VARRAY_CHAR_PTR (file_table, i)) == 0)
13474       {
13475         file_table_last_lookup_index = i;
13476         return i;
13477       }
13478
13479   /* Add the new entry to the end of the filename table.  */
13480   file_table_last_lookup_index = n;
13481   save_file_name = (char *) ggc_strdup (file_name);
13482   VARRAY_PUSH_CHAR_PTR (file_table, save_file_name);
13483   VARRAY_PUSH_UINT (file_table_emitted, 0);
13484
13485   /* If the assembler is emitting the file table, and we aren't eliminating
13486      unused debug types, then we must emit .file here.  If we are eliminating
13487      unused debug types, then this will be done by the maybe_emit_file call in
13488      prune_unused_types_walk_attribs.  */
13489
13490   if (DWARF2_ASM_LINE_DEBUG_INFO && ! flag_eliminate_unused_debug_types)
13491     return maybe_emit_file (i);
13492
13493   return i;
13494 }
13495
13496 /* If the assembler will construct the file table, then translate the compiler
13497    internal file table number into the assembler file table number, and emit
13498    a .file directive if we haven't already emitted one yet.  The file table
13499    numbers are different because we prune debug info for unused variables and
13500    types, which may include filenames.  */
13501
13502 static int
13503 maybe_emit_file (int fileno)
13504 {
13505   if (DWARF2_ASM_LINE_DEBUG_INFO && fileno > 0)
13506     {
13507       if (!VARRAY_UINT (file_table_emitted, fileno))
13508         {
13509           VARRAY_UINT (file_table_emitted, fileno) = ++emitcount;
13510           fprintf (asm_out_file, "\t.file %u ",
13511                    VARRAY_UINT (file_table_emitted, fileno));
13512           output_quoted_string (asm_out_file,
13513                                 VARRAY_CHAR_PTR (file_table, fileno));
13514           fputc ('\n', asm_out_file);
13515         }
13516       return VARRAY_UINT (file_table_emitted, fileno);
13517     }
13518   else
13519     return fileno;
13520 }
13521
13522 /* Initialize the compiler internal file table.  */
13523
13524 static void
13525 init_file_table (void)
13526 {
13527   /* Allocate the initial hunk of the file_table.  */
13528   VARRAY_CHAR_PTR_INIT (file_table, 64, "file_table");
13529   VARRAY_UINT_INIT (file_table_emitted, 64, "file_table_emitted");
13530
13531   /* Skip the first entry - file numbers begin at 1.  */
13532   VARRAY_PUSH_CHAR_PTR (file_table, NULL);
13533   VARRAY_PUSH_UINT (file_table_emitted, 0);
13534   file_table_last_lookup_index = 0;
13535 }
13536
13537 /* Called by the final INSN scan whenever we see a var location.  We
13538    use it to drop labels in the right places, and throw the location in
13539    our lookup table.  */
13540
13541 static void
13542 dwarf2out_var_location (rtx loc_note)
13543 {
13544   char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
13545   struct var_loc_node *newloc;
13546   rtx prev_insn;
13547   static rtx last_insn;
13548   static const char *last_label;
13549   tree decl;
13550
13551   if (!DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
13552     return;
13553   prev_insn = PREV_INSN (loc_note);
13554
13555   newloc = ggc_alloc_cleared (sizeof (struct var_loc_node));
13556   /* If the insn we processed last time is the previous insn
13557      and it is also a var location note, use the label we emitted
13558      last time.  */
13559   if (last_insn != NULL_RTX
13560       && last_insn == prev_insn
13561       && NOTE_P (prev_insn)
13562       && NOTE_LINE_NUMBER (prev_insn) == NOTE_INSN_VAR_LOCATION)
13563     {
13564       newloc->label = last_label;
13565     }
13566   else
13567     {
13568       ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
13569       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
13570       loclabel_num++;
13571       newloc->label = ggc_strdup (loclabel);
13572     }
13573   newloc->var_loc_note = loc_note;
13574   newloc->next = NULL;
13575
13576   if (cfun
13577       && (last_text_section == in_unlikely_executed_text
13578           || (last_text_section == in_named
13579               && last_text_section_name == cfun->unlikely_text_section_name)))
13580     newloc->section_label = cfun->cold_section_label;
13581   else
13582     newloc->section_label = text_section_label;
13583
13584   last_insn = loc_note;
13585   last_label = newloc->label;
13586   decl = NOTE_VAR_LOCATION_DECL (loc_note);
13587   if (DECL_DEBUG_EXPR_IS_FROM (decl) && DECL_DEBUG_EXPR (decl) 
13588       && DECL_P (DECL_DEBUG_EXPR (decl)))
13589     decl = DECL_DEBUG_EXPR (decl); 
13590   add_var_loc_to_decl (decl, newloc);
13591 }
13592
13593 /* We need to reset the locations at the beginning of each
13594    function. We can't do this in the end_function hook, because the
13595    declarations that use the locations won't have been outputted when
13596    that hook is called.  */
13597
13598 static void
13599 dwarf2out_begin_function (tree unused ATTRIBUTE_UNUSED)
13600 {
13601   htab_empty (decl_loc_table);
13602 }
13603
13604 /* Output a label to mark the beginning of a source code line entry
13605    and record information relating to this source line, in
13606    'line_info_table' for later output of the .debug_line section.  */
13607
13608 static void
13609 dwarf2out_source_line (unsigned int line, const char *filename)
13610 {
13611   if (debug_info_level >= DINFO_LEVEL_NORMAL
13612       && line != 0)
13613     {
13614       current_function_section (current_function_decl);
13615
13616       /* If requested, emit something human-readable.  */
13617       if (flag_debug_asm)
13618         fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
13619                  filename, line);
13620
13621       if (DWARF2_ASM_LINE_DEBUG_INFO)
13622         {
13623           unsigned file_num = lookup_filename (filename);
13624
13625           file_num = maybe_emit_file (file_num);
13626
13627           /* Emit the .loc directive understood by GNU as.  */
13628           fprintf (asm_out_file, "\t.loc %d %d 0\n", file_num, line);
13629
13630           /* Indicate that line number info exists.  */
13631           line_info_table_in_use++;
13632
13633           /* Indicate that multiple line number tables exist.  */
13634           if (DECL_SECTION_NAME (current_function_decl))
13635             separate_line_info_table_in_use++;
13636         }
13637       else if (DECL_SECTION_NAME (current_function_decl))
13638         {
13639           dw_separate_line_info_ref line_info;
13640           targetm.asm_out.internal_label (asm_out_file, SEPARATE_LINE_CODE_LABEL,
13641                                      separate_line_info_table_in_use);
13642
13643           /* Expand the line info table if necessary.  */
13644           if (separate_line_info_table_in_use
13645               == separate_line_info_table_allocated)
13646             {
13647               separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
13648               separate_line_info_table
13649                 = ggc_realloc (separate_line_info_table,
13650                                separate_line_info_table_allocated
13651                                * sizeof (dw_separate_line_info_entry));
13652               memset (separate_line_info_table
13653                        + separate_line_info_table_in_use,
13654                       0,
13655                       (LINE_INFO_TABLE_INCREMENT
13656                        * sizeof (dw_separate_line_info_entry)));
13657             }
13658
13659           /* Add the new entry at the end of the line_info_table.  */
13660           line_info
13661             = &separate_line_info_table[separate_line_info_table_in_use++];
13662           line_info->dw_file_num = lookup_filename (filename);
13663           line_info->dw_line_num = line;
13664           line_info->function = current_function_funcdef_no;
13665         }
13666       else
13667         {
13668           dw_line_info_ref line_info;
13669
13670           targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL,
13671                                      line_info_table_in_use);
13672
13673           /* Expand the line info table if necessary.  */
13674           if (line_info_table_in_use == line_info_table_allocated)
13675             {
13676               line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
13677               line_info_table
13678                 = ggc_realloc (line_info_table,
13679                                (line_info_table_allocated
13680                                 * sizeof (dw_line_info_entry)));
13681               memset (line_info_table + line_info_table_in_use, 0,
13682                       LINE_INFO_TABLE_INCREMENT * sizeof (dw_line_info_entry));
13683             }
13684
13685           /* Add the new entry at the end of the line_info_table.  */
13686           line_info = &line_info_table[line_info_table_in_use++];
13687           line_info->dw_file_num = lookup_filename (filename);
13688           line_info->dw_line_num = line;
13689         }
13690     }
13691 }
13692
13693 /* Record the beginning of a new source file.  */
13694
13695 static void
13696 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
13697 {
13698   if (flag_eliminate_dwarf2_dups)
13699     {
13700       /* Record the beginning of the file for break_out_includes.  */
13701       dw_die_ref bincl_die;
13702
13703       bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die, NULL);
13704       add_AT_string (bincl_die, DW_AT_name, filename);
13705     }
13706
13707   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13708     {
13709       int fileno;
13710
13711       named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
13712       dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
13713       dw2_asm_output_data_uleb128 (lineno, "Included from line number %d",
13714                                    lineno);
13715
13716       fileno = maybe_emit_file (lookup_filename (filename));
13717       dw2_asm_output_data_uleb128 (fileno, "Filename we just started");
13718     }
13719 }
13720
13721 /* Record the end of a source file.  */
13722
13723 static void
13724 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
13725 {
13726   if (flag_eliminate_dwarf2_dups)
13727     /* Record the end of the file for break_out_includes.  */
13728     new_die (DW_TAG_GNU_EINCL, comp_unit_die, NULL);
13729
13730   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13731     {
13732       named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
13733       dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
13734     }
13735 }
13736
13737 /* Called from debug_define in toplev.c.  The `buffer' parameter contains
13738    the tail part of the directive line, i.e. the part which is past the
13739    initial whitespace, #, whitespace, directive-name, whitespace part.  */
13740
13741 static void
13742 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
13743                   const char *buffer ATTRIBUTE_UNUSED)
13744 {
13745   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13746     {
13747       named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
13748       dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
13749       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
13750       dw2_asm_output_nstring (buffer, -1, "The macro");
13751     }
13752 }
13753
13754 /* Called from debug_undef in toplev.c.  The `buffer' parameter contains
13755    the tail part of the directive line, i.e. the part which is past the
13756    initial whitespace, #, whitespace, directive-name, whitespace part.  */
13757
13758 static void
13759 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
13760                  const char *buffer ATTRIBUTE_UNUSED)
13761 {
13762   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13763     {
13764       named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
13765       dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
13766       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
13767       dw2_asm_output_nstring (buffer, -1, "The macro");
13768     }
13769 }
13770
13771 /* Set up for Dwarf output at the start of compilation.  */
13772
13773 static void
13774 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
13775 {
13776   init_file_table ();
13777
13778   /* Allocate the decl_die_table.  */
13779   decl_die_table = htab_create_ggc (10, decl_die_table_hash,
13780                                     decl_die_table_eq, NULL);
13781
13782   /* Allocate the decl_loc_table.  */
13783   decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
13784                                     decl_loc_table_eq, NULL);
13785
13786   /* Allocate the initial hunk of the decl_scope_table.  */
13787   decl_scope_table = VEC_alloc (tree, gc, 256);
13788
13789   /* Allocate the initial hunk of the abbrev_die_table.  */
13790   abbrev_die_table = ggc_alloc_cleared (ABBREV_DIE_TABLE_INCREMENT
13791                                         * sizeof (dw_die_ref));
13792   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
13793   /* Zero-th entry is allocated, but unused.  */
13794   abbrev_die_table_in_use = 1;
13795
13796   /* Allocate the initial hunk of the line_info_table.  */
13797   line_info_table = ggc_alloc_cleared (LINE_INFO_TABLE_INCREMENT
13798                                        * sizeof (dw_line_info_entry));
13799   line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
13800
13801   /* Zero-th entry is allocated, but unused.  */
13802   line_info_table_in_use = 1;
13803
13804   /* Generate the initial DIE for the .debug section.  Note that the (string)
13805      value given in the DW_AT_name attribute of the DW_TAG_compile_unit DIE
13806      will (typically) be a relative pathname and that this pathname should be
13807      taken as being relative to the directory from which the compiler was
13808      invoked when the given (base) source file was compiled.  We will fill
13809      in this value in dwarf2out_finish.  */
13810   comp_unit_die = gen_compile_unit_die (NULL);
13811
13812   incomplete_types = VEC_alloc (tree, gc, 64);
13813
13814   used_rtx_array = VEC_alloc (rtx, gc, 32);
13815
13816   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
13817   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
13818                                DEBUG_ABBREV_SECTION_LABEL, 0);
13819   ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
13820   ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label, 
13821                                COLD_TEXT_SECTION_LABEL, 0);
13822   ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
13823
13824   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
13825                                DEBUG_INFO_SECTION_LABEL, 0);
13826   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
13827                                DEBUG_LINE_SECTION_LABEL, 0);
13828   ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
13829                                DEBUG_RANGES_SECTION_LABEL, 0);
13830   named_section_flags (DEBUG_ABBREV_SECTION, SECTION_DEBUG);
13831   ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
13832   named_section_flags (DEBUG_INFO_SECTION, SECTION_DEBUG);
13833   ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
13834   named_section_flags (DEBUG_LINE_SECTION, SECTION_DEBUG);
13835   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
13836
13837   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13838     {
13839       named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
13840       ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
13841                                    DEBUG_MACINFO_SECTION_LABEL, 0);
13842       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
13843     }
13844
13845   text_section ();
13846   ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
13847   if (flag_reorder_blocks_and_partition)
13848     {
13849       unlikely_text_section ();
13850       ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
13851     }
13852 }
13853
13854 /* A helper function for dwarf2out_finish called through
13855    ht_forall.  Emit one queued .debug_str string.  */
13856
13857 static int
13858 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
13859 {
13860   struct indirect_string_node *node = (struct indirect_string_node *) *h;
13861
13862   if (node->form == DW_FORM_strp)
13863     {
13864       named_section_flags (DEBUG_STR_SECTION, DEBUG_STR_SECTION_FLAGS);
13865       ASM_OUTPUT_LABEL (asm_out_file, node->label);
13866       assemble_string (node->str, strlen (node->str) + 1);
13867     }
13868
13869   return 1;
13870 }
13871
13872
13873
13874 /* Clear the marks for a die and its children.
13875    Be cool if the mark isn't set.  */
13876
13877 static void
13878 prune_unmark_dies (dw_die_ref die)
13879 {
13880   dw_die_ref c;
13881   die->die_mark = 0;
13882   for (c = die->die_child; c; c = c->die_sib)
13883     prune_unmark_dies (c);
13884 }
13885
13886
13887 /* Given DIE that we're marking as used, find any other dies
13888    it references as attributes and mark them as used.  */
13889
13890 static void
13891 prune_unused_types_walk_attribs (dw_die_ref die)
13892 {
13893   dw_attr_ref a;
13894
13895   for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
13896     {
13897       if (a->dw_attr_val.val_class == dw_val_class_die_ref)
13898         {
13899           /* A reference to another DIE.
13900              Make sure that it will get emitted.  */
13901           prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
13902         }
13903       else if (a->dw_attr == DW_AT_decl_file || a->dw_attr == DW_AT_call_file)
13904         {
13905           /* A reference to a file.  Make sure the file name is emitted.  */
13906           a->dw_attr_val.v.val_unsigned =
13907             maybe_emit_file (a->dw_attr_val.v.val_unsigned);
13908         }
13909     }
13910 }
13911
13912
13913 /* Mark DIE as being used.  If DOKIDS is true, then walk down
13914    to DIE's children.  */
13915
13916 static void
13917 prune_unused_types_mark (dw_die_ref die, int dokids)
13918 {
13919   dw_die_ref c;
13920
13921   if (die->die_mark == 0)
13922     {
13923       /* We haven't done this node yet.  Mark it as used.  */
13924       die->die_mark = 1;
13925
13926       /* We also have to mark its parents as used.
13927          (But we don't want to mark our parents' kids due to this.)  */
13928       if (die->die_parent)
13929         prune_unused_types_mark (die->die_parent, 0);
13930
13931       /* Mark any referenced nodes.  */
13932       prune_unused_types_walk_attribs (die);
13933
13934       /* If this node is a specification,
13935          also mark the definition, if it exists.  */
13936       if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
13937         prune_unused_types_mark (die->die_definition, 1);
13938     }
13939
13940   if (dokids && die->die_mark != 2)
13941     {
13942       /* We need to walk the children, but haven't done so yet.
13943          Remember that we've walked the kids.  */
13944       die->die_mark = 2;
13945
13946       /* Walk them.  */
13947       for (c = die->die_child; c; c = c->die_sib)
13948         {
13949           /* If this is an array type, we need to make sure our
13950              kids get marked, even if they're types.  */
13951           if (die->die_tag == DW_TAG_array_type)
13952             prune_unused_types_mark (c, 1);
13953           else
13954             prune_unused_types_walk (c);
13955         }
13956     }
13957 }
13958
13959
13960 /* Walk the tree DIE and mark types that we actually use.  */
13961
13962 static void
13963 prune_unused_types_walk (dw_die_ref die)
13964 {
13965   dw_die_ref c;
13966
13967   /* Don't do anything if this node is already marked.  */
13968   if (die->die_mark)
13969     return;
13970
13971   switch (die->die_tag) {
13972   case DW_TAG_const_type:
13973   case DW_TAG_packed_type:
13974   case DW_TAG_pointer_type:
13975   case DW_TAG_reference_type:
13976   case DW_TAG_volatile_type:
13977   case DW_TAG_typedef:
13978   case DW_TAG_array_type:
13979   case DW_TAG_structure_type:
13980   case DW_TAG_union_type:
13981   case DW_TAG_class_type:
13982   case DW_TAG_friend:
13983   case DW_TAG_variant_part:
13984   case DW_TAG_enumeration_type:
13985   case DW_TAG_subroutine_type:
13986   case DW_TAG_string_type:
13987   case DW_TAG_set_type:
13988   case DW_TAG_subrange_type:
13989   case DW_TAG_ptr_to_member_type:
13990   case DW_TAG_file_type:
13991     /* It's a type node --- don't mark it.  */
13992     return;
13993
13994   default:
13995     /* Mark everything else.  */
13996     break;
13997   }
13998
13999   die->die_mark = 1;
14000
14001   /* Now, mark any dies referenced from here.  */
14002   prune_unused_types_walk_attribs (die);
14003
14004   /* Mark children.  */
14005   for (c = die->die_child; c; c = c->die_sib)
14006     prune_unused_types_walk (c);
14007 }
14008
14009
14010 /* Remove from the tree DIE any dies that aren't marked.  */
14011
14012 static void
14013 prune_unused_types_prune (dw_die_ref die)
14014 {
14015   dw_die_ref c, p, n;
14016
14017   gcc_assert (die->die_mark);
14018
14019   p = NULL;
14020   for (c = die->die_child; c; c = n)
14021     {
14022       n = c->die_sib;
14023       if (c->die_mark)
14024         {
14025           prune_unused_types_prune (c);
14026           p = c;
14027         }
14028       else
14029         {
14030           if (p)
14031             p->die_sib = n;
14032           else
14033             die->die_child = n;
14034           free_die (c);
14035         }
14036     }
14037 }
14038
14039
14040 /* Remove dies representing declarations that we never use.  */
14041
14042 static void
14043 prune_unused_types (void)
14044 {
14045   unsigned int i;
14046   limbo_die_node *node;
14047
14048   /* Clear all the marks.  */
14049   prune_unmark_dies (comp_unit_die);
14050   for (node = limbo_die_list; node; node = node->next)
14051     prune_unmark_dies (node->die);
14052
14053   /* Set the mark on nodes that are actually used.  */
14054   prune_unused_types_walk (comp_unit_die);
14055   for (node = limbo_die_list; node; node = node->next)
14056     prune_unused_types_walk (node->die);
14057
14058   /* Also set the mark on nodes referenced from the
14059      pubname_table or arange_table.  */
14060   for (i = 0; i < pubname_table_in_use; i++)
14061     prune_unused_types_mark (pubname_table[i].die, 1);
14062   for (i = 0; i < arange_table_in_use; i++)
14063     prune_unused_types_mark (arange_table[i], 1);
14064
14065   /* Get rid of nodes that aren't marked.  */
14066   prune_unused_types_prune (comp_unit_die);
14067   for (node = limbo_die_list; node; node = node->next)
14068     prune_unused_types_prune (node->die);
14069
14070   /* Leave the marks clear.  */
14071   prune_unmark_dies (comp_unit_die);
14072   for (node = limbo_die_list; node; node = node->next)
14073     prune_unmark_dies (node->die);
14074 }
14075
14076 /* Output stuff that dwarf requires at the end of every file,
14077    and generate the DWARF-2 debugging info.  */
14078
14079 static void
14080 dwarf2out_finish (const char *filename)
14081 {
14082   limbo_die_node *node, *next_node;
14083   dw_die_ref die = 0;
14084
14085   /* Add the name for the main input file now.  We delayed this from
14086      dwarf2out_init to avoid complications with PCH.  */
14087   add_name_attribute (comp_unit_die, filename);
14088   if (filename[0] != DIR_SEPARATOR)
14089     add_comp_dir_attribute (comp_unit_die);
14090   else if (get_AT (comp_unit_die, DW_AT_comp_dir) == NULL)
14091     {
14092       size_t i;
14093       for (i = 1; i < VARRAY_ACTIVE_SIZE (file_table); i++)
14094         if (VARRAY_CHAR_PTR (file_table, i)[0] != DIR_SEPARATOR
14095             /* Don't add cwd for <built-in>.  */
14096             && VARRAY_CHAR_PTR (file_table, i)[0] != '<')
14097           {
14098             add_comp_dir_attribute (comp_unit_die);
14099             break;
14100           }
14101     }
14102
14103   /* Traverse the limbo die list, and add parent/child links.  The only
14104      dies without parents that should be here are concrete instances of
14105      inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
14106      For concrete instances, we can get the parent die from the abstract
14107      instance.  */
14108   for (node = limbo_die_list; node; node = next_node)
14109     {
14110       next_node = node->next;
14111       die = node->die;
14112
14113       if (die->die_parent == NULL)
14114         {
14115           dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
14116
14117           if (origin)
14118             add_child_die (origin->die_parent, die);
14119           else if (die == comp_unit_die)
14120             ;
14121           else if (errorcount > 0 || sorrycount > 0)
14122             /* It's OK to be confused by errors in the input.  */
14123             add_child_die (comp_unit_die, die);
14124           else
14125             {
14126               /* In certain situations, the lexical block containing a
14127                  nested function can be optimized away, which results
14128                  in the nested function die being orphaned.  Likewise
14129                  with the return type of that nested function.  Force
14130                  this to be a child of the containing function.
14131
14132                  It may happen that even the containing function got fully
14133                  inlined and optimized out.  In that case we are lost and
14134                  assign the empty child.  This should not be big issue as
14135                  the function is likely unreachable too.  */
14136               tree context = NULL_TREE;
14137
14138               gcc_assert (node->created_for);
14139
14140               if (DECL_P (node->created_for))
14141                 context = DECL_CONTEXT (node->created_for);
14142               else if (TYPE_P (node->created_for))
14143                 context = TYPE_CONTEXT (node->created_for);
14144
14145               gcc_assert (context && TREE_CODE (context) == FUNCTION_DECL);
14146
14147               origin = lookup_decl_die (context);
14148               if (origin)
14149                 add_child_die (origin, die);
14150               else
14151                 add_child_die (comp_unit_die, die);
14152             }
14153         }
14154     }
14155
14156   limbo_die_list = NULL;
14157
14158   /* Walk through the list of incomplete types again, trying once more to
14159      emit full debugging info for them.  */
14160   retry_incomplete_types ();
14161
14162   /* We need to reverse all the dies before break_out_includes, or
14163      we'll see the end of an include file before the beginning.  */
14164   reverse_all_dies (comp_unit_die);
14165
14166   if (flag_eliminate_unused_debug_types)
14167     prune_unused_types ();
14168
14169   /* Generate separate CUs for each of the include files we've seen.
14170      They will go into limbo_die_list.  */
14171   if (flag_eliminate_dwarf2_dups)
14172     break_out_includes (comp_unit_die);
14173
14174   /* Traverse the DIE's and add add sibling attributes to those DIE's
14175      that have children.  */
14176   add_sibling_attributes (comp_unit_die);
14177   for (node = limbo_die_list; node; node = node->next)
14178     add_sibling_attributes (node->die);
14179
14180   /* Output a terminator label for the .text section.  */
14181   text_section ();
14182   targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
14183   if (flag_reorder_blocks_and_partition)
14184     {
14185       unlikely_text_section ();
14186       targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
14187     }
14188
14189   /* Output the source line correspondence table.  We must do this
14190      even if there is no line information.  Otherwise, on an empty
14191      translation unit, we will generate a present, but empty,
14192      .debug_info section.  IRIX 6.5 `nm' will then complain when
14193      examining the file.  */
14194   if (! DWARF2_ASM_LINE_DEBUG_INFO)
14195     {
14196       named_section_flags (DEBUG_LINE_SECTION, SECTION_DEBUG);
14197       output_line_info ();
14198     }
14199
14200   /* Output location list section if necessary.  */
14201   if (have_location_lists)
14202     {
14203       /* Output the location lists info.  */
14204       named_section_flags (DEBUG_LOC_SECTION, SECTION_DEBUG);
14205       ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
14206                                    DEBUG_LOC_SECTION_LABEL, 0);
14207       ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
14208       output_location_lists (die);
14209       have_location_lists = 0;
14210     }
14211
14212   /* We can only use the low/high_pc attributes if all of the code was
14213      in .text.  */
14214   if (!separate_line_info_table_in_use && !have_switched_text_section)
14215     {
14216       add_AT_lbl_id (comp_unit_die, DW_AT_low_pc, text_section_label);
14217       add_AT_lbl_id (comp_unit_die, DW_AT_high_pc, text_end_label);
14218     }
14219
14220   /* If it wasn't, we need to give .debug_loc and .debug_ranges an appropriate
14221      "base address".  Use zero so that these addresses become absolute.  */
14222   else if (have_location_lists || ranges_table_in_use)
14223     add_AT_addr (comp_unit_die, DW_AT_entry_pc, const0_rtx);
14224
14225   if (debug_info_level >= DINFO_LEVEL_NORMAL)
14226     add_AT_lbl_offset (comp_unit_die, DW_AT_stmt_list,
14227                        debug_line_section_label);
14228
14229   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14230     add_AT_lbl_offset (comp_unit_die, DW_AT_macro_info, macinfo_section_label);
14231
14232   /* Output all of the compilation units.  We put the main one last so that
14233      the offsets are available to output_pubnames.  */
14234   for (node = limbo_die_list; node; node = node->next)
14235     output_comp_unit (node->die, 0);
14236
14237   output_comp_unit (comp_unit_die, 0);
14238
14239   /* Output the abbreviation table.  */
14240   named_section_flags (DEBUG_ABBREV_SECTION, SECTION_DEBUG);
14241   output_abbrev_section ();
14242
14243   /* Output public names table if necessary.  */
14244   if (pubname_table_in_use)
14245     {
14246       named_section_flags (DEBUG_PUBNAMES_SECTION, SECTION_DEBUG);
14247       output_pubnames ();
14248     }
14249
14250   /* Output the address range information.  We only put functions in the arange
14251      table, so don't write it out if we don't have any.  */
14252   if (fde_table_in_use)
14253     {
14254       named_section_flags (DEBUG_ARANGES_SECTION, SECTION_DEBUG);
14255       output_aranges ();
14256     }
14257
14258   /* Output ranges section if necessary.  */
14259   if (ranges_table_in_use)
14260     {
14261       named_section_flags (DEBUG_RANGES_SECTION, SECTION_DEBUG);
14262       ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
14263       output_ranges ();
14264     }
14265
14266   /* Have to end the macro section.  */
14267   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14268     {
14269       named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
14270       dw2_asm_output_data (1, 0, "End compilation unit");
14271     }
14272
14273   /* If we emitted any DW_FORM_strp form attribute, output the string
14274      table too.  */
14275   if (debug_str_hash)
14276     htab_traverse (debug_str_hash, output_indirect_string, NULL);
14277 }
14278 #else
14279
14280 /* This should never be used, but its address is needed for comparisons.  */
14281 const struct gcc_debug_hooks dwarf2_debug_hooks;
14282
14283 #endif /* DWARF2_DEBUGGING_INFO */
14284
14285 #include "gt-dwarf2out.h"