OSDN Git Service

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