OSDN Git Service

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