OSDN Git Service

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