OSDN Git Service

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