OSDN Git Service

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